Skip to content

sudo_preload_dso_alloc: advance the write pointer, not the array base - #609

Merged
millert merged 1 commit into
sudo-project:mainfrom
iefa-m:preload-enable-var-nep
Dec 2, 2026
Merged

millert merged 1 commit into
sudo-project:mainfrom
iefa-m:preload-enable-var-nep

Conversation

@iefa-m

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

Copy link
Copy Markdown
Contributor

sudo_preload_dso_alloc() builds the command's new environment in nenvp and appends to it through the cursor nep, but the branch that adds RTLD_PRELOAD_ENABLE_VAR writes through nenvp. That overwrites the first copied environment variable and leaves the returned pointer one element past the allocation base, so the command loses that variable, the enable variable never reaches it (the slot it landed in is the one that gets skipped), and the caller frees a pointer that is not the one that was allocated. RTLD_PRELOAD_ENABLE_VAR is only set by configure for Darwin 7 and earlier and for NeXTSTEP, so the branch is not compiled on current platforms, but where it is, disable_execute() cannot turn the preload on at all and a NOEXEC rule has no effect on the command.

Append through nep like the other two appends in the function. env_size already reserves a slot for this entry when dso_enabled is false, so the buffer is still large enough. This is the only place in the tree that advances the array rather than the cursor.

Repro: build src/exec_preload.c with -DRTLD_PRELOAD_ENABLE_VAR='"DYLD_FORCE_FLAT_NAMESPACE"' and call sudo_preload_dso() with {"FIRST=1", "SECOND=2", "THIRD=3"}. Before, the result starts at SECOND=2, contains no DYLD_FORCE_FLAT_NAMESPACE, and AddressSanitizer reports attempting free on address which was not malloc()-ed ... 8 bytes inside of 48-byte region when the array is freed, which is what src/sudo_intercept.c does with it. After, all three variables and the enable variable are there and the free is clean. I also checked the existing preload variable, duplicate SUDO_INTERCEPT_FD, empty envp and already-enabled cases. make check passes.

@millert
millert merged commit 62dc2ea into sudo-project:main Dec 2, 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