Skip to contentSkip to conversation

Tolerate zlib deflation with window size < 32Kb

PATCH Superseded [email protected] 3 participants 8 messages Jun 15, 2016 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Tolerate zlib deflation with window size < 32Kb

From: Roberto Tyley <[email protected]>

Git currently reports loose objects as 'corrupt' if they've been deflated using a window size less than 32Kb, because the experimental_loose_object() function doesn't recognise the header byte as a zlib header. This patch makes the function tolerant of all valid window sizes (15-bit to 8-bit) - but doesn't sacrifice it's accuracy in distingushing the standard loose-object format from the experimental (now abandoned) format.

On memory constrained systems zlib may use a much smaller window size - working on Agit, I found that Android uses a 4KB window; giving a header byte of 0x48, not 0x78. Consequently all loose objects generated appear 'corrupt', which is why Agit is a read-only Git client at this time - I don't want my client to generate Git repos that other clients treat as broken :(

This patch makes Git tolerant of different deflate settings - it might appear that it changes experimental_loose_object() to the point where it could incorrectly identify the experimental format as the standard one, but the two criteria (bitmask & checksum) can only give a false result for an experimental object where both of the following are true:

1) object size is exactly 8 bytes when uncompressed (bitmask)
2) [single-byte in-pack git type&size header] * 256
+ [1st byte of the following zlib header] % 31 = 0 (checksum)

As it happens, for all possible combinations of valid object type (1-4) and window bits (0-7), the only time when the checksum will be divisible by 31 is for 0x1838 - ie object type *1*, a Commit - which, due the fields all Commit objects must contain, could never be as small as 8 bytes in size.

Given this, the combination of the two criteria (bitmask & checksum) always correctly determines the buffer format, and is more tolerant than the previous version.

The alternative to this patch is simply removing support for the experimental format, which I am also totally cool with.

References:

Android uses a 4KB window for deflation:
http://android.git.kernel.org/?p=platform/libcore.git;a=blob;f=luni/src/main/native/java_util_zip_Deflater.cpp;h=c0b2feff196e63a7b85d97cf9ae5bb2583409c28;hb=refs/heads/gingerbread#l53

Code snippet searching for false positives with the zlib checksum:
https://gist.github.com/1118177

---
 sha1_file.c |   32 ++++++++++++++++++++++++++------
 1 files changed, 26 insertions(+), 6 deletions(-)
sha1_file.c +26 -6
index 89d7e5e..2083e8b 100644
@@ -1217,14 +1217,34 @@ static int experimental_loose_object(unsigned char *map)
 	unsigned int word;
 
 	/*
-	 * Is it a zlib-compressed buffer? If so, the first byte
-	 * must be 0x78 (15-bit window size, deflated), and the
-	 * first 16-bit word is evenly divisible by 31. If so,
-	 * we are looking at the official format, not the experimental
-	 * one.
+	 * We must determine if the buffer contains the standard
+	 * zlib-deflated stream or the experimental format based
+	 * on the in-pack object format. Compare the header byte
+	 * for each format:
+	 *
+	 * RFC1950 zlib w/ deflate : 0www1000 : 0 <= www <= 7
+	 * Experimental pack-based : Stttssss : ttt = 1,2,3,4
+	 *
+	 * If bit 7 is clear and bits 0-3 equal 8, the buffer MUST be
+	 * in standard loose-object format, UNLESS it is a Git-pack
+	 * format object *exactly* 8 bytes in size when inflated.
+	 *
+	 * However, RFC1950 also specifies that the 1st 16-bit word
+	 * must be divisible by 31 - this checksum tells us our buffer
+	 * is in the standard format, giving a false positive only if
+	 * the 1st word of the Git-pack format object happens to be
+	 * divisible by 31, ie:
+	 *      ((byte0 * 256) + byte1) % 31 = 0
+	 *   =>        0ttt10000www1000 % 31 = 0
+	 *
+	 * As it happens, this case can only arise for www=3 & ttt=1
+	 * - ie, a Commit object, which would have to be 8 bytes in
+	 * size. As no Commit can be that small, we find that the
+	 * combination of these two criteria (bitmask & checksum)
+	 * can always correctly determine the buffer format.
 	 */
 	word = (map[0] << 8) + map[1];
-	if (map[0] == 0x78 && !(word % 31))
+	if ((map[0] & 0x88) == 0x08 && !(word % 31))
 		return 0;
 	else
 		return 1;
Signature · 1 line
1.7.4.1
#1 lore raw
Quoted 19 lines · [email protected] writes:
sha1_file.c +19 -0
+	 * We must determine if the buffer contains the standard
+	 * zlib-deflated stream or the experimental format based
+	 * on the in-pack object format. Compare the header byte
+	 * for each format:
+	 *
+	 * RFC1950 zlib w/ deflate : 0www1000 : 0 <= www <= 7
+	 * Experimental pack-based : Stttssss : ttt = 1,2,3,4
+	 *
+	 * If bit 7 is clear and bits 0-3 equal 8, the buffer MUST be
+	 * in standard loose-object format, UNLESS it is a Git-pack
+	 * format object *exactly* 8 bytes in size when inflated.
+	 *
+	 * However, RFC1950 also specifies that the 1st 16-bit word
+	 * must be divisible by 31 - this checksum tells us our buffer
+	 * is in the standard format, giving a false positive only if
+	 * the 1st word of the Git-pack format object happens to be
+	 * divisible by 31, ie:
+	 *      ((byte0 * 256) + byte1) % 31 = 0
+	 *   =>        0ttt10000www1000 % 31 = 0

sha1_file.c static int experimental_loose_object(unsigned char *map) · line 1247

Quoted 10 lines
sha1_file.c +7 -1
+	 *
+	 * As it happens, this case can only arise for www=3 & ttt=1
+	 * - ie, a Commit object, which would have to be 8 bytes in
+	 * size. As no Commit can be that small, we find that the
+	 * combination of these two criteria (bitmask & checksum)
+	 * can always correctly determine the buffer format.
 	 */
 	word = (map[0] << 8) + map[1];
-	if (map[0] == 0x78 && !(word % 31))
+	if ((map[0] & 0x88) == 0x08 && !(word % 31))

Are you sure about this 0x88? Isn't it 0x8F or something?

Quoted 3 lines
sha1_file.c +0 -0
 		return 0;
 	else
 		return 1;
#3 lore raw
In reply to a message not in the archive
Quoted 5 lines · Junio C Hamano
Quoted 2 lines · [email protected] writes:

-     if (map[0] == 0x78 && !(word % 31))
+     if ((map[0] & 0x88) == 0x08 && !(word % 31))

Are you sure about this 0x88? Isn't it 0x8F or something?

Ah- yes, you're right - the bitmask should be 0x8F, because the lower nibble of a deflated zlib stream is 8 (8=deflate) and we want to match all 4 bits.

Thankfully the rest of the logic still applies - I'll submit an updated patch in a minute, once I've conquered my embarrassment :-)

#4 lore raw

Heya,

Quoted 2 lines · Roberto Tyley

Thankfully the rest of the logic still applies - I'll submit an updated patch in a minute, once I've conquered my embarrassment :-)

Perhaps you can include a test to increase confidence that it works now? :)

Signature · 3 lines
Cheers,

Sverre Rabbelier
#5 lore raw
In reply to a message not in the archive
Quoted 9 lines · Roberto Tyley
Quoted 5 lines · Junio C Hamano

sha1_file.c static int experimental_loose_object(unsigned char *map) · line 1247

Quoted 2 lines · [email protected] writes:
sha1_file.c +1 -1
-     if (map[0] == 0x78 && !(word % 31))
+     if ((map[0] & 0x88) == 0x08 && !(word % 31))

Are you sure about this 0x88? Isn't it 0x8F or something?

Quoted 6 lines

Ah- yes, you're right - the bitmask should be 0x8F, because the lower nibble of a deflated zlib stream is 8 (8=deflate) and we want to match all 4 bits.

Thankfully the rest of the logic still applies - I'll submit an updated patch in a minute, once I've conquered my embarrassment :-)

Heh, don't get embarrassed. It was your well-written in-code comment that helped me to follow the new logic well enough to spot it.

#6 lore raw

PATCH v2 1/1 Tolerate zlib deflation with window size < 32Kb

From: Roberto Tyley <[email protected]>

Git currently reports loose objects as 'corrupt' if they've been deflated using a window size less than 32Kb, because the experimental_loose_object() function doesn't recognise the header byte as a zlib header. This patch makes the function tolerant of all valid window sizes (15-bit to 8-bit) - but doesn't sacrifice it's accuracy in distingushing the standard loose-object format from the experimental (now abandoned) format.

On memory constrained systems zlib may use a much smaller window size - working on Agit, I found that Android uses a 4KB window; giving a header byte of 0x48, not 0x78. Consequently all loose objects generated appear 'corrupt', which is why Agit is a read-only Git client at this time - I don't want my client to generate Git repos that other clients treat as broken :(

This patch makes Git tolerant of different deflate settings - it might appear that it changes experimental_loose_object() to the point where it could incorrectly identify the experimental format as the standard one, but the two criteria (bitmask & checksum) can only give a false result for an experimental object where both of the following are true:

1) object size is exactly 8 bytes when uncompressed (bitmask)
2) [single-byte in-pack git type&size header] * 256
+ [1st byte of the following zlib header] % 31 = 0 (checksum)

As it happens, for all possible combinations of valid object type (1-4) and window bits (0-7), the only time when the checksum will be divisible by 31 is for 0x1838 - ie object type *1*, a Commit - which, due the fields all Commit objects must contain, could never be as small as 8 bytes in size.

Given this, the combination of the two criteria (bitmask & checksum) always correctly determines the buffer format, and is more tolerant than the previous version.

The alternative to this patch is simply removing support for the experimental format, which I am also totally cool with.

References:

Android uses a 4KB window for deflation:
http://android.git.kernel.org/?p=platform/libcore.git;a=blob;f=luni/src/main/native/java_util_zip_Deflater.cpp;h=c0b2feff196e63a7b85d97cf9ae5bb2583409c28;hb=refs/heads/gingerbread#l53

Code snippet searching for false positives with the zlib checksum:
https://gist.github.com/1118177

---

This updated patch contains the corrected 0x8F bitmask, and also provides tests to ensure that the experimental & standard format headers are still correctly identified and parsed.

As Git doesn't generate the old experimental format anymore, the tests include a set of loose objects in that format created using Git v1.4.3, as well as two objects generated on the Android platform using a reduced-size window buffer for deflation.

 sha1_file.c                                        |   32 ++++++++--
 t/t1013-loose-object-format.sh                     |   66 ++++++++++++++++++++
 .../14/9cedb5c46929d18e0f118e9fa31927487af3b6      |  Bin 0 -> 117 bytes
 .../16/56f9233d999f61ef23ef390b9c71d75399f435      |  Bin 0 -> 17 bytes
 .../1e/72a6b2c4a577ab0338860fa9fe87f761fc9bbd      |  Bin 0 -> 18 bytes
 .../25/7cc5642cb1a054f08cc83f2d943e56fd3ebe99      |  Bin 0 -> 19 bytes
 .../2e/65efe2a145dda7ee51d1741299f848e5bf752e      |  Bin 0 -> 10 bytes
 .../6b/aee0540ea990d9761a3eb9ab183003a71c3696      |  Bin 0 -> 181 bytes
 .../70/e6a83d8dcb26fc8bc0cf702e2ddeb6adca18fd      |  Bin 0 -> 26 bytes
 .../76/e7fa9941f4d5f97f64fea65a2cba436bc79cbb      |  Bin 0 -> 155 bytes
 .../78/75c6237d3fcdd0ac2f0decc7d3fa6a50b66c09      |  Bin 0 -> 139 bytes
 .../7a/37b887a73791d12d26c0d3e39568a8fb0fa6e8      |  Bin 0 -> 54 bytes
 .../85/df50785d62d3b05ab03d9cbf7e4a0b49449730      |  Bin 0 -> 13 bytes
 .../8d/4e360d6c70fbd72411991c02a09c442cf7a9fa      |  Bin 0 -> 156 bytes
 .../95/b1625de3ba8b2214d1e0d0591138aea733f64f      |  Bin 0 -> 252 bytes
 .../9a/e9e86b7bd6cb1472d9373702d8249973da0832      |  Bin 0 -> 11 bytes
 .../bd/15045f6ce8ff75747562173640456a394412c8      |  Bin 0 -> 34 bytes
 .../e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391      |  Bin 0 -> 9 bytes
 .../f8/16d5255855ac160652ee5253b06cd8ee14165a      |  Bin 0 -> 116 bytes
 19 files changed, 92 insertions(+), 6 deletions(-)
 create mode 100755 t/t1013-loose-object-format.sh
 create mode 100644 t/t1013/objects/14/9cedb5c46929d18e0f118e9fa31927487af3b6
 create mode 100644 t/t1013/objects/16/56f9233d999f61ef23ef390b9c71d75399f435
 create mode 100644 t/t1013/objects/1e/72a6b2c4a577ab0338860fa9fe87f761fc9bbd
 create mode 100644 t/t1013/objects/25/7cc5642cb1a054f08cc83f2d943e56fd3ebe99
 create mode 100644 t/t1013/objects/2e/65efe2a145dda7ee51d1741299f848e5bf752e
 create mode 100644 t/t1013/objects/6b/aee0540ea990d9761a3eb9ab183003a71c3696
 create mode 100644 t/t1013/objects/70/e6a83d8dcb26fc8bc0cf702e2ddeb6adca18fd
 create mode 100644 t/t1013/objects/76/e7fa9941f4d5f97f64fea65a2cba436bc79cbb
 create mode 100644 t/t1013/objects/78/75c6237d3fcdd0ac2f0decc7d3fa6a50b66c09
 create mode 100644 t/t1013/objects/7a/37b887a73791d12d26c0d3e39568a8fb0fa6e8
 create mode 100644 t/t1013/objects/85/df50785d62d3b05ab03d9cbf7e4a0b49449730
 create mode 100644 t/t1013/objects/8d/4e360d6c70fbd72411991c02a09c442cf7a9fa
 create mode 100644 t/t1013/objects/95/b1625de3ba8b2214d1e0d0591138aea733f64f
 create mode 100644 t/t1013/objects/9a/e9e86b7bd6cb1472d9373702d8249973da0832
 create mode 100644 t/t1013/objects/bd/15045f6ce8ff75747562173640456a394412c8
 create mode 100644 t/t1013/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391
 create mode 100644 t/t1013/objects/f8/16d5255855ac160652ee5253b06cd8ee14165a
sha1_file.c +26 -6
index d5616dc..44444ae 100644
@@ -1217,14 +1217,34 @@ static int experimental_loose_object(unsigned char *map)
 	unsigned int word;
 
 	/*
-	 * Is it a zlib-compressed buffer? If so, the first byte
-	 * must be 0x78 (15-bit window size, deflated), and the
-	 * first 16-bit word is evenly divisible by 31. If so,
-	 * we are looking at the official format, not the experimental
-	 * one.
+	 * We must determine if the buffer contains the standard
+	 * zlib-deflated stream or the experimental format based
+	 * on the in-pack object format. Compare the header byte
+	 * for each format:
+	 *
+	 * RFC1950 zlib w/ deflate : 0www1000 : 0 <= www <= 7
+	 * Experimental pack-based : Stttssss : ttt = 1,2,3,4
+	 *
+	 * If bit 7 is clear and bits 0-3 equal 8, the buffer MUST be
+	 * in standard loose-object format, UNLESS it is a Git-pack
+	 * format object *exactly* 8 bytes in size when inflated.
+	 *
+	 * However, RFC1950 also specifies that the 1st 16-bit word
+	 * must be divisible by 31 - this checksum tells us our buffer
+	 * is in the standard format, giving a false positive only if
+	 * the 1st word of the Git-pack format object happens to be
+	 * divisible by 31, ie:
+	 *      ((byte0 * 256) + byte1) % 31 = 0
+	 *   =>        0ttt10000www1000 % 31 = 0
+	 *
+	 * As it happens, this case can only arise for www=3 & ttt=1
+	 * - ie, a Commit object, which would have to be 8 bytes in
+	 * size. As no Commit can be that small, we find that the
+	 * combination of these two criteria (bitmask & checksum)
+	 * can always correctly determine the buffer format.
 	 */
 	word = (map[0] << 8) + map[1];
-	if (map[0] == 0x78 && !(word % 31))
+	if ((map[0] & 0x8F) == 0x08 && !(word % 31))
 		return 0;
 	else
 		return 1;
t/t1013-loose-object-format.sh new file +66 -0

66 lines not on this page · raw message

t/t1013/objects/14/9cedb5c46929d18e0f118e9fa31927487af3b6 new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..472fd1458e03e47136416bce60d6e7c893a468ab
GIT binary patch
t/t1013/objects/16/56f9233d999f61ef23ef390b9c71d75399f435 new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..c379d74ae2b40faae9c31cb7478bc7f42a6fb13c
GIT binary patch
t/t1013/objects/1e/72a6b2c4a577ab0338860fa9fe87f761fc9bbd new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..93706305bcff060547181ee4b7d3a8583b691181
GIT binary patch
t/t1013/objects/25/7cc5642cb1a054f08cc83f2d943e56fd3ebe99 new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..bdcf704c9e663f3a11b3146b1b455bc2581b4761
GIT binary patch
t/t1013/objects/2e/65efe2a145dda7ee51d1741299f848e5bf752e new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..ad62c43e418c11254cede4dc94982ac6a30dbe9c
GIT binary patch
t/t1013/objects/6b/aee0540ea990d9761a3eb9ab183003a71c3696 new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..3d2f0337dbb64c092b4a7e9bd324a66986cfe01a
GIT binary patch
t/t1013/objects/70/e6a83d8dcb26fc8bc0cf702e2ddeb6adca18fd new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..b3f71a6ee5802b36b4576c5ebf111f30ef2017a4
GIT binary patch
t/t1013/objects/76/e7fa9941f4d5f97f64fea65a2cba436bc79cbb new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..af4e9a7b0c035fc7c26355b85f250f3ff7d3afa1
GIT binary patch
t/t1013/objects/78/75c6237d3fcdd0ac2f0decc7d3fa6a50b66c09 new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..3dd28be5c61840c23486bc654213c493c5387d75
GIT binary patch
t/t1013/objects/7a/37b887a73791d12d26c0d3e39568a8fb0fa6e8 new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..2b97b264c33f78fe6c8230b5bbeacd6409d9f963
GIT binary patch
t/t1013/objects/85/df50785d62d3b05ab03d9cbf7e4a0b49449730 new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..6dff746876aab1acb9b42c557cdda69164d68398
GIT binary patch
t/t1013/objects/8d/4e360d6c70fbd72411991c02a09c442cf7a9fa new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..cb41e92d076c191245389bedfc0184ff78511c03
GIT binary patch
t/t1013/objects/95/b1625de3ba8b2214d1e0d0591138aea733f64f new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..7ac46b4f703aa00cd96c42971a237e3da5bbb5ca
GIT binary patch
t/t1013/objects/9a/e9e86b7bd6cb1472d9373702d8249973da0832 new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..9d8316d4e598e32ef17b3918cf00276c8c1bffba
GIT binary patch
t/t1013/objects/bd/15045f6ce8ff75747562173640456a394412c8 new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..eebf23956e3b8ac736fa04e682b4214ac75c716a
GIT binary patch
t/t1013/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..134cf1937963c733d7affa6919f9835db864188a
GIT binary patch
t/t1013/objects/f8/16d5255855ac160652ee5253b06cd8ee14165a new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..26b75aec56f8f178a9f001be3b630a4e48ceb6b9
GIT binary patch
Signature · 1 line
1.7.4.1
#7 lore raw

Review

Quoted 5 lines · [email protected] writes:

+. ./test-lib.sh

(quoted diff) +4 -0
+
+assert_blob_equals() {
+	echo -n $2 > expected &&
+	git cat-file -p $1 > actual &&

Needs proper quoting with dq.

Review

Quoted 13 lines
(quoted diff) +13 -0
+	test_cmp expected actual
+}
+
+test_expect_success setup '
+	cp -R ../t1013/objects .git/
+	git --version
+'
+
+test_expect_success 'read standard-format loose objects' '
+	git cat-file tag 8d4e360d6c70fbd72411991c02a09c442cf7a9fa &&
+	git cat-file commit 6baee0540ea990d9761a3eb9ab183003a71c3696 &&
+	git ls-tree 7a37b887a73791d12d26c0d3e39568a8fb0fa6e8 &&
+	assert_blob_equals "257cc5642cb1a054f08cc83f2d943e56fd3ebe99" "foo\n"

Isn't it unportable to expect "\n" to be kept or expanded by "echo"?

Will squash the following in (and I have another patch on top to consolidate the definition of $LF in this and other test scripts).

Thanks.

-- >8 --
Subject: [PATCH] fixup! Tolerate zlib deflation with window

---
 t/t1013-loose-object-format.sh |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)
t/t1013-loose-object-format.sh +5 -3
index b5ac46d..36b4027 100755
@@ -26,10 +26,12 @@ standard format, deflated with 4KB window size: Agit/JGit on Android
 '
 
 . ./test-lib.sh
+LF='
+'
 
 assert_blob_equals() {
-	echo -n $2 > expected &&
-	git cat-file -p $1 > actual &&
+	printf "%s" "$2" >expected &&
+	git cat-file -p "$1" >actual &&
 	test_cmp expected actual
 }
 
@@ -42,7 +44,7 @@ test_expect_success 'read standard-format loose objects' '
 	git cat-file tag 8d4e360d6c70fbd72411991c02a09c442cf7a9fa &&
 	git cat-file commit 6baee0540ea990d9761a3eb9ab183003a71c3696 &&
 	git ls-tree 7a37b887a73791d12d26c0d3e39568a8fb0fa6e8 &&
-	assert_blob_equals "257cc5642cb1a054f08cc83f2d943e56fd3ebe99" "foo\n"
+	assert_blob_equals "257cc5642cb1a054f08cc83f2d943e56fd3ebe99" "foo$LF"
 '
 
 test_expect_success 'read experimental-format loose objects' '
Signature · 1 line
1.7.6.409.ge7a85
#8 lore raw
In reply to [email protected]

t/t1013-loose-object-format.sh line 39

Quoted 4 lines · [email protected] writes:
t/t1013-loose-object-format.sh new file +4 -0
+test_expect_success setup '
+	cp -R ../t1013/objects .git/
+	git --version
+'

This "my parent directory" reference ../t1013 does not work when you use a separate test directory (e.g. to use ramdisks) with --root=/some/where/

I've squashed

	cp -R "$TEST_DIRECTORY/t1013/objects" .git/

as a fix while queuing.

Thanks.