I am trying to steal the statx syscall on Linux 6.8 and to modify the returning arguments.
However, when attemp to load the module, I got a BUG about NULL pointer deference at line
r = strncpy_from_user(path, (char __user *)user_regs->si, 128);
Here is my code:
I use this question (Cannot read syscall arguments from a kprobe handler). But I did manage to deal with the argument.
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kprobes.h>
MODULE_VERSION("v.0");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Flavien <flav> ASTRAUD <[email protected]>");
MODULE_DESCRIPTION("ex. pour interception d'un syscall");
static int m_statx(struct kretprobe_instance *ri, struct pt_regs *regs)
{
long r = 0;
char path[128];
struct pt_regs *user_regs = (struct pt_regs *)regs->di;
struct statx statxbuf; // (struct statx *) regs->r8;
r = strncpy_from_user(path, (char __user *)user_regs->si, 128);
r = copy_from_user(&statxbuf, (struct statx *)user_regs->r8,
sizeof (struct statx));
pr_info("STATX_V3 %s size=%lld\n", path, statxbuf.stx_size);
statxbuf.stx_size = 42;
r = copy_to_user((struct statx *)user_regs->r8,
&statxbuf, sizeof(statxbuf));
return 0;
}
static struct kretprobe kret = {
// .symbol_name = "__x64_sys_statx",
.handler = m_statx,
};
static int override_statx(void)
{
kret.kp.symbol_name = "__x64_sys_statx";
if (register_kretprobe(&kret) < 0)
return 0;
return 0;
}
static void pullback_syscall(void)
{
unregister_kretprobe(&kret);
return;
}
static int __init kstatx_init(void)
{
pr_info("kstatx INIT\n=============\n");
override_statx();
return 0;
}
static void __exit kstatx_exit(void)
{
pr_info("kstatx END\n=============\n");
pullback_syscall();
}
module_init(kstatx_init);
module_exit(kstatx_exit);
statxsystem call, so post-handler is executed after given instruction, not after the entire function. For execute your code after the function, use kretprobe and its.handlerfield.regsargument contains registers upon function's returning, not the ones upon function's entering. But those arguments could be extracted in the pre-handler, and passed viakretprobe_instanceargument. See that my comment: stackoverflow.com/questions/78619294/…