mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: bot+bpf-ci@kernel.org
To: alexis.lothore@bootlin.com,ast@kernel.org,daniel@iogearbox.net,john.fastabend@gmail.com,andrii@kernel.org,martin.lau@linux.dev,eddyz87@gmail.com,memxor@gmail.com,song@kernel.org,yonghong.song@linux.dev,jolsa@kernel.org,tglx@kernel.org,bp@alien8.de,dave.hansen@linux.intel.com,x86@kernel.org,hpa@zytor.com,shuah@kernel.org,mingo@redhat.com,andreyknvl@gmail.com
Cc: ebpf@linuxfoundation.org,bastien.curutchet@bootlin.com,thomas.petazzoni@bootlin.com,bpf@vger.kernel.org,linux-kernel@vger.kernel.org,linux-kselftest@vger.kernel.org,alexis.lothore@bootlin.com,ast@kernel.org,andrii@kernel.org,daniel@iogearbox.net,martin.lau@kernel.org,eddyz87@gmail.com,yonghong.song@linux.dev,clm@meta.com,ihor.solodrai@linux.dev
Subject: Re: [PATCH bpf-next v6 6/9] selftests/bpf: make cmdline_contains stricter
Date: Tue,  4 Aug 2026 18:49:41 +0000 (UTC)	[thread overview]
Message-ID: <fd8d539b6c09ca90f3f00840e4c842ac8784b0ed4d0fcc018b3e43ec2dedd9cf@mail.kernel.org> (raw)
In-Reply-To: <20260804-kasan-v6-6-549ef845f491@bootlin.com>

[-- Attachment #1: Type: text/plain, Size: 3808 bytes --]

> selftests/bpf: make cmdline_contains stricter
>
> cmdline_contains is used by BPF selftests to check the presence of
> specific kernel commandline parameters, but it currently suffers from
> two issues:
> - the read commandline isn't NULL terminated right after the read data
>   but only at the end of the buffer, leaving uninitialized bytes that
>   are then possibly tokenized
> - the comparison of found tokens is done based on the size of found
>   token. This could lead to too-short-but-matching tokens to wrongly
>   match the search pattern.
>
> Enforce stricter checks in cmdline_contains to avoid accidental matches.
>
> Signed-off-by: Alexis Lothoré (eBPF Foundation) <alexis.lothore@bootlin.com>

The commit message describes two concrete bugs in existing selftest
code ('the read commandline isn't NULL terminated right after the read
data but only at the end of the buffer, leaving uninitialized bytes
that are then possibly tokenized' and 'the comparison of found tokens
is done based on the size of found token. This could lead to
too-short-but-matching tokens to wrongly match the search pattern'),
but there is no Fixes: tag.

Both defects were introduced together by 399f6185a1c0 ("selftests/bpf:
Fix selftests broken by mitigations=off"), which added the original
helper:

    +	if (read(fd, cmdline, sizeof(cmdline) - 1) < 0) {
    ...
    +	cmdline[sizeof(cmdline) - 1] = '\0';
    +	for (c = strtok(cmdline, " \n"); c; c = strtok(NULL, " \n")) {
    +		if (strncmp(c, "mitigations=off", strlen(c)))

The late NUL termination and the strncmp(c, pat, strlen(c)) prefix
comparison are both present verbatim in that commit. The
prefix-compare bug is real and reachable: a one-character token such
as 'm' on the command line yields strlen(c) == 1, so strncmp(c,
"mitigations=off", 1) returns 0 and the helper reports a match,
causing get_unpriv_disabled() to skip the unprivileged verifier tests
on a machine where mitigations are actually enabled.

Should this include:

  Fixes: 399f6185a1c0 ("selftests/bpf: Fix selftests broken by mitigations=off")

> diff --git a/tools/testing/selftests/bpf/unpriv_helpers.c b/tools/testing/selftests/bpf/unpriv_helpers.c
> index f997d7ec8fd08..c99d81df2aa25 100644
> --- a/tools/testing/selftests/bpf/unpriv_helpers.c
> +++ b/tools/testing/selftests/bpf/unpriv_helpers.c
> @@ -72,8 +72,9 @@ static int config_contains(const char *pat)
>
>  static bool cmdline_contains(const char *pat)
>  {
> +	int fd, cnt, ret = false;
>  	char cmdline[4096], *c;
> -	int fd, ret = false;
> +	size_t pat_len;
>
>  	fd = open("/proc/cmdline", O_RDONLY);
>  	if (fd < 0) {
> @@ -81,14 +82,16 @@ static bool cmdline_contains(const char *pat)
>  		return false;
>  	}
>
> -	if (read(fd, cmdline, sizeof(cmdline) - 1) < 0) {
> +	cnt = read(fd, cmdline, sizeof(cmdline) - 1);
> +	if (cnt < 0) {
>  		perror("read /proc/cmdline");
>  		goto out;
>  	}
>
> -	cmdline[sizeof(cmdline) - 1] = '\0';
> +	cmdline[cnt] = '\0';
> +	pat_len = strlen(pat);
>  	for (c = strtok(cmdline, " \n"); c; c = strtok(NULL, " \n")) {
> -		if (strncmp(c, pat, strlen(c)))
> +		if (strlen(c) != pat_len || strcmp(c, pat))
>  			continue;
>  		ret = true;
>  		break;

This isn't a bug, but since strcmp() already fails on strings of
different lengths, would `if (strcmp(c, pat))` on its own be enough
here, letting `pat_len` go away and matching how config_contains()
does the same check?

The sibling helper in the same file expresses the same idea with a
bare strcmp:

    config_contains(), unpriv_helpers.c:
        if (strcmp(buf, pat) == 0) {


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/30936192563

  reply	other threads:[~2026-08-04 18:49 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-04 17:45 [PATCH bpf-next v6 0/9] bpf: add support for KASAN checks in JITed programs Alexis Lothoré (eBPF Foundation)
2026-08-04 17:45 ` [PATCH bpf-next v6 1/9] bpf: mark instructions accessing program stack Alexis Lothoré (eBPF Foundation)
2026-08-04 17:45 ` [PATCH bpf-next v6 2/9] bpf: add BPF_JIT_KASAN for KASAN instrumentation of JITed programs Alexis Lothoré (eBPF Foundation)
2026-08-04 17:45 ` [PATCH bpf-next v6 3/9] bpf, x86: refactor BPF_ST management in do_jit Alexis Lothoré (eBPF Foundation)
2026-08-04 19:04   ` bot+bpf-ci
2026-08-04 17:45 ` [PATCH bpf-next v6 4/9] bpf, x86: emit KASAN checks in x86 JITed programs Alexis Lothoré (eBPF Foundation)
2026-08-04 17:45 ` [PATCH bpf-next v6 5/9] bpf, x86: enable KASAN for JITed programs on x86 Alexis Lothoré (eBPF Foundation)
2026-08-04 17:45 ` [PATCH bpf-next v6 6/9] selftests/bpf: make cmdline_contains stricter Alexis Lothoré (eBPF Foundation)
2026-08-04 18:49   ` bot+bpf-ci [this message]
2026-08-04 17:45 ` [PATCH bpf-next v6 7/9] selftests/bpf: add helpers for KASAN in JIT testing Alexis Lothoré (eBPF Foundation)
2026-08-04 17:45 ` [PATCH bpf-next v6 8/9] selftests/bpf: move bpf_jit_harden helper into testing_helpers Alexis Lothoré (eBPF Foundation)
2026-08-04 17:45 ` [PATCH bpf-next v6 9/9] selftests/bpf: add tests to validate KASAN on JIT programs Alexis Lothoré (eBPF Foundation)
2026-08-21 18:59 ` [PATCH bpf-next v6 0/9] bpf: add support for KASAN checks in JITed programs Andrii Nakryiko
2026-08-21 19:44   ` Alexis Lothoré

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=fd8d539b6c09ca90f3f00840e4c842ac8784b0ed4d0fcc018b3e43ec2dedd9cf@mail.kernel.org \
    --to=bot+bpf-ci@kernel.org \
    --cc=alexis.lothore@bootlin.com \
    --cc=andreyknvl@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bastien.curutchet@bootlin.com \
    --cc=bp@alien8.de \
    --cc=bpf@vger.kernel.org \
    --cc=clm@meta.com \
    --cc=daniel@iogearbox.net \
    --cc=dave.hansen@linux.intel.com \
    --cc=ebpf@linuxfoundation.org \
    --cc=eddyz87@gmail.com \
    --cc=hpa@zytor.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=martin.lau@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=mingo@redhat.com \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=tglx@kernel.org \
    --cc=thomas.petazzoni@bootlin.com \
    --cc=x86@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®