From patchwork Sat Oct 22 07:21:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 7270 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp1091914wrr; Sat, 22 Oct 2022 00:52:15 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4H/SI9Bd0uUgTwJw1LdAD8KbHkbv+SJWQb0/I0PTT/wPOXTeqNoTIvGim5bnajXuY+lXpg X-Received: by 2002:a63:5955:0:b0:438:8287:6a43 with SMTP id j21-20020a635955000000b0043882876a43mr19252056pgm.495.1666425135362; Sat, 22 Oct 2022 00:52:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666425135; cv=none; d=google.com; s=arc-20160816; b=eJ/TAhImNhn6HNDa2NkI9fZ6ok9vq10cWS12zin/bUFrSef+DflZB6oQHMC1h737gn amIexTKaleyhXWDy1wwK5MD0USszrIsGWJ4o61TSXhiJArFIRy1kdXL8xkJRUg2qftR4 4GgtIYbJs0Ypi+2kDF9pKA9JV3putNZZ71awMsvS4w/c5W39BsV4EuECAzJreflK6c10 YGGmQyOD7g6kTLwpoG7IHU/q52qC9Lze8TWkrjoPVb8Zrmh/IMbDYQUYvyFkPPu1UWy/ B+dfnT1wfsYxyrp35XjUCslgnDBw/gmqrRmf8SMEFNctM5lRWCUetrD9AKizo3b7KFNS 1c6w== 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=1q3Nr3s1tG92jsFCGg70+hkh/BKyzFIsBGyp3rjROks=; b=HzFR1qSQMltQFxp7fqJDhdQeoSyznl3h4OUFKnEuoFE/uMAQ7piSaaF4IHQpibwgd9 63RdXt55YthitoSg599xk+QkQD866Maydi+av6kyYSsEg50Gqi0B/odiGWJuPOTyxXgz 1XeC6SCZfJmSgGikJmtqUNCVZ59l+/3d5KjLQCfl0zbDtNBlcj4E+mlcz/6zF7r43W32 VWlgXUH/bQvZWnEwnOBfv8Czw7IqjQqsgoqXjnsU1crlSzP/nQRfeoPsu+2M7meT+IhJ f0E488gyw2e3zGLL0uB3sy4C+BehzYHPlNkdKpV+jvTtwyHm+NmOHMYSaF7snKlLwxFO p7aQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=P1YRs7C1; 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 y16-20020a17090322d000b0017c0d822437si29276768plg.82.2022.10.22.00.52.02; Sat, 22 Oct 2022 00:52: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=P1YRs7C1; 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 S231954AbiJVHvk (ORCPT + 99 others); Sat, 22 Oct 2022 03:51:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51316 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231800AbiJVHtK (ORCPT ); Sat, 22 Oct 2022 03: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 4B869263B6E; Sat, 22 Oct 2022 00:46:01 -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 076A1B82E0A; Sat, 22 Oct 2022 07:43:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5C927C433D6; Sat, 22 Oct 2022 07:43:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666424635; bh=tT3anD75WlhdKhqutw+l0nDcDEE3SDZgAaFmH8y3fag=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=P1YRs7C1/pXV7rzP35otxYKrd7Yh2GKOz8rhFyJ3ikJwKzfYtkZQLUdRQymRqLYRG j0utn4lc2ziQdiVJGJV0/MSROQPKuhfunQkfgh1FDjqnmONctTMjkSYgcM7qrrEO4J lefCFDMKIqofZSrW24cpMjI0c8CuD8oKvP8gM5NA= 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 5.19 219/717] bpf: Propagate error from htab_lock_bucket() to userspace Date: Sat, 22 Oct 2022 09:21:38 +0200 Message-Id: <20221022072453.934425338@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?1747373402934440991?= X-GMAIL-MSGID: =?utf-8?q?1747373402934440991?= 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 717f85973443..e20f3d0e3fc7 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;