From patchwork Sat Nov 19 07:56:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shigeru Yoshida X-Patchwork-Id: 23230 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp607329wrr; Sat, 19 Nov 2022 00:10:21 -0800 (PST) X-Google-Smtp-Source: AA0mqf5tjr7QUGvHbN8wV/2GSN7+BSVmvT5a3U4UN2KBy/B2n1zT5QR2xbFIkl9S5bSsOzjJndry X-Received: by 2002:a17:902:868b:b0:186:8c19:d472 with SMTP id g11-20020a170902868b00b001868c19d472mr3087316plo.12.1668845421314; Sat, 19 Nov 2022 00:10:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668845421; cv=none; d=google.com; s=arc-20160816; b=HosJ8fruNY81tQsqGCzKAcuS6LzqHRJejtZ0hpYogG5CMUmudOw7HUpKOmBzpn08+D Y5S7AJ42tnBfCERaQUvw8ZGe83pGQreUj3cwAXu8I5YybVwQKM4kXkFltrVd4MW4ZUgW GDl2au1dyTgzQaKPPfvKWFftVQohpwJ1aw9qCCdwSiYRqmrpzvOF0r+jmPdT1COgANhh qcHdoFRh4H8Voe/z5Iu2bOpXGc8ePckozt/wrkvMLFtrxbtSkuUPraDfRN+bYYHkmHiQ 5gIjaVZWd51llvcrTrN87TgVRQlovccFotucAsM2uaz9H+UpV4rmmui2pHJUTTLJOjnH 50kA== 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=oop6ZAvV1b5arBaLgtXt5J9GRGtip/WQKXGjVoxJr6g=; b=MUfneb1OQs0niDPksnzNV6qcaVfWyhKiD01hKkJsC2aRYunLrFRprE7BVO8ZyDcF7w fiY1rrnyyoI5TQ6nGnhwmIdco+wNxUmLP/BaFFpYoKuZAZlx27ljIn3mPJXk7kiHVYKn EcWaS/afHAKd7PuV2kqDAzwd6Qp380cDjNImz/XUO2UTU2kMz1+zX+DLnR1jTEJwJAx+ rPvXKENW3y1J7VV1qgwircKUiVYqIJ/p2zdfeeYCz3Gl39yp5p0vD5T1AfgesD4D0qTZ 9ng+6AnPU3ZzBwK0FvK14iql/w70SVjA2AhMINbd8PLdsc0FDJjznPxSZIlPDT7SjIpo b7yw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ONKlmeY8; 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 q18-20020a170902f35200b00186b766d6cbsi5016584ple.414.2022.11.19.00.10.07; Sat, 19 Nov 2022 00:10:21 -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=ONKlmeY8; 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 S231548AbiKSH5V (ORCPT + 99 others); Sat, 19 Nov 2022 02:57:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53432 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229515AbiKSH5T (ORCPT ); Sat, 19 Nov 2022 02:57:19 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 533DA4F18C for ; Fri, 18 Nov 2022 23:56:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1668844584; 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=oop6ZAvV1b5arBaLgtXt5J9GRGtip/WQKXGjVoxJr6g=; b=ONKlmeY8hrrsr38eSndupvX6WLNiX4GUJInGGdPzFLlSUNBtiyv1FsH6pIkKx7/VhS0JjB j5ku1DWfZLs019ENV42BY+ozA1KrlUinM833Xp4yt5NdBJvV4peMC9ET11+9nDRfYsYqlQ jKBsI6Nmt37FxSFz3PWZo4P7CYQtod0= Received: from mail-pg1-f199.google.com (mail-pg1-f199.google.com [209.85.215.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-39-aq0uwJOyPDGhTUOJljfgAQ-1; Sat, 19 Nov 2022 02:56:22 -0500 X-MC-Unique: aq0uwJOyPDGhTUOJljfgAQ-1 Received: by mail-pg1-f199.google.com with SMTP id j21-20020a63fc15000000b00476d6932baeso4302742pgi.23 for ; Fri, 18 Nov 2022 23:56:22 -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=oop6ZAvV1b5arBaLgtXt5J9GRGtip/WQKXGjVoxJr6g=; b=M4alzXH3KyR7OMfw+/lb+g0cViA+fHJ6qC9xFO+7tfprTzahxiCW8dGm/PEWKLxS6n ZTqbbuOFeSLAXkZucmx6Kjm5+H7Nr9oRUxLy/Nm94f5nzeHxZWnicPPGV/JHvEPOAL1t ZWnWLI76xR1xUA0dWK2TkIWt/C/Mu57eFwY8/VQ5bCxEYBGK/tuujUQLe1AxY1zW5J+V UVM+j3zjO5zYA8bQBg/WfpM6/Tl/VZm7qOzMrMbC2APORA2J93xpFgfRgs0WRoh0LbIc CF2LuQpx6tLwkxOzOdBWp/YYbCU2N6yKIMVsIKlxH0UFMT2Rwa6AyGAYHXOZMi1h8Wd1 1TLw== X-Gm-Message-State: ANoB5pmp1FrROM+5AlmH14GG3J9fITENuRrz7/yrQsEPz0yX8NEo82X5 GKxRN+USHTzRDmPxdoC1B+QjgGFvhPhoGg2ERbiR4Lzw9yEmf42ywyG65hlnYMlGMao0ftXxgxa 898UGmFUJ0HkFnffB78JiGsim X-Received: by 2002:a17:902:bc4b:b0:188:5340:4a38 with SMTP id t11-20020a170902bc4b00b0018853404a38mr2998913plz.34.1668844581893; Fri, 18 Nov 2022 23:56:21 -0800 (PST) X-Received: by 2002:a17:902:bc4b:b0:188:5340:4a38 with SMTP id t11-20020a170902bc4b00b0018853404a38mr2998894plz.34.1668844581641; Fri, 18 Nov 2022 23:56:21 -0800 (PST) Received: from ryzen.. ([240d:1a:c0d:9f00:fc9c:8ee9:e32c:2d9]) by smtp.gmail.com with ESMTPSA id qe1-20020a17090b4f8100b00210039560c0sm6636821pjb.49.2022.11.18.23.56.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Nov 2022 23:56:21 -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, syzkaller-bugs@googlegroups.com, Shigeru Yoshida Subject: [PATCH] net: tun: Fix use-after-free in tun_detach() Date: Sat, 19 Nov 2022 16:56:15 +0900 Message-Id: <20221119075615.723290-1-syoshida@redhat.com> X-Mailer: git-send-email 2.38.1 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?1749911256687682614?= X-GMAIL-MSGID: =?utf-8?q?1749911256687682614?= syzbot reported use-after-free in tun_detach() [1]. This causes call trace like below: ================================================================== BUG: KASAN: use-after-free in notifier_call_chain+0x1da/0x1e0 ... Call Trace: dump_stack_lvl+0x100/0x178 print_report+0x167/0x470 ? __virt_addr_valid+0x5e/0x2d0 ? __phys_addr+0xc6/0x140 ? notifier_call_chain+0x1da/0x1e0 ? notifier_call_chain+0x1da/0x1e0 kasan_report+0xbf/0x1e0 ? notifier_call_chain+0x1da/0x1e0 notifier_call_chain+0x1da/0x1e0 call_netdevice_notifiers_info+0x83/0x130 netdev_run_todo+0xc33/0x11b0 ? generic_xdp_install+0x490/0x490 ? __tun_detach+0x1500/0x1500 tun_chr_close+0xe2/0x190 __fput+0x26a/0xa40 task_work_run+0x14d/0x240 ? task_work_cancel+0x30/0x30 do_exit+0xb31/0x2a40 ? reacquire_held_locks+0x4a0/0x4a0 ? do_raw_spin_lock+0x12e/0x2b0 ? mm_update_next_owner+0x7c0/0x7c0 ? rwlock_bug.part.0+0x90/0x90 ? lockdep_hardirqs_on_prepare+0x17f/0x410 do_group_exit+0xd4/0x2a0 __x64_sys_exit_group+0x3e/0x50 do_syscall_64+0x38/0xb0 entry_SYSCALL_64_after_hwframe+0x63/0xcd The cause of the issue is that sock_put() from __tun_detach() drops last reference count for struct net, and then notifier_call_chain() from netdev_state_change() accesses that struct net. This patch fixes the issue by calling sock_put() from tun_detach() after all necessary accesses for the struct net has done. Link: https://syzkaller.appspot.com/bug?id=96eb7f1ce75ef933697f24eeab928c4a716edefe [1] Signed-off-by: Shigeru Yoshida --- drivers/net/tun.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/drivers/net/tun.c b/drivers/net/tun.c index 7a3ab3427369..ce9fcf4c8ef4 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -686,7 +686,6 @@ static void __tun_detach(struct tun_file *tfile, bool clean) if (tun) xdp_rxq_info_unreg(&tfile->xdp_rxq); ptr_ring_cleanup(&tfile->tx_ring, tun_ptr_free); - sock_put(&tfile->sk); } } @@ -702,6 +701,11 @@ static void tun_detach(struct tun_file *tfile, bool clean) if (dev) netdev_state_change(dev); rtnl_unlock(); + + if (clean) { + synchronize_rcu(); + sock_put(&tfile->sk); + } } static void tun_detach_all(struct net_device *dev)