Skip to content

Commit

Permalink
libbpf: Fix possible use after free in xsk_socket__delete
Browse files Browse the repository at this point in the history
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 <magnus.karlsson@intel.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/1604396490-12129-3-git-send-email-magnus.karlsson@gmail.com
  • Loading branch information
magnus-karlsson authored and borkmann committed Nov 4, 2020
1 parent f78331f commit 25cf73b
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions tools/lib/bpf/xsk.c
Original file line number Diff line number Diff line change
Expand Up @@ -892,13 +892,15 @@ void xsk_socket__delete(struct xsk_socket *xsk)
{
size_t desc_sz = sizeof(struct xdp_desc);
struct xdp_mmap_offsets off;
struct xsk_umem *umem;
struct xsk_ctx *ctx;
int err;

if (!xsk)
return;

ctx = xsk->ctx;
umem = ctx->umem;
if (ctx->prog_fd != -1) {
xsk_delete_bpf_maps(xsk);
close(ctx->prog_fd);
Expand All @@ -918,11 +920,11 @@ void xsk_socket__delete(struct xsk_socket *xsk)

xsk_put_ctx(ctx);

ctx->umem->refcount--;
umem->refcount--;
/* Do not close an fd that also has an associated umem connected
* to it.
*/
if (xsk->fd != ctx->umem->fd)
if (xsk->fd != umem->fd)
close(xsk->fd);
free(xsk);
}

0 comments on commit 25cf73b

Please sign in to comment.