From patchwork Wed Oct 19 08:25:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 5572 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp350769wrs; Wed, 19 Oct 2022 07:14:16 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5SZdPrV/m0cS5Bnc3Ha2EHtDamYGwAfhl6xQSL5HOsC/ReIGUkNJM06KWrvwvSjqhsSFIL X-Received: by 2002:a63:470b:0:b0:442:24d7:578 with SMTP id u11-20020a63470b000000b0044224d70578mr7491550pga.198.1666188855959; Wed, 19 Oct 2022 07:14:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666188855; cv=none; d=google.com; s=arc-20160816; b=VOzL7AA7+JewUPAg6SYpwOSD2u5p5kH91Rd5LZKDnunrcQK7uV+1WN7XbORjIICXAO tuGzkYzbrweAaxIrMvL0dBmsl3DZCzdiKhB7lmX10e1bvqDy0UKdJcY8xDjh4HrIuE9R txjvAivG6riH9VEEtiZ0RDtTTURph7u79+1qKjloiJDwICTytzTWqe8w4HQiZo5S4hou kFCHhXgpj02uxXjAKSc3JgSpHq8mwCOKuhFnj7CM0IcYUqBoO1B6ijUySano1KduA8zu byg8BA49o4/KTXyEvTwMlD9fMhFGjUGPteQokxWETTrOjd7B+FUEGvGGRXf1nR+tFtT5 k3Kg== 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=KBP9CDjuSm1V730VCvHxKXemME8vhoad4zzweXXjX4o=; b=TN+fjEGzPJtE1y5R/Aiua3IsZwLQiGxIuuNAnSOaesd7mMTlCjJTItPC6x/GAMHB+x ptByIzkazbDg5MSBDqEsljQ9eGTtqLu6Ot6OHzpCvJsnXrourA/z/VOBno+uYlmYrV8e SwcuWeDsD8jUSCMofVPUhWiwALdBs1QjJkj7jum/YKWRPpQwlxgGHbMMloeDHITFnh0+ 6/UfRNNsNvP+jdkz/JwvHwIdpJOrRWaeP9dJxEZ6GTnQIBviAsUPS8hnnTYS9bBo9Rjv t3RURU3srlFAF3QvS12AzGT5t+AEE6FwivBVeo/7eZj8G4EJa6jkAha/Vq8hXXclSoiE QAlg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="qV/rN+La"; 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 s123-20020a632c81000000b00443860ee255si18289603pgs.17.2022.10.19.07.13.53; Wed, 19 Oct 2022 07:14:15 -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="qV/rN+La"; 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 S233823AbiJSN5F (ORCPT + 99 others); Wed, 19 Oct 2022 09:57:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233741AbiJSNxS (ORCPT ); Wed, 19 Oct 2022 09:53:18 -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 31164115401; Wed, 19 Oct 2022 06:36:47 -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 E1153B82385; Wed, 19 Oct 2022 08:50:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4D594C433D6; Wed, 19 Oct 2022 08:50:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666169410; bh=fzYPWcBxAX/2ERexkBxL7ZPO3pSxG7BbIQ7qi2LD/8M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qV/rN+La5L8aE4pRe+fEaNeOYKFwEWJBnUbDs2ZxkZTlxdEL7nfUtKVyM02v4WRuy D4Nl3f8zv6UlhwxbkkmPqWSlhB/7dlF34UBzujgmalc0XINp6OtrGlaXdXdU817FG3 h9j36gozf28NMPk0WKgQ2R8a1enjvqzeEAgXLimM= 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 6.0 265/862] selftests/xsk: Avoid use-after-free on ctx Date: Wed, 19 Oct 2022 10:25:52 +0200 Message-Id: <20221019083301.736032033@linuxfoundation.org> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221019083249.951566199@linuxfoundation.org> References: <20221019083249.951566199@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.4 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?1747125646016439700?= X-GMAIL-MSGID: =?utf-8?q?1747125646016439700?= 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/testing/selftests/bpf/xsk.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/bpf/xsk.c b/tools/testing/selftests/bpf/xsk.c index f2721a4ae7c5..0b3ff49c740d 100644 --- a/tools/testing/selftests/bpf/xsk.c +++ b/tools/testing/selftests/bpf/xsk.c @@ -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) {