sodium-native: Segmentation fault using 3.4.1 (musl, x64)

Given the following Dockerfile

FROM node:16.16.0-alpine3.16

ENTRYPOINT "sh"

RUN apk add --no-cache \
    python3 \
    build-base \
    cmake \
    cairo-dev \
    jpeg-dev \
    pango-dev \
    git

WORKDIR /app

# Adapted from https://github.com/sodium-friends/sodium-native/blob/32ae395ad558ec86680c547b909daf3a13b2dea8/test/crypto_secretbox.js#L4-L32
RUN echo " \
const sodium = require('sodium-native');\
\
const message = Buffer.from('Hej, Verden!');\
const output = Buffer.alloc(message.length + sodium.crypto_secretbox_MACBYTES);\
\
const key = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES);\
sodium.randombytes_buf(key);\
\
const nonce = Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES);\
sodium.randombytes_buf(nonce);\
\
const result = Buffer.alloc(output.length - sodium.crypto_secretbox_MACBYTES);\
\
sodium.crypto_secretbox_easy(output, message, nonce, key);\
\
sodium.crypto_secretbox_open_easy(result, output, nonce, key);\
\
console.log({ result, message });\
" > index.js

RUN npm init -y && npm install --save sodium-native@3.4.1

Run docker build -t sodium . && docker run sodium followed by node index.js inside the image to see it segfault.

If you install sodium-native@3.3.0 instead, it runs fine.


Note that I’m not able to reproduce this when built on an M1 mac, only on an x64 one. So this might be instruction set dependent.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 4
  • Comments: 17 (8 by maintainers)

Most upvoted comments

Ya, I think we figured it out, a new experimental API is using stuff from libc, so just need to use a helper for that instead, and I’ll make a new build. Using 3.3.0 is fine until then, the only change is the addition of the experimental API which you prob do not care about 😃

I experience the same. Now I’m using sodium-native@3.3.0 and then it works fine. But 3.4.0 and 3.4.1 produce a segmentation fault. However, this is not on a Mac, this is happening to me inside Docker container with Alpine Linux running on an Ubuntu host and also on my local machine with the same Docker container running on macOS Intel.

I have to admit I got to this solution mostly by try and error and have no further experience. If I can provide anything or test out anything that might be helpful, I would appreciate a quick explanation of what to do.

There’s no build folder for either 3.3.0 or 3.4.1. It shows up if I do cd node_modules/sodium-native && npm run dev. Doing so makes it work for 3.4.1. (🎉)

Should https://github.com/sodium-friends/sodium-native/blob/32ae395ad558ec86680c547b909daf3a13b2dea8/package.json#L17 be postinstall? But even so, it works without build on 3.3.0.

@SimenB can you see if it triggered recompilation at install on 3.3.0 and not on 3.4.1? afaik since this using musl it would need to be compiled

ah sorry you mention that