Skip to content

Commit f42a58f

Browse files
olsajirimhiramat
authored andcommitted
selftests/bpf: Add uretprobe syscall test for regs changes
Adding test that creates uprobe consumer on uretprobe which changes some of the registers. Making sure the changed registers are propagated to the user space when the ureptobe syscall trampoline is used on x86_64. To be able to do this, adding support to bpf_testmod to create uprobe via new attribute file: /sys/kernel/bpf_testmod_uprobe This file is expecting file offset and creates related uprobe on current process exe file and removes existing uprobe if offset is 0. The can be only single uprobe at any time. The uprobe has specific consumer that changes registers used in ureprobe syscall trampoline and which are later checked in the test. Link: https://lore.kernel.org/all/[email protected]/ Acked-by: Andrii Nakryiko <[email protected]> Reviewed-by: Masami Hiramatsu (Google) <[email protected]> Signed-off-by: Jiri Olsa <[email protected]> Signed-off-by: Masami Hiramatsu (Google) <[email protected]>
1 parent 3e8e257 commit f42a58f

File tree

2 files changed

+189
-1
lines changed

2 files changed

+189
-1
lines changed

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/in6.h>
1919
#include <linux/un.h>
2020
#include <net/sock.h>
21+
#include <linux/namei.h>
2122
#include "bpf_testmod.h"
2223
#include "bpf_testmod_kfunc.h"
2324

@@ -358,6 +359,119 @@ static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = {
358359
.write = bpf_testmod_test_write,
359360
};
360361

362+
/* bpf_testmod_uprobe sysfs attribute is so far enabled for x86_64 only,
363+
* please see test_uretprobe_regs_change test
364+
*/
365+
#ifdef __x86_64__
366+
367+
static int
368+
uprobe_ret_handler(struct uprobe_consumer *self, unsigned long func,
369+
struct pt_regs *regs)
370+
371+
{
372+
regs->ax = 0x12345678deadbeef;
373+
regs->cx = 0x87654321feebdaed;
374+
regs->r11 = (u64) -1;
375+
return true;
376+
}
377+
378+
struct testmod_uprobe {
379+
struct path path;
380+
loff_t offset;
381+
struct uprobe_consumer consumer;
382+
};
383+
384+
static DEFINE_MUTEX(testmod_uprobe_mutex);
385+
386+
static struct testmod_uprobe uprobe = {
387+
.consumer.ret_handler = uprobe_ret_handler,
388+
};
389+
390+
static int testmod_register_uprobe(loff_t offset)
391+
{
392+
int err = -EBUSY;
393+
394+
if (uprobe.offset)
395+
return -EBUSY;
396+
397+
mutex_lock(&testmod_uprobe_mutex);
398+
399+
if (uprobe.offset)
400+
goto out;
401+
402+
err = kern_path("/proc/self/exe", LOOKUP_FOLLOW, &uprobe.path);
403+
if (err)
404+
goto out;
405+
406+
err = uprobe_register_refctr(d_real_inode(uprobe.path.dentry),
407+
offset, 0, &uprobe.consumer);
408+
if (err)
409+
path_put(&uprobe.path);
410+
else
411+
uprobe.offset = offset;
412+
413+
out:
414+
mutex_unlock(&testmod_uprobe_mutex);
415+
return err;
416+
}
417+
418+
static void testmod_unregister_uprobe(void)
419+
{
420+
mutex_lock(&testmod_uprobe_mutex);
421+
422+
if (uprobe.offset) {
423+
uprobe_unregister(d_real_inode(uprobe.path.dentry),
424+
uprobe.offset, &uprobe.consumer);
425+
uprobe.offset = 0;
426+
}
427+
428+
mutex_unlock(&testmod_uprobe_mutex);
429+
}
430+
431+
static ssize_t
432+
bpf_testmod_uprobe_write(struct file *file, struct kobject *kobj,
433+
struct bin_attribute *bin_attr,
434+
char *buf, loff_t off, size_t len)
435+
{
436+
unsigned long offset = 0;
437+
int err = 0;
438+
439+
if (kstrtoul(buf, 0, &offset))
440+
return -EINVAL;
441+
442+
if (offset)
443+
err = testmod_register_uprobe(offset);
444+
else
445+
testmod_unregister_uprobe();
446+
447+
return err ?: strlen(buf);
448+
}
449+
450+
static struct bin_attribute bin_attr_bpf_testmod_uprobe_file __ro_after_init = {
451+
.attr = { .name = "bpf_testmod_uprobe", .mode = 0666, },
452+
.write = bpf_testmod_uprobe_write,
453+
};
454+
455+
static int register_bpf_testmod_uprobe(void)
456+
{
457+
return sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_uprobe_file);
458+
}
459+
460+
static void unregister_bpf_testmod_uprobe(void)
461+
{
462+
testmod_unregister_uprobe();
463+
sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_uprobe_file);
464+
}
465+
466+
#else
467+
static int register_bpf_testmod_uprobe(void)
468+
{
469+
return 0;
470+
}
471+
472+
static void unregister_bpf_testmod_uprobe(void) { }
473+
#endif
474+
361475
BTF_KFUNCS_START(bpf_testmod_common_kfunc_ids)
362476
BTF_ID_FLAGS(func, bpf_iter_testmod_seq_new, KF_ITER_NEW)
363477
BTF_ID_FLAGS(func, bpf_iter_testmod_seq_next, KF_ITER_NEXT | KF_RET_NULL)
@@ -912,7 +1026,13 @@ static int bpf_testmod_init(void)
9121026
return -EINVAL;
9131027
sock = NULL;
9141028
mutex_init(&sock_lock);
915-
return sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
1029+
ret = sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
1030+
if (ret < 0)
1031+
return ret;
1032+
ret = register_bpf_testmod_uprobe();
1033+
if (ret < 0)
1034+
return ret;
1035+
return 0;
9161036
}
9171037

9181038
static void bpf_testmod_exit(void)
@@ -927,6 +1047,7 @@ static void bpf_testmod_exit(void)
9271047

9281048
bpf_kfunc_close_sock();
9291049
sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
1050+
unregister_bpf_testmod_uprobe();
9301051
}
9311052

9321053
module_init(bpf_testmod_init);

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,82 @@ static void test_uretprobe_regs_equal(void)
149149
cleanup:
150150
uprobe_syscall__destroy(skel);
151151
}
152+
153+
#define BPF_TESTMOD_UPROBE_TEST_FILE "/sys/kernel/bpf_testmod_uprobe"
154+
155+
static int write_bpf_testmod_uprobe(unsigned long offset)
156+
{
157+
size_t n, ret;
158+
char buf[30];
159+
int fd;
160+
161+
n = sprintf(buf, "%lu", offset);
162+
163+
fd = open(BPF_TESTMOD_UPROBE_TEST_FILE, O_WRONLY);
164+
if (fd < 0)
165+
return -errno;
166+
167+
ret = write(fd, buf, n);
168+
close(fd);
169+
return ret != n ? (int) ret : 0;
170+
}
171+
172+
static void test_uretprobe_regs_change(void)
173+
{
174+
struct pt_regs before = {}, after = {};
175+
unsigned long *pb = (unsigned long *) &before;
176+
unsigned long *pa = (unsigned long *) &after;
177+
unsigned long cnt = sizeof(before)/sizeof(*pb);
178+
unsigned int i, err, offset;
179+
180+
offset = get_uprobe_offset(uretprobe_regs_trigger);
181+
182+
err = write_bpf_testmod_uprobe(offset);
183+
if (!ASSERT_OK(err, "register_uprobe"))
184+
return;
185+
186+
uretprobe_regs(&before, &after);
187+
188+
err = write_bpf_testmod_uprobe(0);
189+
if (!ASSERT_OK(err, "unregister_uprobe"))
190+
return;
191+
192+
for (i = 0; i < cnt; i++) {
193+
unsigned int offset = i * sizeof(unsigned long);
194+
195+
switch (offset) {
196+
case offsetof(struct pt_regs, rax):
197+
ASSERT_EQ(pa[i], 0x12345678deadbeef, "rax");
198+
break;
199+
case offsetof(struct pt_regs, rcx):
200+
ASSERT_EQ(pa[i], 0x87654321feebdaed, "rcx");
201+
break;
202+
case offsetof(struct pt_regs, r11):
203+
ASSERT_EQ(pa[i], (__u64) -1, "r11");
204+
break;
205+
default:
206+
if (!ASSERT_EQ(pa[i], pb[i], "register before-after value check"))
207+
fprintf(stdout, "failed register offset %u\n", offset);
208+
}
209+
}
210+
}
211+
152212
#else
153213
static void test_uretprobe_regs_equal(void)
154214
{
155215
test__skip();
156216
}
217+
218+
static void test_uretprobe_regs_change(void)
219+
{
220+
test__skip();
221+
}
157222
#endif
158223

159224
void test_uprobe_syscall(void)
160225
{
161226
if (test__start_subtest("uretprobe_regs_equal"))
162227
test_uretprobe_regs_equal();
228+
if (test__start_subtest("uretprobe_regs_change"))
229+
test_uretprobe_regs_change();
163230
}

0 commit comments

Comments
 (0)