bpfilter_kern.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  3. #include <linux/init.h>
  4. #include <linux/module.h>
  5. #include <linux/umh.h>
  6. #include <linux/bpfilter.h>
  7. #include <linux/sched.h>
  8. #include <linux/sched/signal.h>
  9. #include <linux/fs.h>
  10. #include <linux/file.h>
  11. #include "msgfmt.h"
  12. extern char bpfilter_umh_start;
  13. extern char bpfilter_umh_end;
  14. static struct umh_info info;
  15. /* since ip_getsockopt() can run in parallel, serialize access to umh */
  16. static DEFINE_MUTEX(bpfilter_lock);
  17. static void shutdown_umh(struct umh_info *info)
  18. {
  19. struct task_struct *tsk;
  20. if (!info->pid)
  21. return;
  22. tsk = get_pid_task(find_vpid(info->pid), PIDTYPE_PID);
  23. if (tsk) {
  24. send_sig(SIGKILL, tsk, 1);
  25. put_task_struct(tsk);
  26. }
  27. fput(info->pipe_to_umh);
  28. fput(info->pipe_from_umh);
  29. info->pid = 0;
  30. }
  31. static void __stop_umh(void)
  32. {
  33. if (IS_ENABLED(CONFIG_INET)) {
  34. bpfilter_process_sockopt = NULL;
  35. shutdown_umh(&info);
  36. }
  37. }
  38. static void stop_umh(void)
  39. {
  40. mutex_lock(&bpfilter_lock);
  41. __stop_umh();
  42. mutex_unlock(&bpfilter_lock);
  43. }
  44. static int __bpfilter_process_sockopt(struct sock *sk, int optname,
  45. char __user *optval,
  46. unsigned int optlen, bool is_set)
  47. {
  48. struct mbox_request req;
  49. struct mbox_reply reply;
  50. loff_t pos;
  51. ssize_t n;
  52. int ret = -EFAULT;
  53. req.is_set = is_set;
  54. req.pid = current->pid;
  55. req.cmd = optname;
  56. req.addr = (long __force __user)optval;
  57. req.len = optlen;
  58. mutex_lock(&bpfilter_lock);
  59. if (!info.pid)
  60. goto out;
  61. n = __kernel_write(info.pipe_to_umh, &req, sizeof(req), &pos);
  62. if (n != sizeof(req)) {
  63. pr_err("write fail %zd\n", n);
  64. __stop_umh();
  65. ret = -EFAULT;
  66. goto out;
  67. }
  68. pos = 0;
  69. n = kernel_read(info.pipe_from_umh, &reply, sizeof(reply), &pos);
  70. if (n != sizeof(reply)) {
  71. pr_err("read fail %zd\n", n);
  72. __stop_umh();
  73. ret = -EFAULT;
  74. goto out;
  75. }
  76. ret = reply.status;
  77. out:
  78. mutex_unlock(&bpfilter_lock);
  79. return ret;
  80. }
  81. static int __init load_umh(void)
  82. {
  83. int err;
  84. /* fork usermode process */
  85. err = fork_usermode_blob(&bpfilter_umh_start,
  86. &bpfilter_umh_end - &bpfilter_umh_start,
  87. &info);
  88. if (err)
  89. return err;
  90. pr_info("Loaded bpfilter_umh pid %d\n", info.pid);
  91. /* health check that usermode process started correctly */
  92. if (__bpfilter_process_sockopt(NULL, 0, NULL, 0, 0) != 0) {
  93. stop_umh();
  94. return -EFAULT;
  95. }
  96. if (IS_ENABLED(CONFIG_INET))
  97. bpfilter_process_sockopt = &__bpfilter_process_sockopt;
  98. return 0;
  99. }
  100. static void __exit fini_umh(void)
  101. {
  102. stop_umh();
  103. }
  104. module_init(load_umh);
  105. module_exit(fini_umh);
  106. MODULE_LICENSE("GPL");