|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package com.amazon.corretto.hotpatch.patch.impl.log4j2; |
| 17 | + |
| 18 | +import com.amazon.corretto.hotpatch.org.objectweb.asm.ClassReader; |
| 19 | +import com.amazon.corretto.hotpatch.org.objectweb.asm.ClassVisitor; |
| 20 | +import com.amazon.corretto.hotpatch.org.objectweb.asm.ClassWriter; |
| 21 | +import com.amazon.corretto.hotpatch.org.objectweb.asm.Label; |
| 22 | +import com.amazon.corretto.hotpatch.org.objectweb.asm.MethodVisitor; |
| 23 | +import com.amazon.corretto.hotpatch.org.objectweb.asm.Opcodes; |
| 24 | +import com.amazon.corretto.hotpatch.patch.ClassTransformerHotPatch; |
| 25 | + |
| 26 | +public class Log4j2DisableLiteralPatternConverter implements ClassTransformerHotPatch { |
| 27 | + static final String CLASS_NAME = "org.apache.logging.log4j.core.pattern.LiteralPatternConverter"; |
| 28 | + static final String CLASS_NAME_SLASH = CLASS_NAME.replace(".", "/"); |
| 29 | + |
| 30 | + private final static String NAME = "Log4j2_DisableLiteralPatternConverter"; |
| 31 | + |
| 32 | + @Override |
| 33 | + public String getName() { |
| 34 | + return NAME; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public String getDescription() { |
| 39 | + return "Fixes CVE-2021-45105 by patching the LiteralPatternConverter to disable lookup in message patterns."; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public boolean isTargetClass(String className) { |
| 44 | + return className.endsWith(CLASS_NAME) || className.endsWith(CLASS_NAME_SLASH); |
| 45 | + } |
| 46 | + |
| 47 | + public static boolean isEnabled(String args) { |
| 48 | + String param = "--enable-" + NAME; |
| 49 | + return args != null && args.contains(param); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public byte[] apply(int asmApiVersion, String className, byte[] classfileBuffer) { |
| 54 | + ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); |
| 55 | + ClassVisitor cv = new DisableLiteralPatternConverterClassVisitor(asmApiVersion, cw); |
| 56 | + ClassReader cr = new ClassReader(classfileBuffer); |
| 57 | + cr.accept(cv, 0); |
| 58 | + return cw.toByteArray(); |
| 59 | + } |
| 60 | + |
| 61 | + public static class DisableLiteralPatternConverterClassVisitor extends ClassVisitor { |
| 62 | + |
| 63 | + public DisableLiteralPatternConverterClassVisitor(int asmApiVersion, ClassVisitor classVisitor) { |
| 64 | + super(asmApiVersion, classVisitor); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { |
| 69 | + MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions); |
| 70 | + if ("format".equals(name)) { |
| 71 | + mv = new LiteralPatternConverterMethodVisitor(api, mv); |
| 72 | + } |
| 73 | + return mv; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public static class LiteralPatternConverterMethodVisitor extends MethodVisitor implements Opcodes { |
| 78 | + private static final String OWNER = CLASS_NAME_SLASH; |
| 79 | + private static final String DESC = "Z"; |
| 80 | + private static final String NAME = "substitute"; |
| 81 | + enum State { |
| 82 | + CLEAR, |
| 83 | + LOADED_SUBSTITUTE, |
| 84 | + } |
| 85 | + |
| 86 | + private State state = State.CLEAR; |
| 87 | + |
| 88 | + public LiteralPatternConverterMethodVisitor(int asmApiVersion, MethodVisitor methodVisitor) { |
| 89 | + super(asmApiVersion, methodVisitor); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void visitFieldInsn(int opc, String owner, String name, String desc) { |
| 94 | + if (OWNER.equals(owner) && NAME.equals(name) && DESC.equals(desc) && opc == GETFIELD) { |
| 95 | + visitState(); |
| 96 | + } else { |
| 97 | + clearState(); |
| 98 | + } |
| 99 | + mv.visitFieldInsn(opc, owner, name, desc); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void visitJumpInsn(int opc, Label label) { |
| 104 | + mv.visitJumpInsn(opc, label); |
| 105 | + if (state == State.LOADED_SUBSTITUTE && opc == IFEQ) { |
| 106 | + mv.visitJumpInsn(GOTO, label); |
| 107 | + } |
| 108 | + clearState(); |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | + private void clearState() { |
| 113 | + state = State.CLEAR; |
| 114 | + } |
| 115 | + |
| 116 | + private void visitState() { |
| 117 | + state = State.LOADED_SUBSTITUTE; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void visitVarInsn(int opcode, int var) { |
| 122 | + clearState(); |
| 123 | + mv.visitVarInsn(opcode, var); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void visitTypeInsn(int opcode, String desc) { |
| 128 | + clearState(); |
| 129 | + mv.visitTypeInsn(opcode, desc); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public void visitMethodInsn(int opcode, String owner, String name, String desc) { |
| 134 | + clearState(); |
| 135 | + mv.visitMethodInsn(opcode, owner, name, desc); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public void visitLabel(Label label) { |
| 140 | + mv.visitLabel(label); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public void visitLdcInsn(Object cst) { |
| 145 | + clearState(); |
| 146 | + mv.visitLdcInsn(cst); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public void visitIincInsn(int var, int increment) { |
| 151 | + clearState(); |
| 152 | + mv.visitIincInsn(var, increment); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public void visitTableSwitchInsn(int min, int max, Label dflt, Label[] labels) { |
| 157 | + mv.visitTableSwitchInsn(min, max, dflt, labels); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) { |
| 162 | + mv.visitLookupSwitchInsn(dflt, keys, labels); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public void visitMultiANewArrayInsn(String desc, int dims) { |
| 167 | + mv.visitMultiANewArrayInsn(desc, dims); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public void visitTryCatchBlock(Label start, Label end, Label handler, String type) { |
| 172 | + mv.visitTryCatchBlock(start, end, handler, type); |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) { |
| 177 | + mv.visitLocalVariable(name, desc, signature, start, end, index); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public void visitLineNumber(int line, Label start) { |
| 182 | + mv.visitLineNumber(line, start); |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments