Skip to contentSkip to conversation

libbpf: avoid reading past ELF data section end when copying license

PATCH Applied Andrii Nakryiko 2 participants 2 messages Dec 14, 2021 – Dec 15, 2021

Subsystems (2)
lore
Share & tools
lore

Andrii Nakryiko

author maintainer
#0 lore raw

PATCH 1/1 libbpf: avoid reading past ELF data section end when copying license

Fix possible read beyond ELF "license" data section if the license string is not properly zero-terminated. Use the fact that libbpf_strlcpy never accesses the (N-1)st byte of the source string because it's replaced with '\0' anyways.

If this happens, it's a violation of contract between libbpf and a user, but not handling this more robustly upsets CIFuzz, so given the fix is trivial, let's fix the potential issue.

Fixes 9fc205b413b3 ("libbpf: Add sane strncpy alternative and use it internally")
---
 tools/lib/bpf/libbpf.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +4 -1
index 684c34ce1a26..cf862a19222b 100644
@@ -1320,7 +1320,10 @@ static int bpf_object__check_endianness(struct bpf_object *obj)
 static int
 bpf_object__init_license(struct bpf_object *obj, void *data, size_t size)
 {
-	libbpf_strlcpy(obj->license, data, sizeof(obj->license));
+	/* libbpf_strlcpy() only copies first N - 1 bytes, so size + 1 won't
+	 * go over allowed ELF data section buffer
+	 */
+	libbpf_strlcpy(obj->license, data, min(size + 1, sizeof(obj->license)));
 	pr_debug("license of %s is %s\n", obj->path, obj->license);
 	return 0;
 }
Signature · 1 line
2.30.2
#1 lore raw
Quoted 6 lines · Andrii Nakryiko

Fix possible read beyond ELF "license" data section if the license string is not properly zero-terminated. Use the fact that libbpf_strlcpy never accesses the (N-1)st byte of the source string because it's replaced with '\0' anyways.

Quoted 6 lines

If this happens, it's a violation of contract between libbpf and a user, but not handling this more robustly upsets CIFuzz, so given the fix is trivial, let's fix the potential issue.

Fixes 9fc205b413b3 ("libbpf: Add sane strncpy alternative and use it internally")

Applied. Thanks