Sleepable LSM Hooks
Subsystems (6)
v2 cover Sleepable LSM Hooks
From: KP Singh <[email protected]>
# v1 -> v2
* Fixed typos and formatting errors.
* Added Andrii's ack.
KP Singh (2):
bpf: Augment the set of sleepable LSM hooks
bpf: Expose bpf_d_path helper to sleepable LSM hooks
include/linux/bpf_lsm.h | 7 +++ kernel/bpf/bpf_lsm.c | 121 +++++++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 16 +----- kernel/trace/bpf_trace.c | 7 ++- 4 files changed, 135 insertions(+), 16 deletions(-)
Signature · 1 line
2.29.2.222.g5d2a92d10f8-goog
PATCH v2 1/2 bpf: Augment the set of sleepable LSM hooks
From: KP Singh <[email protected]>
Update the set of sleepable hooks with the ones that do not trigger a warning with might_fault() when exercised with the correct kernel config options enabled, i.e.
DEBUG_ATOMIC_SLEEP=y
LOCKDEP=y
PROVE_LOCKING=y
This means that a sleepable LSM eBPF program can be attached to these LSM hooks. A new helper method bpf_lsm_is_sleepable_hook is added and the set is maintained locally in bpf_lsm.c
A comment is added about the list of LSM hooks that have been observed to be called from softirqs, atomic contexts, or the ones that can trigger pagefaults and thus should not be added to this list.
--- include/linux/bpf_lsm.h | 7 +++ kernel/bpf/bpf_lsm.c | 121 ++++++++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 16 +----- 3 files changed, 129 insertions(+), 15 deletions(-)
include/linux/bpf_lsm.h +7 -0
@@ -27,6 +27,8 @@ extern struct lsm_blob_sizes bpf_lsm_blob_sizes;int bpf_lsm_verify_prog (struct bpf_verifier_log *vlog,const struct bpf_prog *prog); +bool bpf_lsm_is_sleepable_hook (u32 btf_id); +static inline struct bpf_storage_blob *bpf_inode (const struct inode *inode) {
@@ -54,6 +56,11 @@ void bpf_task_storage_free(struct task_struct *task);#else /* !CONFIG_BPF_LSM */ +static inline bool bpf_lsm_is_sleepable_hook (u32 btf_id) +{ +return false; +} +static inline int bpf_lsm_verify_prog (struct bpf_verifier_log *vlog,const struct bpf_prog *prog) {
kernel/bpf/bpf_lsm.c +121 -0
133 lines not on this page · raw message
kernel/bpf/verifier.c +1 -15
@@ -11477,20 +11477,6 @@ static int check_attach_modify_return(unsigned long addr, const char *func_name)return -EINVAL ; } -/* non exhaustive list of sleepable bpf_lsm_*() functions */ -BTF_SET_START(btf_sleepable_lsm_hooks) -#ifdef CONFIG_BPF_LSM -BTF_ID(func, bpf_lsm_bprm_committed_creds) -#else -BTF_ID_UNUSED -#endif -BTF_SET_END(btf_sleepable_lsm_hooks) - -static int check_sleepable_lsm_hook(u32 btf_id) -{ - return btf_id_set_contains(&btf_sleepable_lsm_hooks, btf_id); -} -/* list of non-sleepable functions that are otherwise on * ALLOW_ERROR_INJECTION list */
@@ -11712,7 +11698,7 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,/* LSM progs check that they are attached to bpf_lsm_*() funcs. * Only some of them are sleepable. */ -if (check_sleepable_lsm_hook (btf_id)) +if (bpf_lsm_is_sleepable_hook (btf_id)) ret =0 ;break ;default :
Signature · 1 line
2.29.2.222.g5d2a92d10f8-goog
Daniel Borkmann
maintainerRe: [PATCH bpf-next v2 1/2] bpf: Augment the set of sleepable LSM hooks
Quoted 18 lines · KP Singh
From: KP Singh <[email protected]>
Update the set of sleepable hooks with the ones that do not trigger a warning with
might_fault()when exercised with the correct kernel config options enabled, i.e.DEBUG_ATOMIC_SLEEP=y
LOCKDEP=y
PROVE_LOCKING=yThis means that a sleepable LSM eBPF program can be attached to these LSM hooks. A new helper method
bpf_lsm_is_sleepable_hookis added and the set is maintained locally inbpf_lsm.cA comment is added about the list of LSM hooks that have been observed to be called from softirqs, atomic contexts, or the ones that can trigger pagefaults and thus should not be added to this list.
[...]
Quoted 43 lines
+/* The set of hooks which are called without pagefaults disabled and are allowed
+ * to "sleep" and thus can be used for sleeable BPF programs.
+ *
+ * There are some hooks which have been observed to be called from a
+ * non-sleepable context and should not be added to this set:
+ *+ * bpf_lsm_bpf_prog_free_security + * bpf_lsm_capable + * bpf_lsm_cred_free + * bpf_lsm_d_instantiate + * bpf_lsm_file_alloc_security + * bpf_lsm_file_mprotect + * bpf_lsm_file_send_sigiotask + * bpf_lsm_inet_conn_request + * bpf_lsm_inet_csk_clone + * bpf_lsm_inode_alloc_security + * bpf_lsm_inode_follow_link + * bpf_lsm_inode_permission + * bpf_lsm_key_permission + * bpf_lsm_locked_down + * bpf_lsm_mmap_addr + * bpf_lsm_perf_event_read + * bpf_lsm_ptrace_access_check + * bpf_lsm_req_classify_flow + * bpf_lsm_sb_free_security + * bpf_lsm_sk_alloc_security + * bpf_lsm_sk_clone_security + * bpf_lsm_sk_free_security + * bpf_lsm_sk_getsecid + * bpf_lsm_socket_sock_rcv_skb + * bpf_lsm_sock_graft + * bpf_lsm_task_free + * bpf_lsm_task_getioprio + * bpf_lsm_task_getscheduler + * bpf_lsm_task_kill + * bpf_lsm_task_setioprio + * bpf_lsm_task_setnice + * bpf_lsm_task_setpgid + * bpf_lsm_task_setrlimit + * bpf_lsm_unix_may_send + * bpf_lsm_unix_stream_connect + * bpf_lsm_vm_enough_memory
Quoted 1 line
+ */
I think this is very useful info. I was wondering whether it would make sense to annotate these more closely to the code so there's less chance this info becomes stale? Maybe something like below, not sure ... issue is if you would just place a cant_sleep() in there it might be wrong since this should just document that it can be invoked from non-sleepable context but it might not have to.
security/security.c +6 -0
@@ -94,6 +94,11 @@ static __initdata bool debug;pr_info (__VA_ARGS__); \ }while (0 ) +/* +* Placeholder for now to document that hook implementation cannot sleep +* since it could potentially be called from non-sleepable context, too. + */ +#define hook_cant_sleep () do { } while (0) +static bool __init is_enabled (struct lsm_info *lsm) {
if (!lsm->enabled)
@@ -2522,6 +2527,7 @@ void security_bpf_map_free(struct bpf_map *map)
}
void security_bpf_prog_free(struct bpf_prog_aux *aux)
{
+ hook_cant_sleep();
call_void_hook(bpf_prog_free_security, aux); }
#endif /* CONFIG_BPF_SYSCALL */
Re: [PATCH bpf-next v2 1/2] bpf: Augment the set of sleepable LSM hooks
Quoted 7 lines · Daniel Borkmann
Quoted 5 lines · KP Singh
From: KP Singh <[email protected]>
Update the set of sleepable hooks with the ones that do not trigger a warning with
might_fault()when exercised with the correct kernel config options enabled, i.e.
[...]
Quoted 7 lines
I think this is very useful info. I was wondering whether it would make sense to annotate these more closely to the code so there's less chance this info becomes stale? Maybe something like below, not sure ... issue is if you would just place a
cant_sleep()in there it might be wrong since this should just document that it can be invoked from non-sleepable context but it might not have to.
Indeed, this is why I did not make an explicit cant_sleep() call for these hooks in __bpf_prog_enter (with a change in the signature to pass struct *prog).
security/security.c static __initdata bool debug; · line 101
Quoted 13 lines
security/security.c +5
-0@@ -94,6 +94,11 @@ static __initdata bool debug;pr_info (__VA_ARGS__); \ }while (0 ) +/* +* Placeholder for now to document that hook implementation cannot sleep +* since it could potentially be called from non-sleepable context, too. + */ +#define hook_cant_sleep () do { } while (0)
Good idea!
At the very least, we can update the comments in lsm_hooks.h which already mention some of the LSM hooks as being called from non-sleepable contexts.
I will remove this comment, send a separate patch to security folks and respin these patches.
-KP
Quoted 12 lines
security/security.c +2
-0+static bool __init is_enabled (struct lsm_info *lsm) {if (!lsm->enabled)@@ -2522,6 +2527,7 @@ void security_bpf_map_free(struct bpf_map *map) }void security_bpf_prog_free (struct bpf_prog_aux *aux) { +hook_cant_sleep ();call_void_hook (bpf_prog_free_security, aux); }#endif /* CONFIG_BPF_SYSCALL */
PATCH v2 2/2 bpf: Expose bpf_d_path helper to sleepable LSM hooks
In reply to KP SinghFrom: 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.
--- kernel/trace/bpf_trace.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/trace/bpf_trace.c +6 -1
@@ -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)
Signature · 1 line
2.29.2.222.g5d2a92d10f8-goog
Quoted 7 lines · KP Singh
From: KP Singh <[email protected]>
# v1 -> v2* Fixed typos and formatting errors.
* Added Andrii's ack.
Oops, I sent an older patch file which does not have Andrii's ack.