shikhabhadoria opened a new issue, #22566:
URL: https://github.com/apache/nuttx/issues/22566
### Description / Steps to reproduce the issue
### Summary
`procfs_sprintf()` in `fs/procfs/fs_procfsutil.c` stores the `vsnprintf()`
result in a `size_t` and then caps it with `MIN(linesize, LINEBUF_SIZE - 1)`:
```c
void procfs_sprintf(FAR char *buf, size_t size, FAR off_t *offset,
FAR const IPTR char *format, ...)
{
char linebuf[LINEBUF_SIZE];
size_t linesize;
size_t copysize;
va_list ap;
va_start(ap, format);
linesize = vsnprintf(linebuf, LINEBUF_SIZE, format, ap);
va_end(ap);
linesize = MIN(linesize, LINEBUF_SIZE - 1);
...
}
```
### Root Cause
1. `vsnprintf()` returns `int`. A failure is a negative value (typically
`-1`).
2. Assigning that to `size_t linesize` converts `-1` to `SIZE_MAX`.
3. `MIN(SIZE_MAX, LINEBUF_SIZE - 1)` is `127`, so the function copies 127
bytes from `linebuf`.
4. On failure `vsnprintf()` does not promise that `linebuf` is fully
initialized, so callers can publish uninitialized stack contents through procfs.
This is the leftover sibling of #22550, which fixed the same promotion in
`procfs_snprintf()`. `procfs_sprintf()` still has it.
### Steps to Reproduce
1. Call `procfs_sprintf()` with a format that makes `vsnprintf()` return
`-1` (encoding error / invalid conversion on the C library in use), or inject
`n = -1` into the same assignment and `MIN` sequence.
2. Observe `linesize` become `LINEBUF_SIZE - 1` instead of 0.
3. The following `memcpy` copies 127 bytes from `linebuf`.
Host check of the current expression (gcc 13.3, Ubuntu 24.04):
```
old n=-1: MIN((size_t)-1, 127) = 127
```
### Expected Behavior
If `vsnprintf()` fails (`n < 0`), `procfs_sprintf()` must not copy from
`linebuf`. Treat the line length as 0 and return without `memcpy`.
### Proposed Fix
Keep the formatted length in an `int`, return immediately on `n < 0`, then
cap with `MIN((size_t)n, LINEBUF_SIZE - 1)`.
### On which OS does this issue occur?
[OS: Linux]
### What is the version of your OS?
Ubuntu 24.04
### NuttX Version
master
### Issue Architecture
[Arch: all]
### Issue Area
[Area: File System]
### Host information
Generic architecture-independent C defect in fs/procfs/fs_procfsutil.c.
Reproduced the integer promotion with a host C program.
### Verification
- [x] I have verified before submitting the report.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]