Skip to contentSkip to conversation

libbpf: fix passing uninitialized bytes to setsockopt

PATCH Applied Ilya Maximets 3 participants 3 messages Oct 9, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 libbpf: fix passing uninitialized bytes to setsockopt

'struct xdp_umem_reg' has 4 bytes of padding at the end that makes valgrind complain about passing uninitialized stack memory to the syscall:

Syscall param socketcall.setsockopt() points to uninitialised byte(s)

    at 0x4E7AB7E: setsockopt (in /usr/lib64/libc-2.29.so)
    by 0x4BDE035: xsk_umem__create@@LIBBPF_0.0.4 (xsk.c:172)
  Uninitialised value was created by a stack allocation
    at 0x4BDDEBA: xsk_umem__create@@LIBBPF_0.0.4 (xsk.c:140)

Padding bytes appeared after introducing of a new 'flags' field. memset() is required to clear them.

Fixes 10d30e301732 ("libbpf: add flags to umem config")

---

Version 2:
* Struct initializer replaced with explicit memset(). [Andrii]

 tools/lib/bpf/xsk.c | 1 +
 1 file changed, 1 insertion(+)
tools/lib/bpf/xsk.c +1 -0
index a902838f9fcc..9d5348086203 100644
@@ -163,6 +163,7 @@ int xsk_umem__create_v0_0_4(struct xsk_umem **umem_ptr, void *umem_area,
 	umem->umem_area = umem_area;
 	xsk_set_umem_config(&umem->config, usr_config);
 
+	memset(&mr, 0, sizeof(mr));
 	mr.addr = (uintptr_t)umem_area;
 	mr.len = size;
 	mr.chunk_size = umem->config.frame_size;
Signature · 1 line
2.17.1
Acked-by on v2 1/1
#1 lore raw
Quoted 11 lines · Ilya Maximets

'struct xdp_umem_reg' has 4 bytes of padding at the end that makes valgrind complain about passing uninitialized stack memory to the syscall:

Syscall param socketcall.setsockopt() points to uninitialised byte(s)

    at 0x4E7AB7E: setsockopt (in /usr/lib64/libc-2.29.so)
    by 0x4BDE035: xsk_umem__create@@LIBBPF_0.0.4 (xsk.c:172)
  Uninitialised value was created by a stack allocation
    at 0x4BDDEBA: xsk_umem__create@@LIBBPF_0.0.4 (xsk.c:140)
Quoted 7 lines

Padding bytes appeared after introducing of a new 'flags' field. memset() is required to clear them.

Fixes 10d30e301732 ("libbpf: add flags to umem config")

---

Thanks!

Quoted 21 lines

Version 2:
* Struct initializer replaced with explicit memset(). [Andrii]

 tools/lib/bpf/xsk.c | 1 +
 1 file changed, 1 insertion(+)
tools/lib/bpf/xsk.c +1 -1
index a902838f9fcc..9d5348086203 100644
@@ -163,6 +163,7 @@ int xsk_umem__create_v0_0_4(struct xsk_umem **umem_ptr, void *umem_area,
        umem->umem_area = umem_area;
        xsk_set_umem_config(&umem->config, usr_config);
 
+       memset(&mr, 0, sizeof(mr));
        mr.addr = (uintptr_t)umem_area;
        mr.len = size;
        mr.chunk_size = umem->config.frame_size;
--

2.17.1

#2 lore raw
In reply to Ilya Maximets
Quoted 11 lines · Ilya Maximets

'struct xdp_umem_reg' has 4 bytes of padding at the end that makes valgrind complain about passing uninitialized stack memory to the syscall:

Syscall param socketcall.setsockopt() points to uninitialised byte(s)

    at 0x4E7AB7E: setsockopt (in /usr/lib64/libc-2.29.so)
    by 0x4BDE035: xsk_umem__create@@LIBBPF_0.0.4 (xsk.c:172)
  Uninitialised value was created by a stack allocation
    at 0x4BDDEBA: xsk_umem__create@@LIBBPF_0.0.4 (xsk.c:140)
Quoted 5 lines

Padding bytes appeared after introducing of a new 'flags' field. memset() is required to clear them.

Fixes 10d30e301732 ("libbpf: add flags to umem config")

Applied. Thanks