]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests/bpf: Use bpf_dynptr_slice() to read file dynptr in leak test
authorAmery Hung <ameryhung@gmail.com>
Fri, 5 Jun 2026 20:20:56 +0000 (13:20 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 5 Jun 2026 21:18:20 +0000 (14:18 -0700)
use_file_dynptr_slice_after_put_file() reads the dynptr via
bpf_dynptr_data(), which always returns NULL for a read-only file
dynptr, making the example confusing. Switch to bpf_dynptr_slice(), the
correct read API for file dynptrs, and read (rather than write) the slice
since it is read-only. The test still fails as expected.

Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Amery Hung <ameryhung@gmail.com>
Link: https://lore.kernel.org/r/20260605202056.1780352-6-ameryhung@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/progs/file_reader_fail.c

index d5fae5e4cf9adbe5ef7edec2e4714ff40d47045e..3bb9e2612f8f39e8b863eb7621f6c478330ee4e7 100644 (file)
@@ -87,7 +87,8 @@ int use_file_dynptr_slice_after_put_file(void *ctx)
        struct task_struct *task = bpf_get_current_task_btf();
        struct file *file = bpf_get_task_exe_file(task);
        struct bpf_dynptr dynptr;
-       char *data;
+       char buf[1];
+       const char *data;
 
        if (!file)
                return 0;
@@ -95,15 +96,14 @@ int use_file_dynptr_slice_after_put_file(void *ctx)
        if (bpf_dynptr_from_file(file, 0, &dynptr))
                goto out;
 
-       data = bpf_dynptr_data(&dynptr, 0, 1);
+       data = bpf_dynptr_slice(&dynptr, 0, buf, sizeof(buf));
        if (!data)
                goto out;
 
        /* this should fail - file dynptr should be discarded first to prevent resource leak */
        bpf_put_file(file);
 
-       *data = 'x';
-       return 0;
+       return data[0];
 
 out:
        bpf_dynptr_file_discard(&dynptr);