Skip to content

Commit 0c191f6

Browse files
author
Eirik Bjørsnøs
committed
8344185: Remove calls to SecurityManager in sun.net.ftp
Reviewed-by: alanb, michaelm, dfuchs
1 parent bfee766 commit 0c191f6

File tree

3 files changed

+22
-75
lines changed

3 files changed

+22
-75
lines changed

src/java.base/share/classes/sun/net/ftp/FtpClient.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2009, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -234,8 +234,6 @@ public static FtpClient create(String dest) throws FtpProtocolException, IOExcep
234234
* @param dest the address of the destination server
235235
* @return this FtpClient
236236
* @throws IOException if connection failed.
237-
* @throws SecurityException if there is a SecurityManager installed and it
238-
* denied the authorization to connect to the destination.
239237
* @throws FtpProtocolException
240238
*/
241239
public abstract FtpClient connect(SocketAddress dest) throws FtpProtocolException, IOException;
@@ -247,8 +245,6 @@ public static FtpClient create(String dest) throws FtpProtocolException, IOExcep
247245
* @param timeout the value, in milliseconds, to use as a connection timeout
248246
* @return this FtpClient
249247
* @throws IOException if connection failed.
250-
* @throws SecurityException if there is a SecurityManager installed and it
251-
* denied the authorization to connect to the destination.
252248
* @throws FtpProtocolException
253249
*/
254250
public abstract FtpClient connect(SocketAddress dest, int timeout) throws FtpProtocolException, IOException;

src/java.base/share/classes/sun/net/ftp/FtpClientProvider.java

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2009, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,8 +24,6 @@
2424
*/
2525
package sun.net.ftp;
2626

27-
import java.security.AccessController;
28-
import java.security.PrivilegedAction;
2927
import java.util.ServiceConfigurationError;
3028
//import java.util.ServiceLoader;
3129

@@ -50,16 +48,8 @@ public abstract class FtpClientProvider {
5048

5149
/**
5250
* Initializes a new instance of this class.
53-
*
54-
* @throws SecurityException if a security manager is installed and it denies
55-
* {@link RuntimePermission}{@code ("ftpClientProvider")}
5651
*/
5752
protected FtpClientProvider() {
58-
@SuppressWarnings("removal")
59-
SecurityManager sm = System.getSecurityManager();
60-
if (sm != null) {
61-
sm.checkPermission(new RuntimePermission("ftpClientProvider"));
62-
}
6353
}
6454

6555
private static boolean loadProviderFromProperty() {
@@ -74,8 +64,7 @@ private static boolean loadProviderFromProperty() {
7464
return true;
7565
} catch (ClassNotFoundException |
7666
IllegalAccessException |
77-
InstantiationException |
78-
SecurityException x) {
67+
InstantiationException x) {
7968
throw new ServiceConfigurationError(x.toString());
8069
}
8170
}
@@ -135,26 +124,19 @@ private static boolean loadProviderAsService() {
135124
*
136125
* @return The system-wide default FtpClientProvider
137126
*/
138-
@SuppressWarnings("removal")
139127
public static FtpClientProvider provider() {
140128
synchronized (lock) {
141129
if (provider != null) {
142130
return provider;
143131
}
144-
return (FtpClientProvider) AccessController.doPrivileged(
145-
new PrivilegedAction<Object>() {
146-
147-
public Object run() {
148-
if (loadProviderFromProperty()) {
149-
return provider;
150-
}
151-
if (loadProviderAsService()) {
152-
return provider;
153-
}
154-
provider = new sun.net.ftp.impl.DefaultFtpClientProvider();
155-
return provider;
156-
}
157-
});
132+
if (loadProviderFromProperty()) {
133+
return provider;
134+
}
135+
if (loadProviderAsService()) {
136+
return provider;
137+
}
138+
provider = new sun.net.ftp.impl.DefaultFtpClientProvider();
139+
return provider;
158140
}
159141
}
160142
}

src/java.base/share/classes/sun/net/ftp/impl/FtpClient.java

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@
4444
import java.net.ServerSocket;
4545
import java.net.Socket;
4646
import java.net.SocketAddress;
47-
import java.security.AccessController;
48-
import java.security.PrivilegedAction;
49-
import java.security.PrivilegedExceptionAction;
5047
import java.text.DateFormat;
5148
import java.time.ZoneOffset;
5249
import java.time.ZonedDateTime;
@@ -133,31 +130,17 @@ public class FtpClient extends sun.net.ftp.FtpClient {
133130
private DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, java.util.Locale.US);
134131
private static final boolean acceptPasvAddressVal;
135132
static {
136-
final int vals[] = {0, 0};
137-
final String acceptPasvAddress[] = {null};
138-
@SuppressWarnings("removal")
139-
final String enc = AccessController.doPrivileged(
140-
new PrivilegedAction<String>() {
141-
public String run() {
142-
acceptPasvAddress[0] = System.getProperty("jdk.net.ftp.trustPasvAddress", "false");
143-
vals[0] = Integer.getInteger("sun.net.client.defaultReadTimeout", 300_000).intValue();
144-
vals[1] = Integer.getInteger("sun.net.client.defaultConnectTimeout", 300_000).intValue();
145-
return System.getProperty("file.encoding", "ISO8859_1");
146-
}
147-
});
148-
if (vals[0] == 0) {
133+
defaultSoTimeout = Integer.getInteger("sun.net.client.defaultReadTimeout", 300_000).intValue();
134+
if (defaultSoTimeout == 0) {
149135
defaultSoTimeout = -1;
150-
} else {
151-
defaultSoTimeout = vals[0];
152136
}
153137

154-
if (vals[1] == 0) {
138+
defaultConnectTimeout = Integer.getInteger("sun.net.client.defaultConnectTimeout", 300_000).intValue();
139+
if (defaultConnectTimeout == 0) {
155140
defaultConnectTimeout = -1;
156-
} else {
157-
defaultConnectTimeout = vals[1];
158141
}
159142

160-
encoding = enc;
143+
encoding = System.getProperty("file.encoding", "ISO8859_1");
161144
try {
162145
if (!isASCIISuperset(encoding)) {
163146
encoding = "ISO8859_1";
@@ -171,7 +154,7 @@ public String run() {
171154
patterns[i] = Pattern.compile(patStrings[i]);
172155
}
173156

174-
acceptPasvAddressVal = Boolean.parseBoolean(acceptPasvAddress[0]);
157+
acceptPasvAddressVal = Boolean.getBoolean("jdk.net.ftp.trustPasvAddress");
175158
}
176159

177160
/**
@@ -662,20 +645,15 @@ private Socket openPassiveDataConnection(String cmd) throws sun.net.ftp.FtpProto
662645
Socket s;
663646
if (proxy != null) {
664647
if (proxy.type() == Proxy.Type.SOCKS) {
665-
PrivilegedAction<Socket> pa = () -> new Socket(proxy);
666-
@SuppressWarnings("removal")
667-
var tmp = AccessController.doPrivileged(pa);
668-
s = tmp;
648+
s = new Socket(proxy);
669649
} else {
670650
s = new Socket(Proxy.NO_PROXY);
671651
}
672652
} else {
673653
s = new Socket();
674654
}
675655

676-
PrivilegedAction<InetAddress> pa = () -> server.getLocalAddress();
677-
@SuppressWarnings("removal")
678-
InetAddress serverAddress = AccessController.doPrivileged(pa);
656+
InetAddress serverAddress = server.getLocalAddress();
679657

680658
// Bind the socket to the same address as the control channel. This
681659
// is needed in case of multi-homed systems.
@@ -761,11 +739,8 @@ private InetSocketAddress validatePasvAddress(int port, String s, InetAddress ad
761739
}
762740

763741
private static InetAddress privilegedLocalHost() throws FtpProtocolException {
764-
PrivilegedExceptionAction<InetAddress> action = InetAddress::getLocalHost;
765742
try {
766-
@SuppressWarnings("removal")
767-
var tmp = AccessController.doPrivileged(action);
768-
return tmp;
743+
return InetAddress.getLocalHost();
769744
} catch (Exception e) {
770745
var ftpEx = new FtpProtocolException(ERROR_MSG);
771746
ftpEx.initCause(e);
@@ -774,11 +749,8 @@ private static InetAddress privilegedLocalHost() throws FtpProtocolException {
774749
}
775750

776751
private static InetAddress[] privilegedGetAllByName(String hostName) throws FtpProtocolException {
777-
PrivilegedExceptionAction<InetAddress[]> pAction = () -> InetAddress.getAllByName(hostName);
778752
try {
779-
@SuppressWarnings("removal")
780-
var tmp =AccessController.doPrivileged(pAction);
781-
return tmp;
753+
return InetAddress.getAllByName(hostName);
782754
} catch (Exception e) {
783755
var ftpEx = new FtpProtocolException(ERROR_MSG);
784756
ftpEx.initCause(e);
@@ -1021,10 +993,7 @@ private Socket doConnect(InetSocketAddress dest, int timeout) throws IOException
1021993
Socket s;
1022994
if (proxy != null) {
1023995
if (proxy.type() == Proxy.Type.SOCKS) {
1024-
PrivilegedAction<Socket> pa = () -> new Socket(proxy);
1025-
@SuppressWarnings("removal")
1026-
var tmp = AccessController.doPrivileged(pa);
1027-
s = tmp;
996+
s = new Socket(proxy);
1028997
} else {
1029998
s = new Socket(Proxy.NO_PROXY);
1030999
}

0 commit comments

Comments
 (0)