Skip to content

Commit 605309e

Browse files
authored
Misc non-java.base file entitlements (#123078) (#123152)
* Misc non-java.base file entitlements * Remove unnecessary dependency
1 parent 73bea3a commit 605309e

File tree

7 files changed

+195
-3
lines changed

7 files changed

+195
-3
lines changed

libs/entitlement/bridge/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
module org.elasticsearch.entitlement.bridge {
1313
requires java.net.http;
1414
requires jdk.net;
15+
requires java.logging;
1516

1617
exports org.elasticsearch.entitlement.bridge;
1718
}

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
import java.util.concurrent.ForkJoinPool;
8989
import java.util.function.BiPredicate;
9090
import java.util.function.Consumer;
91+
import java.util.logging.FileHandler;
9192

9293
import javax.net.ssl.HostnameVerifier;
9394
import javax.net.ssl.HttpsURLConnection;
@@ -882,9 +883,34 @@ public interface EntitlementChecker {
882883

883884
void check$java_nio_file_Files$$lines(Class<?> callerClass, Path path);
884885

885-
// file system providers
886886
void check$java_nio_file_spi_FileSystemProvider$(Class<?> callerClass);
887887

888+
void check$java_util_logging_FileHandler$(Class<?> callerClass);
889+
890+
void check$java_util_logging_FileHandler$(Class<?> callerClass, String pattern);
891+
892+
void check$java_util_logging_FileHandler$(Class<?> callerClass, String pattern, boolean append);
893+
894+
void check$java_util_logging_FileHandler$(Class<?> callerClass, String pattern, int limit, int count);
895+
896+
void check$java_util_logging_FileHandler$(Class<?> callerClass, String pattern, int limit, int count, boolean append);
897+
898+
void check$java_util_logging_FileHandler$(Class<?> callerClass, String pattern, long limit, int count, boolean append);
899+
900+
void check$java_util_logging_FileHandler$close(Class<?> callerClass, FileHandler that);
901+
902+
void check$java_net_http_HttpRequest$BodyPublishers$$ofFile(Class<?> callerClass, Path path);
903+
904+
void check$java_net_http_HttpResponse$BodyHandlers$$ofFile(Class<?> callerClass, Path path);
905+
906+
void check$java_net_http_HttpResponse$BodyHandlers$$ofFile(Class<?> callerClass, Path path, OpenOption... options);
907+
908+
void check$java_net_http_HttpResponse$BodyHandlers$$ofFileDownload(Class<?> callerClass, Path directory, OpenOption... openOptions);
909+
910+
void check$java_net_http_HttpResponse$BodySubscribers$$ofFile(Class<?> callerClass, Path directory);
911+
912+
void check$java_net_http_HttpResponse$BodySubscribers$$ofFile(Class<?> callerClass, Path directory, OpenOption... openOptions);
913+
888914
void checkNewFileSystem(Class<?> callerClass, FileSystemProvider that, URI uri, Map<String, ?> env);
889915

890916
void checkNewFileSystem(Class<?> callerClass, FileSystemProvider that, Path path, Map<String, ?> env);

libs/entitlement/qa/entitled-plugin/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
requires org.elasticsearch.entitlement;
1313
requires org.elasticsearch.base; // SuppressForbidden
1414
requires org.elasticsearch.logging;
15+
requires java.logging;
1516

1617
exports org.elasticsearch.entitlement.qa.entitled; // Must be unqualified so non-modular IT tests can call us
1718
}

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,22 @@
2222
import java.io.FileWriter;
2323
import java.io.IOException;
2424
import java.io.RandomAccessFile;
25+
import java.net.http.HttpRequest;
26+
import java.net.http.HttpResponse;
2527
import java.nio.charset.StandardCharsets;
2628
import java.nio.file.Path;
2729
import java.nio.file.Paths;
2830
import java.security.GeneralSecurityException;
2931
import java.security.KeyStore;
3032
import java.util.Scanner;
3133
import java.util.jar.JarFile;
34+
import java.util.logging.FileHandler;
3235
import java.util.zip.ZipException;
3336
import java.util.zip.ZipFile;
3437

3538
import static java.nio.charset.Charset.defaultCharset;
39+
import static java.nio.file.StandardOpenOption.CREATE;
40+
import static java.nio.file.StandardOpenOption.WRITE;
3641
import static java.util.zip.ZipFile.OPEN_DELETE;
3742
import static java.util.zip.ZipFile.OPEN_READ;
3843
import static org.elasticsearch.entitlement.qa.entitled.EntitledActions.createTempFileForWrite;
@@ -477,5 +482,86 @@ static void createScannerFileWithCharsetName() throws FileNotFoundException {
477482
new Scanner(readFile().toFile(), "UTF-8");
478483
}
479484

485+
@EntitlementTest(expectedAccess = ALWAYS_DENIED)
486+
static void fileHandler() throws IOException {
487+
new FileHandler();
488+
}
489+
490+
@EntitlementTest(expectedAccess = ALWAYS_DENIED)
491+
static void fileHandler_String() throws IOException {
492+
new FileHandler(readFile().toString());
493+
}
494+
495+
@EntitlementTest(expectedAccess = ALWAYS_DENIED)
496+
static void fileHandler_StringBoolean() throws IOException {
497+
new FileHandler(readFile().toString(), false);
498+
}
499+
500+
@EntitlementTest(expectedAccess = ALWAYS_DENIED)
501+
static void fileHandler_StringIntInt() throws IOException {
502+
new FileHandler(readFile().toString(), 1, 2);
503+
}
504+
505+
@EntitlementTest(expectedAccess = ALWAYS_DENIED)
506+
static void fileHandler_StringIntIntBoolean() throws IOException {
507+
new FileHandler(readFile().toString(), 1, 2, false);
508+
}
509+
510+
@EntitlementTest(expectedAccess = ALWAYS_DENIED)
511+
static void fileHandler_StringLongIntBoolean() throws IOException {
512+
new FileHandler(readFile().toString(), 1L, 2, false);
513+
}
514+
515+
@EntitlementTest(expectedAccess = PLUGINS)
516+
static void httpRequestBodyPublishersOfFile() throws IOException {
517+
HttpRequest.BodyPublishers.ofFile(readFile());
518+
}
519+
520+
@EntitlementTest(expectedAccess = PLUGINS)
521+
static void httpResponseBodyHandlersOfFile() {
522+
HttpResponse.BodyHandlers.ofFile(readWriteFile());
523+
}
524+
525+
@EntitlementTest(expectedAccess = ALWAYS_DENIED)
526+
static void httpResponseBodyHandlersOfFile_readOnly() {
527+
HttpResponse.BodyHandlers.ofFile(readFile());
528+
}
529+
530+
@EntitlementTest(expectedAccess = PLUGINS)
531+
static void httpResponseBodyHandlersOfFileDownload() {
532+
HttpResponse.BodyHandlers.ofFileDownload(readWriteDir());
533+
}
534+
535+
@EntitlementTest(expectedAccess = ALWAYS_DENIED)
536+
static void httpResponseBodyHandlersOfFileDownload_readOnly() {
537+
HttpResponse.BodyHandlers.ofFileDownload(readDir());
538+
}
539+
540+
@EntitlementTest(expectedAccess = PLUGINS)
541+
static void httpResponseBodySubscribersOfFile_File() {
542+
HttpResponse.BodySubscribers.ofFile(readWriteFile());
543+
}
544+
545+
@EntitlementTest(expectedAccess = ALWAYS_DENIED)
546+
static void httpResponseBodySubscribersOfFile_File_readOnly() {
547+
HttpResponse.BodySubscribers.ofFile(readFile());
548+
}
549+
550+
@EntitlementTest(expectedAccess = PLUGINS)
551+
static void httpResponseBodySubscribersOfFile_FileOpenOptions() {
552+
// Note that, unlike other methods like BodyHandlers.ofFile, this is indeed
553+
// an overload distinct from ofFile with no OpenOptions, and so it needs its
554+
// own instrumentation and its own test.
555+
HttpResponse.BodySubscribers.ofFile(readWriteFile(), CREATE, WRITE);
556+
}
557+
558+
@EntitlementTest(expectedAccess = ALWAYS_DENIED)
559+
static void httpResponseBodySubscribersOfFile_FileOpenOptions_readOnly() {
560+
// Note that, unlike other methods like BodyHandlers.ofFile, this is indeed
561+
// an overload distinct from ofFile with no OpenOptions, and so it needs its
562+
// own instrumentation and its own test.
563+
HttpResponse.BodySubscribers.ofFile(readFile(), CREATE, WRITE);
564+
}
565+
480566
private FileCheckActions() {}
481567
}

libs/entitlement/src/main/java/module-info.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
*/
99

1010
module org.elasticsearch.entitlement {
11+
requires org.elasticsearch.base;
1112
requires org.elasticsearch.xcontent;
1213
requires org.elasticsearch.logging;
1314
requires java.instrument;
14-
requires org.elasticsearch.base;
15-
requires jdk.attach;
15+
requires java.logging;
1616
requires java.net.http;
17+
requires jdk.attach;
1718
requires jdk.net;
1819

1920
requires static org.elasticsearch.entitlement.bridge; // At runtime, this will be in java.base

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
import java.util.concurrent.ForkJoinPool;
9898
import java.util.function.BiPredicate;
9999
import java.util.function.Consumer;
100+
import java.util.logging.FileHandler;
100101

101102
import javax.net.ssl.HostnameVerifier;
102103
import javax.net.ssl.HttpsURLConnection;
@@ -1845,6 +1846,78 @@ public void checkSelectorProviderInheritedChannel(Class<?> callerClass, Selector
18451846
policyManager.checkChangeJVMGlobalState(callerClass);
18461847
}
18471848

1849+
@Override
1850+
public void check$java_util_logging_FileHandler$(Class<?> callerClass) {
1851+
policyManager.checkLoggingFileHandler(callerClass);
1852+
}
1853+
1854+
@Override
1855+
public void check$java_util_logging_FileHandler$(Class<?> callerClass, String pattern) {
1856+
policyManager.checkLoggingFileHandler(callerClass);
1857+
}
1858+
1859+
@Override
1860+
public void check$java_util_logging_FileHandler$(Class<?> callerClass, String pattern, boolean append) {
1861+
policyManager.checkLoggingFileHandler(callerClass);
1862+
}
1863+
1864+
@Override
1865+
public void check$java_util_logging_FileHandler$(Class<?> callerClass, String pattern, int limit, int count) {
1866+
policyManager.checkLoggingFileHandler(callerClass);
1867+
}
1868+
1869+
@Override
1870+
public void check$java_util_logging_FileHandler$(Class<?> callerClass, String pattern, int limit, int count, boolean append) {
1871+
policyManager.checkLoggingFileHandler(callerClass);
1872+
}
1873+
1874+
@Override
1875+
public void check$java_util_logging_FileHandler$(Class<?> callerClass, String pattern, long limit, int count, boolean append) {
1876+
policyManager.checkLoggingFileHandler(callerClass);
1877+
}
1878+
1879+
@Override
1880+
public void check$java_util_logging_FileHandler$close(Class<?> callerClass, FileHandler that) {
1881+
// Note that there's no IT test for this one, because there's no way to create
1882+
// a FileHandler. However, we have this check just in case someone does manage
1883+
// to get their hands on a FileHandler and uses close() to cause its lock file to be deleted.
1884+
policyManager.checkLoggingFileHandler(callerClass);
1885+
}
1886+
1887+
@Override
1888+
public void check$java_net_http_HttpRequest$BodyPublishers$$ofFile(Class<?> callerClass, Path path) {
1889+
policyManager.checkFileRead(callerClass, path);
1890+
}
1891+
1892+
@Override
1893+
public void check$java_net_http_HttpResponse$BodyHandlers$$ofFile(Class<?> callerClass, Path path) {
1894+
policyManager.checkFileWrite(callerClass, path);
1895+
}
1896+
1897+
@Override
1898+
public void check$java_net_http_HttpResponse$BodyHandlers$$ofFile(Class<?> callerClass, Path path, OpenOption... options) {
1899+
policyManager.checkFileWrite(callerClass, path);
1900+
}
1901+
1902+
@Override
1903+
public void check$java_net_http_HttpResponse$BodyHandlers$$ofFileDownload(
1904+
Class<?> callerClass,
1905+
Path directory,
1906+
OpenOption... openOptions
1907+
) {
1908+
policyManager.checkFileWrite(callerClass, directory);
1909+
}
1910+
1911+
@Override
1912+
public void check$java_net_http_HttpResponse$BodySubscribers$$ofFile(Class<?> callerClass, Path directory) {
1913+
policyManager.checkFileWrite(callerClass, directory);
1914+
}
1915+
1916+
@Override
1917+
public void check$java_net_http_HttpResponse$BodySubscribers$$ofFile(Class<?> callerClass, Path directory, OpenOption... openOptions) {
1918+
policyManager.checkFileWrite(callerClass, directory);
1919+
}
1920+
18481921
@Override
18491922
public void checkNewFileSystem(Class<?> callerClass, FileSystemProvider that, URI uri, Map<String, ?> env) {
18501923
policyManager.checkChangeJVMGlobalState(callerClass);

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/PolicyManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ public void checkChangeJVMGlobalState(Class<?> callerClass) {
240240
neverEntitled(callerClass, () -> walkStackForCheckMethodName().orElse("change JVM global state"));
241241
}
242242

243+
public void checkLoggingFileHandler(Class<?> callerClass) {
244+
neverEntitled(callerClass, () -> walkStackForCheckMethodName().orElse("create logging file handler"));
245+
}
246+
243247
private Optional<String> walkStackForCheckMethodName() {
244248
// Look up the check$ method to compose an informative error message.
245249
// This way, we don't need to painstakingly describe every individual global-state change.

0 commit comments

Comments
 (0)