From patchwork Wed Oct 19 08:25:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 4713 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp213884wrs; Wed, 19 Oct 2022 02:11:22 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4tcDfXPvlikYdIs25TLEyhZ5gcKJQWgCxDnGbpgc9gwtJChww1r0ad6kY+U5Qik3jg8m1p X-Received: by 2002:a05:6402:3c5:b0:45b:55d8:21ff with SMTP id t5-20020a05640203c500b0045b55d821ffmr6455745edw.253.1666170681984; Wed, 19 Oct 2022 02:11:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666170681; cv=none; d=google.com; s=arc-20160816; b=jfM0JK71YVE4vKxa0rQSUFRQkVWnWT6gQJSFXhVYVoPHYaPg3oQKspKqNd/hytSfh6 3uPTpPGOdT+/XRRoKn8mp/B3rH0x66CJ9o+Ee9VdoYAbbGSn7CPbxvJXKKWRH2WIBK+B SNq7SznlQ9lgHnKwz7CjL8ZjhiMXrw2Plp0v36ZUa9KbMXg737GBp+Kaq6gWoI6laY3V 3Gi+A+X1zOAKuGArSxF6eFdBLeEB7qpRfW8aNkL5E5A+oMSDbrP5fPWBJkF9beLpRLuK 7SIDhlGfaLjGgbtR2T+iq5Qe0r3fNhD5vNdRHFoqbHdiyFPw4u6SgrQT6T65+wcqVCJR t/4A== 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=B8xFbSWI3Q/KI2+3yAXY6ILxH+VsePEyTPm6vJn9Tno=; b=Shfd/BgsifNusEAZMQNYHpQbrvAW6okopLc0RWqJ0L8tA9ZnohyRZvp6m9qVyBUjuW 4aNYOyUuTVXhC1XkVx0lar+vKZW7a1RqztDxsOynhtT07UAOvpbtLS6LxubleAWBsVLp S3xEftUGPK9fv4VDpTGD4YueBzPCCVwIcuFiKKpK7Hf4F85Kg6CyVbN4+2DKMQy58Frz 6xuVGEsQCzxbxJRUwejHE9dEj7BmpfaWHWjfA7w60hvKUl+wls8FVj8iB6Zsz22VKNvw Fxmy9nLK7N26azRiQb4CuvSIgpLvQUuRHm+43NijJQJOWkAnhgxbAPD5T37LfIfN4WOQ 5hfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=UKYvfa06; 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 b13-20020a056402278d00b0045ddb2076casi4007479ede.177.2022.10.19.02.10.56; Wed, 19 Oct 2022 02:11:21 -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=UKYvfa06; 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 S232420AbiJSJHT (ORCPT + 99 others); Wed, 19 Oct 2022 05:07:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47622 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232735AbiJSJFA (ORCPT ); Wed, 19 Oct 2022 05:05:00 -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 9CFAC80BE7; Wed, 19 Oct 2022 01:58:51 -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 6DDC461852; Wed, 19 Oct 2022 08:49:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 81EDEC433D7; Wed, 19 Oct 2022 08:49:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666169378; bh=cWq4xP8NGRkzIrU/H+Fa2QHaOV3Z04pSDgZ9XoUKCLw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UKYvfa06Sjz5S6GBvBXckDcLfjfCIDwa3zbhFFofYvpfKOLoUX5a9bu5ILDvVg7kg qrGADoG8QkBjmioq8bP3nV4OzbB98iqhpZeFjGSIWtaNRqBSRhbnrmsNPWGSJbtv3k paB9al+SYAcqamZF2zofwIevrud9tccc1XHuupWw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Hao Sun , Hou Tao , Martin KaFai Lau , Sasha Levin Subject: [PATCH 6.0 254/862] bpf: Propagate error from htab_lock_bucket() to userspace Date: Wed, 19 Oct 2022 10:25:41 +0200 Message-Id: <20221019083301.276436592@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?1747106589423397750?= X-GMAIL-MSGID: =?utf-8?q?1747106589423397750?= From: Hou Tao [ Upstream commit 66a7a92e4d0d091e79148a4c6ec15d1da65f4280 ] In __htab_map_lookup_and_delete_batch() if htab_lock_bucket() returns -EBUSY, it will go to next bucket. Going to next bucket may not only skip the elements in current bucket silently, but also incur out-of-bound memory access or expose kernel memory to userspace if current bucket_cnt is greater than bucket_size or zero. Fixing it by stopping batch operation and returning -EBUSY when htab_lock_bucket() fails, and the application can retry or skip the busy batch as needed. Fixes: 20b6cc34ea74 ("bpf: Avoid hashtab deadlock with map_locked") Reported-by: Hao Sun Signed-off-by: Hou Tao Link: https://lore.kernel.org/r/20220831042629.130006-3-houtao@huaweicloud.com Signed-off-by: Martin KaFai Lau Signed-off-by: Sasha Levin --- kernel/bpf/hashtab.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index ad09da139589..75f77df910dc 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -1704,8 +1704,11 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *map, /* do not grab the lock unless need it (bucket_cnt > 0). */ if (locked) { ret = htab_lock_bucket(htab, b, batch, &flags); - if (ret) - goto next_batch; + if (ret) { + rcu_read_unlock(); + bpf_enable_instrumentation(); + goto after_loop; + } } bucket_cnt = 0;