blob: c0f0990f30b60415fa6b929415f5ac67bba4ed01 [file] [log] [blame]
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -07001// 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
Masahiro Yamada8e758872018-06-26 20:13:48 -070013extern char bpfilter_umh_start;
14extern char bpfilter_umh_end;
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -070015
Taehee Yoo61fbf592019-01-09 02:24:53 +090016static void shutdown_umh(void)
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -070017{
18 struct task_struct *tsk;
19
Taehee Yoo61fbf592019-01-09 02:24:53 +090020 if (bpfilter_ops.stop)
Alexei Starovoitov66e58e0ef2018-06-07 15:31:14 -070021 return;
Taehee Yoo61fbf592019-01-09 02:24:53 +090022
23 tsk = get_pid_task(find_vpid(bpfilter_ops.info.pid), PIDTYPE_PID);
Taehee Yoo84258432018-10-17 00:35:10 +090024 if (tsk) {
Eric W. Biederman1dfd1712019-05-15 12:23:03 -050025 send_sig(SIGKILL, tsk, 1);
Taehee Yoo84258432018-10-17 00:35:10 +090026 put_task_struct(tsk);
27 }
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -070028}
29
30static void __stop_umh(void)
31{
Taehee Yoo61fbf592019-01-09 02:24:53 +090032 if (IS_ENABLED(CONFIG_INET))
33 shutdown_umh();
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -070034}
35
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -070036static int __bpfilter_process_sockopt(struct sock *sk, int optname,
37 char __user *optval,
38 unsigned int optlen, bool is_set)
39{
40 struct mbox_request req;
41 struct mbox_reply reply;
42 loff_t pos;
43 ssize_t n;
Alexei Starovoitov66e58e0ef2018-06-07 15:31:14 -070044 int ret = -EFAULT;
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -070045
46 req.is_set = is_set;
47 req.pid = current->pid;
48 req.cmd = optname;
Shanthosh RK33aa8da2018-10-05 20:57:48 +053049 req.addr = (long __force __user)optval;
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -070050 req.len = optlen;
Taehee Yoo5b4cb652019-01-09 02:24:34 +090051 if (!bpfilter_ops.info.pid)
Alexei Starovoitov66e58e0ef2018-06-07 15:31:14 -070052 goto out;
Taehee Yoo5b4cb652019-01-09 02:24:34 +090053 n = __kernel_write(bpfilter_ops.info.pipe_to_umh, &req, sizeof(req),
54 &pos);
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -070055 if (n != sizeof(req)) {
56 pr_err("write fail %zd\n", n);
57 __stop_umh();
58 ret = -EFAULT;
59 goto out;
60 }
61 pos = 0;
Taehee Yoo5b4cb652019-01-09 02:24:34 +090062 n = kernel_read(bpfilter_ops.info.pipe_from_umh, &reply, sizeof(reply),
63 &pos);
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -070064 if (n != sizeof(reply)) {
65 pr_err("read fail %zd\n", n);
66 __stop_umh();
67 ret = -EFAULT;
68 goto out;
69 }
70 ret = reply.status;
71out:
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -070072 return ret;
73}
74
Taehee Yoo61fbf592019-01-09 02:24:53 +090075static int start_umh(void)
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -070076{
77 int err;
78
79 /* fork usermode process */
Masahiro Yamada8e758872018-06-26 20:13:48 -070080 err = fork_usermode_blob(&bpfilter_umh_start,
81 &bpfilter_umh_end - &bpfilter_umh_start,
Taehee Yoo5b4cb652019-01-09 02:24:34 +090082 &bpfilter_ops.info);
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -070083 if (err)
84 return err;
Taehee Yoo61fbf592019-01-09 02:24:53 +090085 bpfilter_ops.stop = false;
Taehee Yoo5b4cb652019-01-09 02:24:34 +090086 pr_info("Loaded bpfilter_umh pid %d\n", bpfilter_ops.info.pid);
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -070087
88 /* health check that usermode process started correctly */
Shanthosh RK33aa8da2018-10-05 20:57:48 +053089 if (__bpfilter_process_sockopt(NULL, 0, NULL, 0, 0) != 0) {
Taehee Yoo71a85082019-01-09 02:25:10 +090090 shutdown_umh();
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -070091 return -EFAULT;
92 }
Arnd Bergmannd71dbda2018-05-29 11:55:06 +020093
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -070094 return 0;
95}
96
Taehee Yoo61fbf592019-01-09 02:24:53 +090097static int __init load_umh(void)
98{
99 int err;
100
Taehee Yoo71a85082019-01-09 02:25:10 +0900101 mutex_lock(&bpfilter_ops.lock);
102 if (!bpfilter_ops.stop) {
103 err = -EFAULT;
104 goto out;
105 }
Taehee Yoo61fbf592019-01-09 02:24:53 +0900106 err = start_umh();
107 if (!err && IS_ENABLED(CONFIG_INET)) {
108 bpfilter_ops.sockopt = &__bpfilter_process_sockopt;
109 bpfilter_ops.start = &start_umh;
110 }
Taehee Yoo71a85082019-01-09 02:25:10 +0900111out:
112 mutex_unlock(&bpfilter_ops.lock);
Taehee Yoo61fbf592019-01-09 02:24:53 +0900113 return err;
114}
115
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -0700116static void __exit fini_umh(void)
117{
Taehee Yoo71a85082019-01-09 02:25:10 +0900118 mutex_lock(&bpfilter_ops.lock);
Taehee Yoo61fbf592019-01-09 02:24:53 +0900119 if (IS_ENABLED(CONFIG_INET)) {
Taehee Yoo71a85082019-01-09 02:25:10 +0900120 shutdown_umh();
Taehee Yoo61fbf592019-01-09 02:24:53 +0900121 bpfilter_ops.start = NULL;
122 bpfilter_ops.sockopt = NULL;
123 }
Taehee Yoo71a85082019-01-09 02:25:10 +0900124 mutex_unlock(&bpfilter_ops.lock);
Alexei Starovoitovd2ba09c2018-05-21 19:22:30 -0700125}
126module_init(load_umh);
127module_exit(fini_umh);
128MODULE_LICENSE("GPL");