[PATCH bpf-next v3 1/3] bpf: add destructive kfunc flag

Artem Savkov posted 3 patches 3 years, 8 months ago
There is a newer version of this series
[PATCH bpf-next v3 1/3] bpf: add destructive kfunc flag
Posted by Artem Savkov 3 years, 8 months ago
Add KF_DESTRUCTIVE flag for destructive functions. Functions with this
flag set will require CAP_SYS_BOOT capabilities.

Signed-off-by: Artem Savkov <asavkov@redhat.com>
---
 include/linux/btf.h   | 1 +
 kernel/bpf/verifier.c | 5 +++++
 2 files changed, 6 insertions(+)

diff --git a/include/linux/btf.h b/include/linux/btf.h
index cdb376d53238..51a0961c84e3 100644
--- a/include/linux/btf.h
+++ b/include/linux/btf.h
@@ -49,6 +49,7 @@
  * for this case.
  */
 #define KF_TRUSTED_ARGS (1 << 4) /* kfunc only takes trusted pointer arguments */
+#define KF_DESTRUCTIVE  (1 << 5) /* kfunc performs destructive actions */
 
 struct btf;
 struct btf_member;
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 096fdac70165..e52ca1631d3f 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -7584,6 +7584,11 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 			func_name);
 		return -EACCES;
 	}
+	if (*kfunc_flags & KF_DESTRUCTIVE && !capable(CAP_SYS_BOOT)) {
+		verbose(env, "destructive kfunc calls require CAP_SYS_BOOT capabilities\n");
+		return -EACCES;
+	}
+
 	acq = *kfunc_flags & KF_ACQUIRE;
 
 	/* Check the arguments */
-- 
2.37.1
Re: [PATCH bpf-next v3 1/3] bpf: add destructive kfunc flag
Posted by Kumar Kartikeya Dwivedi 3 years, 8 months ago
On Mon, 8 Aug 2022 at 11:48, Artem Savkov <asavkov@redhat.com> wrote:
>
> Add KF_DESTRUCTIVE flag for destructive functions. Functions with this
> flag set will require CAP_SYS_BOOT capabilities.
>
> Signed-off-by: Artem Savkov <asavkov@redhat.com>
> ---
>  include/linux/btf.h   | 1 +
>  kernel/bpf/verifier.c | 5 +++++
>  2 files changed, 6 insertions(+)
>
> diff --git a/include/linux/btf.h b/include/linux/btf.h
> index cdb376d53238..51a0961c84e3 100644
> --- a/include/linux/btf.h
> +++ b/include/linux/btf.h
> @@ -49,6 +49,7 @@
>   * for this case.
>   */
>  #define KF_TRUSTED_ARGS (1 << 4) /* kfunc only takes trusted pointer arguments */
> +#define KF_DESTRUCTIVE  (1 << 5) /* kfunc performs destructive actions */
>

Please also document this flag in Documentation/bpf/kfuncs.rst.

And maybe instead of KF_DESTRUCTIVE, it might be more apt to call this
KF_CAP_SYS_BOOT. While it is true you had a destructive flag for
programs being loaded earlier, so there was a mapping between the two
UAPI and kfunc flags, what it has boiled down to is that this flag
just requires CAP_SYS_BOOT (in addition to other capabilities) during
load. So that name might express the intent a bit better. We might
soon have similar flags encoding requirements of other capabilities on
load.

The flag rename is just a suggestion, up to you.

>  struct btf;
>  struct btf_member;
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 096fdac70165..e52ca1631d3f 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -7584,6 +7584,11 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
>                         func_name);
>                 return -EACCES;
>         }
> +       if (*kfunc_flags & KF_DESTRUCTIVE && !capable(CAP_SYS_BOOT)) {
> +               verbose(env, "destructive kfunc calls require CAP_SYS_BOOT capabilities\n");
> +               return -EACCES;
> +       }
> +
>         acq = *kfunc_flags & KF_ACQUIRE;
>
>         /* Check the arguments */
> --
> 2.37.1
>
Re: [PATCH bpf-next v3 1/3] bpf: add destructive kfunc flag
Posted by Artem Savkov 3 years, 8 months ago
On Mon, Aug 08, 2022 at 02:14:33PM +0200, Kumar Kartikeya Dwivedi wrote:
> On Mon, 8 Aug 2022 at 11:48, Artem Savkov <asavkov@redhat.com> wrote:
> >
> > Add KF_DESTRUCTIVE flag for destructive functions. Functions with this
> > flag set will require CAP_SYS_BOOT capabilities.
> >
> > Signed-off-by: Artem Savkov <asavkov@redhat.com>
> > ---
> >  include/linux/btf.h   | 1 +
> >  kernel/bpf/verifier.c | 5 +++++
> >  2 files changed, 6 insertions(+)
> >
> > diff --git a/include/linux/btf.h b/include/linux/btf.h
> > index cdb376d53238..51a0961c84e3 100644
> > --- a/include/linux/btf.h
> > +++ b/include/linux/btf.h
> > @@ -49,6 +49,7 @@
> >   * for this case.
> >   */
> >  #define KF_TRUSTED_ARGS (1 << 4) /* kfunc only takes trusted pointer arguments */
> > +#define KF_DESTRUCTIVE  (1 << 5) /* kfunc performs destructive actions */
> >
> 
> Please also document this flag in Documentation/bpf/kfuncs.rst.

Ok, will do.

> And maybe instead of KF_DESTRUCTIVE, it might be more apt to call this
> KF_CAP_SYS_BOOT. While it is true you had a destructive flag for
> programs being loaded earlier, so there was a mapping between the two
> UAPI and kfunc flags, what it has boiled down to is that this flag
> just requires CAP_SYS_BOOT (in addition to other capabilities) during
> load. So that name might express the intent a bit better. We might
> soon have similar flags encoding requirements of other capabilities on
> load.
> 
> The flag rename is just a suggestion, up to you.

This makes sense right now, but if going forward we'll add stricter
signing requirements or other prerequisites we'll either have to rename
the flag back, or add those as separate flags. I guess the decision here
depends on whether some of non-destructive bpf programs might ever require
CAP_SYS_BOOT capabilities or not.

-- 
 Artem
Re: [PATCH bpf-next v3 1/3] bpf: add destructive kfunc flag
Posted by Kumar Kartikeya Dwivedi 3 years, 8 months ago
On Mon, 8 Aug 2022 at 14:41, Artem Savkov <asavkov@redhat.com> wrote:
>
> On Mon, Aug 08, 2022 at 02:14:33PM +0200, Kumar Kartikeya Dwivedi wrote:
> > On Mon, 8 Aug 2022 at 11:48, Artem Savkov <asavkov@redhat.com> wrote:
> > >
> > > Add KF_DESTRUCTIVE flag for destructive functions. Functions with this
> > > flag set will require CAP_SYS_BOOT capabilities.
> > >
> > > Signed-off-by: Artem Savkov <asavkov@redhat.com>
> > > ---
> > >  include/linux/btf.h   | 1 +
> > >  kernel/bpf/verifier.c | 5 +++++
> > >  2 files changed, 6 insertions(+)
> > >
> > > diff --git a/include/linux/btf.h b/include/linux/btf.h
> > > index cdb376d53238..51a0961c84e3 100644
> > > --- a/include/linux/btf.h
> > > +++ b/include/linux/btf.h
> > > @@ -49,6 +49,7 @@
> > >   * for this case.
> > >   */
> > >  #define KF_TRUSTED_ARGS (1 << 4) /* kfunc only takes trusted pointer arguments */
> > > +#define KF_DESTRUCTIVE  (1 << 5) /* kfunc performs destructive actions */
> > >
> >
> > Please also document this flag in Documentation/bpf/kfuncs.rst.
>
> Ok, will do.
>
> > And maybe instead of KF_DESTRUCTIVE, it might be more apt to call this
> > KF_CAP_SYS_BOOT. While it is true you had a destructive flag for
> > programs being loaded earlier, so there was a mapping between the two
> > UAPI and kfunc flags, what it has boiled down to is that this flag
> > just requires CAP_SYS_BOOT (in addition to other capabilities) during
> > load. So that name might express the intent a bit better. We might
> > soon have similar flags encoding requirements of other capabilities on
> > load.
> >
> > The flag rename is just a suggestion, up to you.
>
> This makes sense right now, but if going forward we'll add stricter
> signing requirements or other prerequisites we'll either have to rename
> the flag back, or add those as separate flags. I guess the decision here

IMO we should do that when the time comes, for now it should reflect
the current state.
To me this helper requiring cap_sys_boot is just like how some
existing stable helpers are gated behind bpf_capable or
perfmon_capable.
When it requires that the program calling it be signed, we can revisit this.

> depends on whether some of non-destructive bpf programs might ever require
> CAP_SYS_BOOT capabilities or not.

These are just internal kernel flags, so refactoring/renaming is not a
big deal when it is needed. E.g. we've changed just how kfuncs are
registered twice since the support was added not long ago :).

>
> --
>  Artem
>
Re: [PATCH bpf-next v3 1/3] bpf: add destructive kfunc flag
Posted by Andrii Nakryiko 3 years, 8 months ago
On Mon, Aug 8, 2022 at 6:33 AM Kumar Kartikeya Dwivedi <memxor@gmail.com> wrote:
>
> On Mon, 8 Aug 2022 at 14:41, Artem Savkov <asavkov@redhat.com> wrote:
> >
> > On Mon, Aug 08, 2022 at 02:14:33PM +0200, Kumar Kartikeya Dwivedi wrote:
> > > On Mon, 8 Aug 2022 at 11:48, Artem Savkov <asavkov@redhat.com> wrote:
> > > >
> > > > Add KF_DESTRUCTIVE flag for destructive functions. Functions with this
> > > > flag set will require CAP_SYS_BOOT capabilities.
> > > >
> > > > Signed-off-by: Artem Savkov <asavkov@redhat.com>
> > > > ---
> > > >  include/linux/btf.h   | 1 +
> > > >  kernel/bpf/verifier.c | 5 +++++
> > > >  2 files changed, 6 insertions(+)
> > > >
> > > > diff --git a/include/linux/btf.h b/include/linux/btf.h
> > > > index cdb376d53238..51a0961c84e3 100644
> > > > --- a/include/linux/btf.h
> > > > +++ b/include/linux/btf.h
> > > > @@ -49,6 +49,7 @@
> > > >   * for this case.
> > > >   */
> > > >  #define KF_TRUSTED_ARGS (1 << 4) /* kfunc only takes trusted pointer arguments */
> > > > +#define KF_DESTRUCTIVE  (1 << 5) /* kfunc performs destructive actions */
> > > >
> > >
> > > Please also document this flag in Documentation/bpf/kfuncs.rst.
> >
> > Ok, will do.
> >
> > > And maybe instead of KF_DESTRUCTIVE, it might be more apt to call this
> > > KF_CAP_SYS_BOOT. While it is true you had a destructive flag for
> > > programs being loaded earlier, so there was a mapping between the two
> > > UAPI and kfunc flags, what it has boiled down to is that this flag
> > > just requires CAP_SYS_BOOT (in addition to other capabilities) during
> > > load. So that name might express the intent a bit better. We might
> > > soon have similar flags encoding requirements of other capabilities on
> > > load.
> > >
> > > The flag rename is just a suggestion, up to you.
> >
> > This makes sense right now, but if going forward we'll add stricter
> > signing requirements or other prerequisites we'll either have to rename
> > the flag back, or add those as separate flags. I guess the decision here
>
> IMO we should do that when the time comes, for now it should reflect
> the current state.

But names should be also semantically meaningful, so KF_DESTRUCTIVE
explains that kfunc can do destructive operations, which is better
than just declaring that kfunc needs CAP_SYS_BOOT, as the latter is
current implementation detail which has no bearing on kfunc definition
itself.

Unless we anticipate we'll have another "destructive" kfunc not using
KF_DESTRUCTIVE and instead we'll add some other
KF_CAP_SYS_WHATEVERELSE?

> To me this helper requiring cap_sys_boot is just like how some
> existing stable helpers are gated behind bpf_capable or
> perfmon_capable.
> When it requires that the program calling it be signed, we can revisit this.
>
> > depends on whether some of non-destructive bpf programs might ever require
> > CAP_SYS_BOOT capabilities or not.
>
> These are just internal kernel flags, so refactoring/renaming is not a
> big deal when it is needed. E.g. we've changed just how kfuncs are
> registered twice since the support was added not long ago :).
>
> >
> > --
> >  Artem
> >
Re: [PATCH bpf-next v3 1/3] bpf: add destructive kfunc flag
Posted by Kumar Kartikeya Dwivedi 3 years, 8 months ago
On Tue, 9 Aug 2022 at 02:37, Andrii Nakryiko <andrii.nakryiko@gmail.com> wrote:
>
> On Mon, Aug 8, 2022 at 6:33 AM Kumar Kartikeya Dwivedi <memxor@gmail.com> wrote:
> >
> > On Mon, 8 Aug 2022 at 14:41, Artem Savkov <asavkov@redhat.com> wrote:
> > >
> > > On Mon, Aug 08, 2022 at 02:14:33PM +0200, Kumar Kartikeya Dwivedi wrote:
> > > > On Mon, 8 Aug 2022 at 11:48, Artem Savkov <asavkov@redhat.com> wrote:
> > > > >
> > > > > Add KF_DESTRUCTIVE flag for destructive functions. Functions with this
> > > > > flag set will require CAP_SYS_BOOT capabilities.
> > > > >
> > > > > Signed-off-by: Artem Savkov <asavkov@redhat.com>
> > > > > ---
> > > > >  include/linux/btf.h   | 1 +
> > > > >  kernel/bpf/verifier.c | 5 +++++
> > > > >  2 files changed, 6 insertions(+)
> > > > >
> > > > > diff --git a/include/linux/btf.h b/include/linux/btf.h
> > > > > index cdb376d53238..51a0961c84e3 100644
> > > > > --- a/include/linux/btf.h
> > > > > +++ b/include/linux/btf.h
> > > > > @@ -49,6 +49,7 @@
> > > > >   * for this case.
> > > > >   */
> > > > >  #define KF_TRUSTED_ARGS (1 << 4) /* kfunc only takes trusted pointer arguments */
> > > > > +#define KF_DESTRUCTIVE  (1 << 5) /* kfunc performs destructive actions */
> > > > >
> > > >
> > > > Please also document this flag in Documentation/bpf/kfuncs.rst.
> > >
> > > Ok, will do.
> > >
> > > > And maybe instead of KF_DESTRUCTIVE, it might be more apt to call this
> > > > KF_CAP_SYS_BOOT. While it is true you had a destructive flag for
> > > > programs being loaded earlier, so there was a mapping between the two
> > > > UAPI and kfunc flags, what it has boiled down to is that this flag
> > > > just requires CAP_SYS_BOOT (in addition to other capabilities) during
> > > > load. So that name might express the intent a bit better. We might
> > > > soon have similar flags encoding requirements of other capabilities on
> > > > load.
> > > >
> > > > The flag rename is just a suggestion, up to you.
> > >
> > > This makes sense right now, but if going forward we'll add stricter
> > > signing requirements or other prerequisites we'll either have to rename
> > > the flag back, or add those as separate flags. I guess the decision here
> >
> > IMO we should do that when the time comes, for now it should reflect
> > the current state.
>
> But names should be also semantically meaningful, so KF_DESTRUCTIVE
> explains that kfunc can do destructive operations, which is better
> than just declaring that kfunc needs CAP_SYS_BOOT, as the latter is
> current implementation detail which has no bearing on kfunc definition
> itself.
>
> Unless we anticipate we'll have another "destructive" kfunc not using
> KF_DESTRUCTIVE and instead we'll add some other
> KF_CAP_SYS_WHATEVERELSE?
>

I just found it a bit odd that KF_DESTRUCTIVE would require
CAP_SYS_BOOT. When thinking about what one would write in the docs:
just that KF_DESTRUCTIVE kfuncs can do destructive operations? That
doesn't really capture what the flag ends up doing to the kfunc (it
limits use to those who have a certain cap on program load). There can
be several destructive operations (e.g. a frequently mentioned socket
kill helper that may be considered equally destructive for some
workload) but would probably require CAP_NET_ADMIN instead.

But anyway, I didn't really want to bikeshed over this :), we can give
it a better name next time something like this is added, and just go
with KF_DESTRUCTIVE for now.

> > To me this helper requiring cap_sys_boot is just like how some
> > existing stable helpers are gated behind bpf_capable or
> > perfmon_capable.
> > When it requires that the program calling it be signed, we can revisit this.
> >
> > > depends on whether some of non-destructive bpf programs might ever require
> > > CAP_SYS_BOOT capabilities or not.
> >
> > These are just internal kernel flags, so refactoring/renaming is not a
> > big deal when it is needed. E.g. we've changed just how kfuncs are
> > registered twice since the support was added not long ago :).
> >
> > >
> > > --
> > >  Artem
> > >