Blame SOURCES/0178-util-add-offs-sanity-check-to-print_clock_t.patch

b48781
From 6d3e97e83a7d61cbb2f5109efb4b519383a55712 Mon Sep 17 00:00:00 2001
b48781
From: Eugene Syromyatnikov <evgsyr@gmail.com>
b48781
Date: Tue, 28 Jun 2022 16:55:49 +0200
b48781
Subject: [PATCH] util: add offs sanity check to print_clock_t
b48781
b48781
While it is not strictly needed right now, the code that uses
b48781
the calculated offs value lacks any checks for possible buf overruns,
b48781
which is not defensive enough, so let's add them.  Reported by covscan:
b48781
b48781
    Error: OVERRUN (CWE-119):
b48781
    strace-5.18/src/util.c:248: assignment: Assigning:
b48781
    "offs" = "ilog10(val / clk_tck)". The value of "offs" is now between
b48781
    16 and 31 (inclusive).
b48781
    strace-5.18/src/util.c:249: overrun-local: Overrunning array of 30 bytes
b48781
    at byte offset 31 by dereferencing pointer "buf + offs". [Note: The source
b48781
    code implementation of the function has been overridden by a builtin model.]
b48781
b48781
    Error: OVERRUN (CWE-119):
b48781
    strace-5.18/src/util.c:248: assignment: Assigning:
b48781
    "offs" = "ilog10(val / clk_tck)". The value of "offs" is now between
b48781
    16 and 31 (inclusive).
b48781
    strace-5.18/src/util.c:253: overrun-buffer-arg: Overrunning array "buf"
b48781
    of 30 bytes by passing it to a function which accesses it at byte offset
b48781
    32 using argument "offs + 2UL" (which evaluates to 33). [Note: The source
b48781
    code implementation of the function has been overridden by a builtin model.]
b48781
b48781
    Error: OVERRUN (CWE-119):
b48781
    strace-5.18/src/util.c:248: assignment: Assigning:
b48781
    "offs" = "ilog10(val / clk_tck)". The value of "offs" is now between
b48781
    16 and 31 (inclusive).
b48781
    strace-5.18/src/util.c:254: overrun-local: Overrunning array "buf"
b48781
    of 30 bytes at byte offset 32 using index "offs + 1UL" (which evaluates
b48781
    to 32).
b48781
b48781
* src/util.c (print_clock_t): Add check that offs is small enough
b48781
for it and "offs + 2" not to overrun buf.
b48781
---
b48781
 src/util.c | 8 ++++++++
b48781
 1 file changed, 8 insertions(+)
b48781
b48781
diff --git a/src/util.c b/src/util.c
b48781
index 5f87acb..93aa7b3 100644
b48781
--- a/src/util.c
b48781
+++ b/src/util.c
b48781
@@ -246,6 +246,14 @@ print_clock_t(uint64_t val)
b48781
 		 */
b48781
 		char buf[sizeof(uint64_t) * 3 + sizeof("0.0 s")];
b48781
 		size_t offs = ilog10(val / clk_tck);
b48781
+		/*
b48781
+		 * This check is mostly to appease covscan, which thinks
b48781
+		 * that offs can go as high as 31 (it cannot), but since
b48781
+		 * there is no proper sanity checks against offs overrunning
b48781
+		 * buf down the code, it may as well be here.
b48781
+		 */
b48781
+		if (offs > (sizeof(buf) - sizeof("0.0 s")))
b48781
+			return;
b48781
 		int ret = snprintf(buf + offs, sizeof(buf) - offs, "%.*f s",
b48781
 				   frac_width,
b48781
 				   (double) (val % clk_tck) / clk_tck);
b48781
-- 
b48781
2.1.4
b48781