Skip to contentSkip to conversation

selftests/bpf: Bump path and command buffer sizes in bpftool_helpers.c

PATCH Superseded Ihor Solodrai 2 participants 3 messages Mar 12

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ihor Solodrai

author reviewer
#0 lore raw

PATCH 1/1 selftests/bpf: Bump path and command buffer sizes in bpftool_helpers.c

The path length of 64 is way too low in some envirnoments, which leads to subtle failures due to truncation [1].

Replace BPFTOOL_PATH_MAX_LEN with PATH_MAX, and set BPFTOOL_FULL_CMD_MAX_LEN to double of PATH_MAX.

[1] https://github.com/libbpf/libbpf/actions/runs/22980753016/job/66719800527

---
 tools/testing/selftests/bpf/bpftool_helpers.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
tools/testing/selftests/bpf/bpftool_helpers.c +6 -7
index 929fc257f431..6a5e99b5e542 100644
@@ -2,18 +2,18 @@
 #include <unistd.h>
 #include <string.h>
 #include <stdbool.h>
+#include <limits.h>
 
 #include "bpf_util.h"
 #include "bpftool_helpers.h"
 
-#define BPFTOOL_PATH_MAX_LEN		64
-#define BPFTOOL_FULL_CMD_MAX_LEN	512
+#define BPFTOOL_FULL_CMD_MAX_LEN	(PATH_MAX * 2)
 
 #define BPFTOOL_DEFAULT_PATH		"tools/sbin/bpftool"
 
 static int detect_bpftool_path(char *buffer, size_t size)
 {
-	char tmp[BPFTOOL_PATH_MAX_LEN];
+	char tmp[PATH_MAX];
 	const char *env_path;
 
 	/* First, check if BPFTOOL environment variable is set */
@@ -29,7 +29,7 @@ static int detect_bpftool_path(char *buffer, size_t size)
 	/* Check default bpftool location (will work if we are running the
 	 * default flavor of test_progs)
 	 */
-	snprintf(tmp, BPFTOOL_PATH_MAX_LEN, "./%s", BPFTOOL_DEFAULT_PATH);
+	snprintf(tmp, PATH_MAX, "./%s", BPFTOOL_DEFAULT_PATH);
 	if (access(tmp, X_OK) == 0) {
 		strscpy(buffer, tmp, size);
 		return 0;
@@ -38,7 +38,7 @@ static int detect_bpftool_path(char *buffer, size_t size)
 	/* Check alternate bpftool location (will work if we are running a
 	 * specific flavor of test_progs, e.g. cpuv4 or no_alu32)
 	 */
-	snprintf(tmp, BPFTOOL_PATH_MAX_LEN, "../%s", BPFTOOL_DEFAULT_PATH);
+	snprintf(tmp, PATH_MAX, "../%s", BPFTOOL_DEFAULT_PATH);
 	if (access(tmp, X_OK) == 0) {
 		strscpy(buffer, tmp, size);
 		return 0;
@@ -50,7 +50,7 @@ static int detect_bpftool_path(char *buffer, size_t size)
 
 static int run_command(char *args, char *output_buf, size_t output_max_len)
 {
-	static char bpftool_path[BPFTOOL_PATH_MAX_LEN] = {0};
+	static char bpftool_path[PATH_MAX] = {0};
 	bool suppress_output = !(output_buf && output_max_len);
 	char command[BPFTOOL_FULL_CMD_MAX_LEN];
 	FILE *f;
@@ -84,4 +84,3 @@ int get_bpftool_command_output(char *args, char *output_buf, size_t output_max_l
 {
 	return run_command(args, output_buf, output_max_len);
 }
-
Signature · 1 line
2.53.0
#1 lore raw
Quoted 41 lines · Ihor Solodrai

The path length of 64 is way too low in some envirnoments, which leads to subtle failures due to truncation [1].

Replace BPFTOOL_PATH_MAX_LEN with PATH_MAX, and set BPFTOOL_FULL_CMD_MAX_LEN to double of PATH_MAX.

[1] https://github.com/libbpf/libbpf/actions/runs/22980753016/job/66719800527

---
 tools/testing/selftests/bpf/bpftool_helpers.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
tools/testing/selftests/bpf/bpftool_helpers.c +3 -3
index 929fc257f431..6a5e99b5e542 100644
@@ -2,18 +2,18 @@
 #include <unistd.h>
 #include <string.h>
 #include <stdbool.h>
+#include <limits.h>
 
 #include "bpf_util.h"
 #include "bpftool_helpers.h"
 
-#define BPFTOOL_PATH_MAX_LEN           64
-#define BPFTOOL_FULL_CMD_MAX_LEN       512
+#define BPFTOOL_FULL_CMD_MAX_LEN       (PATH_MAX * 2)
 
 #define BPFTOOL_DEFAULT_PATH           "tools/sbin/bpftool"
 
 static int detect_bpftool_path(char *buffer, size_t size)
 {
-       char tmp[BPFTOOL_PATH_MAX_LEN];
+       char tmp[PATH_MAX];
        const char *env_path;
 
        /* First, check if BPFTOOL environment variable is set */

tools/testing/selftests/bpf/bpftool_helpers.c static int detect_bpftool_path(char *buffer, size_t size) · line 32

Quoted 5 lines
tools/testing/selftests/bpf/bpftool_helpers.c +1 -1
@@ -29,7 +29,7 @@ static int detect_bpftool_path(char *buffer, size_t size)
        /* Check default bpftool location (will work if we are running the
         * default flavor of test_progs)
         */
-       snprintf(tmp, BPFTOOL_PATH_MAX_LEN, "./%s", BPFTOOL_DEFAULT_PATH);
+       snprintf(tmp, PATH_MAX, "./%s", BPFTOOL_DEFAULT_PATH);

nit, but why not s/PATH_MAX/sizeof(tmp)/?

Quoted 13 lines
tools/testing/selftests/bpf/bpftool_helpers.c +1 -1
        if (access(tmp, X_OK) == 0) {
                strscpy(buffer, tmp, size);
                return 0;
@@ -38,7 +38,7 @@ static int detect_bpftool_path(char *buffer, size_t size)
        /* Check alternate bpftool location (will work if we are running a
         * specific flavor of test_progs, e.g. cpuv4 or no_alu32)
         */
-       snprintf(tmp, BPFTOOL_PATH_MAX_LEN, "../%s", BPFTOOL_DEFAULT_PATH);
+       snprintf(tmp, PATH_MAX, "../%s", BPFTOOL_DEFAULT_PATH);
        if (access(tmp, X_OK) == 0) {
                strscpy(buffer, tmp, size);
                return 0;

tools/testing/selftests/bpf/bpftool_helpers.c static int detect_bpftool_path(char *buffer, size_t size) · line 53

Quoted 5 lines
tools/testing/selftests/bpf/bpftool_helpers.c +1 -1
@@ -50,7 +50,7 @@ static int detect_bpftool_path(char *buffer, size_t size)
 
 static int run_command(char *args, char *output_buf, size_t output_max_len)
 {
-       static char bpftool_path[BPFTOOL_PATH_MAX_LEN] = {0};
+       static char bpftool_path[PATH_MAX] = {0};

this doesn't matter in practical terms, but I'm not sure all people realize that this initializes entire buffer with zeroes, not just first byte. = {0} is super misleading, it's better to use = {} in cases like this

Quoted 11 lines
tools/testing/selftests/bpf/bpftool_helpers.c +0 -2
        bool suppress_output = !(output_buf && output_max_len);
        char command[BPFTOOL_FULL_CMD_MAX_LEN];
        FILE *f;
@@ -84,4 +84,3 @@ int get_bpftool_command_output(char *args, char *output_buf, size_t output_max_l
 {
        return run_command(args, output_buf, output_max_len);
 }
-
--

2.53.0

author reviewer
#2 lore raw
Quoted 40 lines · Andrii Nakryiko
Quoted 39 lines · Ihor Solodrai

The path length of 64 is way too low in some envirnoments, which leads to subtle failures due to truncation [1].

Replace BPFTOOL_PATH_MAX_LEN with PATH_MAX, and set BPFTOOL_FULL_CMD_MAX_LEN to double of PATH_MAX.

[1] https://github.com/libbpf/libbpf/actions/runs/22980753016/job/66719800527

---
 tools/testing/selftests/bpf/bpftool_helpers.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
tools/testing/selftests/bpf/bpftool_helpers.c +3 -3
index 929fc257f431..6a5e99b5e542 100644
@@ -2,18 +2,18 @@
 #include <unistd.h>
 #include <string.h>
 #include <stdbool.h>
+#include <limits.h>
 
 #include "bpf_util.h"
 #include "bpftool_helpers.h"
 
-#define BPFTOOL_PATH_MAX_LEN           64
-#define BPFTOOL_FULL_CMD_MAX_LEN       512
+#define BPFTOOL_FULL_CMD_MAX_LEN       (PATH_MAX * 2)
 
 #define BPFTOOL_DEFAULT_PATH           "tools/sbin/bpftool"
 
 static int detect_bpftool_path(char *buffer, size_t size)
 {
-       char tmp[BPFTOOL_PATH_MAX_LEN];
+       char tmp[PATH_MAX];
        const char *env_path;
 
Quoted 9 lines

tools/testing/selftests/bpf/bpftool_helpers.c static int detect_bpftool_path(char *buffer, size_t size) · line 32

Quoted 7 lines
tools/testing/selftests/bpf/bpftool_helpers.c +1 -1
        /* First, check if BPFTOOL environment variable is set */
@@ -29,7 +29,7 @@ static int detect_bpftool_path(char *buffer, size_t size)
        /* Check default bpftool location (will work if we are running the
         * default flavor of test_progs)
         */
-       snprintf(tmp, BPFTOOL_PATH_MAX_LEN, "./%s", BPFTOOL_DEFAULT_PATH);
+       snprintf(tmp, PATH_MAX, "./%s", BPFTOOL_DEFAULT_PATH);

nit, but why not s/PATH_MAX/sizeof(tmp)/?

Hi Andrii, thanks for quick review.

I just did the s/BPFTOOL_PATH_MAX_LEN/PATH_MAX in v1, will fix up.

Quoted 15 lines
Quoted 15 lines
tools/testing/selftests/bpf/bpftool_helpers.c +1 -1
        if (access(tmp, X_OK) == 0) {
                strscpy(buffer, tmp, size);
                return 0;
@@ -38,7 +38,7 @@ static int detect_bpftool_path(char *buffer, size_t size)
        /* Check alternate bpftool location (will work if we are running a
         * specific flavor of test_progs, e.g. cpuv4 or no_alu32)
         */
-       snprintf(tmp, BPFTOOL_PATH_MAX_LEN, "../%s", BPFTOOL_DEFAULT_PATH);
+       snprintf(tmp, PATH_MAX, "../%s", BPFTOOL_DEFAULT_PATH);
        if (access(tmp, X_OK) == 0) {
                strscpy(buffer, tmp, size);
                return 0;
@@ -50,7 +50,7 @@ static int detect_bpftool_path(char *buffer, size_t size)
 
Quoted 9 lines

tools/testing/selftests/bpf/bpftool_helpers.c static int detect_bpftool_path(char *buffer, size_t size) · line 53

Quoted 4 lines
tools/testing/selftests/bpf/bpftool_helpers.c +1 -1
 static int run_command(char *args, char *output_buf, size_t output_max_len)
 {
-       static char bpftool_path[BPFTOOL_PATH_MAX_LEN] = {0};
+       static char bpftool_path[PATH_MAX] = {0};

this doesn't matter in practical terms, but I'm not sure all people realize that this initializes entire buffer with zeroes, not just first byte. = {0} is super misleading, it's better to use = {} in cases like this

I think setting the entire buffer to 0 to determine if the path is cached is an overkill, although in this particular case it's not important since the buffer is static.

I'll change to {}, I agree it's more obvious.

Not directly relevant, but fun news re {0} init, courtesy of Eduard:
https://news.ycombinator.com/item?id=43792948

Quoted 12 lines
Quoted 12 lines
tools/testing/selftests/bpf/bpftool_helpers.c +0 -2
        bool suppress_output = !(output_buf && output_max_len);
        char command[BPFTOOL_FULL_CMD_MAX_LEN];
        FILE *f;
@@ -84,4 +84,3 @@ int get_bpftool_command_output(char *args, char *output_buf, size_t output_max_l
 {
        return run_command(args, output_buf, output_max_len);
 }
-
--

2.53.0