]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests/bpf: Cover exclusive map create-time validation
authorDaniel Borkmann <daniel@iogearbox.net>
Wed, 3 Jun 2026 21:16:57 +0000 (23:16 +0200)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 4 Jun 2026 16:44:42 +0000 (09:44 -0700)
map_excl exercises exclusive-map binding (allowed/denied), map-in-map
and map iterator rejection. It does not cover the create-time validation
of excl_prog_hash: the kernel only accepts a SHA-256-sized hash and
requires the pointer and size to be consistent.

Add map_excl_create_validation to check the rejected combinations:

  # LDLIBS=-static PKG_CONFIG='pkg-config --static' ./vmtest.sh -- ./test_progs -t map_excl
  [...]
  [    1.780305] clocksource: Switched to clocksource tsc
  #215/1   map_excl/map_excl_allowed:OK
  #215/2   map_excl/map_excl_denied:OK
  #215/3   map_excl/map_excl_no_map_in_map:OK
  #215/4   map_excl/map_excl_no_map_iter:OK
  #215/5   map_excl/map_excl_create_validation:OK
  #215     map_excl:OK
  Summary: 1/5 PASSED, 0 SKIPPED, 0 FAILED

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/r/20260603211658.471212-1-daniel@iogearbox.net
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/prog_tests/map_excl.c

index 3088668e2e450b1131273a294cefee9941ff5011..3f4422b9ffa699a768407aa36502147343592ad8 100644 (file)
@@ -126,6 +126,41 @@ out:
        close(excl_fd);
 }
 
+static void test_map_excl_create_validation(void)
+{
+       LIBBPF_OPTS(bpf_map_create_opts, o);
+       __u8 hash[SHA256_DIGEST_SIZE] = {};
+       int fd;
+
+       o.excl_prog_hash = hash;
+       o.excl_prog_hash_size = SHA256_DIGEST_SIZE / 2;
+       fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "excl", 4, 4, 1, &o);
+       if (fd >= 0)
+               close(fd);
+       ASSERT_EQ(fd, -EINVAL, "reject short excl_prog_hash_size");
+
+       o.excl_prog_hash = hash;
+       o.excl_prog_hash_size = SHA256_DIGEST_SIZE * 2;
+       fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "excl", 4, 4, 1, &o);
+       if (fd >= 0)
+               close(fd);
+       ASSERT_EQ(fd, -EINVAL, "reject long excl_prog_hash_size");
+
+       o.excl_prog_hash = hash;
+       o.excl_prog_hash_size = 0;
+       fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "excl", 4, 4, 1, &o);
+       if (fd >= 0)
+               close(fd);
+       ASSERT_EQ(fd, -EINVAL, "reject hash pointer with zero size");
+
+       o.excl_prog_hash = NULL;
+       o.excl_prog_hash_size = SHA256_DIGEST_SIZE;
+       fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "excl", 4, 4, 1, &o);
+       if (fd >= 0)
+               close(fd);
+       ASSERT_EQ(fd, -EINVAL, "reject size with NULL hash pointer");
+}
+
 void test_map_excl(void)
 {
        if (test__start_subtest("map_excl_allowed"))
@@ -136,4 +171,6 @@ void test_map_excl(void)
                test_map_excl_no_map_in_map();
        if (test__start_subtest("map_excl_no_map_iter"))
                test_map_excl_no_map_iter();
+       if (test__start_subtest("map_excl_create_validation"))
+               test_map_excl_create_validation();
 }