stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Alexander Lobakin <alobakin@pm.me>,
	Thomas Bogendoerfer <tsbogend@alpha.franken.de>,
	Sasha Levin <sashal@kernel.org>,
	akpm@linux-foundation.org, david@redhat.com,
	ldufour@linux.ibm.com, dbueso@suse.de, liam.howlett@oracle.com,
	f.fainelli@gmail.com, ebiederm@xmission.com,
	linux-mips@vger.kernel.org
Subject: [PATCH AUTOSEL 5.4 29/37] MIPS: fix fortify panic when copying asm exception handlers
Date: Fri,  1 Apr 2022 10:44:38 -0400	[thread overview]
Message-ID: <20220401144446.1954694-29-sashal@kernel.org> (raw)
In-Reply-To: <20220401144446.1954694-1-sashal@kernel.org>

From: Alexander Lobakin <alobakin@pm.me>

[ 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 <alobakin@pm.me>
Signed-off-by: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 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 8282d0feb0b2..749089c25d5e 100644
--- a/arch/mips/kernel/traps.c
+++ b/arch/mips/kernel/traps.c
@@ -2020,19 +2020,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) {
 			/*
@@ -2240,7 +2240,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.34.1


  parent reply	other threads:[~2022-04-01 15:05 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-01 14:44 [PATCH AUTOSEL 5.4 01/37] drm: Add orientation quirk for GPD Win Max Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 02/37] ath5k: fix OOB in ath5k_eeprom_read_pcal_info_5111 Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 03/37] drm/amd/amdgpu/amdgpu_cs: fix refcount leak of a dma_fence obj Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 04/37] ptp: replace snprintf with sysfs_emit Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 05/37] powerpc: dts: t104xrdb: fix phy type for FMAN 4/5 Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 06/37] bpf: Make dst_port field in struct bpf_sock 16-bit wide Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 07/37] scsi: mvsas: Replace snprintf() with sysfs_emit() Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 08/37] scsi: bfa: " Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 09/37] power: supply: axp20x_battery: properly report current when discharging Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 10/37] ipv6: make mc_forwarding atomic Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 11/37] powerpc: Set crashkernel offset to mid of RMA region Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 12/37] drm/amdgpu: Fix recursive locking warning Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 13/37] PCI: aardvark: Fix support for MSI interrupts Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 14/37] iommu/arm-smmu-v3: fix event handling soft lockup Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 15/37] usb: ehci: add pci device support for Aspeed platforms Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 16/37] PCI: pciehp: Add Qualcomm quirk for Command Completed erratum Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 17/37] power: supply: axp288-charger: Set Vhold to 4.4V Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 18/37] ipv4: Invalidate neighbour for broadcast address upon address addition Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 19/37] dm ioctl: prevent potential spectre v1 gadget Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 20/37] drm/amdkfd: make CRAT table missing message informational only Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 21/37] scsi: pm8001: Fix pm8001_mpi_task_abort_resp() Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 22/37] scsi: aha152x: Fix aha152x_setup() __setup handler return value Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 23/37] net/smc: correct settings of RMB window update limit Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 24/37] mips: ralink: fix a refcount leak in ill_acc_of_setup() Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 25/37] macvtap: advertise link netns via netlink Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 26/37] tuntap: add sanity checks about msg_controllen in sendmsg Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 27/37] iommu/iova: Improve 32-bit free space estimate Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 28/37] bnxt_en: Eliminate unintended link toggle during FW reset Sasha Levin
2022-04-01 14:44 ` Sasha Levin [this message]
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 30/37] powerpc/code-patching: Pre-map patch area Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 31/37] scsi: libfc: Fix use after free in fc_exch_abts_resp() Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 32/37] usb: dwc3: omap: fix "unbalanced disables for smps10_out1" on omap5evm Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 33/37] xtensa: fix DTC warning unit_address_format Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 34/37] Bluetooth: Fix use after free in hci_send_acl Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 35/37] netlabel: fix out-of-bounds memory accesses Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 36/37] init/main.c: return 1 from handled __setup() functions Sasha Levin
2022-04-01 14:44 ` [PATCH AUTOSEL 5.4 37/37] minix: fix bug when opening a file with O_DIRECT Sasha Levin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220401144446.1954694-29-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=alobakin@pm.me \
    --cc=david@redhat.com \
    --cc=dbueso@suse.de \
    --cc=ebiederm@xmission.com \
    --cc=f.fainelli@gmail.com \
    --cc=ldufour@linux.ibm.com \
    --cc=liam.howlett@oracle.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mips@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=tsbogend@alpha.franken.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).