-
Notifications
You must be signed in to change notification settings - Fork 25k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrap netty accept/connect ops with doPrivileged (#22572)
This is related to #22116. netty channels require socket `connect` and `accept` privileges. Netty does not currently wrap these operations with `doPrivileged` blocks. These changes extend the netty channels and wrap calls to the relevant super methods in doPrivileged blocks.
- Loading branch information
1 parent
cd236c4
commit f4270f9
Showing
7 changed files
with
218 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...ain/java/org/elasticsearch/transport/netty4/channel/PrivilegedNioServerSocketChannel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 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.transport.netty4.channel; | ||
|
||
import io.netty.channel.socket.nio.NioServerSocketChannel; | ||
import org.elasticsearch.SpecialPermission; | ||
|
||
import java.security.AccessController; | ||
import java.security.PrivilegedAction; | ||
import java.security.PrivilegedActionException; | ||
import java.security.PrivilegedExceptionAction; | ||
import java.util.List; | ||
|
||
/** | ||
* Wraps netty calls to {@link java.nio.channels.ServerSocketChannel#accept()} in | ||
* {@link AccessController#doPrivileged(PrivilegedAction)} blocks. This is necessary to limit | ||
* {@link java.net.SocketPermission} to the transport module. | ||
*/ | ||
public class PrivilegedNioServerSocketChannel extends NioServerSocketChannel { | ||
|
||
@Override | ||
protected int doReadMessages(List<Object> buf) throws Exception { | ||
SecurityManager sm = System.getSecurityManager(); | ||
if (sm != null) { | ||
sm.checkPermission(new SpecialPermission()); | ||
} | ||
try { | ||
return AccessController.doPrivileged((PrivilegedExceptionAction<Integer>) () -> super.doReadMessages(buf)); | ||
} catch (PrivilegedActionException e) { | ||
throw (Exception) e.getCause(); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
.../src/main/java/org/elasticsearch/transport/netty4/channel/PrivilegedNioSocketChannel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 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.transport.netty4.channel; | ||
|
||
import io.netty.channel.socket.nio.NioSocketChannel; | ||
import org.elasticsearch.SpecialPermission; | ||
|
||
import java.net.SocketAddress; | ||
import java.security.AccessController; | ||
import java.security.PrivilegedAction; | ||
import java.security.PrivilegedActionException; | ||
import java.security.PrivilegedExceptionAction; | ||
|
||
/** | ||
* Wraps netty calls to {@link java.nio.channels.SocketChannel#connect(SocketAddress)} in | ||
* {@link AccessController#doPrivileged(PrivilegedAction)} blocks. This is necessary to limit | ||
* {@link java.net.SocketPermission} to the transport module. | ||
*/ | ||
public class PrivilegedNioSocketChannel extends NioSocketChannel { | ||
|
||
@Override | ||
protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception { | ||
SecurityManager sm = System.getSecurityManager(); | ||
if (sm != null) { | ||
sm.checkPermission(new SpecialPermission()); | ||
} | ||
try { | ||
return AccessController.doPrivileged((PrivilegedExceptionAction<Boolean>) () -> super.doConnect(remoteAddress, localAddress)); | ||
} catch (PrivilegedActionException e) { | ||
throw (Exception) e.getCause(); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...ain/java/org/elasticsearch/transport/netty4/channel/PrivilegedOioServerSocketChannel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 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.transport.netty4.channel; | ||
|
||
import io.netty.channel.socket.oio.OioServerSocketChannel; | ||
import org.elasticsearch.SpecialPermission; | ||
|
||
import java.net.ServerSocket; | ||
import java.security.AccessController; | ||
import java.security.PrivilegedAction; | ||
import java.security.PrivilegedActionException; | ||
import java.security.PrivilegedExceptionAction; | ||
import java.util.List; | ||
|
||
/** | ||
* Wraps netty calls to {@link ServerSocket#accept()} in {@link AccessController#doPrivileged(PrivilegedAction)} blocks. | ||
* This is necessary to limit {@link java.net.SocketPermission} to the transport module. | ||
*/ | ||
public class PrivilegedOioServerSocketChannel extends OioServerSocketChannel { | ||
|
||
@Override | ||
protected int doReadMessages(List<Object> buf) throws Exception { | ||
SecurityManager sm = System.getSecurityManager(); | ||
if (sm != null) { | ||
sm.checkPermission(new SpecialPermission()); | ||
} | ||
try { | ||
return AccessController.doPrivileged((PrivilegedExceptionAction<Integer>) () -> super.doReadMessages(buf)); | ||
} catch (PrivilegedActionException e) { | ||
throw (Exception) e.getCause(); | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
.../src/main/java/org/elasticsearch/transport/netty4/channel/PrivilegedOioSocketChannel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* 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.transport.netty4.channel; | ||
|
||
import io.netty.channel.socket.oio.OioSocketChannel; | ||
import org.elasticsearch.SpecialPermission; | ||
|
||
import java.net.SocketAddress; | ||
import java.security.AccessController; | ||
import java.security.PrivilegedAction; | ||
import java.security.PrivilegedActionException; | ||
import java.security.PrivilegedExceptionAction; | ||
|
||
/** | ||
* Wraps netty calls to {@link java.net.Socket#connect(SocketAddress)} in | ||
* {@link AccessController#doPrivileged(PrivilegedAction)} blocks. This is necessary to limit | ||
* {@link java.net.SocketPermission} to the transport module. | ||
*/ | ||
public class PrivilegedOioSocketChannel extends OioSocketChannel { | ||
|
||
@Override | ||
protected void doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception { | ||
SecurityManager sm = System.getSecurityManager(); | ||
if (sm != null) { | ||
sm.checkPermission(new SpecialPermission()); | ||
} | ||
try { | ||
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> { | ||
super.doConnect(remoteAddress, localAddress); | ||
return null; | ||
}); | ||
} catch (PrivilegedActionException e) { | ||
throw (Exception) e.getCause(); | ||
} | ||
super.doConnect(remoteAddress, localAddress); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters