Skip to contentSkip to conversation

selftests/bpf: Fix strncpy() fortify warning

PATCH In review Rong Tao 5 participants 17 messages Oct 27, 2022 – Nov 4, 2022

Subsystems (5)
Kselftest BPF BPF selftests libbpf +1 more subsystems
lore
Share & tools
lore

Rong Tao

author
#0 lore raw

PATCH 1/1 selftests/bpf: Fix strncpy() fortify warning

From: Rong Tao <[email protected]>

Compile samples/bpf, error:

$ cd samples/bpf
$ make
...
In function ‘__enable_controllers’:
samples/bpf/../../tools/testing/selftests/bpf/cgroup_helpers.c:80:17: warning: ‘strncpy’ specified bound 4097 equals destination size [-Wstringop-truncation]
   80 |                 strncpy(enable, controllers, sizeof(enable));
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 tools/testing/selftests/bpf/cgroup_helpers.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/cgroup_helpers.c +1 -1
index e914cc45b766..a70e873b267e 100644
@@ -77,7 +77,7 @@ static int __enable_controllers(const char *cgroup_path, const char *controllers
 		enable[len] = 0;
 		close(fd);
 	} else {
-		strncpy(enable, controllers, sizeof(enable));
+		strncpy(enable, controllers, sizeof(enable) - 1);
 	}
 
 	snprintf(path, sizeof(path), "%s/cgroup.subtree_control", cgroup_path);
Signature · 1 line
2.31.1
#1 lore raw
Quoted 22 lines · Rong Tao

From: Rong Tao <[email protected]>

Compile samples/bpf, error:

$ cd samples/bpf
$ make
...
In function ‘__enable_controllers’:
samples/bpf/../../tools/testing/selftests/bpf/cgroup_helpers.c:80:17: warning: ‘strncpy’ specified bound 4097 equals destination size [-Wstringop-truncation]
   80 |                 strncpy(enable, controllers, sizeof(enable));
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 tools/testing/selftests/bpf/cgroup_helpers.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/cgroup_helpers.c +0 -0
index e914cc45b766..a70e873b267e 100644

tools/testing/selftests/bpf/cgroup_helpers.c static int __enable_controllers(const char *cgroup_path, const char *controllers · line 80

Quoted 5 lines
tools/testing/selftests/bpf/cgroup_helpers.c +1 -1
@@ -77,7 +77,7 @@ static int __enable_controllers(const char *cgroup_path, const char *controllers
                enable[len] = 0;
                close(fd);
        } else {
-               strncpy(enable, controllers, sizeof(enable));
+               strncpy(enable, controllers, sizeof(enable) - 1);

enable is not initialized, so we might end up with non-zero-terminated string. Let's enable[0] = '\0'; at the beginning and then strncat() here?

Quoted 6 lines
tools/testing/selftests/bpf/cgroup_helpers.c +0 -1
        }
 
        snprintf(path, sizeof(path), "%s/cgroup.subtree_control", cgroup_path);
--

2.31.1

author
#2 lore raw

Thanks for your reply, enable[0] = '\0'; at the beginning and then strncat() still has the same compile warning

tools/testing/selftests/bpf/cgroup_helpers.c +2 -1
@@ -77,7 +77,8 @@ static int __enable_controllers(const char *cgroup_path, const char *controllers
                enable[len] = 0;
                close(fd);
        } else {
-               strncpy(enable, controllers, sizeof(enable));
+               enable[0] = '\0';
+               strncat(enable, controllers, sizeof(enable));
        }
 

In function ‘__enable_controllers’:
tools/testing/selftests/bpf/cgroup_helpers.c:81:17: warning: ‘strncat’ specified bound 4097 equals destination size [-Wstringop-truncation]

   81 |                 strncat(enable, controllers, sizeof(enable));
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

tools/testing/selftests/bpf/cgroup_helpers.c:81:17: warning: ‘strncat’ specified bound 4097 equals destination size [-Wstringop-overflow=]

So, i think just add '-1' for strncpy() is a good way.

#3 lore raw
Quoted 17 lines · Rong Tao

Thanks for your reply, enable[0] = '\0'; at the beginning and then strncat() still has the same compile warning

tools/testing/selftests/bpf/cgroup_helpers.c +2 -1
@@ -77,7 +77,8 @@ static int __enable_controllers(const char *cgroup_path, const char *controllers
                enable[len] = 0;
                close(fd);
        } else {
-               strncpy(enable, controllers, sizeof(enable));
+               enable[0] = '\0';
+               strncat(enable, controllers, sizeof(enable));
        }
 

In function ‘__enable_controllers’:
tools/testing/selftests/bpf/cgroup_helpers.c:81:17: warning: ‘strncat’ specified bound 4097 equals destination size [-Wstringop-truncation]

Quoted 5 lines
   81 |                 strncat(enable, controllers, sizeof(enable));
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

tools/testing/selftests/bpf/cgroup_helpers.c:81:17: warning: ‘strncat’ specified bound 4097 equals destination size [-Wstringop-overflow=]

So, i think just add '-1' for strncpy() is a good way.

no, it's not, see my previous email about ending up with non-zero-terminated C string.

check strncat() API, it leaves the dst string zero terminated, and yes, you need -1 for strncat as well, your compiler is right

author
#4 lore raw

PATCH 1/1 selftests/bpf: Fix strncpy() fortify warning

From: Rong Tao <[email protected]>

Replace strncpy() with strncat(), strncat() leaves the dst string zero terminated. Compile samples/bpf warning:

$ cd samples/bpf
$ make
...
In function ‘__enable_controllers’:
samples/bpf/../../tools/testing/selftests/bpf/cgroup_helpers.c:80:17: warning: ‘strncpy’ specified bound 4097 equals destination size [-Wstringop-truncation]
   80 |                 strncpy(enable, controllers, sizeof(enable));
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 tools/testing/selftests/bpf/cgroup_helpers.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/cgroup_helpers.c +2 -1
index a70e873b267e..912e6522c7c5 100644
@@ -77,7 +77,8 @@ static int __enable_controllers(const char *cgroup_path, const char *controllers
 		enable[len] = 0;
 		close(fd);
 	} else {
-		strncpy(enable, controllers, sizeof(enable) - 1);
+		enable[0] = '\0';
+		strncat(enable, controllers, sizeof(enable) - 1);
 	}
 
 	snprintf(path, sizeof(path), "%s/cgroup.subtree_control", cgroup_path);
Signature · 1 line
2.31.1
#5 lore raw

From: Rong Tao

Quoted 6 lines

Sent: 29 October 2022 04:00

From: Rong Tao <[email protected]>

Replace strncpy() with strncat(), strncat() leaves the dst string zero terminated. Compile samples/bpf warning:

This only makes a difference because tests for strncat() have not been added. srtncat() really is never the string function you are looking for.

David

Quoted 31 lines
$ cd samples/bpf
$ make
...
In function ‘__enable_controllers’:
samples/bpf/../../tools/testing/selftests/bpf/cgroup_helpers.c:80:17: warning: ‘strncpy’ specified
bound 4097 equals destination size [-Wstringop-truncation]
   80 |                 strncpy(enable, controllers, sizeof(enable));
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 tools/testing/selftests/bpf/cgroup_helpers.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/cgroup_helpers.c +2 -2
index a70e873b267e..912e6522c7c5 100644
@@ -77,7 +77,8 @@ static int __enable_controllers(const char *cgroup_path, const char *controllers
 		enable[len] = 0;
 		close(fd);
 	} else {
-		strncpy(enable, controllers, sizeof(enable) - 1);
+		enable[0] = '\0';
+		strncat(enable, controllers, sizeof(enable) - 1);
 	}
 
 	snprintf(path, sizeof(path), "%s/cgroup.subtree_control", cgroup_path);
--
Quoted 1 line

2.31.1

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

author
#8 lore raw

PATCH 1/1 selftests/bpf: Fix strncpy() fortify warning

From: Rong Tao <[email protected]>

move libbpf_strlcpy() to bpf_util.h, and replace strncpy() with libbpf_strlcpy(), fix compile warning.

Compile samples/bpf, warning:

$ cd samples/bpf
$ make
...
cgroup_helpers.c: In function ‘__enable_controllers’:
cgroup_helpers.c:80:17: warning: ‘strncpy’ specified bound 4097 equals destination size [-Wstringop-truncation]
   80 |                 strncpy(enable, controllers, sizeof(enable));
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 tools/testing/selftests/bpf/bpf_util.h       | 19 +++++++++++++++++++
 tools/testing/selftests/bpf/cgroup_helpers.c |  3 ++-
 tools/testing/selftests/bpf/xsk.c            | 19 -------------------
 3 files changed, 21 insertions(+), 20 deletions(-)
tools/testing/selftests/bpf/bpf_util.h +19 -0
index a3352a64c067..bf78212ff6e9 100644
@@ -20,6 +20,25 @@ static inline unsigned int bpf_num_possible_cpus(void)
 	return possible_cpus;
 }
 
+/* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst
+ * is zero-terminated string no matter what (unless sz == 0, in which case
+ * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs
+ * in what is returned. Given this is internal helper, it's trivial to extend
+ * this, when necessary. Use this instead of strncpy inside libbpf source code.
+ */
+static inline void libbpf_strlcpy(char *dst, const char *src, size_t sz)
+{
+	size_t i;
+
+	if (sz == 0)
+		return;
+
+	sz--;
+	for (i = 0; i < sz && src[i]; i++)
+		dst[i] = src[i];
+	dst[i] = '\0';
+}
+
 #define __bpf_percpu_val_align	__attribute__((__aligned__(8)))
 
 #define BPF_DECLARE_PERCPU(type, name)				\
tools/testing/selftests/bpf/cgroup_helpers.c +2 -1
index e914cc45b766..e33b70e509da 100644
@@ -13,6 +13,7 @@
 #include <ftw.h>
 
 #include "cgroup_helpers.h"
+#include "bpf_util.h"
 
 /*
  * To avoid relying on the system setup, when setup_cgroup_env is called
@@ -77,7 +78,7 @@ static int __enable_controllers(const char *cgroup_path, const char *controllers
 		enable[len] = 0;
 		close(fd);
 	} else {
-		strncpy(enable, controllers, sizeof(enable));
+		libbpf_strlcpy(enable, controllers, sizeof(enable));
 	}
 
 	snprintf(path, sizeof(path), "%s/cgroup.subtree_control", cgroup_path);
tools/testing/selftests/bpf/xsk.c +0 -19
index 0b3ff49c740d..4b6890e2a0a9 100644
@@ -521,25 +521,6 @@ static int xsk_create_bpf_link(struct xsk_socket *xsk)
 	return 0;
 }
 
-/* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst
- * is zero-terminated string no matter what (unless sz == 0, in which case
- * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs
- * in what is returned. Given this is internal helper, it's trivial to extend
- * this, when necessary. Use this instead of strncpy inside libbpf source code.
- */
-static inline void libbpf_strlcpy(char *dst, const char *src, size_t sz)
-{
-        size_t i;
-
-        if (sz == 0)
-                return;
-
-        sz--;
-        for (i = 0; i < sz && src[i]; i++)
-                dst[i] = src[i];
-        dst[i] = '\0';
-}
-
 static int xsk_get_max_queues(struct xsk_socket *xsk)
 {
 	struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
Signature · 1 line
2.31.1
maintainer
#9 lore raw
Quoted 5 lines · Rong Tao

From: Rong Tao <[email protected]>

move libbpf_strlcpy() to bpf_util.h, and replace strncpy() with libbpf_strlcpy(), fix compile warning.

Quoted 9 lines

Compile samples/bpf, warning:

$ cd samples/bpf
$ make
...
cgroup_helpers.c: In function ‘__enable_controllers’:
cgroup_helpers.c:80:17: warning: ‘strncpy’ specified bound 4097 equals destination size [-Wstringop-truncation]
    80 |                 strncpy(enable, controllers, sizeof(enable));
       |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

BPF CI now throws a new warning after your patch:

https://github.com/kernel-patches/bpf/actions/runs/3369416153/jobs/5589052613

[...]
xsk.c:536:2: error: call to undeclared function 'libbpf_strlcpy'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]

          libbpf_strlcpy(ifr.ifr_name, ctx->ifname, IFNAMSIZ);
          ^
  xsk.c:755:2: error: call to undeclared function 'libbpf_strlcpy'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
          libbpf_strlcpy(ctx->ifname, ifname, IFNAMSIZ);
          ^
  xsk.c:942:2: error: call to undeclared function 'libbpf_strlcpy'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
          libbpf_strlcpy(ctx->ifname, ifname, IFNAMSIZ);
          ^

3 errors generated.
make: *** [Makefile:166: /tmp/work/bpf/bpf/tools/testing/selftests/bpf/xsk.o] Error 1
make: *** Waiting for unfinished jobs....

author
#10 lore raw

PATCH 1/1 selftests/bpf: Fix strncpy() fortify warning

From: Rong Tao <[email protected]>

move libbpf_strlcpy() to bpf_util.h, and replace strncpy() with libbpf_strlcpy(), fix compile warning.

Compile samples/bpf, warning:

$ cd samples/bpf
$ make
...
cgroup_helpers.c: In function ‘__enable_controllers’:
cgroup_helpers.c:80:17: warning: ‘strncpy’ specified bound 4097 equals destination size [-Wstringop-truncation]
   80 |                 strncpy(enable, controllers, sizeof(enable));
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 tools/testing/selftests/bpf/bpf_util.h       | 19 +++++++++++++++++++
 tools/testing/selftests/bpf/cgroup_helpers.c |  3 ++-
 tools/testing/selftests/bpf/xsk.c            | 20 +-------------------
 3 files changed, 22 insertions(+), 20 deletions(-)
tools/testing/selftests/bpf/bpf_util.h +19 -0
index a3352a64c067..bf78212ff6e9 100644
@@ -20,6 +20,25 @@ static inline unsigned int bpf_num_possible_cpus(void)
 	return possible_cpus;
 }
 
+/* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst
+ * is zero-terminated string no matter what (unless sz == 0, in which case
+ * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs
+ * in what is returned. Given this is internal helper, it's trivial to extend
+ * this, when necessary. Use this instead of strncpy inside libbpf source code.
+ */
+static inline void libbpf_strlcpy(char *dst, const char *src, size_t sz)
+{
+	size_t i;
+
+	if (sz == 0)
+		return;
+
+	sz--;
+	for (i = 0; i < sz && src[i]; i++)
+		dst[i] = src[i];
+	dst[i] = '\0';
+}
+
 #define __bpf_percpu_val_align	__attribute__((__aligned__(8)))
 
 #define BPF_DECLARE_PERCPU(type, name)				\
tools/testing/selftests/bpf/cgroup_helpers.c +2 -1
index e914cc45b766..e33b70e509da 100644
@@ -13,6 +13,7 @@
 #include <ftw.h>
 
 #include "cgroup_helpers.h"
+#include "bpf_util.h"
 
 /*
  * To avoid relying on the system setup, when setup_cgroup_env is called
@@ -77,7 +78,7 @@ static int __enable_controllers(const char *cgroup_path, const char *controllers
 		enable[len] = 0;
 		close(fd);
 	} else {
-		strncpy(enable, controllers, sizeof(enable));
+		libbpf_strlcpy(enable, controllers, sizeof(enable));
 	}
 
 	snprintf(path, sizeof(path), "%s/cgroup.subtree_control", cgroup_path);
tools/testing/selftests/bpf/xsk.c +1 -19
index 0b3ff49c740d..cf6e9ab37b1b 100644
@@ -33,6 +33,7 @@
 #include <bpf/bpf.h>
 #include <bpf/libbpf.h>
 #include "xsk.h"
+#include "bpf_util.h"
 
 #ifndef SOL_XDP
  #define SOL_XDP 283
@@ -521,25 +522,6 @@ static int xsk_create_bpf_link(struct xsk_socket *xsk)
 	return 0;
 }
 
-/* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst
- * is zero-terminated string no matter what (unless sz == 0, in which case
- * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs
- * in what is returned. Given this is internal helper, it's trivial to extend
- * this, when necessary. Use this instead of strncpy inside libbpf source code.
- */
-static inline void libbpf_strlcpy(char *dst, const char *src, size_t sz)
-{
-        size_t i;
-
-        if (sz == 0)
-                return;
-
-        sz--;
-        for (i = 0; i < sz && src[i]; i++)
-                dst[i] = src[i];
-        dst[i] = '\0';
-}
-
 static int xsk_get_max_queues(struct xsk_socket *xsk)
 {
 	struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
Signature · 1 line
2.31.1
maintainer
#11 lore raw
Quoted 14 lines · Rong Tao

From: Rong Tao <[email protected]>

move libbpf_strlcpy() to bpf_util.h, and replace strncpy() with libbpf_strlcpy(), fix compile warning.

Compile samples/bpf, warning:

$ cd samples/bpf
$ make
...
cgroup_helpers.c: In function ‘__enable_controllers’:
cgroup_helpers.c:80:17: warning: ‘strncpy’ specified bound 4097 equals destination size [-Wstringop-truncation]
    80 |                 strncpy(enable, controllers, sizeof(enable));
       |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Quoted 1 line

Nope, BPF CI fails once again with:

https://github.com/kernel-patches/bpf/actions/runs/3370230622/jobs/5590876906

[...]
TEST-OBJ [test_progs-no_alu32] connect_ping.test.o
TEST-OBJ [test_progs-no_alu32] map_kptr.test.o
In file included from /tmp/work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/perf_branches.c:7:
/tmp/work/bpf/bpf/tools/testing/selftests/bpf/tools/include/bpf/libbpf_internal.h:200:20: error: redefinition of 'libbpf_strlcpy' static inline void libbpf_strlcpy(char *dst, const char *src, size_t sz)

                     ^
  /tmp/work/bpf/bpf/tools/testing/selftests/bpf/bpf_util.h:29:20: note: previous definition is here
  static inline void libbpf_strlcpy(char *dst, const char *src, size_t sz)
                     ^

1 error generated.
TEST-OBJ [test_progs-no_alu32] hash_large_key.test.o
make: *** [Makefile:539: /tmp/work/bpf/bpf/tools/testing/selftests/bpf/no_alu32/perf_branches.test.o] Error 1
make: *** Waiting for unfinished jobs....
make: Leaving directory '/tmp/work/bpf/bpf/tools/testing/selftests/bpf'
Error: Process completed with exit code 2.

Please do not send broken stuff that was not even compile tested.

Here's a small howto for running BPF selftests:

https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/tree/tools/testing/selftests/bpf/README.rst#n48

author
#12 lore raw

PATCH 1/1 selftests/bpf: cgroup_helpers.c: Fix strncpy() fortify warning

From: Rong Tao <[email protected]>

Move libbpf_strlcpy() to libbpf_common.h, and replace strncpy() with libbpf_strlcpy(), fix compile warning.

We can't use libbpf_internal.h directly, because it introduces a lot of header dependency issues. So move libbpf_strlcpy() into libbpf_common.h, and if you need to use the libbpf_strlcpy() function, you need to include the header file libbpf.h

How to reproduce this compilation warning:

$ make -C samples/bpf
cgroup_helpers.c: In function ‘__enable_controllers’:
cgroup_helpers.c:80:17: warning: ‘strncpy’ specified bound 4097 equals destination size [-Wstringop-truncation]
   80 |                 strncpy(enable, controllers, sizeof(enable));
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 tools/lib/bpf/libbpf_common.h                | 19 +++++++++++++++++++
 tools/lib/bpf/libbpf_internal.h              | 19 -------------------
 tools/testing/selftests/bpf/cgroup_helpers.c |  3 ++-
 tools/testing/selftests/bpf/xsk.c            | 19 -------------------
 4 files changed, 21 insertions(+), 39 deletions(-)
tools/lib/bpf/libbpf_common.h +19 -0
index 9a7937f339df..9d5132e0bec9 100644
@@ -70,4 +70,23 @@
 		};							    \
 	})
 
+/* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst
+ * is zero-terminated string no matter what (unless sz == 0, in which case
+ * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs
+ * in what is returned. Given this is internal helper, it's trivial to extend
+ * this, when necessary. Use this instead of strncpy inside libbpf source code.
+ */
+static inline void libbpf_strlcpy(char *dst, const char *src, size_t sz)
+{
+	size_t i;
+
+	if (sz == 0)
+		return;
+
+	sz--;
+	for (i = 0; i < sz && src[i]; i++)
+		dst[i] = src[i];
+	dst[i] = '\0';
+}
+
 #endif /* __LIBBPF_LIBBPF_COMMON_H */
tools/lib/bpf/libbpf_internal.h +0 -19
index 377642ff51fc..902110ffb7a6 100644
@@ -191,25 +191,6 @@ static inline void *libbpf_reallocarray(void *ptr, size_t nmemb, size_t size)
 	return realloc(ptr, total);
 }
 
-/* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst
- * is zero-terminated string no matter what (unless sz == 0, in which case
- * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs
- * in what is returned. Given this is internal helper, it's trivial to extend
- * this, when necessary. Use this instead of strncpy inside libbpf source code.
- */
-static inline void libbpf_strlcpy(char *dst, const char *src, size_t sz)
-{
-	size_t i;
-
-	if (sz == 0)
-		return;
-
-	sz--;
-	for (i = 0; i < sz && src[i]; i++)
-		dst[i] = src[i];
-	dst[i] = '\0';
-}
-
 __u32 get_kernel_version(void);
 
 struct btf;
tools/testing/selftests/bpf/cgroup_helpers.c +2 -1
index e914cc45b766..e3bfe2b13018 100644
@@ -11,6 +11,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <ftw.h>
+#include <bpf/libbpf.h>
 
 #include "cgroup_helpers.h"
 
@@ -77,7 +78,7 @@ static int __enable_controllers(const char *cgroup_path, const char *controllers
 		enable[len] = 0;
 		close(fd);
 	} else {
-		strncpy(enable, controllers, sizeof(enable));
+		libbpf_strlcpy(enable, controllers, sizeof(enable));
 	}
 
 	snprintf(path, sizeof(path), "%s/cgroup.subtree_control", cgroup_path);
tools/testing/selftests/bpf/xsk.c +0 -19
index 0b3ff49c740d..4b6890e2a0a9 100644
@@ -521,25 +521,6 @@ static int xsk_create_bpf_link(struct xsk_socket *xsk)
 	return 0;
 }
 
-/* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst
- * is zero-terminated string no matter what (unless sz == 0, in which case
- * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs
- * in what is returned. Given this is internal helper, it's trivial to extend
- * this, when necessary. Use this instead of strncpy inside libbpf source code.
- */
-static inline void libbpf_strlcpy(char *dst, const char *src, size_t sz)
-{
-        size_t i;
-
-        if (sz == 0)
-                return;
-
-        sz--;
-        for (i = 0; i < sz && src[i]; i++)
-                dst[i] = src[i];
-        dst[i] = '\0';
-}
-
 static int xsk_get_max_queues(struct xsk_socket *xsk)
 {
 	struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
Signature · 1 line
2.31.1
#13 lore raw

Re: [PATCH bpf-next] selftests/bpf: cgroup_helpers.c: Fix strncpy() fortify warning

Quoted 10 lines · Rong Tao

From: Rong Tao <[email protected]>

Move libbpf_strlcpy() to libbpf_common.h, and replace strncpy() with libbpf_strlcpy(), fix compile warning.

We can't use libbpf_internal.h directly, because it introduces a lot of header dependency issues. So move libbpf_strlcpy() into libbpf_common.h, and if you need to use the libbpf_strlcpy() function, you need to include the header file libbpf.h

well, no, we should make libbpf_strlcpy as a public API, it's internal helper. libbpf_common.h is part of libbpf's UAPI.

So don't touch libbpf, please. Name the function as _strlcpy or something like that, put it into bpf_util.h and use that from selftests.

Quoted 17 lines

How to reproduce this compilation warning:

$ make -C samples/bpf
cgroup_helpers.c: In function ‘__enable_controllers’:
cgroup_helpers.c:80:17: warning: ‘strncpy’ specified bound 4097 equals destination size [-Wstringop-truncation]
   80 |                 strncpy(enable, controllers, sizeof(enable));
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 tools/lib/bpf/libbpf_common.h                | 19 +++++++++++++++++++
 tools/lib/bpf/libbpf_internal.h              | 19 -------------------
 tools/testing/selftests/bpf/cgroup_helpers.c |  3 ++-
 tools/testing/selftests/bpf/xsk.c            | 19 -------------------
 4 files changed, 21 insertions(+), 39 deletions(-)

[...]

author
#14 lore raw

PATCH 1/1 selftests/bpf: cgroup_helpers.c: Fix strncpy() fortify warning

From: Rong Tao <[email protected]>

Copy libbpf_strlcpy() from libbpf_internal.h to bpf_util.h, and rename it to bpf_strlcpy(), then replace selftests strncpy()/libbpf_strlcpy() with bpf_strlcpy(), fix compile warning.

The libbpf_internal.h header cannot be used directly here, because references to cgroup_helpers.c in samples/bpf will generate compilation errors. We also can't add libbpf_strlcpy() directly to bpf_util.h, because the definition of libbpf_strlcpy() in libbpf_internal.h is duplicated. In order not to modify the libbpf code, add a new function bpf_strlcpy() to selftests bpf_util.h.

How to reproduce this compilation warning:

$ make -C samples/bpf
cgroup_helpers.c: In function ‘__enable_controllers’:
cgroup_helpers.c:80:17: warning: ‘strncpy’ specified bound 4097 equals destination size [-Wstringop-truncation]
   80 |                 strncpy(enable, controllers, sizeof(enable));
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 tools/testing/selftests/bpf/bpf_util.h       | 19 ++++++++++++++
 tools/testing/selftests/bpf/cgroup_helpers.c |  3 ++-
 tools/testing/selftests/bpf/xsk.c            | 26 +++-----------------
 3 files changed, 25 insertions(+), 23 deletions(-)
tools/testing/selftests/bpf/bpf_util.h +19 -0
index a3352a64c067..10587a29b967 100644
@@ -20,6 +20,25 @@ static inline unsigned int bpf_num_possible_cpus(void)
 	return possible_cpus;
 }
 
+/* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst
+ * is zero-terminated string no matter what (unless sz == 0, in which case
+ * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs
+ * in what is returned. Given this is internal helper, it's trivial to extend
+ * this, when necessary. Use this instead of strncpy inside libbpf source code.
+ */
+static inline void bpf_strlcpy(char *dst, const char *src, size_t sz)
+{
+	size_t i;
+
+	if (sz == 0)
+		return;
+
+	sz--;
+	for (i = 0; i < sz && src[i]; i++)
+		dst[i] = src[i];
+	dst[i] = '\0';
+}
+
 #define __bpf_percpu_val_align	__attribute__((__aligned__(8)))
 
 #define BPF_DECLARE_PERCPU(type, name)				\
tools/testing/selftests/bpf/cgroup_helpers.c +2 -1
index e914cc45b766..dd1aa5afcf5a 100644
@@ -13,6 +13,7 @@
 #include <ftw.h>
 
 #include "cgroup_helpers.h"
+#include "bpf_util.h"
 
 /*
  * To avoid relying on the system setup, when setup_cgroup_env is called
@@ -77,7 +78,7 @@ static int __enable_controllers(const char *cgroup_path, const char *controllers
 		enable[len] = 0;
 		close(fd);
 	} else {
-		strncpy(enable, controllers, sizeof(enable));
+		bpf_strlcpy(enable, controllers, sizeof(enable));
 	}
 
 	snprintf(path, sizeof(path), "%s/cgroup.subtree_control", cgroup_path);
tools/testing/selftests/bpf/xsk.c +4 -22
index 0b3ff49c740d..39d349509ba4 100644
@@ -33,6 +33,7 @@
 #include <bpf/bpf.h>
 #include <bpf/libbpf.h>
 #include "xsk.h"
+#include "bpf_util.h"
 
 #ifndef SOL_XDP
  #define SOL_XDP 283
@@ -521,25 +522,6 @@ static int xsk_create_bpf_link(struct xsk_socket *xsk)
 	return 0;
 }
 
-/* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst
- * is zero-terminated string no matter what (unless sz == 0, in which case
- * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs
- * in what is returned. Given this is internal helper, it's trivial to extend
- * this, when necessary. Use this instead of strncpy inside libbpf source code.
- */
-static inline void libbpf_strlcpy(char *dst, const char *src, size_t sz)
-{
-        size_t i;
-
-        if (sz == 0)
-                return;
-
-        sz--;
-        for (i = 0; i < sz && src[i]; i++)
-                dst[i] = src[i];
-        dst[i] = '\0';
-}
-
 static int xsk_get_max_queues(struct xsk_socket *xsk)
 {
 	struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
@@ -552,7 +534,7 @@ static int xsk_get_max_queues(struct xsk_socket *xsk)
 		return -errno;
 
 	ifr.ifr_data = (void *)&channels;
-	libbpf_strlcpy(ifr.ifr_name, ctx->ifname, IFNAMSIZ);
+	bpf_strlcpy(ifr.ifr_name, ctx->ifname, IFNAMSIZ);
 	err = ioctl(fd, SIOCETHTOOL, &ifr);
 	if (err && errno != EOPNOTSUPP) {
 		ret = -errno;
@@ -771,7 +753,7 @@ static int xsk_create_xsk_struct(int ifindex, struct xsk_socket *xsk)
 	}
 
 	ctx->ifindex = ifindex;
-	libbpf_strlcpy(ctx->ifname, ifname, IFNAMSIZ);
+	bpf_strlcpy(ctx->ifname, ifname, IFNAMSIZ);
 
 	xsk->ctx = ctx;
 	xsk->ctx->has_bpf_link = xsk_probe_bpf_link();
@@ -958,7 +940,7 @@ static struct xsk_ctx *xsk_create_ctx(struct xsk_socket *xsk,
 	ctx->refcount = 1;
 	ctx->umem = umem;
 	ctx->queue_id = queue_id;
-	libbpf_strlcpy(ctx->ifname, ifname, IFNAMSIZ);
+	bpf_strlcpy(ctx->ifname, ifname, IFNAMSIZ);
 
 	ctx->fill = fill;
 	ctx->comp = comp;
Signature · 1 line
2.31.1
#15 lore raw

Re: [PATCH bpf-next] selftests/bpf: cgroup_helpers.c: Fix strncpy() fortify warning

This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 6 lines · On Fri, 4 Nov 2022 09:27:54 +0800 you wrote:

From: Rong Tao <[email protected]>

Copy libbpf_strlcpy() from libbpf_internal.h to bpf_util.h, and rename it to bpf_strlcpy(), then replace selftests strncpy()/libbpf_strlcpy() with bpf_strlcpy(), fix compile warning.

Quoted 8 lines

The libbpf_internal.h header cannot be used directly here, because references to cgroup_helpers.c in samples/bpf will generate compilation errors. We also can't add libbpf_strlcpy() directly to bpf_util.h, because the definition of libbpf_strlcpy() in libbpf_internal.h is duplicated. In order not to modify the libbpf code, add a new function bpf_strlcpy() to selftests bpf_util.h.

[...]

Here is the summary with links:
- [bpf-next] selftests/bpf: cgroup_helpers.c: Fix strncpy() fortify warning

    https://git.kernel.org/bpf/bpf-next/c/b3c09fdca113

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
author
#16 lore raw

PATCH RESEND 1/1 selftests/bpf: Fix strncpy() fortify warning

In reply to Andrii Nakryiko

From: Rong Tao <[email protected]>

Replace strncpy() with strncat(), strncat() leaves the dst string zero terminated. Compile samples/bpf warning:

$ cd samples/bpf
$ make
...
In function ‘__enable_controllers’:
samples/bpf/../../tools/testing/selftests/bpf/cgroup_helpers.c:80:17: warning: ‘strncpy’ specified bound 4097 equals destination size [-Wstringop-truncation]
   80 |                 strncpy(enable, controllers, sizeof(enable));
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 tools/testing/selftests/bpf/cgroup_helpers.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/cgroup_helpers.c +2 -1
index e914cc45b766..912e6522c7c5 100644
@@ -77,7 +77,8 @@ static int __enable_controllers(const char *cgroup_path, const char *controllers
 		enable[len] = 0;
 		close(fd);
 	} else {
-		strncpy(enable, controllers, sizeof(enable));
+		enable[0] = '\0';
+		strncat(enable, controllers, sizeof(enable) - 1);
 	}
 
 	snprintf(path, sizeof(path), "%s/cgroup.subtree_control", cgroup_path);
Signature · 1 line
2.31.1