syscall.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. * - ver <= 5.4 : manual symbol lookup
  25. * - 5.4 < ver < 5.7 : kallsyms_lookup_name
  26. * - 5.7 <= ver : 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
  52. #endif
  53. 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. asmlinkage int (*original_call)(const char *, int, int);
  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. asmlinkage int our_sys_open(const char *filename, int flags, int mode)
  79. {
  80. int i = 0;
  81. char ch;
  82. /* Report the file, if relevant */
  83. pr_info("Opened file by %d: ", uid);
  84. do {
  85. get_user(ch, filename + i);
  86. i++;
  87. pr_info("%c", ch);
  88. } while (ch != 0);
  89. pr_info("\n");
  90. /* Call the original sys_open - otherwise, we lose the ability to
  91. * open files.
  92. */
  93. return original_call(filename, flags, mode);
  94. }
  95. static unsigned long **aquire_sys_call_table(void)
  96. {
  97. #ifdef HAVE_KSYS_CLOSE
  98. unsigned long int offset = PAGE_OFFSET;
  99. unsigned long **sct;
  100. while (offset < ULLONG_MAX) {
  101. sct = (unsigned long **) offset;
  102. if (sct[__NR_close] == (unsigned long *) ksys_close)
  103. return sct;
  104. offset += sizeof(void *);
  105. }
  106. return NULL;
  107. #endif
  108. #ifdef HAVE_PARAM
  109. const char sct_name[15] = "sys_call_table";
  110. char symbol[40] = {0};
  111. if (sym == 0) {
  112. pr_alert(
  113. "For Linux v5.7+, Kprobes is the preferable way to get "
  114. "symbol.\n");
  115. pr_info(
  116. "If Kprobes is absent, you have to specify the address of "
  117. "sys_call_table symbol\n");
  118. pr_info(
  119. "by /boot/System.map or /proc/kallsyms, which contains all the "
  120. "symbol addresses, into sym parameter.\n");
  121. return NULL;
  122. }
  123. sprint_symbol(symbol, sym);
  124. if (!strncmp(sct_name, symbol, sizeof(sct_name) - 1))
  125. return (unsigned long **) sym;
  126. return NULL;
  127. #endif
  128. #ifdef HAVE_KPROBES
  129. unsigned long (*kallsyms_lookup_name)(const char *name);
  130. struct kprobe kp = {
  131. .symbol_name = "kallsyms_lookup_name",
  132. };
  133. if (register_kprobe(&kp) < 0)
  134. return NULL;
  135. kallsyms_lookup_name = (unsigned long (*)(const char *name)) kp.addr;
  136. unregister_kprobe(&kp);
  137. #endif
  138. return (unsigned long **) kallsyms_lookup_name("sys_call_table");
  139. }
  140. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)
  141. static inline void __write_cr0(unsigned long cr0)
  142. {
  143. asm volatile("mov %0,%%cr0" : "+r"(cr0) : : "memory");
  144. }
  145. #else
  146. #define __write_cr0 write_cr0
  147. #endif
  148. static void enable_write_protection(void)
  149. {
  150. unsigned long cr0 = read_cr0();
  151. set_bit(16, &cr0);
  152. __write_cr0(cr0);
  153. }
  154. static void disable_write_protection(void)
  155. {
  156. unsigned long cr0 = read_cr0();
  157. clear_bit(16, &cr0);
  158. __write_cr0(cr0);
  159. }
  160. static int __init syscall_start(void)
  161. {
  162. if (!(sys_call_table = aquire_sys_call_table()))
  163. return -1;
  164. disable_write_protection();
  165. /* keep track of the original open function */
  166. original_call = (void *) sys_call_table[__NR_open];
  167. /* use our open function instead */
  168. sys_call_table[__NR_open] = (unsigned long *) our_sys_open;
  169. enable_write_protection();
  170. pr_info("Spying on UID:%d\n", uid);
  171. return 0;
  172. }
  173. static void __exit syscall_end(void)
  174. {
  175. if (!sys_call_table)
  176. return;
  177. /* Return the system call back to normal */
  178. if (sys_call_table[__NR_open] != (unsigned long *) our_sys_open) {
  179. pr_alert("Somebody else also played with the ");
  180. pr_alert("open system call\n");
  181. pr_alert("The system may be left in ");
  182. pr_alert("an unstable state.\n");
  183. }
  184. disable_write_protection();
  185. sys_call_table[__NR_open] = (unsigned long *) original_call;
  186. enable_write_protection();
  187. msleep(2000);
  188. }
  189. module_init(syscall_start);
  190. module_exit(syscall_end);
  191. MODULE_LICENSE("GPL");