syscall-steal.c 7.1 KB

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