2018-05-21 20:22:30 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/umh.h>
|
|
|
|
#include <linux/bpfilter.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/sched/signal.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/file.h>
|
|
|
|
#include "msgfmt.h"
|
|
|
|
|
2018-06-26 21:13:48 -06:00
|
|
|
extern char bpfilter_umh_start;
|
|
|
|
extern char bpfilter_umh_end;
|
2018-05-21 20:22:30 -06:00
|
|
|
|
2019-01-08 10:24:53 -07:00
|
|
|
static void shutdown_umh(void)
|
2018-05-21 20:22:30 -06:00
|
|
|
{
|
|
|
|
struct task_struct *tsk;
|
|
|
|
|
2019-01-08 10:24:53 -07:00
|
|
|
if (bpfilter_ops.stop)
|
2018-06-07 16:31:14 -06:00
|
|
|
return;
|
2019-01-08 10:24:53 -07:00
|
|
|
|
|
|
|
tsk = get_pid_task(find_vpid(bpfilter_ops.info.pid), PIDTYPE_PID);
|
2018-10-16 09:35:10 -06:00
|
|
|
if (tsk) {
|
2019-05-15 11:23:03 -06:00
|
|
|
send_sig(SIGKILL, tsk, 1);
|
2018-10-16 09:35:10 -06:00
|
|
|
put_task_struct(tsk);
|
|
|
|
}
|
2018-05-21 20:22:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __stop_umh(void)
|
|
|
|
{
|
2019-01-08 10:24:53 -07:00
|
|
|
if (IS_ENABLED(CONFIG_INET))
|
|
|
|
shutdown_umh();
|
2018-05-21 20:22:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static int __bpfilter_process_sockopt(struct sock *sk, int optname,
|
|
|
|
char __user *optval,
|
|
|
|
unsigned int optlen, bool is_set)
|
|
|
|
{
|
|
|
|
struct mbox_request req;
|
|
|
|
struct mbox_reply reply;
|
|
|
|
loff_t pos;
|
|
|
|
ssize_t n;
|
2018-06-07 16:31:14 -06:00
|
|
|
int ret = -EFAULT;
|
2018-05-21 20:22:30 -06:00
|
|
|
|
|
|
|
req.is_set = is_set;
|
|
|
|
req.pid = current->pid;
|
|
|
|
req.cmd = optname;
|
2018-10-05 09:27:48 -06:00
|
|
|
req.addr = (long __force __user)optval;
|
2018-05-21 20:22:30 -06:00
|
|
|
req.len = optlen;
|
2019-01-08 10:24:34 -07:00
|
|
|
if (!bpfilter_ops.info.pid)
|
2018-06-07 16:31:14 -06:00
|
|
|
goto out;
|
2019-01-08 10:24:34 -07:00
|
|
|
n = __kernel_write(bpfilter_ops.info.pipe_to_umh, &req, sizeof(req),
|
|
|
|
&pos);
|
2018-05-21 20:22:30 -06:00
|
|
|
if (n != sizeof(req)) {
|
|
|
|
pr_err("write fail %zd\n", n);
|
|
|
|
__stop_umh();
|
|
|
|
ret = -EFAULT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
pos = 0;
|
2019-01-08 10:24:34 -07:00
|
|
|
n = kernel_read(bpfilter_ops.info.pipe_from_umh, &reply, sizeof(reply),
|
|
|
|
&pos);
|
2018-05-21 20:22:30 -06:00
|
|
|
if (n != sizeof(reply)) {
|
|
|
|
pr_err("read fail %zd\n", n);
|
|
|
|
__stop_umh();
|
|
|
|
ret = -EFAULT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
ret = reply.status;
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-01-08 10:24:53 -07:00
|
|
|
static int start_umh(void)
|
2018-05-21 20:22:30 -06:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/* fork usermode process */
|
2018-06-26 21:13:48 -06:00
|
|
|
err = fork_usermode_blob(&bpfilter_umh_start,
|
|
|
|
&bpfilter_umh_end - &bpfilter_umh_start,
|
2019-01-08 10:24:34 -07:00
|
|
|
&bpfilter_ops.info);
|
2018-05-21 20:22:30 -06:00
|
|
|
if (err)
|
|
|
|
return err;
|
2019-01-08 10:24:53 -07:00
|
|
|
bpfilter_ops.stop = false;
|
2019-01-08 10:24:34 -07:00
|
|
|
pr_info("Loaded bpfilter_umh pid %d\n", bpfilter_ops.info.pid);
|
2018-05-21 20:22:30 -06:00
|
|
|
|
|
|
|
/* health check that usermode process started correctly */
|
2018-10-05 09:27:48 -06:00
|
|
|
if (__bpfilter_process_sockopt(NULL, 0, NULL, 0, 0) != 0) {
|
2019-01-08 10:25:10 -07:00
|
|
|
shutdown_umh();
|
2018-05-21 20:22:30 -06:00
|
|
|
return -EFAULT;
|
|
|
|
}
|
2018-05-29 03:55:06 -06:00
|
|
|
|
2018-05-21 20:22:30 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-08 10:24:53 -07:00
|
|
|
static int __init load_umh(void)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2019-01-08 10:25:10 -07:00
|
|
|
mutex_lock(&bpfilter_ops.lock);
|
|
|
|
if (!bpfilter_ops.stop) {
|
|
|
|
err = -EFAULT;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-01-08 10:24:53 -07:00
|
|
|
err = start_umh();
|
|
|
|
if (!err && IS_ENABLED(CONFIG_INET)) {
|
|
|
|
bpfilter_ops.sockopt = &__bpfilter_process_sockopt;
|
|
|
|
bpfilter_ops.start = &start_umh;
|
|
|
|
}
|
2019-01-08 10:25:10 -07:00
|
|
|
out:
|
|
|
|
mutex_unlock(&bpfilter_ops.lock);
|
2019-01-08 10:24:53 -07:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2018-05-21 20:22:30 -06:00
|
|
|
static void __exit fini_umh(void)
|
|
|
|
{
|
2019-01-08 10:25:10 -07:00
|
|
|
mutex_lock(&bpfilter_ops.lock);
|
2019-01-08 10:24:53 -07:00
|
|
|
if (IS_ENABLED(CONFIG_INET)) {
|
2019-01-08 10:25:10 -07:00
|
|
|
shutdown_umh();
|
2019-01-08 10:24:53 -07:00
|
|
|
bpfilter_ops.start = NULL;
|
|
|
|
bpfilter_ops.sockopt = NULL;
|
|
|
|
}
|
2019-01-08 10:25:10 -07:00
|
|
|
mutex_unlock(&bpfilter_ops.lock);
|
2018-05-21 20:22:30 -06:00
|
|
|
}
|
|
|
|
module_init(load_umh);
|
|
|
|
module_exit(fini_umh);
|
|
|
|
MODULE_LICENSE("GPL");
|