Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2210908iof; Tue, 7 Jun 2022 23:19:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzkvN3JLpmIak55ABcD/L5AlaJXbvZLqbkENpvpC1FOlBa7PfurChtsXuewrtcnxhGbXFyN X-Received: by 2002:a17:90b:4d05:b0:1e0:b53:f4a3 with SMTP id mw5-20020a17090b4d0500b001e00b53f4a3mr59821174pjb.3.1654669163066; Tue, 07 Jun 2022 23:19:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654669163; cv=none; d=google.com; s=arc-20160816; b=0CKzcrV3QJCI2jSoAWvj1OP6pJxtwDUVXOz9fMktIH+Imt3+jZE4a/SH3Sn7yoUbqS xPm+8WLK7IGs8y05xAFw4B1lC/5VLn9rkPQUjzNknD3pVqKvnB6ko5Cr/OaarrC32mOt e/jmQ5tPo+IVVpH+rvV/NUgvTbBnI/XZKdcX7vPqWPpdFCuav+yt7yff/rPPAFDp1ure ipG2r6TBzKyvJI4W4o6fdO/5ONgdV+u1mRV0d9LVH8x66IPFrORnQYtitzklVqgxLwx+ fvWs2+ghq+V31UCHBHZEyhFYSZRbWAGJPwdBeWb8mnF263M9Gz9objmO8Vk+qRHF1u9o 75BQ== 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=P7FzmxdK+Er67k/TgryM6LiTcKHcXBXoIEUQzVJ6V80=; b=RV+oeFAnx4AKAH9Y5AqhmQoa/erbF3tMDBowPkhbXSITQRmMw4F4hknApAJigbd0Bq +NDZg9NBap2jpV1lr4uAQIL6QapADb1HCR2JPySnfYZ+8qocI4QGf6a6xkxW1rWeRdfe tVn1lfHENBTO7eiU+PjFgYl4XpStMG8PJxCls8+XVJDS91WrNhESfGfp9Yw4HHolTm/9 tXwEpC6n6rpkNZAXAoeatBx0acl4RXmSL6wjubCmcnPkZz46drRpVGRo91+Gc+Bph2Dm m1uHjSOhLxOVg4UgCF0PmU2L8iZNAcVLnzBWRiJkmykdnFbaS31IiAuOJjqgIJWYfNAW B54w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=08b5ShS7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id j1-20020a170902c3c100b00163bad6ec7bsi25954543plj.613.2022.06.07.23.19.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 23:19:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=08b5ShS7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6D6BB1F8995; Tue, 7 Jun 2022 22:40:58 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1442551AbiFHCAm (ORCPT + 99 others); Tue, 7 Jun 2022 22:00:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1837548AbiFHAAK (ORCPT ); Tue, 7 Jun 2022 20:00:10 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 38B5827CCCE; Tue, 7 Jun 2022 12:26: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 dfw.source.kernel.org (Postfix) with ESMTPS id C4A84609D0; Tue, 7 Jun 2022 19:26:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B513BC385A5; Tue, 7 Jun 2022 19:25:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654629960; bh=+cCPHyDiE6bNC1DR+IBdCXwECQlqi81MVWn1zojPDGI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=08b5ShS7wl4naBnwHKIFQJwgwZ2g9j5YdYoeOH4m/ApgApT0CJKdJcHcsfTgJ/JR+ DCUX56wHsWeIv3HyN4nV+m2OmJ020jThBa2jqqsCzxWra+gXJOO2gKBU743ns4hl+G FqEvgCdb5S5UTdO6ozzxbGb+B8RCcoNlDKc1eoTY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+d0e94b65ac259c29ce7a@syzkaller.appspotmail.com, Ziyang Xuan , Paolo Abeni Subject: [PATCH 5.18 878/879] macsec: fix UAF bug for real_dev Date: Tue, 7 Jun 2022 19:06:36 +0200 Message-Id: <20220607165028.341154150@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220607165002.659942637@linuxfoundation.org> References: <20220607165002.659942637@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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: Ziyang Xuan commit 196a888ca6571deb344468e1d7138e3273206335 upstream. Create a new macsec device but not get reference to real_dev. That can not ensure that real_dev is freed after macsec. That will trigger the UAF bug for real_dev as following: ================================================================== BUG: KASAN: use-after-free in macsec_get_iflink+0x5f/0x70 drivers/net/macsec.c:3662 Call Trace: ... macsec_get_iflink+0x5f/0x70 drivers/net/macsec.c:3662 dev_get_iflink+0x73/0xe0 net/core/dev.c:637 default_operstate net/core/link_watch.c:42 [inline] rfc2863_policy+0x233/0x2d0 net/core/link_watch.c:54 linkwatch_do_dev+0x2a/0x150 net/core/link_watch.c:161 Allocated by task 22209: ... alloc_netdev_mqs+0x98/0x1100 net/core/dev.c:10549 rtnl_create_link+0x9d7/0xc00 net/core/rtnetlink.c:3235 veth_newlink+0x20e/0xa90 drivers/net/veth.c:1748 Freed by task 8: ... kfree+0xd6/0x4d0 mm/slub.c:4552 kvfree+0x42/0x50 mm/util.c:615 device_release+0x9f/0x240 drivers/base/core.c:2229 kobject_cleanup lib/kobject.c:673 [inline] kobject_release lib/kobject.c:704 [inline] kref_put include/linux/kref.h:65 [inline] kobject_put+0x1c8/0x540 lib/kobject.c:721 netdev_run_todo+0x72e/0x10b0 net/core/dev.c:10327 After commit faab39f63c1f ("net: allow out-of-order netdev unregistration") and commit e5f80fcf869a ("ipv6: give an IPv6 dev to blackhole_netdev"), we can add dev_hold_track() in macsec_dev_init() and dev_put_track() in macsec_free_netdev() to fix the problem. Fixes: 2bce1ebed17d ("macsec: fix refcnt leak in module exit routine") Reported-by: syzbot+d0e94b65ac259c29ce7a@syzkaller.appspotmail.com Signed-off-by: Ziyang Xuan Link: https://lore.kernel.org/r/20220531074500.1272846-1-william.xuanziyang@huawei.com Signed-off-by: Paolo Abeni Signed-off-by: Greg Kroah-Hartman --- drivers/net/macsec.c | 7 +++++++ 1 file changed, 7 insertions(+) --- a/drivers/net/macsec.c +++ b/drivers/net/macsec.c @@ -99,6 +99,7 @@ struct pcpu_secy_stats { * struct macsec_dev - private data * @secy: SecY config * @real_dev: pointer to underlying netdevice + * @dev_tracker: refcount tracker for @real_dev reference * @stats: MACsec device stats * @secys: linked list of SecY's on the underlying device * @gro_cells: pointer to the Generic Receive Offload cell @@ -107,6 +108,7 @@ struct pcpu_secy_stats { struct macsec_dev { struct macsec_secy secy; struct net_device *real_dev; + netdevice_tracker dev_tracker; struct pcpu_secy_stats __percpu *stats; struct list_head secys; struct gro_cells gro_cells; @@ -3459,6 +3461,9 @@ static int macsec_dev_init(struct net_de if (is_zero_ether_addr(dev->broadcast)) memcpy(dev->broadcast, real_dev->broadcast, dev->addr_len); + /* Get macsec's reference to real_dev */ + dev_hold_track(real_dev, &macsec->dev_tracker, GFP_KERNEL); + return 0; } @@ -3704,6 +3709,8 @@ static void macsec_free_netdev(struct ne free_percpu(macsec->stats); free_percpu(macsec->secy.tx_sc.stats); + /* Get rid of the macsec's reference to real_dev */ + dev_put_track(macsec->real_dev, &macsec->dev_tracker); } static void macsec_setup(struct net_device *dev)