diff --git a/changes-entries/70387.txt b/changes-entries/70387.txt new file mode 100644 index 00000000000..b0f2fe446b2 --- /dev/null +++ b/changes-entries/70387.txt @@ -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 ] diff --git a/modules/filters/sed1.c b/modules/filters/sed1.c index b134fe3634f..f07417d6177 100644 --- a/modules/filters/sed1.c +++ b/modules/filters/sed1.c @@ -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; } diff --git a/test/modules/filters/test_003_sed.py b/test/modules/filters/test_003_sed.py index d97a493d6bb..236ad5855ae 100644 --- a/test/modules/filters/test_003_sed.py +++ b/test/modules/filters/test_003_sed.py @@ -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)) @@ -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"