Skip to content

uucore parse_size: arithmetic overflow (overflow-checks) on a large % (percent-of-memory) size #13736

Description

@leeewee

uucore's shared size parser (parse_size.rs) supports a % suffix meaning "this percentage of physical memory". It computes (value / 100) * total_physical_memory as an unchecked u128 multiply. A % value large enough to fit in u128 but whose product exceeds u128::MAX overflows, panicking with attempt to multiply with overflow under -C overflow-checks (exit 134);

// Special case: for percentage, just compute the given fraction
// of the total physical memory on the machine, if possible.
if unit == "%" {
let number: u128 = Self::parse_number(&numeric_string, 10, size)?;
return match total_physical_memory() {
Ok(total) => Ok((number / 100) * total),
Err(_) => Err(ParseSizeError::PhysicalMem(size.to_string())),
};

Because it is shared code with a wide-open default, the panic is reachable from many utils, like du.

$ mkdir -p /tmp/dutest

$ du --block-size=9223372036854775808000000000000% /tmp/dutest
thread 'main' panicked at src/uucore/src/lib/features/parser/parse_size.rs:258:33:
attempt to multiply with overflow
$ echo $?
134

$ DU_BLOCK_SIZE=9223372036854775808000000000000% du /tmp/dutest   # same panic via env

The same % value (N = 9223372036854775808000000000000%) panics at parse_size.rs:258:33 from every command below.

util command that triggers the panic GNU exit
du du --block-size=$N /tmp (also DU_BLOCK_SIZE=$N du /tmp) 0/1
sort printf 'b\na\n' > f; sort -S $N f 2
df df --block-size=$N 1
ls ls --block-size=$N /tmp 2
split printf 'x\n' > f; split -b $N f 1
stdbuf stdbuf -o$N true 125
od printf 'x' > f; od -N$N f 1
shred printf 'x' > f; shred --size=$N f 1
dd dd bs=$N (also count=$N, ibs=/obs=/seek=/skip=) 1
head printf 'x' > f; head -c $N f 1
tail printf 'x' > f; tail -c $N f 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions