Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp3329761rwa; Tue, 23 Aug 2022 02:59:52 -0700 (PDT) X-Google-Smtp-Source: AA6agR4Ak/UyYadd4UYtbyE/7VtMbZwrOPef9HWpwXUb1Xl3JKPjz4xemk/lPq69jSRrO5KNxCXl X-Received: by 2002:a17:906:58d1:b0:73d:6329:5e14 with SMTP id e17-20020a17090658d100b0073d63295e14mr10543789ejs.121.1661248791887; Tue, 23 Aug 2022 02:59:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661248791; cv=none; d=google.com; s=arc-20160816; b=rB0VFnDboG0VNuG/U0lXaeoNTQj21U3z6EscYKATSupRUVvoAB2yCjTge94pxB62uc CYx9/YN87603q+/KXETXNIq4PGqHwccc11GGp1U72/Ke/pC/7jpEFA34ozLmKbaF4bKF S7aQCIeO8KYlQerG76dOSf8Q6KsQdwnERJnvzKWy2nDp6qVUyQ5qxgnwvb/nAT1SJoXy il66MkwGDp2aAymTOr1Fm6BFBv7/HAQZ6/KV6b2zp/0WO93d+8ugEhRDqiCvVa+NNb9K jFkFP2l+8Qc9/+Dh0KtICc1W6QzfkV6/8PV3lBFN/uPd9tM7OScwP1SevWLLorH8PR75 B6lA== 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=tHOj+CUk+JnoHAqcJgRAxGGFEFhY7x2ETMNwS3PG3XM=; b=I5HlDM0qN1vD57IYhUmSEFclX0DLuNC2l8uHK01G4an1iYC9mzL6VOhBVK11rrNPEp yWP02Kr34JHWjdjtFL7nEMH82jsfPqa3Z1Iouj6NT4ONuvlHiwEsCo0nAnZEjPRNL+bs UOFsdlokxn83g5WBuZ/xFqX9P9HMNtxOB2KL2t133lM1ZGeSEwohRPChTlao15jWYsI7 +LxhC/sCfvGrq2mPtwKtqukRSfupj3rFgDs5BhlLGJFu/5c5ALIX3TiSY6Y0ulcnyeOR avCMb64NNi8YgNe2+JQcZ0lRcXvbm/+lOshRN1gN5Bs0aHODz29DkjN7WfeUuRmXok+H YImQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=bXUBK7mZ; 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 u11-20020a50950b000000b00441e5fddd4asi1741900eda.413.2022.08.23.02.59.25; Tue, 23 Aug 2022 02:59:51 -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=bXUBK7mZ; 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 S240792AbiHWJrp (ORCPT + 99 others); Tue, 23 Aug 2022 05:47:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41686 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351388AbiHWJpZ (ORCPT ); Tue, 23 Aug 2022 05:45:25 -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 9C0509AFC4; Tue, 23 Aug 2022 01:43:02 -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 CCEFA614E9; Tue, 23 Aug 2022 08:42:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D45DEC433D7; Tue, 23 Aug 2022 08:42:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661244174; bh=dcknOVvdekOuukoPTOE+yv5c06pASb4XC+al3idwRmo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bXUBK7mZtaPdR0X52dX83joQF8QXQ3dvFefWikjV6EFhOk0nyhaAkeV5hXiuUB2Zm W8+WcMkyoSNEEH9UPziFvoA+3TYDXFT0wedvtMM5qCC30YCLlHKZht34jNNAB6nEmG se5bJIao+oxILvgnhw/1QhlOdnLDX29MhV20RSSc= 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 047/244] bpf: Acquire map uref in .init_seq_private for sock{map,hash} iterator Date: Tue, 23 Aug 2022 10:23:26 +0200 Message-Id: <20220823080100.632196561@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 f0d2b2716d71778d0b0c8eaa433c073287d69d93 upstream. sock_map_iter_attach_target() 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 sock_map_iter_detach_target() as part of bpf_link_release(), or could be released in bpf_map_put_with_uref() as part of bpf_map_release(). Fixing it by acquiring an extra map uref in .init_seq_private and releasing it in .fini_seq_private. Fixes: 0365351524d7 ("net: Allow iterating sockmap and sockhash") Signed-off-by: Hou Tao Acked-by: Yonghong Song Link: https://lore.kernel.org/r/20220810080538.1845898-5-houtao@huaweicloud.com Signed-off-by: Alexei Starovoitov Signed-off-by: Greg Kroah-Hartman --- net/core/sock_map.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) --- a/net/core/sock_map.c +++ b/net/core/sock_map.c @@ -789,13 +789,22 @@ static int sock_map_init_seq_private(voi { struct sock_map_seq_info *info = priv_data; + bpf_map_inc_with_uref(aux->map); info->map = aux->map; return 0; } +static void sock_map_fini_seq_private(void *priv_data) +{ + struct sock_map_seq_info *info = priv_data; + + bpf_map_put_with_uref(info->map); +} + static const struct bpf_iter_seq_info sock_map_iter_seq_info = { .seq_ops = &sock_map_seq_ops, .init_seq_private = sock_map_init_seq_private, + .fini_seq_private = sock_map_fini_seq_private, .seq_priv_size = sizeof(struct sock_map_seq_info), }; @@ -1376,18 +1385,27 @@ static const struct seq_operations sock_ }; static int sock_hash_init_seq_private(void *priv_data, - struct bpf_iter_aux_info *aux) + struct bpf_iter_aux_info *aux) { struct sock_hash_seq_info *info = priv_data; + bpf_map_inc_with_uref(aux->map); info->map = aux->map; info->htab = container_of(aux->map, struct bpf_shtab, map); return 0; } +static void sock_hash_fini_seq_private(void *priv_data) +{ + struct sock_hash_seq_info *info = priv_data; + + bpf_map_put_with_uref(info->map); +} + static const struct bpf_iter_seq_info sock_hash_iter_seq_info = { .seq_ops = &sock_hash_seq_ops, .init_seq_private = sock_hash_init_seq_private, + .fini_seq_private = sock_hash_fini_seq_private, .seq_priv_size = sizeof(struct sock_hash_seq_info), };