syscall-steal.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * syscall-steal.c
  3. *
  4. * System call "stealing" sample.
  5. *
  6. * Disables page protection at a processor level by changing the 16th bit
  7. * in the cr0 register (could be Intel specific).
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h> /* which will have params */
  13. #include <linux/unistd.h> /* The list of system calls */
  14. #include <linux/cred.h> /* For current_uid() */
  15. #include <linux/uidgid.h> /* For __kuid_val() */
  16. #include <linux/version.h>
  17. /* For the current (process) structure, we need this to know who the
  18. * current user is.
  19. */
  20. #include <linux/sched.h>
  21. #include <linux/uaccess.h>
  22. /* The way we access "sys_call_table" varies as kernel internal changes.
  23. * - Prior to v5.4 : manual symbol lookup
  24. * - v5.5 to v5.6 : use kallsyms_lookup_name()
  25. * - v5.7+ : Kprobes or specific kernel module parameter
  26. */
  27. /* The in-kernel calls to the ksys_close() syscall were removed in Linux v5.11+.
  28. */
  29. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 7, 0))
  30. #if defined(CONFIG_KPROBES)
  31. #define HAVE_KPROBES 1
  32. #if defined(CONFIG_X86_64)
  33. /* If you have tried to use the syscall table to intercept syscalls and it
  34. * doesn't work, you can try to use Kprobes to intercept syscalls.
  35. * Set USE_KPROBES_PRE_HANDLER_BEFORE_SYSCALL to 1 to register a pre-handler
  36. * before the syscall.
  37. */
  38. #define USE_KPROBES_PRE_HANDLER_BEFORE_SYSCALL 0
  39. #endif
  40. #include <linux/kprobes.h>
  41. #else
  42. #define HAVE_PARAM 1
  43. #include <linux/kallsyms.h> /* For sprint_symbol */
  44. /* The address of the sys_call_table, which can be obtained with looking up
  45. * "/boot/System.map" or "/proc/kallsyms". When the kernel version is v5.7+,
  46. * without CONFIG_KPROBES, you can input the parameter or the module will look
  47. * up all the memory.
  48. */
  49. static unsigned long sym = 0;
  50. module_param(sym, ulong, 0644);
  51. #endif /* CONFIG_KPROBES */
  52. #else
  53. #if LINUX_VERSION_CODE <= KERNEL_VERSION(5, 4, 0)
  54. #define HAVE_KSYS_CLOSE 1
  55. #include <linux/syscalls.h> /* For ksys_close() */
  56. #else
  57. #include <linux/kallsyms.h> /* For kallsyms_lookup_name */
  58. #endif
  59. #endif /* Version >= v5.7 */
  60. /* UID we want to spy on - will be filled from the command line. */
  61. static uid_t uid = -1;
  62. module_param(uid, int, 0644);
  63. #if USE_KPROBES_PRE_HANDLER_BEFORE_SYSCALL
  64. /* syscall_sym is the symbol name of the syscall to spy on. The default is
  65. * "__x64_sys_openat", which can be changed by the module parameter. You can
  66. * look up the symbol name of a syscall in /proc/kallsyms.
  67. */
  68. static char *syscall_sym = "__x64_sys_openat";
  69. module_param(syscall_sym, charp, 0644);
  70. static int sys_call_kprobe_pre_handler(struct kprobe *p, struct pt_regs *regs)
  71. {
  72. if (__kuid_val(current_uid()) != uid) {
  73. return 0;
  74. }
  75. pr_info("%s called by %d\n", syscall_sym, uid);
  76. return 0;
  77. }
  78. static struct kprobe syscall_kprobe = {
  79. .symbol_name = "__x64_sys_openat",
  80. .pre_handler = sys_call_kprobe_pre_handler,
  81. };
  82. #else
  83. static unsigned long **sys_call_table_stolen;
  84. /* A pointer to the original system call. The reason we keep this, rather
  85. * than call the original function (sys_openat), is because somebody else
  86. * might have replaced the system call before us. Note that this is not
  87. * 100% safe, because if another module replaced sys_openat before us,
  88. * then when we are inserted, we will call the function in that module -
  89. * and it might be removed before we are.
  90. *
  91. * Another reason for this is that we can not get sys_openat.
  92. * It is a static variable, so it is not exported.
  93. */
  94. #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER
  95. static asmlinkage long (*original_call)(const struct pt_regs *);
  96. #else
  97. static asmlinkage long (*original_call)(int, const char __user *, int, umode_t);
  98. #endif
  99. /* The function we will replace sys_openat (the function called when you
  100. * call the open system call) with. To find the exact prototype, with
  101. * the number and type of arguments, we find the original function first
  102. * (it is at fs/open.c).
  103. *
  104. * In theory, this means that we are tied to the current version of the
  105. * kernel. In practice, the system calls almost never change (it would
  106. * wreck havoc and require programs to be recompiled, since the system
  107. * calls are the interface between the kernel and the processes).
  108. */
  109. #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER
  110. static asmlinkage long our_sys_openat(const struct pt_regs *regs)
  111. #else
  112. static asmlinkage long our_sys_openat(int dfd, const char __user *filename,
  113. int flags, umode_t mode)
  114. #endif
  115. {
  116. int i = 0;
  117. char ch;
  118. if (__kuid_val(current_uid()) != uid)
  119. goto orig_call;
  120. /* Report the file, if relevant */
  121. pr_info("Opened file by %d: ", uid);
  122. do {
  123. #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER
  124. get_user(ch, (char __user *)regs->si + i);
  125. #else
  126. get_user(ch, (char __user *)filename + i);
  127. #endif
  128. i++;
  129. pr_info("%c", ch);
  130. } while (ch != 0);
  131. pr_info("\n");
  132. orig_call:
  133. /* Call the original sys_openat - otherwise, we lose the ability to
  134. * open files.
  135. */
  136. #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER
  137. return original_call(regs);
  138. #else
  139. return original_call(dfd, filename, flags, mode);
  140. #endif
  141. }
  142. static unsigned long **acquire_sys_call_table(void)
  143. {
  144. #ifdef HAVE_KSYS_CLOSE
  145. unsigned long int offset = PAGE_OFFSET;
  146. unsigned long **sct;
  147. while (offset < ULLONG_MAX) {
  148. sct = (unsigned long **)offset;
  149. if (sct[__NR_close] == (unsigned long *)ksys_close)
  150. return sct;
  151. offset += sizeof(void *);
  152. }
  153. return NULL;
  154. #endif
  155. #ifdef HAVE_PARAM
  156. const char sct_name[15] = "sys_call_table";
  157. char symbol[40] = { 0 };
  158. if (sym == 0) {
  159. pr_alert("For Linux v5.7+, Kprobes is the preferable way to get "
  160. "symbol.\n");
  161. pr_info("If Kprobes is absent, you have to specify the address of "
  162. "sys_call_table symbol\n");
  163. pr_info("by /boot/System.map or /proc/kallsyms, which contains all the "
  164. "symbol addresses, into sym parameter.\n");
  165. return NULL;
  166. }
  167. sprint_symbol(symbol, sym);
  168. if (!strncmp(sct_name, symbol, sizeof(sct_name) - 1))
  169. return (unsigned long **)sym;
  170. return NULL;
  171. #endif
  172. #ifdef HAVE_KPROBES
  173. unsigned long (*kallsyms_lookup_name)(const char *name);
  174. struct kprobe kp = {
  175. .symbol_name = "kallsyms_lookup_name",
  176. };
  177. if (register_kprobe(&kp) < 0)
  178. return NULL;
  179. kallsyms_lookup_name = (unsigned long (*)(const char *name))kp.addr;
  180. unregister_kprobe(&kp);
  181. #endif
  182. return (unsigned long **)kallsyms_lookup_name("sys_call_table");
  183. }
  184. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)
  185. static inline void __write_cr0(unsigned long cr0)
  186. {
  187. asm volatile("mov %0,%%cr0" : "+r"(cr0) : : "memory");
  188. }
  189. #else
  190. #define __write_cr0 write_cr0
  191. #endif
  192. static void enable_write_protection(void)
  193. {
  194. unsigned long cr0 = read_cr0();
  195. set_bit(16, &cr0);
  196. __write_cr0(cr0);
  197. }
  198. static void disable_write_protection(void)
  199. {
  200. unsigned long cr0 = read_cr0();
  201. clear_bit(16, &cr0);
  202. __write_cr0(cr0);
  203. }
  204. #endif
  205. static int __init syscall_steal_start(void)
  206. {
  207. #if USE_KPROBES_PRE_HANDLER_BEFORE_SYSCALL
  208. int err;
  209. /* use symbol name from the module parameter */
  210. syscall_kprobe.symbol_name = syscall_sym;
  211. err = register_kprobe(&syscall_kprobe);
  212. if (err) {
  213. pr_err("register_kprobe() on %s failed: %d\n", syscall_sym, err);
  214. pr_err("Please check the symbol name from 'syscall_sym' parameter.\n");
  215. return err;
  216. }
  217. #else
  218. if (!(sys_call_table_stolen = acquire_sys_call_table()))
  219. return -1;
  220. disable_write_protection();
  221. /* keep track of the original open function */
  222. original_call = (void *)sys_call_table_stolen[__NR_openat];
  223. /* use our openat function instead */
  224. sys_call_table_stolen[__NR_openat] = (unsigned long *)our_sys_openat;
  225. enable_write_protection();
  226. #endif
  227. pr_info("Spying on UID:%d\n", uid);
  228. return 0;
  229. }
  230. static void __exit syscall_steal_end(void)
  231. {
  232. #if USE_KPROBES_PRE_HANDLER_BEFORE_SYSCALL
  233. unregister_kprobe(&syscall_kprobe);
  234. #else
  235. if (!sys_call_table_stolen)
  236. return;
  237. /* Return the system call back to normal */
  238. if (sys_call_table_stolen[__NR_openat] != (unsigned long *)our_sys_openat) {
  239. pr_alert("Somebody else also played with the ");
  240. pr_alert("open system call\n");
  241. pr_alert("The system may be left in ");
  242. pr_alert("an unstable state.\n");
  243. }
  244. disable_write_protection();
  245. sys_call_table_stolen[__NR_openat] = (unsigned long *)original_call;
  246. enable_write_protection();
  247. #endif
  248. msleep(2000);
  249. }
  250. module_init(syscall_steal_start);
  251. module_exit(syscall_steal_end);
  252. MODULE_LICENSE("GPL");