cregit-Linux how code gets into the kernel

Release 4.18 net/bpfilter/bpfilter_kern.c

Directory: net/bpfilter
// 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"

extern char bpfilter_umh_start;
extern char bpfilter_umh_end;


static struct umh_info info;
/* since ip_getsockopt() can run in parallel, serialize access to umh */
static DEFINE_MUTEX(bpfilter_lock);


static void shutdown_umh(struct umh_info *info) { struct task_struct *tsk; if (!info->pid) return; tsk = pid_task(find_vpid(info->pid), PIDTYPE_PID); if (tsk) force_sig(SIGKILL, tsk); fput(info->pipe_to_umh); fput(info->pipe_from_umh); info->pid = 0; }

Contributors

PersonTokensPropCommitsCommitProp
Alexei Starovoitov69100.00%2100.00%
Total69100.00%2100.00%


static void __stop_umh(void) { if (IS_ENABLED(CONFIG_INET)) { bpfilter_process_sockopt = NULL; shutdown_umh(&info); } }

Contributors

PersonTokensPropCommitsCommitProp
Alexei Starovoitov2385.19%150.00%
Arnd Bergmann414.81%150.00%
Total27100.00%2100.00%


static void stop_umh(void) { mutex_lock(&bpfilter_lock); __stop_umh(); mutex_unlock(&bpfilter_lock); }

Contributors

PersonTokensPropCommitsCommitProp
Alexei Starovoitov23100.00%1100.00%
Total23100.00%1100.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; int ret = -EFAULT; req.is_set = is_set; req.pid = current->pid; req.cmd = optname; req.addr = (long)optval; req.len = optlen; mutex_lock(&bpfilter_lock); if (!info.pid) goto out; n = __kernel_write(info.pipe_to_umh, &req, sizeof(req), &pos); if (n != sizeof(req)) { pr_err("write fail %zd\n", n); __stop_umh(); ret = -EFAULT; goto out; } pos = 0; n = kernel_read(info.pipe_from_umh, &reply, sizeof(reply), &pos); if (n != sizeof(reply)) { pr_err("read fail %zd\n", n); __stop_umh(); ret = -EFAULT; goto out; } ret = reply.status; out: mutex_unlock(&bpfilter_lock); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Alexei Starovoitov216100.00%2100.00%
Total216100.00%2100.00%


static int __init load_umh(void) { int err; /* fork usermode process */ err = fork_usermode_blob(&bpfilter_umh_start, &bpfilter_umh_end - &bpfilter_umh_start, &info); if (err) return err; pr_info("Loaded bpfilter_umh pid %d\n", info.pid); /* health check that usermode process started correctly */ if (__bpfilter_process_sockopt(NULL, 0, 0, 0, 0) != 0) { stop_umh(); return -EFAULT; } if (IS_ENABLED(CONFIG_INET)) bpfilter_process_sockopt = &__bpfilter_process_sockopt; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Alexei Starovoitov7888.64%133.33%
Arnd Bergmann77.95%133.33%
Masahiro Yamada33.41%133.33%
Total88100.00%3100.00%


static void __exit fini_umh(void) { stop_umh(); }

Contributors

PersonTokensPropCommitsCommitProp
Alexei Starovoitov12100.00%1100.00%
Total12100.00%1100.00%

module_init(load_umh); module_exit(fini_umh); MODULE_LICENSE("GPL");

Overall Contributors

PersonTokensPropCommitsCommitProp
Alexei Starovoitov48996.83%250.00%
Arnd Bergmann112.18%125.00%
Masahiro Yamada50.99%125.00%
Total505100.00%4100.00%
Directory: net/bpfilter
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
Created with cregit.