Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2718609rwd; Fri, 19 May 2023 09:15:37 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7PoIH0cbSEJMFdlD4S1xYlmRB4bHV1LPUVgYgILN9ieAcazEjVcR4mYGOxuho8Kki+NIeY X-Received: by 2002:a17:903:2343:b0:1ab:109e:a553 with SMTP id c3-20020a170903234300b001ab109ea553mr3906723plh.62.1684512936881; Fri, 19 May 2023 09:15:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684512936; cv=none; d=google.com; s=arc-20160816; b=WgpXA615Z3Ocpd9EgnsdamDP03Ax6Dh+dDV8gDN20RT3M+8+Ok+dm/lL/uY6zPlKFJ fep3Onal1k23RAT+tcrSkROoqK/VzztXKVEWJ6Uh+z77+ufj9S/W0jgYoHSKzf+ke4jU yCEZ0vbFaOdELM8cMLvXM64LvfsHMzyGiFDRTTdzkFVAndff3y8Jvkkh85aF57A1qtoI GWV5F/msz2WSKGzhsxULgZGy2cZi9wtSJW65Em0T8AAbiffyJ42ipP+h/sWFZgSRwqok TIrPxQzl41ekvHl8w1rxqV/+2oAdQOZy7psvAbyGInEZmiF85yz06wAAlyTUavdP7Y/M mGKA== 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=+boOcrtnW2EIoIzSqmpcFQJh1gKYjBjysOjuwNaH4gY=; b=goe+1aaGUZUg4ZWoYNmbFVcElywpPfcw41vT6/Fru4m3lMYHTsPpIBmKKZ7Jl/FU3L ifLnkUURaAev6FL58Z6L0muXtwvTjkDYMKH9lnBTLbuhWUUowWEF2jnZflae3dWSu1sd meRYkdk9UmWmexZ4mRoLa2zjo+oRKq/Zk+Kqgpy74EeM3Eyg5msZwn1Bal7mhZU5Lbfw KzOSiVkVUi2VOapSB5hOyisMTR6jjzaDik6qo1XV+2moUuktFPxGv9/t8cVHxTbUNCZ3 iVM7MpGD3vjFSOxhtEJkzMbhR+aS8CGsjWLQVLfw9Nm7ja8OECvmUlTFNIqaS+1iiBVP mY7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QdwUqtYQ; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n9-20020a170902e54900b001a1b0a828a8si4009358plf.621.2023.05.19.09.15.23; Fri, 19 May 2023 09:15:36 -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; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QdwUqtYQ; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229959AbjESQNz (ORCPT + 99 others); Fri, 19 May 2023 12:13:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229915AbjESQNx (ORCPT ); Fri, 19 May 2023 12:13:53 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A4456AA; Fri, 19 May 2023 09:13:52 -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 39538658E3; Fri, 19 May 2023 16:13:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E0DAFC433D2; Fri, 19 May 2023 16:13:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1684512831; bh=pqzdTNgUwHqgceYCq0XJCIoUPvNv3LVkJxE36qND4w4=; h=From:To:Cc:Subject:Date:From; b=QdwUqtYQxRYvZ8fGL7aXJNySCssZTF4hBaqwayQZqCy0SJOiXuIzSoatyvi9Wvc+F zAHc1PMP4JFoL1TK892sJz2q7daRQTIsx3rzwcFNU26So3QUB1nYuX7dfg6NH2hyni W4zJatNmfO4g39jEN5kXUDeh4AkR7BGO5cl8UyP9fzWmuJZBHsBnLNKTJnQpIeA3Kn 2SaCaQi/V8o6yUeSrz+po6MdnbyT3+bOWtuw3UswpkL61LRQXnhSIe9F/WRvAwZ3GO R5BzANOHoG+up1I1dglOUeoRShwkUt1VFLKw1Vu3p6l5uyUrpnTXSVDNvbvKE70siY EetpdLrjD4TDA== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Lee Jones , Jassi Brar , Sasha Levin , jassisinghbrar@gmail.com Subject: [PATCH AUTOSEL 6.3] mailbox: mailbox-test: Fix potential double-free in mbox_test_message_write() Date: Fri, 19 May 2023 12:13:47 -0400 Message-Id: <20230519161348.2750641-1-sashal@kernel.org> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, 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 From: Lee Jones [ Upstream commit 2d1e952a2b8e5e92d8d55ac88a7cf7ca5ea591ad ] If a user can make copy_from_user() fail, there is a potential for UAF/DF due to a lack of locking around the allocation, use and freeing of the data buffers. This issue is not theoretical. I managed to author a POC for it: BUG: KASAN: double-free in kfree+0x5c/0xac Free of addr ffff29280be5de00 by task poc/356 CPU: 1 PID: 356 Comm: poc Not tainted 6.1.0-00001-g961aa6552c04-dirty #20 Hardware name: linux,dummy-virt (DT) Call trace: dump_backtrace.part.0+0xe0/0xf0 show_stack+0x18/0x40 dump_stack_lvl+0x64/0x80 print_report+0x188/0x48c kasan_report_invalid_free+0xa0/0xc0 ____kasan_slab_free+0x174/0x1b0 __kasan_slab_free+0x18/0x24 __kmem_cache_free+0x130/0x2e0 kfree+0x5c/0xac mbox_test_message_write+0x208/0x29c full_proxy_write+0x90/0xf0 vfs_write+0x154/0x440 ksys_write+0xcc/0x180 __arm64_sys_write+0x44/0x60 invoke_syscall+0x60/0x190 el0_svc_common.constprop.0+0x7c/0x160 do_el0_svc+0x40/0xf0 el0_svc+0x2c/0x6c el0t_64_sync_handler+0xf4/0x120 el0t_64_sync+0x18c/0x190 Allocated by task 356: kasan_save_stack+0x3c/0x70 kasan_set_track+0x2c/0x40 kasan_save_alloc_info+0x24/0x34 __kasan_kmalloc+0xb8/0xc0 kmalloc_trace+0x58/0x70 mbox_test_message_write+0x6c/0x29c full_proxy_write+0x90/0xf0 vfs_write+0x154/0x440 ksys_write+0xcc/0x180 __arm64_sys_write+0x44/0x60 invoke_syscall+0x60/0x190 el0_svc_common.constprop.0+0x7c/0x160 do_el0_svc+0x40/0xf0 el0_svc+0x2c/0x6c el0t_64_sync_handler+0xf4/0x120 el0t_64_sync+0x18c/0x190 Freed by task 357: kasan_save_stack+0x3c/0x70 kasan_set_track+0x2c/0x40 kasan_save_free_info+0x38/0x5c ____kasan_slab_free+0x13c/0x1b0 __kasan_slab_free+0x18/0x24 __kmem_cache_free+0x130/0x2e0 kfree+0x5c/0xac mbox_test_message_write+0x208/0x29c full_proxy_write+0x90/0xf0 vfs_write+0x154/0x440 ksys_write+0xcc/0x180 __arm64_sys_write+0x44/0x60 invoke_syscall+0x60/0x190 el0_svc_common.constprop.0+0x7c/0x160 do_el0_svc+0x40/0xf0 el0_svc+0x2c/0x6c el0t_64_sync_handler+0xf4/0x120 el0t_64_sync+0x18c/0x190 Signed-off-by: Lee Jones Signed-off-by: Jassi Brar Signed-off-by: Sasha Levin --- drivers/mailbox/mailbox-test.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/drivers/mailbox/mailbox-test.c b/drivers/mailbox/mailbox-test.c index 4555d678fadda..6dd5b9614452b 100644 --- a/drivers/mailbox/mailbox-test.c +++ b/drivers/mailbox/mailbox-test.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -38,6 +39,7 @@ struct mbox_test_device { char *signal; char *message; spinlock_t lock; + struct mutex mutex; wait_queue_head_t waitq; struct fasync_struct *async_queue; struct dentry *root_debugfs_dir; @@ -110,6 +112,8 @@ static ssize_t mbox_test_message_write(struct file *filp, return -EINVAL; } + mutex_lock(&tdev->mutex); + tdev->message = kzalloc(MBOX_MAX_MSG_LEN, GFP_KERNEL); if (!tdev->message) return -ENOMEM; @@ -144,6 +148,8 @@ static ssize_t mbox_test_message_write(struct file *filp, kfree(tdev->message); tdev->signal = NULL; + mutex_unlock(&tdev->mutex); + return ret < 0 ? ret : count; } @@ -392,6 +398,7 @@ static int mbox_test_probe(struct platform_device *pdev) platform_set_drvdata(pdev, tdev); spin_lock_init(&tdev->lock); + mutex_init(&tdev->mutex); if (tdev->rx_channel) { tdev->rx_buffer = devm_kzalloc(&pdev->dev, -- 2.39.2