Skip to content

Commit

Permalink
selftests/xsk: Avoid use-after-free on ctx
Browse files Browse the repository at this point in the history
The put lowers the reference count to 0 and frees ctx, reading it
afterwards is invalid. Move the put after the uses and determine the
last use by the reference count being 1.

Fixes: 39e940d ("selftests/xsk: Destroy BPF resources only when ctx refcount drops to 0")
Signed-off-by: Ian Rogers <irogers@google.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Magnus Karlsson <magnus.karlsson@intel.com>
Link: https://lore.kernel.org/bpf/20220901202645.1463552-1-irogers@google.com
  • Loading branch information
Ian Rogers authored and Daniel Borkmann committed Sep 2, 2022
1 parent afef88e commit af515a5
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions tools/testing/selftests/bpf/xsk.c
Original file line number Diff line number Diff line change
Expand Up @@ -1237,15 +1237,15 @@ void xsk_socket__delete(struct xsk_socket *xsk)
ctx = xsk->ctx;
umem = ctx->umem;

xsk_put_ctx(ctx, true);

if (!ctx->refcount) {
if (ctx->refcount == 1) {
xsk_delete_bpf_maps(xsk);
close(ctx->prog_fd);
if (ctx->has_bpf_link)
close(ctx->link_fd);
}

xsk_put_ctx(ctx, true);

err = xsk_get_mmap_offsets(xsk->fd, &off);
if (!err) {
if (xsk->rx) {
Expand Down

0 comments on commit af515a5

Please sign in to comment.