Skip to content

Commit 944df7a

Browse files
mulugetamluben
authored andcommitted
Wrap System.load and System.loadLibrary calls with doPrivileged.
Signed-off-by: Mulugeta Mammo <[email protected]>
1 parent 692e6ad commit 944df7a

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

src/main/java/com/github/luben/zstd/util/Native.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.io.IOException;
66
import java.io.InputStream;
77
import java.lang.UnsatisfiedLinkError;
8+
import java.security.AccessController;
9+
import java.security.PrivilegedAction;
810

911
public enum Native {
1012
;
@@ -61,6 +63,24 @@ public static synchronized boolean isLoaded() {
6163
return loaded;
6264
}
6365

66+
private static void loadLibrary(final String libName) {
67+
AccessController.doPrivileged(new PrivilegedAction<Void>() {
68+
public Void run() {
69+
System.loadLibrary(libName);
70+
return null;
71+
}
72+
});
73+
}
74+
75+
private static void loadLibraryFile(final String libFileName) {
76+
AccessController.doPrivileged(new PrivilegedAction<Void>() {
77+
public Void run() {
78+
System.load(libFileName);
79+
return null;
80+
}
81+
});
82+
}
83+
6484
public static synchronized void load() {
6585
load(null);
6686
}
@@ -74,15 +94,15 @@ public static synchronized void load(final File tempFolder) {
7494
String overridePath = System.getProperty(nativePathOverride);
7595
if (overridePath != null) {
7696
// Do not fall back to auto-discovery - consumers know better
77-
System.load(overridePath);
97+
loadLibraryFile(overridePath);
7898
loaded = true;
7999
return;
80100
}
81101

82102
// try to load the shared library directly from the JAR
83103
try {
84104
Class.forName("org.osgi.framework.BundleEvent"); // Simple OSGI env. check
85-
System.loadLibrary(libname);
105+
loadLibrary(libname);
86106
loaded = true;
87107
return;
88108
} catch (Throwable e) {
@@ -94,7 +114,7 @@ public static synchronized void load(final File tempFolder) {
94114
// fallback to loading the zstd-jni from the system library path.
95115
// It also covers loading on Android.
96116
try {
97-
System.loadLibrary(libnameShort);
117+
loadLibrary(libnameShort);
98118
loaded = true;
99119
return;
100120
} catch (UnsatisfiedLinkError e) {
@@ -127,11 +147,11 @@ public static synchronized void load(final File tempFolder) {
127147
// ignore
128148
}
129149
try {
130-
System.load(tempLib.getAbsolutePath());
150+
loadLibraryFile(tempLib.getAbsolutePath());
131151
} catch (UnsatisfiedLinkError e) {
132152
// fall-back to loading the zstd-jni from the system library path
133153
try {
134-
System.loadLibrary(libnameShort);
154+
loadLibrary(libnameShort);
135155
} catch (UnsatisfiedLinkError e1) {
136156
// display error in case problem with loading from temp folder
137157
// and from system library path - concatenate both messages

0 commit comments

Comments
 (0)