From patchwork Sat Feb 25 18:28:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shigeru Yoshida X-Patchwork-Id: 61542 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp1629229wrd; Sat, 25 Feb 2023 11:20:46 -0800 (PST) X-Google-Smtp-Source: AK7set8aZJT+hstdYlOrlWSbsb1lLJxcej1jkdUKJKwvFn2Ki8Z9I9Af3REaQsblhskXZj7bAsCn X-Received: by 2002:a17:902:cecf:b0:198:adc4:229d with SMTP id d15-20020a170902cecf00b00198adc4229dmr4013714plg.24.1677352846495; Sat, 25 Feb 2023 11:20:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677352846; cv=none; d=google.com; s=arc-20160816; b=a/Xls/w6z4BMDfT7Gij3N8TK41OHPuVN6M0F+YVD+FlGYnW1icquXjAq5lU3jTvmwi p/VDopLRAZ9TYNwc2wBoHe07hBrnECREcAk54nb/utD15481vPurkQ8MQ7J70fAiMCSu 2g4AwgfDP8hqQKsZCho6f6Di8CnJC16icjQHSsNV2UKxCWAPUtKgOoMf32Qh0yJ3Vs1X 1nVUEia1S9CQ4K6dPWY7x4SCyN7pnlnbP+PzQ430EQMJUKpucSlsOhPf5jUqEMTt66b9 Ug2sPv8gu0NxXIr2+G5Sa9k4LzQxhRpeSXHn0Q1OqaijJ8zxYxU9SrQxzSNTd1xZ6Yal InUg== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=Zv/xQL8kguH2DCyuojYf/vRV+UjJ6qJW7G0m4Ktfp5I=; b=qpDjECshR2U/+ZlKsEv0bjAciZEqNhEsignFL3MGcEKxS72SUJIs8RevkAZCkGKXu7 Zt8bBDkNuuZrUAyb3/RGFjK/gqVtl/TpHM3HEAzjnPZ7PmItJlMky00mz1ZaJFMeb967 92tP9N8LMmVPC0aIUgbkrBMaoIEvfAT97TP89m7ua4BLvkIGoFALo+v6rICrKIR61ze+ 0+GxoZkI3vPSLq/0IqbmScvCamgxT3tUMFxnJandraaYYOIG3V9ZvUiInvSImiBeILvz 0ymsMOm/Klg4NSUNr0xUthjQynt0qZ0AmkIp4AJ2B9KqgKyMEN3ypScxVghHZc3IwaSw 4cMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=S2Zr4YbA; 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=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c20-20020a170902c1d400b0019a7ef5e9acsi2362392plc.305.2023.02.25.11.20.34; Sat, 25 Feb 2023 11:20:46 -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; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=S2Zr4YbA; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229610AbjBYS3Y (ORCPT + 99 others); Sat, 25 Feb 2023 13:29:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229569AbjBYS3X (ORCPT ); Sat, 25 Feb 2023 13:29:23 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C463E058 for ; Sat, 25 Feb 2023 10:28:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677349716; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Zv/xQL8kguH2DCyuojYf/vRV+UjJ6qJW7G0m4Ktfp5I=; b=S2Zr4YbA6RSEwvFIkSmr3uRzqDdPLuDRZc57RlHxyIzTnAuZOX3tNeJFYaiRPkPFepXWNC KaDHyaEiRwNSTjwydZjhsCjGa0H2U06TTndtqBeUOlAK7oRmpnjmuXd9NHKZGj9RzkdCyA 2wLXHIXw7/0tUWSg6/pKSneenCHDbUQ= Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-28-GlUOS5pNOHGJqAC_CZHgDA-1; Sat, 25 Feb 2023 13:28:35 -0500 X-MC-Unique: GlUOS5pNOHGJqAC_CZHgDA-1 Received: by mail-pl1-f200.google.com with SMTP id z1-20020a170902d54100b00198bc9ba4edso1348941plf.21 for ; Sat, 25 Feb 2023 10:28:34 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Zv/xQL8kguH2DCyuojYf/vRV+UjJ6qJW7G0m4Ktfp5I=; b=P6r5Uu8vjNEVmsVDzJNtlZA9gSJuEWefikIHR7PflQmuFn8BuSl8DAbd9DJWLAMK31 PhtIExaJiaFOWJ1JnHXqBdWxm73bxU/XzV6Pa60GGLtnFa6l0F4wZlhCxn7zb5n2CwyK awrqoYxf9azWiNrqQThOELmzITgEUHxbyr8ijY2BpSpIXodKlI33JzDb7+io3p7dCxbr uNCGnW4Qo0/StKcTV7qfoIRXoaQYSI7coyf4Kabx1vss1cm04z/j6I94MZRTKtmfwPPN 8/DGVZjMU2W39T89zD+tiI4fDl4/6MUM9MTXFg6hhJS5wekwJvOTEX2aGw9zDTgW3Cge jS+A== X-Gm-Message-State: AO0yUKXLfXx3ChLq4VKpzuj94SpBPEpQ/vP6QPM0HR7sYW61gyAV6ai0 NFkP3bze6sO25z9odIYdKVs7F32hW8Z4NyXkaLvoNTHm7n4O7UOGUTB+bn/ZARpAfxFCz6/uOS2 zcWvG1PVmccI2M2Sb1YmI/yd7 X-Received: by 2002:a17:902:f64b:b0:19a:b588:6fe2 with SMTP id m11-20020a170902f64b00b0019ab5886fe2mr3116031plg.13.1677349714051; Sat, 25 Feb 2023 10:28:34 -0800 (PST) X-Received: by 2002:a17:902:f64b:b0:19a:b588:6fe2 with SMTP id m11-20020a170902f64b00b0019ab5886fe2mr3116018plg.13.1677349713689; Sat, 25 Feb 2023 10:28:33 -0800 (PST) Received: from localhost.localdomain ([240d:1a:c0d:9f00:ca6:1aff:fead:cef4]) by smtp.gmail.com with ESMTPSA id a2-20020a170902ee8200b0019926c7757asm1543214pld.289.2023.02.25.10.28.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Feb 2023 10:28:33 -0800 (PST) From: Shigeru Yoshida To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Shigeru Yoshida , syzbot+b563d33852b893653a9e@syzkaller.appspotmail.com Subject: [PATCH net] net: caif: Fix use-after-free in cfusbl_device_notify() Date: Sun, 26 Feb 2023 03:28:20 +0900 Message-Id: <20230225182820.4048336-1-syoshida@redhat.com> X-Mailer: git-send-email 2.39.0 MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758831938482330423?= X-GMAIL-MSGID: =?utf-8?q?1758831938482330423?= syzbot reported use-after-free in cfusbl_device_notify() [1]. This causes a stack trace like below: BUG: KASAN: use-after-free in cfusbl_device_notify+0x7c9/0x870 net/caif/caif_usb.c:138 Read of size 8 at addr ffff88807ac4e6f0 by task kworker/u4:6/1214 CPU: 0 PID: 1214 Comm: kworker/u4:6 Not tainted 5.19.0-rc3-syzkaller-00146-g92f20ff72066 #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Workqueue: netns cleanup_net Call Trace: __dump_stack lib/dump_stack.c:88 [inline] dump_stack_lvl+0xcd/0x134 lib/dump_stack.c:106 print_address_description.constprop.0.cold+0xeb/0x467 mm/kasan/report.c:313 print_report mm/kasan/report.c:429 [inline] kasan_report.cold+0xf4/0x1c6 mm/kasan/report.c:491 cfusbl_device_notify+0x7c9/0x870 net/caif/caif_usb.c:138 notifier_call_chain+0xb5/0x200 kernel/notifier.c:87 call_netdevice_notifiers_info+0xb5/0x130 net/core/dev.c:1945 call_netdevice_notifiers_extack net/core/dev.c:1983 [inline] call_netdevice_notifiers net/core/dev.c:1997 [inline] netdev_wait_allrefs_any net/core/dev.c:10227 [inline] netdev_run_todo+0xbc0/0x10f0 net/core/dev.c:10341 default_device_exit_batch+0x44e/0x590 net/core/dev.c:11334 ops_exit_list+0x125/0x170 net/core/net_namespace.c:167 cleanup_net+0x4ea/0xb00 net/core/net_namespace.c:594 process_one_work+0x996/0x1610 kernel/workqueue.c:2289 worker_thread+0x665/0x1080 kernel/workqueue.c:2436 kthread+0x2e9/0x3a0 kernel/kthread.c:376 ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:302 When unregistering a net device, unregister_netdevice_many_notify() sets the device's reg_state to NETREG_UNREGISTERING, calls notifiers with NETDEV_UNREGISTER, and adds the device to the todo list. Later on, devices in the todo list are processed by netdev_run_todo(). netdev_run_todo() waits devices' reference count become 1 while rebdoadcasting NETDEV_UNREGISTER notification. When cfusbl_device_notify() is called with NETDEV_UNREGISTER multiple times, the parent device might be freed. This could cause UAF. Processing NETDEV_UNREGISTER multiple times also causes inbalance of reference count for the module. This patch fixes the issue by accepting only first NETDEV_UNREGISTER notification. Link: https://syzkaller.appspot.com/bug?id=c3bfd8e2450adab3bffe4d80821fbbced600407f [1] Reported-by: syzbot+b563d33852b893653a9e@syzkaller.appspotmail.com Signed-off-by: Shigeru Yoshida --- net/caif/caif_usb.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/net/caif/caif_usb.c b/net/caif/caif_usb.c index ebc202ffdd8d..bf61ea4b8132 100644 --- a/net/caif/caif_usb.c +++ b/net/caif/caif_usb.c @@ -134,6 +134,9 @@ static int cfusbl_device_notify(struct notifier_block *me, unsigned long what, struct usb_device *usbdev; int res; + if (what == NETDEV_UNREGISTER && dev->reg_state >= NETREG_UNREGISTERED) + return 0; + /* Check whether we have a NCM device, and find its VID/PID. */ if (!(dev->dev.parent && dev->dev.parent->driver && strcmp(dev->dev.parent->driver->name, "cdc_ncm") == 0))