Skip to content

Commit 25cf73b

Browse files
magnus-karlssonborkmann
authored andcommitted
libbpf: Fix possible use after free in xsk_socket__delete
Fix a possible use after free in xsk_socket__delete that will happen if xsk_put_ctx() frees the ctx. To fix, save the umem reference taken from the context and just use that instead. Fixes: 2f6324a ("libbpf: Support shared umems between queues and devices") Signed-off-by: Magnus Karlsson <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent f78331f commit 25cf73b

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

tools/lib/bpf/xsk.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -892,13 +892,15 @@ void xsk_socket__delete(struct xsk_socket *xsk)
892892
{
893893
size_t desc_sz = sizeof(struct xdp_desc);
894894
struct xdp_mmap_offsets off;
895+
struct xsk_umem *umem;
895896
struct xsk_ctx *ctx;
896897
int err;
897898

898899
if (!xsk)
899900
return;
900901

901902
ctx = xsk->ctx;
903+
umem = ctx->umem;
902904
if (ctx->prog_fd != -1) {
903905
xsk_delete_bpf_maps(xsk);
904906
close(ctx->prog_fd);
@@ -918,11 +920,11 @@ void xsk_socket__delete(struct xsk_socket *xsk)
918920

919921
xsk_put_ctx(ctx);
920922

921-
ctx->umem->refcount--;
923+
umem->refcount--;
922924
/* Do not close an fd that also has an associated umem connected
923925
* to it.
924926
*/
925-
if (xsk->fd != ctx->umem->fd)
927+
if (xsk->fd != umem->fd)
926928
close(xsk->fd);
927929
free(xsk);
928930
}

0 commit comments

Comments
 (0)