Skip to content

FileOpen + InitInterProcessComm: bound user-input string parsing#192

Open
nurdymuny wants to merge 2 commits into
ericstoneking:masterfrom
nurdymuny:fix/input-parser-overflows
Open

FileOpen + InitInterProcessComm: bound user-input string parsing#192
nurdymuny wants to merge 2 commits into
ericstoneking:masterfrom
nurdymuny:fix/input-parser-overflows

Conversation

@nurdymuny

Copy link
Copy Markdown

Summary

Two memory-safety fixes on 42's config-file ingest paths. Closes #191.

1. FileOpenstrcpy+strcatsnprintf with truncation check

Kit/Source/iokit.c::FileOpen builds the full path by strcpy(FileName, Path); strcat(FileName, File) into a fixed 1024-byte stack buffer. A long working directory + long config filename overflows that buffer. FileOpen is on every config-file load path in 42, so this is broadly reachable.

Switched to snprintf(FileName, sizeof(FileName), "%s%s", Path, File) and added a truncation check that aborts with a clear error if the combined length exceeds 1023 bytes.

2. InitInterProcessComm — width specifiers on every fscanf %s / %[^...]

Source/42ipc.c::InitInterProcessComm reads Inp_IPC.txt with five fscanf calls, every %s / %[^...] is unbounded. A malformed/long field in the config file overflows the receiving buffer (HostName[40], response[120], FileName[80], Prefix[80]).

Added explicit width specifiers (%119s, %39s, %79[^"], %119[^\n], %1[\n]) on every conversion so they truncate at the receiving-buffer size minus the NUL terminator instead of overflowing.

Diff

 Kit/Source/iokit.c | 16 +++++++++++++---
 Source/42ipc.c     | 14 +++++++-------
 2 files changed, 20 insertions(+), 10 deletions(-)

Tests

  • Manual: hand-built an Inp_IPC.txt with a 60-byte hostname; before the fix that overflows I->HostName[40] and trips stack-protector; after the fix the field is truncated to 39 bytes and processing continues.
  • Existing tests should be unaffected — width specifiers larger than every realistic input.

Scope

I kept this PR to the IPC entry-path so the review is small. The same unbounded fscanf %s pattern recurs in Source/42init.c (LoadSC) and Source/42gl.c (POV / Map config parsers) — happy to extend the same fix shape to those if you want a broader sweep in a follow-up.

Linked

Closes #191.

nurdymuny added 2 commits June 9, 2026 06:41
Two memory-safety fixes in the config-file ingest paths:

1. Kit/Source/iokit.c::FileOpen
   strcpy + strcat into a 1024-byte fixed buffer with no bound check
   on the inputs. A long working directory + long config filename
   passed through the same FileOpen() call overflows FileName[1024]
   on the stack. Replaced with snprintf + truncation check.

2. Source/42ipc.c::InitInterProcessComm
   Five fscanf calls used the unbounded '%s' / '%[^...]' conversion
   into fixed-size buffers (HostName[40], response[120], FileName[80],
   Prefix[80]). A hand-written or auto-generated Inp_IPC.txt with a
   field longer than the buffer overflows the stack frame. Added
   explicit width specifiers to every %s / %[^...] specifier so the
   conversion truncates instead of overrunning.

These are the two functions on the IPC-config entry path; the same
pattern recurs in 42init.c (LoadSC) and 42gl.c (Map / POV config
parsers). I left those for a follow-up if you want this scope of
change applied more broadly — happy to extend the PR.
…Open

scj-hunt's per-target catalog flagged these three OpenFile copies at
score 10.0 — same strcpy+strcat-into-FileName[80] pattern as
iokit.c::FileOpen (FileName[1024]). Even smaller buffer makes the
overflow easier to trigger from a long Path or File argument.

All three converted to snprintf with the same truncation-detection
shape used in iokit.c. The three tools (Mercator/DEM/Albedo cube
converters) share the same OpenFile helper because of copy-paste;
ideally they would refactor to call the canonical iokit.c::FileOpen,
but that's an architectural change out of scope here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stack buffer overflows in FileOpen and InitInterProcessComm config parsing

1 participant