Skip to content

Commit fcfde3b

Browse files
authored
Misc non-java.base file entitlements (elastic#123078) (elastic#123154) (elastic#123164)
* Misc non-java.base file entitlements * Remove unnecessary dependency
1 parent e498ad1 commit fcfde3b

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
@@ -80,6 +80,7 @@
8080
import java.util.concurrent.ExecutorService;
8181
import java.util.concurrent.ForkJoinPool;
8282
import java.util.function.BiPredicate;
83+
import java.util.logging.FileHandler;
8384

8485
import javax.net.ssl.HostnameVerifier;
8586
import javax.net.ssl.HttpsURLConnection;
@@ -820,9 +821,34 @@ public interface EntitlementChecker {
820821

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

823-
// file system providers
824824
void check$java_nio_file_spi_FileSystemProvider$(Class<?> callerClass);
825825

826+
void check$java_util_logging_FileHandler$(Class<?> callerClass);
827+
828+
void check$java_util_logging_FileHandler$(Class<?> callerClass, String pattern);
829+
830+
void check$java_util_logging_FileHandler$(Class<?> callerClass, String pattern, boolean append);
831+
832+
void check$java_util_logging_FileHandler$(Class<?> callerClass, String pattern, int limit, int count);
833+
834+
void check$java_util_logging_FileHandler$(Class<?> callerClass, String pattern, int limit, int count, boolean append);
835+
836+
void check$java_util_logging_FileHandler$(Class<?> callerClass, String pattern, long limit, int count, boolean append);
837+
838+
void check$java_util_logging_FileHandler$close(Class<?> callerClass, FileHandler that);
839+
840+
void check$java_net_http_HttpRequest$BodyPublishers$$ofFile(Class<?> callerClass, Path path);
841+
842+
void check$java_net_http_HttpResponse$BodyHandlers$$ofFile(Class<?> callerClass, Path path);
843+
844+
void check$java_net_http_HttpResponse$BodyHandlers$$ofFile(Class<?> callerClass, Path path, OpenOption... options);
845+
846+
void check$java_net_http_HttpResponse$BodyHandlers$$ofFileDownload(Class<?> callerClass, Path directory, OpenOption... openOptions);
847+
848+
void check$java_net_http_HttpResponse$BodySubscribers$$ofFile(Class<?> callerClass, Path directory);
849+
850+
void check$java_net_http_HttpResponse$BodySubscribers$$ofFile(Class<?> callerClass, Path directory, OpenOption... openOptions);
851+
826852
void checkNewFileSystem(Class<?> callerClass, FileSystemProvider that, URI uri, Map<String, ?> env);
827853

828854
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;
@@ -476,5 +481,86 @@ static void createScannerFileWithCharsetName() throws FileNotFoundException {
476481
new Scanner(readFile().toFile(), "UTF-8");
477482
}
478483

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

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
@@ -89,6 +89,7 @@
8989
import java.util.concurrent.ExecutorService;
9090
import java.util.concurrent.ForkJoinPool;
9191
import java.util.function.BiPredicate;
92+
import java.util.logging.FileHandler;
9293

9394
import javax.net.ssl.HostnameVerifier;
9495
import javax.net.ssl.HttpsURLConnection;
@@ -1758,6 +1759,78 @@ public void checkSelectorProviderInheritedChannel(Class<?> callerClass, Selector
17581759
policyManager.checkChangeJVMGlobalState(callerClass);
17591760
}
17601761

1762+
@Override
1763+
public void check$java_util_logging_FileHandler$(Class<?> callerClass) {
1764+
policyManager.checkLoggingFileHandler(callerClass);
1765+
}
1766+
1767+
@Override
1768+
public void check$java_util_logging_FileHandler$(Class<?> callerClass, String pattern) {
1769+
policyManager.checkLoggingFileHandler(callerClass);
1770+
}
1771+
1772+
@Override
1773+
public void check$java_util_logging_FileHandler$(Class<?> callerClass, String pattern, boolean append) {
1774+
policyManager.checkLoggingFileHandler(callerClass);
1775+
}
1776+
1777+
@Override
1778+
public void check$java_util_logging_FileHandler$(Class<?> callerClass, String pattern, int limit, int count) {
1779+
policyManager.checkLoggingFileHandler(callerClass);
1780+
}
1781+
1782+
@Override
1783+
public void check$java_util_logging_FileHandler$(Class<?> callerClass, String pattern, int limit, int count, boolean append) {
1784+
policyManager.checkLoggingFileHandler(callerClass);
1785+
}
1786+
1787+
@Override
1788+
public void check$java_util_logging_FileHandler$(Class<?> callerClass, String pattern, long limit, int count, boolean append) {
1789+
policyManager.checkLoggingFileHandler(callerClass);
1790+
}
1791+
1792+
@Override
1793+
public void check$java_util_logging_FileHandler$close(Class<?> callerClass, FileHandler that) {
1794+
// Note that there's no IT test for this one, because there's no way to create
1795+
// a FileHandler. However, we have this check just in case someone does manage
1796+
// to get their hands on a FileHandler and uses close() to cause its lock file to be deleted.
1797+
policyManager.checkLoggingFileHandler(callerClass);
1798+
}
1799+
1800+
@Override
1801+
public void check$java_net_http_HttpRequest$BodyPublishers$$ofFile(Class<?> callerClass, Path path) {
1802+
policyManager.checkFileRead(callerClass, path);
1803+
}
1804+
1805+
@Override
1806+
public void check$java_net_http_HttpResponse$BodyHandlers$$ofFile(Class<?> callerClass, Path path) {
1807+
policyManager.checkFileWrite(callerClass, path);
1808+
}
1809+
1810+
@Override
1811+
public void check$java_net_http_HttpResponse$BodyHandlers$$ofFile(Class<?> callerClass, Path path, OpenOption... options) {
1812+
policyManager.checkFileWrite(callerClass, path);
1813+
}
1814+
1815+
@Override
1816+
public void check$java_net_http_HttpResponse$BodyHandlers$$ofFileDownload(
1817+
Class<?> callerClass,
1818+
Path directory,
1819+
OpenOption... openOptions
1820+
) {
1821+
policyManager.checkFileWrite(callerClass, directory);
1822+
}
1823+
1824+
@Override
1825+
public void check$java_net_http_HttpResponse$BodySubscribers$$ofFile(Class<?> callerClass, Path directory) {
1826+
policyManager.checkFileWrite(callerClass, directory);
1827+
}
1828+
1829+
@Override
1830+
public void check$java_net_http_HttpResponse$BodySubscribers$$ofFile(Class<?> callerClass, Path directory, OpenOption... openOptions) {
1831+
policyManager.checkFileWrite(callerClass, directory);
1832+
}
1833+
17611834
@Override
17621835
public void checkNewFileSystem(Class<?> callerClass, FileSystemProvider that, URI uri, Map<String, ?> env) {
17631836
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)