From patchwork Mon Oct 24 11:28:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 9249 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp461289wru; Mon, 24 Oct 2022 06:43:34 -0700 (PDT) X-Google-Smtp-Source: AMsMyM42spOCaWIr/nQaBbo7rFq2Ou102l70nCy8bOg3OQKrOIVwOfDp6OaOrs1bCOym/gnlDoHS X-Received: by 2002:a17:907:3e01:b0:730:a690:a211 with SMTP id hp1-20020a1709073e0100b00730a690a211mr27397254ejc.596.1666619014121; Mon, 24 Oct 2022 06:43:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666619014; cv=none; d=google.com; s=arc-20160816; b=m1zo8gcF3tVBQZX5dkwOK1pRybQlIhOJnwMb4VT66IaTI+1FfDjbz8jzC93pzHLIJ5 CPedWJHKWj6PFiw4rt23XTqjFdKJO47p8HW8b5eDktoXZDWkceOH+W23+nVTwoucgflb kkDZrcxbCortN9E0rFNCftyim/st9kznXFmLre6tDcD9dfValFmw9KNHyGaljNwl+q3C NnL0OwyxKJanVD2KA8tJ3xB0MNIAg2nAbc1xu19/hwO/m5FunP1tI4DUSLgDZJ3PAp3T JZULEpXuw0/NpO+mhSk6fz2JnpD5SFHqqCmualvDud5GNqWSeWAsn9E7jlZdfgYGIV1u 7yMA== 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=/Bz1/2F8wcDIMOGJnYsXmMMs1Gg+eAMd2gGJB5nl6T4=; b=md15KpOoHHG1pOEa62xEa2nf62m4v5/m0+4mUooD/AgT3jKL0A24EotkDFnysiKO0z fanbVJUdhsG2hXuLkb+ye7RkxgH6VLm1C5VXMIaT23MF+BeVefgCjbJh35ceo8NpgHy7 771WoMXqyvoG7jXFyKe8oy21QDglbmsujNf6uWDllzOmuSw5Tq7eTJ0WdH+5uCXcdDyk ASXkv9mNNtLQF3yXC0Hn8gHjt3QDZrHNa0AIVTu6cgBps0BDXZMYgXf4kAYgFlfpqq+B HFfO7dkeDYHuUZAFVqPhN99fF6dLHBTxnr6gDYZXj1AXh8JEMLVwcB48Sg6YX20TGPPp jpZg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Q0Kd1Ele; 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 v4-20020aa7d9c4000000b00458bb34ee93si10024490eds.149.2022.10.24.06.43.10; Mon, 24 Oct 2022 06:43:34 -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=Q0Kd1Ele; 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 S235360AbiJXNeA (ORCPT + 99 others); Mon, 24 Oct 2022 09:34:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236175AbiJXN34 (ORCPT ); Mon, 24 Oct 2022 09:29:56 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76297AC397; Mon, 24 Oct 2022 05:32:58 -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 D4C2661254; Mon, 24 Oct 2022 12:20:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E90FDC433D6; Mon, 24 Oct 2022 12:20:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666614045; bh=/1ZCy2yMOfLvr4rzRd2jKrGbKf9H84wkmcfWzYf04SQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Q0Kd1Elebq1+0oUxbZe3SGGI87FgXa4XtRWQ4JMn01j5wsWTHQMMA26Z8PnP7DZ9d I7ShUikRKo/wAyaVo1CvJvGxyIMmnU4+AsqHtOvwOWeOqbaWq5JH5jYVaispZNBcD0 3EHkN3/tqyz0+yNgDC7QAYEtR6PxF5AvhmQwYzJM= 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.10 110/390] selftests/xsk: Avoid use-after-free on ctx Date: Mon, 24 Oct 2022 13:28:27 +0200 Message-Id: <20221024113027.405755567@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024113022.510008560@linuxfoundation.org> References: <20221024113022.510008560@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.6 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?1747576699147216315?= X-GMAIL-MSGID: =?utf-8?q?1747576699147216315?= 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 e8745f646371..fa1f8faf7dfe 100644 --- a/tools/lib/bpf/xsk.c +++ b/tools/lib/bpf/xsk.c @@ -930,13 +930,13 @@ 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); } + xsk_put_ctx(ctx, true); + err = xsk_get_mmap_offsets(xsk->fd, &off); if (!err) { if (xsk->rx) {