Skip to content

Commit 75e554a

Browse files
iop/devfs: fix off-by-one buffer overflow in devfs_ioctl2 GETDESC
## Description In `devfs_ioctl2()`, the `DEVFS_IOCTL_GETDESC` handler copies the device description with `strncpy(buf, dev->node.desc, buflen - 1)` and then tries to NUL-terminate the result with: *(((u8 *)buf + buflen)) = 0; This writes at `buf[buflen]`, which is one byte past the end of the caller-provided buffer (valid indices are `0 .. buflen - 1`). It also leaves `buf[buflen - 1]` unterminated when the description is longer than `buflen - 1` characters, so the following `strlen(buf)` can read past the buffer. The terminator must be written at `buf[buflen - 1]`, which is the last byte of the buffer and the one `strncpy` was told to leave free. ## Changes - `iop/fs/devfs/src/devfs.c`: NUL-terminate at `buflen - 1` instead of `buflen`. ## Impact - Fixes a 1-byte out-of-bounds write on the IOP (memory corruption of whatever follows the caller's buffer). - Fixes a possible unterminated string being passed to `strlen()`. - No behavior change for valid use otherwise; the return value is still `strlen(buf)`.
1 parent f1ca58a commit 75e554a

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

‎iop/fs/devfs/src/devfs.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ int devfs_ioctl2(iop_file_t *file, int cmd, void *args, unsigned int arglen, voi
600600
if(dev->node.desc)
601601
{
602602
strncpy(buf, dev->node.desc, buflen-1);
603-
*(((u8 *)buf + buflen)) = 0;
603+
((char *)buf)[buflen - 1] = '\0';
604604
return strlen(buf);
605605
}
606606

0 commit comments

Comments
 (0)