Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp41056pxb; Tue, 12 Apr 2022 16:13:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx9UCEX705jXVXTpwlOWUAn7cuqb39K2G7lgQjzqSc8QxnDotzQBgnoGiC95s3iDquZM1v9 X-Received: by 2002:a05:6a00:b47:b0:4fd:a5b5:a279 with SMTP id p7-20020a056a000b4700b004fda5b5a279mr40235082pfo.19.1649805185978; Tue, 12 Apr 2022 16:13:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649805185; cv=none; d=google.com; s=arc-20160816; b=pqqOZcUrJyEmnUXHr8Al3xTjm0uqMXQgicjl45HZewGYeJmTkOIXF7MtavHr9Tq/k8 lTwnQXQ6ZeWIj6EJ3emi3kTTaIHug18G9MfEMYgxEzZw6sRD145r8PPnSdD93btGLlQM rY+TWqllKOoAOVF93+VOaTvLuzuERUMDaYlBwTIWKyDDnLgevVZDl0H9/amvCycC/0hN aJdnR1bXL1cVxG6mI3LJYqoY3wtw2Bacq1nfBDJ5ZfoHgulNL4N2w0ykDvZJyjgyvDbt xIf146rEnhl1y7wio3Pqulek7GdTWA1dT+Ugzp+PJ681PGptXu+2URRDNoAobsUFp9qI on1A== 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=728XG9ubDgclPP3lNgn2i1bqs/CUKjqOKt/t2jC1oz4=; b=RlyY311VlYufGo2CfJbmuX9Jx4/bt2xnD+yPffubGo5gxbHz6tCsoA5dth/0chAAyW uyhJtqM2YS7lxjJe7fM6PDqMyKPQoBOBlnzJugS05qpmBF/UcSFdyAt5bGR8WHD12e54 3AXaHR2S+fbdga7ktMuM7ESAFMeLBwm7M7Gsuhq/h56Y54bfB3PbKArV7BCOKOWKD5MV 2ZdyOiWIW33hldOBOlSFEgjnOAPnCTBc7GF4Xg4qs8y8PV8zIsfxNXBnylBxvCuBdDSs FXybYHIe1tDkHbg9rRHuQhqe47iIwuYa1vVTMc6bSjNnAAFZJniM5Zb8dmQv2fgYu0cq ErfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=OucAgQPm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id h18-20020a170902f55200b00153c3f96abesi13735713plf.137.2022.04.12.16.13.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Apr 2022 16:13:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=OucAgQPm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E39687005F; Tue, 12 Apr 2022 14:55:55 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245466AbiDLHK3 (ORCPT + 99 others); Tue, 12 Apr 2022 03:10:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352141AbiDLGzJ (ORCPT ); Tue, 12 Apr 2022 02:55:09 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F86D393C6; Mon, 11 Apr 2022 23:45:00 -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 sin.source.kernel.org (Postfix) with ESMTPS id C0A6ECE1C0A; Tue, 12 Apr 2022 06:44:58 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CF33AC385A1; Tue, 12 Apr 2022 06:44:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649745897; bh=Nq4PFmOn6Y0TxJ4NhDk0XkoQCdlfSaGTmCaEbHTGe/8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OucAgQPm02OFvip5UGCgEeB/h5fsSVXgY4Ym/ClrictWg+gxyDnlv7979gB97ty1e pCdO3vCaImL2ZUEHBnIgUwi6BKmC8r4KyK7FbZz1B2oCNrPtpewi5UTFMhqYAgXBf0 liZXlbSb5BhC2l7+EGz6Gv5qq8+oynEJGO4kl2Yo= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Alexander Lobakin , Thomas Bogendoerfer , Sasha Levin Subject: [PATCH 5.15 086/277] MIPS: fix fortify panic when copying asm exception handlers Date: Tue, 12 Apr 2022 08:28:09 +0200 Message-Id: <20220412062944.536324914@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220412062942.022903016@linuxfoundation.org> References: <20220412062942.022903016@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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: Alexander Lobakin [ Upstream commit d17b66417308996e7e64b270a3c7f3c1fbd4cfc8 ] With KCFLAGS="-O3", I was able to trigger a fortify-source memcpy() overflow panic on set_vi_srs_handler(). Although O3 level is not supported in the mainline, under some conditions that may've happened with any optimization settings, it's just a matter of inlining luck. The panic itself is correct, more precisely, 50/50 false-positive and not at the same time. >From the one side, no real overflow happens. Exception handler defined in asm just gets copied to some reserved places in the memory. But the reason behind is that C code refers to that exception handler declares it as `char`, i.e. something of 1 byte length. It's obvious that the asm function itself is way more than 1 byte, so fortify logics thought we are going to past the symbol declared. The standard way to refer to asm symbols from C code which is not supposed to be called from C is to declare them as `extern const u8[]`. This is fully correct from any point of view, as any code itself is just a bunch of bytes (including 0 as it is for syms like _stext/_etext/etc.), and the exact size is not known at the moment of compilation. Adjust the type of the except_vec_vi_*() and related variables. Make set_handler() take `const` as a second argument to avoid cast-away warnings and give a little more room for optimization. Signed-off-by: Alexander Lobakin Signed-off-by: Thomas Bogendoerfer Signed-off-by: Sasha Levin --- arch/mips/include/asm/setup.h | 2 +- arch/mips/kernel/traps.c | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/arch/mips/include/asm/setup.h b/arch/mips/include/asm/setup.h index bb36a400203d..8c56b862fd9c 100644 --- a/arch/mips/include/asm/setup.h +++ b/arch/mips/include/asm/setup.h @@ -16,7 +16,7 @@ static inline void setup_8250_early_printk_port(unsigned long base, unsigned int reg_shift, unsigned int timeout) {} #endif -extern void set_handler(unsigned long offset, void *addr, unsigned long len); +void set_handler(unsigned long offset, const void *addr, unsigned long len); extern void set_uncached_handler(unsigned long offset, void *addr, unsigned long len); typedef void (*vi_handler_t)(void); diff --git a/arch/mips/kernel/traps.c b/arch/mips/kernel/traps.c index 6f07362de5ce..edd93430b954 100644 --- a/arch/mips/kernel/traps.c +++ b/arch/mips/kernel/traps.c @@ -2085,19 +2085,19 @@ static void *set_vi_srs_handler(int n, vi_handler_t addr, int srs) * If no shadow set is selected then use the default handler * that does normal register saving and standard interrupt exit */ - extern char except_vec_vi, except_vec_vi_lui; - extern char except_vec_vi_ori, except_vec_vi_end; - extern char rollback_except_vec_vi; - char *vec_start = using_rollback_handler() ? - &rollback_except_vec_vi : &except_vec_vi; + extern const u8 except_vec_vi[], except_vec_vi_lui[]; + extern const u8 except_vec_vi_ori[], except_vec_vi_end[]; + extern const u8 rollback_except_vec_vi[]; + const u8 *vec_start = using_rollback_handler() ? + rollback_except_vec_vi : except_vec_vi; #if defined(CONFIG_CPU_MICROMIPS) || defined(CONFIG_CPU_BIG_ENDIAN) - const int lui_offset = &except_vec_vi_lui - vec_start + 2; - const int ori_offset = &except_vec_vi_ori - vec_start + 2; + const int lui_offset = except_vec_vi_lui - vec_start + 2; + const int ori_offset = except_vec_vi_ori - vec_start + 2; #else - const int lui_offset = &except_vec_vi_lui - vec_start; - const int ori_offset = &except_vec_vi_ori - vec_start; + const int lui_offset = except_vec_vi_lui - vec_start; + const int ori_offset = except_vec_vi_ori - vec_start; #endif - const int handler_len = &except_vec_vi_end - vec_start; + const int handler_len = except_vec_vi_end - vec_start; if (handler_len > VECTORSPACING) { /* @@ -2305,7 +2305,7 @@ void per_cpu_trap_init(bool is_boot_cpu) } /* Install CPU exception handler */ -void set_handler(unsigned long offset, void *addr, unsigned long size) +void set_handler(unsigned long offset, const void *addr, unsigned long size) { #ifdef CONFIG_CPU_MICROMIPS memcpy((void *)(ebase + offset), ((unsigned char *)addr - 1), size); -- 2.35.1