phxql / argon2-jvm

Argon2 Binding for the JVM
GNU Lesser General Public License v3.0
330 stars 32 forks source link

SIGSEGV when running low on memory #75

Closed AlexGustafsson closed 3 years ago

AlexGustafsson commented 3 years ago

On a system with about 400MiB free RAM, trying to hash a password with a memory cost of 256 MiB causes a SIGSEGV.

Excerpt from the log, showing the cause to be this package:

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  com.sun.jna.Native.invokeInt(Lcom/sun/jna/Function;JI[Ljava/lang/Object;)I+0
j  com.sun.jna.Function.invoke([Ljava/lang/Object;Ljava/lang/Class;ZI)Ljava/lang/Object;+211
j  com.sun.jna.Function.invoke(Ljava/lang/reflect/Method;[Ljava/lang/Class;Ljava/lang/Class;[Ljava/lang/Object;Ljava/util/Map;)Ljava/lang/Object;+271
j  com.sun.jna.Library$Handler.invoke(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;+390
j  com.sun.proxy.$Proxy40.argon2id_hash_encoded(Lde/mkammerer/argon2/jna/JnaUint32;Lde/mkammerer/argon2/jna/JnaUint32;Lde/mkammerer/argon2/jna/JnaUint32;[BLde/mkammerer/argon2/jna/Size_t;[BLde/mkammerer/argon2/jna/Size_t;Lde/mkammerer/argon2/jna/Size_t;[BLde/mkammerer/argon2/jna/Size_t;)I+64
j  de.mkammerer.argon2.Argon2id.callLibraryHash([B[BLde/mkammerer/argon2/jna/JnaUint32;Lde/mkammerer/argon2/jna/JnaUint32;Lde/mkammerer/argon2/jna/JnaUint32;[B)I+55
j  de.mkammerer.argon2.BaseArgon2.hashBytes(III[B[B)Ljava/lang/String;+96
j  de.mkammerer.argon2.BaseArgon2.hashBytes(III[B)Ljava/lang/String;+14
j  de.mkammerer.argon2.BaseArgon2.hash(III[CLjava/nio/charset/Charset;)Ljava/lang/String;+16
j  de.mkammerer.argon2.BaseArgon2.hash(III[C)Ljava/lang/String;+9

/proc/meminfo at the time of the crash:

/proc/meminfo:
MemTotal:        6115268 kB
MemFree:          415488 kB
MemAvailable:     688780 kB
Buffers:           48792 kB
Cached:           437468 kB
SwapCached:       101536 kB
Active:          2851072 kB
Inactive:        2615364 kB
Active(anon):    2573784 kB
Inactive(anon):  2431860 kB
Active(file):     277288 kB
Inactive(file):   183504 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:       4192252 kB
SwapFree:        1587112 kB
Dirty:               252 kB
Writeback:             0 kB
AnonPages:       4932544 kB
Mapped:           137060 kB
Shmem:             25284 kB
Slab:             117424 kB
SReclaimable:      51756 kB
SUnreclaim:        65668 kB
KernelStack:       22176 kB
PageTables:        44112 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     7249884 kB
Committed_AS:   16778784 kB
VmallocTotal:   34359738367 kB
VmallocUsed:           0 kB
VmallocChunk:          0 kB
HardwareCorrupted:     0 kB
AnonHugePages:         0 kB
ShmemHugePages:        0 kB
ShmemPmdMapped:        0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:      120704 kB
DirectMap2M:     5122048 kB
DirectMap1G:     3145728 kB

There's been some discussion here: https://github.com/P-H-C/phc-winner-argon2/issues/5. Would it be feasible to check the amount of available RAM before invoking the JNA bindings and throw an exception if there's too little? As can be seen in the /proc/meminfo output, this might not be accurate as there's a SIGSEGV despite 256MiB being available.

AlexGustafsson commented 3 years ago

This seems to occur pretty much no matter what memory cost I set, even for 65536KiB as in the examples. The service is running in an Alpine container. This seems to mostly affect Docker containers as the bare application works fine when running outside of Docker.

phxql commented 3 years ago

I haven't heard of any memory problems regarding docker. Could you paste the code which results in the failure and the docker commandline to run it here?

phxql commented 3 years ago

Alpine uses the musl libc. the embedded argon2 is compiled against glibc, that's the reason for the SIGSEGV. The workaround is to install argon2 via the apk package manager:

apk add argon2-dev

I will add that to the documentation. Please let me know if this fixes the problem for you.

remylavergne commented 3 years ago

I'm running the same issue.. Do you have found a solution ?

AlexGustafsson commented 3 years ago

Have you tried the suggested fix? Unfortunately I'm no longer part of the project where this issue came up, so I'm unable to test it in context.

remylavergne commented 3 years ago

I will try to modify my Dockerfile :)

PS: It's working :) Thanks !

phxql commented 3 years ago

Great :) I've added that to the documentation.