Skip to content

sudo_ev_add_impl: store the poll slot index as an int - #602

Merged
millert merged 1 commit into
sudo-project:mainfrom
iefa-m:event-poll-pfd-idx-int
Nov 21, 2026
Merged

millert merged 1 commit into
sudo-project:mainfrom
iefa-m:event-poll-pfd-idx-int

Conversation

@iefa-m

@iefa-m iefa-m commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

struct sudo_event keeps its slot in the poll backend's pfds array in a short (pfd_idx, already marked XXX), while sudo_ev_add_impl() lets that array grow up to RLIMIT_NOFILE entries. When the 32769th I/O event is added, (short)base->pfd_free becomes -32768 and the event's fd and poll flags are written to &base->pfds[-32768], 256 KiB before the heap array. sudo_ev_del_impl() and sudo_ev_scan_impl() then keep using that slot, and the != -1 check in the scan loop doesn't catch it. sudo_logsrvd hits this from the network: unlimit_nofile() raises the soft limit to the hard limit (524288 or more under systemd), there is no cap on concurrent connections, and a plaintext connection gets a persistent read event with no timeout as soon as it is accepted (start_protocol()). A client that opens about 32.8k connections and sends nothing makes the root daemon write outside the array.

Making pfd_idx an int matches pfd_free, pfd_max and pfd_high. On LP64 the field sits in the padding before callback, so the struct's size and the offsets of later members don't change. I reproduced it with AddressSanitizer by driving the unmodified event_poll.c with 40000 read events and RLIMIT_NOFILE at 524288: adding event 32768 triggers a 4-byte WRITE at event_poll.c:120, 256 KiB before the live pfds allocation. With the patch, all 40000 events get slots 0 through 39999. The select backend doesn't use pfd_idx.

The pollfd array in the event base can grow to RLIMIT_NOFILE entries,
but each event remembers its slot in a short.  Once more than 32767
I/O events are registered, slot 32768 is stored as -32768 and the
event's fd and events are written 256 KiB before the start of the
array, with later deletes and scans using the same bad slot.

sudo_logsrvd raises RLIMIT_NOFILE to the hard limit and keeps a read
event for every client connection, so enough idle connections are
sufficient to reach this.  Use an int, like pfd_free, pfd_max and
pfd_high.

@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.

Thanks. The event code was written before the log server support; at the time it was only used for keyboard I/O logging.

@millert
millert merged commit 49e4b24 into sudo-project:main Nov 21, 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