Skip to contentSkip to conversation

libbpf: fix negative FD close() in xsk_setup_xdp_prog()

PATCH Applied Andrii Nakryiko 2 participants 2 messages Nov 7, 2019

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

PATCH 1/1 libbpf: fix negative FD close() in xsk_setup_xdp_prog()

Fix issue reported by static analysis (Coverity). If bpf_prog_get_fd_by_id() fails, xsk_lookup_bpf_maps() will fail as well and clean-up code will attempt close() with fd=-1. Fix by checking bpf_prog_get_fd_by_id() return result and exiting early.

Fixes 10a13bb40e54 ("libbpf: remove qidconf and better support external bpf programs.")
---
 tools/lib/bpf/xsk.c | 2 ++
 1 file changed, 2 insertions(+)
tools/lib/bpf/xsk.c +2 -0
index 74d84f36a5b2..86c1b61017f6 100644
@@ -553,6 +553,8 @@ static int xsk_setup_xdp_prog(struct xsk_socket *xsk)
 		}
 	} else {
 		xsk->prog_fd = bpf_prog_get_fd_by_id(prog_id);
+		if (xsk->prog_fd < 0)
+			return -errno;
 		err = xsk_lookup_bpf_maps(xsk);
 		if (err) {
 			close(xsk->prog_fd);
Signature · 1 line
2.17.1
maintainer Applied
#1 lore raw
Quoted 7 lines · Andrii Nakryiko

Fix issue reported by static analysis (Coverity). If bpf_prog_get_fd_by_id() fails, xsk_lookup_bpf_maps() will fail as well and clean-up code will attempt close() with fd=-1. Fix by checking bpf_prog_get_fd_by_id() return result and exiting early.

Fixes 10a13bb40e54 ("libbpf: remove qidconf and better support external bpf programs.")

Applied, thanks!