From de6717ca3a8cf11873aeb84b78505f4c388709af Mon Sep 17 00:00:00 2001 From: bchristi Date: Tue, 19 Nov 2024 10:48:53 -0800 Subject: [PATCH] Remove SecMgr, etc usage from java.prefs --- .../java/util/prefs/MacOSXPreferences.java | 2 - .../util/prefs/MacOSXPreferencesFile.java | 10 +- .../java/util/prefs/AbstractPreferences.java | 9 +- .../classes/java/util/prefs/Preferences.java | 47 +--- .../util/prefs/FileSystemPreferences.java | 261 +++++++----------- .../java/util/prefs/WindowsPreferences.java | 12 +- 6 files changed, 114 insertions(+), 227 deletions(-) diff --git a/src/java.prefs/macosx/classes/java/util/prefs/MacOSXPreferences.java b/src/java.prefs/macosx/classes/java/util/prefs/MacOSXPreferences.java index 6f395514352d5..b4c97c47e1925 100644 --- a/src/java.prefs/macosx/classes/java/util/prefs/MacOSXPreferences.java +++ b/src/java.prefs/macosx/classes/java/util/prefs/MacOSXPreferences.java @@ -28,8 +28,6 @@ import java.util.Objects; class MacOSXPreferences extends AbstractPreferences { - // fixme need security checks? - // CF preferences file name for Java nodes with short names // This value is also in MacOSXPreferencesFile.c private static final String defaultAppName = "com.apple.java.util.prefs"; diff --git a/src/java.prefs/macosx/classes/java/util/prefs/MacOSXPreferencesFile.java b/src/java.prefs/macosx/classes/java/util/prefs/MacOSXPreferencesFile.java index c222bc3d81f02..7e2bee595b08e 100644 --- a/src/java.prefs/macosx/classes/java/util/prefs/MacOSXPreferencesFile.java +++ b/src/java.prefs/macosx/classes/java/util/prefs/MacOSXPreferencesFile.java @@ -82,15 +82,9 @@ class MacOSXPreferencesFile { loadPrefsLib(); } - @SuppressWarnings({"removal", "restricted"}) + @SuppressWarnings("restricted") private static void loadPrefsLib() { - java.security.AccessController.doPrivileged( - new java.security.PrivilegedAction() { - public Void run() { - System.loadLibrary("prefs"); - return null; - } - }); + System.loadLibrary("prefs"); } private static class FlushTask extends TimerTask { diff --git a/src/java.prefs/share/classes/java/util/prefs/AbstractPreferences.java b/src/java.prefs/share/classes/java/util/prefs/AbstractPreferences.java index 94963658f1d24..4674c34241a10 100644 --- a/src/java.prefs/share/classes/java/util/prefs/AbstractPreferences.java +++ b/src/java.prefs/share/classes/java/util/prefs/AbstractPreferences.java @@ -27,8 +27,6 @@ import java.util.*; import java.io.*; -import java.security.AccessController; -import java.security.PrivilegedAction; /** * This class provides a skeletal implementation of the {@link Preferences} @@ -1060,12 +1058,7 @@ public String absolutePath() { */ @SuppressWarnings("removal") public boolean isUserNode() { - return AccessController.doPrivileged( - new PrivilegedAction() { - public Boolean run() { - return root == Preferences.userRoot(); - } - }).booleanValue(); + return root == Preferences.userRoot(); } public void addPreferenceChangeListener(PreferenceChangeListener pcl) { diff --git a/src/java.prefs/share/classes/java/util/prefs/Preferences.java b/src/java.prefs/share/classes/java/util/prefs/Preferences.java index d428261aac91c..dc85278f0d7a1 100644 --- a/src/java.prefs/share/classes/java/util/prefs/Preferences.java +++ b/src/java.prefs/share/classes/java/util/prefs/Preferences.java @@ -30,20 +30,10 @@ import java.io.InputStream; import java.io.IOException; import java.io.OutputStream; -import java.security.AccessController; -import java.security.Permission; -import java.security.PrivilegedAction; import java.util.Iterator; import java.util.ServiceLoader; import java.util.ServiceConfigurationError; -// These imports needed only as a workaround for a JavaDoc bug -import java.lang.RuntimePermission; -import java.lang.Integer; -import java.lang.Long; -import java.lang.Float; -import java.lang.Double; - /** * A node in a hierarchical collection of preference data. This class * allows applications to store and retrieve user and system @@ -230,16 +220,8 @@ public abstract class Preferences { @SuppressWarnings("removal") private static PreferencesFactory factory() { // 1. Try user-specified system property - String factoryName = AccessController.doPrivileged( - new PrivilegedAction() { - public String run() { - return System.getProperty( - "java.util.prefs.PreferencesFactory");}}); + String factoryName = System.getProperty("java.util.prefs.PreferencesFactory"); if (factoryName != null) { - // FIXME: This code should be run in a doPrivileged and - // not use the context classloader, to avoid being - // dependent on the invoking thread. - // Checking AllPermission also seems wrong. try { @SuppressWarnings("deprecation") Object result =Class.forName(factoryName, false, @@ -250,10 +232,6 @@ public String run() { try { // workaround for javaws, plugin, // load factory class using non-system classloader - SecurityManager sm = System.getSecurityManager(); - if (sm != null) { - sm.checkPermission(new java.security.AllPermission()); - } @SuppressWarnings("deprecation") Object result = Class.forName(factoryName, false, Thread.currentThread() @@ -267,11 +245,7 @@ public String run() { } } } - - return AccessController.doPrivileged( - new PrivilegedAction() { - public PreferencesFactory run() { - return factory1();}}); + return factory1(); } private static PreferencesFactory factory1() { @@ -427,24 +401,12 @@ private static String nodeName(Class c) { return "/" + packageName.replace('.', '/'); } - /** - * This permission object represents the permission required to get - * access to the user or system root (which in turn allows for all - * other operations). - */ - private static Permission prefsPerm = new RuntimePermission("preferences"); - /** * Returns the root preference node for the calling user. * * @return the root preference node for the calling user. */ public static Preferences userRoot() { - @SuppressWarnings("removal") - SecurityManager security = System.getSecurityManager(); - if (security != null) - security.checkPermission(prefsPerm); - return factory.userRoot(); } @@ -454,11 +416,6 @@ public static Preferences userRoot() { * @return the root preference node for the system. */ public static Preferences systemRoot() { - @SuppressWarnings("removal") - SecurityManager security = System.getSecurityManager(); - if (security != null) - security.checkPermission(prefsPerm); - return factory.systemRoot(); } diff --git a/src/java.prefs/unix/classes/java/util/prefs/FileSystemPreferences.java b/src/java.prefs/unix/classes/java/util/prefs/FileSystemPreferences.java index 756eedade72a3..75edd4ae19c38 100644 --- a/src/java.prefs/unix/classes/java/util/prefs/FileSystemPreferences.java +++ b/src/java.prefs/unix/classes/java/util/prefs/FileSystemPreferences.java @@ -27,9 +27,9 @@ import java.util.*; import java.io.*; -import java.security.AccessController; -import java.security.PrivilegedAction; -import java.security.PrivilegedExceptionAction; +//import java.security.AccessController; +//import java.security.PrivilegedAction; +//import java.security.PrivilegedExceptionAction; import java.security.PrivilegedActionException; import sun.util.logging.PlatformLogger; @@ -53,13 +53,13 @@ class FileSystemPreferences extends AbstractPreferences { loadPrefsLib(); } - @SuppressWarnings({"removal", "restricted"}) + @SuppressWarnings("restricted") private static void loadPrefsLib() { - PrivilegedAction load = () -> { +// PrivilegedAction load = () -> { System.loadLibrary("prefs"); - return null; - }; - AccessController.doPrivileged(load); +// return null; +// }; +// AccessController.doPrivileged(load); } /** @@ -67,8 +67,7 @@ private static void loadPrefsLib() { */ @SuppressWarnings("removal") private static final int SYNC_INTERVAL = Math.max(1, - AccessController.doPrivileged((PrivilegedAction) () -> - Integer.getInteger("java.util.prefs.syncInterval", 30))); + Integer.getInteger("java.util.prefs.syncInterval", 30)); /** * Returns logger for error messages. Backing store exceptions are logged at @@ -117,10 +116,10 @@ static Preferences getUserRoot() { return root; } - @SuppressWarnings("removal") +// @SuppressWarnings("removal") private static void setupUserRoot() { - AccessController.doPrivileged(new PrivilegedAction() { - public Void run() { +// AccessController.doPrivileged(new PrivilegedAction() { +// public Void run() { userRootDir = new File(System.getProperty("java.util.prefs.userRoot", System.getProperty("user.home")), ".java/.userPrefs"); @@ -160,9 +159,9 @@ public Void run() { getLogger().warning(e.toString()); } userRootModTime = userRootModFile.lastModified(); - return null; - } - }); +// return null; +// } +// }); } @@ -185,10 +184,10 @@ static Preferences getSystemRoot() { return root; } - @SuppressWarnings("removal") +// @SuppressWarnings("removal") private static void setupSystemRoot() { - AccessController.doPrivileged(new PrivilegedAction() { - public Void run() { +// AccessController.doPrivileged(new PrivilegedAction() { +// public Void run() { String systemPrefsDirName = System.getProperty("java.util.prefs.systemRoot","/etc/.java"); systemRootDir = @@ -234,9 +233,9 @@ public Void run() { } catch (IOException e) { getLogger().warning(e.toString()); } systemRootModTime = systemRootModFile.lastModified(); - return null; - } - }); +// return null; +// } +// }); } @@ -456,7 +455,7 @@ private void replayChanges() { addShutdownHook(); } - @SuppressWarnings("removal") +// @SuppressWarnings("removal") private static void addShutdownHook() { // Add periodic timer task to periodically sync cached prefs syncTimer.schedule(new TimerTask() { @@ -466,8 +465,8 @@ public void run() { }, SYNC_INTERVAL*1000, SYNC_INTERVAL*1000); // Add shutdown hook to flush cached prefs on normal termination - AccessController.doPrivileged(new PrivilegedAction() { - public Void run() { +// AccessController.doPrivileged(new PrivilegedAction() { +// public Void run() { Runtime.getRuntime().addShutdownHook( new Thread(null, null, "Sync Timer Thread", 0, false) { public void run() { @@ -475,9 +474,9 @@ public void run() { syncWorld(); } }); - return null; - } - }); +// return null; +// } +// }); } private static void syncWorld() { @@ -526,19 +525,19 @@ private FileSystemPreferences(boolean user) { * parent node and name. This constructor, called from childSpi, * is used to make every node except for the two //roots. */ - @SuppressWarnings("removal") +// @SuppressWarnings("removal") private FileSystemPreferences(FileSystemPreferences parent, String name) { super(parent, name); isUserNode = parent.isUserNode; dir = new File(parent.dir, dirName(name)); prefsFile = new File(dir, "prefs.xml"); tmpFile = new File(dir, "prefs.tmp"); - AccessController.doPrivileged(new PrivilegedAction() { - public Void run() { +// AccessController.doPrivileged(new PrivilegedAction() { +// public Void run() { newNode = !dir.exists(); - return null; - } - }); +// return null; +// } +// }); if (newNode) { // These 2 things guarantee node will get written at next flush/sync prefsCache = new TreeMap<>(); @@ -596,12 +595,12 @@ private void initCacheIfNecessary() { * fails, a BackingStoreException is thrown and both prefsCache and * lastSyncTime are unaffected by the call. */ - @SuppressWarnings("removal") +// @SuppressWarnings("removal") private void loadCache() throws BackingStoreException { - try { - AccessController.doPrivileged( - new PrivilegedExceptionAction() { - public Void run() throws BackingStoreException { +// try { +// AccessController.doPrivileged( +// new PrivilegedExceptionAction() { +// public Void run() throws BackingStoreException { Map m = new TreeMap<>(); long newLastSyncTime = 0; try { @@ -627,12 +626,12 @@ public Void run() throws BackingStoreException { // Attempt succeeded; update state prefsCache = m; lastSyncTime = newLastSyncTime; - return null; - } - }); - } catch (PrivilegedActionException e) { - throw (BackingStoreException) e.getException(); - } +// return null; +// } +// }); +// } catch (PrivilegedActionException e) { // get rid of? +// throw (BackingStoreException) e.getException(); +// } } /** @@ -644,32 +643,21 @@ public Void run() throws BackingStoreException { * and lastSyncTime will be unaffected by this call. This call will * NEVER leave prefsFile in a corrupt state. */ - @SuppressWarnings("removal") private void writeBackCache() throws BackingStoreException { try { - AccessController.doPrivileged( - new PrivilegedExceptionAction() { - public Void run() throws BackingStoreException { - try { - if (!dir.exists() && !dir.mkdirs()) - throw new BackingStoreException(dir + - " create failed."); - try (FileOutputStream fos = new FileOutputStream(tmpFile)) { - XmlSupport.exportMap(fos, prefsCache); - } - if (!tmpFile.renameTo(prefsFile)) - throw new BackingStoreException("Can't rename " + - tmpFile + " to " + prefsFile); - } catch(Exception e) { - if (e instanceof BackingStoreException) - throw (BackingStoreException)e; - throw new BackingStoreException(e); - } - return null; - } - }); - } catch (PrivilegedActionException e) { - throw (BackingStoreException) e.getException(); + if (!dir.exists() && !dir.mkdirs()) + throw new BackingStoreException(dir + + " create failed."); + try (FileOutputStream fos = new FileOutputStream(tmpFile)) { + XmlSupport.exportMap(fos, prefsCache); + } + if (!tmpFile.renameTo(prefsFile)) + throw new BackingStoreException("Can't rename " + + tmpFile + " to " + prefsFile); + } catch(BackingStoreException e) { + throw e; + } catch(Exception e) { + throw new BackingStoreException(e); } } @@ -678,21 +666,15 @@ protected String[] keysSpi() { return prefsCache.keySet().toArray(new String[prefsCache.size()]); } - @SuppressWarnings("removal") protected String[] childrenNamesSpi() { - return AccessController.doPrivileged( - new PrivilegedAction() { - public String[] run() { - List result = new ArrayList<>(); - File[] dirContents = dir.listFiles(); - if (dirContents != null) { - for (int i = 0; i < dirContents.length; i++) - if (dirContents[i].isDirectory()) - result.add(nodeName(dirContents[i].getName())); - } - return result.toArray(EMPTY_STRING_ARRAY); - } - }); + List result = new ArrayList<>(); + File[] dirContents = dir.listFiles(); + if (dirContents != null) { + for (int i = 0; i < dirContents.length; i++) + if (dirContents[i].isDirectory()) + result.add(nodeName(dirContents[i].getName())); + } + return result.toArray(EMPTY_STRING_ARRAY); } private static final String[] EMPTY_STRING_ARRAY = new String[0]; @@ -717,42 +699,30 @@ public void removeNode() throws BackingStoreException { /** * Called with file lock held (in addition to node locks). */ - @SuppressWarnings("removal") protected void removeNodeSpi() throws BackingStoreException { - try { - AccessController.doPrivileged( - new PrivilegedExceptionAction() { - public Void run() throws BackingStoreException { - if (changeLog.contains(nodeCreate)) { - changeLog.remove(nodeCreate); - nodeCreate = null; - return null; - } - if (!dir.exists()) - return null; - prefsFile.delete(); - tmpFile.delete(); - // dir should be empty now. If it's not, empty it - File[] junk = dir.listFiles(); - if (junk.length != 0) { - getLogger().warning( - "Found extraneous files when removing node: " - + Arrays.asList(junk)); - for (int i=0; i() { - public Long run() { - long nmt; - if (isUserNode()) { - nmt = userRootModFile.lastModified(); - isUserRootModified = userRootModTime == nmt; - } else { - nmt = systemRootModFile.lastModified(); - isSystemRootModified = systemRootModTime == nmt; - } - return nmt; - } - }); + if (!lockFile(shared)) { + throw (new BackingStoreException("Couldn't get file lock.")); + } + long nmt; + if (isUserNode()) { + nmt = userRootModFile.lastModified(); + isUserRootModified = userRootModTime == nmt; + } else { + nmt = systemRootModFile.lastModified(); + isSystemRootModified = systemRootModTime == nmt; + } + final long newModTime = nmt; try { super.sync(); - AccessController.doPrivileged(new PrivilegedAction() { - public Void run() { - if (isUserNode()) { - userRootModTime = newModTime.longValue() + 1000; - userRootModFile.setLastModified(userRootModTime); - } else { - systemRootModTime = newModTime.longValue() + 1000; - systemRootModFile.setLastModified(systemRootModTime); - } - return null; - } - }); + if (isUserNode()) { + userRootModTime = newModTime + 1000; + userRootModFile.setLastModified(userRootModTime); + } else { + systemRootModTime = newModTime + 1000; + systemRootModFile.setLastModified(systemRootModTime); + } } finally { unlockFile(); } } } - @SuppressWarnings("removal") protected void syncSpi() throws BackingStoreException { - try { - AccessController.doPrivileged( - new PrivilegedExceptionAction() { - public Void run() throws BackingStoreException { - syncSpiPrivileged(); - return null; - } - }); - } catch (PrivilegedActionException e) { - throw (BackingStoreException) e.getException(); - } + syncSpiPrivileged(); } private void syncSpiPrivileged() throws BackingStoreException { if (isRemoved()) diff --git a/src/java.prefs/windows/classes/java/util/prefs/WindowsPreferences.java b/src/java.prefs/windows/classes/java/util/prefs/WindowsPreferences.java index 885535755a2aa..15fb6f582f852 100644 --- a/src/java.prefs/windows/classes/java/util/prefs/WindowsPreferences.java +++ b/src/java.prefs/windows/classes/java/util/prefs/WindowsPreferences.java @@ -27,8 +27,8 @@ import java.util.StringTokenizer; import java.io.ByteArrayOutputStream; -import java.security.AccessController; -import java.security.PrivilegedAction; +//import java.security.AccessController; +//import java.security.PrivilegedAction; import sun.util.logging.PlatformLogger; @@ -50,13 +50,9 @@ class WindowsPreferences extends AbstractPreferences { loadPrefsLib(); } - @SuppressWarnings({"removal", "restricted"}) + @SuppressWarnings("restricted") private static void loadPrefsLib() { - PrivilegedAction load = () -> { - System.loadLibrary("prefs"); - return null; - }; - AccessController.doPrivileged(load); + System.loadLibrary("prefs"); } /**