diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-28 00:31:36 -0600 |
|---|---|---|
| committer | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-28 00:31:36 -0600 |
| commit | 447151dabfbad0a41c8f049938042cd886d9f451 (patch) | |
| tree | 1205f62a2d8f7bd34f3030b7715184cd622c362e | |
| parent | 56bd0e6c2ab87ab140cef35882cff52153f36b7c (diff) | |
| download | zash-447151dabfbad0a41c8f049938042cd886d9f451.tar.gz | |
fix(ulimit): refuse a limit that does not fit rlim_t
`ulimit -s 18014398509481984` (2^54 kbytes = 2^64 bytes) multiplied a
64-bit value by 1024 and wrapped to 0: the command succeeded and set the
stack limit to zero. It is now "limit out of range" and status 1, and so
is a value that would come out exactly RLIM_INFINITY without the user
writing "unlimited".
The `ulimit -a` label was copied into a 64-byte buffer for no reason;
"% -20s" gets the name straight from the table. And if a build has no
RLIMIT_FSIZE entry there is no default resource at all, so say that
instead of dereferencing a null pointer.
Test: tests/zash/ulimit.sh, in a subshell so a broken build cannot take
the test shell down with it. Verified on Linux: before the change the
command exits 0 and `ulimit -s` reports 0; after it, the warning and
status 1.
| -rw-r--r-- | src/builtin_ext.c | 23 | ||||
| -rw-r--r-- | tests/zash/ulimit.exp | 2 | ||||
| -rw-r--r-- | tests/zash/ulimit.sh | 4 |
3 files changed, 21 insertions, 8 deletions
diff --git a/src/builtin_ext.c b/src/builtin_ext.c index 2f0271a..8983e5a 100644 --- a/src/builtin_ext.c +++ b/src/builtin_ext.c @@ -377,16 +377,12 @@ int builtin_ulimit(int argc, char **argv) { } if (all) { for (size_t k = 0; k < ARRAY_LEN(limits); k++) { - char label[64]; const limit *e = &limits[k]; - size_t n = 0; - for (const char *s = e->name; *s; s++) - label[n++] = *s; - label[n] = '\0'; if (e->unit) - out_printf(&out1, "%-20s (%s, -%c) ", label, e->unit, e->opt); + out_printf(&out1, "%-20s (%s, -%c) ", e->name, e->unit, + e->opt); else - out_printf(&out1, "%-20s (-%c) ", label, e->opt); + out_printf(&out1, "%-20s (-%c) ", e->name, e->opt); limit_print(e, hard && !soft); } return 0; @@ -397,6 +393,11 @@ int builtin_ulimit(int argc, char **argv) { l = &limits[k]; } } + if (!l) { + /* Built without RLIMIT_FSIZE: there is no default resource. */ + sh_warn("ulimit: -f: not supported here"); + return 1; + } if (i >= argc) { limit_print(l, hard && !soft); return 0; @@ -417,7 +418,13 @@ int builtin_ulimit(int argc, char **argv) { sh_warn("ulimit: %s: bad limit", argv[i]); return 2; } - v = (rlim_t)n * l->scale; + /* n * scale may not wrap, and may not quietly mean "unlimited". */ + if (n > RLIM_INFINITY / l->scale || + n * l->scale == RLIM_INFINITY) { + sh_warn("ulimit: %s: limit out of range", argv[i]); + return 1; + } + v = (rlim_t)n * (rlim_t)l->scale; } if (hard || !soft) rl.rlim_max = v; diff --git a/tests/zash/ulimit.exp b/tests/zash/ulimit.exp index 75ed7d8..11fbc2f 100644 --- a/tests/zash/ulimit.exp +++ b/tests/zash/ulimit.exp @@ -9,4 +9,6 @@ same status=2 status=2 0 +status=1 +ulimit: 18014398509481984: limit out of range [status 0] diff --git a/tests/zash/ulimit.sh b/tests/zash/ulimit.sh index a00c2f6..a335e37 100644 --- a/tests/zash/ulimit.sh +++ b/tests/zash/ulimit.sh @@ -9,3 +9,7 @@ ulimit -a | grep -c -- '-n)' ulimit -Z 2>/dev/null; echo "status=$?" ulimit -n abc 2>/dev/null; echo "status=$?" (ulimit -c 0; ulimit -c) +# A limit whose byte count does not fit rlim_t is refused, not wrapped to +# zero: 2^54 kbytes is 2^64 bytes, and it used to become a 0-byte stack. +(ulimit -s 18014398509481984) 2>err; echo "status=$?" +sed 's/^[^:]*: line [0-9]*: //' err |
