Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp3885235rwi; Sat, 22 Oct 2022 01:32:40 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5KkQTQQBR8TnbuvYBPST08nmqw3L0T5/q0BmNtdAOQXf1FWOpZlIkuugu9oRxSbiqbImvy X-Received: by 2002:a05:6a00:21ca:b0:569:92dc:2949 with SMTP id t10-20020a056a0021ca00b0056992dc2949mr11037637pfj.63.1666427559978; Sat, 22 Oct 2022 01:32:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666427559; cv=none; d=google.com; s=arc-20160816; b=ASrE36jNfMGhR7/HWRjEF5su7xsCyBXBLiQAL8DlCQMYejp4fwXifFjQMJqNMB0zSn kuILI+QmKp13x6R022dHwnwH6PIFJsrH7QSc0+m9zokIGgFnc4YSJriEWJaJzMzqNsJJ g/C0WSda+KUQ1rzyZNOVwnpIoBy7ydQ/tROA9j9L8lUYuywUq4l621f+sCu0brK15fLR uaCI2zTk/LLtmJy3RC1msSCgguUHF+xHOHR6d1UtciBM7eB05Ud3LGjnhIHOn0ytFFA7 Xtk02cUYBIuRZFDP0VdwTTNL0HDq981OZKgUtOb2C1Y+yIy9yGqa6Q2zN6VjvrEPVEZh 58IA== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q4-20020a056a00088400b00558991a667esi28204105pfj.359.2022.10.22.01.32.28; Sat, 22 Oct 2022 01:32:39 -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 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 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; -- 2.35.1