Skip to content

Commit b29a5ae

Browse files
authored
Missing providers from nio (#122004) (#122053)
1 parent 09a64e6 commit b29a5ae

File tree

5 files changed

+155
-0
lines changed

5 files changed

+155
-0
lines changed

libs/entitlement/bridge/src/main/java/org/elasticsearch/entitlement/bridge/EntitlementChecker.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.nio.channels.DatagramChannel;
4949
import java.nio.channels.ServerSocketChannel;
5050
import java.nio.channels.SocketChannel;
51+
import java.nio.channels.spi.SelectorProvider;
5152
import java.nio.charset.Charset;
5253
import java.nio.file.OpenOption;
5354
import java.nio.file.Path;
@@ -214,6 +215,8 @@ public interface EntitlementChecker {
214215

215216
void check$jdk_vm_ci_services_Services$$loadSingle(Class<?> callerClass, Class<?> service, boolean required);
216217

218+
void check$java_nio_charset_spi_CharsetProvider$(Class<?> callerClass);
219+
217220
/// /////////////////
218221
//
219222
// Network access
@@ -411,6 +414,16 @@ public interface EntitlementChecker {
411414

412415
void check$sun_nio_ch_DatagramChannelImpl$receive(Class<?> callerClass, DatagramChannel that, ByteBuffer dst);
413416

417+
// providers (SPI)
418+
419+
// protected constructors
420+
void check$java_nio_channels_spi_SelectorProvider$(Class<?> callerClass);
421+
422+
void check$java_nio_channels_spi_AsynchronousChannelProvider$(Class<?> callerClass);
423+
424+
// provider methods (dynamic)
425+
void checkSelectorProviderInheritedChannel(Class<?> callerClass, SelectorProvider that);
426+
414427
/// /////////////////
415428
//
416429
// Load native libraries

libs/entitlement/qa/entitlement-test-plugin/src/main/java/org/elasticsearch/entitlement/qa/test/DummyImplementations.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,24 @@
1717
import java.net.DatagramSocketImpl;
1818
import java.net.InetAddress;
1919
import java.net.NetworkInterface;
20+
import java.net.ProtocolFamily;
2021
import java.net.ServerSocket;
2122
import java.net.Socket;
2223
import java.net.SocketAddress;
2324
import java.net.SocketException;
2425
import java.net.SocketImpl;
26+
import java.nio.channels.AsynchronousChannelGroup;
27+
import java.nio.channels.AsynchronousServerSocketChannel;
28+
import java.nio.channels.AsynchronousSocketChannel;
29+
import java.nio.channels.DatagramChannel;
30+
import java.nio.channels.Pipe;
31+
import java.nio.channels.ServerSocketChannel;
32+
import java.nio.channels.SocketChannel;
33+
import java.nio.channels.spi.AbstractSelector;
34+
import java.nio.channels.spi.AsynchronousChannelProvider;
35+
import java.nio.channels.spi.SelectorProvider;
36+
import java.nio.charset.Charset;
37+
import java.nio.charset.spi.CharsetProvider;
2538
import java.security.cert.Certificate;
2639
import java.text.BreakIterator;
2740
import java.text.Collator;
@@ -35,8 +48,11 @@
3548
import java.text.spi.DateFormatSymbolsProvider;
3649
import java.text.spi.DecimalFormatSymbolsProvider;
3750
import java.text.spi.NumberFormatProvider;
51+
import java.util.Iterator;
3852
import java.util.Locale;
3953
import java.util.Map;
54+
import java.util.concurrent.ExecutorService;
55+
import java.util.concurrent.ThreadFactory;
4056
import java.util.spi.CalendarDataProvider;
4157
import java.util.spi.CalendarNameProvider;
4258
import java.util.spi.CurrencyNameProvider;
@@ -486,4 +502,70 @@ protected void connect(InetAddress address, int port) throws SocketException {}
486502
private static RuntimeException unexpected() {
487503
return new IllegalStateException("This method isn't supposed to be called");
488504
}
505+
506+
static class DummySelectorProvider extends SelectorProvider {
507+
@Override
508+
public DatagramChannel openDatagramChannel() throws IOException {
509+
return null;
510+
}
511+
512+
@Override
513+
public DatagramChannel openDatagramChannel(ProtocolFamily family) throws IOException {
514+
return null;
515+
}
516+
517+
@Override
518+
public Pipe openPipe() throws IOException {
519+
return null;
520+
}
521+
522+
@Override
523+
public AbstractSelector openSelector() throws IOException {
524+
return null;
525+
}
526+
527+
@Override
528+
public ServerSocketChannel openServerSocketChannel() throws IOException {
529+
return null;
530+
}
531+
532+
@Override
533+
public SocketChannel openSocketChannel() throws IOException {
534+
return null;
535+
}
536+
}
537+
538+
static class DummyAsynchronousChannelProvider extends AsynchronousChannelProvider {
539+
@Override
540+
public AsynchronousChannelGroup openAsynchronousChannelGroup(int nThreads, ThreadFactory threadFactory) throws IOException {
541+
return null;
542+
}
543+
544+
@Override
545+
public AsynchronousChannelGroup openAsynchronousChannelGroup(ExecutorService executor, int initialSize) throws IOException {
546+
return null;
547+
}
548+
549+
@Override
550+
public AsynchronousServerSocketChannel openAsynchronousServerSocketChannel(AsynchronousChannelGroup group) throws IOException {
551+
return null;
552+
}
553+
554+
@Override
555+
public AsynchronousSocketChannel openAsynchronousSocketChannel(AsynchronousChannelGroup group) throws IOException {
556+
return null;
557+
}
558+
}
559+
560+
static class DummyCharsetProvider extends CharsetProvider {
561+
@Override
562+
public Iterator<Charset> charsets() {
563+
return null;
564+
}
565+
566+
@Override
567+
public Charset charsetForName(String charsetName) {
568+
return null;
569+
}
570+
}
489571
}

libs/entitlement/qa/entitlement-test-plugin/src/main/java/org/elasticsearch/entitlement/qa/test/SpiActions.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
package org.elasticsearch.entitlement.qa.test;
1111

12+
import java.io.IOException;
13+
import java.nio.channels.Channel;
14+
import java.nio.channels.spi.SelectorProvider;
15+
1216
import static org.elasticsearch.entitlement.qa.test.EntitlementTest.ExpectedAccess.ALWAYS_DENIED;
1317

1418
class SpiActions {
@@ -72,5 +76,32 @@ static void createLocaleServiceProvider() {
7276
new DummyImplementations.DummyLocaleServiceProvider();
7377
}
7478

79+
@EntitlementTest(expectedAccess = ALWAYS_DENIED)
80+
static void getInheritedChannel() throws IOException {
81+
Channel channel = null;
82+
try {
83+
channel = SelectorProvider.provider().inheritedChannel();
84+
} finally {
85+
if (channel != null) {
86+
channel.close();
87+
}
88+
}
89+
}
90+
91+
@EntitlementTest(expectedAccess = ALWAYS_DENIED)
92+
static void createSelectorProvider() {
93+
new DummyImplementations.DummySelectorProvider();
94+
}
95+
96+
@EntitlementTest(expectedAccess = ALWAYS_DENIED)
97+
static void createAsynchronousChannelProvider() {
98+
new DummyImplementations.DummyAsynchronousChannelProvider();
99+
}
100+
101+
@EntitlementTest(expectedAccess = ALWAYS_DENIED)
102+
static void createCharsetProvider() {
103+
new DummyImplementations.DummyCharsetProvider();
104+
}
105+
75106
private SpiActions() {}
76107
}

libs/entitlement/src/main/java/org/elasticsearch/entitlement/initialization/EntitlementInitialization.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.lang.instrument.Instrumentation;
3232
import java.lang.reflect.Constructor;
3333
import java.lang.reflect.InvocationTargetException;
34+
import java.nio.channels.spi.SelectorProvider;
3435
import java.nio.file.FileSystems;
3536
import java.nio.file.OpenOption;
3637
import java.nio.file.Path;
@@ -79,6 +80,13 @@ public static void initialize(Instrumentation inst) throws Exception {
7980
"checkNewInputStream",
8081
Path.class,
8182
OpenOption[].class
83+
),
84+
INSTRUMENTATION_SERVICE.lookupImplementationMethod(
85+
SelectorProvider.class,
86+
"inheritedChannel",
87+
SelectorProvider.provider().getClass(),
88+
EntitlementChecker.class,
89+
"checkSelectorProviderInheritedChannel"
8290
)
8391
).forEach(instrumentation -> checkMethods.put(instrumentation.targetMethod(), instrumentation.checkMethod()));
8492

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/api/ElasticsearchEntitlementChecker.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import java.nio.channels.DatagramChannel;
5454
import java.nio.channels.ServerSocketChannel;
5555
import java.nio.channels.SocketChannel;
56+
import java.nio.channels.spi.SelectorProvider;
5657
import java.nio.charset.Charset;
5758
import java.nio.file.OpenOption;
5859
import java.nio.file.Path;
@@ -289,6 +290,11 @@ public ElasticsearchEntitlementChecker(PolicyManager policyManager) {
289290
policyManager.checkChangeJVMGlobalState(callerClass);
290291
}
291292

293+
@Override
294+
public void check$java_nio_charset_spi_CharsetProvider$(Class<?> callerClass) {
295+
policyManager.checkChangeJVMGlobalState(callerClass);
296+
}
297+
292298
@Override
293299
public void check$com_sun_tools_jdi_VirtualMachineManagerImpl$$virtualMachineManager(Class<?> callerClass) {
294300
policyManager.checkChangeJVMGlobalState(callerClass);
@@ -801,6 +807,21 @@ public ElasticsearchEntitlementChecker(PolicyManager policyManager) {
801807
policyManager.checkInboundNetworkAccess(callerClass);
802808
}
803809

810+
@Override
811+
public void check$java_nio_channels_spi_SelectorProvider$(Class<?> callerClass) {
812+
policyManager.checkChangeNetworkHandling(callerClass);
813+
}
814+
815+
@Override
816+
public void check$java_nio_channels_spi_AsynchronousChannelProvider$(Class<?> callerClass) {
817+
policyManager.checkChangeNetworkHandling(callerClass);
818+
}
819+
820+
@Override
821+
public void checkSelectorProviderInheritedChannel(Class<?> callerClass, SelectorProvider that) {
822+
policyManager.checkChangeNetworkHandling(callerClass);
823+
}
824+
804825
@Override
805826
public void check$java_lang_Runtime$load(Class<?> callerClass, Runtime that, String filename) {
806827
// TODO: check filesystem entitlement READ

0 commit comments

Comments
 (0)