Skip to content

event_select: don't leave freed fd sets in the event base - #613

Merged
millert merged 1 commit into
sudo-project:mainfrom
iefa-m:event-select-fdset-realloc
Dec 6, 2026
Merged

millert merged 1 commit into
sudo-project:mainfrom
iefa-m:event-select-fdset-realloc

Conversation

@iefa-m

@iefa-m iefa-m commented Dec 6, 2026

Copy link
Copy Markdown
Contributor

sudo_ev_add_impl() grows the four select(2) fd sets with four consecutive reallocarray() calls, but only stores the results in the event base once all four have succeeded. reallocarray() frees the old block on success, so when a later call fails the base still points at memory an earlier call already released, and the error path frees the new blocks instead of recording them. sudo_ev_scan_impl() then memcpy()s from base->readfds_in, sudo_ev_del_impl() clears bits in it with FD_CLR(), and sudo_ev_base_free_impl() frees it a second time. Reducing the growth path to a single translation unit, gcc -fanalyzer (GCC 16.2) reports three -Wanalyzer-double-free (CWE-415) paths through sudo_ev_base_free_impl(), for readfds_in, readfds_out and writefds_out, when the second allocation fails.

Store each fd set in the base as soon as its reallocarray() returns and drop the free() calls from the error path, since the base now owns the new blocks. Leaving base->maxfd alone on the error path keeps every reader in bounds, because maxfd only grows once all four sets are the new size, and a partial grow leaves the sets larger than maxfd implies rather than smaller. The poll backend commits its single reallocarray() the same way, and this is the only place left in the tree that reallocates several owning pointers before recording any of them. event_select.c is built only when poll(2) is unavailable or --disable-poll is given, so the default Linux and BSD builds are unaffected.

@millert millert left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is much safer, thanks.

@millert
millert merged commit 61ae5d5 into sudo-project:main Dec 6, 2026
5 checks passed
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.

2 participants