-
-
Notifications
You must be signed in to change notification settings - Fork 359
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
For the few cases where we use variables as formatting strings, find a way to ensure it is safe #2450
Comments
…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]>
…rdened *_dynamic() string methods [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]>
…valid use-case for vsnprintf() [networkupstools#2450] Signed-off-by: Jim Klimov <[email protected]>
… an unknown model [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]>
…rmat-extra-args" [networkupstools#2450] Signed-off-by: Jim Klimov <[email protected]>
…t-extra-args" in pragmas to quiesce "bogus-looking" test cases [networkupstools#2450] Signed-off-by: Jim Klimov <[email protected]>
Not sure if relevant, but on GCC you can add typechecking by adding the following attribute to the function declaration:
In GLib it is used extensively through the |
Sorry, I just seen you are already using it. |
Thanks! Not sure how portable this is, but the attribute is actually used in Probably that helps clang and gcc raise the compile-time warnings, and this is what one part of this solution relies on with the "reference" formatting strings (the fallback being that they can be checked by a human to match any subsequent actual string/numeric/... arguments). |
…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]>
…nt [networkupstools#2511, networkupstools#2431, networkupstools#2450 et al] Earlier bump went to "patch" component, but here we actually have an API expansion (and more exported symbols), so the more important component should be bumped. Signed-off-by: Jim Klimov <[email protected]>
In a few cases we use formatting strings as variables (e.g. coming from some tables or even constructed at run-time) which is error-prone with regard to interpretation of subsequent memory stack when calling a
printf
-related method. While this is done only for strings defined in NUT codebase, it has a potential to regress if someone modifies the table value in some later revision. Currently we hush a warning likeformat not a string literal and no format arguments
and a few similar others with pragmas, but there gotta be some better way.Some first ideas (more welcome):
pycparser
as used elsewhere? simplerperl
magic?) to statically check that the amount of unescaped percent characters in the resolved first argument (at least when fixed from tables) matches the macro hint amount;fatalx()
or similar and not do dangerous things - at least in our methods likeupsdebugx()
ordstate_setinfo()
, we can at least control thevarargc
vs. amount of percents in the actual formatting string.Either way, I think we lose the facility of modern compilers to also statically check the types (that a
%i
refers to anint
-sized number, and not along
orchar*
, etc.) in these cases, so some error-proneness remains even if the amount of args remains but their type changes.Maybe the solution to get the best of all worlds could be in fact to specify the runtime method to return a string (so callers would go like
dstate_setinfo("ups.model", "%s", checked_format(variableFormat, checkingFormat, ...));
and avoid hushing pragmas altogether) withcheckingFormat
being a real formatting string like"%s%s%i%"PRIuSIZE"%f"
according to the types of subsequent vararg parameters, and the precedingvariableFormat
argument specifying the actual formatting string (expected/checked to mention exactly the same set of percent-formats in same order).This way we could have compile-time checks that varargs conform in amount and type to some contrived formatting string, and run-time assertions that whatever variable string we actually use to produce the
checked_format()
string is compatible with those expectations.Is there some (library?) method to strip non-formatting characters (plain text, format beautification with sizes/alignments like
%.01f
=>%f
or whatever) so we could directlystrcmp()
the expected pattern vs. the stripped dynamic formatting string? If not, we have a fallbackprintf()
implementation that I guess could be wrangled into such a helper method...The text was updated successfully, but these errors were encountered: