Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp3334735rwa; Tue, 23 Aug 2022 03:04:24 -0700 (PDT) X-Google-Smtp-Source: AA6agR7hOOvsx9CMq3hGb6f5Sa/gyzfsX53Fjlhp0acqMwBHSyIHJj8eKNHSjV514xNuUE8CcDBM X-Received: by 2002:a05:6402:134e:b0:445:d389:266e with SMTP id y14-20020a056402134e00b00445d389266emr3067104edw.97.1661249064501; Tue, 23 Aug 2022 03:04:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661249064; cv=none; d=google.com; s=arc-20160816; b=b8spWSLgz+jjL/aq2XeIyvPNCEOeyVFLDixU5qyvYi2IitBsu8DKqF7CwPaNY3s6dR DQo1CENvb/yfaw+S2C6Q4be9xHOEvMfVG/mkHDE3ECXwx1PqFY3VaOGcT5LDgDftIM5F 2grAIpxfKCEmqx8iMB8EweHVU+ve7MG90CZXB+KIhn5ZRM6rW6K0tDnlwPmm3kMo6OvQ 5YjnxhyHffTR/gGV2L1oiOkz/SwDjSE9YNbi7kTjxJIJp28d3ubKZK7GDpIbS4fvX5ld XhuX2D+8EIgms7gcHkYnKLk/pL+BDoB01+u8QlkUW7KwVprFGdWEjGBQbAvtQ01z7EHg 8Kjw== 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=t/48IFW8unkbUP5xsJPwYUgzbA5KNe6g4lkFJx7lZts=; b=leydgn3dCrssjerQEWO2hqaL/6Y9/niR5KKqs5+qPscWYs3SRaOtjH5NNgvTT0tYkA vk4pw+NVJ3si3yR5xPUQVXiQwr1IXS2Y+2qhVPC1ZqYPU7j2DfnWY20/b3vFN1KqN2vg a5ZZDD3+T4/x/El2N5Ya1fauDKC2ofGSMBVUh4Pbwv8aEvCaOeeokv/a9BGLGlLTUhw0 tHURkAphA6tqMp1W6HkJYMssZ8JfU3p5HLyF9q57usrMBJYgd9eu8tfe9ca6qkb0zAxP VaIiVDlpBzE5FRKsX8eptBdd1sj6LvUO1ni9R0EbNgY1Beyf2x01ylEVeNcpjMSPWuvP U3uw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=JNE0I+hu; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u11-20020a50950b000000b00441e5fddd4asi1741900eda.413.2022.08.23.03.03.57; Tue, 23 Aug 2022 03:04:24 -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=@linuxfoundation.org header.s=korg header.b=JNE0I+hu; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243900AbiHWJzV (ORCPT + 99 others); Tue, 23 Aug 2022 05:55:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242564AbiHWJxx (ORCPT ); Tue, 23 Aug 2022 05:53: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 8E36F6E8A2; Tue, 23 Aug 2022 01:46:23 -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 4322B61499; Tue, 23 Aug 2022 08:45:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2D07CC433C1; Tue, 23 Aug 2022 08:45:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661244335; bh=/+eRUPMPZDWrhSIl0FMK0dBhrrqdcIZx5ZENoVmu4KY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JNE0I+humPVzjic7nZQqey3n0yMA+iqgJJhvqSp8KHJWbI/M2gguelXuFJceP1o1n 8BP4eTWQEa4WSXIVKCwjdju68Lt+QLP7Cw28Nd+zRdiQi2mfSChwUFgPYG/uY6VHiV R7/hosLko1swpAX36KznlY6ebusL5kapMXUJ10Xk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Chen Zhongjin , Andrew Morton , Sasha Levin Subject: [PATCH 4.14 121/229] profiling: fix shift too large makes kernel panic Date: Tue, 23 Aug 2022 10:24:42 +0200 Message-Id: <20220823080058.031432587@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220823080053.202747790@linuxfoundation.org> References: <20220823080053.202747790@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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: Chen Zhongjin [ Upstream commit 0fe6ee8f123a4dfb529a5aff07536bb481f34043 ] 2d186afd04d6 ("profiling: fix shift-out-of-bounds bugs") limits shift value by [0, BITS_PER_LONG -1], which means [0, 63]. However, syzbot found that the max shift value should be the bit number of (_etext - _stext). If shift is outside of this, the "buffer_bytes" will be zero and will cause kzalloc(0). Then the kernel panics due to dereferencing the returned pointer 16. This can be easily reproduced by passing a large number like 60 to enable profiling and then run readprofile. LOGS: BUG: kernel NULL pointer dereference, address: 0000000000000010 #PF: supervisor write access in kernel mode #PF: error_code(0x0002) - not-present page PGD 6148067 P4D 6148067 PUD 6142067 PMD 0 PREEMPT SMP CPU: 4 PID: 184 Comm: readprofile Not tainted 5.18.0+ #162 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.15.0-0-g2dd4b9b3f840-prebuilt.qemu.org 04/01/2014 RIP: 0010:read_profile+0x104/0x220 RSP: 0018:ffffc900006fbe80 EFLAGS: 00000202 RAX: 0000000000000000 RBX: 0000000000000000 RCX: 0000000000000000 RDX: ffff888006150000 RSI: 0000000000000001 RDI: ffffffff82aba4a0 RBP: 000000000188bb60 R08: 0000000000000010 R09: ffff888006151000 R10: 0000000000000000 R11: 0000000000000000 R12: ffffffff82aba4a0 R13: 0000000000000000 R14: ffffc900006fbf08 R15: 0000000000020c30 FS: 000000000188a8c0(0000) GS:ffff88803ed00000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000000000000010 CR3: 0000000006144000 CR4: 00000000000006e0 Call Trace: proc_reg_read+0x56/0x70 vfs_read+0x9a/0x1b0 ksys_read+0xa1/0xe0 ? fpregs_assert_state_consistent+0x1e/0x40 do_syscall_64+0x3a/0x80 entry_SYSCALL_64_after_hwframe+0x46/0xb0 RIP: 0033:0x4d4b4e RSP: 002b:00007ffebb668d58 EFLAGS: 00000246 ORIG_RAX: 0000000000000000 RAX: ffffffffffffffda RBX: 000000000188a8a0 RCX: 00000000004d4b4e RDX: 0000000000000400 RSI: 000000000188bb60 RDI: 0000000000000003 RBP: 0000000000000003 R08: 000000000000006e R09: 0000000000000000 R10: 0000000000000041 R11: 0000000000000246 R12: 000000000188bb60 R13: 0000000000000400 R14: 0000000000000000 R15: 000000000188bb60 Modules linked in: CR2: 0000000000000010 Killed ---[ end trace 0000000000000000 ]--- Check prof_len in profile_init() to prevent it be zero. Link: https://lkml.kernel.org/r/20220531012854.229439-1-chenzhongjin@huawei.com Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Chen Zhongjin Signed-off-by: Andrew Morton Signed-off-by: Sasha Levin --- kernel/profile.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/kernel/profile.c b/kernel/profile.c index efa58f63dc1b..7fc621404230 100644 --- a/kernel/profile.c +++ b/kernel/profile.c @@ -108,6 +108,13 @@ int __ref profile_init(void) /* only text is profiled */ prof_len = (_etext - _stext) >> prof_shift; + + if (!prof_len) { + pr_warn("profiling shift: %u too large\n", prof_shift); + prof_on = 0; + return -EINVAL; + } + buffer_bytes = prof_len*sizeof(atomic_t); if (!alloc_cpumask_var(&prof_cpu_mask, GFP_KERNEL)) -- 2.35.1