Blame SOURCES/bz1819519-fix-handling-hpack-zero-bytes-overwrite.patch

622f05
From 4e372dc350be5c72b88546bf03392a5793cea179 Mon Sep 17 00:00:00 2001
622f05
From: Willy Tarreau <w@1wt.eu>
622f05
Date: Sun, 29 Mar 2020 08:53:31 +0200
622f05
Subject: BUG/CRITICAL: hpack: never index a header into the headroom after
622f05
 wrapping
622f05

622f05
The HPACK header table is implemented as a wrapping list inside a contigous
622f05
area. Headers names and values are stored from right to left while indexes
622f05
are stored from left to right. When there's no more room to store a new one,
622f05
we wrap to the right again, or possibly defragment it if needed. The condition
622f05
do use the right part (called tailroom) or the left part (called headroom)
622f05
depends on the location of the last inserted header. After wrapping happens,
622f05
the code forces to stick to tailroom by pretending there's no more headroom,
622f05
so that the size fit test always fails. The problem is that nothing prevents
622f05
from storing a header with an empty name and empty value, resulting in a
622f05
total size of zero bytes, which satisfies the condition to use the headroom.
622f05
Doing this in a wrapped buffer results in changing the "front" header index
622f05
and causing miscalculations on the available size and the addresses of the
622f05
next headers. This may even allow to overwrite some parts of the index,
622f05
opening the possibility to perform arbitrary writes into a 32-bit relative
622f05
address space.
622f05

622f05
This patch fixes the issue by making sure the headroom is considered only
622f05
when the buffer does not wrap, instead of relying on the zero size. This
622f05
must be backported to all versions supporting H2, which is as far as 1.8.
622f05

622f05
Many thanks to Felix Wilhelm of Google Project Zero for responsibly
622f05
reporting this problem with a reproducer and a detailed analysis.
622f05
---
622f05
 src/hpack-tbl.c | 4 ++--
622f05
 1 file changed, 2 insertions(+), 2 deletions(-)
622f05

622f05
diff --git a/src/hpack-tbl.c b/src/hpack-tbl.c
622f05
index 70d7f35834..727ff7a17b 100644
622f05
--- a/src/hpack-tbl.c
622f05
+++ b/src/hpack-tbl.c
622f05
@@ -346,9 +346,9 @@ int hpack_dht_insert(struct hpack_dht *dht, struct ist name, struct ist value)
622f05
 	 * room left in the tail to suit the protocol, but tests show that in
622f05
 	 * practice it almost never happens in other situations so the extra
622f05
 	 * test is useless and we simply fill the headroom as long as it's
622f05
-	 * available.
622f05
+	 * available and we don't wrap.
622f05
 	 */
622f05
-	if (headroom >= name.len + value.len) {
622f05
+	if (prev == dht->front && headroom >= name.len + value.len) {
622f05
 		/* install upfront and update ->front */
622f05
 		dht->dte[head].addr = dht->dte[dht->front].addr - (name.len + value.len);
622f05
 		dht->front = head;
622f05
-- 
622f05
2.20.1
622f05