Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp294972pxp; Fri, 11 Mar 2022 04:38:48 -0800 (PST) X-Google-Smtp-Source: ABdhPJxdBV+ptpRtC6ICjwhGf2Yhy3MShegFV+/zSGbYoHB9RXyAvRLEMQk2MV1n+Kh8fKsh2Gh3 X-Received: by 2002:a17:906:888e:b0:6da:ed04:5e40 with SMTP id ak14-20020a170906888e00b006daed045e40mr8642446ejc.286.1647002328569; Fri, 11 Mar 2022 04:38:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1647002328; cv=none; d=google.com; s=arc-20160816; b=Bp6QwD1Efm13GORERgXms+rQsVAEjlDZpRhNYTlZdZSTNl+Mydlu1HunJ7UcG5GE7y YX3oGMffvk65gdqggQW+4kGIIZvIAA8Bc/8i61uoy9mty4j604YfF590VqLpeNtveMeA CfA/1BO4LXu1cE/hEFLtsXw91m/qaAjeWuD22S9ppv8SgIl394qlaSqyI3ZOTHqrNEaG mJXk8DFgOpiHYH3YR4kpH2gr1SudE+DY6vhwXGPDD+/WMVNd9CJDXvHxNQ2g3IJB06FC cDB71vo7XC3lFlaizazjWByKLFQUE5yNcZwZmYL2rsIin5J35VgXfAUcHHg15GnvObnX /khg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=x/rhbYKnuOzaJal/qAuDN+b8HvJQFd1gxSoDHNsTX2M=; b=Sqs+MVOnLMbQB5tEqGR3i3KgsOWqnYVJmYy7GP06T3ayi4V0pYsAl/ndWaaDTGEMlW AEcMVlG4EXPqK9Yy9kq3FU6i2Dl0Tvu7RD5rrIT78UXmq8Yf5J1/MzGBEyRydGcNwHzv 2/kefr6vGqRYHU3Lg4jkwb4xWgjYYN0Tx5z+OhLp5yRFangkQsPXP9ciE7qxeENwT9+e qvR73nuSQ1yq8RP70dS0HkhNaOba8j8MbAp+QquA7L0O2uo8X6CXhOIj/QhmSEpyqFmG yzrTEn8zr467PWT/L2v0HO7SbLqAdoxXBou/22ThQQbL3XyLvr3qrV1PhDwPutpj07eX eM+g== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ds18-20020a170907725200b006db166ef5a8si6037047ejc.872.2022.03.11.04.38.21; Fri, 11 Mar 2022 04:38:48 -0800 (PST) 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; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240086AbiCKIqp (ORCPT + 99 others); Fri, 11 Mar 2022 03:46:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51756 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233411AbiCKIqn (ORCPT ); Fri, 11 Mar 2022 03:46:43 -0500 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 713B71BA91F; Fri, 11 Mar 2022 00:45:40 -0800 (PST) Received: from canpemm500006.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4KFKC31TJRzcZyn; Fri, 11 Mar 2022 16:40:47 +0800 (CST) Received: from localhost.localdomain (10.175.104.82) by canpemm500006.china.huawei.com (7.192.105.130) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Fri, 11 Mar 2022 16:45:38 +0800 From: Ziyang Xuan To: , , CC: Subject: [PATCH net-next 1/2] net: macvlan: fix potential UAF problem for lowerdev Date: Fri, 11 Mar 2022 17:03:26 +0800 Message-ID: <5e4fa8717da38025cd353085d5733d1928b11b25.1646989143.git.william.xuanziyang@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.104.82] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To canpemm500006.china.huawei.com (7.192.105.130) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,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 Add the reference operation to lowerdev of macvlan to avoid the potential UAF problem under the following known scenario: Someone module puts the NETDEV_UNREGISTER event handler to a work, and lowerdev is accessed in the work handler. But when the work is excuted, lowerdev has been destroyed because upper macvlan did not get reference to lowerdev correctly. That likes as the scenario occurred by commit 563bcbae3ba2 ("net: vlan: fix a UAF in vlan_dev_real_dev()"). Signed-off-by: Ziyang Xuan --- drivers/net/macvlan.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/drivers/net/macvlan.c b/drivers/net/macvlan.c index 33753a2fde29..d36af413e372 100644 --- a/drivers/net/macvlan.c +++ b/drivers/net/macvlan.c @@ -889,7 +889,7 @@ static void macvlan_set_lockdep_class(struct net_device *dev) static int macvlan_init(struct net_device *dev) { struct macvlan_dev *vlan = netdev_priv(dev); - const struct net_device *lowerdev = vlan->lowerdev; + struct net_device *lowerdev = vlan->lowerdev; struct macvlan_port *port = vlan->port; dev->state = (dev->state & ~MACVLAN_STATE_MASK) | @@ -911,6 +911,9 @@ static int macvlan_init(struct net_device *dev) port->count += 1; + /* Get macvlan's reference to lowerdev */ + dev_hold(lowerdev); + return 0; } @@ -1173,6 +1176,14 @@ static const struct net_device_ops macvlan_netdev_ops = { .ndo_features_check = passthru_features_check, }; +static void macvlan_dev_free(struct net_device *dev) +{ + struct macvlan_dev *vlan = netdev_priv(dev); + + /* Get rid of the macvlan's reference to lowerdev */ + dev_put(vlan->lowerdev); +} + void macvlan_common_setup(struct net_device *dev) { ether_setup(dev); @@ -1184,6 +1195,7 @@ void macvlan_common_setup(struct net_device *dev) dev->priv_flags |= IFF_UNICAST_FLT | IFF_CHANGE_PROTO_DOWN; dev->netdev_ops = &macvlan_netdev_ops; dev->needs_free_netdev = true; + dev->priv_destructor = macvlan_dev_free; dev->header_ops = &macvlan_hard_header_ops; dev->ethtool_ops = &macvlan_ethtool_ops; } -- 2.25.1