syscall.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 /* 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 *, 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. static 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, (char __user *)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("For Linux v5.7+, Kprobes is the preferable way to get "
  113. "symbol.\n");
  114. pr_info("If Kprobes is absent, you have to specify the address of "
  115. "sys_call_table symbol\n");
  116. pr_info("by /boot/System.map or /proc/kallsyms, which contains all the "
  117. "symbol addresses, into sym parameter.\n");
  118. return NULL;
  119. }
  120. sprint_symbol(symbol, sym);
  121. if (!strncmp(sct_name, symbol, sizeof(sct_name) - 1))
  122. return (unsigned long **)sym;
  123. return NULL;
  124. #endif
  125. #ifdef HAVE_KPROBES
  126. unsigned long (*kallsyms_lookup_name)(const char *name);
  127. struct kprobe kp = {
  128. .symbol_name = "kallsyms_lookup_name",
  129. };
  130. if (register_kprobe(&kp) < 0)
  131. return NULL;
  132. kallsyms_lookup_name = (unsigned long (*)(const char *name))kp.addr;
  133. unregister_kprobe(&kp);
  134. #endif
  135. return (unsigned long **)kallsyms_lookup_name("sys_call_table");
  136. }
  137. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)
  138. static inline void __write_cr0(unsigned long cr0)
  139. {
  140. asm volatile("mov %0,%%cr0" : "+r"(cr0) : : "memory");
  141. }
  142. #else
  143. #define __write_cr0 write_cr0
  144. #endif
  145. static void enable_write_protection(void)
  146. {
  147. unsigned long cr0 = read_cr0();
  148. set_bit(16, &cr0);
  149. __write_cr0(cr0);
  150. }
  151. static void disable_write_protection(void)
  152. {
  153. unsigned long cr0 = read_cr0();
  154. clear_bit(16, &cr0);
  155. __write_cr0(cr0);
  156. }
  157. static int __init syscall_start(void)
  158. {
  159. if (!(sys_call_table = aquire_sys_call_table()))
  160. return -1;
  161. disable_write_protection();
  162. /* keep track of the original open function */
  163. original_call = (void *)sys_call_table[__NR_open];
  164. /* use our open function instead */
  165. sys_call_table[__NR_open] = (unsigned long *)our_sys_open;
  166. enable_write_protection();
  167. pr_info("Spying on UID:%d\n", uid);
  168. return 0;
  169. }
  170. static void __exit syscall_end(void)
  171. {
  172. if (!sys_call_table)
  173. return;
  174. /* Return the system call back to normal */
  175. if (sys_call_table[__NR_open] != (unsigned long *)our_sys_open) {
  176. pr_alert("Somebody else also played with the ");
  177. pr_alert("open system call\n");
  178. pr_alert("The system may be left in ");
  179. pr_alert("an unstable state.\n");
  180. }
  181. disable_write_protection();
  182. sys_call_table[__NR_open] = (unsigned long *)original_call;
  183. enable_write_protection();
  184. msleep(2000);
  185. }
  186. module_init(syscall_start);
  187. module_exit(syscall_end);
  188. MODULE_LICENSE("GPL");