From patchwork Sat Oct 22 07:21:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 7319 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp1093584wrr; Sat, 22 Oct 2022 00:57:44 -0700 (PDT) X-Google-Smtp-Source: AMsMyM46ao7Ju/z1kienERpUx8wOse2hUbtTg6Z6llOnUieZGHXeWDPYdzEXTihqNOUMDaJK2F3Y X-Received: by 2002:a17:902:ed93:b0:185:4421:24b with SMTP id e19-20020a170902ed9300b001854421024bmr22841220plj.158.1666425464053; Sat, 22 Oct 2022 00:57:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666425464; cv=none; d=google.com; s=arc-20160816; b=uryMA0HoJtP7HvVyRHwWNUUqJEtvavNiEkGPUXkz9PSr0DfYmKeVXIV+2+IU1upLOG /KZlZ94mQAmoEKs6gawJeQ+wDmp+Tdq7tzYzlB6t5gq97fAckgTzitzmSpXodj5YzzPh ZFdZc9ZBxLU0opxQMmk/VA40XJQzXbMFdtuOCprEyZKdefXQ7S6euXw+eWSEfdWBe+sQ 5DwhJw+DnEgddgaY7XsFB9LEm+ELSEme+ygD2k02+GgFoltDO+jQP2eZ6VyC2ITSevll CsGetGcLOzLkezdYlGh3RmIuslakRDOay3aEBJsKDu75CWKGdvb2AhlQUZOctgWHxX83 eEIA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=tod+YBu3wJrXeWsQtbPMwwH04aWfd/GiUUksgrJ83Cw=; b=M7WtLaf7NG5VQlU1o/pFv/9hjXEp2+/3wWc0Biyf0fbFEScDd8c/x1Uh75NgyKXxhg 85NigjWFxJDpgy+NjlHstWj3FMvHnCWJRW86kD4FVxiCVW4M4yj/UXbaE2l4Yc4m8/tU efZ6OiXBsqX1YAxpIDWpCi3cEdg24FjtP+Gq4Y42rqmwXIXgRR6Aic8dbQRug+Qs5rZ5 U35KTIPWPzshYagzeWCjAahrTeQFRohy4jPmql+bDXviJ4/cm/Epwl14YaYVBAI1pSVk 5G+s0jcXcgU0VxTG1N715GVmcLco7BAAIcd2NWRCCZNrNQfEnA9LdqS4RKgbD2MVD4W1 xBOQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ndJTbDHl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y21-20020a056a00191500b0055ffebbba91si24855971pfi.112.2022.10.22.00.57.31; Sat, 22 Oct 2022 00:57:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ndJTbDHl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232116AbiJVH5O (ORCPT + 99 others); Sat, 22 Oct 2022 03:57:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56532 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231974AbiJVHxM (ORCPT ); Sat, 22 Oct 2022 03:53:12 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 65E072CA7DD; Sat, 22 Oct 2022 00:46:55 -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 dfw.source.kernel.org (Postfix) with ESMTPS id E9ED560BB1; Sat, 22 Oct 2022 07:45:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 040F3C433D7; Sat, 22 Oct 2022 07:45:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666424708; bh=X8yel3LUYWigUVRK5S1/ihrs7Lr86aKcl3MMM3hFUdY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ndJTbDHlo8/Ue8QInr1hdk59o6sWDZ80+TV6sTm97eS2WLqQUcea4NR9LTkStGzXK hD2VJvqa0cr3xK0a7u1uwPhz0vMf0/O01JLfU7GYWIb6YdTBlEL4Wsa+E97lHZocw+ cZb0WQxhBlHD8qmYatomoh/Vlzwe0dM33uIoc+k4= 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.19 229/717] selftests/xsk: Avoid use-after-free on ctx Date: Sat, 22 Oct 2022 09:21:48 +0200 Message-Id: <20221022072455.624457009@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221022072415.034382448@linuxfoundation.org> References: <20221022072415.034382448@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747373747573783055?= X-GMAIL-MSGID: =?utf-8?q?1747373747573783055?= 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 refcount 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.com 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 67dc010e9fe3..63954a2e213d 100644 --- a/tools/lib/bpf/xsk.c +++ b/tools/lib/bpf/xsk.c @@ -1228,15 +1228,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) {