]>
Commit | Line | Data |
---|---|---|
d2ba09c1 AS |
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 | ||
8e75887d MY |
13 | extern char bpfilter_umh_start; |
14 | extern char bpfilter_umh_end; | |
d2ba09c1 AS |
15 | |
16 | static struct umh_info info; | |
17 | /* since ip_getsockopt() can run in parallel, serialize access to umh */ | |
18 | static DEFINE_MUTEX(bpfilter_lock); | |
19 | ||
20 | static void shutdown_umh(struct umh_info *info) | |
21 | { | |
22 | struct task_struct *tsk; | |
23 | ||
66e58e0e AS |
24 | if (!info->pid) |
25 | return; | |
d2ba09c1 AS |
26 | tsk = pid_task(find_vpid(info->pid), PIDTYPE_PID); |
27 | if (tsk) | |
28 | force_sig(SIGKILL, tsk); | |
29 | fput(info->pipe_to_umh); | |
30 | fput(info->pipe_from_umh); | |
66e58e0e | 31 | info->pid = 0; |
d2ba09c1 AS |
32 | } |
33 | ||
34 | static void __stop_umh(void) | |
35 | { | |
66e58e0e | 36 | if (IS_ENABLED(CONFIG_INET)) { |
d2ba09c1 AS |
37 | bpfilter_process_sockopt = NULL; |
38 | shutdown_umh(&info); | |
39 | } | |
40 | } | |
41 | ||
42 | static void stop_umh(void) | |
43 | { | |
44 | mutex_lock(&bpfilter_lock); | |
45 | __stop_umh(); | |
46 | mutex_unlock(&bpfilter_lock); | |
47 | } | |
48 | ||
49 | static int __bpfilter_process_sockopt(struct sock *sk, int optname, | |
50 | char __user *optval, | |
51 | unsigned int optlen, bool is_set) | |
52 | { | |
53 | struct mbox_request req; | |
54 | struct mbox_reply reply; | |
55 | loff_t pos; | |
56 | ssize_t n; | |
66e58e0e | 57 | int ret = -EFAULT; |
d2ba09c1 AS |
58 | |
59 | req.is_set = is_set; | |
60 | req.pid = current->pid; | |
61 | req.cmd = optname; | |
62 | req.addr = (long)optval; | |
63 | req.len = optlen; | |
64 | mutex_lock(&bpfilter_lock); | |
66e58e0e AS |
65 | if (!info.pid) |
66 | goto out; | |
d2ba09c1 AS |
67 | n = __kernel_write(info.pipe_to_umh, &req, sizeof(req), &pos); |
68 | if (n != sizeof(req)) { | |
69 | pr_err("write fail %zd\n", n); | |
70 | __stop_umh(); | |
71 | ret = -EFAULT; | |
72 | goto out; | |
73 | } | |
74 | pos = 0; | |
75 | n = kernel_read(info.pipe_from_umh, &reply, sizeof(reply), &pos); | |
76 | if (n != sizeof(reply)) { | |
77 | pr_err("read fail %zd\n", n); | |
78 | __stop_umh(); | |
79 | ret = -EFAULT; | |
80 | goto out; | |
81 | } | |
82 | ret = reply.status; | |
83 | out: | |
84 | mutex_unlock(&bpfilter_lock); | |
85 | return ret; | |
86 | } | |
87 | ||
88 | static int __init load_umh(void) | |
89 | { | |
90 | int err; | |
91 | ||
92 | /* fork usermode process */ | |
8e75887d MY |
93 | err = fork_usermode_blob(&bpfilter_umh_start, |
94 | &bpfilter_umh_end - &bpfilter_umh_start, | |
95 | &info); | |
d2ba09c1 AS |
96 | if (err) |
97 | return err; | |
98 | pr_info("Loaded bpfilter_umh pid %d\n", info.pid); | |
99 | ||
100 | /* health check that usermode process started correctly */ | |
101 | if (__bpfilter_process_sockopt(NULL, 0, 0, 0, 0) != 0) { | |
102 | stop_umh(); | |
103 | return -EFAULT; | |
104 | } | |
d71dbdaa AB |
105 | if (IS_ENABLED(CONFIG_INET)) |
106 | bpfilter_process_sockopt = &__bpfilter_process_sockopt; | |
107 | ||
d2ba09c1 AS |
108 | return 0; |
109 | } | |
110 | ||
111 | static void __exit fini_umh(void) | |
112 | { | |
113 | stop_umh(); | |
114 | } | |
115 | module_init(load_umh); | |
116 | module_exit(fini_umh); | |
117 | MODULE_LICENSE("GPL"); |