Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CVE-2018-25032 (zlib memory corruption on deflate) #605

Closed
vielmetti opened this issue Mar 26, 2022 · 29 comments
Closed

CVE-2018-25032 (zlib memory corruption on deflate) #605

vielmetti opened this issue Mar 26, 2022 · 29 comments

Comments

@vielmetti
Copy link

CVE-2018-25032 tracks a bug in zlib 1.2.11 which allows memory corruption when deflating (i.e., when compressing) if the input has many distant matches.

There is a fix from @madler at 5c44459

@taviso reports at https://www.openwall.com/lists/oss-security/2022/03/24/1 that this patch never made it into a release, and at the time of writing no distros had picked it up as a fix.

vielmetti referenced this issue Mar 26, 2022
This bug was reported by Danilo Ramos of Eideticom, Inc. It has
lain in wait 13 years before being found! The bug was introduced
in zlib 1.2.2.2, with the addition of the Z_FIXED option. That
option forces the use of fixed Huffman codes. For rare inputs with
a large number of distant matches, the pending buffer into which
the compressed data is written can overwrite the distance symbol
table which it overlays. That results in corrupted output due to
invalid distances, and can result in out-of-bound accesses,
crashing the application.

The fix here combines the distance buffer and literal/length
buffers into a single symbol buffer. Now three bytes of pending
buffer space are opened up for each literal or length/distance
pair consumed, instead of the previous two bytes. This assures
that the pending buffer cannot overwrite the symbol table, since
the maximum fixed code compressed length/distance is 31 bits, and
since there are four bytes of pending space for every three bytes
of symbol space.
@vielmetti
Copy link
Author

PR to fix in nixpkgs from @twz123 NixOS/nixpkgs#165642

@vielmetti
Copy link
Author

PR to fix in Alpine aports from @ncopa https://git.alpinelinux.org/aports/commit/?id=361df5902aa1e81594b17f06a13e10527dfb8aed and alpinelinux/aports@361df59

@ynezz
Copy link

ynezz commented Mar 26, 2022

Fixed in OpenWrt down to 19.07 release with openwrt/openwrt@b3aa290

@vielmetti
Copy link
Author

Fixed in Debian, the relevant bug is https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1008265 and the version with the fix is zlib/1:1.2.11.dfsg-4

@vielmetti
Copy link
Author

vielmetti commented Mar 26, 2022

Ubuntu has tracked this as https://ubuntu.com/security/CVE-2018-25032 and has a proposed patch which will bring the version to 1:1.2.11.dfsg-2ubuntu9, details are up to date at https://launchpad.net/ubuntu/+source/zlib . Ubuntu notes that "rsync" is also affected and is fixed in 3.1.3-6.

@vielmetti
Copy link
Author

Fedora and Red Hat are tracking this bug at https://bugzilla.redhat.com/show_bug.cgi?id=2067945 and there is no "fixed in" version as of this writing. It's also marked as affecting the packages "mingw-zlib" and "rsync".

@briangreenery
Copy link

briangreenery commented Mar 26, 2022

Do you know if the full fix requires both of these commits?

5c44459
4346a16

I only see the first commit referenced as related to the CVE, but the linked NixOS and Alpine issues are applying both commits. I'm not sure how they knew the second commit should also be applied.

@vielmetti
Copy link
Author

@briangreenery It looks like this Ubuntu patch at https://launchpad.net/ubuntu/+source/zlib/1:1.2.11.dfsg-2ubuntu9 also picks up the deflatePrime() validation check you reference at 4346a16 .

@vielmetti
Copy link
Author

@taviso posts a reproducer at https://www.openwall.com/lists/oss-security/2022/03/26/1 with a sample input file that triggers the bug when run against a minimal compressor that uses zlib.

@vielmetti
Copy link
Author

In https://twitter.com/OpenBSD_src/status/1507325285665968130 there is a reference to a (forthcoming) errata for OpenBSD with the name "errata/7.0/018_zlib.patch.sig".

From the looks of it this hasn't been published just yet, but when it does I expect it to be at https://ftp.openbsd.org/pub/OpenBSD/patches/7.0/common/

The reference acknowledges mbuhl and millert of the OpenBSD project.

From the looks of it, @bluhm is also doing work for OpenBSD on zlib, see e.g.
openbsd/src@b1b1dcf
which references 38e8ce3
fixing the CLEAR_HASH macro.

@vielmetti
Copy link
Author

In zlib-ng/zlib-ng#1208 @nmoinvaz adds a unit test for CVE-2018-25032 using test/CVE-2018-25032/test.txt which is a file beginning with AAABAACAA that was contributed by @taviso .

zlib-ng passes the unit test for that file, whereas zlib (master) does not.

@madler
Copy link
Owner

madler commented Mar 27, 2022

So much excitement! I will release the develop branch to master. Note that this bug is only when Z_FIXED is used, which it never is in production code. The only reason it's there is to facilitate testing of the decompression of fixed blocks.

@rurban
Copy link

rurban commented Mar 27, 2022

not really, because of this condition:

https://github.com/madler/zlib/blob/master/trees.c#L976

} else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) {

@madler
Copy link
Owner

madler commented Mar 27, 2022

I don't understand your comment rurban.

@ncopa
Copy link

ncopa commented Mar 27, 2022

Do you know if the full fix requires both of these commits?

5c44459 4346a16

I only see the first commit referenced as related to the CVE, but the linked NixOS and Alpine issues are applying both commits. I'm not sure how they knew the second commit should also be applied.

When cherry-picking it to Alpine Linux I saw that there was a second commit (4346a16) that was done the same day as the commit that fixes the CVE. It happens that security fixes introduces regressions or are incomplete so I often look at commits that happens directly before and after security fixes. I also thought that it looked like 4346a16 commit could have security implication, but I have no idea if its does. I included it just in case, and because the change was done at the same time as the security fix.

@ebiggers
Copy link

Note, this is also reproducible with Z_DEFAULT_STRATEGY when memLevel=1 and windowBits=15.

@madler
Copy link
Owner

madler commented Mar 28, 2022

@ebiggers Can you provide an example?

@madler
Copy link
Owner

madler commented Mar 28, 2022

zlib 1.2.12 is now live on the master branch here, which includes the remedy from 2018.

@vielmetti
Copy link
Author

Closed by 21767c6 , thanks @madler .

@taviso
Copy link

taviso commented Mar 28, 2022

@madler Eric posted a reproducer that does work with Z_DEFAULT_STRATEGY to oss-security. The file is here: https://www.openwall.com/lists/oss-security/2022/03/28/1

IMHO, that makes this bug far more critical.

@Adenilson
Copy link

For Chromium (and AOSP since it uses Chromium's zlib starting with Android R), we actively backported the fix from 'develop' all the way back in 2018:
https://chromium.googlesource.com/chromium/src/+/777b3763b4f45e4e10f6ff3c6cd46c6e086dc243

Plus, we have fuzzers covering Z_FIXED:
https://source.chromium.org/chromium/chromium/src/+/main:third_party/zlib/contrib/tests/fuzzers/deflate_fuzzer.cc;l=30

@Adenilson
Copy link

Adenilson commented Mar 28, 2022

A unit test stressing the bug can be found at:
https://chromium-review.googlesource.com/c/chromium/src/+/3550971

frezbo added a commit to frezbo/tools that referenced this issue Mar 28, 2022
Bump zlib to 1.2.12

Fixes: [CVE-2018-25032](https://nvd.nist.gov/vuln/detail/CVE-2018-25032)

Refs:
 - GHSA-jc36-42cf-vqwj
 - madler/zlib#605

Signed-off-by: Noel Georgi <git@frezbo.dev>
facebook-github-bot pushed a commit to facebook/rocksdb that referenced this issue Mar 29, 2022
Summary:
Zlib (https://www.zlib.net/) has been updated to 1.2.12 due to CVE-2018-25032

- https://nvd.nist.gov/vuln/detail/CVE-2018-25032
- madler/zlib#605

The source .tar.gz is no longer available, and the Makefile for rocksdb now fails as a result. This PR updates the dependency to the newer (and available) version, 1.2.12

Pull Request resolved: #9764

Reviewed By: ajkr

Differential Revision: D35220367

Pulled By: jay-zhuang

fbshipit-source-id: 1f68ff8f048a6dba42077f048ac143468f0e2478
@Hanmac
Copy link

Hanmac commented Mar 31, 2022

@madler Eric posted a reproducer that does work with Z_DEFAULT_STRATEGY to oss-security. The file is here: https://www.openwall.com/lists/oss-security/2022/03/28/1

IMHO, that makes this bug far more critical.

does the new 1.2.12 release fixes this problem too, or will there be a followup issue?

@vittring
Copy link

vittring commented Apr 1, 2022

I just saw this CVE using Snyk to scan a container image. Glad the bug has a patch! Thanks for the fix!

@ljavorsk
Copy link

ljavorsk commented Apr 8, 2022

Could anyone please give me some guidance regarding the reproducer?
I've managed to compile the deflate.c file and use its binary with the reproducer .txt file, but no error occurred in my case.

Then I saw, that I should you the zpipe utility, but that's where the problem arrives.
I'm fairly new to the zlib and I'm not really sure how to use the zpipe and what do you refer to as "garbage" from its output.

I would be so grateful for any kind of help with this.

How I've used the zpipe:
wget https://www.zlib.net/zpipe.c -> Downloaded it from this source
cc -o zpipe zpipe.c -lz -> Found that it should be compiled like this
./zpipe < CVE-2018-25032.txt > decompres -> Used it according to the manual
How do I determine if the decompres file is garbage?

@ebiggers
Copy link

ebiggers commented Apr 8, 2022

Could anyone please give me some guidance regarding the reproducer?

I've` managed to compile the deflate.c file and use its binary with the reproducer .txt file, but no error occurred in my case.

It is an out-of-bounds memory read, which is undefined behavior, so a crash is not guaranteed. You can compile both zlib and the program using gcc's -fsanitize=address option to get AddressSanitizer to report the out-of-bounds read.

I'm fairly new to the zlib and I'm not really sure how to use the zpipe and what do you refer to as "garbage" from its output.

The point is that if CVE-2018-25032.txt is deflated and then inflated, it doesn't match the original file. There are many ways to test this. The deflate.c programs that Tavis and I posted only have the deflate(), as they were only intended to reproduce the out-of-bounds access. So you would need to add an inflate() if you wanted to verify the issue with the round trip.

@ebiggers
Copy link

ebiggers commented Apr 8, 2022

@madler Eric posted a reproducer that does work with Z_DEFAULT_STRATEGY to oss-security. The file is here: https://www.openwall.com/lists/oss-security/2022/03/28/1
IMHO, that makes this bug far more critical.

does the new 1.2.12 release fixes this problem too, or will there be a followup issue?

This was essentially the same problem, so it was fixed by the same commit and thus is fixed in 1.2.12.

@ljavorsk
Copy link

ljavorsk commented Apr 11, 2022

The point is that if CVE-2018-25032.txt is deflated and then inflated, it doesn't match the original file. There are many ways to test this. The deflate.c programs that Tavis and I posted only have the deflate(), as they were only intended to reproduce the out-of-bounds access. So you would need to add an inflate() if you wanted to verify the issue with the round trip.

And did someone (if you know of anything that could be used in this test) create the inflate() already so I don't invent the wheel again?

@vielmetti
Copy link
Author

@ljavorsk this commit in @zlib-ng zlib-ng/zlib-ng#1208 from @nmoinvaz adds a unit test and a "minideflate.c" and an "inflate.c" which I think is what you are looking for. No need to reinvent this particular wheel.

ajkr pushed a commit to facebook/rocksdb that referenced this issue Apr 18, 2022
Summary:
Zlib (https://www.zlib.net/) has been updated to 1.2.12 due to CVE-2018-25032

- https://nvd.nist.gov/vuln/detail/CVE-2018-25032
- madler/zlib#605

The source .tar.gz is no longer available, and the Makefile for rocksdb now fails as a result. This PR updates the dependency to the newer (and available) version, 1.2.12

Pull Request resolved: #9764

Reviewed By: ajkr

Differential Revision: D35220367

Pulled By: jay-zhuang

fbshipit-source-id: 1f68ff8f048a6dba42077f048ac143468f0e2478
Myasuka pushed a commit to Myasuka/rocksdb that referenced this issue Jul 18, 2022
…#9764)

Summary:
Zlib (https://www.zlib.net/) has been updated to 1.2.12 due to CVE-2018-25032

- https://nvd.nist.gov/vuln/detail/CVE-2018-25032
- madler/zlib#605

The source .tar.gz is no longer available, and the Makefile for rocksdb now fails as a result. This PR updates the dependency to the newer (and available) version, 1.2.12

Pull Request resolved: facebook#9764

Reviewed By: ajkr

Differential Revision: D35220367

Pulled By: jay-zhuang

fbshipit-source-id: 1f68ff8f048a6dba42077f048ac143468f0e2478
Myasuka pushed a commit to Myasuka/frocksdb that referenced this issue Jul 18, 2022
Summary:
Zlib (https://www.zlib.net/) has been updated to 1.2.12 due to CVE-2018-25032

- https://nvd.nist.gov/vuln/detail/CVE-2018-25032
- madler/zlib#605

The source .tar.gz is no longer available, and the Makefile for rocksdb now fails as a result. This PR updates the dependency to the newer (and available) version, 1.2.12

Pull Request resolved: facebook/rocksdb#9764

Reviewed By: ajkr

Differential Revision: D35220367

Pulled By: jay-zhuang

fbshipit-source-id: 1f68ff8f048a6dba42077f048ac143468f0e2478
Myasuka pushed a commit to Myasuka/frocksdb that referenced this issue Jul 18, 2022
Summary:
Zlib (https://www.zlib.net/) has been updated to 1.2.12 due to CVE-2018-25032

- https://nvd.nist.gov/vuln/detail/CVE-2018-25032
- madler/zlib#605

The source .tar.gz is no longer available, and the Makefile for rocksdb now fails as a result. This PR updates the dependency to the newer (and available) version, 1.2.12

Pull Request resolved: facebook/rocksdb#9764

Reviewed By: ajkr

Differential Revision: D35220367

Pulled By: jay-zhuang

fbshipit-source-id: 1f68ff8f048a6dba42077f048ac143468f0e2478
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet