8227587: Add internal privileged System.loadLibrary

Reviewed-by: rriggs, mchung, chegar
This commit is contained in:
Claes Redestad 2019-07-17 12:35:46 +02:00
parent ec7f1c13d9
commit 78a1c8ea0a
26 changed files with 97 additions and 218 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013 SAP SE. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
@ -26,9 +26,6 @@
package sun.nio.fs;
import java.security.AccessController;
import java.security.PrivilegedAction;
/**
* AIX specific system calls.
*/
@ -46,11 +43,7 @@ class AixNativeDispatcher extends UnixNativeDispatcher {
private static native void init();
static {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
System.loadLibrary("nio");
return null;
}});
jdk.internal.loader.BootLoader.loadLibrary("nio");
init();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,9 +25,6 @@
package sun.nio.fs;
import java.security.AccessController;
import java.security.PrivilegedAction;
/**
* Linux specific system calls.
*/
@ -121,11 +118,7 @@ class LinuxNativeDispatcher extends UnixNativeDispatcher {
private static native void init();
static {
AccessController.doPrivileged(new PrivilegedAction<>() {
public Void run() {
System.loadLibrary("nio");
return null;
}});
jdk.internal.loader.BootLoader.loadLibrary("nio");
init();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -26,8 +26,6 @@
package sun.nio.fs;
import java.nio.file.*;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.*;
import java.io.IOException;
import jdk.internal.misc.Unsafe;
@ -459,10 +457,6 @@ class LinuxWatchService
private static native int poll(int fd1, int fd2) throws UnixException;
static {
AccessController.doPrivileged(new PrivilegedAction<>() {
public Void run() {
System.loadLibrary("nio");
return null;
}});
jdk.internal.loader.BootLoader.loadLibrary("nio");
}
}

View File

@ -105,13 +105,7 @@ public final class KeychainStore extends KeyStoreSpi {
private static final Debug debug = Debug.getInstance("keystore");
static {
AccessController.doPrivileged(
new PrivilegedAction<Void>() {
public Void run() {
System.loadLibrary("osxsecurity");
return null;
}
});
jdk.internal.loader.BootLoader.loadLibrary("osxsecurity");
try {
PKCS8ShroudedKeyBag_OID = new ObjectIdentifier(keyBag);
pbeWithSHAAnd3KeyTripleDESCBC_OID = new ObjectIdentifier(pbeWithSHAAnd3KeyTripleDESCBC);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -27,8 +27,6 @@ package sun.nio.fs;
import java.io.IOException;
import java.nio.file.Path;
import java.security.AccessController;
import java.security.PrivilegedAction;
/**
* File type detector that uses a file extension to look up its MIME type
@ -55,12 +53,6 @@ class UTIFileTypeDetector extends AbstractFileTypeDetector {
}
static {
AccessController.doPrivileged(new PrivilegedAction<>() {
@Override
public Void run() {
System.loadLibrary("nio");
return null;
}
});
jdk.internal.loader.BootLoader.loadLibrary("nio");
}
}

View File

@ -2274,6 +2274,11 @@ public final class System {
public void setCause(Throwable t, Throwable cause) {
t.setCause(cause);
}
public void loadLibrary(Class<?> caller, String library) {
assert library.indexOf(java.io.File.separatorChar) < 0;
ClassLoader.loadLibrary(caller, library, false);
}
});
}
}

View File

@ -67,13 +67,7 @@ abstract class AbstractPlainDatagramSocketImpl extends DatagramSocketImpl
* Load net library into runtime.
*/
static {
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<>() {
public Void run() {
System.loadLibrary("net");
return null;
}
});
jdk.internal.loader.BootLoader.loadLibrary("net");
}
private static volatile boolean checkedReusePort;

View File

@ -94,13 +94,7 @@ abstract class AbstractPlainSocketImpl extends SocketImpl implements PlatformSoc
* Load net library into runtime.
*/
static {
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<>() {
public Void run() {
System.loadLibrary("net");
return null;
}
});
jdk.internal.loader.BootLoader.loadLibrary("net");
}
private static volatile boolean checkedReusePort;

View File

@ -46,13 +46,7 @@ class DatagramPacket {
* Perform class initialization
*/
static {
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<>() {
public Void run() {
System.loadLibrary("net");
return null;
}
});
jdk.internal.loader.BootLoader.loadLibrary("net");
init();
}

View File

@ -317,13 +317,7 @@ class InetAddress implements java.io.Serializable {
} else {
preferIPv6Address = PREFER_IPV4_VALUE;
}
AccessController.doPrivileged(
new java.security.PrivilegedAction<>() {
public Void run() {
System.loadLibrary("net");
return null;
}
});
jdk.internal.loader.BootLoader.loadLibrary("net");
SharedSecrets.setJavaNetInetAddressAccess(
new JavaNetInetAddressAccess() {
public String getOriginalHostName(InetAddress ia) {

View File

@ -28,7 +28,6 @@ package java.net;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.NoSuchElementException;
import java.security.AccessController;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Stream;
@ -57,13 +56,7 @@ public final class NetworkInterface {
private static final int defaultIndex; /* index of defaultInterface */
static {
AccessController.doPrivileged(
new java.security.PrivilegedAction<>() {
public Void run() {
System.loadLibrary("net");
return null;
}
});
jdk.internal.loader.BootLoader.loadLibrary("net");
init();
defaultInterface = DefaultInterface.getDefault();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -27,8 +27,6 @@ package java.util.zip;
import java.nio.ByteBuffer;
import java.nio.file.attribute.FileTime;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.time.DateTimeException;
import java.time.Instant;
import java.time.LocalDateTime;
@ -278,13 +276,7 @@ class ZipUtils {
* Loads zip native library, if not already laoded
*/
static void loadLibrary() {
SecurityManager sm = System.getSecurityManager();
if (sm == null) {
System.loadLibrary("zip");
} else {
PrivilegedAction<Void> pa = () -> { System.loadLibrary("zip"); return null; };
AccessController.doPrivileged(pa);
}
jdk.internal.loader.BootLoader.loadLibrary("zip");
}
private static final Unsafe unsafe = Unsafe.getUnsafe();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -311,4 +311,12 @@ public interface JavaLangAccess {
* @param cause set t's cause to new value
*/
void setCause(Throwable t, Throwable cause);
/**
* Privileged System.loadLibrary
*
* @param caller on behalf of which the library is being loaded
* @param library name of the library to load
*/
void loadLibrary(Class<?> caller, String library);
}

View File

@ -128,6 +128,23 @@ public class BootLoader {
}
}
/**
* Loads a library from the system path.
*/
public static void loadLibrary(String library) {
if (System.getSecurityManager() == null) {
SharedSecrets.getJavaLangAccess().loadLibrary(BootLoader.class, library);
} else {
AccessController.doPrivileged(
new java.security.PrivilegedAction<>() {
public Void run() {
SharedSecrets.getJavaLangAccess().loadLibrary(BootLoader.class, library);
return null;
}
});
}
}
/**
* Returns a URL to a resource in a module defined to the boot loader.
*/

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -27,7 +27,6 @@ package sun.net.sdp;
import java.io.IOException;
import java.io.FileDescriptor;
import java.security.AccessController;
import jdk.internal.access.SharedSecrets;
import jdk.internal.access.JavaIOFileDescriptorAccess;
@ -75,12 +74,6 @@ public final class SdpSupport {
private static native void convert0(int fd) throws IOException;
static {
AccessController.doPrivileged(
new java.security.PrivilegedAction<Void>() {
public Void run() {
System.loadLibrary("net");
return null;
}
});
jdk.internal.loader.BootLoader.loadLibrary("net");
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -98,13 +98,7 @@ public class DefaultProxySelector extends ProxySelector {
return NetProperties.getBoolean(key);
}});
if (b != null && b.booleanValue()) {
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<Void>() {
public Void run() {
System.loadLibrary("net");
return null;
}
});
jdk.internal.loader.BootLoader.loadLibrary("net");
hasSystemProxies = init();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -432,15 +432,8 @@ public class IOUtil {
public static void load() { }
static {
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<Void>() {
public Void run() {
System.loadLibrary("net");
System.loadLibrary("nio");
return null;
}
});
jdk.internal.loader.BootLoader.loadLibrary("net");
jdk.internal.loader.BootLoader.loadLibrary("nio");
initIDs();
IOV_MAX = iovMax();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,9 +25,6 @@
package sun.nio.fs;
import java.security.AccessController;
import java.security.PrivilegedAction;
/**
* Solaris specific system calls.
*/
@ -52,11 +49,7 @@ class SolarisNativeDispatcher extends UnixNativeDispatcher {
private static native void init();
static {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
System.loadLibrary("nio");
return null;
}});
jdk.internal.loader.BootLoader.loadLibrary("nio");
init();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -26,8 +26,6 @@
package sun.nio.fs;
import java.nio.file.*;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.*;
import java.io.IOException;
import jdk.internal.misc.Unsafe;
@ -817,11 +815,7 @@ class SolarisWatchService
throws UnixException;
static {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
System.loadLibrary("nio");
return null;
}});
jdk.internal.loader.BootLoader.loadLibrary("nio");
init();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,7 +25,7 @@
package sun.net;
import java.security.AccessController;
import sun.security.action.GetPropertyAction;
/**
* Determines the ephemeral port range in use on this system.
@ -41,35 +41,29 @@ public final class PortConfig {
private PortConfig() {}
static {
AccessController.doPrivileged(
new java.security.PrivilegedAction<>() {
public Void run() {
System.loadLibrary("net");
String os = System.getProperty("os.name");
if (os.startsWith("Linux")) {
defaultLower = 32768;
defaultUpper = 61000;
} else if (os.startsWith("SunOS")) {
defaultLower = 32768;
defaultUpper = 65535;
} else if (os.contains("OS X")) {
defaultLower = 49152;
defaultUpper = 65535;
} else if (os.startsWith("AIX")) {
// The ephemeral port is OS version dependent on AIX:
// http://publib.boulder.ibm.com/infocenter/aix/v7r1/topic/com.ibm.aix.rsct315.admin/bl503_ephport.htm
// However, on AIX 5.3 / 6.1 / 7.1 we always see the
// settings below by using:
// /usr/sbin/no -a | fgrep ephemeral
defaultLower = 32768;
defaultUpper = 65535;
} else {
throw new InternalError(
"sun.net.PortConfig: unknown OS");
}
return null;
}
});
jdk.internal.loader.BootLoader.loadLibrary("net");
String os = GetPropertyAction.privilegedGetProperty("os.name");
if (os.startsWith("Linux")) {
defaultLower = 32768;
defaultUpper = 61000;
} else if (os.startsWith("SunOS")) {
defaultLower = 32768;
defaultUpper = 65535;
} else if (os.contains("OS X")) {
defaultLower = 49152;
defaultUpper = 65535;
} else if (os.startsWith("AIX")) {
// The ephemeral port is OS version dependent on AIX:
// http://publib.boulder.ibm.com/infocenter/aix/v7r1/topic/com.ibm.aix.rsct315.admin/bl503_ephport.htm
// However, on AIX 5.3 / 6.1 / 7.1 we always see the
// settings below by using:
// /usr/sbin/no -a | fgrep ephemeral
defaultLower = 32768;
defaultUpper = 65535;
} else {
throw new InternalError(
"sun.net.PortConfig: unknown OS");
}
int v = getLower0();
if (v == -1) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -259,13 +259,7 @@ public class ResolverConfigurationImpl
static native String fallbackDomain0();
static {
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<>() {
public Void run() {
System.loadLibrary("net");
return null;
}
});
jdk.internal.loader.BootLoader.loadLibrary("net");
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -33,8 +33,6 @@ import java.nio.file.FileAlreadyExistsException;
import java.nio.file.LinkOption;
import java.nio.file.LinkPermission;
import java.nio.file.StandardCopyOption;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
@ -628,12 +626,7 @@ class UnixCopyFile {
throws UnixException;
static {
AccessController.doPrivileged(new PrivilegedAction<>() {
@Override
public Void run() {
System.loadLibrary("nio");
return null;
}});
jdk.internal.loader.BootLoader.loadLibrary("nio");
}
}

View File

@ -25,9 +25,6 @@
package sun.nio.fs;
import java.security.AccessController;
import java.security.PrivilegedAction;
/**
* Unix system and library calls.
*/
@ -630,11 +627,7 @@ class UnixNativeDispatcher {
private static native int init();
static {
AccessController.doPrivileged(new PrivilegedAction<>() {
public Void run() {
System.loadLibrary("nio");
return null;
}});
jdk.internal.loader.BootLoader.loadLibrary("nio");
capabilities = init();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -160,13 +160,7 @@ public class ResolverConfigurationImpl
static native int notifyAddrChange0();
static {
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<Void>() {
public Void run() {
System.loadLibrary("net");
return null;
}
});
jdk.internal.loader.BootLoader.loadLibrary("net");
init0();
// start the address listener thread

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -27,8 +27,6 @@ package sun.nio.fs;
import java.nio.file.*;
import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedAction;
/**
* File type detector that does lookup of file extension using Windows Registry.
@ -70,13 +68,8 @@ public class RegistryFileTypeDetector
private static native String queryStringValue(long subKey, long name);
static {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
@Override
public Void run() {
// nio.dll has dependency on net.dll
System.loadLibrary("net");
System.loadLibrary("nio");
return null;
}});
// nio.dll has dependency on net.dll
jdk.internal.loader.BootLoader.loadLibrary("net");
jdk.internal.loader.BootLoader.loadLibrary("nio");
}
}

View File

@ -25,8 +25,6 @@
package sun.nio.fs;
import java.security.AccessController;
import java.security.PrivilegedAction;
import jdk.internal.misc.Unsafe;
import static sun.nio.fs.WindowsConstants.*;
@ -1153,13 +1151,9 @@ class WindowsNativeDispatcher {
private static native void initIDs();
static {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
// nio.dll has dependency on net.dll
System.loadLibrary("net");
System.loadLibrary("nio");
return null;
}});
// nio.dll has dependency on net.dll
jdk.internal.loader.BootLoader.loadLibrary("net");
jdk.internal.loader.BootLoader.loadLibrary("nio");
initIDs();
}