Take the 2-minute tour ×
Information Security Stack Exchange is a question and answer site for Information security professionals. It's 100% free, no registration required.

Jumping from http://stackoverflow.com/questions/7016391/difference-between-system-load-and-system-loadlibrary-in-java

What are the security implications of both approach ?

  • System.loadLibrary("_name_");
    • Automatically searches for a library named "lib_name_.so" in $LD_LIBRARY_PATH
  • System.load("/PATH/TO/_name_.so");
    • do load "/PATH/TO/_name_.so"
share|improve this question
    
You've mentioned loadLibrary two times. Shouldn't the second one be System.load? –  Rob W May 10 '13 at 20:07
    
Correct ! I'm editing right now –  Cerber May 11 '13 at 14:30

2 Answers 2

up vote 4 down vote accepted

System.loadLibrary() refuses to load "libraries" with the path separator appearing in the name; see this excerpt from Java's source code (in java.lang.Runtime.java):

synchronized void loadLibrary0(Class fromClass, String libname) {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkLink(libname);
    }
    if (libname.indexOf((int)File.separatorChar) != -1) {
        throw new UnsatisfiedLinkError(
"Directory separator should not appear in library name: " + libname);
    }
    ClassLoader.loadLibrary(fromClass, libname, false);
}

As such, you could imagine allowing System.loadLibrary() but rejecting System.load(), giving access to only a specific set of loadable "safe" libraries... however, this seems indirect and fragile. The "normal" way of restricting native code loading is through SystemManager.checkLink(), which is called from both System.load() and System.loadLibrary(). In that case, there is no real difference in security between the two methods.

share|improve this answer

Relying on the calling system to help locate things can certainly lead down odd paths. The biggest problem is that the system may provide a file that wasn't intended to be loaded by the application. If an attacker can place a file in a location that falls under the search, then the attacker can inject arbitrary code into the application and execute it. E.g. an arbitrary code execution vulnerability (http://en.wikipedia.org/wiki/Arbitrary_code_execution).

If you specify the exact path, you have the ability to protect the loaded file through things like file permissions, etc.

share|improve this answer
    
extra question : what if the library path has to be passed by command line ? I have to replace a failing autodiscovery mecanism and, following the KISS principle, would tend to use System.load(..) with the full path provided by the command line at runtime –  Cerber May 10 '13 at 18:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.