This is an automated email from the ASF dual-hosted git repository.

markt-asf pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/9.0.x by this push:
     new d86c432d24 Align BCEL fork with upstream using Claude / Sonnet 5.5
d86c432d24 is described below

commit d86c432d245a35b93e88c31d33aeb93d4df717a8
Author: Mark Thomas <[email protected]>
AuthorDate: Mon Nov 30 15:28:13 2026 +0000

    Align BCEL fork with upstream using Claude / Sonnet 5.5
---
 .../util/bcel/classfile/AnnotationEntry.java       | 15 +++-
 .../util/bcel/classfile/ClassFormatException.java  |  2 +-
 .../tomcat/util/bcel/classfile/ConstantPool.java   |  4 +-
 .../tomcat/util/bcel/classfile/ElementValue.java   | 79 +++++++++++-----------
 .../util/bcel/classfile/ElementValuePair.java      |  4 +-
 5 files changed, 60 insertions(+), 44 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 639686facb..479f1dfc8d 100644
--- a/java/org/apache/tomcat/util/bcel/classfile/ElementValue.java
+++ b/java/org/apache/tomcat/util/bcel/classfile/ElementValue.java
@@ -120,44 +120,47 @@ public abstract class ElementValue {
             throws IOException {
         final byte tag = input.readByte();
         switch (tag) {
-        case PRIMITIVE_BYTE:
-        case PRIMITIVE_CHAR:
-        case PRIMITIVE_DOUBLE:
-        case PRIMITIVE_FLOAT:
-        case PRIMITIVE_INT:
-        case PRIMITIVE_LONG:
-        case PRIMITIVE_SHORT:
-        case PRIMITIVE_BOOLEAN:
-        case STRING:
-            return new SimpleElementValue(tag, input.readUnsignedShort(), 
cpool);
-
-        case ENUM_CONSTANT:
-            input.readUnsignedShort();    // Unused type_index
-            return new EnumElementValue(ENUM_CONSTANT, 
input.readUnsignedShort(), cpool);
-
-        case CLASS:
-            return new ClassElementValue(CLASS, input.readUnsignedShort(), 
cpool);
-
-        case ANNOTATION:
-            // TODO isRuntimeVisible
-            return new AnnotationElementValue(ANNOTATION, new 
AnnotationEntry(input, cpool), cpool);
-
-        case ARRAY:
-            arrayNesting++;
-            if (arrayNesting > Const.MAX_ARRAY_DIMENSIONS) {
-                // JVM spec 4.4.1
-                throw new ClassFormatException(String.format("Arrays are only 
valid if they represent %,d or fewer dimensions.",
-                        Integer.valueOf(Const.MAX_ARRAY_DIMENSIONS)));
-            }
-            final int numArrayVals = input.readUnsignedShort();
-            final ElementValue[] evalues = new ElementValue[numArrayVals];
-            for (int j = 0; j < numArrayVals; j++) {
-                evalues[j] = readElementValue(input, cpool, arrayNesting);
-            }
-            return new ArrayElementValue(ARRAY, evalues, cpool);
-
-        default:
-            throw new ClassFormatException("Unexpected element value kind in 
annotation: " + tag);
+            case PRIMITIVE_BYTE:
+            case PRIMITIVE_CHAR:
+            case PRIMITIVE_DOUBLE:
+            case PRIMITIVE_FLOAT:
+            case PRIMITIVE_INT:
+            case PRIMITIVE_LONG:
+            case PRIMITIVE_SHORT:
+            case PRIMITIVE_BOOLEAN:
+            case STRING:
+                return new SimpleElementValue(tag, input.readUnsignedShort(), 
cpool);
+            case ENUM_CONSTANT:
+                input.readUnsignedShort();    // Unused type_index
+                return new EnumElementValue(ENUM_CONSTANT, 
input.readUnsignedShort(), cpool);
+            case CLASS:
+                return new ClassElementValue(CLASS, input.readUnsignedShort(), 
cpool);
+            case ANNOTATION:
+                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) {
+                    // JVM spec 4.4.1
+                    throw new ClassFormatException(String.format("Arrays are 
only valid if they represent %,d or fewer dimensions.",
+                            Integer.valueOf(Const.MAX_ARRAY_DIMENSIONS)));
+                }
+                final int numArrayVals = input.readUnsignedShort();
+                final ElementValue[] evalues = new ElementValue[numArrayVals];
+                for (int j = 0; j < numArrayVals; j++) {
+                    evalues[j] = readElementValue(input, cpool, arrayNesting);
+                }
+                return new ArrayElementValue(ARRAY, evalues, cpool);
+            default:
+                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]

Reply via email to