2a466c
From 3a4e355796891149adfd9228633f179015293dbd Mon Sep 17 00:00:00 2001
2a466c
From: Richard Atkins <rjatkins359@gmail.com>
2a466c
Date: Wed, 21 Sep 2022 23:18:58 +1000
2a466c
Subject: [PATCH] CVE-2022-42920
2a466c
2a466c
---
2a466c
 .../org/apache/bcel/classfile/ConstantPool.java   | 15 +++++++++++----
2a466c
 .../org/apache/bcel/generic/ConstantPoolGen.java  | 11 ++++++++++-
2a466c
 2 files changed, 21 insertions(+), 5 deletions(-)
2a466c
2a466c
diff --git a/src/main/java/org/apache/bcel/classfile/ConstantPool.java b/src/main/java/org/apache/bcel/classfile/ConstantPool.java
2a466c
index f2c946a1..77ab0da4 100644
2a466c
--- a/src/main/java/org/apache/bcel/classfile/ConstantPool.java
2a466c
+++ b/src/main/java/org/apache/bcel/classfile/ConstantPool.java
2a466c
@@ -218,10 +218,17 @@ public class ConstantPool implements Cloneable, Node {
2a466c
      * @throws IOException
2a466c
      */
2a466c
     public void dump( final DataOutputStream file ) throws IOException {
2a466c
-        file.writeShort(constant_pool.length);
2a466c
-        for (int i = 1; i < constant_pool.length; i++) {
2a466c
-            if (constant_pool[i] != null) {
2a466c
-                constant_pool[i].dump(file);
2a466c
+        /*
2a466c
+         * Constants over the size of the constant pool shall not be written out.
2a466c
+         * This is a redundant measure as the ConstantPoolGen should have already
2a466c
+         * reported an error back in the situation.
2a466c
+        */
2a466c
+        final int size = Math.min(constant_pool.length, Const.MAX_CP_ENTRIES);
2a466c
+
2a466c
+        file.writeShort(size);
2a466c
+        for (int i = 1; i < size; i++) {
2a466c
+            if (constant_pool[i] != null) {
2a466c
+                constant_pool[i].dump(file);
2a466c
             }
2a466c
         }
2a466c
     }
2a466c
diff --git a/src/main/java/org/apache/bcel/generic/ConstantPoolGen.java b/src/main/java/org/apache/bcel/generic/ConstantPoolGen.java
2a466c
index fd0af47e..d3189ba4 100644
2a466c
--- a/src/main/java/org/apache/bcel/generic/ConstantPoolGen.java
2a466c
+++ b/src/main/java/org/apache/bcel/generic/ConstantPoolGen.java
2a466c
@@ -95,7 +95,7 @@ public class ConstantPoolGen {
2a466c
     public ConstantPoolGen(final Constant[] cs) {
2a466c
         final StringBuilder sb = new StringBuilder(DEFAULT_BUFFER_SIZE);
2a466c
 
2a466c
-        size = Math.max(DEFAULT_BUFFER_SIZE, cs.length + 64);
2a466c
+        size = Math.min(Math.max(DEFAULT_BUFFER_SIZE, cs.length + 64), Const.MAX_CP_ENTRIES + 1);
2a466c
         constants = new Constant[size];
2a466c
 
2a466c
         System.arraycopy(cs, 0, constants, 0, cs.length);
2a466c
@@ -224,9 +224,18 @@ public class ConstantPoolGen {
2a466c
     /** Resize internal array of constants.
2a466c
      */
2a466c
     protected void adjustSize() {
2a466c
+        // 3 extra spaces are needed as some entries may take 3 slots
2a466c
+        if (index + 3 >= Const.MAX_CP_ENTRIES + 1) {
2a466c
+            throw new IllegalStateException("The number of constants " + (index + 3)
2a466c
+                    + " is over the size of the constant pool: "
2a466c
+                    + Const.MAX_CP_ENTRIES);
2a466c
+        }
2a466c
+
2a466c
         if (index + 3 >= size) {
2a466c
             final Constant[] cs = constants;
2a466c
             size *= 2;
2a466c
+            // the constant array shall not exceed the size of the constant pool
2a466c
+            size = Math.min(size, Const.MAX_CP_ENTRIES + 1);
2a466c
             constants = new Constant[size];
2a466c
             System.arraycopy(cs, 0, constants, 0, index);
2a466c
         }
2a466c
-- 
2a466c
2.38.1
2a466c