On Wed, Apr 12, 2023 at 10:49 AM Kees Cook <keescook@xxxxxxxxxxxx> wrote: > > On Tue, Apr 11, 2023 at 09:32:53PM -0700, Andrii Nakryiko wrote: > > Make each bpf() syscall command a bit more self-contained, making it > > easier to further enhance it. We move sysctl_unprivileged_bpf_disabled > > handling down to map_create() and bpf_prog_load(), two special commands > > in this regard. > > > > Signed-off-by: Andrii Nakryiko <andrii@xxxxxxxxxx> > > --- > > kernel/bpf/syscall.c | 37 ++++++++++++++++++++++--------------- > > 1 file changed, 22 insertions(+), 15 deletions(-) > > > > diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c > > index 6d575505f89c..c1d268025985 100644 > > --- a/kernel/bpf/syscall.c > > +++ b/kernel/bpf/syscall.c > > @@ -1130,6 +1130,17 @@ static int map_create(union bpf_attr *attr) > > int f_flags; > > int err; > > > > + /* Intent here is for unprivileged_bpf_disabled to block key object > > + * creation commands for unprivileged users; other actions depend > > + * of fd availability and access to bpffs, so are dependent on > > + * object creation success. Capabilities are later verified for > > + * operations such as load and map create, so even with unprivileged > > + * BPF disabled, capability checks are still carried out for these > > + * and other operations. > > + */ > > + if (!bpf_capable() && sysctl_unprivileged_bpf_disabled) > > + return -EPERM; > > This appears to be a problem in the original code, but capability checks > should be last, so that audit doesn't see a capability as having been > used when it wasn't. i.e. if bpf_capable() passes, but > sysctl_unprivileged_bpf_disabled isn't true, it'll look like a > capability got used, and the flag gets set. Not a big deal at the end of > the day, but the preferred ordering should be: > > if (sysctl_unprivileged_bpf_disabled && !bpf_capable()) > ... > makes sense, I'll change the order > > + > > err = CHECK_ATTR(BPF_MAP_CREATE); > > if (err) > > return -EINVAL; > > @@ -2512,6 +2523,17 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size) > > char license[128]; > > bool is_gpl; > > > > + /* Intent here is for unprivileged_bpf_disabled to block key object > > + * creation commands for unprivileged users; other actions depend > > + * of fd availability and access to bpffs, so are dependent on > > + * object creation success. Capabilities are later verified for > > + * operations such as load and map create, so even with unprivileged > > + * BPF disabled, capability checks are still carried out for these > > + * and other operations. > > + */ > > + if (!bpf_capable() && sysctl_unprivileged_bpf_disabled) > > + return -EPERM; > > + > > if (CHECK_ATTR(BPF_PROG_LOAD)) > > return -EINVAL; > > > > @@ -5008,23 +5030,8 @@ static int bpf_prog_bind_map(union bpf_attr *attr) > > static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size) > > { > > union bpf_attr attr; > > - bool capable; > > int err; > > > > - capable = bpf_capable() || !sysctl_unprivileged_bpf_disabled; > > - > > - /* Intent here is for unprivileged_bpf_disabled to block key object > > - * creation commands for unprivileged users; other actions depend > > - * of fd availability and access to bpffs, so are dependent on > > - * object creation success. Capabilities are later verified for > > - * operations such as load and map create, so even with unprivileged > > - * BPF disabled, capability checks are still carried out for these > > - * and other operations. > > - */ > > - if (!capable && > > - (cmd == BPF_MAP_CREATE || cmd == BPF_PROG_LOAD)) > > - return -EPERM; > > - > > err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size); > > if (err) > > return err; > > -- > > 2.34.1 > > > > -- > Kees Cook