Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp3329845rwa; Tue, 23 Aug 2022 02:59:57 -0700 (PDT) X-Google-Smtp-Source: AA6agR5d1YQ+mCQirI879D4A8LnXpHOIYX0UWSSiTp3WSwLhNlgWZzGoPXifQawVHz/yvi5IG4zP X-Received: by 2002:a17:907:2721:b0:73d:821a:4c31 with SMTP id d1-20020a170907272100b0073d821a4c31mr5863969ejl.93.1661248797123; Tue, 23 Aug 2022 02:59:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661248797; cv=none; d=google.com; s=arc-20160816; b=mGccG2P3JZ+WbuIMvsF8s03QPlFmBgz/99kkUzCdlOkXK2SAIkQzgSz7V9Un50IVMJ HqAvFBv8x8N89nSC7qB2TEIeRPeGIwDW4fK2gCZRSCiHnykXZLYDh861ig7iu9PwOBWU TkWExmZFTwgFuIIxxoQaWV2iVBUJ561QjLCmGGELujsjD/xhCh+8Hvw+F4LN4zFtFZft 6ezhryw7ZkF/F5PQ2X/DN9pLRWz/Ai0aAjhmkKk1i2M+ALV0QkxtGcrrCTsGzySZf90Q rPLGH7amggGRWPZ7hmuTkpsYGLlW1SE4we9VuP78sIprPujExiCof8o61BzcRWPFA9eh P8aQ== 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=ZGhCadLUBSWROfYdQ9cRp559wah7UC0N/97ZZJGU+9s=; b=FxTob4wZu863u6d9Uj1C/s8jLLL1rkGSv5r2x9/YSLoUXTd+9BvkfVUBP7yJ26+tM6 PgwUHCpKBXr1iYO19hhSp0kaMZP9XHNmAhn72061Gcy6QvgKdcXHphxmdYYrWjo5bqNX eOLDWDbqduzO5GcfouCpjmx6CMnf3FxseLu58MqAc+jPZaB5MMM3AY7y92tu/OWO2dBg gEmxT4VqCrkbqsRsiPX7wFVyRzW+4c0RtWyKkjLl06Iy7U95nxwPbfjolbLyppSRNyhA dFZrDq866BRVzTNgeFifXwu3kd5l9t2yAQfXlN8doA+Est/I297hdcV5AAmPttsMyXG2 r1yQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=yDb5JIx2; 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 g13-20020a056402424d00b0044665ff00fdsi1980025edb.558.2022.08.23.02.59.30; Tue, 23 Aug 2022 02:59:57 -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=yDb5JIx2; 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 S243472AbiHWJrw (ORCPT + 99 others); Tue, 23 Aug 2022 05:47:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42214 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351867AbiHWJpg (ORCPT ); Tue, 23 Aug 2022 05:45:36 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 69CFD208; Tue, 23 Aug 2022 01:43:18 -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 sin.source.kernel.org (Postfix) with ESMTPS id 0178BCE1B50; Tue, 23 Aug 2022 08:42:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0FF05C433C1; Tue, 23 Aug 2022 08:42:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661244152; bh=gRXOJdIGssNxS+kxPIDu1FjF/pzKNtVhwUjsxe9M6fk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=yDb5JIx2zqETTGlPmFQeUY6W4VMqsjo5rLSZEZc14OQXbm05YZXzLicCK8TsM3R23 cb1XSE91wySBHMzb5/p3wkRKjDxmq9Rstc96lD1Yyb/pQjeFM7LjIzAXXQUKCneiie Q/kpATAz02klIJzxIFpQIR4CO9gKLtfGdRAZd430= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Hou Tao , Yonghong Song , Alexei Starovoitov Subject: [PATCH 5.15 044/244] bpf: Acquire map uref in .init_seq_private for array map iterator Date: Tue, 23 Aug 2022 10:23:23 +0200 Message-Id: <20220823080100.536819994@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220823080059.091088642@linuxfoundation.org> References: <20220823080059.091088642@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.1 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,T_SCC_BODY_TEXT_LINE 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 commit f76fa6b338055054f80c72b29c97fb95c1becadc upstream. bpf_iter_attach_map() acquires a map uref, and the uref may be released before or in the middle of iterating map elements. For example, the uref could be released in bpf_iter_detach_map() as part of bpf_link_release(), or could be released in bpf_map_put_with_uref() as part of bpf_map_release(). Alternative fix is acquiring an extra bpf_link reference just like a pinned map iterator does, but it introduces unnecessary dependency on bpf_link instead of bpf_map. So choose another fix: acquiring an extra map uref in .init_seq_private for array map iterator. Fixes: d3cc2ab546ad ("bpf: Implement bpf iterator for array maps") Signed-off-by: Hou Tao Acked-by: Yonghong Song Link: https://lore.kernel.org/r/20220810080538.1845898-2-houtao@huaweicloud.com Signed-off-by: Alexei Starovoitov Signed-off-by: Greg Kroah-Hartman --- kernel/bpf/arraymap.c | 6 ++++++ 1 file changed, 6 insertions(+) --- a/kernel/bpf/arraymap.c +++ b/kernel/bpf/arraymap.c @@ -620,6 +620,11 @@ static int bpf_iter_init_array_map(void seq_info->percpu_value_buf = value_buf; } + /* bpf_iter_attach_map() acquires a map uref, and the uref may be + * released before or in the middle of iterating map elements, so + * acquire an extra map uref for iterator. + */ + bpf_map_inc_with_uref(map); seq_info->map = map; return 0; } @@ -628,6 +633,7 @@ static void bpf_iter_fini_array_map(void { struct bpf_iter_seq_array_map_info *seq_info = priv_data; + bpf_map_put_with_uref(seq_info->map); kfree(seq_info->percpu_value_buf); }