2020-11-12 17:21:14

by KP Singh

[permalink] [raw]
Subject: [PATCH bpf-next 2/2] bpf: Expose bpf_d_path helper to sleepable LSM hooks

From: KP Singh <[email protected]>

Sleepable hooks are never called from an NMI/interrupt context, so it is
safe to use the bpf_d_path helper in LSM programs attaching to these
hooks.

The helper is not restricted to sleepable programs and merely uses the
list of sleeable hooks as the initial subset of LSM hooks where it can
be used.

Signed-off-by: KP Singh <[email protected]>
---
kernel/trace/bpf_trace.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index e4515b0f62a8..eab1af02c90d 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -16,6 +16,7 @@
#include <linux/syscalls.h>
#include <linux/error-injection.h>
#include <linux/btf_ids.h>
+#include <linux/bpf_lsm.h>

#include <uapi/linux/bpf.h>
#include <uapi/linux/btf.h>
@@ -1178,7 +1179,11 @@ BTF_SET_END(btf_allowlist_d_path)

static bool bpf_d_path_allowed(const struct bpf_prog *prog)
{
- return btf_id_set_contains(&btf_allowlist_d_path, prog->aux->attach_btf_id);
+ if (prog->type == BPF_PROG_TYPE_LSM)
+ return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id);
+
+ return btf_id_set_contains(&btf_allowlist_d_path,
+ prog->aux->attach_btf_id);
}

BTF_ID_LIST_SINGLE(bpf_d_path_btf_ids, struct, path)
--
2.29.2.222.g5d2a92d10f8-goog


2020-11-12 18:53:28

by Andrii Nakryiko

[permalink] [raw]
Subject: Re: [PATCH bpf-next 2/2] bpf: Expose bpf_d_path helper to sleepable LSM hooks

On Thu, Nov 12, 2020 at 9:20 AM KP Singh <[email protected]> wrote:
>
> From: KP Singh <[email protected]>
>
> Sleepable hooks are never called from an NMI/interrupt context, so it is
> safe to use the bpf_d_path helper in LSM programs attaching to these
> hooks.
>
> The helper is not restricted to sleepable programs and merely uses the
> list of sleeable hooks as the initial subset of LSM hooks where it can
> be used.
>
> Signed-off-by: KP Singh <[email protected]>
> ---

LGTM.

Acked-by: Andrii Nakryiko <[email protected]>


> kernel/trace/bpf_trace.c | 7 ++++++-
> 1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> index e4515b0f62a8..eab1af02c90d 100644
> --- a/kernel/trace/bpf_trace.c
> +++ b/kernel/trace/bpf_trace.c
> @@ -16,6 +16,7 @@
> #include <linux/syscalls.h>
> #include <linux/error-injection.h>
> #include <linux/btf_ids.h>
> +#include <linux/bpf_lsm.h>
>
> #include <uapi/linux/bpf.h>
> #include <uapi/linux/btf.h>
> @@ -1178,7 +1179,11 @@ BTF_SET_END(btf_allowlist_d_path)
>
> static bool bpf_d_path_allowed(const struct bpf_prog *prog)
> {
> - return btf_id_set_contains(&btf_allowlist_d_path, prog->aux->attach_btf_id);
> + if (prog->type == BPF_PROG_TYPE_LSM)
> + return bpf_lsm_is_sleepable_hook(prog->aux->attach_btf_id);
> +
> + return btf_id_set_contains(&btf_allowlist_d_path,
> + prog->aux->attach_btf_id);
> }
>
> BTF_ID_LIST_SINGLE(bpf_d_path_btf_ids, struct, path)
> --
> 2.29.2.222.g5d2a92d10f8-goog
>

2020-11-13 03:21:27

by Yonghong Song

[permalink] [raw]
Subject: Re: [PATCH bpf-next 2/2] bpf: Expose bpf_d_path helper to sleepable LSM hooks



On 11/12/20 9:19 AM, KP Singh wrote:
> From: KP Singh <[email protected]>
>
> Sleepable hooks are never called from an NMI/interrupt context, so it is
> safe to use the bpf_d_path helper in LSM programs attaching to these
> hooks.
>
> The helper is not restricted to sleepable programs and merely uses the
> list of sleeable hooks as the initial subset of LSM hooks where it can

sleeable => sleepable

probably not need to resend if no other major changes. The maintainer
can just fix it up before merging.

> be used.
>
> Signed-off-by: KP Singh <[email protected]>

Acked-by: Yonghong Song <[email protected]>

2020-11-13 15:35:49

by Daniel Borkmann

[permalink] [raw]
Subject: Re: [PATCH bpf-next 2/2] bpf: Expose bpf_d_path helper to sleepable LSM hooks

On 11/13/20 4:18 AM, Yonghong Song wrote:
>
>
> On 11/12/20 9:19 AM, KP Singh wrote:
>> From: KP Singh <[email protected]>
>>
>> Sleepable hooks are never called from an NMI/interrupt context, so it is
>> safe to use the bpf_d_path helper in LSM programs attaching to these
>> hooks.
>>
>> The helper is not restricted to sleepable programs and merely uses the
>> list of sleeable hooks as the initial subset of LSM hooks where it can
>
> sleeable => sleepable
>
> probably not need to resend if no other major changes. The maintainer
> can just fix it up before merging.

Did while rebasing & applying, thanks everyone!

>> be used.
>>
>> Signed-off-by: KP Singh <[email protected]>
>
> Acked-by: Yonghong Song <[email protected]>