From nobody Fri Dec 19 22:02:52 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8672AFA3742 for ; Mon, 24 Oct 2022 16:52:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233725AbiJXQwO (ORCPT ); Mon, 24 Oct 2022 12:52:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58066 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235170AbiJXQtK (ORCPT ); Mon, 24 Oct 2022 12:49:10 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 36103C96FA; Mon, 24 Oct 2022 08:32:11 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 2E1D7B8197E; Mon, 24 Oct 2022 12:41:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 809A4C433D6; Mon, 24 Oct 2022 12:41:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666615295; bh=lqrC55lsgxGFYlCL7aLsw05ogFmTRBAR4987pkT+MJY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BUUfCjaDANdqFB+A+2Dg+mPk/NomYG3sz5Jozq2ICMCU9U+qGp4zoOYH8DiAMRuzp AjpkIcccW708zvNnfC6aG6bPrtdPSZ0CUJKHaYPXjqMhROHy9uS7i4A/Tg+3OHyxQB 9qvPQFSdFs4STUVJLx5gATnGDDyfpy1k4fLHmOpQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ian Rogers , Daniel Borkmann , Magnus Karlsson , Sasha Levin Subject: [PATCH 5.15 166/530] selftests/xsk: Avoid use-after-free on ctx Date: Mon, 24 Oct 2022 13:28:30 +0200 Message-Id: <20221024113052.578179410@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024113044.976326639@linuxfoundation.org> References: <20221024113044.976326639@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Ian Rogers [ Upstream commit af515a5587b8f45f19e11657746e0c89411b0380 ] 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 re= fcount drops to 0") Signed-off-by: Ian Rogers Signed-off-by: Daniel Borkmann Acked-by: Magnus Karlsson Link: https://lore.kernel.org/bpf/20220901202645.1463552-1-irogers@google.c= om Signed-off-by: Sasha Levin --- tools/lib/bpf/xsk.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/lib/bpf/xsk.c b/tools/lib/bpf/xsk.c index 42b8437b0535..2be3197914e4 100644 --- a/tools/lib/bpf/xsk.c +++ b/tools/lib/bpf/xsk.c @@ -1245,15 +1245,15 @@ void xsk_socket__delete(struct xsk_socket *xsk) ctx =3D xsk->ctx; umem =3D ctx->umem; =20 - xsk_put_ctx(ctx, true); - - if (!ctx->refcount) { + if (ctx->refcount =3D=3D 1) { xsk_delete_bpf_maps(xsk); close(ctx->prog_fd); if (ctx->has_bpf_link) close(ctx->link_fd); } =20 + xsk_put_ctx(ctx, true); + err =3D xsk_get_mmap_offsets(xsk->fd, &off); if (!err) { if (xsk->rx) { --=20 2.35.1