From: Mykyta Yatsenko yatsenko@meta.com
[ Upstream commit d0445d7dd3fd9b15af7564c38d7aa3cbc29778ee ]
Avoid dereferencing bpf_map_skeleton's link field if it's NULL. If BPF map skeleton is created with the size, that indicates containing link field, but the field was not actually initialized with valid bpf_link pointer, libbpf crashes. This may happen when using libbpf-rs skeleton. Skeleton loading may still progress, but user needs to attach struct_ops map separately.
Signed-off-by: Mykyta Yatsenko yatsenko@meta.com Signed-off-by: Andrii Nakryiko andrii@kernel.org Link: https://lore.kernel.org/bpf/20250514113220.219095-1-mykyta.yatsenko5@gmail.c... Signed-off-by: Sasha Levin sashal@kernel.org ---
Now I have enough information to provide a comprehensive analysis. **YES** This commit should be backported to stable kernel trees. Here's my extensive analysis: ## Critical Vulnerability Fix This commit addresses a **NULL pointer dereference vulnerability** in the `bpf_object__attach_skeleton()` function. The fix adds a simple but crucial NULL check for the `link` pointer before dereferencing it. ## Analysis of the Code Changes ### The Problem Looking at lines 14104-14114 in the current code: ```c link = map_skel->link; if (!link) { pr_warn("map '%s': BPF map skeleton link is uninitialized\n", bpf_map__name(map)); continue; } if (*link) continue; ``` The commit adds a NULL check for `link` before attempting to dereference it with `*link`. **Without this check, if `link` is NULL, the code would crash when executing `if (*link)`**. ### Why This is Critical 1. **Immediate crash potential**: The missing NULL check can cause immediate segmentation faults when `map_skel->link` is NULL 2. **Common occurrence**: This can happen when using libbpf-rs skeletons or other third-party skeleton generators that may not properly initialize the link field 3. **User-facing impact**: The commit message explicitly states this affects libbpf-rs skeleton usage, indicating real-world impact ## Comparison with Historical Patterns This fix follows **exactly the same pattern** as Similar Commit #1 (marked YES for backporting), which added a NULL check to `bpf_object__destroy_skeleton()`: - **Similar Commit #1**: Added `if (!s) return;` to prevent NULL deref in destroy_skeleton - **Current Commit**: Adds `if (!link)` check to prevent NULL deref in attach_skeleton Both are small, defensive programming fixes that prevent crashes without changing functionality. ## Consistency with Existing Code Looking at the current kernel tree, `bpf_object__destroy_skeleton()` already has a NULL check (line 14154-14155): ```c void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s) { if (!s) return; ``` This commit brings `bpf_object__attach_skeleton()` in line with the same defensive programming pattern. ## Stable Tree Criteria Assessment ✅ **Fixes important bug**: Prevents NULL pointer dereference crashes ✅ **Small and contained**: Only adds 4 lines of code ✅ **No architectural changes**: Pure defensive programming ✅ **Minimal regression risk**: Cannot break existing functionality ✅ **Clear side effects**: Only prevents crashes, no behavioral changes ✅ **Confined to subsystem**: Only affects libbpf skeleton handling ## Risk Assessment - **Regression risk**: **Extremely low** - the check only prevents crashes - **Compatibility**: **Perfect** - no API changes, only prevents invalid operations - **Dependencies**: **None** - standalone fix with no external dependencies This is a textbook example of a stable tree candidate: a small, safe fix that prevents crashes without changing any functionality or introducing new behavior.
tools/lib/bpf/libbpf.c | 6 ++++++ 1 file changed, 6 insertions(+)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 6b85060f07b3b..956dfd3b5fc9b 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -14099,6 +14099,12 @@ int bpf_object__attach_skeleton(struct bpf_object_skeleton *s) }
link = map_skel->link; + if (!link) { + pr_warn("map '%s': BPF map skeleton link is uninitialized\n", + bpf_map__name(map)); + continue; + } + if (*link) continue;