syscall-steal.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 LINUX_VERSION_CODE <= KERNEL_VERSION(5, 4, 0)
  31. #define HAVE_KSYS_CLOSE 1
  32. #include <linux/syscalls.h> /* For ksys_close() */
  33. #else
  34. #include <linux/kallsyms.h> /* For kallsyms_lookup_name */
  35. #endif
  36. #else
  37. #if defined(CONFIG_KPROBES)
  38. #define HAVE_KPROBES 1
  39. #include <linux/kprobes.h>
  40. #else
  41. #define HAVE_PARAM 1
  42. #include <linux/kallsyms.h> /* For sprint_symbol */
  43. /* The address of the sys_call_table, which can be obtained with looking up
  44. * "/boot/System.map" or "/proc/kallsyms". When the kernel version is v5.7+,
  45. * without CONFIG_KPROBES, you can input the parameter or the module will look
  46. * up all the memory.
  47. */
  48. static unsigned long sym = 0;
  49. module_param(sym, ulong, 0644);
  50. #endif /* CONFIG_KPROBES */
  51. #endif /* Version < v5.7 */
  52. static unsigned long **sys_call_table_stolen;
  53. /* UID we want to spy on - will be filled from the command line. */
  54. static uid_t uid = -1;
  55. module_param(uid, int, 0644);
  56. /* A pointer to the original system call. The reason we keep this, rather
  57. * than call the original function (sys_openat), is because somebody else
  58. * might have replaced the system call before us. Note that this is not
  59. * 100% safe, because if another module replaced sys_openat before us,
  60. * then when we are inserted, we will call the function in that module -
  61. * and it might be removed before we are.
  62. *
  63. * Another reason for this is that we can not get sys_openat.
  64. * It is a static variable, so it is not exported.
  65. */
  66. #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER
  67. static asmlinkage long (*original_call)(const struct pt_regs *);
  68. #else
  69. static asmlinkage long (*original_call)(int, const char __user *, int, umode_t);
  70. #endif
  71. /* The function we will replace sys_openat (the function called when you
  72. * call the open system call) with. To find the exact prototype, with
  73. * the number and type of arguments, we find the original function first
  74. * (it is at fs/open.c).
  75. *
  76. * In theory, this means that we are tied to the current version of the
  77. * kernel. In practice, the system calls almost never change (it would
  78. * wreck havoc and require programs to be recompiled, since the system
  79. * calls are the interface between the kernel and the processes).
  80. */
  81. #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER
  82. static asmlinkage long our_sys_openat(const struct pt_regs *regs)
  83. #else
  84. static asmlinkage long our_sys_openat(int dfd, const char __user *filename,
  85. int flags, umode_t mode)
  86. #endif
  87. {
  88. int i = 0;
  89. char ch;
  90. if (__kuid_val(current_uid()) != uid)
  91. goto orig_call;
  92. /* Report the file, if relevant */
  93. pr_info("Opened file by %d: ", uid);
  94. do {
  95. #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER
  96. get_user(ch, (char __user *)regs->si + i);
  97. #else
  98. get_user(ch, (char __user *)filename + i);
  99. #endif
  100. i++;
  101. pr_info("%c", ch);
  102. } while (ch != 0);
  103. pr_info("\n");
  104. orig_call:
  105. /* Call the original sys_openat - otherwise, we lose the ability to
  106. * open files.
  107. */
  108. #ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER
  109. return original_call(regs);
  110. #else
  111. return original_call(dfd, filename, flags, mode);
  112. #endif
  113. }
  114. static unsigned long **acquire_sys_call_table(void)
  115. {
  116. #ifdef HAVE_KSYS_CLOSE
  117. unsigned long int offset = PAGE_OFFSET;
  118. unsigned long **sct;
  119. while (offset < ULLONG_MAX) {
  120. sct = (unsigned long **)offset;
  121. if (sct[__NR_close] == (unsigned long *)ksys_close)
  122. return sct;
  123. offset += sizeof(void *);
  124. }
  125. return NULL;
  126. #endif
  127. #ifdef HAVE_PARAM
  128. const char sct_name[15] = "sys_call_table";
  129. char symbol[40] = { 0 };
  130. if (sym == 0) {
  131. pr_alert("For Linux v5.7+, Kprobes is the preferable way to get "
  132. "symbol.\n");
  133. pr_info("If Kprobes is absent, you have to specify the address of "
  134. "sys_call_table symbol\n");
  135. pr_info("by /boot/System.map or /proc/kallsyms, which contains all the "
  136. "symbol addresses, into sym parameter.\n");
  137. return NULL;
  138. }
  139. sprint_symbol(symbol, sym);
  140. if (!strncmp(sct_name, symbol, sizeof(sct_name) - 1))
  141. return (unsigned long **)sym;
  142. return NULL;
  143. #endif
  144. #ifdef HAVE_KPROBES
  145. unsigned long (*kallsyms_lookup_name)(const char *name);
  146. struct kprobe kp = {
  147. .symbol_name = "kallsyms_lookup_name",
  148. };
  149. if (register_kprobe(&kp) < 0)
  150. return NULL;
  151. kallsyms_lookup_name = (unsigned long (*)(const char *name))kp.addr;
  152. unregister_kprobe(&kp);
  153. #endif
  154. return (unsigned long **)kallsyms_lookup_name("sys_call_table");
  155. }
  156. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)
  157. static inline void __write_cr0(unsigned long cr0)
  158. {
  159. asm volatile("mov %0,%%cr0" : "+r"(cr0) : : "memory");
  160. }
  161. #else
  162. #define __write_cr0 write_cr0
  163. #endif
  164. static void enable_write_protection(void)
  165. {
  166. unsigned long cr0 = read_cr0();
  167. set_bit(16, &cr0);
  168. __write_cr0(cr0);
  169. }
  170. static void disable_write_protection(void)
  171. {
  172. unsigned long cr0 = read_cr0();
  173. clear_bit(16, &cr0);
  174. __write_cr0(cr0);
  175. }
  176. static int __init syscall_steal_start(void)
  177. {
  178. if (!(sys_call_table_stolen = acquire_sys_call_table()))
  179. return -1;
  180. disable_write_protection();
  181. /* keep track of the original open function */
  182. original_call = (void *)sys_call_table_stolen[__NR_openat];
  183. /* use our openat function instead */
  184. sys_call_table_stolen[__NR_openat] = (unsigned long *)our_sys_openat;
  185. enable_write_protection();
  186. pr_info("Spying on UID:%d\n", uid);
  187. return 0;
  188. }
  189. static void __exit syscall_steal_end(void)
  190. {
  191. if (!sys_call_table_stolen)
  192. return;
  193. /* Return the system call back to normal */
  194. if (sys_call_table_stolen[__NR_openat] != (unsigned long *)our_sys_openat) {
  195. pr_alert("Somebody else also played with the ");
  196. pr_alert("open system call\n");
  197. pr_alert("The system may be left in ");
  198. pr_alert("an unstable state.\n");
  199. }
  200. disable_write_protection();
  201. sys_call_table_stolen[__NR_openat] = (unsigned long *)original_call;
  202. enable_write_protection();
  203. msleep(2000);
  204. }
  205. module_init(syscall_steal_start);
  206. module_exit(syscall_steal_end);
  207. MODULE_LICENSE("GPL");