Skip to content

Commit f979823

Browse files
anakryikoAlexei Starovoitov
authored and
Alexei Starovoitov
committed
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: 9fc205b ("libbpf: Add sane strncpy alternative and use it internally") Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent a34efe5 commit f979823

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1320,7 +1320,10 @@ static int bpf_object__check_endianness(struct bpf_object *obj)
13201320
static int
13211321
bpf_object__init_license(struct bpf_object *obj, void *data, size_t size)
13221322
{
1323-
libbpf_strlcpy(obj->license, data, sizeof(obj->license));
1323+
/* libbpf_strlcpy() only copies first N - 1 bytes, so size + 1 won't
1324+
* go over allowed ELF data section buffer
1325+
*/
1326+
libbpf_strlcpy(obj->license, data, min(size + 1, sizeof(obj->license)));
13241327
pr_debug("license of %s is %s\n", obj->path, obj->license);
13251328
return 0;
13261329
}

0 commit comments

Comments
 (0)