Skip to content

handle_server_error: don't stash a pointer into the freed message - #584

Merged
millert merged 1 commit into
sudo-project:mainfrom
iefa-m:relay-error-use-after-free
Oct 27, 2026
Merged

millert merged 1 commit into
sudo-project:mainfrom
iefa-m:relay-error-use-after-free

Conversation

@iefa-m

@iefa-m iefa-m commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

handle_server_error() sets closure->errstr to msg->u.error, a string owned by the ServerMessage that handle_server_message() just unpacked. That function frees the message with server_message__free_unpacked() as soon as the handler returns false, and relay_server_msg_cb() then passes the now dangling closure->errstr to schedule_error_message(), which packs it into the error ServerMessage sent back to the client. The error and abort strings come straight off the wire from the relay peer, so in relay mode an error or abort message makes sudo_logsrvd read freed heap and copy those bytes into its reply to the client. This came in with fcc315f, which moved the scheduling out of the handler but kept the pointer.

Every other closure->errstr in the daemon is a static translated string and errstr is never freed, so do the same here and leave the relay's own text to the debug log just above. Confirmed with AddressSanitizer by unpacking an ERROR ServerMessage, aliasing u.error, freeing the message, then formatting the error reply: a heap-use-after-free read of the freed string, gone after the change. make check passes.

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

It's a little unfortunate that we can't pass the relay server's error message to the client but this keeps the lifetime of errstr consistent which is more important.

@millert
millert merged commit 5a9c130 into sudo-project:main Oct 27, 2026
5 checks passed
@iefa-m

iefa-m commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Agreed, losing the relay's text is a shame, but it still ends up in the debug log so it isn't gone entirely. Thanks for merging this.

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