Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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 @@ -9,6 +9,7 @@

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;
Expand All @@ -24,9 +25,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 Down Expand Up @@ -63,6 +67,7 @@ public class InstrumenterImpl implements Instrumenter {
}

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

Type checkerClassType = Type.getType(checkerClass);
String handleClass = checkerClassType.getInternalName() + "Handle";
String getCheckerClassMethodDescriptor = Type.getMethodDescriptor(checkerClassType);
Expand All @@ -82,13 +87,60 @@ 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) {
try {
String result = verify(classfileBuffer);
if (result.isEmpty() == false) {
logger.error(Strings.format("Bytecode verification (%s) for class [%s] failed: %s", phase, className, result));
} else {
logger.info("Bytecode verification ({}) for class [{}] passed", phase, className);
}
} catch (ClassCircularityError e) {
// Apparently, verification during instrumentation is challenging for class resolution and loading
// Treat this not as an error, but as "inconclusive"
logger.warn(
"Cannot perform bytecode verification ({}) for class [{}]: {}: {}",
phase,
className,
e.getClass().getName(),
e.getMessage()
);
} catch (IllegalArgumentException e) {
// The ASM CheckClassAdapter in some cases throws this instead of printing the error
logger.error(Strings.format("Bytecode verification (%s) for class [%s] failed", phase, className), e);
}
}

@Override
public byte[] instrumentClass(String className, byte[] classfileBuffer) {
public byte[] instrumentClass(String className, byte[] classfileBuffer, boolean verify) {
if (verify) {
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 (verify) {
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 @@ -227,9 +227,9 @@ public void testNotInstrumentedTwice() throws Exception {
var instrumenter = createInstrumenter(Map.of("checkSomeStaticMethod", targetMethod));

var loader1 = instrumentTestClass(instrumenter);
byte[] instrumentedTwiceBytecode = instrumenter.instrumentClass(TestClassToInstrument.class.getName(), loader1.testClassBytes);
logger.trace(() -> Strings.format("Bytecode after 2nd instrumentation:\n%s", bytecode2text(instrumentedTwiceBytecode)));
var loader2 = new TestLoader(TestClassToInstrument.class.getName(), instrumentedTwiceBytecode);
byte[] instrumentedTwiceBytes = instrumenter.instrumentClass(TestClassToInstrument.class.getName(), loader1.testClassBytes, true);
logger.trace(() -> Strings.format("Bytecode after 2nd instrumentation:\n%s", bytecode2text(instrumentedTwiceBytes)));
var loader2 = new TestLoader(TestClassToInstrument.class.getName(), instrumentedTwiceBytes);

assertStaticMethodThrows(loader2, targetMethod, 123);
assertEquals(1, TestEntitlementCheckerHolder.checkerInstance.checkSomeStaticMethodIntCallCount);
Expand Down Expand Up @@ -307,7 +307,7 @@ private static InstrumenterImpl createInstrumenter(Map<String, Executable> metho
private static TestLoader instrumentTestClass(InstrumenterImpl instrumenter) throws IOException {
var clazz = TestClassToInstrument.class;
ClassFileInfo initial = getClassFileInfo(clazz);
byte[] newBytecode = instrumenter.instrumentClass(Type.getInternalName(clazz), initial.bytecodes());
byte[] newBytecode = instrumenter.instrumentClass(Type.getInternalName(clazz), initial.bytecodes(), true);
if (logger.isTraceEnabled()) {
logger.trace("Bytecode after instrumentation:\n{}", bytecode2text(newBytecode));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ protected void before() throws Throwable {
.module("entitled", spec -> buildEntitlements(spec, "org.elasticsearch.entitlement.qa.entitled", ENTITLED_POLICY))
.module(ENTITLEMENT_TEST_PLUGIN_NAME, spec -> setupEntitlements(spec, modular, policyBuilder))
.systemProperty("es.entitlements.enabled", "true")
.systemProperty("es.entitlements.verify_bytecode", "true")
.systemProperty("es.entitlements.testdir", () -> testDir.getRoot().getAbsolutePath())
.systemProperties(spec -> tempDirSystemPropertyProvider.get(testDir.getRoot().toPath()))
.setting("xpack.security.enabled", "false")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,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
ensureClassesSensitiveToVerificationAreInitialized();
}

Map<MethodKey, CheckMethod> checkMethods = new HashMap<>(INSTRUMENTATION_SERVICE.lookupMethods(latestCheckerInterface));
Stream.of(
Expand All @@ -123,8 +130,24 @@ 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);
inst.addTransformer(new Transformer(instrumenter, classesToTransform), true);
inst.retransformClasses(findClassesToRetransform(inst.getAllLoadedClasses(), classesToTransform));
var transformer = new Transformer(instrumenter, classesToTransform, verifyBytecode);
inst.addTransformer(transformer, true);

var classesToRetransform = findClassesToRetransform(inst.getAllLoadedClasses(), classesToTransform);
try {
inst.retransformClasses(classesToRetransform);
} catch (VerifyError e) {
// Turn on verification and try to retransform one class at the time to get detailed diagnostic
ensureClassesSensitiveToVerificationAreInitialized();
transformer.enableClassVerification();

for (var classToRetransform : classesToRetransform) {
inst.retransformClasses(classToRetransform);
}

// We should have failed already in the loop above, but just in case we did not, rethrow.
throw e;
}
}

private static Class<?>[] findClassesToRetransform(Class<?>[] loadedClasses, Set<String> classesToTransform) {
Expand Down Expand Up @@ -424,6 +447,17 @@ private static Stream<InstrumentationService.InstrumentationInfo> pathChecks() {
});
}

private static void ensureClassesSensitiveToVerificationAreInitialized() {
var classesToInitialize = Set.of("sun.net.www.protocol.http.HttpURLConnection");
for (String className : classesToInitialize) {
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 @@ -10,5 +10,5 @@
package org.elasticsearch.entitlement.instrumentation;

public interface Instrumenter {
byte[] instrumentClass(String className, byte[] classfileBuffer);
byte[] instrumentClass(String className, byte[] classfileBuffer, boolean verify);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@ public class Transformer implements ClassFileTransformer {
private final Instrumenter instrumenter;
private final Set<String> classesToTransform;

public Transformer(Instrumenter instrumenter, Set<String> classesToTransform) {
private boolean verifyClasses;

public Transformer(Instrumenter instrumenter, Set<String> classesToTransform, boolean verifyClasses) {
this.instrumenter = instrumenter;
this.classesToTransform = classesToTransform;
this.verifyClasses = verifyClasses;
// TODO: Should warn if any MethodKey doesn't match any methods
}

public void enableClassVerification() {
this.verifyClasses = true;
}

@Override
public byte[] transform(
ClassLoader loader,
Expand All @@ -36,7 +43,7 @@ public byte[] transform(
) {
if (classesToTransform.contains(className)) {
// System.out.println("Transforming " + className);
return instrumenter.instrumentClass(className, classfileBuffer);
return instrumenter.instrumentClass(className, classfileBuffer, verifyClasses);
} else {
// System.out.println("Not transforming " + className);
return classfileBuffer;
Expand Down