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

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