-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add doPrivilege blocks for socket connect operations in plugins #22534
Changes from 7 commits
6870089
41178fb
060a4c0
6a89265
ec69de3
2fb3da9
704561d
71f2a32
5b166e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.cloud.aws.util; | ||
|
||
import org.elasticsearch.SpecialPermission; | ||
|
||
import java.io.IOException; | ||
import java.net.SocketPermission; | ||
import java.security.AccessController; | ||
import java.security.PrivilegedAction; | ||
import java.security.PrivilegedActionException; | ||
import java.security.PrivilegedExceptionAction; | ||
|
||
/** | ||
* This plugin uses aws libraries to connect to aws services. For these remote calls the plugin needs | ||
* {@link SocketPermission} 'connect' to establish connections. This class wraps the operations requiring access in | ||
* {@link AccessController#doPrivileged(PrivilegedAction)} blocks. | ||
*/ | ||
public final class SocketAccess { | ||
|
||
private SocketAccess() {} | ||
|
||
public static <T> T doPrivileged(PrivilegedAction<T> operation) { | ||
checkSpecialPermission(); | ||
return AccessController.doPrivileged(operation); | ||
} | ||
|
||
public static <T> T doPrivilegedIOException(PrivilegedExceptionAction<T> operation) throws IOException { | ||
checkSpecialPermission(); | ||
try { | ||
return AccessController.doPrivileged(operation); | ||
} catch (PrivilegedActionException e) { | ||
throw (IOException) e.getCause(); | ||
} | ||
} | ||
|
||
private static void checkSpecialPermission() { | ||
SecurityManager sm = System.getSecurityManager(); | ||
if (sm != null) { | ||
sm.checkPermission(new SpecialPermission()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ | |
import org.elasticsearch.cloud.aws.AwsEc2Service; | ||
import org.elasticsearch.cloud.aws.AwsEc2ServiceImpl; | ||
import org.elasticsearch.cloud.aws.network.Ec2NameResolver; | ||
import org.elasticsearch.cloud.aws.util.SocketAccess; | ||
import org.elasticsearch.cluster.service.ClusterService; | ||
import org.elasticsearch.common.io.stream.NamedWriteableRegistry; | ||
import org.elasticsearch.common.logging.DeprecationLogger; | ||
|
@@ -56,7 +57,6 @@ | |
import org.elasticsearch.discovery.ec2.AwsEc2UnicastHostsProvider; | ||
import org.elasticsearch.discovery.zen.UnicastHostsProvider; | ||
import org.elasticsearch.discovery.zen.ZenDiscovery; | ||
import org.elasticsearch.discovery.zen.ZenPing; | ||
import org.elasticsearch.node.Node; | ||
import org.elasticsearch.plugins.DiscoveryPlugin; | ||
import org.elasticsearch.plugins.Plugin; | ||
|
@@ -75,20 +75,19 @@ public class Ec2DiscoveryPlugin extends Plugin implements DiscoveryPlugin, Close | |
if (sm != null) { | ||
sm.checkPermission(new SpecialPermission()); | ||
} | ||
AccessController.doPrivileged(new PrivilegedAction<Void>() { | ||
@Override | ||
public Void run() { | ||
try { | ||
// kick jackson to do some static caching of declared members info | ||
Jackson.jsonNodeOf("{}"); | ||
// ClientConfiguration clinit has some classloader problems | ||
// TODO: fix that | ||
Class.forName("com.amazonaws.ClientConfiguration"); | ||
} catch (ClassNotFoundException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return null; | ||
// Initializing Jackson requires RuntimePermission accessDeclaredMembers | ||
// The ClientConfiguration class requires RuntimePermission getClassLoader | ||
AccessController.doPrivileged((PrivilegedAction<Void>) () -> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you leave a comment why this is needed in this particular place? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes - I can add a comment. |
||
try { | ||
// kick jackson to do some static caching of declared members info | ||
Jackson.jsonNodeOf("{}"); | ||
// ClientConfiguration clinit has some classloader problems | ||
// TODO: fix that | ||
Class.forName("com.amazonaws.ClientConfiguration"); | ||
} catch (ClassNotFoundException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return null; | ||
}); | ||
} | ||
|
||
|
@@ -194,14 +193,14 @@ static Settings getAvailabilityZoneNodeAttributes(Settings settings, String azMe | |
try { | ||
url = new URL(azMetadataUrl); | ||
logger.debug("obtaining ec2 [placement/availability-zone] from ec2 meta-data url {}", url); | ||
urlConnection = url.openConnection(); | ||
urlConnection = SocketAccess.doPrivilegedIOException(url::openConnection); | ||
urlConnection.setConnectTimeout(2000); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we can leverage forbidden APIs to restrict the use of openConnection and add a dedicated method that does it with suppressforbidden on it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shall we spin off anotehr issue for this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought that would be good. On #22116 I left a comment a few days ago that maybe we should add another task on that issue to mark relevant APIs as forbidden? |
||
} catch (IOException e) { | ||
// should not happen, we know the url is not malformed, and openConnection does not actually hit network | ||
throw new UncheckedIOException(e); | ||
} | ||
|
||
try (InputStream in = urlConnection.getInputStream(); | ||
try (InputStream in = SocketAccess.doPrivilegedIOException(urlConnection::getInputStream); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here... |
||
BufferedReader urlReader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) { | ||
|
||
String metadataResult = urlReader.readLine(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can use a singleton instance?