52b84b
From 696d56fc75e72f47e4d3232a2140fac10b6b44de Mon Sep 17 00:00:00 2001
52b84b
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
52b84b
Date: Mon, 29 Oct 2018 14:55:33 +0100
52b84b
Subject: [PATCH] journal: adapt for new improved LZ4_decompress_safe_partial()
52b84b
MIME-Version: 1.0
52b84b
Content-Type: text/plain; charset=UTF-8
52b84b
Content-Transfer-Encoding: 8bit
52b84b
52b84b
With lz4 1.8.3, this function can now decompress partial results into a smaller
52b84b
buffer. The release news don't say anything interesting, but the test case that
52b84b
was previously failing now works OK.
52b84b
52b84b
Fixes #10259.
52b84b
52b84b
A test is added. It shows that with *older* lz4, a partial decompression can
52b84b
occur with the returned size smaller then the requested number of bytes _and_
52b84b
smaller then the size of the compressed data:
52b84b
52b84b
(lz4-libs-1.8.2-1.fc29.x86_64)
52b84b
Compressed 4194304 → 16464
52b84b
Decompressed → 4194304
52b84b
Decompressed partial 12/4194304 → 4194304
52b84b
Decompressed partial 1/1 → -2 (bad)
52b84b
Decompressed partial 2/2 → -2 (bad)
52b84b
Decompressed partial 3/3 → -2 (bad)
52b84b
Decompressed partial 4/4 → -2 (bad)
52b84b
Decompressed partial 5/5 → -2 (bad)
52b84b
Decompressed partial 6/6 → 6 (good)
52b84b
Decompressed partial 7/7 → 6 (good)
52b84b
Decompressed partial 8/8 → 6 (good)
52b84b
Decompressed partial 9/9 → 6 (good)
52b84b
Decompressed partial 10/10 → 6 (good)
52b84b
Decompressed partial 11/11 → 6 (good)
52b84b
Decompressed partial 12/12 → 6 (good)
52b84b
Decompressed partial 13/13 → 6 (good)
52b84b
Decompressed partial 14/14 → 6 (good)
52b84b
Decompressed partial 15/15 → 6 (good)
52b84b
Decompressed partial 16/16 → 6 (good)
52b84b
Decompressed partial 17/17 → 6 (good)
52b84b
Decompressed partial 18/18 → -16459 (bad)
52b84b
52b84b
(lz4-libs-1.8.3-1.fc29.x86_64)
52b84b
Compressed 4194304 → 16464
52b84b
Decompressed → 4194304
52b84b
Decompressed partial 12/4194304 → 12
52b84b
Decompressed partial 1/1 → 1 (good)
52b84b
Decompressed partial 2/2 → 2 (good)
52b84b
Decompressed partial 3/3 → 3 (good)
52b84b
Decompressed partial 4/4 → 4 (good)
52b84b
...
52b84b
52b84b
If we got such a short "successful" decompression in decompress_startswith() as
52b84b
implemented before this patch, we could be confused and return a false negative
52b84b
result. But it turns out that this only occurs with small output buffer
52b84b
sizes. We use greedy_realloc() to manager the buffer, so it is always at least
52b84b
64 bytes. I couldn't hit a case where decompress_startswith() would actually
52b84b
return a bogus result. But since the lack of proof is not conclusive, the code
52b84b
for *older* lz4 is changed too, just to be safe. We cannot rule out that on a
52b84b
different architecture or with some unlucky compressed string we could hit this
52b84b
corner case.
52b84b
52b84b
The fallback code is guarded by a version check. The check uses a function not
52b84b
the compile-time define, because there was no soversion bump in lz4 or new
52b84b
symbols, and we could be compiled against a newer lz4 and linked at runtime
52b84b
with an older one. (This happens routinely e.g. when somebody upgrades a subset
52b84b
of distro packages.)
52b84b
52b84b
(cherry picked from commit e41ef6fd0027d3619dc1cf062100b2d224d0ee7e)
52b84b
Resolves: #1843871
52b84b
---
52b84b
 src/journal/compress.c      | 39 ++++++++++++++++++++++++-------------
52b84b
 src/journal/test-compress.c | 21 ++++++++++----------
52b84b
 2 files changed, 37 insertions(+), 23 deletions(-)
52b84b
52b84b
diff --git a/src/journal/compress.c b/src/journal/compress.c
52b84b
index a4a5e63840..e95ce2bcaa 100644
52b84b
--- a/src/journal/compress.c
52b84b
+++ b/src/journal/compress.c
52b84b
@@ -290,7 +290,6 @@ int decompress_startswith_lz4(const void *src, uint64_t src_size,
52b84b
          * prefix */
52b84b
 
52b84b
         int r;
52b84b
-        size_t size;
52b84b
 
52b84b
         assert(src);
52b84b
         assert(src_size > 0);
52b84b
@@ -307,23 +306,37 @@ int decompress_startswith_lz4(const void *src, uint64_t src_size,
52b84b
 
52b84b
         r = LZ4_decompress_safe_partial((char*)src + 8, *buffer, src_size - 8,
52b84b
                                         prefix_len + 1, *buffer_size);
52b84b
-        if (r >= 0)
52b84b
-                size = (unsigned) r;
52b84b
-        else {
52b84b
-                /* lz4 always tries to decode full "sequence", so in
52b84b
-                 * pathological cases might need to decompress the
52b84b
-                 * full field. */
52b84b
+        /* One lz4 < 1.8.3, we might get "failure" (r < 0), or "success" where
52b84b
+         * just a part of the buffer is decompressed. But if we get a smaller
52b84b
+         * amount of bytes than requested, we don't know whether there isn't enough
52b84b
+         * data to fill the requested size or whether we just got a partial answer.
52b84b
+         */
52b84b
+        if (r < 0 || (size_t) r < prefix_len + 1) {
52b84b
+                size_t size;
52b84b
+
52b84b
+                if (LZ4_versionNumber() >= 10803)
52b84b
+                        /* We trust that the newer lz4 decompresses the number of bytes we
52b84b
+                         * requested if available in the compressed string. */
52b84b
+                        return 0;
52b84b
+
52b84b
+                if (r > 0)
52b84b
+                        /* Compare what we have first, in case of mismatch we can
52b84b
+                         * shortcut the full comparison. */
52b84b
+                        if (memcmp(*buffer, prefix, r) != 0)
52b84b
+                                return 0;
52b84b
+
52b84b
+                /* Before version 1.8.3, lz4 always tries to decode full a "sequence",
52b84b
+                 * so in pathological cases might need to decompress the full field. */
52b84b
                 r = decompress_blob_lz4(src, src_size, buffer, buffer_size, &size, 0);
52b84b
                 if (r < 0)
52b84b
                         return r;
52b84b
-        }
52b84b
 
52b84b
-        if (size >= prefix_len + 1)
52b84b
-                return memcmp(*buffer, prefix, prefix_len) == 0 &&
52b84b
-                        ((const uint8_t*) *buffer)[prefix_len] == extra;
52b84b
-        else
52b84b
-                return 0;
52b84b
+                if (size < prefix_len + 1)
52b84b
+                        return 0;
52b84b
+        }
52b84b
 
52b84b
+        return memcmp(*buffer, prefix, prefix_len) == 0 &&
52b84b
+                ((const uint8_t*) *buffer)[prefix_len] == extra;
52b84b
 #else
52b84b
         return -EPROTONOSUPPORT;
52b84b
 #endif
52b84b
diff --git a/src/journal/test-compress.c b/src/journal/test-compress.c
52b84b
index 65cd3fbfeb..f60c4ae3d7 100644
52b84b
--- a/src/journal/test-compress.c
52b84b
+++ b/src/journal/test-compress.c
52b84b
@@ -223,13 +223,13 @@ static void test_compress_stream(int compression,
52b84b
 
52b84b
 #if HAVE_LZ4
52b84b
 static void test_lz4_decompress_partial(void) {
52b84b
-        char buf[20000];
52b84b
+        char buf[20000], buf2[100];
52b84b
         size_t buf_size = sizeof(buf), compressed;
52b84b
         int r;
52b84b
         _cleanup_free_ char *huge = NULL;
52b84b
 
52b84b
 #define HUGE_SIZE (4096*1024)
52b84b
-        huge = malloc(HUGE_SIZE);
52b84b
+        assert_se(huge = malloc(HUGE_SIZE));
52b84b
         memset(huge, 'x', HUGE_SIZE);
52b84b
         memcpy(huge, "HUGE=", 5);
52b84b
 
52b84b
@@ -248,14 +248,15 @@ static void test_lz4_decompress_partial(void) {
52b84b
         assert_se(r >= 0);
52b84b
         log_info("Decompressed partial %i/%i → %i", 12, HUGE_SIZE, r);
52b84b
 
52b84b
-        /* We expect this to fail, because that's how current lz4 works. If this
52b84b
-         * call succeeds, then lz4 has been fixed, and we need to change our code.
52b84b
-         */
52b84b
-        r = LZ4_decompress_safe_partial(buf, huge,
52b84b
-                                        compressed,
52b84b
-                                        12, HUGE_SIZE-1);
52b84b
-        assert_se(r < 0);
52b84b
-        log_info("Decompressed partial %i/%i → %i", 12, HUGE_SIZE-1, r);
52b84b
+        for (size_t size = 1; size < sizeof(buf2); size++) {
52b84b
+                /* This failed in older lz4s but works in newer ones. */
52b84b
+                r = LZ4_decompress_safe_partial(buf, buf2, compressed, size, size);
52b84b
+                log_info("Decompressed partial %zu/%zu → %i (%s)", size, size, r,
52b84b
+                                                                   r < 0 ? "bad" : "good");
52b84b
+                if (r >= 0 && LZ4_versionNumber() >= 10803)
52b84b
+                        /* lz4 <= 1.8.2 should fail that test, let's only check for newer ones */
52b84b
+                        assert_se(memcmp(buf2, huge, r) == 0);
52b84b
+        }
52b84b
 }
52b84b
 #endif
52b84b