Merge branch 'bpf-follow-up-fixes-for-bpf-syscall-common-attributes'

Leon Hwang says:

====================
bpf: Follow-up fixes for BPF syscall common attributes

Address sashiko reviews for BPF syscall common attributes series.

1. The tailing padding bytes in struct bpf_common_attr should be
   checked. [1]
2. There was a concurrent regression in syscall.c::map_create(). [2]
3. OPTS_VALID() was missing to validate the nested struct bpf_log_opts
   in libbpf. [3]
4. The token_fd should be -1 to avoid a valid token fd in test. [4]

A test is added to verify the fix #1.

The fix #2 is hard to be verified by test. So, I decide not to add a test
for it to avoid over-engineering.

Decide not to add a test for fix #3 to avoid over-engineering, as the
fix looks really simple.

Links:
[1] https://lore.kernel.org/bpf/20260513224823.6494FC19425@smtp.kernel.org/
[2] https://lore.kernel.org/bpf/20260512233658.CEED7C2BCB0@smtp.kernel.org/
[3] https://lore.kernel.org/bpf/20260512235629.C5CABC2BCB0@smtp.kernel.org/
[4] https://lore.kernel.org/bpf/20260513003358.55836C2BCB0@smtp.kernel.org/
====================

Link: https://patch.msgid.link/20260518145446.6794-1-leon.hwang@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
Alexei Starovoitov 2026-05-18 18:51:14 -07:00
commit 3fc9a70a14
3 changed files with 33 additions and 2 deletions

View File

@ -6278,7 +6278,9 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size,
memset(&attr_common, 0, sizeof(attr_common));
if (cmd & BPF_COMMON_ATTRS) {
err = bpf_check_uarg_tail_zero(uattr_common, sizeof(attr_common), size_common);
err = bpf_check_uarg_tail_zero(uattr_common,
offsetofend(struct bpf_common_attr, log_true_size),
size_common);
if (err)
return err;

View File

@ -246,6 +246,9 @@ int bpf_map_create(enum bpf_map_type map_type,
attr.excl_prog_hash_size = OPTS_GET(opts, excl_prog_hash_size, 0);
log_opts = OPTS_GET(opts, log_opts, NULL);
if (!OPTS_VALID(log_opts, bpf_log_opts))
return libbpf_err(-EINVAL);
if (log_opts && feat_supported(NULL, FEAT_BPF_SYSCALL_COMMON_ATTRS)) {
memset(&attr_common, 0, attr_common_sz);
attr_common.log_buf = ptr_to_u64(OPTS_GET(log_opts, buf, NULL));

View File

@ -306,7 +306,7 @@ static void test_invalid_token_fd(void)
const char *msg = "Invalid map_token_fd.\n";
LIBBPF_OPTS(bpf_map_create_opts, opts,
.map_flags = BPF_F_TOKEN_FD,
.token_fd = 0xFF,
.token_fd = -1,
);
test_map_create_array(&opts, msg);
@ -353,6 +353,30 @@ static void test_excl_prog_hash_size_2(void)
test_map_create_array(&opts, msg);
}
static void test_common_attr_padding(void)
{
struct bpf_common_attr_fake {
__u8 attrs[offsetofend(struct bpf_common_attr, log_true_size)];
__u32 pad;
} attr_common = {
.pad = 1,
};
union bpf_attr attr = {
.map_type = BPF_MAP_TYPE_ARRAY,
.key_size = 4,
.value_size = 4,
.max_entries = 1,
};
int fd;
fd = syscall(__NR_bpf, BPF_MAP_CREATE | BPF_COMMON_ATTRS, &attr, sizeof(attr), &attr_common,
sizeof(attr_common));
if (!ASSERT_LT(fd, 0, "syscall"))
close(fd);
else
ASSERT_EQ(errno, E2BIG, "errno");
}
void test_map_create_failure(void)
{
if (test__start_subtest("invalid_vmlinux_value_type_id_struct_ops"))
@ -377,4 +401,6 @@ void test_map_create_failure(void)
test_excl_prog_hash_size_1();
if (test__start_subtest("invalid_excl_prog_hash_size_2"))
test_excl_prog_hash_size_2();
if (test__start_subtest("common_attr_padding"))
test_common_attr_padding();
}