syscall.c 6.4 KB

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