Skip to content

Commit 85a3fa6

Browse files
committed
selftests/bpf: Add test for writes to .rodata
Add a small test to write a (verification-time) fixed vs unknown but bounded-sized buffer into .rodata BPF map and assert that both get rejected. # ./vmtest.sh -- ./test_progs -t verifier_const [...] ./test_progs -t verifier_const [ 1.418717] tsc: Refined TSC clocksource calibration: 3407.994 MHz [ 1.419113] clocksource: tsc: mask: 0xffffffffffffffff max_cycles: 0x311fcde90a1, max_idle_ns: 440795222066 ns [ 1.419972] clocksource: Switched to clocksource tsc [ 1.449596] bpf_testmod: loading out-of-tree module taints kernel. [ 1.449958] bpf_testmod: module verification failed: signature and/or required key missing - tainting kernel torvalds#475/1 verifier_const/rodata/strtol: write rejected:OK torvalds#475/2 verifier_const/bss/strtol: write accepted:OK torvalds#475/3 verifier_const/data/strtol: write accepted:OK torvalds#475/4 verifier_const/rodata/mtu: write rejected:OK torvalds#475/5 verifier_const/bss/mtu: write accepted:OK torvalds#475/6 verifier_const/data/mtu: write accepted:OK torvalds#475/7 verifier_const/rodata/mark: write with unknown reg rejected:OK torvalds#475/8 verifier_const/rodata/mark: write with unknown reg rejected:OK torvalds#475 verifier_const:OK torvalds#476/1 verifier_const_or/constant register |= constant should keep constant type:OK torvalds#476/2 verifier_const_or/constant register |= constant should not bypass stack boundary checks:OK torvalds#476/3 verifier_const_or/constant register |= constant register should keep constant type:OK torvalds#476/4 verifier_const_or/constant register |= constant register should not bypass stack boundary checks:OK torvalds#476 verifier_const_or:OK Summary: 2/12 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 9ea8813 commit 85a3fa6

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// SPDX-License-Identifier: GPL-2.0
22
/* Copyright (c) 2024 Isovalent */
33

4-
#include <linux/bpf.h>
4+
#include "vmlinux.h"
55
#include <bpf/bpf_helpers.h>
6+
#include <bpf/bpf_tracing.h>
67
#include "bpf_misc.h"
78

89
const volatile long foo = 42;
@@ -66,4 +67,32 @@ int tcx6(struct __sk_buff *skb)
6667
return TCX_PASS;
6768
}
6869

70+
static inline void write_fixed(volatile void *p, __u32 val)
71+
{
72+
*(volatile __u32 *)p = val;
73+
}
74+
75+
static inline void write_dyn(void *p, void *val, int len)
76+
{
77+
bpf_copy_from_user(p, len, val);
78+
}
79+
80+
SEC("tc/ingress")
81+
__description("rodata/mark: write with unknown reg rejected")
82+
__failure __msg("write into map forbidden")
83+
int tcx7(struct __sk_buff *skb)
84+
{
85+
write_fixed((void *)&foo, skb->mark);
86+
return TCX_PASS;
87+
}
88+
89+
SEC("lsm.s/bprm_committed_creds")
90+
__description("rodata/mark: write with unknown reg rejected")
91+
__failure __msg("write into map forbidden")
92+
int BPF_PROG(bprm, struct linux_binprm *bprm)
93+
{
94+
write_dyn((void *)&foo, &bart, bpf_get_prandom_u32() & 3);
95+
return 0;
96+
}
97+
6998
char LICENSE[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)