Skip to content

Fix: copy sockinfo into local buffer before in-place split in mt_connect - #2

Open
edmundlod wants to merge 1 commit into
trusteddomainproject:mainfrom
edmundlod:fix/mt-connect-sockinfo-uaf
Open

Fix: copy sockinfo into local buffer before in-place split in mt_connect#2
edmundlod wants to merge 1 commit into
trusteddomainproject:mainfrom
edmundlod:fix/mt-connect-sockinfo-uaf

Conversation

@edmundlod

Copy link
Copy Markdown

In mt_connect(), sockinfo holds the pointer returned by lua_tostring(l, 1). After lua_pop(l, top) runs, the connection string is dispatched by NUL-terminating its protocol prefix (inet/unix/local) and host part in place (*p = '\0', *at = '\0').

lua_tostring() returns a pointer into Lua-owned storage that the pop may reclaim or share with other interned strings. So both mutating that storage and reading it afterwards (the strlcpy into sa.sun_path, and the connect() error string) are undefined behaviour.

This replaces the const char *sockinfo pointer with a char sockinfo[BUFRSZ] stack buffer populated via strlcpy() before lua_pop(). The in-place ':' and '@' splits then operate on memory we own. No call-site changes; builds clean via autoreconf -fvi && ./configure && make.

Found and fixed in the PhoenixDKIM fork of OpenDKIM; sending it back upstream.

mt_connect() stored lua_tostring()'s return in a const char *sockinfo and,
after lua_pop() had popped the argument, dispatched on the connection
string by NUL-terminating its protocol prefix ('inet'/'unix'/'local') and
host part in place (*p = '\0' / *at = '\0'). lua_tostring() returns a
pointer into Lua-owned storage that the pop may reclaim or share with
other interned strings, so both mutating it and reading it afterwards
(e.g. the strlcpy into sa.sun_path and the connect() error string) are
undefined behaviour.

Replace the const char *sockinfo pointer with a char sockinfo[BUFRSZ]
stack buffer populated via strlcpy() before lua_pop(). The in-place ':'
and '@' splits now operate on memory we own. No call-site changes.

Ported from the PhoenixDKIM fork of OpenDKIM.
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