Skip to content

Guard get_default_if_name against empty or unparseable /proc/net/route#17

Open
acts-1631 wants to merge 1 commit into
karlkleinpaste:masterfrom
acts-1631:fix/get-default-if-name-uninit
Open

Guard get_default_if_name against empty or unparseable /proc/net/route#17
acts-1631 wants to merge 1 commit into
karlkleinpaste:masterfrom
acts-1631:fix/get-default-if-name-uninit

Conversation

@acts-1631

Copy link
Copy Markdown

get_default_if_name reads /proc/net/route looking for the default
route. The fallback block that runs when no default route is found
dereferences the uninitialized 'field' pointer and copies the
uninitialized 'line' buffer into the caller's 16-byte destination.

Two failure modes exist:

  • An empty /proc/net/route leaves both 'field' and 'line' entirely
    uninitialized; *field writes to an arbitrary stack location and
    strcpy(name, line) overflows the 16-byte destination.
  • A file with lines but no tabs leaves 'field' NULL from the last
    strchr call; *field = '\0' is a null pointer dereference.

This initializes field to NULL and line[0] to '\0' before the loop,
guards the fallback block so it only runs when a valid last line was
seen, and sets name[0] = '\0' when the guard fails so the caller
InterfaceAddress() keeps its 127.0.0.1 fallback instead of reading
the uninitialized gw_if buffer.

get_default_if_name reads /proc/net/route looking for the default
route entry. The fallback block that runs when no default route is
found dereferences the uninitialized 'field' pointer and copies the
uninitialized 'line' buffer into the caller's 16-byte destination.

Two failure modes exist:
- An empty /proc/net/route leaves both 'field' and 'line' entirely
  uninitialized; *field writes to an arbitrary stack location and
  strcpy(name, line) overflows the 16-byte destination.
- A file with lines but no tabs leaves 'field' NULL from the last
  strchr call; *field = '\0' is a null pointer dereference (crash).

Initialize field to NULL and line[0] to '\0' before the loop. Guard
the fallback block so it only runs when a valid last line was seen.
When the guard fails, set name[0] = '\0' so the caller
InterfaceAddress() keeps its 127.0.0.1 fallback instead of reading
the uninitialized gw_if buffer and entering the getifaddrs block
with a garbage interface name.
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.

1 participant