File: timer_crash.c

package info (click to toggle)
linux 6.12.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,673,568 kB
  • sloc: ansic: 25,888,630; asm: 268,782; sh: 136,481; python: 64,809; makefile: 55,668; perl: 38,052; xml: 19,270; cpp: 5,893; yacc: 4,923; lex: 2,939; awk: 1,592; sed: 28; ruby: 25
file content (54 lines) | stat: -rw-r--r-- 1,213 bytes parent folder | download | duplicates (18)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// SPDX-License-Identifier: GPL-2.0

#include <vmlinux.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_helpers.h>

struct map_elem {
	struct bpf_timer timer;
	struct bpf_spin_lock lock;
};

struct {
	__uint(type, BPF_MAP_TYPE_ARRAY);
	__uint(max_entries, 1);
	__type(key, int);
	__type(value, struct map_elem);
} amap SEC(".maps");

struct {
	__uint(type, BPF_MAP_TYPE_HASH);
	__uint(max_entries, 1);
	__type(key, int);
	__type(value, struct map_elem);
} hmap SEC(".maps");

int pid = 0;
int crash_map = 0; /* 0 for amap, 1 for hmap */

SEC("fentry/do_nanosleep")
int sys_enter(void *ctx)
{
	struct map_elem *e, value = {};
	void *map = crash_map ? (void *)&hmap : (void *)&amap;

	if (bpf_get_current_task_btf()->tgid != pid)
		return 0;

	*(void **)&value = (void *)0xdeadcaf3;

	bpf_map_update_elem(map, &(int){0}, &value, 0);
	/* For array map, doing bpf_map_update_elem will do a
	 * check_and_free_timer_in_array, which will trigger the crash if timer
	 * pointer was overwritten, for hmap we need to use bpf_timer_cancel.
	 */
	if (crash_map == 1) {
		e = bpf_map_lookup_elem(map, &(int){0});
		if (!e)
			return 0;
		bpf_timer_cancel(&e->timer);
	}
	return 0;
}

char _license[] SEC("license") = "GPL";