Skip to content

Commit

Permalink
Harden NUT work with strings: comment which use-cases we DID NOT swit…
Browse files Browse the repository at this point in the history
…ch to snprintf_dynamic() instead of hushing potential flaws with macros [networkupstools#2450]

Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com>
  • Loading branch information
jimklimov committed Jun 2, 2024
1 parent 8f3f609 commit 5a3986e
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 1 deletion.
6 changes: 6 additions & 0 deletions clients/upsimage.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,12 @@ static void noimage(const char *fmt, ...)
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_SECURITY
#pragma GCC diagnostic ignored "-Wformat-security"
#endif
/* Note: Not converting to hardened NUT methods with dynamic
* format string checking, this one is used locally with
* fixed strings (and args) */
/* FIXME: Actually, almost only fixed strings, no formatting
* needed here: one use-case of having a format, and another
* with externally prepared snprintf(). */
vsnprintf(msg, sizeof(msg), fmt, ap);
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
#pragma GCC diagnostic pop
Expand Down
4 changes: 4 additions & 0 deletions clients/upssched.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,10 @@ static int send_to_one(conn_t *conn, const char *fmt, ...)
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_SECURITY
#pragma GCC diagnostic ignored "-Wformat-security"
#endif
/* Note: Not converting to hardened NUT methods with dynamic
* format string checking, this one is used locally with
* fixed strings (and args) */
/* FIXME: Actually, only fixed strings, no formatting here. */
vsnprintf(buf, sizeof(buf), fmt, ap);
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
#pragma GCC diagnostic pop
Expand Down
3 changes: 3 additions & 0 deletions clients/upsset.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ static void error_page(const char *next, const char *title,
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_SECURITY
#pragma GCC diagnostic ignored "-Wformat-security"
#endif
/* Note: Not converting to hardened NUT methods with dynamic
* format string checking, this one is used locally with
* fixed strings (and args) quite extensively. */
vsnprintf(msg, sizeof(msg), fmt, ap);
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
#pragma GCC diagnostic pop
Expand Down
3 changes: 3 additions & 0 deletions drivers/belkinunv.c
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,9 @@ static void updatestatus(int smode, const char *fmt, ...) {
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_SECURITY
#pragma GCC diagnostic ignored "-Wformat-security"
#endif
/* Note: Not converting to hardened NUT methods with dynamic
* format string checking, this one is used locally with
* fixed strings (and args) a few times */
vsnprintf(buf, sizeof(buf), fmt, ap);
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
#pragma GCC diagnostic pop
Expand Down
3 changes: 3 additions & 0 deletions drivers/bestfortress.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ static int upssend(const char *fmt,...) {
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_SECURITY
#pragma GCC diagnostic ignored "-Wformat-security"
#endif
/* Note: Not converting to hardened NUT methods with dynamic
* format string checking, this one is used locally with
* fixed strings (and args) */
ret = vsnprintf(buf, sizeof(buf), fmt, ap);
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
#pragma GCC diagnostic pop
Expand Down
5 changes: 4 additions & 1 deletion drivers/mge-utalk.c
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ static void extract_info(const char *buf, const mge_info_item_t *item,
NOTE: MGE counts bytes/chars the opposite way as C,
see mge-utalk manpage. If status commands send two
data items, these are separated by a space, so
the elements of the second item are in buf[16..9].
the elements of the second item are in buf[16..9].
*/

static int get_ups_status(void)
Expand Down Expand Up @@ -903,6 +903,9 @@ static ssize_t mge_command(char *reply, size_t replylen, const char *fmt, ...)
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_SECURITY
#pragma GCC diagnostic ignored "-Wformat-security"
#endif
/* Note: Not converting to hardened NUT methods with dynamic
* format string checking, this one is used locally with
* fixed strings (and args) quite intensively */
ret = vsnprintf(command, sizeof(command), fmt, ap);
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
#pragma GCC diagnostic pop
Expand Down
4 changes: 4 additions & 0 deletions drivers/nutdrv_atcl_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ static void usb_comm_fail(const char *fmt, ...)
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_SECURITY
#pragma GCC diagnostic ignored "-Wformat-security"
#endif
/* Note: Not converting to hardened NUT methods with dynamic
* format string checking, this one is used locally with
* fixed strings (and args) */
/* FIXME: Actually, only fixed strings, no formatting here. */
ret = vsnprintf(why, sizeof(why), fmt, ap);
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
#pragma GCC diagnostic pop
Expand Down
4 changes: 4 additions & 0 deletions drivers/richcomm_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ static void usb_comm_fail(const char *fmt, ...)
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_SECURITY
#pragma GCC diagnostic ignored "-Wformat-security"
#endif
/* Note: Not converting to hardened NUT methods with dynamic
* format string checking, this one is used locally with
* fixed strings (and args) */
/* FIXME: Actually, only fixed strings, no formatting here. */
ret = vsnprintf(why, sizeof(why), fmt, ap);
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
#pragma GCC diagnostic pop
Expand Down
21 changes: 21 additions & 0 deletions drivers/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,9 @@ static ssize_t send_formatted(TYPE_FD_SER fd, const char *fmt, va_list va, useco
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_SECURITY
#pragma GCC diagnostic ignored "-Wformat-security"
#endif
/* Note: Not converting to hardened NUT methods with dynamic
* format string checking, technically this one is only used
* locally with args whose validity other methods may check */
ret = vsnprintf(buf, sizeof(buf), fmt, va);
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
#pragma GCC diagnostic pop
Expand Down Expand Up @@ -384,6 +387,12 @@ ssize_t ser_send_pace(TYPE_FD_SER fd, useconds_t d_usec, const char *fmt, ...)
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_SECURITY
#pragma GCC diagnostic ignored "-Wformat-security"
#endif
/* Note: Not converting to hardened NUT methods with dynamic
* format string checking, this one is used from drivers with
* fixed strings (and args).
* TODO: Propose a ser_send_pace_dynamic() in case non-static
* format strings appear? Currently there are none.
*/
ret = send_formatted(fd, fmt, ap, d_usec);
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
#pragma GCC diagnostic pop
Expand Down Expand Up @@ -411,6 +420,12 @@ ssize_t ser_send(TYPE_FD_SER fd, const char *fmt, ...)
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_SECURITY
#pragma GCC diagnostic ignored "-Wformat-security"
#endif
/* Note: Not converting to hardened NUT methods with dynamic
* format string checking, this one is used from drivers with
* fixed strings (and args).
* TODO: Propose a ser_send_dynamic() in case non-static
* format strings appear? Currently there are none.
*/
ret = send_formatted(fd, fmt, ap, 0);
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
#pragma GCC diagnostic pop
Expand Down Expand Up @@ -606,6 +621,12 @@ void ser_comm_fail(const char *fmt, ...)
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_SECURITY
#pragma GCC diagnostic ignored "-Wformat-security"
#endif
/* Note: Not converting to hardened NUT methods with dynamic
* format string checking, this one is used from drivers with
* fixed strings (and args).
* TODO: Propose a ser_comm_fail_dynamic() in case non-static
* format strings appear? Currently there are none.
*/
ret = vsnprintf(why, sizeof(why), fmt, ap);
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
#pragma GCC diagnostic pop
Expand Down

0 comments on commit 5a3986e

Please sign in to comment.