linux/drivers/mmc/core
Shawn Lin 3e0483e93a mmc: core: Add validation for host-provided max_segs
The max_segs field is of type unsigned short, and if a host driver
sets an excessively large value, it may be truncated to zero. This
can cause mmc_alloc_sg() to call kmalloc_objs() with a zero size
allocation request, which leads to undefined behavior.

Under the SLUB allocator, kmalloc(0) returns a special pointer
(ZERO_SIZE_PTR). The subsequent 'if (sg)' check will evaluate to
true, and sg_init_table() will then attempt to access invalid memory,
resulting in a crash:

dwmmc_rockchip 2a310000.mmc: Successfully tuned phase to 133
mmc1: new UHS-I speed SDR104 SDHC card at address aaaa
Unable to handle kernel paging request at virtual address 0000001ffffffff0
Mem abort info:
ESR = 0x0000000096000004
EC = 0x25: DABT (current EL), IL = 32 bits
SET = 0, FnV = 0
EA = 0, S1PTW = 0
FSC = 0x04: level 0 translation fault
Data abort info:
ISV = 0, ISS = 0x00000004, ISS2 = 0x00000000
CM = 0, WnR = 0, TnD = 0, TagAccess = 0
GCS = 0, Overlay = 0, DirtyBit = 0, Xs = 0
user pgtable: 4k pages, 48-bit VAs, pgdp=0000000102c88000
[0000001ffffffff0] pgd=0000000000000000, p4d=0000000000000000
Internal error: Oops: 0000000096000004 [#1] SMP
Modules linked in:
CPU: 2 UID: 0 PID: 102 Comm: kworker/2:1 Not tainted 7.0.0-rc6-next-20260331-00013-g4d93c25963c5-dirty #80 PREEMPT
Hardware name: Rockchip RK3576 EVB V10 Board (DT)
Workqueue: events_freezable mmc_rescan
pstate: 80000005 (Nzcv daif -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
pc : sg_init_table+0x2c/0x50
lr : sg_init_table+0x24/0x50
sp : ffff8000837db710
x29: ffff8000837db710 x28: 000000000000c000 x27: 0000000000000300
x26: 0000000000000000 x25: 0000000000000040 x24: ffff0000c46a0000
x23: 0000000000000000 x22: ffff0000c0c73c00 x21: 0000000000000010
x20: 0000000000000010 x19: 0000000000000000 x18: 000000000000002c
x17: 0000000000000000 x16: 0000000000000001 x15: 0000000000000000
x14: 0000000000000400 x13: ffff8000837dc000 x12: 0000000000000000
x11: ffff0000c0c73ca0 x10: 0000000000000040 x9 : 459ec1f0abbdbb00
x8 : 0000001fffffffe0 x7 : 0000000000000000 x6 : 000000000000003f
x5 : 0000000000035579 x4 : 0000000000000901 x3 : 0000000000000000
x2 : 0000000000000000 x1 : 0000000000000000 x0 : 0000000000000010
Call trace:
sg_init_table+0x2c/0x50 (P)
mmc_mq_init_request+0x64/0x90
blk_mq_alloc_map_and_rqs+0x3ac/0x480
blk_mq_alloc_set_map_and_rqs+0x98/0x1e0
blk_mq_alloc_tag_set+0x1c0/0x290
mmc_init_queue+0x120/0x370
mmc_blk_alloc_req+0x150/0x420

To prevent this, add a validation check in mmc_mq_init_request() to
detect when sg_len (derived from max_segs) is zero. If sg_len is zero,
we return an error and print an error message, allowing host driver
developers to identify and fix incorrect max_segs configuration.

This is a defensive measure that ensures the MMC core fails gracefully
when host drivers provide invalid max_segs values, rather than crashing
with a page fault.

Signed-off-by: Shawn Lin <shawn.lin@rock-chips.com>
Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>
History 2026-05-11 15:28:26 +02:00
..
block.c mmc: block: Use MQRQ_XFER_SINGLE_BLOCK for both read and write recovery 2026-03-31 12:41:32 +02:00
block.h mmc: block: Remove code no longer needed after the switch to blk-mq 2017-12-11 13:02:22 +01:00
bus.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
bus.h mmc: use octal file permissions instead of symbolic 2025-10-21 13:37:38 +02:00
card.h mmc: core: Optimize time for secure erase/trim for some Kingston eMMCs 2026-02-23 12:06:55 +01:00
core.c mmc: core: Replace the hard-coded shift value 9 with SECTOR_SHIFT 2026-03-31 12:41:31 +02:00
core.h mmc: core: Add infrastructure for undervoltage handling 2025-08-22 12:08:07 +02:00
crypto.c blk-crypto: rename blk_keyslot_manager to blk_crypto_profile 2021-10-21 10:49:32 -06:00
crypto.h mmc: core: Add basic support for inline encryption 2021-02-01 12:02:33 +01:00
debugfs.c mmc: core: Allow more host caps to be modified through debugfs 2025-11-11 17:59:17 +01:00
host.c mmc: core: Switch to use pm_ptr() for mmc_host_class_dev_pm_ops 2026-03-27 10:25:16 +01:00
host.h mmc: core: Remove checking MMC_CAP_4_BIT_DATA from mmc_host_can_uhs() 2026-03-16 15:46:58 +01:00
Kconfig mmc: block: add RPMB dependency 2024-09-03 14:34:44 +02:00
Makefile mmc: core: Prepare to support SD UHS-II cards 2024-10-14 12:40:10 +02:00
mmc.c mmc: core: Add quirk for incorrect manufacturing date 2026-02-23 12:06:52 +01:00
mmc_ops.c mmc: core: add mmc_read_tuning 2025-08-19 14:34:15 +02:00
mmc_ops.h mmc: rename mmc_can_ext_csd() to mmc_card_can_ext_csd() 2025-05-14 16:59:17 +02:00
mmc_test.c mmc: mmc_test: Replace hard-coded values with macros and consolidate test parameters 2026-03-31 12:41:31 +02:00
pwrseq.c mmc: pwrseq: Use device_match_of_node() 2022-12-07 13:22:37 +01:00
pwrseq.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 194 2019-05-30 11:29:22 -07:00
pwrseq_emmc.c mmc: Switch back to struct platform_driver::remove() 2024-10-14 12:29:01 +02:00
pwrseq_sd8787.c mmc: Switch back to struct platform_driver::remove() 2024-10-14 12:29:01 +02:00
pwrseq_simple.c mmc: pwrseq_simple: use gpiod_multi_set_value_cansleep 2025-02-17 14:27:34 +01:00
queue.c mmc: core: Add validation for host-provided max_segs 2026-05-11 15:28:26 +02:00
queue.h mmc: core: Optimize size of struct mmc_queue_req 2026-04-09 17:49:54 +02:00
quirks.h mmc: core: Simplify with scoped for each OF child loop 2026-04-09 17:51:21 +02:00
regulator.c mmc: core: Add infrastructure for undervoltage handling 2025-08-22 12:08:07 +02:00
sd.c mmc: core: Skip to set the default 200mA SD current limit 2025-10-23 16:07:21 +02:00
sd.h mmc: core: Support UHS-II card control and access 2024-10-24 14:22:24 +02:00
sd_ops.c mmc: core: Support UHS-II card control and access 2024-10-24 14:22:24 +02:00
sd_ops.h mmc: core: Support UHS-II card control and access 2024-10-24 14:22:24 +02:00
sd_uhs2.c mmc: core: Adjust some error messages for SD UHS-II cards 2025-06-24 12:38:02 +02:00
sdio.c mmc: core: SPI mode remove cmd7 2025-08-19 14:33:37 +02:00
sdio_bus.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
sdio_bus.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152 2019-05-30 11:26:32 -07:00
sdio_cis.c mmc: sdio: fix possible resource leaks in some error paths 2023-02-14 00:06:22 +01:00
sdio_cis.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152 2019-05-30 11:26:32 -07:00
sdio_io.c mmc: sdio: Use min3() to simplify sdio_set_block_size() 2026-03-09 14:24:35 +01:00
sdio_irq.c mmc: core: Switch to basic workqueue API for sdio_irq_work 2022-09-14 11:53:47 +02:00
sdio_ops.c mmc: sdio: Use mmc_pre_req() / mmc_post_req() 2020-09-07 08:57:44 +02:00
sdio_ops.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152 2019-05-30 11:26:32 -07:00
sdio_uart.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
slot-gpio.c mmc: rename mmc_can_gpio_ro() to mmc_host_can_gpio_ro() 2025-05-14 16:59:17 +02:00
slot-gpio.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 194 2019-05-30 11:29:22 -07:00