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

Memory leak when using zlib.inflate to inflate invalid data v10.9.0 #22705

Closed
aclelland opened this issue Sep 5, 2018 · 2 comments
Closed

Memory leak when using zlib.inflate to inflate invalid data v10.9.0 #22705

aclelland opened this issue Sep 5, 2018 · 2 comments
Labels
confirmed-bug Issues with confirmed bugs. memory Issues and PRs related to the memory management or memory footprint. zlib Issues and PRs related to the zlib subsystem.

Comments

@aclelland
Copy link

Version: 10.9.0
Platform: Linux Ubuntu 3.13.0-36-generic
Subsystem: zlib

I've recently updated a large project from Node 8.11.3 to 10.9.0 but the service crashes after a few minutes because of memory exhaustion (4GB RAM). I've narrowed down the issue to the use of the asynchronous zlib.inflate method if the input isn't a valid input.

I've been able to create a small script which shows the issue

const crypto = require('crypto'), zlib = require('zlib');

const LOOPS = 10 * 1000;
let body = process.argv[2] === "valid" ? zlib.deflateSync(crypto.randomBytes(1024)) : 
crypto.randomBytes(1024);

let totalRuns = 0;
let initialRSS = process.memoryUsage().rss;
setInterval(() => {

for (let i = 0; i < LOOPS; i++) {
    zlib.inflate(body, (error, result) => { });
}

totalRuns += LOOPS

let currentRSS = process.memoryUsage().rss;

console.log(totalRuns, currentRSS, currentRSS - initialRSS);

}, 100);

When using Node 8.11.3 I get the following output from that script, you can see memory usage levels out around 280MiB.

Loops ** Current RSS** RSS Increase
10000 150556672 132362240
100000 281853952 263659520
200000 294563840 276369408
1000000 297381888 279158784

Switching to Node 10.9.0 I get the following output

Loops ** Current RSS** RSS Increase
10000 149655552 130969600
100000 790159360 771473408
200000 1477070848 1458384896

After 200k iterations the 8.11.3 version uses 280MiB but the 10.9.0 version is using 1400MiB of memory. After about 300k iterations using 10.9.0 the process crashes because it runs out of RAM.

Running the script with the "valid" parameter to send zlib a valid stream shows an increase in memory but it's small and eventually levels out and doesn't run out of memory.

node memoryleak.js valid
10000 156794880 137863168
100000 505155584 486223872
200000 546017280 527085568

I've corrected our service so that we check for invalid streams before trying to inflate them but I thought that there is potential for a denial of service against any service which uses zlib.inflate to decompress user input.

@addaleax addaleax added confirmed-bug Issues with confirmed bugs. zlib Issues and PRs related to the zlib subsystem. memory Issues and PRs related to the memory management or memory footprint. labels Sep 5, 2018
addaleax added a commit to addaleax/node that referenced this issue Sep 5, 2018
Don’t toggle the weak/strong reference flag from the error
handler, that’s too confusing. Instead, always do it in the
code that handles the write call.

Fixes: nodejs#22705
@addaleax
Copy link
Member

addaleax commented Sep 5, 2018

Fix is in #22713 :)

targos pushed a commit that referenced this issue Sep 6, 2018
Don’t toggle the weak/strong reference flag from the error
handler, that’s too confusing. Instead, always do it in the
code that handles the write call.

Fixes: #22705

PR-URL: #22713
Reviewed-By: James M Snell <jasnell@gmail.com>
@laino
Copy link

laino commented Sep 7, 2018

This might not just affect invalid data. When using the 'ws' module with per message deflate enabled, my application quickly reaches 3GB memory usage. Without it uses about 800MB.

I only managed to grab one heapdump so far before my server inevitably runs out of memory. Zlib internals looked like a major culprit.

Here's what I'm looking at on Node v10.9.0: https://i.imgur.com/byTUInU.png
I'm not too good at reading these, but this really looks like zlib being the culprit to me.

If this is a separate issue I'll open a ticket.

Edit: Nevermind, I somehow missed the fact that this ticket was about inflate, not deflate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. memory Issues and PRs related to the memory management or memory footprint. zlib Issues and PRs related to the zlib subsystem.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants