Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Harden NUT work with strings where dynamic formatting strings are used #2460

Draft
wants to merge 29 commits into
base: master
Choose a base branch
from

Conversation

jimklimov
Copy link
Member

@jimklimov jimklimov commented Jun 2, 2024

Closes: #2450

Builds upon the idea suggested in the issue above.

Impacts quite a lot of drivers and other code, so I initially plan on mile-stoning for a later release and asking the community about testing it in practice (after this seems okay with all systems of the NUT CI farm).

One way this can mis-fire is if some mapping tables (potentially) use different formatting strings like %d and %ld for some value stored in a long -- one of the sources deals with that by checking if the dynamic formatting string normalized to this or that and casts accordingly. Maybe this can be generalized. Test code is prepared for TDD-style development, if it comes to that. (Was already helpful for some scenarios)

Some locations were not modified, where varargs are used (at least currently) with fixed formatting strings, and static compilers do not smell anything fishy about this. A few cases can be currently simplified, where error messages etc. are thrown with only the "fixed" formatting string and no varargs. Not sure it should be done though (less flexibility later).

CC @arnaudquette-eaton : snmp-ups (primarily around daisy-chain support) is one of the impacted code-bases, testing with some ePDUs would be beneficial.

CC @ericclappier-eaton : this may impact or benefit your work too, pinging just in case :)

jimklimov added 16 commits June 1, 2024 22:49
…roduce snprintf_dynamic() and related methods [networkupstools#2450]

Mitigate the inherent insecurity of dynamically constructed formatting
strings vs. a fixed vararg list with its amounts and types of variables
printed by this or that method and pre-compiled in the program.

* minimize_formatting_string() with caller-specified buffer;
* minimize_formatting_string_staticbuf() for one-shot use-cases;
* validate_formatting_string() to compare a dynamic and expected formatting strings;
* vsnprintf_dynamic(), vsnprintfcat_dynamic() for practical applications (with fixed va_list argument);
* snprintf_dynamic(), snprintfcat_dynamic(), mkstr_dynamic() for practical applications (with ... variadic arguments);
* added vsnprintfcat() with fixed va_list argument, for good measure.

Signed-off-by: Jim Klimov <[email protected]>
…rsions for hardened dynamic format string support [networkupstools#2450]

Signed-off-by: Jim Klimov <[email protected]>
…llapse known different formats for same data type into same char to ease sanity-check comparisons [networkupstools#2450]

Signed-off-by: Jim Klimov <[email protected]>
….c: introduce verbosity option to validate_formatting_string() and minimize_formatting_string() [networkupstools#2450]

Signed-off-by: Jim Klimov <[email protected]>
…ch to snprintf_dynamic() instead of hushing potential flaws with macros [networkupstools#2450]

Signed-off-by: Jim Klimov <[email protected]>
…ad of hushing potential flaws with macros [networkupstools#2450]

Found by pragmas to clean up, with

    :; git grep -En 'Wformat-(sec|nonlit)'

Signed-off-by: Jim Klimov <[email protected]>
…operly cast the value, and harden with snprintf_dynamic() [networkupstools#2450]

Signed-off-by: Jim Klimov <[email protected]>
…t the odd conversion, and harden with snprintf_dynamic() [networkupstools#2450]

Signed-off-by: Jim Klimov <[email protected]>
….battery.start" might vary by applicable formatting strings [networkupstools#2450]

Signed-off-by: Jim Klimov <[email protected]>
…witching to snprintf_dynamic() instead of hushing potential flaws with macros [networkupstools#2450]

Signed-off-by: Jim Klimov <[email protected]>
…may produce invalid printf-style strings and not complain (garbage in = garbage out) [networkupstools#2450]

Signed-off-by: Jim Klimov <[email protected]>
…lerate dynamic formats that are sub-strings and beginnings of reference (wasteful but survivable) [networkupstools#2450]

Signed-off-by: Jim Klimov <[email protected]>
@jimklimov jimklimov added enhancement need testing Code looks reasonable, but the feature would better be tested against hardware or OSes ready / code review Author (and CI) consider the PR worthy of human rewievers' time nut-scanner SNMP USB CI Entries related to continuous integration infrastructure (historically also recipes like Makefiles) modbus Qx protocol driver Driver based on Megatec Q<number> such as new nutdrv_qx, or obsoleted blazer and some others serial port upsmon portability We want NUT to build and run everywhere possible upssched Questions and issues about upssched (timer helper for upsmon) C-str Issues and PRs about C/C++ methods, headers and data types dealing with strings and memory blocks labels Jun 2, 2024
@jimklimov jimklimov added this to the 2.8.4 milestone Jun 2, 2024
@AppVeyorBot
Copy link

@clepple clepple removed their request for review June 2, 2024 02:48
jimklimov added 5 commits June 2, 2024 12:49
…t-extra-args" in pragmas to quiesce "bogus-looking" test cases [networkupstools#2450]

Signed-off-by: Jim Klimov <[email protected]>
…x_blazer-common.c: define macros for minimize_formatting_string() and validate_formatting_string() verbosity argument values [networkupstools#2450]

Custom builds that do not want to require setting driver/tool debug levels
can re-define their NUT_DYNAMICFORMATTING_DEBUG_LEVEL to e.g. 0 and see
any formatting discrepancies instantly.

Signed-off-by: Jim Klimov <[email protected]>
@AppVeyorBot
Copy link

@jimklimov jimklimov marked this pull request as draft July 17, 2024 13:49
@jimklimov jimklimov marked this pull request as ready for review July 19, 2024 08:18
@jimklimov jimklimov marked this pull request as draft July 19, 2024 08:22
@jimklimov jimklimov marked this pull request as ready for review July 19, 2024 11:02
@jimklimov jimklimov marked this pull request as draft July 19, 2024 21:24
@jimklimov jimklimov marked this pull request as ready for review July 21, 2024 13:38
@jimklimov jimklimov marked this pull request as draft July 22, 2024 06:49
@jimklimov jimklimov marked this pull request as ready for review July 23, 2024 16:07
@jimklimov jimklimov marked this pull request as draft July 24, 2024 11:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-str Issues and PRs about C/C++ methods, headers and data types dealing with strings and memory blocks CI Entries related to continuous integration infrastructure (historically also recipes like Makefiles) enhancement modbus need testing Code looks reasonable, but the feature would better be tested against hardware or OSes nut-scanner portability We want NUT to build and run everywhere possible Qx protocol driver Driver based on Megatec Q<number> such as new nutdrv_qx, or obsoleted blazer and some others ready / code review Author (and CI) consider the PR worthy of human rewievers' time serial port SNMP upsmon upssched Questions and issues about upssched (timer helper for upsmon) USB
Projects
None yet
Development

Successfully merging this pull request may close these issues.

For the few cases where we use variables as formatting strings, find a way to ensure it is safe
2 participants