Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Add uretprobe syscall call from user space test

Adding test to verify that when called from outside of the
trampoline provided by kernel, the uretprobe syscall will cause
calling process to receive SIGILL signal and the attached bpf
program is not executed.

Link: https://lore.kernel.org/all/20240611112158.40795-8-jolsa@kernel.org/

Acked-by: Andrii Nakryiko <andrii@kernel.org>
Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>

authored by

Jiri Olsa and committed by
Masami Hiramatsu (Google)
9e7f74e6 f42a58ff

+112
+95
tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c
··· 7 7 #include <unistd.h> 8 8 #include <asm/ptrace.h> 9 9 #include <linux/compiler.h> 10 + #include <linux/stringify.h> 11 + #include <sys/wait.h> 10 12 #include "uprobe_syscall.skel.h" 13 + #include "uprobe_syscall_executed.skel.h" 11 14 12 15 __naked unsigned long uretprobe_regs_trigger(void) 13 16 { ··· 212 209 } 213 210 } 214 211 212 + #ifndef __NR_uretprobe 213 + #define __NR_uretprobe 463 214 + #endif 215 + 216 + __naked unsigned long uretprobe_syscall_call_1(void) 217 + { 218 + /* 219 + * Pretend we are uretprobe trampoline to trigger the return 220 + * probe invocation in order to verify we get SIGILL. 221 + */ 222 + asm volatile ( 223 + "pushq %rax\n" 224 + "pushq %rcx\n" 225 + "pushq %r11\n" 226 + "movq $" __stringify(__NR_uretprobe) ", %rax\n" 227 + "syscall\n" 228 + "popq %r11\n" 229 + "popq %rcx\n" 230 + "retq\n" 231 + ); 232 + } 233 + 234 + __naked unsigned long uretprobe_syscall_call(void) 235 + { 236 + asm volatile ( 237 + "call uretprobe_syscall_call_1\n" 238 + "retq\n" 239 + ); 240 + } 241 + 242 + static void test_uretprobe_syscall_call(void) 243 + { 244 + LIBBPF_OPTS(bpf_uprobe_multi_opts, opts, 245 + .retprobe = true, 246 + ); 247 + struct uprobe_syscall_executed *skel; 248 + int pid, status, err, go[2], c; 249 + 250 + if (ASSERT_OK(pipe(go), "pipe")) 251 + return; 252 + 253 + skel = uprobe_syscall_executed__open_and_load(); 254 + if (!ASSERT_OK_PTR(skel, "uprobe_syscall_executed__open_and_load")) 255 + goto cleanup; 256 + 257 + pid = fork(); 258 + if (!ASSERT_GE(pid, 0, "fork")) 259 + goto cleanup; 260 + 261 + /* child */ 262 + if (pid == 0) { 263 + close(go[1]); 264 + 265 + /* wait for parent's kick */ 266 + err = read(go[0], &c, 1); 267 + if (err != 1) 268 + exit(-1); 269 + 270 + uretprobe_syscall_call(); 271 + _exit(0); 272 + } 273 + 274 + skel->links.test = bpf_program__attach_uprobe_multi(skel->progs.test, pid, 275 + "/proc/self/exe", 276 + "uretprobe_syscall_call", &opts); 277 + if (!ASSERT_OK_PTR(skel->links.test, "bpf_program__attach_uprobe_multi")) 278 + goto cleanup; 279 + 280 + /* kick the child */ 281 + write(go[1], &c, 1); 282 + err = waitpid(pid, &status, 0); 283 + ASSERT_EQ(err, pid, "waitpid"); 284 + 285 + /* verify the child got killed with SIGILL */ 286 + ASSERT_EQ(WIFSIGNALED(status), 1, "WIFSIGNALED"); 287 + ASSERT_EQ(WTERMSIG(status), SIGILL, "WTERMSIG"); 288 + 289 + /* verify the uretprobe program wasn't called */ 290 + ASSERT_EQ(skel->bss->executed, 0, "executed"); 291 + 292 + cleanup: 293 + uprobe_syscall_executed__destroy(skel); 294 + close(go[1]); 295 + close(go[0]); 296 + } 215 297 #else 216 298 static void test_uretprobe_regs_equal(void) 217 299 { ··· 304 216 } 305 217 306 218 static void test_uretprobe_regs_change(void) 219 + { 220 + test__skip(); 221 + } 222 + 223 + static void test_uretprobe_syscall_call(void) 307 224 { 308 225 test__skip(); 309 226 } ··· 320 227 test_uretprobe_regs_equal(); 321 228 if (test__start_subtest("uretprobe_regs_change")) 322 229 test_uretprobe_regs_change(); 230 + if (test__start_subtest("uretprobe_syscall_call")) 231 + test_uretprobe_syscall_call(); 323 232 }
+17
tools/testing/selftests/bpf/progs/uprobe_syscall_executed.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + #include "vmlinux.h" 3 + #include <bpf/bpf_helpers.h> 4 + #include <string.h> 5 + 6 + struct pt_regs regs; 7 + 8 + char _license[] SEC("license") = "GPL"; 9 + 10 + int executed = 0; 11 + 12 + SEC("uretprobe.multi") 13 + int test(struct pt_regs *regs) 14 + { 15 + executed = 1; 16 + return 0; 17 + }