Skip to content

Commit adf66be

Browse files
authored
Patch Shell class in hdfs to not execute (#119189) (#119243)
Shell utility in hdfs tries to execute a local script statically to determine whether setsid is available. With the security manager this doesn't work, but hdfs catches the SecurityException and assumes false. With entitlements this doesn't work since hdfs does not know about our NotEntitledException. This commit reworks the patching of hdfs-client-api to use asm. It then adds patching of hdfs' Shell class to replace the method that tries to execute.
1 parent 027f96a commit adf66be

File tree

8 files changed

+231
-58
lines changed

8 files changed

+231
-58
lines changed

build-tools-internal/src/main/groovy/elasticsearch.ide.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ if (providers.systemProperty('idea.active').getOrNull() == 'true') {
132132
tasks.register('buildDependencyArtifacts') {
133133
group = 'ide'
134134
description = 'Builds artifacts needed as dependency for IDE modules'
135-
dependsOn([':plugins:repository-hdfs:hadoop-client-api:shadowJar',
135+
dependsOn([':plugins:repository-hdfs:hadoop-client-api:jar',
136136
':x-pack:plugin:esql:compute:ann:jar',
137137
':x-pack:plugin:esql:compute:gen:jar',
138138
':server:generateModulesList',

plugins/repository-hdfs/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ configurations {
2929
}
3030

3131
dependencies {
32-
api project(path: 'hadoop-client-api', configuration: 'shadow')
32+
api project(path: 'hadoop-client-api', configuration: 'default')
3333
if (isEclipse) {
3434
/*
3535
* Eclipse can't pick up the shadow dependency so we point it at *something*
Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,46 @@
1-
apply plugin: 'elasticsearch.build'
2-
apply plugin: 'com.gradleup.shadow'
1+
apply plugin: 'elasticsearch.java'
2+
3+
sourceSets {
4+
patcher
5+
}
6+
7+
configurations {
8+
thejar {
9+
canBeResolved = true
10+
}
11+
}
312

413
dependencies {
5-
implementation "org.apache.hadoop:hadoop-client-api:${project.parent.versions.hadoop}"
14+
thejar("org.apache.hadoop:hadoop-client-api:${project.parent.versions.hadoop}") {
15+
transitive = false
16+
}
17+
18+
patcherImplementation 'org.ow2.asm:asm:9.7.1'
19+
patcherImplementation 'org.ow2.asm:asm-tree:9.7.1'
620
}
721

8-
tasks.named('shadowJar').configure {
9-
exclude 'org/apache/hadoop/util/ShutdownHookManager$*.class'
22+
def outputDir = layout.buildDirectory.dir("patched-classes")
23+
24+
def patchTask = tasks.register("patchClasses", JavaExec) {
25+
inputs.files(configurations.thejar).withPathSensitivity(PathSensitivity.RELATIVE)
26+
inputs.files(sourceSets.patcher.output).withPathSensitivity(PathSensitivity.RELATIVE)
27+
outputs.dir(outputDir)
28+
classpath = sourceSets.patcher.runtimeClasspath
29+
mainClass = 'org.elasticsearch.hdfs.patch.HdfsClassPatcher'
30+
doFirst {
31+
args(configurations.thejar.singleFile, outputDir.get().asFile)
32+
}
1033
}
1134

12-
['jarHell', 'thirdPartyAudit', 'forbiddenApisMain', 'splitPackagesAudit'].each {
13-
tasks.named(it).configure {
14-
enabled = false
35+
tasks.named('jar').configure {
36+
dependsOn(configurations.thejar)
37+
38+
from(patchTask)
39+
from({ project.zipTree(configurations.thejar.singleFile) }) {
40+
eachFile {
41+
if (outputDir.get().file(it.relativePath.pathString).asFile.exists()) {
42+
it.exclude()
43+
}
44+
}
1545
}
1646
}

plugins/repository-hdfs/hadoop-client-api/src/main/java/org/apache/hadoop/util/ShutdownHookManager.java

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.hdfs.patch;
11+
12+
import org.objectweb.asm.ClassReader;
13+
import org.objectweb.asm.ClassVisitor;
14+
import org.objectweb.asm.ClassWriter;
15+
16+
import java.io.File;
17+
import java.nio.file.Files;
18+
import java.nio.file.Path;
19+
import java.nio.file.Paths;
20+
import java.util.Map;
21+
import java.util.function.Function;
22+
import java.util.jar.JarEntry;
23+
import java.util.jar.JarFile;
24+
25+
public class HdfsClassPatcher {
26+
static final Map<String, Function<ClassWriter, ClassVisitor>> patchers = Map.of(
27+
"org/apache/hadoop/util/ShutdownHookManager.class",
28+
ShutdownHookManagerPatcher::new,
29+
"org/apache/hadoop/util/Shell.class",
30+
ShellPatcher::new
31+
);
32+
33+
public static void main(String[] args) throws Exception {
34+
String jarPath = args[0];
35+
Path outputDir = Paths.get(args[1]);
36+
37+
try (JarFile jarFile = new JarFile(new File(jarPath))) {
38+
for (var patcher : patchers.entrySet()) {
39+
JarEntry jarEntry = jarFile.getJarEntry(patcher.getKey());
40+
if (jarEntry == null) {
41+
throw new IllegalArgumentException("path [" + patcher.getKey() + "] not found in [" + jarPath + "]");
42+
}
43+
byte[] classToPatch = jarFile.getInputStream(jarEntry).readAllBytes();
44+
45+
ClassReader classReader = new ClassReader(classToPatch);
46+
ClassWriter classWriter = new ClassWriter(classReader, 0);
47+
classReader.accept(patcher.getValue().apply(classWriter), 0);
48+
49+
Path outputFile = outputDir.resolve(patcher.getKey());
50+
Files.createDirectories(outputFile.getParent());
51+
Files.write(outputFile, classWriter.toByteArray());
52+
}
53+
}
54+
}
55+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.hdfs.patch;
11+
12+
import org.objectweb.asm.MethodVisitor;
13+
import org.objectweb.asm.Opcodes;
14+
15+
public class MethodReplacement extends MethodVisitor {
16+
private final MethodVisitor delegate;
17+
private final Runnable bodyWriter;
18+
19+
MethodReplacement(MethodVisitor delegate, Runnable bodyWriter) {
20+
super(Opcodes.ASM9);
21+
this.delegate = delegate;
22+
this.bodyWriter = bodyWriter;
23+
}
24+
25+
@Override
26+
public void visitCode() {
27+
// delegate.visitCode();
28+
bodyWriter.run();
29+
// delegate.visitEnd();
30+
}
31+
32+
@Override
33+
public void visitMaxs(int maxStack, int maxLocals) {
34+
delegate.visitMaxs(maxStack, maxLocals);
35+
}
36+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.hdfs.patch;
11+
12+
import org.objectweb.asm.ClassVisitor;
13+
import org.objectweb.asm.ClassWriter;
14+
import org.objectweb.asm.MethodVisitor;
15+
import org.objectweb.asm.Opcodes;
16+
17+
class ShellPatcher extends ClassVisitor {
18+
19+
ShellPatcher(ClassWriter classWriter) {
20+
super(Opcodes.ASM9, classWriter);
21+
}
22+
23+
@Override
24+
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
25+
MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions);
26+
if (name.equals("isSetsidSupported")) {
27+
return new MethodReplacement(mv, () -> {
28+
mv.visitInsn(Opcodes.ICONST_0);
29+
mv.visitInsn(Opcodes.IRETURN);
30+
});
31+
}
32+
return mv;
33+
}
34+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.hdfs.patch;
11+
12+
import org.objectweb.asm.ClassVisitor;
13+
import org.objectweb.asm.ClassWriter;
14+
import org.objectweb.asm.MethodVisitor;
15+
import org.objectweb.asm.Opcodes;
16+
import org.objectweb.asm.Type;
17+
18+
import java.util.Set;
19+
import java.util.concurrent.ExecutorService;
20+
import java.util.concurrent.TimeUnit;
21+
22+
class ShutdownHookManagerPatcher extends ClassVisitor {
23+
private static final String CLASSNAME = "org/apache/hadoop/util/ShutdownHookManager";
24+
private static final Set<String> VOID_METHODS = Set.of("addShutdownHook", "clearShutdownHooks");
25+
private static final Set<String> BOOLEAN_METHODS = Set.of("removeShutdownHook", "hasShutdownHook", "isShutdownInProgress");
26+
27+
ShutdownHookManagerPatcher(ClassWriter classWriter) {
28+
super(Opcodes.ASM9, classWriter);
29+
}
30+
31+
@Override
32+
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
33+
MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions);
34+
if (VOID_METHODS.contains(name)) {
35+
// make void methods noops
36+
return new MethodReplacement(mv, () -> { mv.visitInsn(Opcodes.RETURN); });
37+
} else if (BOOLEAN_METHODS.contains(name)) {
38+
// make boolean methods always return false
39+
return new MethodReplacement(mv, () -> {
40+
mv.visitInsn(Opcodes.ICONST_0);
41+
mv.visitInsn(Opcodes.IRETURN);
42+
});
43+
} else if (name.equals("<clinit>")) {
44+
return new MethodReplacement(mv, () -> {
45+
// just initialize the statics, don't actually get runtime to add shutdown hook
46+
47+
var classType = Type.getObjectType(CLASSNAME);
48+
mv.visitTypeInsn(Opcodes.NEW, CLASSNAME);
49+
mv.visitInsn(Opcodes.DUP);
50+
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, CLASSNAME, "<init>", "()V", false);
51+
mv.visitFieldInsn(Opcodes.PUTSTATIC, CLASSNAME, "MGR", classType.getDescriptor());
52+
53+
var timeUnitType = Type.getType(TimeUnit.class);
54+
mv.visitFieldInsn(Opcodes.GETSTATIC, timeUnitType.getInternalName(), "SECONDS", timeUnitType.getDescriptor());
55+
mv.visitFieldInsn(Opcodes.PUTSTATIC, CLASSNAME, "TIME_UNIT_DEFAULT", timeUnitType.getDescriptor());
56+
57+
var executorServiceType = Type.getType(ExecutorService.class);
58+
mv.visitInsn(Opcodes.ACONST_NULL);
59+
mv.visitFieldInsn(Opcodes.PUTSTATIC, CLASSNAME, "EXECUTOR", executorServiceType.getDescriptor());
60+
61+
mv.visitInsn(Opcodes.RETURN);
62+
});
63+
}
64+
return mv;
65+
}
66+
}

0 commit comments

Comments
 (0)