|
| 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 | + |
| 13 | +#define UMH_start _binary_net_bpfilter_bpfilter_umh_start |
| 14 | +#define UMH_end _binary_net_bpfilter_bpfilter_umh_end |
| 15 | + |
| 16 | +extern char UMH_start; |
| 17 | +extern char UMH_end; |
| 18 | + |
| 19 | +static struct umh_info info; |
| 20 | +/* since ip_getsockopt() can run in parallel, serialize access to umh */ |
| 21 | +static DEFINE_MUTEX(bpfilter_lock); |
| 22 | + |
| 23 | +static void shutdown_umh(struct umh_info *info) |
| 24 | +{ |
| 25 | + struct task_struct *tsk; |
| 26 | + |
| 27 | + tsk = pid_task(find_vpid(info->pid), PIDTYPE_PID); |
| 28 | + if (tsk) |
| 29 | + force_sig(SIGKILL, tsk); |
| 30 | + fput(info->pipe_to_umh); |
| 31 | + fput(info->pipe_from_umh); |
| 32 | +} |
| 33 | + |
| 34 | +static void __stop_umh(void) |
| 35 | +{ |
| 36 | + if (bpfilter_process_sockopt) { |
| 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; |
| 57 | + int ret; |
| 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); |
| 65 | + n = __kernel_write(info.pipe_to_umh, &req, sizeof(req), &pos); |
| 66 | + if (n != sizeof(req)) { |
| 67 | + pr_err("write fail %zd\n", n); |
| 68 | + __stop_umh(); |
| 69 | + ret = -EFAULT; |
| 70 | + goto out; |
| 71 | + } |
| 72 | + pos = 0; |
| 73 | + n = kernel_read(info.pipe_from_umh, &reply, sizeof(reply), &pos); |
| 74 | + if (n != sizeof(reply)) { |
| 75 | + pr_err("read fail %zd\n", n); |
| 76 | + __stop_umh(); |
| 77 | + ret = -EFAULT; |
| 78 | + goto out; |
| 79 | + } |
| 80 | + ret = reply.status; |
| 81 | +out: |
| 82 | + mutex_unlock(&bpfilter_lock); |
| 83 | + return ret; |
| 84 | +} |
| 85 | + |
| 86 | +static int __init load_umh(void) |
| 87 | +{ |
| 88 | + int err; |
| 89 | + |
| 90 | + /* fork usermode process */ |
| 91 | + err = fork_usermode_blob(&UMH_start, &UMH_end - &UMH_start, &info); |
| 92 | + if (err) |
| 93 | + return err; |
| 94 | + pr_info("Loaded bpfilter_umh pid %d\n", info.pid); |
| 95 | + |
| 96 | + /* health check that usermode process started correctly */ |
| 97 | + if (__bpfilter_process_sockopt(NULL, 0, 0, 0, 0) != 0) { |
| 98 | + stop_umh(); |
| 99 | + return -EFAULT; |
| 100 | + } |
| 101 | + bpfilter_process_sockopt = &__bpfilter_process_sockopt; |
| 102 | + return 0; |
| 103 | +} |
| 104 | + |
| 105 | +static void __exit fini_umh(void) |
| 106 | +{ |
| 107 | + stop_umh(); |
| 108 | +} |
| 109 | +module_init(load_umh); |
| 110 | +module_exit(fini_umh); |
| 111 | +MODULE_LICENSE("GPL"); |
0 commit comments