-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[8.x] [Entitlements] Add checks for native libraries restricted metho…
…ds (#120775) (#121017) * [Entitlements] Add checks for native libraries restricted methods (#120775) * Introducing main21 (does not compile with main23 on the main lib) * Move foreign API to Java22; fix EntitlementInitialization to work across multiple versions * [CI] Auto commit changes from spotless --------- Co-authored-by: elasticsearchmachine <[email protected]>
- Loading branch information
1 parent
98c65ff
commit 2171064
Showing
17 changed files
with
513 additions
and
21 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
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
12 changes: 12 additions & 0 deletions
12
...bridge/src/main21/java/org/elasticsearch/entitlement/bridge/Java21EntitlementChecker.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,12 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.entitlement.bridge; | ||
|
||
public interface Java21EntitlementChecker extends EntitlementChecker {} |
27 changes: 27 additions & 0 deletions
27
.../src/main21/java/org/elasticsearch/entitlement/bridge/Java21EntitlementCheckerHandle.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,27 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.entitlement.bridge; | ||
|
||
/** | ||
* Java21 variant of {@link EntitlementChecker} handle holder. | ||
*/ | ||
public class Java21EntitlementCheckerHandle { | ||
|
||
public static Java21EntitlementChecker instance() { | ||
return Holder.instance; | ||
} | ||
|
||
private static class Holder { | ||
private static final Java21EntitlementChecker instance = HandleLoader.load(Java21EntitlementChecker.class); | ||
} | ||
|
||
// no construction | ||
private Java21EntitlementCheckerHandle() {} | ||
} |
76 changes: 76 additions & 0 deletions
76
...bridge/src/main22/java/org/elasticsearch/entitlement/bridge/Java22EntitlementChecker.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,76 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.entitlement.bridge; | ||
|
||
import java.lang.foreign.AddressLayout; | ||
import java.lang.foreign.Arena; | ||
import java.lang.foreign.FunctionDescriptor; | ||
import java.lang.foreign.Linker; | ||
import java.lang.foreign.MemoryLayout; | ||
import java.lang.foreign.MemorySegment; | ||
import java.lang.invoke.MethodHandle; | ||
import java.nio.file.Path; | ||
import java.util.function.Consumer; | ||
|
||
public interface Java22EntitlementChecker extends Java21EntitlementChecker { | ||
// Sealed implementation of java.lang.foreign.AddressLayout | ||
void check$jdk_internal_foreign_layout_ValueLayouts$OfAddressImpl$withTargetLayout( | ||
Class<?> callerClass, | ||
AddressLayout that, | ||
MemoryLayout memoryLayout | ||
); | ||
|
||
// Sealed implementation of java.lang.foreign.Linker | ||
void check$jdk_internal_foreign_abi_AbstractLinker$downcallHandle( | ||
Class<?> callerClass, | ||
Linker that, | ||
FunctionDescriptor function, | ||
Linker.Option... options | ||
); | ||
|
||
void check$jdk_internal_foreign_abi_AbstractLinker$downcallHandle( | ||
Class<?> callerClass, | ||
Linker that, | ||
MemorySegment address, | ||
FunctionDescriptor function, | ||
Linker.Option... options | ||
); | ||
|
||
void check$jdk_internal_foreign_abi_AbstractLinker$upcallStub( | ||
Class<?> callerClass, | ||
Linker that, | ||
MethodHandle target, | ||
FunctionDescriptor function, | ||
Arena arena, | ||
Linker.Option... options | ||
); | ||
|
||
// Sealed implementation for java.lang.foreign.MemorySegment.reinterpret(long) | ||
void check$jdk_internal_foreign_AbstractMemorySegmentImpl$reinterpret(Class<?> callerClass, MemorySegment that, long newSize); | ||
|
||
void check$jdk_internal_foreign_AbstractMemorySegmentImpl$reinterpret( | ||
Class<?> callerClass, | ||
MemorySegment that, | ||
long newSize, | ||
Arena arena, | ||
Consumer<MemorySegment> cleanup | ||
); | ||
|
||
void check$jdk_internal_foreign_AbstractMemorySegmentImpl$reinterpret( | ||
Class<?> callerClass, | ||
MemorySegment that, | ||
Arena arena, | ||
Consumer<MemorySegment> cleanup | ||
); | ||
|
||
void check$java_lang_foreign_SymbolLookup$$libraryLookup(Class<?> callerClass, String name, Arena arena); | ||
|
||
void check$java_lang_foreign_SymbolLookup$$libraryLookup(Class<?> callerClass, Path path, Arena arena); | ||
} |
27 changes: 27 additions & 0 deletions
27
.../src/main22/java/org/elasticsearch/entitlement/bridge/Java22EntitlementCheckerHandle.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,27 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.entitlement.bridge; | ||
|
||
/** | ||
* Java22 variant of {@link EntitlementChecker} handle holder. | ||
*/ | ||
public class Java22EntitlementCheckerHandle { | ||
|
||
public static Java22EntitlementChecker instance() { | ||
return Holder.instance; | ||
} | ||
|
||
private static class Holder { | ||
private static final Java22EntitlementChecker instance = HandleLoader.load(Java22EntitlementChecker.class); | ||
} | ||
|
||
// no construction | ||
private Java22EntitlementCheckerHandle() {} | ||
} |
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
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
33 changes: 33 additions & 0 deletions
33
...ugin/src/main/java/org/elasticsearch/entitlement/qa/test/VersionSpecificNativeChecks.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,33 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.entitlement.qa.test; | ||
|
||
class VersionSpecificNativeChecks { | ||
|
||
static void enableNativeAccess() throws Exception {} | ||
|
||
static void addressLayoutWithTargetLayout() {} | ||
|
||
static void linkerDowncallHandle() {} | ||
|
||
static void linkerDowncallHandleWithAddress() {} | ||
|
||
static void linkerUpcallStub() throws NoSuchMethodException {} | ||
|
||
static void memorySegmentReinterpret() {} | ||
|
||
static void memorySegmentReinterpretWithCleanup() {} | ||
|
||
static void memorySegmentReinterpretWithSizeAndCleanup() {} | ||
|
||
static void symbolLookupWithPath() {} | ||
|
||
static void symbolLookupWithName() {} | ||
} |
118 changes: 118 additions & 0 deletions
118
...in/src/main22/java/org/elasticsearch/entitlement/qa/test/VersionSpecificNativeChecks.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,118 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.entitlement.qa.test; | ||
|
||
import org.elasticsearch.entitlement.qa.entitled.EntitledPlugin; | ||
|
||
import java.lang.foreign.AddressLayout; | ||
import java.lang.foreign.Arena; | ||
import java.lang.foreign.FunctionDescriptor; | ||
import java.lang.foreign.Linker; | ||
import java.lang.foreign.MemoryLayout; | ||
import java.lang.foreign.MemorySegment; | ||
import java.lang.foreign.SymbolLookup; | ||
import java.lang.foreign.ValueLayout; | ||
import java.lang.invoke.MethodHandle; | ||
import java.lang.invoke.MethodHandles; | ||
import java.lang.invoke.MethodType; | ||
import java.lang.module.Configuration; | ||
import java.lang.module.ModuleFinder; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import static java.lang.foreign.ValueLayout.ADDRESS; | ||
import static java.lang.foreign.ValueLayout.JAVA_LONG; | ||
|
||
class VersionSpecificNativeChecks { | ||
|
||
static void enableNativeAccess() throws Exception { | ||
ModuleLayer parent = ModuleLayer.boot(); | ||
|
||
var location = EntitledPlugin.class.getProtectionDomain().getCodeSource().getLocation(); | ||
|
||
// We create a layer for our own module, so we have a controller to try and call enableNativeAccess on it. | ||
// This works in both the modular and non-modular case: the target module has to be present in the new layer, but its entitlements | ||
// and policies do not matter to us: we are checking that the caller is (or isn't) entitled to use enableNativeAccess | ||
Configuration cf = parent.configuration() | ||
.resolve(ModuleFinder.of(Path.of(location.toURI())), ModuleFinder.of(), Set.of("org.elasticsearch.entitlement.qa.entitled")); | ||
var controller = ModuleLayer.defineModulesWithOneLoader(cf, List.of(parent), ClassLoader.getSystemClassLoader()); | ||
var targetModule = controller.layer().findModule("org.elasticsearch.entitlement.qa.entitled"); | ||
|
||
controller.enableNativeAccess(targetModule.get()); | ||
} | ||
|
||
static void addressLayoutWithTargetLayout() { | ||
AddressLayout addressLayout = ADDRESS.withoutTargetLayout(); | ||
addressLayout.withTargetLayout(MemoryLayout.sequenceLayout(Long.MAX_VALUE, ValueLayout.JAVA_BYTE)); | ||
} | ||
|
||
static void linkerDowncallHandle() { | ||
Linker linker = Linker.nativeLinker(); | ||
linker.downcallHandle(FunctionDescriptor.of(JAVA_LONG, ADDRESS)); | ||
} | ||
|
||
static void linkerDowncallHandleWithAddress() { | ||
Linker linker = Linker.nativeLinker(); | ||
linker.downcallHandle(linker.defaultLookup().find("strlen").get(), FunctionDescriptor.of(JAVA_LONG, ADDRESS)); | ||
} | ||
|
||
static int callback() { | ||
return 0; | ||
} | ||
|
||
static void linkerUpcallStub() throws NoSuchMethodException { | ||
Linker linker = Linker.nativeLinker(); | ||
|
||
MethodHandle mh = null; | ||
try { | ||
mh = MethodHandles.lookup().findStatic(VersionSpecificNativeChecks.class, "callback", MethodType.methodType(int.class)); | ||
} catch (IllegalAccessException e) { | ||
assert false; | ||
} | ||
|
||
FunctionDescriptor callbackDescriptor = FunctionDescriptor.of(ValueLayout.JAVA_INT); | ||
linker.upcallStub(mh, callbackDescriptor, Arena.ofAuto()); | ||
} | ||
|
||
static void memorySegmentReinterpret() { | ||
Arena arena = Arena.ofAuto(); | ||
MemorySegment segment = arena.allocate(100); | ||
segment.reinterpret(50); | ||
} | ||
|
||
static void memorySegmentReinterpretWithCleanup() { | ||
Arena arena = Arena.ofAuto(); | ||
MemorySegment segment = arena.allocate(100); | ||
segment.reinterpret(Arena.ofAuto(), s -> {}); | ||
} | ||
|
||
static void memorySegmentReinterpretWithSizeAndCleanup() { | ||
Arena arena = Arena.ofAuto(); | ||
MemorySegment segment = arena.allocate(100); | ||
segment.reinterpret(50, Arena.ofAuto(), s -> {}); | ||
} | ||
|
||
static void symbolLookupWithPath() { | ||
try { | ||
SymbolLookup.libraryLookup(Path.of("/foo/bar/libFoo.so"), Arena.ofAuto()); | ||
} catch (IllegalArgumentException e) { | ||
// IllegalArgumentException is thrown if path does not point to a valid library (and it does not) | ||
} | ||
} | ||
|
||
static void symbolLookupWithName() { | ||
try { | ||
SymbolLookup.libraryLookup("foo", Arena.ofAuto()); | ||
} catch (IllegalArgumentException e) { | ||
// IllegalArgumentException is thrown if path does not point to a valid library (and it does not) | ||
} | ||
} | ||
} |
Oops, something went wrong.