linux/block/partitions
INAGAKI Hiroshi bc2e07dfd2 block: fix and simplify blkdevparts= cmdline parsing
Fix the cmdline parsing of the "blkdevparts=" parameter using strsep(),
which makes the code simpler.

Before commit 146afeb235 ("block: use strscpy() to instead of
strncpy()"), we used a strncpy() to copy a block device name and partition
names. The commit simply replaced a strncpy() and NULL termination with
a strscpy(). It did not update calculations of length passed to strscpy().
While the length passed to strncpy() is just a length of valid characters
without NULL termination ('\0'), strscpy() takes it as a length of the
destination buffer, including a NULL termination.

Since the source buffer is not necessarily NULL terminated, the current
code copies "length - 1" characters and puts a NULL character in the
destination buffer. It replaces the last character with NULL and breaks
the parsing.

As an example, that buffer will be passed to parse_parts() and breaks
parsing sub-partitions due to the missing ')' at the end, like the
following.

example (Check Point V-80 & OpenWrt):

- Linux Kernel 6.6

  [    0.000000] Kernel command line: console=ttyS0,115200 earlycon=uart8250,mmio32,0xf0512000 crashkernel=30M mvpp2x.queue_mode=1 blkdevparts=mmcblk1:48M@10M(kernel-1),1M(dtb-1),720M(rootfs-1),48M(kernel-2),1M(dtb-2),720M(rootfs-2),300M(default_sw),650M(logs),1M(preset_cfg),1M(adsl),-(storage) maxcpus=4
  ...
  [    0.884016] mmc1: new HS200 MMC card at address 0001
  [    0.889951] mmcblk1: mmc1:0001 004GA0 3.69 GiB
  [    0.895043] cmdline partition format is invalid.
  [    0.895704]  mmcblk1: p1
  [    0.903447] mmcblk1boot0: mmc1:0001 004GA0 2.00 MiB
  [    0.908667] mmcblk1boot1: mmc1:0001 004GA0 2.00 MiB
  [    0.913765] mmcblk1rpmb: mmc1:0001 004GA0 512 KiB, chardev (248:0)

  1. "48M@10M(kernel-1),..." is passed to strscpy() with length=17
     from parse_parts()
  2. strscpy() returns -E2BIG and the destination buffer has
     "48M@10M(kernel-1\0"
  3. "48M@10M(kernel-1\0" is passed to parse_subpart()
  4. parse_subpart() fails to find ')' when parsing a partition name,
     and returns error

- Linux Kernel 6.1

  [    0.000000] Kernel command line: console=ttyS0,115200 earlycon=uart8250,mmio32,0xf0512000 crashkernel=30M mvpp2x.queue_mode=1 blkdevparts=mmcblk1:48M@10M(kernel-1),1M(dtb-1),720M(rootfs-1),48M(kernel-2),1M(dtb-2),720M(rootfs-2),300M(default_sw),650M(logs),1M(preset_cfg),1M(adsl),-(storage) maxcpus=4
  ...
  [    0.953142] mmc1: new HS200 MMC card at address 0001
  [    0.959114] mmcblk1: mmc1:0001 004GA0 3.69 GiB
  [    0.964259]  mmcblk1: p1(kernel-1) p2(dtb-1) p3(rootfs-1) p4(kernel-2) p5(dtb-2) 6(rootfs-2) p7(default_sw) p8(logs) p9(preset_cfg) p10(adsl) p11(storage)
  [    0.979174] mmcblk1boot0: mmc1:0001 004GA0 2.00 MiB
  [    0.984674] mmcblk1boot1: mmc1:0001 004GA0 2.00 MiB
  [    0.989926] mmcblk1rpmb: mmc1:0001 004GA0 512 KiB, chardev (248:0

By the way, strscpy() takes a length of destination buffer and it is
often confusing when copying characters with a specified length. Using
strsep() helps to separate the string by the specified character. Then,
we can use strscpy() naturally with the size of the destination buffer.

Separating the string on the fly is also useful to omit the redundant
string copy, reducing memory usage and improve the code readability.

Fixes: 146afeb235 ("block: use strscpy() to instead of strncpy()")
Suggested-by: Naohiro Aota <naota@elisp.net>
Signed-off-by: INAGAKI Hiroshi <musashino.open@gmail.com>
Reviewed-by: Daniel Golle <daniel@makrotopia.org>
Link: https://lore.kernel.org/r/20240421074005.565-1-musashino.open@gmail.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2024-05-03 09:57:53 -06:00
..
Kconfig block: move menu "Partition type" to block/partitions/Kconfig 2021-10-18 06:17:35 -06:00
Makefile block: merge partition-generic.c and check.c 2020-03-24 07:57:08 -06:00
acorn.c block/partitions/acorn: Remove redundant assignments 2022-04-23 07:15:26 -06:00
aix.c block: store a gendisk in struct parsed_partitions 2021-08-12 10:31:35 -06:00
amiga.c block/partition: fix signedness issue for Amiga partitions 2023-07-05 16:34:56 -06:00
atari.c block/partitions/atari: Remove redundant assignment 2022-04-23 07:15:26 -06:00
atari.h block: declare all partition detection routines in check.h 2020-03-24 07:57:08 -06:00
check.h block: Convert read_part_sector() to use a folio 2022-08-02 12:34:03 -04:00
cmdline.c block: fix and simplify blkdevparts= cmdline parsing 2024-05-03 09:57:53 -06:00
core.c block: add a disk_has_partscan helper 2024-05-03 08:59:59 -06:00
efi.c partitions/efi: use bdev_nr_bytes instead of open coding it 2021-10-19 06:16:50 -06:00
efi.h block: remove genhd.h 2022-02-02 07:49:59 -07:00
ibm.c partitions/ibm: Introduce defines for magic string length values 2023-10-04 08:04:08 -06:00
karma.c block: remove block/partitions/karma.h 2020-03-24 07:57:08 -06:00
ldm.c block/partitions/ldm: Remove redundant assignments 2022-04-23 07:15:26 -06:00
ldm.h block: remove genhd.h 2022-02-02 07:49:59 -07:00
mac.c block: partitions: only define function mac_fix_string for CONFIG_PPC_PMAC 2024-03-09 07:31:42 -07:00
mac.h block: declare all partition detection routines in check.h 2020-03-24 07:57:08 -06:00
msdos.c block: store a gendisk in struct parsed_partitions 2021-08-12 10:31:35 -06:00
osf.c block: remove block/partitions/osf.h 2020-03-24 07:57:08 -06:00
sgi.c block: store a gendisk in struct parsed_partitions 2021-08-12 10:31:35 -06:00
sun.c block: store a gendisk in struct parsed_partitions 2021-08-12 10:31:35 -06:00
sysv68.c block: declare all partition detection routines in check.h 2020-03-24 07:57:08 -06:00
ultrix.c block: declare all partition detection routines in check.h 2020-03-24 07:57:08 -06:00