< prev index next >

test/lib/jdk/test/lib/Platform.java

Print this page
rev 51613 : 8210039: move OSInfo to top level testlibrary
Reviewed-by: duke
rev 51614 : imported patch 8210039-1

@@ -29,24 +29,28 @@
 import java.util.Arrays;
 import java.util.List;
 import java.util.Objects;
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
 
 public class Platform {
-    public  static final String vmName      = System.getProperty("java.vm.name");
-    public  static final String vmInfo      = System.getProperty("java.vm.info");
-    private static final String osVersion   = System.getProperty("os.version");
+    public  static final String vmName      = AccessController.doPrivileged((PrivilegedAction<String>) () -> System.getProperty("java.vm.name"));
+    public  static final String vmInfo      = AccessController.doPrivileged((PrivilegedAction<String>) () -> System.getProperty("java.vm.info"));
+    private static final String osVersion   = AccessController.doPrivileged((PrivilegedAction<String>) () -> System.getProperty("os.version"));
     private static       int osVersionMajor = -1;
     private static       int osVersionMinor = -1;
-    private static final String osName      = System.getProperty("os.name");
-    private static final String dataModel   = System.getProperty("sun.arch.data.model");
-    private static final String vmVersion   = System.getProperty("java.vm.version");
-    private static final String jdkDebug    = System.getProperty("jdk.debug");
-    private static final String osArch      = System.getProperty("os.arch");
-    private static final String userName    = System.getProperty("user.name");
-    private static final String compiler    = System.getProperty("sun.management.compiler");
+    private static final String osName      = AccessController.doPrivileged((PrivilegedAction<String>) () -> System.getProperty("os.name"));
+    private static final String dataModel   = AccessController.doPrivileged((PrivilegedAction<String>) () -> System.getProperty("sun.arch.data.model"));
+    private static final String vmVersion   = AccessController.doPrivileged((PrivilegedAction<String>) () -> System.getProperty("java.vm.version"));
+    private static final String jdkDebug    = AccessController.doPrivileged((PrivilegedAction<String>) () -> System.getProperty("jdk.debug"));
+    private static final String osArch      = AccessController.doPrivileged((PrivilegedAction<String>) () -> System.getProperty("os.arch"));
+    private static final String userName    = AccessController.doPrivileged((PrivilegedAction<String>) () -> System.getProperty("user.name"));
+    private static final String compiler    = AccessController.doPrivileged((PrivilegedAction<String>) () -> System.getProperty("sun.management.compiler"));
 
     public static boolean isClient() {
         return vmName.endsWith(" Client VM");
     }
 

@@ -252,33 +256,43 @@
      */
     private static boolean canPtraceAttachLinux() throws IOException {
         // SELinux deny_ptrace:
         File deny_ptrace = new File("/sys/fs/selinux/booleans/deny_ptrace");
         if (deny_ptrace.exists()) {
-            try (RandomAccessFile file = new RandomAccessFile(deny_ptrace, "r")) {
+            try (RandomAccessFile file = AccessController.doPrivileged(
+                    (PrivilegedExceptionAction<RandomAccessFile>) () -> new RandomAccessFile(deny_ptrace, "r"))) {
                 if (file.readByte() != '0') {
                     return false;
                 }
+            } catch (PrivilegedActionException e) {
+                @SuppressWarnings("unchecked")
+                IOException t = (IOException) e.getException();
+                throw t;
             }
         }
 
         // YAMA enhanced security ptrace_scope:
         // 0 - a process can PTRACE_ATTACH to any other process running under the same uid
         // 1 - restricted ptrace: a process must be a children of the inferior or user is root
         // 2 - only processes with CAP_SYS_PTRACE may use ptrace or user is root
         // 3 - no attach: no processes may use ptrace with PTRACE_ATTACH
         File ptrace_scope = new File("/proc/sys/kernel/yama/ptrace_scope");
         if (ptrace_scope.exists()) {
-            try (RandomAccessFile file = new RandomAccessFile(ptrace_scope, "r")) {
+            try (RandomAccessFile file = AccessController.doPrivileged(
+                    (PrivilegedExceptionAction<RandomAccessFile>) () -> new RandomAccessFile(ptrace_scope, "r"))) {
                 byte yama_scope = file.readByte();
                 if (yama_scope == '3') {
                     return false;
                 }
 
                 if (!userName.equals("root") && yama_scope != '0') {
                     return false;
                 }
+            } catch (PrivilegedActionException e) {
+                @SuppressWarnings("unchecked")
+                IOException t = (IOException) e.getException();
+                throw t;
             }
         }
         // Otherwise expect to be permitted:
         return true;
     }
< prev index next >