Search code examples
javalinuxjna

Is there any available method for me to call a dynamic library that requires root permission through JNA?


As described in the title, the functions in the dynamic library need root permission to perform some system calls. If I use JNA, I don't know how to achieve this requirement. Or is it a bad requirement? Maybe I didn't provide more detailed code, but I'm sure it's a valuable question for me...


Solution

  • Is there any available method for me to call a dynamic library that requires root permission through JNA?

    No there isn't. In UNIX / Linux, the only point when application permissions can be elevated is when using exec to execute a new process AND the executable has the "set uid" bit set in its file permissions.

    This presents particular problems for Java. It is totally unsafe to make the java executable a "set uid root" program. The standard java executable is designed run any Java class supplied via the command arguments. You can't restrict it to only running certain classes that are known to be safe to run as root.

    In short, if your Java application needs root access to do something (in Java code or in a native code library), then it needs to have been started by the root user.

    Or is it a bad requirement?

    It is an unimplementable requirement.