I read this article and learn how to hook a linux kernel function by using ftrace.
int fh_install_hook(struct ftrace_hook *hook)
{
err = ftrace_set_filter_ip(&hook->ops, hook->address, 0, 0);
if (err) {
pr_debug("ftrace_set_filter_ip() failed: %d\n", err);
return err;
}
err = register_ftrace_function(&hook->ops);
if (err) {
pr_debug("register_ftrace_function() failed: %d\n", err);
/* Don’t forget to turn off ftrace in case of an error. */
ftrace_set_filter_ip(&hook->ops, hook->address, 1, 0);
return err;
}
return 0;
}
In one machine, It is possible to multi program use this aproach to hook same linux kernel function? I test hook sys_execve but I get register_ftrace_function return -16 if other program already hook sys_execve.
return code -16 typically corresponds to EBUSY, so try to handle race conditions and conflicts. Here is one possible solution