Skip to content

Commit 92a66a8

Browse files
sladkaniokta-10
authored andcommitted
UPSTREAM: netfilter: xt_bpf: Fix XT_BPF_MODE_FD_PINNED mode of 'xt_bpf_info_v1'
Commit 2c16d6033264 ("netfilter: xt_bpf: support ebpf") introduced support for attaching an eBPF object by an fd, with the 'bpf_mt_check_v1' ABI expecting the '.fd' to be specified upon each IPT_SO_SET_REPLACE call. However this breaks subsequent iptables calls: # iptables -A INPUT -m bpf --object-pinned /sys/fs/bpf/xxx -j ACCEPT # iptables -A INPUT -s 5.6.7.8 -j ACCEPT iptables: Invalid argument. Run `dmesg' for more information. That's because iptables works by loading existing rules using IPT_SO_GET_ENTRIES to userspace, then issuing IPT_SO_SET_REPLACE with the replacement set. However, the loaded 'xt_bpf_info_v1' has an arbitrary '.fd' number (from the initial "iptables -m bpf" invocation) - so when 2nd invocation occurs, userspace passes a bogus fd number, which leads to 'bpf_mt_check_v1' to fail. One suggested solution [1] was to hack iptables userspace, to perform a "entries fixup" immediatley after IPT_SO_GET_ENTRIES, by opening a new, process-local fd per every 'xt_bpf_info_v1' entry seen. However, in [2] both Pablo Neira Ayuso and Willem de Bruijn suggested to depricate the xt_bpf_info_v1 ABI dealing with pinned ebpf objects. This fix changes the XT_BPF_MODE_FD_PINNED behavior to ignore the given '.fd' and instead perform an in-kernel lookup for the bpf object given the provided '.path'. It also defines an alias for the XT_BPF_MODE_FD_PINNED mode, named XT_BPF_MODE_PATH_PINNED, to better reflect the fact that the user is expected to provide the path of the pinned object. Existing XT_BPF_MODE_FD_ELF behavior (non-pinned fd mode) is preserved. References: [1] https://marc.info/?l=netfilter-devel&m=150564724607440&w=2 [2] https://marc.info/?l=netfilter-devel&m=150575727129880&w=2 Reported-by: Rafael Buchbinder <rafi@rbk.ms> Signed-off-by: Shmulik Ladkani <shmulik.ladkani@gmail.com> Acked-by: Willem de Bruijn <willemb@google.com> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org> Signed-off-by: Chenbo Feng <fengc@google.com> (cherry picked from commit 98589a0998b8b13c4a8fa1ccb0e62751a019faa5) Change-Id: Ia0d15a76823cca3afb38786a3d2c25c13ccf941d Signed-off-by: Oktapra Amtono <oktapra.amtono@gmail.com>
1 parent 4577636 commit 92a66a8

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

include/linux/bpf.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,11 @@ static inline struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
320320
{
321321
return ERR_PTR(-EOPNOTSUPP);
322322
}
323+
static inline int bpf_obj_get_user(const char __user *pathname)
324+
{
325+
return -EOPNOTSUPP;
326+
}
327+
323328
#endif /* CONFIG_BPF_SYSCALL */
324329

325330
/* verifier prototypes for helper functions called from eBPF programs */

include/uapi/linux/netfilter/xt_bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ enum xt_bpf_modes {
2323
XT_BPF_MODE_FD_PINNED,
2424
XT_BPF_MODE_FD_ELF,
2525
};
26+
#define XT_BPF_MODE_PATH_PINNED XT_BPF_MODE_FD_PINNED
2627

2728
struct xt_bpf_info_v1 {
2829
__u16 mode;

kernel/bpf/inode.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ int bpf_obj_get_user(const char __user *pathname, int flags)
321321
putname(pname);
322322
return ret;
323323
}
324+
EXPORT_SYMBOL_GPL(bpf_obj_get_user);
324325

325326
static void bpf_evict_inode(struct inode *inode)
326327
{

net/netfilter/xt_bpf.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
#include <linux/module.h>
11+
#include <linux/syscalls.h>
1112
#include <linux/skbuff.h>
1213
#include <linux/filter.h>
1314
#include <linux/bpf.h>
@@ -52,6 +53,22 @@ static int __bpf_mt_check_fd(int fd, struct bpf_prog **ret)
5253
return 0;
5354
}
5455

56+
static int __bpf_mt_check_path(const char *path, struct bpf_prog **ret)
57+
{
58+
mm_segment_t oldfs = get_fs();
59+
int retval, fd;
60+
61+
set_fs(KERNEL_DS);
62+
fd = bpf_obj_get_user(path);
63+
set_fs(oldfs);
64+
if (fd < 0)
65+
return fd;
66+
67+
retval = __bpf_mt_check_fd(fd, ret);
68+
sys_close(fd);
69+
return retval;
70+
}
71+
5572
static int bpf_mt_check(const struct xt_mtchk_param *par)
5673
{
5774
struct xt_bpf_info *info = par->matchinfo;
@@ -69,9 +86,10 @@ static int bpf_mt_check_v1(const struct xt_mtchk_param *par)
6986
return __bpf_mt_check_bytecode(info->bpf_program,
7087
info->bpf_program_num_elem,
7188
&info->filter);
72-
else if (info->mode == XT_BPF_MODE_FD_PINNED ||
73-
info->mode == XT_BPF_MODE_FD_ELF)
89+
else if (info->mode == XT_BPF_MODE_FD_ELF)
7490
return __bpf_mt_check_fd(info->fd, &info->filter);
91+
else if (info->mode == XT_BPF_MODE_PATH_PINNED)
92+
return __bpf_mt_check_path(info->path, &info->filter);
7593
else
7694
return -EINVAL;
7795
}

0 commit comments

Comments
 (0)