Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changes-entries/70387.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*) mod_sed: Fix undefined behavior (memcpy() with overlapping source
and destination) when the "D" command shifts the pattern space,
reachable with any script that builds a multi-line pattern space
before "D", such as "N;D". This aborts an AddressSanitizer build and
could corrupt the output on a libc whose memcpy() does not happen to
copy forward. PR 70387. [Vitaly <listratenko1998@mail.ru>]
7 changes: 6 additions & 1 deletion modules/filters/sed1.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,12 @@ static apr_status_t appendmem_to_linebuf(sed_eval_t *eval, const char* sz, apr_s
return rc;
}
}
memcpy(eval->lspend, sz, len);
/* sz may point into eval->linebuf itself: the 'D' command calls
* copy_to_linebuf(eval, linebuf + offset, ...) to shift the remaining
* pattern space to the start of the buffer. memcpy() with overlapping
* ranges is undefined behaviour; use memmove().
*/
memmove(eval->lspend, sz, len);
eval->lspend += len;
return APR_SUCCESS;
}
Expand Down
21 changes: 21 additions & 0 deletions test/modules/filters/test_003_sed.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ def _class_scope(cls, env):
cls.write(env, "insert.txt", "INSERTED\n")
# First line has nothing for \(a*\) to capture, second has "aa".
cls.write(env, "backref.html", "b\naab\n")
# For the D command shifting the pattern space to the start of
# linebuf (BZ 70387): each line must be longer than the one before
# it, or a shift happens to land exactly where the two copied
# ranges touch but do not overlap.
cls.write(env, "nd.html", "A\n" + "B" * 12 + "\n" + "C" * 25 + "\n")
head = "".join(
f"line {i:06d} monday ".ljust(HUGE_LINE_LEN, "y") + "\n"
for i in range(HUGE_HEAD_LINES))
Expand Down Expand Up @@ -564,3 +569,19 @@ def test_filters_003_46(self, env):

def test_filters_003_47(self, env):
assert self.body(env, "9223372036854775807d").decode() == DOC

# --- multi-line pattern space (N, D) -----------------------------------

# N;P;D is the classic "sliding window": N appends the next line to the
# pattern space, P prints its first line, and D then deletes that line
# and restarts the cycle on what is left, without reading a new line.
# mod_sed copies that remainder to the start of its line buffer with
# memcpy(), even though the source still points into the same buffer
# (BZ 70387); each line of nd.html is longer than the one before it so
# every shift actually overlaps. The output shows the shift kept the
# right bytes, not just that it did not crash: the last line is never
# reached, since N finds no further input once it is the only one left,
# which this sed engine ends without printing.
def test_filters_003_48(self, env):
assert self.body(env, "N;P;D", path="/nd.html").decode() == \
"A\n" + "B" * 12 + "\n"
Loading