~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/bpf/prog_tests/lookup_key.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 
  3 /*
  4  * Copyright (C) 2022 Huawei Technologies Duesseldorf GmbH
  5  *
  6  * Author: Roberto Sassu <roberto.sassu@huawei.com>
  7  */
  8 
  9 #include <linux/keyctl.h>
 10 #include <test_progs.h>
 11 
 12 #include "test_lookup_key.skel.h"
 13 
 14 #define KEY_LOOKUP_CREATE       0x01
 15 #define KEY_LOOKUP_PARTIAL      0x02
 16 
 17 static bool kfunc_not_supported;
 18 
 19 static int libbpf_print_cb(enum libbpf_print_level level, const char *fmt,
 20                            va_list args)
 21 {
 22         char *func;
 23 
 24         if (strcmp(fmt, "libbpf: extern (func ksym) '%s': not found in kernel or module BTFs\n"))
 25                 return 0;
 26 
 27         func = va_arg(args, char *);
 28 
 29         if (strcmp(func, "bpf_lookup_user_key") && strcmp(func, "bpf_key_put") &&
 30             strcmp(func, "bpf_lookup_system_key"))
 31                 return 0;
 32 
 33         kfunc_not_supported = true;
 34         return 0;
 35 }
 36 
 37 void test_lookup_key(void)
 38 {
 39         libbpf_print_fn_t old_print_cb;
 40         struct test_lookup_key *skel;
 41         __u32 next_id;
 42         int ret;
 43 
 44         skel = test_lookup_key__open();
 45         if (!ASSERT_OK_PTR(skel, "test_lookup_key__open"))
 46                 return;
 47 
 48         old_print_cb = libbpf_set_print(libbpf_print_cb);
 49         ret = test_lookup_key__load(skel);
 50         libbpf_set_print(old_print_cb);
 51 
 52         if (ret < 0 && kfunc_not_supported) {
 53                 printf("%s:SKIP:bpf_lookup_*_key(), bpf_key_put() kfuncs not supported\n",
 54                        __func__);
 55                 test__skip();
 56                 goto close_prog;
 57         }
 58 
 59         if (!ASSERT_OK(ret, "test_lookup_key__load"))
 60                 goto close_prog;
 61 
 62         ret = test_lookup_key__attach(skel);
 63         if (!ASSERT_OK(ret, "test_lookup_key__attach"))
 64                 goto close_prog;
 65 
 66         skel->bss->monitored_pid = getpid();
 67         skel->bss->key_serial = KEY_SPEC_THREAD_KEYRING;
 68 
 69         /* The thread-specific keyring does not exist, this test fails. */
 70         skel->bss->flags = 0;
 71 
 72         ret = bpf_prog_get_next_id(0, &next_id);
 73         if (!ASSERT_LT(ret, 0, "bpf_prog_get_next_id"))
 74                 goto close_prog;
 75 
 76         /* Force creation of the thread-specific keyring, this test succeeds. */
 77         skel->bss->flags = KEY_LOOKUP_CREATE;
 78 
 79         ret = bpf_prog_get_next_id(0, &next_id);
 80         if (!ASSERT_OK(ret, "bpf_prog_get_next_id"))
 81                 goto close_prog;
 82 
 83         /* Pass both lookup flags for parameter validation. */
 84         skel->bss->flags = KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL;
 85 
 86         ret = bpf_prog_get_next_id(0, &next_id);
 87         if (!ASSERT_OK(ret, "bpf_prog_get_next_id"))
 88                 goto close_prog;
 89 
 90         /* Pass invalid flags. */
 91         skel->bss->flags = UINT64_MAX;
 92 
 93         ret = bpf_prog_get_next_id(0, &next_id);
 94         if (!ASSERT_LT(ret, 0, "bpf_prog_get_next_id"))
 95                 goto close_prog;
 96 
 97         skel->bss->key_serial = 0;
 98         skel->bss->key_id = 1;
 99 
100         ret = bpf_prog_get_next_id(0, &next_id);
101         if (!ASSERT_OK(ret, "bpf_prog_get_next_id"))
102                 goto close_prog;
103 
104         skel->bss->key_id = UINT32_MAX;
105 
106         ret = bpf_prog_get_next_id(0, &next_id);
107         ASSERT_LT(ret, 0, "bpf_prog_get_next_id");
108 
109 close_prog:
110         skel->bss->monitored_pid = 0;
111         test_lookup_key__destroy(skel);
112 }
113 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php