From patchwork Sat Jul 15 09:25:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yue Haibing X-Patchwork-Id: 120778 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp101088vqt; Sat, 15 Jul 2023 03:06:35 -0700 (PDT) X-Google-Smtp-Source: APBJJlHKnn3BGKTHky04xONWUq3QQx4Ra0Q5gSoattnoF5xA5UVA7Xxbe6RG/IRFN7v0JzPe+81h X-Received: by 2002:a17:906:4e56:b0:992:91ce:4508 with SMTP id g22-20020a1709064e5600b0099291ce4508mr6340875ejw.53.1689415594952; Sat, 15 Jul 2023 03:06:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689415594; cv=none; d=google.com; s=arc-20160816; b=xy2ziXCowcGSyQzklZJCt1HLKlHdrrDj32Ktbt5WIM0CbRaInqwoKddza5aBuXX9Zl 0cj7fdjt3lSpY5tiDeF8FWzSYYqeTvgC01739o4C1N20NYgnhZ/mu5MuvOOefWg0KZi/ /eHCnZOaPAH76h/ywqboMoK5W5QmL2fflQRNRdkOe0uVbU0YAALhc+5yuuoPGhLzdJVX QTa4+sn6rUradMdJjnOhoKpwaOkNCVUS2B5swHr8LyoecMA6GschiwUxIViEcy5O6Moj gU+dRpi+1ek/Gqe1AdhIbQGOglzl4JYdB0yTmLJz6C80IREdE0JHTT93lR1noFwllFi/ aDlg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:cc:to:from; bh=ZrdnvojnW6FMxOnQp+t2Hc4dX0F7npKsPu323CmS03I=; fh=rzeRZz6hFkApLLfzpnXTFzQbvwD2QHZjmN9GEapmiI0=; b=DjFjhJq/XbsefCkEwuEBcZTGOSnNyWV/nHVeNglSLn69KZT4RakMLFUQ7o3/9ermNy 2Lwj7g/ALtlQNzm3hJSoyu+wrJ5CY5KP2Wbnj9zHO+ZFka7wC6ZrcDXjYLacxZaN3tFn Wx1JQLgD3Y76E7UUeGsC4ikprsdtrcuTgMKTPTQUAkdkq1vIr5d2f9ycQPZGny3qXRc1 oQKeTNfyGjBpV9x5ss7Qrq6JQaPCuiIGxICwRGV0QYAVrcOCGRFaTr2hS/mBYXnR/Y6j NtyS84aDGuc1SNLGcQG2PRq2IWHxUx5z/nhPlKV3rjohN/439I/xzrf8o1dc/nl20gcD SOqw== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x11-20020a170906134b00b00982a6e33d3esi10560582ejb.1046.2023.07.15.03.06.11; Sat, 15 Jul 2023 03:06:34 -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; 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 S230255AbjGOJ01 (ORCPT + 99 others); Sat, 15 Jul 2023 05:26:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229506AbjGOJ00 (ORCPT ); Sat, 15 Jul 2023 05:26:26 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3418B1B6; Sat, 15 Jul 2023 02:26:25 -0700 (PDT) Received: from canpemm500007.china.huawei.com (unknown [172.30.72.54]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4R32vD0Xj3zNm7Q; Sat, 15 Jul 2023 17:23:04 +0800 (CST) Received: from localhost (10.174.179.215) by canpemm500007.china.huawei.com (7.192.104.62) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Sat, 15 Jul 2023 17:26:22 +0800 From: YueHaibing To: , , , , , , CC: , , , YueHaibing Subject: [PATCH] can: bcm: Fix UAF in bcm_proc_show() Date: Sat, 15 Jul 2023 17:25:43 +0800 Message-ID: <20230715092543.15548-1-yuehaibing@huawei.com> X-Mailer: git-send-email 2.10.2.windows.1 MIME-Version: 1.0 X-Originating-IP: [10.174.179.215] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To canpemm500007.china.huawei.com (7.192.104.62) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771480646905310188 X-GMAIL-MSGID: 1771480646905310188 BUG: KASAN: slab-use-after-free in bcm_proc_show+0x969/0xa80 Read of size 8 at addr ffff888155846230 by task cat/7862 CPU: 1 PID: 7862 Comm: cat Not tainted 6.5.0-rc1-00153-gc8746099c197 #230 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014 Call Trace: dump_stack_lvl+0xd5/0x150 print_report+0xc1/0x5e0 kasan_report+0xba/0xf0 bcm_proc_show+0x969/0xa80 seq_read_iter+0x4f6/0x1260 seq_read+0x165/0x210 proc_reg_read+0x227/0x300 vfs_read+0x1d5/0x8d0 ksys_read+0x11e/0x240 do_syscall_64+0x35/0xb0 entry_SYSCALL_64_after_hwframe+0x63/0xcd Allocated by task 7846: kasan_save_stack+0x1e/0x40 kasan_set_track+0x21/0x30 __kasan_kmalloc+0x9e/0xa0 bcm_sendmsg+0x264b/0x44e0 sock_sendmsg+0xda/0x180 ____sys_sendmsg+0x735/0x920 ___sys_sendmsg+0x11d/0x1b0 __sys_sendmsg+0xfa/0x1d0 do_syscall_64+0x35/0xb0 entry_SYSCALL_64_after_hwframe+0x63/0xcd Freed by task 7846: kasan_save_stack+0x1e/0x40 kasan_set_track+0x21/0x30 kasan_save_free_info+0x27/0x40 ____kasan_slab_free+0x161/0x1c0 slab_free_freelist_hook+0x119/0x220 __kmem_cache_free+0xb4/0x2e0 rcu_core+0x809/0x1bd0 bcm_op is freed before procfs entry be removed in bcm_release(), this lead to bcm_proc_show() may read the freed bcm_op. Fixes: ffd980f976e7 ("[CAN]: Add broadcast manager (bcm) protocol") Signed-off-by: YueHaibing Reviewed-by: Oliver Hartkopp Acked-by: Oliver Hartkopp --- net/can/bcm.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/net/can/bcm.c b/net/can/bcm.c index 9ba35685b043..9168114fc87f 100644 --- a/net/can/bcm.c +++ b/net/can/bcm.c @@ -1526,6 +1526,12 @@ static int bcm_release(struct socket *sock) lock_sock(sk); +#if IS_ENABLED(CONFIG_PROC_FS) + /* remove procfs entry */ + if (net->can.bcmproc_dir && bo->bcm_proc_read) + remove_proc_entry(bo->procname, net->can.bcmproc_dir); +#endif /* CONFIG_PROC_FS */ + list_for_each_entry_safe(op, next, &bo->tx_ops, list) bcm_remove_op(op); @@ -1561,12 +1567,6 @@ static int bcm_release(struct socket *sock) list_for_each_entry_safe(op, next, &bo->rx_ops, list) bcm_remove_op(op); -#if IS_ENABLED(CONFIG_PROC_FS) - /* remove procfs entry */ - if (net->can.bcmproc_dir && bo->bcm_proc_read) - remove_proc_entry(bo->procname, net->can.bcmproc_dir); -#endif /* CONFIG_PROC_FS */ - /* remove device reference */ if (bo->bound) { bo->bound = 0;