Skip to content

Commit 931674f

Browse files
committed
Merge remote-tracking branch 'origin/issue-2450'
Closes: PR networkupstools#2460 Signed-off-by: Jim Klimov <[email protected]>
2 parents 1b7e7ad + 1ff5d46 commit 931674f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1140
-1016
lines changed

NEWS.adoc

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ https://github.com/networkupstools/nut/milestone/9
4949
* Refactored NUT "common" sources to reference `nut_version.h` macros from
5050
a smaller C source file, to minimize the compilation unit size impacted
5151
by development iterations. [issue #2097]
52+
* Common code hardening: added sanity-checking for dynamically constructed
53+
or selected formatting strings with variable-argument list methods
54+
(typically used with log printing, `dstate` setting, etc.) [#2450]
5255

5356

5457
Release notes for NUT 2.8.3 - what's new since 2.8.2

clients/upsclient.c

+15-39
Original file line numberDiff line numberDiff line change
@@ -567,16 +567,6 @@ const char *upscli_strerror(UPSCONN_t *ups)
567567
char sslbuf[UPSCLI_ERRBUF_LEN];
568568
#endif
569569

570-
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
571-
#pragma GCC diagnostic push
572-
#endif
573-
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
574-
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
575-
#endif
576-
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_SECURITY
577-
#pragma GCC diagnostic ignored "-Wformat-security"
578-
#endif
579-
580570
if (!ups) {
581571
return upscli_errlist[UPSCLI_ERR_INVALIDARG].str;
582572
}
@@ -595,23 +585,26 @@ const char *upscli_strerror(UPSCONN_t *ups)
595585
return upscli_errlist[ups->upserror].str;
596586

597587
case 1: /* add message from system's strerror */
598-
snprintf(ups->errbuf, UPSCLI_ERRBUF_LEN,
588+
snprintf_dynamic(
589+
ups->errbuf, UPSCLI_ERRBUF_LEN,
599590
upscli_errlist[ups->upserror].str,
600-
strerror(ups->syserrno));
591+
"%s", strerror(ups->syserrno));
601592
return ups->errbuf;
602593

603594
case 2: /* SSL error */
604595
#ifdef WITH_OPENSSL
605596
err = ERR_get_error();
606597
if (err) {
607598
ERR_error_string(err, sslbuf);
608-
snprintf(ups->errbuf, UPSCLI_ERRBUF_LEN,
599+
snprintf_dynamic(
600+
ups->errbuf, UPSCLI_ERRBUF_LEN,
609601
upscli_errlist[ups->upserror].str,
610-
sslbuf);
602+
"%s", sslbuf);
611603
} else {
612-
snprintf(ups->errbuf, UPSCLI_ERRBUF_LEN,
604+
snprintf_dynamic(
605+
ups->errbuf, UPSCLI_ERRBUF_LEN,
613606
upscli_errlist[ups->upserror].str,
614-
"peer disconnected");
607+
"%s", "peer disconnected");
615608
}
616609
#elif defined(WITH_NSS) /* WITH_OPENSSL */
617610
if (PR_GetErrorTextLength() < UPSCLI_ERRBUF_LEN) {
@@ -628,19 +621,16 @@ const char *upscli_strerror(UPSCONN_t *ups)
628621
return ups->errbuf;
629622

630623
case 3: /* parsing (parseconf) error */
631-
snprintf(ups->errbuf, UPSCLI_ERRBUF_LEN,
624+
snprintf_dynamic(
625+
ups->errbuf, UPSCLI_ERRBUF_LEN,
632626
upscli_errlist[ups->upserror].str,
633-
ups->pc_ctx.errmsg);
627+
"%s", ups->pc_ctx.errmsg);
634628
return ups->errbuf;
635629

636630
default:
637631
break;
638632
}
639633

640-
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
641-
#pragma GCC diagnostic pop
642-
#endif
643-
644634
/* fallthrough */
645635

646636
snprintf(ups->errbuf, UPSCLI_ERRBUF_LEN, "Unknown error flag %d",
@@ -1392,23 +1382,9 @@ static void build_cmd(char *buf, size_t bufsize, const char *cmdname,
13921382
format = " %s";
13931383
}
13941384

1395-
/* snprintfcat would tie us to common */
1396-
1397-
len = strlen(buf);
1398-
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
1399-
#pragma GCC diagnostic push
1400-
#endif
1401-
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
1402-
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
1403-
#endif
1404-
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_SECURITY
1405-
#pragma GCC diagnostic ignored "-Wformat-security"
1406-
#endif
1407-
snprintf(buf + len, bufsize - len, format,
1408-
pconf_encode(arg[i], enc, sizeof(enc)));
1409-
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
1410-
#pragma GCC diagnostic pop
1411-
#endif
1385+
snprintfcat_dynamic(
1386+
buf, bufsize, format,
1387+
"%s", pconf_encode(arg[i], enc, sizeof(enc)));
14121388
}
14131389

14141390
len = strlen(buf);

clients/upsimage.c

+10-14
Original file line numberDiff line numberDiff line change
@@ -278,20 +278,10 @@ static void drawbar(
278278
gdImageFilledRectangle(im, 25, bar_y, width - 25, scale_height,
279279
bar_color);
280280

281-
/* stick the text version of the value at the bottom center */
282-
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
283-
#pragma GCC diagnostic push
284-
#endif
285-
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
286-
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
287-
#endif
288-
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_SECURITY
289-
#pragma GCC diagnostic ignored "-Wformat-security"
290-
#endif
291-
snprintf(text, sizeof(text), format, value);
292-
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
293-
#pragma GCC diagnostic pop
294-
#endif
281+
/* stick the text version of the value at the bottom center
282+
* expected format is one of imgvar[] entries for "double value"
283+
*/
284+
snprintf_dynamic(text, sizeof(text), format, "%f", value);
295285
gdImageString(im, gdFontMediumBold,
296286
(width - (int)(strlen(text))*gdFontMediumBold->w)/2,
297287
height - gdFontMediumBold->h,
@@ -324,6 +314,12 @@ static void noimage(const char *fmt, ...)
324314
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_SECURITY
325315
#pragma GCC diagnostic ignored "-Wformat-security"
326316
#endif
317+
/* Note: Not converting to hardened NUT methods with dynamic
318+
* format string checking, this one is used locally with
319+
* fixed strings (and args) */
320+
/* FIXME: Actually, almost only fixed strings, no formatting
321+
* needed here: one use-case of having a format, and another
322+
* with externally prepared snprintf(). */
327323
vsnprintf(msg, sizeof(msg), fmt, ap);
328324
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
329325
#pragma GCC diagnostic pop

clients/upsmon.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ static void do_notify(const utype_t *ups, unsigned int ntype, const char *extra)
405405
upsdebugx(2, "%s: ntype 0x%04x (%s)",
406406
__func__, ntype,
407407
notifylist[i].name);
408+
408409
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
409410
#pragma GCC diagnostic push
410411
#endif
@@ -427,13 +428,15 @@ static void do_notify(const utype_t *ups, unsigned int ntype, const char *extra)
427428
* only have one "%s", plaster another
428429
* to "msgfmt" and follow this path?
429430
*/
430-
snprintf(msg, sizeof(msg),
431+
snprintf_dynamic(msg, sizeof(msg),
431432
msgfmt,
433+
"%s%s",
432434
upsname ? upsname : "",
433435
NUT_STRARG(extra));
434436
} else {
435-
snprintf(msg, sizeof(msg),
437+
snprintf_dynamic(msg, sizeof(msg),
436438
msgfmt,
439+
"%s",
437440
upsname ? upsname : "");
438441
}
439442
} else {

clients/upssched.c

+4
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,10 @@ static int send_to_one(conn_t *conn, const char *fmt, ...)
413413
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_SECURITY
414414
#pragma GCC diagnostic ignored "-Wformat-security"
415415
#endif
416+
/* Note: Not converting to hardened NUT methods with dynamic
417+
* format string checking, this one is used locally with
418+
* fixed strings (and args) */
419+
/* FIXME: Actually, only fixed strings, no formatting here. */
416420
vsnprintf(buf, sizeof(buf), fmt, ap);
417421
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
418422
#pragma GCC diagnostic pop

clients/upsset.c

+3
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ static void error_page(const char *next, const char *title,
281281
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_SECURITY
282282
#pragma GCC diagnostic ignored "-Wformat-security"
283283
#endif
284+
/* Note: Not converting to hardened NUT methods with dynamic
285+
* format string checking, this one is used locally with
286+
* fixed strings (and args) quite extensively. */
284287
vsnprintf(msg, sizeof(msg), fmt, ap);
285288
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
286289
#pragma GCC diagnostic pop

0 commit comments

Comments
 (0)