Repository navigation
Description
After looking around a java+JNI library with gdb while trying to debug the build of Android Open Source Project, I got to this point:
> │10749 Unique_SSL_CTX sslCtx(SSL_CTX_new(SSLv23_method()));
│
│10750 Unique_SSL ssl(SSL_new(sslCtx.get()));
which in assembly is
>│0x3ffcd67fd50 <NativeCrypto_get_cipher_names(_JNIEnv*, _jclass*, _jstring*)+64> callq 0x3ffcd669060 <SSLv23_method@plt>
Now, SSLv23_method is statically linked, but the code somehow ends up ignoring the statically linked copy and goes straight to the dynamic version:
(gdb) info address SSLv23_method
Symbol "SSLv23_method" is a function at address 0x3ffb9ad7eb0.
(gdb) info address SSLv23_method@plt
Symbol "SSLv23_method@plt" is at 0x3ffb99f9060 in a file compiled without debugging.
The compile/link command is the following:
[ 83% 5/6] /bin/bash -c "prebuilts/clang/host/linux-x86/clang-copperhead/bin/clang++ -Wl,-rpath-link=out/host/linux-x86/obj/lib -Wl,-rpath,\\\$ORIGIN/../lib64 -Wl,-rpath,\\\$ORIGIN/lib64 -shared -Wl,-soname,libconscrypt_openjdk_jni.so -Lout/host/linux-x86/obj/lib -m64 -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,--no-undefined-version --gcc-toolchain=prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.15-4.8 --sysroot prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.15-4.8/sysroot -Bprebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.15-4.8/x86_64-linux/bin -Bprebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.15-4.8/lib/gcc/x86_64-linux/4.8 -Lprebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.15-4.8/lib/gcc/x86_64-linux/4.8 -Lprebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.15-4.8/x86_64-linux/lib64/ -target x86_64-linux-gnu -nodefaultlibs out/host/linux-x86/obj/SHARED_LIBRARIES/libconscrypt_openjdk_jni_intermediates/src/main/native/org_conscrypt_NativeCrypto.o out/host/linux-x86/obj/SHARED_LIBRARIES/libconscrypt_openjdk_jni_intermediates/src/openjdk/native/JNIHelp.o -Wl,--whole-archive out/host/linux-x86/obj/STATIC_LIBRARIES/libcrypto_static_intermediates/libcrypto_static.a out/host/linux-x86/obj/STATIC_LIBRARIES/libssl_static-host_intermediates/libssl_static-host.a -Wl,--no-whole-archive out/host/linux-x86/obj/STATIC_LIBRARIES/libc++_static_intermediates/libc++_static.a out/host/linux-x86/obj/STATIC_LIBRARIES/libcompiler_rt-extras_intermediates/libcompiler_rt-extras.a -o out/host/linux-x86/obj/lib/libconscrypt_openjdk_jni.so -lpthread -lpthread -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc"
Hypothesis: ld-linux and bundled ld mismatch
Using strace, it appears that it calls its own bundled copy of gcc's ld directly.
Could the lack of using ld-wrapper be causing this? Could patchelf? Could this be an insufficiently specified chroot environment in build-fhs-userenv/env.nix?
Result of ldd on the problem library:
android-env-chrootenv:root@spaceserv:/home/spacekitteh/code/copperhead/nougat-mr1-release# ldd out/host/linux-x86/lib64/libconscrypt_openjdk_jni.so
linux-vdso.so.1 (0x000003ff8adca000)
libpthread.so.0 => /usr/lib/libpthread.so.0 (0x0000038f24d91000)
libm.so.6 => /usr/lib/libm.so.6 (0x0000038f24a8c000)
libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x0000038f24876000)
libc.so.6 => /usr/lib/libc.so.6 (0x0000038f244d8000)
/nix/store/aym0jj00gal73w59gp2ifkwqlbf7s98h-glibc-multi-2.24/lib/ld-linux-x86-64.so.2 (0x000002b925384000)
Hypothesis: Java's JNI gets confused by the chroot
Or could it be to do with how Java JNI handles static vs shared libs?
For reference, here is part of the result of running ldd \which java``:
libcrypto.so.1.0.0 => /nix/store/f7vp024rwfnmzm17p3c6ji3zc79a5vyr-openssl-1.0.2j/lib/libcrypto.so.1.0.0 (0x000003be33b05000)
Related StackOverflow question
From the JNI spec
Once a native library is loaded, it is visible from all class loaders. Therefore two classes in different class loaders may link with the same native method. This leads to two problems:
- A class may mistakenly link with native libraries loaded by a class with the same name in a different class loader.
- Native methods can easily mix classes from different class loaders. This breaks the name space separation offered by class loaders, and leads to type safety problems.
background:
#21222 (comment)
Activity
spacekitteh commentedon Jan 3, 2017
Here's where the BoringSSL library is (or at least, should be) statically linked into the libconscrypt_openjdk_jni.so file, which should then be used instead of openjdk loading openssl from the host.
[-]openjdk seems to escape the buildFHSUserEnv chroot and load a package from outside chroot at the expense of a chroot-local package.[/-][+]buildFHSUserEnv chroot, openjdk, statically-linked libraries in .so's, and the procedure linkage table[/+][-]buildFHSUserEnv chroot, openjdk, statically-linked libraries in .so's, and the procedure linkage table[/-][+]buildFHSUserEnv chroot, openjdk, statically-linked libraries in .so's, clang, and the procedure linkage table[/+]spacekitteh commentedon Jan 4, 2017
Gonna tag peeps who know about the core infrastructure and buildFHSUserEnv.
@edolstra @fpletz @vcunat @joachifm @abbradar
abbradar commentedon Jan 4, 2017
Can this be reproduced without building all the AOSP? I've successfully built and ran unit tests for Conscrypt (standalone) in a chrootenv. I don't remember anything specific which we do that can cause this (in my knowledge symbols can't be overridden with anything short of
LD_PRELOAD-- and I haven't even imagined that statically linked PIC code can be affected with this!).spacekitteh commentedon Jan 4, 2017
abbradar commentedon Jan 4, 2017
Hm, that's better but I still don't have necessary space for downloading all the AOSP now (I can meddle with manifests but I don't have this much time).
Interesting fact: Arch Linux's Java doesn't reference
libcrypto:I think they link statically more things than we are.
abbradar commentedon Jan 4, 2017
Oh, I think that's because we link to GTK+ in non-headless version.
Can you try to use
jre8_headlessin the chrootenv?spacekitteh commentedon Jan 4, 2017
Oh hey! Using jre8_headless works! <3
Now to figure out the underlying problem.
abbradar commentedon Jan 4, 2017
spacekitteh commentedon Jan 4, 2017
Yeah, that's what I was gonna explore as my third hypothesis, as it's a combination of the first two!
What do you think causes this? As in, which upstream?
abbradar commentedon Jan 4, 2017
spacekitteh commentedon Jan 4, 2017
Weird to think we discovered a bug in such a commonly used component.
spacekitteh commentedon Jan 4, 2017
Seems like it could be a pretty easy thing to exploit!
abbradar commentedon Jan 4, 2017
abbradar commentedon Jan 4, 2017
spacekitteh commentedon Jan 4, 2017
Hmm, I guess you're right; you'd need LD_PRELOAD or something similar.
abbradar commentedon Jan 5, 2017
Closing this, see #21676.