Clean-up: lttng-ctl: strnlen out of bounds access
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 15 Mar 2022 21:19:27 +0000 (17:19 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 16 Mar 2022 20:06:19 +0000 (16:06 -0400)
gcc 11.2 produces the following warning. The lttng_strncpy helper
assumes that 'src' is a null terminated string. As such, the use of a
string literal (of size 37) in this specific example is correct as
strnlen will not read beyond the null terminator.

Replacing strnlen by strlen eliminates this warning. strnlen was used to
short-circuit the source length check when it was larger than the
destination. This optimization is unlikely to matter. Pascal-style
strings should be used when string length computations are expected to
be prohibitively expensive.

In file included from ../../../src/common/macros.h:15,
                 from ../../../include/lttng/health-internal.h:18,
                 from lttng-ctl-health.cpp:19:
In function 'size_t lttng_strnlen(const char*, size_t)',
    inlined from 'int lttng_strncpy(char*, const char*, size_t)' at ../../../src/common/macros.h:123:19,
    inlined from 'int set_health_socket_path(lttng_health*, int)' at lttng-ctl-health.cpp:198:22,
    inlined from 'int lttng_health_query(lttng_health*)' at lttng-ctl-health.cpp:319:30:
../../../src/common/compat/string.h:19:23: warning: 'size_t strnlen(const char*, size_t)' specified bound 4096 may exceed source size 37 [-Wstringop-overread]
   19 |         return strnlen(str, max);
      |                ~~~~~~~^~~~~~~~~~

Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
Change-Id: I290109433fcae7073321f1b48ecfbb2ec6e4ad26

src/common/macros.h

index 6b8eaf711df21e99612956d1723606bca01c1dff..8c35f0c5f3a297144df3259d084518c63f55e732 100644 (file)
@@ -115,12 +115,14 @@ void *zmalloc(size_t len)
  * It checks that the @src string fits into @dst_len before performing
  * the copy. On failure, no copy has been performed.
  *
+ * Assumes that 'src' is null-terminated.
+ *
  * dst_len includes the string's trailing NULL.
  */
 static inline
 int lttng_strncpy(char *dst, const char *src, size_t dst_len)
 {
-       if (lttng_strnlen(src, dst_len) >= dst_len) {
+       if (strlen(src) >= dst_len) {
                /* Fail since copying would result in truncation. */
                return -1;
        }
This page took 0.025369 seconds and 4 git commands to generate.