The bpftool_maps_access and bpftool_metadata tests may fail on BPF CI
with "command not found", depending on a workflow.
This happens because detect_bpftool_path() only checks two hardcoded
relative paths:
- ./tools/sbin/bpftool
- ../tools/sbin/bpftool
Add support for a BPFTOOL environment variable that allows specifying
the exact path to the bpftool binary.
Also replace strncpy() with snprintf() for proper null-termination.
Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev>
---
tools/testing/selftests/bpf/bpftool_helpers.c | 19 +++++++++++++++----
1 file changed, 15 insertions(+), 4 deletions(-)
diff --git a/tools/testing/selftests/bpf/bpftool_helpers.c b/tools/testing/selftests/bpf/bpftool_helpers.c
index a5824945a4a5..d810e73da6c8 100644
--- a/tools/testing/selftests/bpf/bpftool_helpers.c
+++ b/tools/testing/selftests/bpf/bpftool_helpers.c
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-only
#include "bpftool_helpers.h"
+#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
@@ -12,13 +13,24 @@
static int detect_bpftool_path(char *buffer)
{
char tmp[BPFTOOL_PATH_MAX_LEN];
+ const char *env_path;
+
+ /* First, check if BPFTOOL environment variable is set */
+ env_path = getenv("BPFTOOL");
+ if (env_path && access(env_path, X_OK) == 0) {
+ snprintf(buffer, BPFTOOL_PATH_MAX_LEN, "%s", env_path);
+ return 0;
+ } else if (env_path) {
+ fprintf(stderr, "bpftool '%s' doesn't exist or is not executable\n", env_path);
+ return 1;
+ }
/* 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);
if (access(tmp, X_OK) == 0) {
- strncpy(buffer, tmp, BPFTOOL_PATH_MAX_LEN);
+ snprintf(buffer, BPFTOOL_PATH_MAX_LEN, "%s", tmp);
return 0;
}
@@ -27,11 +39,11 @@ static int detect_bpftool_path(char *buffer)
*/
snprintf(tmp, BPFTOOL_PATH_MAX_LEN, "../%s", BPFTOOL_DEFAULT_PATH);
if (access(tmp, X_OK) == 0) {
- strncpy(buffer, tmp, BPFTOOL_PATH_MAX_LEN);
+ snprintf(buffer, BPFTOOL_PATH_MAX_LEN, "%s", tmp);
return 0;
}
- /* Failed to find bpftool binary */
+ fprintf(stderr, "Failed to detect bpftool path, use BPFTOOL env var to override\n");
return 1;
}
@@ -71,4 +83,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
On Tue, Feb 17, 2026 at 4:31 PM Ihor Solodrai <ihor.solodrai@linux.dev> wrote:
>
> The bpftool_maps_access and bpftool_metadata tests may fail on BPF CI
> with "command not found", depending on a workflow.
> This happens because detect_bpftool_path() only checks two hardcoded
> relative paths:
> - ./tools/sbin/bpftool
> - ../tools/sbin/bpftool
>
> Add support for a BPFTOOL environment variable that allows specifying
> the exact path to the bpftool binary.
>
> Also replace strncpy() with snprintf() for proper null-termination.
>
> Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev>
> ---
> tools/testing/selftests/bpf/bpftool_helpers.c | 19 +++++++++++++++----
> 1 file changed, 15 insertions(+), 4 deletions(-)
>
> diff --git a/tools/testing/selftests/bpf/bpftool_helpers.c b/tools/testing/selftests/bpf/bpftool_helpers.c
> index a5824945a4a5..d810e73da6c8 100644
> --- a/tools/testing/selftests/bpf/bpftool_helpers.c
> +++ b/tools/testing/selftests/bpf/bpftool_helpers.c
> @@ -1,5 +1,6 @@
> // SPDX-License-Identifier: GPL-2.0-only
> #include "bpftool_helpers.h"
> +#include <stdio.h>
> #include <unistd.h>
> #include <string.h>
> #include <stdbool.h>
> @@ -12,13 +13,24 @@
> static int detect_bpftool_path(char *buffer)
> {
> char tmp[BPFTOOL_PATH_MAX_LEN];
> + const char *env_path;
> +
> + /* First, check if BPFTOOL environment variable is set */
> + env_path = getenv("BPFTOOL");
> + if (env_path && access(env_path, X_OK) == 0) {
> + snprintf(buffer, BPFTOOL_PATH_MAX_LEN, "%s", env_path);
> + return 0;
> + } else if (env_path) {
> + fprintf(stderr, "bpftool '%s' doesn't exist or is not executable\n", env_path);
> + return 1;
> + }
>
> /* 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);
> if (access(tmp, X_OK) == 0) {
> - strncpy(buffer, tmp, BPFTOOL_PATH_MAX_LEN);
> + snprintf(buffer, BPFTOOL_PATH_MAX_LEN, "%s", tmp);
I guess it's ok for user space, but
git log --oneline|grep "snprintf with strscpy"
ad789a85b163 mm/cma: replace snprintf with strscpy in cma_new_area
674fb053e95d sparc: vio: Replace snprintf with strscpy in vio_create_one
2dfc417414c6 genirq/proc: Replace snprintf with strscpy in register_handler_proc
f46ebb910989 block: Replace snprintf with strscpy in check_partition
b66215e7b780 media: verisilicon: replace snprintf with strscpy+strlcat
a86028f8e3ee staging: most: sound: replace snprintf with strscpy
and many others...
So.. should we introduce strscpy() in selftests/bpf ?
On 2/18/26 9:38 AM, Alexei Starovoitov wrote:
> On Tue, Feb 17, 2026 at 4:31 PM Ihor Solodrai <ihor.solodrai@linux.dev> wrote:
>>
>> The bpftool_maps_access and bpftool_metadata tests may fail on BPF CI
>> with "command not found", depending on a workflow.
>> This happens because detect_bpftool_path() only checks two hardcoded
>> relative paths:
>> - ./tools/sbin/bpftool
>> - ../tools/sbin/bpftool
>>
>> Add support for a BPFTOOL environment variable that allows specifying
>> the exact path to the bpftool binary.
>>
>> Also replace strncpy() with snprintf() for proper null-termination.
>>
>> Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev>
>> ---
>> tools/testing/selftests/bpf/bpftool_helpers.c | 19 +++++++++++++++----
>> 1 file changed, 15 insertions(+), 4 deletions(-)
>>
>> diff --git a/tools/testing/selftests/bpf/bpftool_helpers.c b/tools/testing/selftests/bpf/bpftool_helpers.c
>> index a5824945a4a5..d810e73da6c8 100644
>> --- a/tools/testing/selftests/bpf/bpftool_helpers.c
>> +++ b/tools/testing/selftests/bpf/bpftool_helpers.c
>> @@ -1,5 +1,6 @@
>> // SPDX-License-Identifier: GPL-2.0-only
>> #include "bpftool_helpers.h"
>> +#include <stdio.h>
>> #include <unistd.h>
>> #include <string.h>
>> #include <stdbool.h>
>> @@ -12,13 +13,24 @@
>> static int detect_bpftool_path(char *buffer)
>> {
>> char tmp[BPFTOOL_PATH_MAX_LEN];
>> + const char *env_path;
>> +
>> + /* First, check if BPFTOOL environment variable is set */
>> + env_path = getenv("BPFTOOL");
>> + if (env_path && access(env_path, X_OK) == 0) {
>> + snprintf(buffer, BPFTOOL_PATH_MAX_LEN, "%s", env_path);
>> + return 0;
>> + } else if (env_path) {
>> + fprintf(stderr, "bpftool '%s' doesn't exist or is not executable\n", env_path);
>> + return 1;
>> + }
>>
>> /* 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);
>> if (access(tmp, X_OK) == 0) {
>> - strncpy(buffer, tmp, BPFTOOL_PATH_MAX_LEN);
>> + snprintf(buffer, BPFTOOL_PATH_MAX_LEN, "%s", tmp);
>
> I guess it's ok for user space, but
>
> git log --oneline|grep "snprintf with strscpy"
> ad789a85b163 mm/cma: replace snprintf with strscpy in cma_new_area
> 674fb053e95d sparc: vio: Replace snprintf with strscpy in vio_create_one
> 2dfc417414c6 genirq/proc: Replace snprintf with strscpy in register_handler_proc
> f46ebb910989 block: Replace snprintf with strscpy in check_partition
> b66215e7b780 media: verisilicon: replace snprintf with strscpy+strlcat
> a86028f8e3ee staging: most: sound: replace snprintf with strscpy
>
> and many others...
> So.. should we introduce strscpy() in selftests/bpf ?
I guess we can, but:
$ grep -r 'snprintf(' --include="*.[ch]" tools/testing/selftests/bpf/ | wc -l
238
The reason to prefer strscpy() is speed, right?
Can we use kernel implementation in userspace directly?
In tools/include I only see this:
#define strscpy strcpy
On 2/18/26 10:17 AM, Ihor Solodrai wrote:
> On 2/18/26 9:38 AM, Alexei Starovoitov wrote:
>> On Tue, Feb 17, 2026 at 4:31 PM Ihor Solodrai <ihor.solodrai@linux.dev> wrote:
>>>
>>> [...]
>>>
>>> /* 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);
>>> if (access(tmp, X_OK) == 0) {
>>> - strncpy(buffer, tmp, BPFTOOL_PATH_MAX_LEN);
>>> + snprintf(buffer, BPFTOOL_PATH_MAX_LEN, "%s", tmp);
>>
>> I guess it's ok for user space, but
>>
>> git log --oneline|grep "snprintf with strscpy"
>> ad789a85b163 mm/cma: replace snprintf with strscpy in cma_new_area
>> 674fb053e95d sparc: vio: Replace snprintf with strscpy in vio_create_one
>> 2dfc417414c6 genirq/proc: Replace snprintf with strscpy in register_handler_proc
>> f46ebb910989 block: Replace snprintf with strscpy in check_partition
>> b66215e7b780 media: verisilicon: replace snprintf with strscpy+strlcat
>> a86028f8e3ee staging: most: sound: replace snprintf with strscpy
>>
>> and many others...
>> So.. should we introduce strscpy() in selftests/bpf ?
>
> I guess we can, but:
>
> $ grep -r 'snprintf(' --include="*.[ch]" tools/testing/selftests/bpf/ | wc -l
> 238
>
> The reason to prefer strscpy() is speed, right?
> Can we use kernel implementation in userspace directly?
>
> In tools/include I only see this:
>
> #define strscpy strcpy
>
I think we can add a simple implementation to tools/lib/string.c
See a diff below: essentially a copy from arch/s390/boot/string.c [1]
I suppose it's faster than snprintf(). However I am not sure about
going through all 238 usages, as that would touch many files. But at
least we can make actual strscpy available in selftests.
I can add this in v3. Alexei, wdyt?
[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/s390/boot/string.c?h=v6.19#n32
diff --git a/tools/include/linux/string.h b/tools/include/linux/string.h
index 51ad3cf4fa82..ca183a0e846a 100644
--- a/tools/include/linux/string.h
+++ b/tools/include/linux/string.h
@@ -3,6 +3,7 @@
#define _TOOLS_LINUX_STRING_H_
#include <linux/types.h> /* for size_t */
+#include <sys/types.h> /* for ssize_t */
#include <string.h>
void *memdup(const void *src, size_t len);
@@ -12,7 +13,7 @@ void argv_free(char **argv);
int strtobool(const char *s, bool *res);
-#define strscpy strcpy
+ssize_t strscpy(char *dest, const char *src, size_t count);
/*
* glibc based builds needs the extern while uClibc doesn't.
diff --git a/tools/lib/string.c b/tools/lib/string.c
index 3126d2cff716..54c9163fa2b0 100644
--- a/tools/lib/string.c
+++ b/tools/lib/string.c
@@ -239,3 +239,17 @@ void *memchr_inv(const void *start, int c, size_t bytes)
return check_bytes8(start, value, bytes % 8);
}
+
+ssize_t strscpy(char *dst, const char *src, size_t count)
+{
+ size_t len;
+
+ if (count == 0)
+ return -E2BIG;
+
+ len = strnlen(src, count - 1);
+ memcpy(dst, src, len);
+ dst[len] = '\0';
+
+ return src[len] ? -E2BIG : len;
+}
On Wed, Feb 18, 2026 at 5:13 PM Ihor Solodrai <ihor.solodrai@linux.dev> wrote:
>
> On 2/18/26 10:17 AM, Ihor Solodrai wrote:
> > On 2/18/26 9:38 AM, Alexei Starovoitov wrote:
> >> On Tue, Feb 17, 2026 at 4:31 PM Ihor Solodrai <ihor.solodrai@linux.dev> wrote:
> >>>
> >>> [...]
> >>>
> >>> /* 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);
> >>> if (access(tmp, X_OK) == 0) {
> >>> - strncpy(buffer, tmp, BPFTOOL_PATH_MAX_LEN);
> >>> + snprintf(buffer, BPFTOOL_PATH_MAX_LEN, "%s", tmp);
> >>
> >> I guess it's ok for user space, but
> >>
> >> git log --oneline|grep "snprintf with strscpy"
> >> ad789a85b163 mm/cma: replace snprintf with strscpy in cma_new_area
> >> 674fb053e95d sparc: vio: Replace snprintf with strscpy in vio_create_one
> >> 2dfc417414c6 genirq/proc: Replace snprintf with strscpy in register_handler_proc
> >> f46ebb910989 block: Replace snprintf with strscpy in check_partition
> >> b66215e7b780 media: verisilicon: replace snprintf with strscpy+strlcat
> >> a86028f8e3ee staging: most: sound: replace snprintf with strscpy
> >>
> >> and many others...
> >> So.. should we introduce strscpy() in selftests/bpf ?
> >
> > I guess we can, but:
> >
> > $ grep -r 'snprintf(' --include="*.[ch]" tools/testing/selftests/bpf/ | wc -l
> > 238
> >
> > The reason to prefer strscpy() is speed, right?
> > Can we use kernel implementation in userspace directly?
> >
> > In tools/include I only see this:
> >
> > #define strscpy strcpy
> >
>
> I think we can add a simple implementation to tools/lib/string.c
> See a diff below: essentially a copy from arch/s390/boot/string.c [1]
>
> I suppose it's faster than snprintf(). However I am not sure about
> going through all 238 usages, as that would touch many files. But at
> least we can make actual strscpy available in selftests.
>
> I can add this in v3. Alexei, wdyt?
>
> [1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/s390/boot/string.c?h=v6.19#n32
>
> diff --git a/tools/include/linux/string.h b/tools/include/linux/string.h
> index 51ad3cf4fa82..ca183a0e846a 100644
> --- a/tools/include/linux/string.h
> +++ b/tools/include/linux/string.h
> @@ -3,6 +3,7 @@
> #define _TOOLS_LINUX_STRING_H_
>
> #include <linux/types.h> /* for size_t */
> +#include <sys/types.h> /* for ssize_t */
> #include <string.h>
>
> void *memdup(const void *src, size_t len);
> @@ -12,7 +13,7 @@ void argv_free(char **argv);
>
> int strtobool(const char *s, bool *res);
>
> -#define strscpy strcpy
> +ssize_t strscpy(char *dest, const char *src, size_t count);
>
> /*
> * glibc based builds needs the extern while uClibc doesn't.
> diff --git a/tools/lib/string.c b/tools/lib/string.c
> index 3126d2cff716..54c9163fa2b0 100644
> --- a/tools/lib/string.c
> +++ b/tools/lib/string.c
> @@ -239,3 +239,17 @@ void *memchr_inv(const void *start, int c, size_t bytes)
>
> return check_bytes8(start, value, bytes % 8);
> }
> +
> +ssize_t strscpy(char *dst, const char *src, size_t count)
> +{
> + size_t len;
> +
> + if (count == 0)
> + return -E2BIG;
> +
> + len = strnlen(src, count - 1);
> + memcpy(dst, src, len);
> + dst[len] = '\0';
> +
> + return src[len] ? -E2BIG : len;
> +}
Hmm. it looks a bit like strlcpy() which is also not recommended.
See Documentation/process/deprecated.rst
that explains the reasons why strscpy() is the only "approved" version.
Also tools/lib/ is too scrutinized.
Let's take sized_strscpy() from lib/string.c
drop all optimizations like word a time, page boundary tricks, etc
and only keep essentially this:
while (count > 1) {
char c;
c = src[res];
dest[res] = c;
if (!c)
return res;
res++;
count--;
}
/* Force NUL-termination. */
dest[res] = '\0';
/* Return E2BIG if the source didn't stop */
return src[res] ? -E2BIG : res;
and add it somewhere in selftests/bpf..
and only use it in these couple cases where we need to fix strncpy.
Further snprintf() cleanup can be dealt with later.
I mainly want to avoid more churn when people will inevitably
start sending patches to replace snprintf with strscpy.
On 2/18/26 00:30, Ihor Solodrai wrote:
> The bpftool_maps_access and bpftool_metadata tests may fail on BPF CI
> with "command not found", depending on a workflow.
> This happens because detect_bpftool_path() only checks two hardcoded
> relative paths:
> - ./tools/sbin/bpftool
> - ../tools/sbin/bpftool
>
> Add support for a BPFTOOL environment variable that allows specifying
> the exact path to the bpftool binary.
>
> Also replace strncpy() with snprintf() for proper null-termination.
>
> Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev>
Acked-by: Mykyta Yatsenko <yatsenko@meta.com>
> ---
> tools/testing/selftests/bpf/bpftool_helpers.c | 19 +++++++++++++++----
> 1 file changed, 15 insertions(+), 4 deletions(-)
>
> diff --git a/tools/testing/selftests/bpf/bpftool_helpers.c b/tools/testing/selftests/bpf/bpftool_helpers.c
> index a5824945a4a5..d810e73da6c8 100644
> --- a/tools/testing/selftests/bpf/bpftool_helpers.c
> +++ b/tools/testing/selftests/bpf/bpftool_helpers.c
> @@ -1,5 +1,6 @@
> // SPDX-License-Identifier: GPL-2.0-only
> #include "bpftool_helpers.h"
> +#include <stdio.h>
> #include <unistd.h>
> #include <string.h>
> #include <stdbool.h>
> @@ -12,13 +13,24 @@
> static int detect_bpftool_path(char *buffer)
> {
> char tmp[BPFTOOL_PATH_MAX_LEN];
> + const char *env_path;
> +
> + /* First, check if BPFTOOL environment variable is set */
> + env_path = getenv("BPFTOOL");
> + if (env_path && access(env_path, X_OK) == 0) {
> + snprintf(buffer, BPFTOOL_PATH_MAX_LEN, "%s", env_path);
> + return 0;
> + } else if (env_path) {
> + fprintf(stderr, "bpftool '%s' doesn't exist or is not executable\n", env_path);
> + return 1;
> + }
>
> /* 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);
> if (access(tmp, X_OK) == 0) {
> - strncpy(buffer, tmp, BPFTOOL_PATH_MAX_LEN);
> + snprintf(buffer, BPFTOOL_PATH_MAX_LEN, "%s", tmp);
> return 0;
> }
>
> @@ -27,11 +39,11 @@ static int detect_bpftool_path(char *buffer)
> */
> snprintf(tmp, BPFTOOL_PATH_MAX_LEN, "../%s", BPFTOOL_DEFAULT_PATH);
> if (access(tmp, X_OK) == 0) {
> - strncpy(buffer, tmp, BPFTOOL_PATH_MAX_LEN);
> + snprintf(buffer, BPFTOOL_PATH_MAX_LEN, "%s", tmp);
> return 0;
> }
>
> - /* Failed to find bpftool binary */
> + fprintf(stderr, "Failed to detect bpftool path, use BPFTOOL env var to override\n");
> return 1;
> }
>
> @@ -71,4 +83,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);
> }
> -
© 2016 - 2026 Red Hat, Inc.