Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion libs/entitlement/asm-provider/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ dependencies {
compileOnly project(':libs:core')
compileOnly project(':libs:logging')
implementation 'org.ow2.asm:asm:9.7.1'
implementation 'org.ow2.asm:asm-util:9.7.1'
implementation 'org.ow2.asm:asm-tree:9.7.1'
implementation 'org.ow2.asm:asm-analysis:9.7.1'
testImplementation project(":test:framework")
testImplementation project(":libs:entitlement:bridge")
testImplementation 'org.ow2.asm:asm-util:9.7.1'
}

tasks.named('test').configure {
Expand Down
26 changes: 26 additions & 0 deletions libs/entitlement/asm-provider/licenses/asm-analysis-LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Copyright (c) 2012 France Télécom
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

26 changes: 26 additions & 0 deletions libs/entitlement/asm-provider/licenses/asm-tree-LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Copyright (c) 2012 France Télécom
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
1 change: 1 addition & 0 deletions libs/entitlement/asm-provider/licenses/asm-tree-NOTICE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

26 changes: 26 additions & 0 deletions libs/entitlement/asm-provider/licenses/asm-util-LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Copyright (c) 2012 France Télécom
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
1 change: 1 addition & 0 deletions libs/entitlement/asm-provider/licenses/asm-util-NOTICE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

module org.elasticsearch.entitlement.instrumentation {
requires org.objectweb.asm;
requires org.objectweb.asm.util;
requires org.elasticsearch.entitlement;

requires static org.elasticsearch.base; // for SuppressForbidden
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public class InstrumentationServiceImpl implements InstrumentationService {
private static final String OBJECT_INTERNAL_NAME = Type.getInternalName(Object.class);

@Override
public Instrumenter newInstrumenter(Class<?> clazz, Map<MethodKey, CheckMethod> methods) {
return InstrumenterImpl.create(clazz, methods);
public Instrumenter newInstrumenter(Class<?> clazz, Map<MethodKey, CheckMethod> methods, boolean verifyBytecode) {
return InstrumenterImpl.create(clazz, methods, verifyBytecode);
}

private interface CheckerMethodVisitor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@

package org.elasticsearch.entitlement.instrumentation.impl;

import org.elasticsearch.core.Strings;
import org.elasticsearch.entitlement.instrumentation.CheckMethod;
import org.elasticsearch.entitlement.instrumentation.EntitlementInstrumented;
import org.elasticsearch.entitlement.instrumentation.Instrumenter;
import org.elasticsearch.entitlement.instrumentation.MethodKey;
import org.elasticsearch.logging.Level;
import org.elasticsearch.logging.LogManager;
import org.elasticsearch.logging.Logger;
import org.objectweb.asm.AnnotationVisitor;
Expand All @@ -24,9 +26,12 @@
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.RecordComponentVisitor;
import org.objectweb.asm.Type;
import org.objectweb.asm.util.CheckClassAdapter;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Map;
import java.util.stream.Stream;

Expand All @@ -43,6 +48,7 @@ public class InstrumenterImpl implements Instrumenter {

private final String getCheckerClassMethodDescriptor;
private final String handleClass;
private final boolean verifyBytecode;

/**
* To avoid class name collisions during testing without an agent to replace classes in-place.
Expand All @@ -54,19 +60,22 @@ public class InstrumenterImpl implements Instrumenter {
String handleClass,
String getCheckerClassMethodDescriptor,
String classNameSuffix,
Map<MethodKey, CheckMethod> checkMethods
Map<MethodKey, CheckMethod> checkMethods,
boolean verifyBytecode
) {
this.handleClass = handleClass;
this.getCheckerClassMethodDescriptor = getCheckerClassMethodDescriptor;
this.classNameSuffix = classNameSuffix;
this.checkMethods = checkMethods;
this.verifyBytecode = verifyBytecode;
}

public static InstrumenterImpl create(Class<?> checkerClass, Map<MethodKey, CheckMethod> checkMethods) {
public static InstrumenterImpl create(Class<?> checkerClass, Map<MethodKey, CheckMethod> checkMethods, boolean verifyBytecode) {

Type checkerClassType = Type.getType(checkerClass);
String handleClass = checkerClassType.getInternalName() + "Handle";
String getCheckerClassMethodDescriptor = Type.getMethodDescriptor(checkerClassType);
return new InstrumenterImpl(handleClass, getCheckerClassMethodDescriptor, "", checkMethods);
return new InstrumenterImpl(handleClass, getCheckerClassMethodDescriptor, "", checkMethods, verifyBytecode);
}

static ClassFileInfo getClassFileInfo(Class<?> clazz) throws IOException {
Expand All @@ -82,13 +91,63 @@ static ClassFileInfo getClassFileInfo(Class<?> clazz) throws IOException {
return new ClassFileInfo(fileName, originalBytecodes);
}

private enum VerificationPhase {
BEFORE_INSTRUMENTATION,
AFTER_INSTRUMENTATION
}

private static String verify(byte[] classfileBuffer) {
ClassReader reader = new ClassReader(classfileBuffer);
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
CheckClassAdapter.verify(reader, false, printWriter);
return stringWriter.toString();
}

private static void verifyAndLog(byte[] classfileBuffer, String className, VerificationPhase phase) {
var failureLogLevel = (phase == VerificationPhase.BEFORE_INSTRUMENTATION ? Level.WARN : Level.ERROR);
try {
String result = verify(classfileBuffer);
if (result.isEmpty() == false) {
logger.log(
failureLogLevel,
Strings.format("Bytecode verification (%s) for class [%s] failed: %s", phase, className, result)
);
} else {
logger.info("Bytecode verification ({}) for class [{}] passed", phase, className);
}
} catch (Throwable e) {
// The ASM CheckClassAdapter in some cases throws instead of printing the error
logger.log(
failureLogLevel,
Strings.format(
"Bytecode verification (%s) for class [%s] failed: %s: %s",
phase,
className,
e.getClass().getName(),
e.getMessage()
)
);
}
}

@Override
public byte[] instrumentClass(String className, byte[] classfileBuffer) {
if (verifyBytecode) {
verifyAndLog(classfileBuffer, className, VerificationPhase.BEFORE_INSTRUMENTATION);
}

ClassReader reader = new ClassReader(classfileBuffer);
ClassWriter writer = new ClassWriter(reader, COMPUTE_FRAMES | COMPUTE_MAXS);
ClassVisitor visitor = new EntitlementClassVisitor(Opcodes.ASM9, writer, className);
reader.accept(visitor, 0);
return writer.toByteArray();
var outBytes = writer.toByteArray();

if (verifyBytecode) {
verifyAndLog(outBytes, className, VerificationPhase.AFTER_INSTRUMENTATION);
}

return outBytes;
}

class EntitlementClassVisitor extends ClassVisitor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ private static InstrumenterImpl createInstrumenter(Map<String, Executable> metho
String handleClass = Type.getInternalName(InstrumenterTests.TestEntitlementCheckerHolder.class);
String getCheckerClassMethodDescriptor = Type.getMethodDescriptor(Type.getObjectType(checkerClass));

return new InstrumenterImpl(handleClass, getCheckerClassMethodDescriptor, "", checkMethods);
return new InstrumenterImpl(handleClass, getCheckerClassMethodDescriptor, "", checkMethods, true);
}

private static TestLoader instrumentTestClass(InstrumenterImpl instrumenter) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ protected void before() throws Throwable {
.module("entitled", spec -> buildEntitlements(spec, "org.elasticsearch.entitlement.qa.entitled", ENTITLED_POLICY))
.module("entitlement-test-plugin", spec -> setupEntitlements(spec, modular, policyBuilder))
.systemProperty("es.entitlements.enabled", "true")
.systemProperty("es.entitlements.verify_bytecode", "true")
.systemProperty("es.entitlements.testdir", () -> testDir.getRoot().getAbsolutePath())
.setting("xpack.security.enabled", "false")
// Logs in libs/entitlement/qa/build/test-results/javaRestTest/TEST-org.elasticsearch.entitlement.qa.EntitlementsXXX.xml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

package org.elasticsearch.entitlement.initialization;

import org.elasticsearch.core.Booleans;
import org.elasticsearch.core.PathUtils;
import org.elasticsearch.core.internal.provider.ProviderLocator;
import org.elasticsearch.entitlement.bootstrap.EntitlementBootstrap;
Expand Down Expand Up @@ -100,6 +101,13 @@ public static void initialize(Instrumentation inst) throws Exception {
manager = initChecker();

var latestCheckerInterface = getVersionSpecificCheckerClass(EntitlementChecker.class);
var verifyBytecode = Booleans.parseBoolean(System.getProperty("es.entitlements.verify_bytecode", "false"));

if (verifyBytecode) {
// If bytecode verification is enabled, ensure these classes get loaded - for these classes, the order of initialization
// matters and gets changed by the verification process
ensureInitialized("sun.net.www.protocol.http.HttpURLConnection");
}

Map<MethodKey, CheckMethod> checkMethods = new HashMap<>(INSTRUMENTATION_SERVICE.lookupMethods(latestCheckerInterface));
Stream.of(
Expand All @@ -121,7 +129,7 @@ public static void initialize(Instrumentation inst) throws Exception {

var classesToTransform = checkMethods.keySet().stream().map(MethodKey::className).collect(Collectors.toSet());

Instrumenter instrumenter = INSTRUMENTATION_SERVICE.newInstrumenter(latestCheckerInterface, checkMethods);
Instrumenter instrumenter = INSTRUMENTATION_SERVICE.newInstrumenter(latestCheckerInterface, checkMethods, verifyBytecode);
inst.addTransformer(new Transformer(instrumenter, classesToTransform), true);
inst.retransformClasses(findClassesToRetransform(inst.getAllLoadedClasses(), classesToTransform));
}
Expand Down Expand Up @@ -420,6 +428,16 @@ private static Stream<InstrumentationService.InstrumentationInfo> pathChecks() {
});
}

private static void ensureInitialized(String... classNames) {
for (String className : classNames) {
try {
Class.forName(className);
} catch (ClassNotFoundException unexpected) {
throw new AssertionError(unexpected);
}
}
}

/**
* Returns the "most recent" checker class compatible with the current runtime Java version.
* For checkers, we have (optionally) version specific classes, each with a prefix (e.g. Java23).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface InstrumentationService {

record InstrumentationInfo(MethodKey targetMethod, CheckMethod checkMethod) {}

Instrumenter newInstrumenter(Class<?> clazz, Map<MethodKey, CheckMethod> methods);
Instrumenter newInstrumenter(Class<?> clazz, Map<MethodKey, CheckMethod> methods, boolean verifyBytecode);

Map<MethodKey, CheckMethod> lookupMethods(Class<?> clazz) throws ClassNotFoundException;

Expand Down