Skip to content

Commit bcba526

Browse files
committed
chore: use SecurityManager only for JDKs up to 24
1 parent 9a7070b commit bcba526

File tree

2 files changed

+58
-19
lines changed

2 files changed

+58
-19
lines changed

modules/kernel/src/org/apache/axis2/java/security/AccessController.java

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,13 @@
2828

2929
/**
3030
* This utility wrapper class is created to support AXIS2 runs
31-
* inside of Java security environments. Due to the access control
32-
* checking algorithm, for Java security to function properly,
31+
* inside of Java 2 Security environment. Due to the access control
32+
* checking algorithm, for Java 2 Security to function properly,
3333
* <code>doPrivileged()</code>
3434
* is required in cases where there is application code on the stack frame
35-
* accessing system resources (ie, read/write files, opening ports, and etc).
36-
* <p/>
37-
* This class provides a consistent security model across Java versions by
38-
* always using doPrivileged(), ensuring proper privilege elevation regardless
39-
* of SecurityManager presence (which was deprecated in Java 17 and removed in Java 21).
35+
* accessing the system resources (ie, read/write files, opening ports, and etc).
36+
* This class also improve performance no matther Security Manager is being enabled
37+
* or not.
4038
* <p/>
4139
* Note: This utility should be used properly, otherwise might introduce
4240
* security holes.
@@ -59,11 +57,11 @@
5957

6058

6159
public class AccessController {
60+
private static final boolean SUPPORTS_SECURITY_MANAGER = Runtime.version().feature() < 24;
6261

6362
/**
6463
* Performs the specified <code>PrivilegedAction</code> with privileges
65-
* enabled. This method always uses doPrivileged for security consistency
66-
* across Java versions.
64+
* enabled if a security manager is present.
6765
* <p/>
6866
* If the action's <code>run</code> method throws an (unchecked) exception,
6967
* it will propagate through this method.
@@ -74,7 +72,11 @@ public class AccessController {
7472
* @see #doPrivileged(PrivilegedExceptionAction)
7573
*/
7674
public static <T> T doPrivileged(PrivilegedAction<T> action) {
77-
return java.security.AccessController.doPrivileged(action);
75+
if (!SUPPORTS_SECURITY_MANAGER) {
76+
return (action.run());
77+
} else {
78+
return java.security.AccessController.doPrivileged(action);
79+
}
7880
}
7981

8082

@@ -83,7 +85,9 @@ public static <T> T doPrivileged(PrivilegedAction<T> action) {
8385
* enabled and restricted by the specified <code>AccessControlContext</code>.
8486
* The action is performed with the intersection of the permissions
8587
* possessed by the caller's protection domain, and those possessed
86-
* by the domains represented by the specified <code>AccessControlContext</code>.
88+
* by the domains represented by the specified
89+
* <code>AccessControlContext</code> if a security manager is present.
90+
* <p/>
8791
* <p/>
8892
* If the action's <code>run</code> method throws an (unchecked) exception,
8993
* it will propagate through this method.
@@ -97,35 +101,49 @@ public static <T> T doPrivileged(PrivilegedAction<T> action) {
97101
* @see #doPrivileged(PrivilegedExceptionAction,AccessControlContext)
98102
*/
99103
public static <T> T doPrivileged(PrivilegedAction<T> action, AccessControlContext context) {
100-
return java.security.AccessController.doPrivileged(action, context);
104+
if (!SUPPORTS_SECURITY_MANAGER) {
105+
return action.run();
106+
} else {
107+
return java.security.AccessController.doPrivileged(action, context);
108+
}
101109
}
102110

103111
/**
104112
* Performs the specified <code>PrivilegedExceptionAction</code> with
105-
* privileges enabled. The action is performed with <i>all</i> of the
113+
* privileges enabled. The action is performed with <i>all</i> of the
106114
* permissions possessed by the caller's protection domain.
107115
* <p/>
108116
* If the action's <code>run</code> method throws an <i>unchecked</i>
109117
* exception, it will propagate through this method.
110118
*
111119
* @param action the action to be performed.
112120
* @return the value returned by the action's <code>run</code> method.
113-
* @throws PrivilegedActionException the specified action's
121+
* @throws PrivilgedActionException the specified action's
114122
* <code>run</code> method threw a <i>checked</i> exception.
115123
* @see #doPrivileged(PrivilegedExceptionAction,AccessControlContext)
116124
* @see #doPrivileged(PrivilegedAction)
117125
*/
118126
public static <T> T doPrivileged(PrivilegedExceptionAction<T> action)
119127
throws PrivilegedActionException {
120-
return java.security.AccessController.doPrivileged(action);
128+
if (!SUPPORTS_SECURITY_MANAGER) {
129+
try {
130+
return action.run();
131+
} catch (java.lang.RuntimeException e) {
132+
throw e;
133+
} catch (Exception e) {
134+
throw new PrivilegedActionException(e);
135+
}
136+
} else {
137+
return java.security.AccessController.doPrivileged(action);
138+
}
121139
}
122140

123141

124142
/**
125143
* Performs the specified <code>PrivilegedExceptionAction</code> with
126144
* privileges enabled and restricted by the specified
127-
* <code>AccessControlContext</code>. The action is performed with the
128-
* intersection of the permissions possessed by the caller's
145+
* <code>AccessControlContext</code>. The action is performed with the
146+
* intersection of the the permissions possessed by the caller's
129147
* protection domain, and those possessed by the domains represented by the
130148
* specified <code>AccessControlContext</code>.
131149
* <p/>
@@ -146,7 +164,18 @@ public static <T> T doPrivileged(PrivilegedExceptionAction<T> action)
146164
public static <T> T doPrivileged(PrivilegedExceptionAction<T> action,
147165
AccessControlContext context)
148166
throws PrivilegedActionException {
149-
return java.security.AccessController.doPrivileged(action, context);
167+
168+
if (!SUPPORTS_SECURITY_MANAGER) {
169+
try {
170+
return action.run();
171+
} catch (java.lang.RuntimeException e) {
172+
throw e;
173+
} catch (Exception e) {
174+
throw new PrivilegedActionException(e);
175+
}
176+
} else {
177+
return java.security.AccessController.doPrivileged(action, context);
178+
}
150179
}
151180

152181
/**
@@ -174,7 +203,9 @@ public static AccessControlContext getContext() {
174203
* is not permitted, based on the current security policy.
175204
*/
176205
public static void checkPermission(Permission perm) throws AccessControlException {
177-
java.security.AccessController.checkPermission(perm);
206+
if (SUPPORTS_SECURITY_MANAGER) {
207+
java.security.AccessController.checkPermission(perm);
208+
}
178209
}
179210

180211
/**

modules/kernel/test/org/apache/axis2/java/security/driver/Java2SecTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import java.util.Calendar;
3939
import java.util.TimeZone;
4040

41+
import static org.junit.Assume.assumeTrue;
42+
4143
/**
4244
* Java2SecTest demonstrates the usages of AccessController class for privileged operations.
4345
*
@@ -69,6 +71,12 @@ public Java2SecTest() {
6971
System.out.println("Current time => " + sdf.format(cal.getTime()) + "\n");
7072
}
7173

74+
@Override
75+
public void setUp() throws Exception {
76+
// Security Manager was removed after that
77+
assumeTrue(Runtime.version().feature() < 24);
78+
}
79+
7280
// Constructor
7381
public Java2SecTest(String arg) {
7482
super(arg);

0 commit comments

Comments
 (0)