Skip to contentSkip to conversation

Sleepable LSM Hooks

PATCH New KP Singh 1 participant 3 messages Nov 13, 2020

Subsystems (5)
BPF Tracing BPF core BPF tracing +1 more subsystems
lore
Share & tools
lore

KP Singh

author
#0 lore raw

v3 cover Sleepable LSM Hooks

From: KP Singh <[email protected]>

# v2 -> v3

* Remove the list of non-sleepable hooks, will send a separate patch to the lsm list based on the discussion with Daniel.
* Add Andrii's ack for real

# 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     | 81 ++++++++++++++++++++++++++++++++++++++++
 kernel/bpf/verifier.c    | 16 +-------
 kernel/trace/bpf_trace.c |  7 +++-
 4 files changed, 95 insertions(+), 16 deletions(-)
Signature · 1 line
2.29.2.299.gdc1121823c-goog
author
#1 lore raw

PATCH v3 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

---
 include/linux/bpf_lsm.h |  7 ++++
 kernel/bpf/bpf_lsm.c    | 81 +++++++++++++++++++++++++++++++++++++++++
 kernel/bpf/verifier.c   | 16 +-------
 3 files changed, 89 insertions(+), 15 deletions(-)
include/linux/bpf_lsm.h +7 -0
index 73226181b744..0d1c33ace398 100644
@@ -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 +81 -0

93 lines not on this page · raw message

kernel/bpf/verifier.c +1 -15
index 10da26e55130..364ec1958c85 100644
@@ -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.299.gdc1121823c-goog
author
#2 lore raw

PATCH v3 2/2 bpf: Expose bpf_d_path helper to sleepable LSM hooks

In reply to KP Singh

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.

---
 kernel/trace/bpf_trace.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/trace/bpf_trace.c +6 -1
index e4515b0f62a8..eab1af02c90d 100644
@@ -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.299.gdc1121823c-goog