[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <CAEf4Bzazw67MNAE9gkFLKLdQnDSvvnmuNrPb7gMf51LkK3pYpg@mail.gmail.com>
Date: Wed, 4 Nov 2020 11:42:54 -0800
From: Andrii Nakryiko <andrii.nakryiko@...il.com>
To: Magnus Karlsson <magnus.karlsson@...il.com>
Cc: Magnus Karlsson <magnus.karlsson@...el.com>,
Björn Töpel <bjorn.topel@...el.com>,
Alexei Starovoitov <ast@...nel.org>,
Daniel Borkmann <daniel@...earbox.net>,
Networking <netdev@...r.kernel.org>,
Jonathan Lemon <jonathan.lemon@...il.com>,
bpf <bpf@...r.kernel.org>
Subject: Re: [PATCH bpf 2/2] libbpf: fix possible use after free in xsk_socket__delete
On Wed, Nov 4, 2020 at 12:27 AM Magnus Karlsson
<magnus.karlsson@...il.com> wrote:
>
> On Tue, Nov 3, 2020 at 8:05 PM Andrii Nakryiko
> <andrii.nakryiko@...il.com> wrote:
> >
> > On Tue, Nov 3, 2020 at 1:42 AM Magnus Karlsson
> > <magnus.karlsson@...il.com> wrote:
> > >
> > > From: Magnus Karlsson <magnus.karlsson@...el.com>
> > >
> > > 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: 2f6324a3937f ("libbpf: Support shared umems between queues and devices")
> > > Signed-off-by: Magnus Karlsson <magnus.karlsson@...el.com>
> > > ---
> > > tools/lib/bpf/xsk.c | 6 ++++--
> > > 1 file changed, 4 insertions(+), 2 deletions(-)
> > >
> > > diff --git a/tools/lib/bpf/xsk.c b/tools/lib/bpf/xsk.c
> > > index 504b7a8..9bc537d 100644
> > > --- a/tools/lib/bpf/xsk.c
> > > +++ b/tools/lib/bpf/xsk.c
> > > @@ -892,6 +892,7 @@ 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;
> > >
> > > @@ -899,6 +900,7 @@ void xsk_socket__delete(struct xsk_socket *xsk)
> > > return;
> > >
> > > ctx = xsk->ctx;
> > > + umem = ctx->umem;
> > > if (ctx->prog_fd != -1) {
> > > xsk_delete_bpf_maps(xsk);
> > > close(ctx->prog_fd);
> > > @@ -918,11 +920,11 @@ void xsk_socket__delete(struct xsk_socket *xsk)
> > >
> > > xsk_put_ctx(ctx);
> > >
> > > - ctx->umem->refcount--;
> > > + umem->refcount--;
> >
> > if you moved ctx->umem->refcount--; to before xdk_put_ctx(ctx), would
> > that also work?
>
> Yes, it would for that statement, but I still need the umem pointer
> for the statement below. And this statement of potentially closing the
> fd needs to be after xsk_put_ctx(). So we might as well keep
> ujmem->refcount-- where it is, if that is ok with you?
Ah, missed the umem->fd below. Then it makes sense, thanks.
Acked-by: Andrii Nakryiko <andrii@...nel.org>
>
> > > /* 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);
> > > }
> > > --
> > > 2.7.4
> > >
Powered by blists - more mailing lists