1 // SPDX-License-Identifier: GPL-2.0 2 3 #include "vmlinux.h" 4 #include <bpf/bpf_helpers.h> 5 #include "bpf_misc.h" 6 7 /* 8 * A uprobe prog can write only the registers that ptrace can set to any 9 * value. The fields the kernel restores on the way to user space (segments, 10 * flags, privilege state) are read-only. 11 */ 12 13 #define WRITE_CTX(name, field, sz) \ 14 __naked void name(void) \ 15 { \ 16 asm volatile (" \ 17 r2 = 0; \ 18 *(" #sz " *)(r1 + %[off]) = r2; \ 19 r0 = 0; \ 20 exit; \ 21 " : \ 22 : __imm_const(off, offsetof(struct pt_regs, field)) \ 23 : __clobber_all); \ 24 } 25 26 #define READ_CTX(name, field) \ 27 __naked void name(void) \ 28 { \ 29 asm volatile (" \ 30 r0 = *(u64 *)(r1 + %[off]); \ 31 r0 = 0; \ 32 exit; \ 33 " : \ 34 : __imm_const(off, offsetof(struct pt_regs, field)) \ 35 : __clobber_all); \ 36 } 37 38 #if defined(__TARGET_ARCH_x86) 39 40 SEC("uprobe") 41 __description("uprobe: write ctx->ax") 42 __success 43 WRITE_CTX(write_ax, ax, u64) 44 45 SEC("uprobe") 46 __description("uprobe: write ctx->r15") 47 __success 48 WRITE_CTX(write_r15, r15, u64) 49 50 SEC("uprobe") 51 __description("uprobe: write ctx->di") 52 __success 53 WRITE_CTX(write_di, di, u64) 54 55 SEC("uprobe") 56 __description("uprobe: write ctx->ip") 57 __success 58 WRITE_CTX(write_ip, ip, u64) 59 60 SEC("uprobe") 61 __description("uprobe: write ctx->sp") 62 __success 63 WRITE_CTX(write_sp, sp, u64) 64 65 SEC("uprobe") 66 __description("uprobe: write lower half of ctx->ax") 67 __success 68 WRITE_CTX(write_ax_u32, ax, u32) 69 70 SEC("uprobe") 71 __description("uprobe: write ctx->orig_ax") 72 __failure __msg("invalid bpf_context access") 73 WRITE_CTX(write_orig_ax, orig_ax, u64) 74 75 SEC("uprobe") 76 __description("uprobe: write ctx->cs") 77 __failure __msg("invalid bpf_context access") 78 WRITE_CTX(write_cs, cs, u64) 79 80 SEC("uprobe") 81 __description("uprobe: write lower half of ctx->cs") 82 __failure __msg("invalid bpf_context access") 83 WRITE_CTX(write_cs_u32, cs, u32) 84 85 SEC("uprobe") 86 __description("uprobe: write ctx->flags") 87 __failure __msg("invalid bpf_context access") 88 WRITE_CTX(write_flags, flags, u64) 89 90 SEC("uprobe") 91 __description("uprobe: write ctx->ss") 92 __failure __msg("invalid bpf_context access") 93 WRITE_CTX(write_ss, ss, u64) 94 95 SEC("uprobe") 96 __description("uprobe: read ctx->flags") 97 __success 98 READ_CTX(read_flags, flags) 99 100 SEC("kprobe") 101 __description("kprobe: write ctx->flags") 102 __failure __msg("invalid bpf_context access") 103 WRITE_CTX(kprobe_write_flags, flags, u64) 104 105 #elif defined(__TARGET_ARCH_arm64) 106 107 SEC("uprobe") 108 __description("uprobe: write ctx->regs[0]") 109 __success 110 WRITE_CTX(write_x0, regs[0], u64) 111 112 SEC("uprobe") 113 __description("uprobe: write ctx->regs[30]") 114 __success 115 WRITE_CTX(write_x30, regs[30], u64) 116 117 SEC("uprobe") 118 __description("uprobe: write ctx->sp") 119 __success 120 WRITE_CTX(write_sp, sp, u64) 121 122 SEC("uprobe") 123 __description("uprobe: write ctx->pc") 124 __success 125 WRITE_CTX(write_pc, pc, u64) 126 127 SEC("uprobe") 128 __description("uprobe: write ctx->pstate") 129 __failure __msg("invalid bpf_context access") 130 WRITE_CTX(write_pstate, pstate, u64) 131 132 SEC("uprobe") 133 __description("uprobe: write lower half of ctx->pstate") 134 __failure __msg("invalid bpf_context access") 135 WRITE_CTX(write_pstate_u32, pstate, u32) 136 137 SEC("uprobe") 138 __description("uprobe: write ctx->orig_x0") 139 __failure __msg("invalid bpf_context access") 140 WRITE_CTX(write_orig_x0, orig_x0, u64) 141 142 SEC("uprobe") 143 __description("uprobe: read ctx->pstate") 144 __success 145 READ_CTX(read_pstate, pstate) 146 147 #elif defined(__TARGET_ARCH_riscv) 148 149 SEC("uprobe") 150 __description("uprobe: write ctx->epc") 151 __success 152 WRITE_CTX(write_epc, epc, u64) 153 154 SEC("uprobe") 155 __description("uprobe: write ctx->a0") 156 __success 157 WRITE_CTX(write_a0, a0, u64) 158 159 SEC("uprobe") 160 __description("uprobe: write ctx->t6") 161 __success 162 WRITE_CTX(write_t6, t6, u64) 163 164 SEC("uprobe") 165 __description("uprobe: write ctx->status") 166 __failure __msg("invalid bpf_context access") 167 WRITE_CTX(write_status, status, u64) 168 169 SEC("uprobe") 170 __description("uprobe: write ctx->badaddr") 171 __failure __msg("invalid bpf_context access") 172 WRITE_CTX(write_badaddr, badaddr, u64) 173 174 SEC("uprobe") 175 __description("uprobe: write ctx->cause") 176 __failure __msg("invalid bpf_context access") 177 WRITE_CTX(write_cause, cause, u64) 178 179 SEC("uprobe") 180 __description("uprobe: read ctx->status") 181 __success 182 READ_CTX(read_status, status) 183 184 #else 185 186 SEC("uprobe") 187 __description("uprobe: ctx writes not checked on this arch") 188 __success 189 __naked void unsupported(void) 190 { 191 asm volatile ("r0 = 0; exit;" ::: __clobber_all); 192 } 193 194 #endif 195 196 char _license[] SEC("license") = "GPL"; 197