Skip to content

Commit

Permalink
8344077: Remove security manager dependency in java.io
Browse files Browse the repository at this point in the history
Reviewed-by: rriggs, alanb, naoto, lancea
  • Loading branch information
Brian Burkhalter committed Nov 19, 2024
1 parent f6f73ce commit 81e4311
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 273 deletions.
29 changes: 10 additions & 19 deletions src/java.base/share/classes/java/io/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

package java.io;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.*;
import java.nio.charset.Charset;
import jdk.internal.access.JavaIOAccess;
Expand Down Expand Up @@ -659,9 +657,8 @@ public Console console() {
});
}

@SuppressWarnings("removal")
private static Console instantiateConsole() {
Console c;
Console c = null;

try {
/*
Expand All @@ -673,25 +670,19 @@ private static Console instantiateConsole() {
* If no providers are available, or instantiation failed, java.base built-in
* Console implementation is used.
*/
c = AccessController.doPrivileged(new PrivilegedAction<Console>() {
public Console run() {
var consModName = System.getProperty("jdk.console",
JdkConsoleProvider.DEFAULT_PROVIDER_MODULE_NAME);
var consModName = System.getProperty("jdk.console",
JdkConsoleProvider.DEFAULT_PROVIDER_MODULE_NAME);

for (var jcp : ServiceLoader.load(ModuleLayer.boot(), JdkConsoleProvider.class)) {
if (consModName.equals(jcp.getClass().getModule().getName())) {
var jc = jcp.console(istty, CHARSET);
if (jc != null) {
return new ProxyingConsole(jc);
}
break;
}
for (var jcp : ServiceLoader.load(ModuleLayer.boot(), JdkConsoleProvider.class)) {
if (consModName.equals(jcp.getClass().getModule().getName())) {
var jc = jcp.console(istty, CHARSET);
if (jc != null) {
c = new ProxyingConsole(jc);
}
return null;
break;
}
});
}
} catch (ServiceConfigurationError _) {
c = null;
}

// If not found, default to built-in Console
Expand Down
138 changes: 2 additions & 136 deletions src/java.base/share/classes/java/io/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -751,11 +751,6 @@ public URI toURI() {
* application; {@code false} otherwise
*/
public boolean canRead() {
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
if (isInvalid()) {
return false;
}
Expand All @@ -775,11 +770,6 @@ public boolean canRead() {
* {@code false} otherwise.
*/
public boolean canWrite() {
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(path);
}
if (isInvalid()) {
return false;
}
Expand All @@ -794,11 +784,6 @@ public boolean canWrite() {
* by this abstract pathname exists; {@code false} otherwise
*/
public boolean exists() {
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
if (isInvalid()) {
return false;
}
Expand All @@ -820,11 +805,6 @@ public boolean exists() {
* {@code false} otherwise
*/
public boolean isDirectory() {
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
if (isInvalid()) {
return false;
}
Expand All @@ -848,11 +828,6 @@ public boolean isDirectory() {
* {@code false} otherwise
*/
public boolean isFile() {
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
if (isInvalid()) {
return false;
}
Expand Down Expand Up @@ -881,11 +856,6 @@ public boolean isFile() {
* @since 1.2
*/
public boolean isHidden() {
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
if (isInvalid()) {
return false;
}
Expand Down Expand Up @@ -920,11 +890,6 @@ public boolean isHidden() {
* epoch
*/
public long lastModified() {
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
if (isInvalid()) {
return 0L;
}
Expand All @@ -947,11 +912,6 @@ public long lastModified() {
* denoting system-dependent entities such as devices or pipes.
*/
public long length() {
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
if (isInvalid()) {
return 0L;
}
Expand Down Expand Up @@ -983,9 +943,6 @@ public long length() {
* @since 1.2
*/
public boolean createNewFile() throws IOException {
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) security.checkWrite(path);
if (isInvalid()) {
throw new IOException("Invalid file path");
}
Expand All @@ -1007,11 +964,6 @@ public boolean createNewFile() throws IOException {
* successfully deleted; {@code false} otherwise
*/
public boolean delete() {
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkDelete(path);
}
if (isInvalid()) {
return false;
}
Expand Down Expand Up @@ -1043,11 +995,6 @@ public boolean delete() {
* @since 1.2
*/
public void deleteOnExit() {
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkDelete(path);
}
if (isInvalid()) {
return;
}
Expand Down Expand Up @@ -1097,11 +1044,6 @@ public String[] list() {
* I/O error occurs.
*/
private final String[] normalizedList() {
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
if (isInvalid()) {
return null;
}
Expand Down Expand Up @@ -1275,11 +1217,6 @@ public File[] listFiles(FileFilter filter) {
* created; {@code false} otherwise
*/
public boolean mkdir() {
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(path);
}
if (isInvalid()) {
return false;
}
Expand Down Expand Up @@ -1345,12 +1282,6 @@ public boolean renameTo(File dest) {
if (dest == null) {
throw new NullPointerException();
}
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(path);
security.checkWrite(dest.path);
}
if (this.isInvalid() || dest.isInvalid()) {
return false;
}
Expand Down Expand Up @@ -1380,11 +1311,6 @@ public boolean renameTo(File dest) {
*/
public boolean setLastModified(long time) {
if (time < 0) throw new IllegalArgumentException("Negative time");
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(path);
}
if (isInvalid()) {
return false;
}
Expand All @@ -1406,11 +1332,6 @@ public boolean setLastModified(long time) {
* @since 1.2
*/
public boolean setReadOnly() {
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(path);
}
if (isInvalid()) {
return false;
}
Expand Down Expand Up @@ -1445,11 +1366,6 @@ public boolean setReadOnly() {
* @since 1.6
*/
public boolean setWritable(boolean writable, boolean ownerOnly) {
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(path);
}
if (isInvalid()) {
return false;
}
Expand Down Expand Up @@ -1517,11 +1433,6 @@ public boolean setWritable(boolean writable) {
* @since 1.6
*/
public boolean setReadable(boolean readable, boolean ownerOnly) {
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(path);
}
if (isInvalid()) {
return false;
}
Expand Down Expand Up @@ -1595,11 +1506,6 @@ public boolean setReadable(boolean readable) {
* @since 1.6
*/
public boolean setExecutable(boolean executable, boolean ownerOnly) {
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(path);
}
if (isInvalid()) {
return false;
}
Expand Down Expand Up @@ -1652,11 +1558,6 @@ public boolean setExecutable(boolean executable) {
* @since 1.6
*/
public boolean canExecute() {
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkExec(path);
}
if (isInvalid()) {
return false;
}
Expand Down Expand Up @@ -1726,12 +1627,6 @@ public static File[] listRoots() {
* @see FileStore#getTotalSpace
*/
public long getTotalSpace() {
@SuppressWarnings("removal")
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
sm.checkRead(path);
}
if (isInvalid()) {
return 0L;
}
Expand Down Expand Up @@ -1764,12 +1659,6 @@ public long getTotalSpace() {
* @see FileStore#getUnallocatedSpace
*/
public long getFreeSpace() {
@SuppressWarnings("removal")
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
sm.checkRead(path);
}
if (isInvalid()) {
return 0L;
}
Expand Down Expand Up @@ -1805,12 +1694,6 @@ public long getFreeSpace() {
* @see FileStore#getUsableSpace
*/
public long getUsableSpace() {
@SuppressWarnings("removal")
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
sm.checkRead(path);
}
if (isInvalid()) {
return 0L;
}
Expand Down Expand Up @@ -1840,7 +1723,6 @@ private static int shortenSubName(int subNameLength, int excess,
}
return subNameLength;
}
@SuppressWarnings("removal")
static File generateFile(String prefix, String suffix, File dir)
throws IOException
{
Expand Down Expand Up @@ -1897,11 +1779,8 @@ static File generateFile(String prefix, String suffix, File dir)

File f = new File(dir, name);
if (!name.equals(f.getName()) || f.isInvalid()) {
if (System.getSecurityManager() != null)
throw new IOException("Unable to create temporary file");
else
throw new IOException("Unable to create temporary file, "
+ name);
throw new IOException("Unable to create temporary file, "
+ name);
}
return f;
}
Expand Down Expand Up @@ -1998,22 +1877,9 @@ public static File createTempFile(String prefix, String suffix,
File tmpdir = (directory != null) ? directory
: TempDirectory.location();

@SuppressWarnings("removal")
SecurityManager sm = System.getSecurityManager();
File f;
do {
f = TempDirectory.generateFile(prefix, suffix, tmpdir);

if (sm != null) {
try {
sm.checkWrite(f.getPath());
} catch (SecurityException se) {
// don't reveal temporary directory location
if (directory == null)
throw new SecurityException("Unable to create temporary file");
throw se;
}
}
} while (FS.hasBooleanAttributes(f, FileSystem.BA_EXISTS));

if (!FS.createFileExclusively(f.getPath()))
Expand Down
Loading

0 comments on commit 81e4311

Please sign in to comment.