GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Piping streams into SHA3 without end: false crashes · Issue #28245 · nodejs/node

Piping streams into SHA3 without end: false crashes #28245

New issue
Copy link
New issue
Copy link
Closed
Closed
Piping streams into SHA3 without end: false crashes#28245
Copy link
Labels
confirmed-bugIssues and PRs for confirmed bugs.cryptoIssues and PRs related to the crypto subsystem.opensslIssues and PRs related to the OpenSSL dependency.streamIssues and PRs related to Node.js streams.

Description

@tniessen
tniessen
opened on Jun 15, 2019
Issue body actions
  • Version: 12.4.0
  • Platform: Windows 10 x64 and Linux 4.4.0-042stab138.1
  • Subsystem: crypto or deps

This code causes a segmentation fault in 12.4.0:

const crypto = require('crypto');
const fs = require('fs');

const myOwnCode = fs.createReadStream(__filename);
const copy = fs.createWriteStream(`${__filename}.copy`);
const hash = crypto.createHash('sha3-512');
myOwnCode.pipe(hash);
myOwnCode.pipe(copy).on('finish', () => {
  hash.digest();
});

This seems to be caused by pipe calling hash._flush when end is not set to false. This code also causes a segmentation fault:

const crypto = require('crypto');

const hash = crypto.createHash('sha3-512');
hash._flush(() => console.log('Flushed'));
hash.digest();

This seems to be at least partially caused by the implementation of _flush:

node/lib/internal/crypto/hash.js

Lines 54 to 57 in 908292c

Hash.prototype._flush = function _flush(callback) {
this.push(this[kHandle].digest());
callback();
};

It bypasses the this[kState][kFinalized] safeguard:

node/lib/internal/crypto/hash.js

Lines 79 to 91 in 908292c

Hash.prototype.digest = function digest(outputEncoding) {
const state = this[kState];
if (state[kFinalized])
throw new ERR_CRYPTO_HASH_FINALIZED();
outputEncoding = outputEncoding || getDefaultEncoding();
if (normalizeEncoding(outputEncoding) === 'utf16le')
throw new ERR_CRYPTO_HASH_DIGEST_NO_UTF16();
// Explicit conversion for backward compatibility.
const ret = this[kHandle].digest(`${outputEncoding}`);
state[kFinalized] = true;
return ret;
};

Note that this bug only happens when using SHA3, sha256 seems to be working just fine, so there might also be some weirdness in OpenSSL.

cc @mcollina @nodejs/crypto @nodejs/streams

Activity

  1. tniessen
    added
    cryptoIssues and PRs related to the crypto subsystem.
    opensslIssues and PRs related to the OpenSSL dependency.
    streamIssues and PRs related to Node.js streams.
    confirmed-bugIssues and PRs for confirmed bugs.
    on Jun 15, 2019
  2. tniessen commented on Jun 15, 2019

    @tniessen
    tniessen
    on Jun 15, 2019
    MemberAuthor
    More actions

    Stack trace:

    Thread 1 "node" received signal SIGSEGV, Segmentation fault.
    0x00000000016a11ad in SHA3_absorb ()
    (gdb) bt
    #0  0x00000000016a11ad in SHA3_absorb ()
    #1  0x00000000015ae107 in sha3_final ()
    #2  0x0000000001593d3f in EVP_DigestFinal_ex ()
    #3  0x0000000000a87230 in node::crypto::Hash::HashDigest(v8::FunctionCallbackInfo<v8::Value> const&) ()
    #4  0x0000000000b9cae6 in v8::internal::MaybeHandle<v8::internal::Object> v8::internal::(anonymous namespace)::HandleApiCallHelper<false>(v8::internal::Isolate*, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::FunctionTemplateInfo>, v8::internal::Handle<v8::internal::Object>, v8::internal::BuiltinArguments) ()
    #5  0x0000000000b9e9f9 in v8::internal::Builtin_HandleApiCall(int, unsigned long*, v8::internal::Isolate*) ()
    #6  0x0000000001a93942 in Builtins_CEntry_Return1_DontSaveFPRegs_ArgvOnStack_NoBuiltinExit () at ../../deps/v8/src/builtins/base.tq:1866
    #7  0x00000000019fd584 in Builtins_InterpreterEntryTrampoline () at ../../deps/v8/src/builtins/base.tq:107
  3. addaleax commented on Jun 16, 2019

    @addaleax
    addaleax
    on Jun 16, 2019
    Member
    More actions

    Valgrind info for the first example:

    ==4491== Invalid read of size 8
    ==4491==    at 0x21311C7: SHA3_absorb (keccak1600-x86_64.s:387)
    ==4491==    by 0x203230D: sha3_final (m_sha3.c:120)
    ==4491==    by 0x201772E: EVP_DigestFinal_ex (digest.c:171)
    ==4491==    by 0xE4305D: node::crypto::Hash::HashDigest(v8::FunctionCallbackInfo<v8::Value> const&) (node_crypto.cc:4640)
    ==4491==    by 0xFB4290: v8::internal::FunctionCallbackArguments::Call(v8::internal::CallHandlerInfo) (api-arguments-inl.h:157)
    ==4491==    by 0xFB7213: v8::internal::MaybeHandle<v8::internal::Object> v8::internal::(anonymous namespace)::HandleApiCallHelper<false>(v8::internal::Isolate*, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::FunctionTemplateInfo>, v8::internal::Handle<v8::internal::Object>, v8::internal::BuiltinArguments) (builtins-api.cc:109)
    ==4491==    by 0xFB8A38: v8::internal::Builtin_Impl_HandleApiCall(v8::internal::BuiltinArguments, v8::internal::Isolate*) (builtins-api.cc:139)
    ==4491==    by 0xFB956C: v8::internal::Builtin_HandleApiCall(int, unsigned long*, v8::internal::Isolate*) (builtins-api.cc:127)
    ==4491==    by 0x1C81D1F: ??? (builtins-async-iterator-gen.cc:256)
    ==4491==    by 0x1A05334: ??? (builtins-string-gen.cc:1971)
    ==4491==    by 0x19EF05B: ??? (builtins-internal-gen.cc:700)
    ==4491==    by 0x1A05334: ??? (builtins-string-gen.cc:1971)
    ==4491==  Address 0x9ed8dd0 is 0 bytes after a block of size 400 alloc'd
    ==4491==    at 0x4C2FDFB: malloc (vg_replace_malloc.c:309)
    ==4491==    by 0x204422F: CRYPTO_malloc (mem.c:222)
    ==4491==    by 0x2044262: CRYPTO_zalloc (mem.c:230)
    ==4491==    by 0x20175AB: EVP_DigestInit_ex (digest.c:129)
    ==4491==    by 0xE42BEF: node::crypto::Hash::HashInit(char const*) (node_crypto.cc:4585)
    ==4491==    by 0xE42B02: node::crypto::Hash::New(v8::FunctionCallbackInfo<v8::Value> const&) (node_crypto.cc:4573)
    ==4491==    by 0xFB4290: v8::internal::FunctionCallbackArguments::Call(v8::internal::CallHandlerInfo) (api-arguments-inl.h:157)
    ==4491==    by 0xFB4E6F: v8::internal::MaybeHandle<v8::internal::Object> v8::internal::(anonymous namespace)::HandleApiCallHelper<true>(v8::internal::Isolate*, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::FunctionTemplateInfo>, v8::internal::Handle<v8::internal::Object>, v8::internal::BuiltinArguments) (builtins-api.cc:109)
    ==4491==    by 0xFB8983: v8::internal::Builtin_Impl_HandleApiCall(v8::internal::BuiltinArguments, v8::internal::Isolate*) (builtins-api.cc:135)
    ==4491==    by 0xFB956C: v8::internal::Builtin_HandleApiCall(int, unsigned long*, v8::internal::Isolate*) (builtins-api.cc:127)
    ==4491==    by 0x1C81D1F: ??? (builtins-async-iterator-gen.cc:256)
    ==4491==    by 0x19F5799: ??? (base.tq:175)
    
  4. tniessen
    closed this as completedin fc50e6bon Jun 18, 2019
  5. targos
    added a commit that references this issue on Jul 2, 2019

    crypto: fix crash when calling digest after piping

    990feaf
  6. evanlucas
    added a commit that references this issue on Feb 4, 2021

    crypto: fix crash when calling digest after piping

    2a0571a
  7. Sunrisepeak
    added a commit that references this issue on Jul 25, 2026

    fix(node:crypto): hash digest survives piping; AEAD tag-length + setA…

    3b472c1
  8. sjarmak
    added a commit that references this issue on Jul 27, 2026

    crypto: fix crash when calling digest after piping

    fc50e6b
  9. mcollina
    added 2 commits that reference this issue on Aug 17, 2026

    crypto: prevent Hmac.digest() from returning uninitialized memory

    e41218a

    crypto: prevent Hmac.digest() from returning uninitialized memory

    49e88ce
  10. nodejs-github-bot
    added a commit that references this issue on Aug 26, 2026

    crypto: prevent Hmac.digest() from returning uninitialized memory

    6737aa3
  11. aduh95
    added 2 commits that reference this issue on Aug 29, 2026

    crypto: prevent Hmac.digest() from returning uninitialized memory

    b272bc9

    crypto: prevent Hmac.digest() from returning uninitialized memory

    a7524cb
  12. Jarred-Sumner
    added a commit that references this issue on Sep 10, 2026

    node:crypto: make Hash update()/copy() after end() throw instead of r…

    e5f9986
  13. aduh95
    added 2 commits that reference this issue on Sep 15, 2026

    crypto: prevent Hmac.digest() from returning uninitialized memory

    656504f

    crypto: prevent Hmac.digest() from returning uninitialized memory

    98ca5e9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues and PRs for confirmed bugs.cryptoIssues and PRs related to the crypto subsystem.opensslIssues and PRs related to the OpenSSL dependency.streamIssues and PRs related to Node.js streams.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.