Skip to content

Commit 3f47f09

Browse files
authored
Merge branch 'kernel-patches:bpf-next_base' into bpf-next-rdtsc
2 parents f47a229 + 720c696 commit 3f47f09

18 files changed

+134
-36
lines changed

include/linux/lsm_hook_defs.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -426,14 +426,14 @@ LSM_HOOK(void, LSM_RET_VOID, audit_rule_free, void *lsmrule)
426426
#endif /* CONFIG_AUDIT */
427427

428428
#ifdef CONFIG_BPF_SYSCALL
429-
LSM_HOOK(int, 0, bpf, int cmd, union bpf_attr *attr, unsigned int size)
429+
LSM_HOOK(int, 0, bpf, int cmd, union bpf_attr *attr, unsigned int size, bool kernel)
430430
LSM_HOOK(int, 0, bpf_map, struct bpf_map *map, fmode_t fmode)
431431
LSM_HOOK(int, 0, bpf_prog, struct bpf_prog *prog)
432432
LSM_HOOK(int, 0, bpf_map_create, struct bpf_map *map, union bpf_attr *attr,
433-
struct bpf_token *token)
433+
struct bpf_token *token, bool kernel)
434434
LSM_HOOK(void, LSM_RET_VOID, bpf_map_free, struct bpf_map *map)
435435
LSM_HOOK(int, 0, bpf_prog_load, struct bpf_prog *prog, union bpf_attr *attr,
436-
struct bpf_token *token)
436+
struct bpf_token *token, bool kernel)
437437
LSM_HOOK(void, LSM_RET_VOID, bpf_prog_free, struct bpf_prog *prog)
438438
LSM_HOOK(int, 0, bpf_token_create, struct bpf_token *token, union bpf_attr *attr,
439439
const struct path *path)

include/linux/security.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -2249,14 +2249,14 @@ struct bpf_map;
22492249
struct bpf_prog;
22502250
struct bpf_token;
22512251
#ifdef CONFIG_SECURITY
2252-
extern int security_bpf(int cmd, union bpf_attr *attr, unsigned int size);
2252+
extern int security_bpf(int cmd, union bpf_attr *attr, unsigned int size, bool kernel);
22532253
extern int security_bpf_map(struct bpf_map *map, fmode_t fmode);
22542254
extern int security_bpf_prog(struct bpf_prog *prog);
22552255
extern int security_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
2256-
struct bpf_token *token);
2256+
struct bpf_token *token, bool kernel);
22572257
extern void security_bpf_map_free(struct bpf_map *map);
22582258
extern int security_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,
2259-
struct bpf_token *token);
2259+
struct bpf_token *token, bool kernel);
22602260
extern void security_bpf_prog_free(struct bpf_prog *prog);
22612261
extern int security_bpf_token_create(struct bpf_token *token, union bpf_attr *attr,
22622262
const struct path *path);
@@ -2265,7 +2265,7 @@ extern int security_bpf_token_cmd(const struct bpf_token *token, enum bpf_cmd cm
22652265
extern int security_bpf_token_capable(const struct bpf_token *token, int cap);
22662266
#else
22672267
static inline int security_bpf(int cmd, union bpf_attr *attr,
2268-
unsigned int size)
2268+
unsigned int size, bool kernel)
22692269
{
22702270
return 0;
22712271
}
@@ -2281,7 +2281,7 @@ static inline int security_bpf_prog(struct bpf_prog *prog)
22812281
}
22822282

22832283
static inline int security_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
2284-
struct bpf_token *token)
2284+
struct bpf_token *token, bool kernel)
22852285
{
22862286
return 0;
22872287
}
@@ -2290,7 +2290,7 @@ static inline void security_bpf_map_free(struct bpf_map *map)
22902290
{ }
22912291

22922292
static inline int security_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,
2293-
struct bpf_token *token)
2293+
struct bpf_token *token, bool kernel)
22942294
{
22952295
return 0;
22962296
}

kernel/bpf/preload/bpf_preload_kern.c

+1
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,4 @@ static void __exit fini(void)
9090
late_initcall(load);
9191
module_exit(fini);
9292
MODULE_LICENSE("GPL");
93+
MODULE_DESCRIPTION("Embedded BPF programs for introspection in bpffs");

kernel/bpf/syscall.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -1332,7 +1332,7 @@ static bool bpf_net_capable(void)
13321332

13331333
#define BPF_MAP_CREATE_LAST_FIELD map_token_fd
13341334
/* called via syscall */
1335-
static int map_create(union bpf_attr *attr)
1335+
static int map_create(union bpf_attr *attr, bool kernel)
13361336
{
13371337
const struct bpf_map_ops *ops;
13381338
struct bpf_token *token = NULL;
@@ -1522,7 +1522,7 @@ static int map_create(union bpf_attr *attr)
15221522
attr->btf_vmlinux_value_type_id;
15231523
}
15241524

1525-
err = security_bpf_map_create(map, attr, token);
1525+
err = security_bpf_map_create(map, attr, token, kernel);
15261526
if (err)
15271527
goto free_map_sec;
15281528

@@ -2959,7 +2959,7 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
29592959
if (err < 0)
29602960
goto free_prog;
29612961

2962-
err = security_bpf_prog_load(prog, attr, token);
2962+
err = security_bpf_prog_load(prog, attr, token, uattr.is_kernel);
29632963
if (err)
29642964
goto free_prog_sec;
29652965

@@ -5784,13 +5784,13 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size)
57845784
if (copy_from_bpfptr(&attr, uattr, size) != 0)
57855785
return -EFAULT;
57865786

5787-
err = security_bpf(cmd, &attr, size);
5787+
err = security_bpf(cmd, &attr, size, uattr.is_kernel);
57885788
if (err < 0)
57895789
return err;
57905790

57915791
switch (cmd) {
57925792
case BPF_MAP_CREATE:
5793-
err = map_create(&attr);
5793+
err = map_create(&attr, uattr.is_kernel);
57945794
break;
57955795
case BPF_MAP_LOOKUP_ELEM:
57965796
err = map_lookup_elem(&attr);

security/security.c

+9-6
Original file line numberDiff line numberDiff line change
@@ -5627,16 +5627,17 @@ int security_audit_rule_match(struct lsm_prop *prop, u32 field, u32 op,
56275627
* @cmd: command
56285628
* @attr: bpf attribute
56295629
* @size: size
5630+
* @kernel: whether or not call originated from kernel
56305631
*
56315632
* Do a initial check for all bpf syscalls after the attribute is copied into
56325633
* the kernel. The actual security module can implement their own rules to
56335634
* check the specific cmd they need.
56345635
*
56355636
* Return: Returns 0 if permission is granted.
56365637
*/
5637-
int security_bpf(int cmd, union bpf_attr *attr, unsigned int size)
5638+
int security_bpf(int cmd, union bpf_attr *attr, unsigned int size, bool kernel)
56385639
{
5639-
return call_int_hook(bpf, cmd, attr, size);
5640+
return call_int_hook(bpf, cmd, attr, size, kernel);
56405641
}
56415642

56425643
/**
@@ -5673,23 +5674,25 @@ int security_bpf_prog(struct bpf_prog *prog)
56735674
* @map: BPF map object
56745675
* @attr: BPF syscall attributes used to create BPF map
56755676
* @token: BPF token used to grant user access
5677+
* @kernel: whether or not call originated from kernel
56765678
*
56775679
* Do a check when the kernel creates a new BPF map. This is also the
56785680
* point where LSM blob is allocated for LSMs that need them.
56795681
*
56805682
* Return: Returns 0 on success, error on failure.
56815683
*/
56825684
int security_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
5683-
struct bpf_token *token)
5685+
struct bpf_token *token, bool kernel)
56845686
{
5685-
return call_int_hook(bpf_map_create, map, attr, token);
5687+
return call_int_hook(bpf_map_create, map, attr, token, kernel);
56865688
}
56875689

56885690
/**
56895691
* security_bpf_prog_load() - Check if loading of BPF program is allowed
56905692
* @prog: BPF program object
56915693
* @attr: BPF syscall attributes used to create BPF program
56925694
* @token: BPF token used to grant user access to BPF subsystem
5695+
* @kernel: whether or not call originated from kernel
56935696
*
56945697
* Perform an access control check when the kernel loads a BPF program and
56955698
* allocates associated BPF program object. This hook is also responsible for
@@ -5698,9 +5701,9 @@ int security_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
56985701
* Return: Returns 0 on success, error on failure.
56995702
*/
57005703
int security_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,
5701-
struct bpf_token *token)
5704+
struct bpf_token *token, bool kernel)
57025705
{
5703-
return call_int_hook(bpf_prog_load, prog, attr, token);
5706+
return call_int_hook(bpf_prog_load, prog, attr, token, kernel);
57045707
}
57055708

57065709
/**

security/selinux/hooks.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -6866,7 +6866,7 @@ static int selinux_ib_alloc_security(void *ib_sec)
68666866

68676867
#ifdef CONFIG_BPF_SYSCALL
68686868
static int selinux_bpf(int cmd, union bpf_attr *attr,
6869-
unsigned int size)
6869+
unsigned int size, bool kernel)
68706870
{
68716871
u32 sid = current_sid();
68726872
int ret;
@@ -6953,7 +6953,7 @@ static int selinux_bpf_prog(struct bpf_prog *prog)
69536953
}
69546954

69556955
static int selinux_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
6956-
struct bpf_token *token)
6956+
struct bpf_token *token, bool kernel)
69576957
{
69586958
struct bpf_security_struct *bpfsec;
69596959

@@ -6976,7 +6976,7 @@ static void selinux_bpf_map_free(struct bpf_map *map)
69766976
}
69776977

69786978
static int selinux_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,
6979-
struct bpf_token *token)
6979+
struct bpf_token *token, bool kernel)
69806980
{
69816981
struct bpf_security_struct *bpfsec;
69826982

tools/bpf/bpftool/prog.c

+1
Original file line numberDiff line numberDiff line change
@@ -1928,6 +1928,7 @@ static int do_loader(int argc, char **argv)
19281928

19291929
obj = bpf_object__open_file(file, &open_opts);
19301930
if (!obj) {
1931+
err = -1;
19311932
p_err("failed to open object file");
19321933
goto err_close_obj;
19331934
}

tools/testing/selftests/bpf/bpf_arena_spin_lock.h

+22-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,28 @@
2222

2323
extern unsigned long CONFIG_NR_CPUS __kconfig;
2424

25-
#define arena_spinlock_t struct qspinlock
25+
/*
26+
* Typically, we'd just rely on the definition in vmlinux.h for qspinlock, but
27+
* PowerPC overrides the definition to define lock->val as u32 instead of
28+
* atomic_t, leading to compilation errors. Import a local definition below so
29+
* that we don't depend on the vmlinux.h version.
30+
*/
31+
32+
struct __qspinlock {
33+
union {
34+
atomic_t val;
35+
struct {
36+
u8 locked;
37+
u8 pending;
38+
};
39+
struct {
40+
u16 locked_pending;
41+
u16 tail;
42+
};
43+
};
44+
};
45+
46+
#define arena_spinlock_t struct __qspinlock
2647
/* FIXME: Using typedef causes CO-RE relocation error */
2748
/* typedef struct qspinlock arena_spinlock_t; */
2849

tools/testing/selftests/bpf/prog_tests/arena_spin_lock.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#include <network_helpers.h>
55
#include <sys/sysinfo.h>
66

7-
struct qspinlock { int val; };
8-
typedef struct qspinlock arena_spinlock_t;
7+
struct __qspinlock { int val; };
8+
typedef struct __qspinlock arena_spinlock_t;
99

1010
struct arena_qnode {
1111
unsigned long next;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2025 Microsoft */
3+
#include <test_progs.h>
4+
#include "kfunc_call_test.skel.h"
5+
#include "kfunc_call_test.lskel.h"
6+
#include "test_kernel_flag.skel.h"
7+
8+
void test_kernel_flag(void)
9+
{
10+
struct test_kernel_flag *lsm_skel;
11+
struct kfunc_call_test *skel = NULL;
12+
struct kfunc_call_test_lskel *lskel = NULL;
13+
int ret;
14+
15+
lsm_skel = test_kernel_flag__open_and_load();
16+
if (!ASSERT_OK_PTR(lsm_skel, "lsm_skel"))
17+
return;
18+
19+
lsm_skel->bss->monitored_tid = gettid();
20+
21+
ret = test_kernel_flag__attach(lsm_skel);
22+
if (!ASSERT_OK(ret, "test_kernel_flag__attach"))
23+
goto close_prog;
24+
25+
/* Test with skel. This should pass the gatekeeper */
26+
skel = kfunc_call_test__open_and_load();
27+
if (!ASSERT_OK_PTR(skel, "skel"))
28+
goto close_prog;
29+
30+
/* Test with lskel. This should fail due to blocking kernel-based bpf() invocations */
31+
lskel = kfunc_call_test_lskel__open_and_load();
32+
if (!ASSERT_ERR_PTR(lskel, "lskel"))
33+
goto close_prog;
34+
35+
close_prog:
36+
if (skel)
37+
kfunc_call_test__destroy(skel);
38+
if (lskel)
39+
kfunc_call_test_lskel__destroy(lskel);
40+
41+
lsm_skel->bss->monitored_tid = 0;
42+
test_kernel_flag__destroy(lsm_skel);
43+
}

tools/testing/selftests/bpf/progs/rcu_read_lock.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ int inproper_sleepable_helper(void *ctx)
242242
}
243243

244244
SEC("?lsm.s/bpf")
245-
int BPF_PROG(inproper_sleepable_kfunc, int cmd, union bpf_attr *attr, unsigned int size)
245+
int BPF_PROG(inproper_sleepable_kfunc, int cmd, union bpf_attr *attr, unsigned int size,
246+
bool kernel)
246247
{
247248
struct bpf_key *bkey;
248249

tools/testing/selftests/bpf/progs/test_cgroup1_hierarchy.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ static int bpf_link_create_verify(int cmd)
5151
}
5252

5353
SEC("lsm/bpf")
54-
int BPF_PROG(lsm_run, int cmd, union bpf_attr *attr, unsigned int size)
54+
int BPF_PROG(lsm_run, int cmd, union bpf_attr *attr, unsigned int size, bool kernel)
5555
{
5656
return bpf_link_create_verify(cmd);
5757
}
5858

5959
SEC("lsm.s/bpf")
60-
int BPF_PROG(lsm_s_run, int cmd, union bpf_attr *attr, unsigned int size)
60+
int BPF_PROG(lsm_s_run, int cmd, union bpf_attr *attr, unsigned int size, bool kernel)
6161
{
6262
return bpf_link_create_verify(cmd);
6363
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
/*
4+
* Copyright (C) 2025 Microsoft Corporation
5+
*
6+
* Author: Blaise Boscaccy <[email protected]>
7+
*/
8+
9+
#include "vmlinux.h"
10+
#include <errno.h>
11+
#include <bpf/bpf_helpers.h>
12+
#include <bpf/bpf_tracing.h>
13+
14+
char _license[] SEC("license") = "GPL";
15+
16+
__u32 monitored_tid;
17+
18+
SEC("lsm.s/bpf")
19+
int BPF_PROG(bpf, int cmd, union bpf_attr *attr, unsigned int size, bool kernel)
20+
{
21+
__u32 tid;
22+
23+
tid = bpf_get_current_pid_tgid() & 0xFFFFFFFF;
24+
if (!kernel || tid != monitored_tid)
25+
return 0;
26+
else
27+
return -EINVAL;
28+
}

tools/testing/selftests/bpf/progs/test_kfunc_dynptr_param.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ char _license[] SEC("license") = "GPL";
3636

3737
SEC("?lsm.s/bpf")
3838
__failure __msg("cannot pass in dynptr at an offset=-8")
39-
int BPF_PROG(not_valid_dynptr, int cmd, union bpf_attr *attr, unsigned int size)
39+
int BPF_PROG(not_valid_dynptr, int cmd, union bpf_attr *attr, unsigned int size, bool kernel)
4040
{
4141
unsigned long val;
4242

@@ -46,7 +46,7 @@ int BPF_PROG(not_valid_dynptr, int cmd, union bpf_attr *attr, unsigned int size)
4646

4747
SEC("?lsm.s/bpf")
4848
__failure __msg("arg#0 expected pointer to stack or const struct bpf_dynptr")
49-
int BPF_PROG(not_ptr_to_stack, int cmd, union bpf_attr *attr, unsigned int size)
49+
int BPF_PROG(not_ptr_to_stack, int cmd, union bpf_attr *attr, unsigned int size, bool kernel)
5050
{
5151
unsigned long val = 0;
5252

@@ -55,7 +55,7 @@ int BPF_PROG(not_ptr_to_stack, int cmd, union bpf_attr *attr, unsigned int size)
5555
}
5656

5757
SEC("lsm.s/bpf")
58-
int BPF_PROG(dynptr_data_null, int cmd, union bpf_attr *attr, unsigned int size)
58+
int BPF_PROG(dynptr_data_null, int cmd, union bpf_attr *attr, unsigned int size, bool kernel)
5959
{
6060
struct bpf_key *trusted_keyring;
6161
struct bpf_dynptr ptr;

tools/testing/selftests/bpf/progs/test_lookup_key.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extern struct bpf_key *bpf_lookup_system_key(__u64 id) __ksym;
2323
extern void bpf_key_put(struct bpf_key *key) __ksym;
2424

2525
SEC("lsm.s/bpf")
26-
int BPF_PROG(bpf, int cmd, union bpf_attr *attr, unsigned int size)
26+
int BPF_PROG(bpf, int cmd, union bpf_attr *attr, unsigned int size, bool kernel)
2727
{
2828
struct bpf_key *bkey;
2929
__u32 pid;

tools/testing/selftests/bpf/progs/test_ptr_untrusted.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
char tp_name[128];
88

99
SEC("lsm.s/bpf")
10-
int BPF_PROG(lsm_run, int cmd, union bpf_attr *attr, unsigned int size)
10+
int BPF_PROG(lsm_run, int cmd, union bpf_attr *attr, unsigned int size, bool kernel)
1111
{
1212
switch (cmd) {
1313
case BPF_RAW_TRACEPOINT_OPEN:

tools/testing/selftests/bpf/progs/test_task_under_cgroup.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ int BPF_PROG(tp_btf_run, struct task_struct *task, u64 clone_flags)
4949
}
5050

5151
SEC("lsm.s/bpf")
52-
int BPF_PROG(lsm_run, int cmd, union bpf_attr *attr, unsigned int size)
52+
int BPF_PROG(lsm_run, int cmd, union bpf_attr *attr, unsigned int size, bool kernel)
5353
{
5454
struct cgroup *cgrp = NULL;
5555
struct task_struct *task;

0 commit comments

Comments
 (0)