Skip to content

mod_sed: Use memmove() for the D command's pattern-space shift. - #1149

Open
arturobernalg wants to merge 1 commit into
apache:trunkfrom
arturobernalg:fix/bz70387-sed-memcpy-overlap
Open

arturobernalg wants to merge 1 commit into
apache:trunkfrom
arturobernalg:fix/bz70387-sed-memcpy-overlap

Conversation

@arturobernalg

Copy link
Copy Markdown
Member

mod_sed's D command shifts the remaining pattern space to the start of its line buffer by copying it over itself, but the copy used memcpy() even though the source and destination can overlap (e.g. with "N;D", a common sliding-window idiom) -- undefined behaviour per C11, masked on this glibc by memcpy() happening to copy forward, but it reliably aborts an AddressSanitizer build with memcpy-param-overlap. This switches that one call to memmove(). Reproduced on trunk with an ASan build: the new pytest actually crashes httpd's restart health-check before the fix and passes clean after it; the rest of modules/filters (123 tests) is unaffected. https://bz.apache.org/bugzilla/show_bug.cgi?id=70387

[x] This PR does not report or address a security vulnerability
(see SECURITY.md and https://www.apache.org/security/#reporting-a-vulnerability).
[x] Code follows the httpd style guide.
[x] New log messages (level debug or higher) use an empty APLOGNO() tag;
numbers are assigned by a committer at merge time (see docs/log-message-tags/README).
[x] If the change is user-visible, a changes-entries/*.txt file is included,
following the template in README.CHANGES (not needed otherwise).
[x] Test cases based on pyhttpd are included where appropriate, in the
test/modules/xxx directory matching the modules/xxx module source,
or test/modules/core for core server changes. On Unix, verify these
pass locally with e.g. make check-pytest PYTEST_DIRS=test/modules/xxx.

The D command removes the first line of the pattern space and restarts
the cycle on what is left, which copy_to_linebuf()/appendmem_to_linebuf()
implement by copying the remainder of eval->linebuf back to its own
start. The source and destination of that copy are both inside
eval->linebuf and can overlap (reached whenever the remaining pattern
space is longer than what preceded it, as "N;D" builds up), but the copy
used memcpy(), which is undefined behavior for overlapping ranges per
C11 7.24.2.1. On this glibc it happens to still produce the right bytes
because memcpy() here always copies forward, but an AddressSanitizer
build aborts with memcpy-param-overlap, and nothing guarantees the same
on every libc.

Use memmove() instead, which is safe for overlapping ranges and behaves
identically otherwise.

PR: 70387
@arturobernalg
arturobernalg force-pushed the fix/bz70387-sed-memcpy-overlap branch from 374ade2 to 26b00d2 Compare December 2, 2026 10:20
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