Message ID | 20200326142803.239183-1-zenczykowski@gmail.com |
---|---|
State | Not Applicable |
Delegated to: | David Miller |
Headers | show |
Series | [v2] iptables: open eBPF programs in read only mode | expand |
On Thu, 26 Mar 2020 07:28:03 -0700 Maciej Żenczykowski wrote: > diff --git a/extensions/libxt_bpf.c b/extensions/libxt_bpf.c > index 92958247..44cdd5cb 100644 > --- a/extensions/libxt_bpf.c > +++ b/extensions/libxt_bpf.c > @@ -61,11 +61,22 @@ static const struct xt_option_entry bpf_opts_v1[] = { > XTOPT_TABLEEND, > }; > > -static int bpf_obj_get(const char *filepath) > +static int bpf_obj_get_readonly(const char *filepath) > { > #if defined HAVE_LINUX_BPF_H && defined __NR_bpf && defined BPF_FS_MAGIC > union bpf_attr attr; > + // file_flags && BPF_F_RDONLY requires Linux 4.15+ uapi kernel headers > +#ifdef BPF_F_RDONLY FWIW the BPF subsystem is about to break uAPI backward-compat and replace the defines with enums. See commit 1aae4bdd7879 ("bpf: Switch BPF UAPI #define constants used from BPF program side to enums"). > + int fd; > > + memset(&attr, 0, sizeof(attr)); > + attr.pathname = (__u64) filepath; > + attr.file_flags = BPF_F_RDONLY; > + fd = syscall(__NR_bpf, BPF_OBJ_GET, &attr, sizeof(attr)); > + if (fd >= 0) return fd; > + > + // on any error fallback to default R/W access for pre-4.15-rc1 kernels > +#endif
> FWIW the BPF subsystem is about to break uAPI backward-compat and > replace the defines with enums. See commit 1aae4bdd7879 ("bpf: Switch > BPF UAPI #define constants used from BPF program side to enums"). Shouldn't it do what is normally done in such a case? #define BPF_F_RDONLY BPF_F_RDONLY
On Thu, Mar 26, 2020 at 11:34 AM Maciej Żenczykowski <zenczykowski@gmail.com> wrote: > > > FWIW the BPF subsystem is about to break uAPI backward-compat and > > replace the defines with enums. See commit 1aae4bdd7879 ("bpf: Switch > > BPF UAPI #define constants used from BPF program side to enums"). > > Shouldn't it do what is normally done in such a case? > #define BPF_F_RDONLY BPF_F_RDONLY No. just update the headers.
diff --git a/extensions/libxt_bpf.c b/extensions/libxt_bpf.c index 92958247..44cdd5cb 100644 --- a/extensions/libxt_bpf.c +++ b/extensions/libxt_bpf.c @@ -61,11 +61,22 @@ static const struct xt_option_entry bpf_opts_v1[] = { XTOPT_TABLEEND, }; -static int bpf_obj_get(const char *filepath) +static int bpf_obj_get_readonly(const char *filepath) { #if defined HAVE_LINUX_BPF_H && defined __NR_bpf && defined BPF_FS_MAGIC union bpf_attr attr; + // file_flags && BPF_F_RDONLY requires Linux 4.15+ uapi kernel headers +#ifdef BPF_F_RDONLY + int fd; + memset(&attr, 0, sizeof(attr)); + attr.pathname = (__u64) filepath; + attr.file_flags = BPF_F_RDONLY; + fd = syscall(__NR_bpf, BPF_OBJ_GET, &attr, sizeof(attr)); + if (fd >= 0) return fd; + + // on any error fallback to default R/W access for pre-4.15-rc1 kernels +#endif memset(&attr, 0, sizeof(attr)); attr.pathname = (__u64) filepath; @@ -125,7 +136,7 @@ static void bpf_parse_string(struct sock_filter *pc, __u16 *lenp, __u16 len_max, static void bpf_parse_obj_pinned(struct xt_bpf_info_v1 *bi, const char *filepath) { - bi->fd = bpf_obj_get(filepath); + bi->fd = bpf_obj_get_readonly(filepath); if (bi->fd < 0) xtables_error(PARAMETER_PROBLEM, "bpf: failed to get bpf object");