Skip to content

{bp-19444} libs/libc: Fix divide-by-zero in stat() with large filesystem block sizes - #19622

Merged
xiaoxiang781216 merged 4 commits into
apache:releases/13.0from
jerpelea:bp-19444
Aug 3, 2026
Merged

{bp-19444} libs/libc: Fix divide-by-zero in stat() with large filesystem block sizes#19622
xiaoxiang781216 merged 4 commits into
apache:releases/13.0from
jerpelea:bp-19444

Conversation

@jerpelea

@jerpelea jerpelea commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Summary

This change fixes a divide-by-zero in stat() caused by overflow of st_blksize when a filesystem reports a block size larger than can be represented by blksize_t. As reported in #19432, this can occur with LittleFS configurations using large block sizes, where blksize_t is currently defined as int16_t. When the filesystem block size exceeds the range of int16_t, st_blksize is truncated to zero, causing an integer divide-by-zero when st_blocks is calculated.

This patch widens blksize_t from int16_t to int32_t in include/sys/types.h and updates the corresponding nuttx_blksize_t definition in include/nuttx/fs/hostfs.h so the two definitions remain consistent.

Fixes #19432

Includes:
#19179

Impact

RELEASE

Testing

CI

xiaoxiang781216 and others added 4 commits August 3, 2026 10:25
Move uid_t/gid_t out of the CONFIG_SMALL_MEMORY #ifdef so they are
always defined as unsigned int regardless of SMALL_MEMORY.

Update include/nuttx/fs/hostfs.h to match: drop the int16_t variants
of nuttx_gid_t/nuttx_uid_t and keep a single unsigned int definition
so the hostfs RPC ABI stays in sync with sys/types.h.

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
A 16-bit ino_t can only address 65536 distinct file serial numbers,
which is not enough for filesystems with large directory trees and
breaks portable software (e.g. dropbear's scp) that expects a wider
inode number space.  Widen ino_t (and nuttx_ino_t in the hostfs ABI)
to uint32_t to match common POSIX practice.

Update fs/rpmsgfs/rpmsgfs.h accordingly: promote the 'ino' field in
struct rpmsgfs_stat_priv_s from uint16_t to uint32_t and move 'nlink'
into the trailing 16-bit slot previously occupied by the reserved
field, keeping the overall packed-struct layout/size unchanged.

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
Add the POSIX d_ino (file serial number) member to struct dirent and
populate it on every readdir() path, so portable callers (e.g. scp in
dropbear) that read dp->d_ino observe a meaningful, non-zero inode
number:

  - include/dirent.h: declare d_ino in struct dirent and drop the
    outdated comment claiming the field is unimplemented.
  - include/nuttx/fs/hostfs.h: add d_ino to struct nuttx_dirent_s so
    the hostfs ABI can carry the inode number across the VFS boundary.
  - arch/sim/src/sim/posix/sim_hostfs.c: forward the host's
    ent->d_ino into entry->d_ino.
  - fs/vfs/fs_dir.c (read_pseudodir): copy the in-memory inode's
    i_ino into entry->d_ino for the pseudo filesystem.
  - fs/yaffs/yaffs_vfs.c: forward yaffs's dirent->d_ino into
    entry->d_ino.
  - fs/rpmsgfs: extend struct rpmsgfs_readdir_s with an 'ino' field
    and propagate it across the RPC in both rpmsgfs_server (fills it
    from the underlying entry) and rpmsgfs_client (writes it back to
    the caller's nuttx_dirent_s).

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
…izes

blksize_t is currently defined as int16_t, which overflows when a
filesystem reports a block size larger than 32767 bytes. This causes
st_blksize to become zero, leading to an integer divide-by-zero when
st_blocks is calculated in stat().

Widen blksize_t to int32_t to support larger filesystem block sizes.

Update nuttx_blksize_t in include/nuttx/fs/hostfs.h to keep it
consistent with include/sys/types.h.

struct geometry.geo_sectorsize (include/nuttx/fs/ioctl.h) is also
typed blksize_t, so every debug print of that field using a 16-bit
format specifier is updated to PRId32 to match the new width:
drivers/misc/ramdisk.c, drivers/mmcsd/mmcsd_spi.c, drivers/mtd/ftl.c,
fs/driver/fs_blockmerge.c, drivers/mtd/smart.c,
drivers/usbhost/usbhost_storage.c, drivers/mmcsd/mmcsd_sdio.c,
arch/arm/src/s32k1xx/s32k1xx_eeeprom.c,
arch/arm/src/lc823450/lc823450_mmcl.c.

Signed-off-by: Ansh Rai <anshrai331@gmail.com>
Signed-off-by: root <root@LAPTOP-9C7LKDC5.localdomain>
@github-actions github-actions Bot added Arch: arm Issues related to ARM (32-bit) architecture Arch: simulator Issues related to the SIMulator Area: Drivers Drivers issues Size: S The size of the change in this PR is small labels Aug 3, 2026
@xiaoxiang781216
xiaoxiang781216 merged commit 5b3b4f3 into apache:releases/13.0 Aug 3, 2026
17 of 41 checks passed
@jerpelea
jerpelea deleted the bp-19444 branch August 3, 2026 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: arm Issues related to ARM (32-bit) architecture Arch: simulator Issues related to the SIMulator Area: Drivers Drivers issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants