This is an automated email from the ASF dual-hosted git repository. markt-asf pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit c7e7fffe0d8bce35f5789bba08254689fe8cbfae Author: Mark Thomas <[email protected]> AuthorDate: Mon Nov 30 15:28:13 2026 +0000 Align BCEL fork with upstream using Claude / Sonnet 5.5 --- .../tomcat/util/bcel/classfile/AnnotationEntry.java | 15 ++++++++++++++- .../tomcat/util/bcel/classfile/ClassFormatException.java | 2 +- .../apache/tomcat/util/bcel/classfile/ConstantPool.java | 4 ++-- .../apache/tomcat/util/bcel/classfile/ElementValue.java | 14 +++++++++++--- .../tomcat/util/bcel/classfile/ElementValuePair.java | 4 ++-- 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/java/org/apache/tomcat/util/bcel/classfile/AnnotationEntry.java b/java/org/apache/tomcat/util/bcel/classfile/AnnotationEntry.java index a8368948e3..9e7a8b9355 100644 --- a/java/org/apache/tomcat/util/bcel/classfile/AnnotationEntry.java +++ b/java/org/apache/tomcat/util/bcel/classfile/AnnotationEntry.java @@ -42,6 +42,19 @@ public class AnnotationEntry { * @throws IOException */ AnnotationEntry(final DataInput input, final ConstantPool constantPool) throws IOException { + this(input, constantPool, 0); + } + + /* + * Creates an AnnotationEntry from a DataInputStream, carrying the nesting depth of the enclosing element values + * so that ElementValue.readElementValue() can bound the combined annotation/array nesting depth. + * + * @param input + * @param constantPool + * @param nesting + * @throws IOException + */ + AnnotationEntry(final DataInput input, final ConstantPool constantPool, final int nesting) throws IOException { this.constantPool = constantPool; @@ -50,7 +63,7 @@ public class AnnotationEntry { elementValuePairs = new ArrayList<>(numElementValuePairs); for (int i = 0; i < numElementValuePairs; i++) { - elementValuePairs.add(new ElementValuePair(input, constantPool)); + elementValuePairs.add(new ElementValuePair(input, constantPool, nesting)); } } diff --git a/java/org/apache/tomcat/util/bcel/classfile/ClassFormatException.java b/java/org/apache/tomcat/util/bcel/classfile/ClassFormatException.java index 39c15d75d0..155d655c0c 100644 --- a/java/org/apache/tomcat/util/bcel/classfile/ClassFormatException.java +++ b/java/org/apache/tomcat/util/bcel/classfile/ClassFormatException.java @@ -21,7 +21,7 @@ package org.apache.tomcat.util.bcel.classfile; */ public class ClassFormatException extends RuntimeException { - private static final long serialVersionUID = 3243149520175287759L; + private static final long serialVersionUID = -3569097343160139969L; /** * Constructs a new instance with {@code null} as its detail message. The cause is not initialized, and may subsequently be initialized by a call to diff --git a/java/org/apache/tomcat/util/bcel/classfile/ConstantPool.java b/java/org/apache/tomcat/util/bcel/classfile/ConstantPool.java index eb670579ea..9f91d69539 100644 --- a/java/org/apache/tomcat/util/bcel/classfile/ConstantPool.java +++ b/java/org/apache/tomcat/util/bcel/classfile/ConstantPool.java @@ -110,8 +110,8 @@ public class ConstantPool { throw new ClassFormatException("Invalid constant pool reference using index: %,d. Constant pool size is: %,d", Integer.valueOf(index), Integer.valueOf(constantPool.length)); } if (constantPool[index] != null && !castTo.isAssignableFrom(constantPool[index].getClass())) { - throw new ClassFormatException("Invalid constant pool reference at index: " + index + - ". Expected " + castTo + " but was " + constantPool[index].getClass()); + throw new ClassFormatException("Invalid constant pool reference at index: %,d. Expected %s but was %s", Integer.valueOf(index), castTo, + constantPool[index].getClass()); } if (index > 1) { final Constant prev = constantPool[index - 1]; diff --git a/java/org/apache/tomcat/util/bcel/classfile/ElementValue.java b/java/org/apache/tomcat/util/bcel/classfile/ElementValue.java index a704a24112..479f1dfc8d 100644 --- a/java/org/apache/tomcat/util/bcel/classfile/ElementValue.java +++ b/java/org/apache/tomcat/util/bcel/classfile/ElementValue.java @@ -136,8 +136,16 @@ public abstract class ElementValue { case CLASS: return new ClassElementValue(CLASS, input.readUnsignedShort(), cpool); case ANNOTATION: - // TODO isRuntimeVisible - return new AnnotationElementValue(ANNOTATION, new AnnotationEntry(input, cpool), cpool); + arrayNesting++; + if (arrayNesting > Const.MAX_ARRAY_DIMENSIONS) { + // Annotation element values may legitimately nest (annotations whose members are annotations or + // arrays thereof), but a malicious class file can alternate annotation and array nesting to + // recurse without limit. Count both kinds of nesting against the same JVM spec 4.4.1 bound so + // the depth cannot be reset by wrapping an array in an annotation (CWE-674). + throw new ClassFormatException("Annotation element values are only valid if they nest %,d or fewer levels.", + Integer.valueOf(Const.MAX_ARRAY_DIMENSIONS)); + } + return new AnnotationElementValue(ANNOTATION, new AnnotationEntry(input, cpool, arrayNesting), cpool); case ARRAY: arrayNesting++; if (arrayNesting > Const.MAX_ARRAY_DIMENSIONS) { @@ -152,7 +160,7 @@ public abstract class ElementValue { } return new ArrayElementValue(ARRAY, evalues, cpool); default: - throw new ClassFormatException("Unexpected element value kind in annotation: " + tag); + throw new ClassFormatException("Unexpected element value tag in annotation: " + tag); } } diff --git a/java/org/apache/tomcat/util/bcel/classfile/ElementValuePair.java b/java/org/apache/tomcat/util/bcel/classfile/ElementValuePair.java index b3bcf40edb..f3ceda571b 100644 --- a/java/org/apache/tomcat/util/bcel/classfile/ElementValuePair.java +++ b/java/org/apache/tomcat/util/bcel/classfile/ElementValuePair.java @@ -33,10 +33,10 @@ public class ElementValuePair { private final int elementNameIndex; - ElementValuePair(final DataInput file, final ConstantPool constantPool) throws IOException { + ElementValuePair(final DataInput file, final ConstantPool constantPool, final int nesting) throws IOException { this.constantPool = constantPool; this.elementNameIndex = file.readUnsignedShort(); - this.elementValue = ElementValue.readElementValue(file, constantPool); + this.elementValue = ElementValue.readElementValue(file, constantPool, nesting); } /** --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
