Skip to contentSkip to conversation

selftests/xsk: Avoid use-after-free on ctx

PATCH Applied Ian Rogers 3 participants 3 messages Sep 1, 2022 – Sep 2, 2022

Subsystems (6)
lore
Share & tools
lore

Ian Rogers

author
#0 lore raw

PATCH 1/1 selftests/xsk: Avoid use-after-free on ctx

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 39e940d4abfa ("selftests/xsk: Destroy BPF resources only when ctx refcount drops to 0")
---
 tools/testing/selftests/bpf/xsk.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/xsk.c +3 -3
index f2721a4ae7c5..0b3ff49c740d 100644
@@ -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) {
Signature · 1 line
2.37.2.789.g6183377224-goog
Acked-by on v1 1/1
#1 lore raw
Quoted 4 lines · Ian Rogers

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.

Thanks for spotting and fixing this Ian.

Quoted 32 lines
Fixes 39e940d4abfa ("selftests/xsk: Destroy BPF resources only when ctx refcount drops to 0")
---
 tools/testing/selftests/bpf/xsk.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/xsk.c +3 -4
index f2721a4ae7c5..0b3ff49c740d 100644
@@ -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) {
--

2.37.2.789.g6183377224-goog

#2 lore raw
In reply to Ian Rogers
This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Quoted 8 lines · On Thu, 1 Sep 2022 13:26:45 -0700 you wrote:

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 39e940d4abfa ("selftests/xsk: Destroy BPF resources only when ctx refcount drops to 0")

[...]

Here is the summary with links:
- [v1] selftests/xsk: Avoid use-after-free on ctx

    https://git.kernel.org/bpf/bpf-next/c/af515a5587b8

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html