All of lore.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, Hari Bathini <hbathini@linux.ibm.com>,
	Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com>,
	Michael Ellerman <mpe@ellerman.id.au>
Subject: [PATCH 4.9 09/63] powerpc/fadump: handle crash memory ranges array index overflow
Date: Fri,  7 Sep 2018 23:09:40 +0200	[thread overview]
Message-ID: <20180907211013.477016436@linuxfoundation.org> (raw)
In-Reply-To: <20180907211012.965501324@linuxfoundation.org>

4.9-stable review patch.  If anyone has any objections, please let me know.

------------------

From: Hari Bathini <hbathini@linux.ibm.com>

commit 1bd6a1c4b80a28d975287630644e6b47d0f977a5 upstream.

Crash memory ranges is an array of memory ranges of the crashing kernel
to be exported as a dump via /proc/vmcore file. The size of the array
is set based on INIT_MEMBLOCK_REGIONS, which works alright in most cases
where memblock memory regions count is less than INIT_MEMBLOCK_REGIONS
value. But this count can grow beyond INIT_MEMBLOCK_REGIONS value since
commit 142b45a72e22 ("memblock: Add array resizing support").

On large memory systems with a few DLPAR operations, the memblock memory
regions count could be larger than INIT_MEMBLOCK_REGIONS value. On such
systems, registering fadump results in crash or other system failures
like below:

  task: c00007f39a290010 ti: c00000000b738000 task.ti: c00000000b738000
  NIP: c000000000047df4 LR: c0000000000f9e58 CTR: c00000000010f180
  REGS: c00000000b73b570 TRAP: 0300   Tainted: G          L   X  (4.4.140+)
  MSR: 8000000000009033 <SF,EE,ME,IR,DR,RI,LE>  CR: 22004484  XER: 20000000
  CFAR: c000000000008500 DAR: 000007a450000000 DSISR: 40000000 SOFTE: 0
  ...
  NIP [c000000000047df4] smp_send_reschedule+0x24/0x80
  LR [c0000000000f9e58] resched_curr+0x138/0x160
  Call Trace:
    resched_curr+0x138/0x160 (unreliable)
    check_preempt_curr+0xc8/0xf0
    ttwu_do_wakeup+0x38/0x150
    try_to_wake_up+0x224/0x4d0
    __wake_up_common+0x94/0x100
    ep_poll_callback+0xac/0x1c0
    __wake_up_common+0x94/0x100
    __wake_up_sync_key+0x70/0xa0
    sock_def_readable+0x58/0xa0
    unix_stream_sendmsg+0x2dc/0x4c0
    sock_sendmsg+0x68/0xa0
    ___sys_sendmsg+0x2cc/0x2e0
    __sys_sendmsg+0x5c/0xc0
    SyS_socketcall+0x36c/0x3f0
    system_call+0x3c/0x100

as array index overflow is not checked for while setting up crash memory
ranges causing memory corruption. To resolve this issue, dynamically
allocate memory for crash memory ranges and resize it incrementally,
in units of pagesize, on hitting array size limit.

Fixes: 2df173d9e85d ("fadump: Initialize elfcore header and add PT_LOAD program headers.")
Cc: stable@vger.kernel.org # v3.4+
Signed-off-by: Hari Bathini <hbathini@linux.ibm.com>
Reviewed-by: Mahesh Salgaonkar <mahesh@linux.vnet.ibm.com>
[mpe: Just use PAGE_SIZE directly, fixup variable placement]
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 arch/powerpc/include/asm/fadump.h |    3 -
 arch/powerpc/kernel/fadump.c      |   91 ++++++++++++++++++++++++++++++++------
 2 files changed, 77 insertions(+), 17 deletions(-)

--- a/arch/powerpc/include/asm/fadump.h
+++ b/arch/powerpc/include/asm/fadump.h
@@ -190,9 +190,6 @@ struct fadump_crash_info_header {
 	struct cpumask	online_mask;
 };
 
-/* Crash memory ranges */
-#define INIT_CRASHMEM_RANGES	(INIT_MEMBLOCK_REGIONS + 2)
-
 struct fad_crash_memory_ranges {
 	unsigned long long	base;
 	unsigned long long	size;
--- a/arch/powerpc/kernel/fadump.c
+++ b/arch/powerpc/kernel/fadump.c
@@ -48,8 +48,10 @@ static struct fadump_mem_struct fdm;
 static const struct fadump_mem_struct *fdm_active;
 
 static DEFINE_MUTEX(fadump_mutex);
-struct fad_crash_memory_ranges crash_memory_ranges[INIT_CRASHMEM_RANGES];
+struct fad_crash_memory_ranges *crash_memory_ranges;
+int crash_memory_ranges_size;
 int crash_mem_ranges;
+int max_crash_mem_ranges;
 
 /* Scan the Firmware Assisted dump configuration details. */
 int __init early_init_dt_scan_fw_dump(unsigned long node,
@@ -731,38 +733,88 @@ static int __init process_fadump(const s
 	return 0;
 }
 
-static inline void fadump_add_crash_memory(unsigned long long base,
-					unsigned long long end)
+static void free_crash_memory_ranges(void)
+{
+	kfree(crash_memory_ranges);
+	crash_memory_ranges = NULL;
+	crash_memory_ranges_size = 0;
+	max_crash_mem_ranges = 0;
+}
+
+/*
+ * Allocate or reallocate crash memory ranges array in incremental units
+ * of PAGE_SIZE.
+ */
+static int allocate_crash_memory_ranges(void)
+{
+	struct fad_crash_memory_ranges *new_array;
+	u64 new_size;
+
+	new_size = crash_memory_ranges_size + PAGE_SIZE;
+	pr_debug("Allocating %llu bytes of memory for crash memory ranges\n",
+		 new_size);
+
+	new_array = krealloc(crash_memory_ranges, new_size, GFP_KERNEL);
+	if (new_array == NULL) {
+		pr_err("Insufficient memory for setting up crash memory ranges\n");
+		free_crash_memory_ranges();
+		return -ENOMEM;
+	}
+
+	crash_memory_ranges = new_array;
+	crash_memory_ranges_size = new_size;
+	max_crash_mem_ranges = (new_size /
+				sizeof(struct fad_crash_memory_ranges));
+	return 0;
+}
+
+static inline int fadump_add_crash_memory(unsigned long long base,
+					  unsigned long long end)
 {
 	if (base == end)
-		return;
+		return 0;
+
+	if (crash_mem_ranges == max_crash_mem_ranges) {
+		int ret;
+
+		ret = allocate_crash_memory_ranges();
+		if (ret)
+			return ret;
+	}
 
 	pr_debug("crash_memory_range[%d] [%#016llx-%#016llx], %#llx bytes\n",
 		crash_mem_ranges, base, end - 1, (end - base));
 	crash_memory_ranges[crash_mem_ranges].base = base;
 	crash_memory_ranges[crash_mem_ranges].size = end - base;
 	crash_mem_ranges++;
+	return 0;
 }
 
-static void fadump_exclude_reserved_area(unsigned long long start,
+static int fadump_exclude_reserved_area(unsigned long long start,
 					unsigned long long end)
 {
 	unsigned long long ra_start, ra_end;
+	int ret = 0;
 
 	ra_start = fw_dump.reserve_dump_area_start;
 	ra_end = ra_start + fw_dump.reserve_dump_area_size;
 
 	if ((ra_start < end) && (ra_end > start)) {
 		if ((start < ra_start) && (end > ra_end)) {
-			fadump_add_crash_memory(start, ra_start);
-			fadump_add_crash_memory(ra_end, end);
+			ret = fadump_add_crash_memory(start, ra_start);
+			if (ret)
+				return ret;
+
+			ret = fadump_add_crash_memory(ra_end, end);
 		} else if (start < ra_start) {
-			fadump_add_crash_memory(start, ra_start);
+			ret = fadump_add_crash_memory(start, ra_start);
 		} else if (ra_end < end) {
-			fadump_add_crash_memory(ra_end, end);
+			ret = fadump_add_crash_memory(ra_end, end);
 		}
 	} else
-		fadump_add_crash_memory(start, end);
+		ret = fadump_add_crash_memory(start, end);
+
+	return ret;
 }
 
 static int fadump_init_elfcore_header(char *bufp)
@@ -802,10 +854,11 @@ static int fadump_init_elfcore_header(ch
  * Traverse through memblock structure and setup crash memory ranges. These
  * ranges will be used create PT_LOAD program headers in elfcore header.
  */
-static void fadump_setup_crash_memory_ranges(void)
+static int fadump_setup_crash_memory_ranges(void)
 {
 	struct memblock_region *reg;
 	unsigned long long start, end;
+	int ret;
 
 	pr_debug("Setup crash memory ranges.\n");
 	crash_mem_ranges = 0;
@@ -816,7 +869,9 @@ static void fadump_setup_crash_memory_ra
 	 * specified during fadump registration. We need to create a separate
 	 * program header for this chunk with the correct offset.
 	 */
-	fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size);
+	ret = fadump_add_crash_memory(RMA_START, fw_dump.boot_memory_size);
+	if (ret)
+		return ret;
 
 	for_each_memblock(memory, reg) {
 		start = (unsigned long long)reg->base;
@@ -825,8 +880,12 @@ static void fadump_setup_crash_memory_ra
 			start = fw_dump.boot_memory_size;
 
 		/* add this range excluding the reserved dump area. */
-		fadump_exclude_reserved_area(start, end);
+		ret = fadump_exclude_reserved_area(start, end);
+		if (ret)
+			return ret;
 	}
+
+	return 0;
 }
 
 /*
@@ -950,6 +1009,7 @@ static void register_fadump(void)
 {
 	unsigned long addr;
 	void *vaddr;
+	int ret;
 
 	/*
 	 * If no memory is reserved then we can not register for firmware-
@@ -958,7 +1018,9 @@ static void register_fadump(void)
 	if (!fw_dump.reserve_dump_area_size)
 		return;
 
-	fadump_setup_crash_memory_ranges();
+	ret = fadump_setup_crash_memory_ranges();
+	if (ret)
+		return ret;
 
 	addr = be64_to_cpu(fdm.rmr_region.destination_address) + be64_to_cpu(fdm.rmr_region.source_len);
 	/* Initialize fadump crash info header. */
@@ -1036,6 +1098,7 @@ void fadump_cleanup(void)
 	} else if (fw_dump.dump_registered) {
 		/* Un-register Firmware-assisted dump if it was registered. */
 		fadump_unregister_dump(&fdm);
+		free_crash_memory_ranges();
 	}
 }
 



  parent reply	other threads:[~2018-09-07 21:34 UTC|newest]

Thread overview: 81+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-07 21:09 [PATCH 4.9 00/63] 4.9.126-stable review Greg Kroah-Hartman
2018-09-07 21:09 ` [PATCH 4.9 01/63] net: 6lowpan: fix reserved space for single frames Greg Kroah-Hartman
2018-09-07 21:09 ` [PATCH 4.9 02/63] net: mac802154: tx: expand tailroom if necessary Greg Kroah-Hartman
2018-09-07 21:09 ` [PATCH 4.9 03/63] 9p/net: Fix zero-copy path in the 9p virtio transport Greg Kroah-Hartman
2018-09-07 21:54   ` Guenter Roeck
2018-09-07 21:56     ` Guenter Roeck
2018-09-07 21:09 ` [PATCH 4.9 04/63] spi: davinci: fix a NULL pointer dereference Greg Kroah-Hartman
2018-09-07 21:09 ` [PATCH 4.9 05/63] spi: spi-fsl-dspi: Fix imprecise abort on VF500 during probe Greg Kroah-Hartman
2018-09-07 21:09 ` [PATCH 4.9 06/63] drm/i915/userptr: reject zero user_size Greg Kroah-Hartman
2018-09-07 21:09 ` [PATCH 4.9 07/63] libertas: fix suspend and resume for SDIO connected cards Greg Kroah-Hartman
2018-09-07 21:09 ` [PATCH 4.9 08/63] mailbox: xgene-slimpro: Fix potential NULL pointer dereference Greg Kroah-Hartman
2018-09-07 21:09 ` Greg Kroah-Hartman [this message]
2018-09-07 21:09 ` [PATCH 4.9 10/63] powerpc/pseries: Fix endianness while restoring of r3 in MCE handler Greg Kroah-Hartman
2018-09-07 21:09 ` [PATCH 4.9 11/63] powerpc/powernv/pci: Work around races in PCI bridge enabling Greg Kroah-Hartman
2018-09-07 21:09 ` [PATCH 4.9 12/63] cxl: Fix wrong comparison in cxl_adapter_context_get() Greg Kroah-Hartman
2018-09-07 21:09 ` [PATCH 4.9 13/63] ib_srpt: Fix a use-after-free in srpt_close_ch() Greg Kroah-Hartman
2018-09-07 21:09 ` [PATCH 4.9 14/63] RDMA/rxe: Set wqe->status correctly if an unexpected response is received Greg Kroah-Hartman
2018-09-07 21:09 ` [PATCH 4.9 15/63] fs/9p/xattr.c: catch the error of p9_client_clunk when setting xattr failed Greg Kroah-Hartman
2018-09-07 21:09 ` [PATCH 4.9 16/63] 9p/virtio: fix off-by-one error in sg list bounds check Greg Kroah-Hartman
2018-09-07 21:09 ` [PATCH 4.9 17/63] net/9p/client.c: version pointer uninitialized Greg Kroah-Hartman
2018-09-07 21:09 ` [PATCH 4.9 18/63] net/9p/trans_fd.c: fix race-condition by flushing workqueue before the kfree() Greg Kroah-Hartman
2018-09-07 21:09 ` [PATCH 4.9 19/63] dm thin: stop no_space_timeout worker when switching to write-mode Greg Kroah-Hartman
2018-09-07 21:09 ` [PATCH 4.9 20/63] dm cache metadata: save in-core policy_hint_size to on-disk superblock Greg Kroah-Hartman
2018-09-07 21:09 ` [PATCH 4.9 22/63] iio: ad9523: Fix displayed phase Greg Kroah-Hartman
2018-09-07 21:09 ` [PATCH 4.9 23/63] iio: ad9523: Fix return value for ad952x_store() Greg Kroah-Hartman
2018-09-07 21:09 ` [PATCH 4.9 24/63] vmw_balloon: fix inflation of 64-bit GFNs Greg Kroah-Hartman
2018-09-07 21:09 ` [PATCH 4.9 25/63] vmw_balloon: do not use 2MB without batching Greg Kroah-Hartman
2018-09-07 21:09 ` [PATCH 4.9 26/63] vmw_balloon: VMCI_DOORBELL_SET does not check status Greg Kroah-Hartman
2018-09-07 21:09 ` [PATCH 4.9 27/63] vmw_balloon: fix VMCI use when balloon built into kernel Greg Kroah-Hartman
2018-09-07 21:09 ` [PATCH 4.9 28/63] rtc: omap: fix potential crash on power off Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 29/63] tracing: Do not call start/stop() functions when tracing_on does not change Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 30/63] tracing/blktrace: Fix to allow setting same value Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 31/63] uprobes: Use synchronize_rcu() not synchronize_sched() Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 32/63] mfd: hi655x: Fix regmap area declared size for hi655x Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 33/63] 9p: fix multiple NULL-pointer-dereferences Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 34/63] PM / sleep: wakeup: Fix build error caused by missing SRCU support Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 35/63] KVM: VMX: fixes for vmentry_l1d_flush module parameter Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 36/63] xtensa: limit offsets in __loop_cache_{all,page} Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 37/63] xtensa: increase ranges in ___invalidate_{i,d}cache_all Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 38/63] pnfs/blocklayout: off by one in bl_map_stripe() Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 39/63] NFSv4 client live hangs after live data migration recovery Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 40/63] ARM: tegra: Fix Tegra30 Cardhu PCA954x reset Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 41/63] Replace magic for trusting the secondary keyring with #define Greg Kroah-Hartman
2018-09-07 21:10   ` Greg Kroah-Hartman
2018-09-07 21:10   ` Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 42/63] Fix kexec forbidding kernels signed with keys in the secondary keyring to boot Greg Kroah-Hartman
2018-09-07 21:10   ` Greg Kroah-Hartman
2018-09-07 21:10   ` Greg Kroah-Hartman
2018-09-07 21:10   ` Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 43/63] mm/tlb: Remove tlb_remove_table() non-concurrent condition Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 44/63] iommu/vt-d: Add definitions for PFSID Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 45/63] iommu/vt-d: Fix dev iotlb pfsid use Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 46/63] osf_getdomainname(): use copy_to_user() Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 47/63] sys: dont hold uts_sem while accessing userspace memory Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 48/63] userns: move user access out of the mutex Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 49/63] ubifs: Fix memory leak in lprobs self-check Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 50/63] Revert "UBIFS: Fix potential integer overflow in allocation" Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 51/63] ubifs: Check data node size before truncate Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 52/63] ubifs: Fix synced_i_size calculation for xattr inodes Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 53/63] pwm: tiehrpwm: Fix disabling of output of PWMs Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 54/63] fb: fix lost console when the user unplugs a USB adapter Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 55/63] udlfb: set optimal write delay Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 56/63] getxattr: use correct xattr length Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 57/63] libnvdimm: fix ars_status output length calculation Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 58/63] printk/tracing: Do not trace printk_nmi_enter() Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 59/63] bcache: release dc->writeback_lock properly in bch_writeback_thread() Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 60/63] perf auxtrace: Fix queue resize Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 61/63] crypto: vmx - Fix sleep-in-atomic bugs Greg Kroah-Hartman
2018-09-07 21:10 ` [PATCH 4.9 63/63] fs/quota: Fix spectre gadget in do_quotactl Greg Kroah-Hartman
2018-09-08 21:14 ` [PATCH 4.9 00/63] 4.9.126-stable review Guenter Roeck
2018-09-09  8:55   ` Greg Kroah-Hartman
2018-09-09 13:33     ` Guenter Roeck
2018-09-09 18:04       ` Greg Kroah-Hartman
2018-09-09 15:54     ` Guenter Roeck
2018-09-09 18:03       ` Greg Kroah-Hartman
2018-09-09 20:53         ` Guenter Roeck
2018-09-10  6:35           ` Greg Kroah-Hartman
2018-09-10  6:54             ` Guenter Roeck
2018-09-10  7:03               ` Greg Kroah-Hartman
2018-09-09  4:30 ` Naresh Kamboju
2018-09-10 15:03 ` Shuah Khan

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=20180907211013.477016436@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=hbathini@linux.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mahesh@linux.vnet.ibm.com \
    --cc=mpe@ellerman.id.au \
    --cc=stable@vger.kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.