|
| 1 | +From 5b4cb650e569db2e6a09d2fa0ef8eb789a0ac5d8 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Taehee Yoo < [email protected]> |
| 3 | +Date: Wed, 9 Jan 2019 02:24:34 +0900 |
| 4 | +Subject: [PATCH] net: bpfilter: use cleanup callback to release umh_info |
| 5 | + |
| 6 | +Now, UMH process is killed, do_exit() calls the umh_info->cleanup callback |
| 7 | +to release members of the umh_info. |
| 8 | +This patch makes bpfilter_umh's cleanup routine to use the |
| 9 | +umh_info->cleanup callback. |
| 10 | + |
| 11 | +Signed-off-by: Taehee Yoo < [email protected]> |
| 12 | +Signed-off-by: David S. Miller < [email protected]> |
| 13 | +--- |
| 14 | + include/linux/bpfilter.h | 11 ++++++++--- |
| 15 | + net/bpfilter/bpfilter_kern.c | 23 ++++++++++------------- |
| 16 | + net/ipv4/bpfilter/sockopt.c | 33 ++++++++++++++++++++++++++------- |
| 17 | + 3 files changed, 44 insertions(+), 23 deletions(-) |
| 18 | + |
| 19 | +diff --git a/include/linux/bpfilter.h b/include/linux/bpfilter.h |
| 20 | +index f02cee0225d4..70ffeed280e9 100644 |
| 21 | +--- a/include/linux/bpfilter.h |
| 22 | ++++ b/include/linux/bpfilter.h |
| 23 | +@@ -3,13 +3,18 @@ |
| 24 | + #define _LINUX_BPFILTER_H |
| 25 | + |
| 26 | + #include <uapi/linux/bpfilter.h> |
| 27 | ++#include <linux/umh.h> |
| 28 | + |
| 29 | + struct sock; |
| 30 | + int bpfilter_ip_set_sockopt(struct sock *sk, int optname, char __user *optval, |
| 31 | + unsigned int optlen); |
| 32 | + int bpfilter_ip_get_sockopt(struct sock *sk, int optname, char __user *optval, |
| 33 | + int __user *optlen); |
| 34 | +-extern int (*bpfilter_process_sockopt)(struct sock *sk, int optname, |
| 35 | +- char __user *optval, |
| 36 | +- unsigned int optlen, bool is_set); |
| 37 | ++struct bpfilter_umh_ops { |
| 38 | ++ struct umh_info info; |
| 39 | ++ int (*sockopt)(struct sock *sk, int optname, |
| 40 | ++ char __user *optval, |
| 41 | ++ unsigned int optlen, bool is_set); |
| 42 | ++}; |
| 43 | ++extern struct bpfilter_umh_ops bpfilter_ops; |
| 44 | + #endif |
| 45 | +diff --git a/net/bpfilter/bpfilter_kern.c b/net/bpfilter/bpfilter_kern.c |
| 46 | +index 7acfc83087d5..a68940b74c01 100644 |
| 47 | +--- a/net/bpfilter/bpfilter_kern.c |
| 48 | ++++ b/net/bpfilter/bpfilter_kern.c |
| 49 | +@@ -13,7 +13,6 @@ |
| 50 | + extern char bpfilter_umh_start; |
| 51 | + extern char bpfilter_umh_end; |
| 52 | + |
| 53 | +-static struct umh_info info; |
| 54 | + /* since ip_getsockopt() can run in parallel, serialize access to umh */ |
| 55 | + static DEFINE_MUTEX(bpfilter_lock); |
| 56 | + |
| 57 | +@@ -28,16 +27,13 @@ static void shutdown_umh(struct umh_info *info) |
| 58 | + send_sig(SIGKILL, tsk, 1); |
| 59 | + put_task_struct(tsk); |
| 60 | + } |
| 61 | +- fput(info->pipe_to_umh); |
| 62 | +- fput(info->pipe_from_umh); |
| 63 | +- info->pid = 0; |
| 64 | + } |
| 65 | + |
| 66 | + static void __stop_umh(void) |
| 67 | + { |
| 68 | + if (IS_ENABLED(CONFIG_INET)) { |
| 69 | +- bpfilter_process_sockopt = NULL; |
| 70 | +- shutdown_umh(&info); |
| 71 | ++ bpfilter_ops.sockopt = NULL; |
| 72 | ++ shutdown_umh(&bpfilter_ops.info); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | +@@ -64,9 +60,10 @@ static int __bpfilter_process_sockopt(struct sock *sk, int optname, |
| 77 | + req.addr = (long __force __user)optval; |
| 78 | + req.len = optlen; |
| 79 | + mutex_lock(&bpfilter_lock); |
| 80 | +- if (!info.pid) |
| 81 | ++ if (!bpfilter_ops.info.pid) |
| 82 | + goto out; |
| 83 | +- n = __kernel_write(info.pipe_to_umh, &req, sizeof(req), &pos); |
| 84 | ++ n = __kernel_write(bpfilter_ops.info.pipe_to_umh, &req, sizeof(req), |
| 85 | ++ &pos); |
| 86 | + if (n != sizeof(req)) { |
| 87 | + pr_err("write fail %zd\n", n); |
| 88 | + __stop_umh(); |
| 89 | +@@ -74,7 +71,8 @@ static int __bpfilter_process_sockopt(struct sock *sk, int optname, |
| 90 | + goto out; |
| 91 | + } |
| 92 | + pos = 0; |
| 93 | +- n = kernel_read(info.pipe_from_umh, &reply, sizeof(reply), &pos); |
| 94 | ++ n = kernel_read(bpfilter_ops.info.pipe_from_umh, &reply, sizeof(reply), |
| 95 | ++ &pos); |
| 96 | + if (n != sizeof(reply)) { |
| 97 | + pr_err("read fail %zd\n", n); |
| 98 | + __stop_umh(); |
| 99 | +@@ -94,10 +94,10 @@ static int __init load_umh(void) |
| 100 | + /* fork usermode process */ |
| 101 | + err = fork_usermode_blob(&bpfilter_umh_start, |
| 102 | + &bpfilter_umh_end - &bpfilter_umh_start, |
| 103 | +- &info); |
| 104 | ++ &bpfilter_ops.info); |
| 105 | + if (err) |
| 106 | + return err; |
| 107 | +- pr_info("Loaded bpfilter_umh pid %d\n", info.pid); |
| 108 | ++ pr_info("Loaded bpfilter_umh pid %d\n", bpfilter_ops.info.pid); |
| 109 | + |
| 110 | + /* health check that usermode process started correctly */ |
| 111 | + if (__bpfilter_process_sockopt(NULL, 0, NULL, 0, 0) != 0) { |
| 112 | +@@ -106,7 +103,7 @@ static int __init load_umh(void) |
| 113 | + return -EFAULT; |
| 114 | + } |
| 115 | + if (IS_ENABLED(CONFIG_INET)) |
| 116 | +- bpfilter_process_sockopt = &__bpfilter_process_sockopt; |
| 117 | ++ bpfilter_ops.sockopt = &__bpfilter_process_sockopt; |
| 118 | + |
| 119 | + return 0; |
| 120 | + } |
| 121 | +diff --git a/net/ipv4/bpfilter/sockopt.c b/net/ipv4/bpfilter/sockopt.c |
| 122 | +index 5e04ed25bc0e..c326cfbc0f62 100644 |
| 123 | +--- a/net/ipv4/bpfilter/sockopt.c |
| 124 | ++++ b/net/ipv4/bpfilter/sockopt.c |
| 125 | +@@ -1,28 +1,37 @@ |
| 126 | + // SPDX-License-Identifier: GPL-2.0 |
| 127 | ++#include <linux/init.h> |
| 128 | ++#include <linux/module.h> |
| 129 | + #include <linux/uaccess.h> |
| 130 | + #include <linux/bpfilter.h> |
| 131 | + #include <uapi/linux/bpf.h> |
| 132 | + #include <linux/wait.h> |
| 133 | + #include <linux/kmod.h> |
| 134 | ++#include <linux/fs.h> |
| 135 | ++#include <linux/file.h> |
| 136 | + |
| 137 | +-int (*bpfilter_process_sockopt)(struct sock *sk, int optname, |
| 138 | +- char __user *optval, |
| 139 | +- unsigned int optlen, bool is_set); |
| 140 | +-EXPORT_SYMBOL_GPL(bpfilter_process_sockopt); |
| 141 | ++struct bpfilter_umh_ops bpfilter_ops; |
| 142 | ++EXPORT_SYMBOL_GPL(bpfilter_ops); |
| 143 | ++ |
| 144 | ++static void bpfilter_umh_cleanup(struct umh_info *info) |
| 145 | ++{ |
| 146 | ++ fput(info->pipe_to_umh); |
| 147 | ++ fput(info->pipe_from_umh); |
| 148 | ++ info->pid = 0; |
| 149 | ++} |
| 150 | + |
| 151 | + static int bpfilter_mbox_request(struct sock *sk, int optname, |
| 152 | + char __user *optval, |
| 153 | + unsigned int optlen, bool is_set) |
| 154 | + { |
| 155 | +- if (!bpfilter_process_sockopt) { |
| 156 | ++ if (!bpfilter_ops.sockopt) { |
| 157 | + int err = request_module("bpfilter"); |
| 158 | + |
| 159 | + if (err) |
| 160 | + return err; |
| 161 | +- if (!bpfilter_process_sockopt) |
| 162 | ++ if (!bpfilter_ops.sockopt) |
| 163 | + return -ECHILD; |
| 164 | + } |
| 165 | +- return bpfilter_process_sockopt(sk, optname, optval, optlen, is_set); |
| 166 | ++ return bpfilter_ops.sockopt(sk, optname, optval, optlen, is_set); |
| 167 | + } |
| 168 | + |
| 169 | + int bpfilter_ip_set_sockopt(struct sock *sk, int optname, char __user *optval, |
| 170 | +@@ -41,3 +50,13 @@ int bpfilter_ip_get_sockopt(struct sock *sk, int optname, char __user *optval, |
| 171 | + |
| 172 | + return bpfilter_mbox_request(sk, optname, optval, len, false); |
| 173 | + } |
| 174 | ++ |
| 175 | ++static int __init bpfilter_sockopt_init(void) |
| 176 | ++{ |
| 177 | ++ bpfilter_ops.info.cmdline = "bpfilter_umh"; |
| 178 | ++ bpfilter_ops.info.cleanup = &bpfilter_umh_cleanup; |
| 179 | ++ |
| 180 | ++ return 0; |
| 181 | ++} |
| 182 | ++ |
| 183 | ++module_init(bpfilter_sockopt_init); |
| 184 | +-- |
| 185 | +2.41.0 |
0 commit comments