Skip to content

fsutils/mkgpt: fix exit status, GPT geometry, GUID and input validation - #4289

Open
JianyuWang0623 wants to merge 8 commits into
apache:masterfrom
JianyuWang0623:mkgpt-fixes
Open

JianyuWang0623 wants to merge 8 commits into
apache:masterfrom
JianyuWang0623:mkgpt-fixes

Conversation

@JianyuWang0623

@JianyuWang0623 JianyuWang0623 commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This series fixes eight bugs in fsutils/mkgpt: a successful write always
exited 255, the last usable LBA overlapped the backup entry array, =linux
selected the Microsoft Basic Data GUID instead of the Linux filesystem data
one, a full partition table wrote one entry past the 128-entry array, a
failed fsync() was reported as success, over-long and duplicate partition
names were silently accepted, size suffix parsing accepted trailing garbage
and could wrap, and disks too small for both GPT copies underflowed the size
math.

Impact

  • Exit status and durability: mkgpt write exits 0 on success (previously
    always 255) and fails when fsync() errors (previously reported success).
  • Written table content: the last usable LBA is now sz-34 and :0 fill
    partitions end at it (previously sz-33, overlapping the backup entry
    array), and =linux writes the Linux filesystem data GUID
    0FC63DAF-8483-4772-8E79-3D69D8477DE4 (previously the Microsoft Basic Data
    GUID EBD0A0A2-B9E5-4433-87C0-68B6B72699C7).
  • Input validation is stricter: names longer than 36 characters, duplicate
    names, malformed or overflowing size suffixes, a 129th partition entry and
    disks smaller than 68 sectors are now rejected with an error instead of
    silently writing a corrupt table.

Testing

Build Host: Ubuntu 22.04 x86_64, gcc (Ubuntu 13.4.0-6ubuntu122ppa2) 13.4.0;

Target: sim:nsh (x86_64) with CONFIG_FSUTILS_MKGPT=y and
CONFIG_FS_TMPFS=y, run natively on the build host. Commands were fed on
the sim console (./nuttx reading the session from stdin).

Write and read back a 256 KiB (512 sector) image with a :0 fill partition,
then check the exit status (before: mkgpt write returned 255 even on
success):

nsh> truncate -s 262144 /tmp/disk
nsh> mkgpt write /tmp/disk a:16k b:16k c:0=linux
ptn  start block   end block     name
---- ------------- ------------- --------------------
#001            34            65 a
#002            66            97 b
#003            98           478 c
nsh> echo $?
0
nsh> mkgpt read /tmp/disk
ptn  start block   end block     name
---- ------------- ------------- --------------------
#001            34            65 a
#002            66            97 b
#003            98           478 c
nsh> echo $?
0

The fill partition ends at LBA 478 = 512 - 34, the last usable LBA before
the 32 backup entry LBAs and the backup header. The c partition entry
type GUID (bytes at offset 1280 of the image, little-endian mixed encoding):

nsh> dd if=/tmp/disk of=/tmp/slice bs=1 skip=1280 count=16
16 bytes (16 blocks) copied, 174 usec, 89 KB/s
nsh> hexdump /tmp/slice
/tmp/slice at 00000000:
0000: af 3d c6 0f 83 84 72 47 8e 79 3d 69 d8 47 7d e4 .=....rG.y=i.G}.

Rejected inputs (before: silently accepted or corrupt output):

nsh> mkgpt write /tmp/disk a:16k a:16k
duplicate partition name: a
nsh> echo $?
255
nsh> mkgpt write /tmp/disk aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:16k
partition name too long: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
nsh> echo $?
255
nsh> mkgpt write /tmp/disk a:16x
zero size partitions not allowed
nsh> echo $?
255
nsh> mkgpt write /tmp/disk a:512x
zero size partitions not allowed
nsh> echo $?
255
nsh> mkgpt write /tmp/disk a:18446744073709551615g
zero size partitions not allowed
nsh> echo $?
255
nsh> truncate -s 34304 /tmp/small
nsh> mkgpt write /tmp/small a:16k
error: disk too small for the GPT
nsh> echo $?
255

/tmp/list with 129 entries (p001:512 through p129:512) is rejected at
the 129th entry instead of writing past the 128-entry array:

nsh> mkgpt write /tmp/disk @/tmp/list
out of partition table entries
nsh> echo $?
255

JianyuWang0623 and others added 8 commits December 3, 2026 18:38
The write path returned -1 even after both tables were written, so the
command always exited with 255.  Set ret to 0 on the success path.

Co-authored-by: multica-agent <github@multica.ai>
Assisted-by: OpenCode:mimo-v2.6-pro
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
The header counted the backup entry area as usable (sz-33) and a fill
entry stopped one LBA short of the end.  Use sz-34 and end :0 fills at
the last usable LBA.

Co-authored-by: multica-agent <github@multica.ai>
Assisted-by: OpenCode:mimo-v2.6-pro
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
=linux selected the Microsoft Basic Data type instead of the Linux
filesystem data type 0FC63DAF-8483-4772-8E79-3D69D8477DE4.

Co-authored-by: multica-agent <github@multica.ai>
Assisted-by: OpenCode:mimo-v2.6-pro
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
The upper bound compared with >, so a full table still wrote entry[128]
past the end of the array.  Use >= so at most 128 entries are accepted.

Co-authored-by: multica-agent <github@multica.ai>
Assisted-by: OpenCode:mimo-v2.6-pro
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
A failed flush was reported as success, so callers could proceed while
the tables are not durable.  Propagate the fsync() error instead.

Co-authored-by: multica-agent <github@multica.ai>
Assisted-by: OpenCode:mimo-v2.6-pro
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
Long names were silently truncated and duplicates were accepted, so two
entries could share one GPT name.  Reject both in add_ptn(), which every
parser path goes through.

Co-authored-by: multica-agent <github@multica.ai>
Assisted-by: OpenCode:mimo-v2.6-pro
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
parse_size() ignored the strtoull end pointer and could overflow the
unit multiply, so trailing garbage was accepted and huge values wrapped.
Require full token consumption and reject overflow.

Co-authored-by: multica-agent <github@multica.ai>
Assisted-by: OpenCode:mimo-v2.6-pro
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
When the disk cannot hold both GPT copies there is no usable area and
the size math underflows; reject disks smaller than 68 sectors.

Co-authored-by: multica-agent <github@multica.ai>
Assisted-by: OpenCode:mimo-v2.6-pro
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
@JianyuWang0623
JianyuWang0623 marked this pull request as ready for review December 4, 2026 03:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants