Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp3317850rwa; Tue, 23 Aug 2022 02:42:40 -0700 (PDT) X-Google-Smtp-Source: AA6agR7lSPsNKJj9rqPZZoaOVfMT6vENK9FjIgqsSVf0p5I5NOxQZUKK7sNCR3GbNJYGEnWCUhZQ X-Received: by 2002:a63:6344:0:b0:42a:498d:2b1e with SMTP id x65-20020a636344000000b0042a498d2b1emr15563633pgb.618.1661247760487; Tue, 23 Aug 2022 02:42:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661247760; cv=none; d=google.com; s=arc-20160816; b=MlMdKCyBYsIskN14QgWu1eA8zhiTSZuPKlOWWNBhTPtXLo+bbeBuKLMiE/1xhPFLUd yyQ3ndFd3go7iJ/ujMaWluvyaLfMoK5y2X31yBuYe7xD/+2hIPFaaa5qvAyLj/PxNKnS Us2fdpe66ns80ksso2V5RGV93gHkrOQNGsUeLFIBn7xBOuTPNT5XWxLz1CzF/ym3CEdG 8yM0n2VgU0AoTX3UCl9n5aCrrIdFwV7fTcotraagdJOqE3kst8z8BfAi6C52l2AtGe/f TJ/TNjiMoWhzxj6SL+01Rf3XJOcy28R4HHhgoGcKFWRPaP3NKQE3WZ8nTSIXd4xIwXLE Vr4g== 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=hfR1zP/yTUiKXZwFSXXncR2tP89KQ2F+wHELOzAjTSs=; b=Bt+s0wr8runlvNMYh1f2hpMHCFCXcgyc+VHxnTG+M9zvJd1DRPbYTS3pJONt17h2nw K+4irmRZpglm+2UQbtyU1MKcdfKzDSMIYykfniMbmKghrwommiPshkLr/YfulYnzwytC NELgvmydAcQhY9ZAY2tY2Y58pdwUZxsMRsikyY0jTzhFsykaInk/RcLlQQOgxcab+E7F 8OqE5EGKCQx3ZRKPoYFR+PElpHe8k1K9Tr78Kqd5e2IznAf/zIk2RWMSRWwplyqcVIS5 +AfZLOaHg4f1jagqfCcDiqBup55Q961e6lHlRkGQaPmonxl2BUO5bHmRLU2bJuHbMoGK vbAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=RGkTgze5; 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 u7-20020a170902e5c700b0016d70bd3fd6si17117413plf.77.2022.08.23.02.42.29; Tue, 23 Aug 2022 02:42:40 -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=RGkTgze5; 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 S243059AbiHWIYh (ORCPT + 99 others); Tue, 23 Aug 2022 04:24:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59432 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243120AbiHWIUb (ORCPT ); Tue, 23 Aug 2022 04:20:31 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 582A26DFBC; Tue, 23 Aug 2022 01:12:04 -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 A12CBB81C26; Tue, 23 Aug 2022 08:12:02 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E9220C433C1; Tue, 23 Aug 2022 08:12:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661242321; bh=fntl2AQxwEntjTVQ0NGLc7DF/QeTA1ol4XdWPwNNzW0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RGkTgze5jBRPKWl4kfqzYEXaB6l4h9JCDR4BuZKOx5xwhGGDZCBj164MwYDYfmLFe MqNvQ5uGgMpMWLNv9EL55zL103L18OVvnFQ24ZUvscW2u/9NlZvXNzu2oCUuRqgdRw ySuGa4Myiav5KM7h3CA24CRLoD84MbGZgNz+4IMo= 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.19 067/365] bpf: Acquire map uref in .init_seq_private for sock{map,hash} iterator Date: Tue, 23 Aug 2022 09:59:28 +0200 Message-Id: <20220823080121.015734563@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220823080118.128342613@linuxfoundation.org> References: <20220823080118.128342613@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 @@ -783,13 +783,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), }; @@ -1369,18 +1378,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), };