2021-07-22 06:35:24 +08:00
|
|
|
/*
|
2023-12-22 20:53:21 +08:00
|
|
|
* syscall-steal.c
|
2021-07-22 06:35:24 +08:00
|
|
|
*
|
2021-08-08 01:24:59 +08:00
|
|
|
* System call "stealing" sample.
|
2021-07-22 06:35:24 +08:00
|
|
|
*
|
2021-08-08 01:24:59 +08:00
|
|
|
* Disables page protection at a processor level by changing the 16th bit
|
|
|
|
* in the cr0 register (could be Intel specific).
|
2021-07-22 06:35:24 +08:00
|
|
|
*/
|
|
|
|
|
2021-07-22 06:58:13 +08:00
|
|
|
#include <linux/delay.h>
|
2021-07-22 06:35:24 +08:00
|
|
|
#include <linux/kernel.h>
|
2021-07-22 06:58:13 +08:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/moduleparam.h> /* which will have params */
|
2021-09-02 15:15:07 +08:00
|
|
|
#include <linux/unistd.h> /* The list of system calls */
|
2022-12-25 13:31:01 +08:00
|
|
|
#include <linux/cred.h> /* For current_uid() */
|
|
|
|
#include <linux/uidgid.h> /* For __kuid_val() */
|
2021-08-23 21:30:43 +08:00
|
|
|
#include <linux/version.h>
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2021-08-08 01:24:59 +08:00
|
|
|
/* For the current (process) structure, we need this to know who the
|
|
|
|
* current user is.
|
2021-07-22 06:35:24 +08:00
|
|
|
*/
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/uaccess.h>
|
|
|
|
|
2021-08-31 11:07:01 +08:00
|
|
|
/* The way we access "sys_call_table" varies as kernel internal changes.
|
2022-03-16 23:17:31 +08:00
|
|
|
* - Prior to v5.4 : manual symbol lookup
|
|
|
|
* - v5.5 to v5.6 : use kallsyms_lookup_name()
|
|
|
|
* - v5.7+ : Kprobes or specific kernel module parameter
|
2021-08-31 11:07:01 +08:00
|
|
|
*/
|
|
|
|
|
2021-08-23 21:30:43 +08:00
|
|
|
/* The in-kernel calls to the ksys_close() syscall were removed in Linux v5.11+.
|
|
|
|
*/
|
2021-08-31 11:07:01 +08:00
|
|
|
#if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 7, 0))
|
|
|
|
|
|
|
|
#if LINUX_VERSION_CODE <= KERNEL_VERSION(5, 4, 0)
|
|
|
|
#define HAVE_KSYS_CLOSE 1
|
|
|
|
#include <linux/syscalls.h> /* For ksys_close() */
|
2021-08-23 21:30:43 +08:00
|
|
|
#else
|
2021-08-31 11:07:01 +08:00
|
|
|
#include <linux/kallsyms.h> /* For kallsyms_lookup_name */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#if defined(CONFIG_KPROBES)
|
|
|
|
#define HAVE_KPROBES 1
|
2024-07-05 07:40:53 +08:00
|
|
|
#if defined(CONFIG_X86_64)
|
|
|
|
/* If you have tried to use the syscall table to intercept syscalls and it
|
|
|
|
* doesn't work, you can try to use Kprobes to intercept syscalls.
|
|
|
|
* Set USE_KPROBES_PRE_HANDLER_BEFORE_SYSCALL to 1 to register a pre-handler
|
|
|
|
* before the syscall.
|
|
|
|
*/
|
|
|
|
#define USE_KPROBES_PRE_HANDLER_BEFORE_SYSCALL 0
|
|
|
|
#endif
|
2021-08-31 11:07:01 +08:00
|
|
|
#include <linux/kprobes.h>
|
|
|
|
#else
|
|
|
|
#define HAVE_PARAM 1
|
|
|
|
#include <linux/kallsyms.h> /* For sprint_symbol */
|
|
|
|
/* The address of the sys_call_table, which can be obtained with looking up
|
|
|
|
* "/boot/System.map" or "/proc/kallsyms". When the kernel version is v5.7+,
|
|
|
|
* without CONFIG_KPROBES, you can input the parameter or the module will look
|
|
|
|
* up all the memory.
|
|
|
|
*/
|
|
|
|
static unsigned long sym = 0;
|
|
|
|
module_param(sym, ulong, 0644);
|
2021-09-02 15:15:07 +08:00
|
|
|
#endif /* CONFIG_KPROBES */
|
2021-08-31 11:07:01 +08:00
|
|
|
|
2021-09-02 15:15:07 +08:00
|
|
|
#endif /* Version < v5.7 */
|
2021-08-23 21:30:43 +08:00
|
|
|
|
2021-08-08 01:24:59 +08:00
|
|
|
/* UID we want to spy on - will be filled from the command line. */
|
2022-12-25 13:31:01 +08:00
|
|
|
static uid_t uid = -1;
|
2021-07-22 06:35:24 +08:00
|
|
|
module_param(uid, int, 0644);
|
|
|
|
|
2024-07-05 07:40:53 +08:00
|
|
|
#if USE_KPROBES_PRE_HANDLER_BEFORE_SYSCALL
|
|
|
|
|
|
|
|
/* syscall_sym is the symbol name of the syscall to spy on. The default is
|
|
|
|
* "__x64_sys_openat", which can be changed by the module parameter. You can
|
|
|
|
* look up the symbol name of a syscall in /proc/kallsyms.
|
|
|
|
*/
|
|
|
|
static char *syscall_sym = "__x64_sys_openat";
|
|
|
|
module_param(syscall_sym, charp, 0644);
|
|
|
|
|
|
|
|
static int sys_call_kprobe_pre_handler(struct kprobe *p, struct pt_regs *regs)
|
|
|
|
{
|
|
|
|
if (__kuid_val(current_uid()) != uid) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pr_info("%s called by %d\n", syscall_sym, uid);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct kprobe syscall_kprobe = {
|
|
|
|
.symbol_name = "__x64_sys_openat",
|
|
|
|
.pre_handler = sys_call_kprobe_pre_handler,
|
|
|
|
};
|
2024-10-05 12:01:25 +08:00
|
|
|
|
2024-07-05 07:40:53 +08:00
|
|
|
#else
|
|
|
|
|
|
|
|
static unsigned long **sys_call_table_stolen;
|
|
|
|
|
2021-08-08 01:24:59 +08:00
|
|
|
/* A pointer to the original system call. The reason we keep this, rather
|
2022-12-25 13:31:01 +08:00
|
|
|
* than call the original function (sys_openat), is because somebody else
|
2021-08-08 01:24:59 +08:00
|
|
|
* might have replaced the system call before us. Note that this is not
|
2022-12-25 13:31:01 +08:00
|
|
|
* 100% safe, because if another module replaced sys_openat before us,
|
2021-08-08 01:24:59 +08:00
|
|
|
* then when we are inserted, we will call the function in that module -
|
|
|
|
* and it might be removed before we are.
|
2021-07-22 06:35:24 +08:00
|
|
|
*
|
2022-12-25 13:31:01 +08:00
|
|
|
* Another reason for this is that we can not get sys_openat.
|
2021-08-08 01:24:59 +08:00
|
|
|
* It is a static variable, so it is not exported.
|
2021-07-22 06:35:24 +08:00
|
|
|
*/
|
2022-12-25 13:31:01 +08:00
|
|
|
#ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER
|
|
|
|
static asmlinkage long (*original_call)(const struct pt_regs *);
|
|
|
|
#else
|
|
|
|
static asmlinkage long (*original_call)(int, const char __user *, int, umode_t);
|
|
|
|
#endif
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2022-12-25 13:31:01 +08:00
|
|
|
/* The function we will replace sys_openat (the function called when you
|
2021-08-08 01:24:59 +08:00
|
|
|
* call the open system call) with. To find the exact prototype, with
|
|
|
|
* the number and type of arguments, we find the original function first
|
|
|
|
* (it is at fs/open.c).
|
2021-07-22 06:35:24 +08:00
|
|
|
*
|
2021-08-08 01:24:59 +08:00
|
|
|
* In theory, this means that we are tied to the current version of the
|
|
|
|
* kernel. In practice, the system calls almost never change (it would
|
|
|
|
* wreck havoc and require programs to be recompiled, since the system
|
|
|
|
* calls are the interface between the kernel and the processes).
|
2021-07-22 06:35:24 +08:00
|
|
|
*/
|
2022-12-25 13:31:01 +08:00
|
|
|
#ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER
|
|
|
|
static asmlinkage long our_sys_openat(const struct pt_regs *regs)
|
|
|
|
#else
|
|
|
|
static asmlinkage long our_sys_openat(int dfd, const char __user *filename,
|
|
|
|
int flags, umode_t mode)
|
|
|
|
#endif
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
char ch;
|
|
|
|
|
2022-12-25 13:31:01 +08:00
|
|
|
if (__kuid_val(current_uid()) != uid)
|
|
|
|
goto orig_call;
|
|
|
|
|
2021-08-08 01:24:59 +08:00
|
|
|
/* Report the file, if relevant */
|
2021-07-22 06:35:24 +08:00
|
|
|
pr_info("Opened file by %d: ", uid);
|
|
|
|
do {
|
2022-12-25 13:31:01 +08:00
|
|
|
#ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER
|
|
|
|
get_user(ch, (char __user *)regs->si + i);
|
|
|
|
#else
|
2021-09-04 17:53:29 +08:00
|
|
|
get_user(ch, (char __user *)filename + i);
|
2022-12-25 13:31:01 +08:00
|
|
|
#endif
|
2021-07-22 06:35:24 +08:00
|
|
|
i++;
|
|
|
|
pr_info("%c", ch);
|
|
|
|
} while (ch != 0);
|
|
|
|
pr_info("\n");
|
|
|
|
|
2022-12-25 13:31:01 +08:00
|
|
|
orig_call:
|
|
|
|
/* Call the original sys_openat - otherwise, we lose the ability to
|
2021-08-08 01:24:59 +08:00
|
|
|
* open files.
|
2021-07-22 06:35:24 +08:00
|
|
|
*/
|
2022-12-25 13:31:01 +08:00
|
|
|
#ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER
|
|
|
|
return original_call(regs);
|
|
|
|
#else
|
|
|
|
return original_call(dfd, filename, flags, mode);
|
|
|
|
#endif
|
2021-07-22 06:35:24 +08:00
|
|
|
}
|
|
|
|
|
2022-12-25 12:33:52 +08:00
|
|
|
static unsigned long **acquire_sys_call_table(void)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
2021-08-31 11:07:01 +08:00
|
|
|
#ifdef HAVE_KSYS_CLOSE
|
2021-07-22 06:35:24 +08:00
|
|
|
unsigned long int offset = PAGE_OFFSET;
|
|
|
|
unsigned long **sct;
|
|
|
|
|
|
|
|
while (offset < ULLONG_MAX) {
|
2021-09-02 15:15:07 +08:00
|
|
|
sct = (unsigned long **)offset;
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2021-09-02 15:15:07 +08:00
|
|
|
if (sct[__NR_close] == (unsigned long *)ksys_close)
|
2021-07-22 06:35:24 +08:00
|
|
|
return sct;
|
|
|
|
|
|
|
|
offset += sizeof(void *);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2021-08-31 11:07:01 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_PARAM
|
|
|
|
const char sct_name[15] = "sys_call_table";
|
2021-09-02 15:15:07 +08:00
|
|
|
char symbol[40] = { 0 };
|
2021-08-31 11:07:01 +08:00
|
|
|
|
|
|
|
if (sym == 0) {
|
2021-09-02 15:15:07 +08:00
|
|
|
pr_alert("For Linux v5.7+, Kprobes is the preferable way to get "
|
|
|
|
"symbol.\n");
|
|
|
|
pr_info("If Kprobes is absent, you have to specify the address of "
|
|
|
|
"sys_call_table symbol\n");
|
|
|
|
pr_info("by /boot/System.map or /proc/kallsyms, which contains all the "
|
|
|
|
"symbol addresses, into sym parameter.\n");
|
2021-08-31 11:07:01 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
sprint_symbol(symbol, sym);
|
|
|
|
if (!strncmp(sct_name, symbol, sizeof(sct_name) - 1))
|
2021-09-02 15:15:07 +08:00
|
|
|
return (unsigned long **)sym;
|
2021-08-31 11:07:01 +08:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_KPROBES
|
|
|
|
unsigned long (*kallsyms_lookup_name)(const char *name);
|
|
|
|
struct kprobe kp = {
|
|
|
|
.symbol_name = "kallsyms_lookup_name",
|
|
|
|
};
|
|
|
|
|
|
|
|
if (register_kprobe(&kp) < 0)
|
|
|
|
return NULL;
|
2021-09-02 15:15:07 +08:00
|
|
|
kallsyms_lookup_name = (unsigned long (*)(const char *name))kp.addr;
|
2021-08-31 11:07:01 +08:00
|
|
|
unregister_kprobe(&kp);
|
|
|
|
#endif
|
|
|
|
|
2021-09-02 15:15:07 +08:00
|
|
|
return (unsigned long **)kallsyms_lookup_name("sys_call_table");
|
2021-08-31 11:07:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)
|
|
|
|
static inline void __write_cr0(unsigned long cr0)
|
|
|
|
{
|
|
|
|
asm volatile("mov %0,%%cr0" : "+r"(cr0) : : "memory");
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define __write_cr0 write_cr0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void enable_write_protection(void)
|
|
|
|
{
|
|
|
|
unsigned long cr0 = read_cr0();
|
|
|
|
set_bit(16, &cr0);
|
|
|
|
__write_cr0(cr0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void disable_write_protection(void)
|
|
|
|
{
|
|
|
|
unsigned long cr0 = read_cr0();
|
|
|
|
clear_bit(16, &cr0);
|
|
|
|
__write_cr0(cr0);
|
2021-07-22 06:35:24 +08:00
|
|
|
}
|
2024-07-05 07:40:53 +08:00
|
|
|
#endif
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2023-12-21 22:12:24 +08:00
|
|
|
static int __init syscall_steal_start(void)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
2024-07-05 07:40:53 +08:00
|
|
|
#if USE_KPROBES_PRE_HANDLER_BEFORE_SYSCALL
|
|
|
|
int err;
|
|
|
|
/* use symbol name from the module parameter */
|
|
|
|
syscall_kprobe.symbol_name = syscall_sym;
|
|
|
|
err = register_kprobe(&syscall_kprobe);
|
|
|
|
if (err) {
|
|
|
|
pr_err("register_kprobe() on %s failed: %d\n", syscall_sym, err);
|
|
|
|
pr_err("Please check the symbol name from 'syscall_sym' parameter.\n");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
#else
|
2023-12-22 11:46:46 +08:00
|
|
|
if (!(sys_call_table_stolen = acquire_sys_call_table()))
|
2021-07-22 06:35:24 +08:00
|
|
|
return -1;
|
|
|
|
|
2021-08-31 11:07:01 +08:00
|
|
|
disable_write_protection();
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
/* keep track of the original open function */
|
2023-12-22 11:46:46 +08:00
|
|
|
original_call = (void *)sys_call_table_stolen[__NR_openat];
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2022-12-25 13:31:01 +08:00
|
|
|
/* use our openat function instead */
|
2023-12-22 11:46:46 +08:00
|
|
|
sys_call_table_stolen[__NR_openat] = (unsigned long *)our_sys_openat;
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2021-08-31 11:07:01 +08:00
|
|
|
enable_write_protection();
|
2024-07-05 07:40:53 +08:00
|
|
|
#endif
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2024-07-05 07:40:53 +08:00
|
|
|
pr_info("Spying on UID:%d\n", uid);
|
2021-07-22 06:35:24 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-12-21 22:12:24 +08:00
|
|
|
static void __exit syscall_steal_end(void)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
2024-07-05 07:40:53 +08:00
|
|
|
#if USE_KPROBES_PRE_HANDLER_BEFORE_SYSCALL
|
|
|
|
unregister_kprobe(&syscall_kprobe);
|
|
|
|
#else
|
2023-12-22 11:46:46 +08:00
|
|
|
if (!sys_call_table_stolen)
|
2021-07-22 06:35:24 +08:00
|
|
|
return;
|
|
|
|
|
2021-08-08 01:24:59 +08:00
|
|
|
/* Return the system call back to normal */
|
2023-12-22 11:46:46 +08:00
|
|
|
if (sys_call_table_stolen[__NR_openat] != (unsigned long *)our_sys_openat) {
|
2021-07-22 06:35:24 +08:00
|
|
|
pr_alert("Somebody else also played with the ");
|
|
|
|
pr_alert("open system call\n");
|
|
|
|
pr_alert("The system may be left in ");
|
|
|
|
pr_alert("an unstable state.\n");
|
|
|
|
}
|
|
|
|
|
2021-08-31 11:07:01 +08:00
|
|
|
disable_write_protection();
|
2023-12-22 11:46:46 +08:00
|
|
|
sys_call_table_stolen[__NR_openat] = (unsigned long *)original_call;
|
2021-08-31 11:07:01 +08:00
|
|
|
enable_write_protection();
|
2024-07-05 07:40:53 +08:00
|
|
|
#endif
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
msleep(2000);
|
|
|
|
}
|
|
|
|
|
2023-12-21 22:12:24 +08:00
|
|
|
module_init(syscall_steal_start);
|
|
|
|
module_exit(syscall_steal_end);
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|