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

coheigea pushed a commit to branch coheigea/corba
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit b96eca914ddaa1055b33640c9b187072ad4315c8
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Tue Nov 17 08:16:06 2026 +0000

    Check CORBA sequence lengths before allocating from them
---
 .../binding/corba/runtime/CorbaObjectReader.java   |  70 ++++++++++++--
 .../corba/runtime/CorbaObjectReaderTest.java       | 104 ++++++++++++++++++++-
 2 files changed, 167 insertions(+), 7 deletions(-)

diff --git 
a/rt/bindings/corba/src/main/java/org/apache/cxf/binding/corba/runtime/CorbaObjectReader.java
 
b/rt/bindings/corba/src/main/java/org/apache/cxf/binding/corba/runtime/CorbaObjectReader.java
index 131e16ca4d8..c461c02c696 100644
--- 
a/rt/bindings/corba/src/main/java/org/apache/cxf/binding/corba/runtime/CorbaObjectReader.java
+++ 
b/rt/bindings/corba/src/main/java/org/apache/cxf/binding/corba/runtime/CorbaObjectReader.java
@@ -19,6 +19,7 @@
 package org.apache.cxf.binding.corba.runtime;
 
 
+import java.io.ByteArrayOutputStream;
 import java.lang.reflect.Array;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
@@ -55,11 +56,13 @@ import org.omg.CORBA.Any;
 import org.omg.CORBA.NO_IMPLEMENT;
 import org.omg.CORBA.TCKind;
 import org.omg.CORBA.TypeCode;
+import org.omg.CORBA.TypeCodePackage.BadKind;
 import org.omg.CORBA.portable.InputStream;
 
 public class CorbaObjectReader {
 
     private static final Logger LOG = 
LogUtils.getL7dLogger(CorbaObjectReader.class);
+    private static final int OCTET_CHUNK_SIZE = 64 * 1024;
 
     private InputStream stream;
 
@@ -514,15 +517,14 @@ public class CorbaObjectReader {
 
     public void readSequence(CorbaObjectHandler obj) throws 
CorbaBindingException {
         if (obj instanceof CorbaOctetSequenceHandler) {
-            int length = stream.read_ulong();
-            byte[] value = new byte[length];
-            stream.read_octet_array(value, 0, length);
-            ((CorbaOctetSequenceHandler) obj).setValue(value);
+            int length = readSequenceLength(obj);
+            ((CorbaOctetSequenceHandler) obj).setValue(readOctets(length));
         } else {
             CorbaSequenceHandler sequenceObj = (CorbaSequenceHandler)obj;
             List<CorbaObjectHandler> seqElements = sequenceObj.getElements();
-            int length = stream.read_ulong();
-            List<CorbaObjectHandler> elements = new ArrayList<>(length);
+            int length = readSequenceLength(obj);
+            // don't size the list from the length in the message, it grows as 
the elements are read
+            List<CorbaObjectHandler> elements = new ArrayList<>();
 
             // Simply checking the bound won't handle our recursive types.  We 
need to check for the
             // existance of template, which will be present for all unbounded 
sequences and for bound
@@ -544,6 +546,10 @@ public class CorbaObjectReader {
                 sequenceObj.setElements(elements);
             } else {
                 // We have a bounded sequence and the object should already be 
pre-built
+                if (length > seqElements.size()) {
+                    throw new CorbaBindingException("Sequence length " + length
+                        + " exceeds the number of elements " + 
seqElements.size());
+                }
                 for (int i = 0; i < length; ++i) {
                     read(seqElements.get(i));
                 }
@@ -552,6 +558,58 @@ public class CorbaObjectReader {
         }
     }
 
+    /**
+     * Read a sequence length from the stream. It is controlled by the sender, 
so check that it is
+     * non-negative and within the bound of the sequence type before it is 
used.
+     */
+    private int readSequenceLength(CorbaObjectHandler obj) {
+        int length = stream.read_ulong();
+        if (length < 0) {
+            // an unsigned long larger than Integer.MAX_VALUE
+            throw new CorbaBindingException("Sequence length " + 
Integer.toUnsignedLong(length)
+                + " is too large");
+        }
+        int bound = getSequenceBound(obj.getTypeCode());
+        if (bound > 0 && length > bound) {
+            throw new CorbaBindingException("Sequence length " + length + " 
exceeds the bound " + bound);
+        }
+        return length;
+    }
+
+    private static int getSequenceBound(TypeCode tc) {
+        try {
+            while (tc != null && tc.kind().value() == TCKind._tk_alias) {
+                tc = tc.content_type();
+            }
+            return tc == null ? 0 : tc.length();
+        } catch (BadKind ex) {
+            // not a sequence type code, so there is no bound
+            return 0;
+        }
+    }
+
+    /**
+     * Read a sequence of octets. A large sequence is read in chunks, so that 
memory is only allocated as
+     * the data actually arrives, rather than up front for whatever length the 
sender declared.
+     */
+    private byte[] readOctets(int length) {
+        if (length <= OCTET_CHUNK_SIZE) {
+            byte[] value = new byte[length];
+            stream.read_octet_array(value, 0, length);
+            return value;
+        }
+        ByteArrayOutputStream value = new 
ByteArrayOutputStream(OCTET_CHUNK_SIZE);
+        byte[] chunk = new byte[OCTET_CHUNK_SIZE];
+        int remaining = length;
+        while (remaining > 0) {
+            int size = Math.min(remaining, chunk.length);
+            stream.read_octet_array(chunk, 0, size);
+            value.write(chunk, 0, size);
+            remaining -= size;
+        }
+        return value.toByteArray();
+    }
+
     public void readObjectReference(CorbaObjectReferenceHandler objRefObj) 
throws CorbaBindingException {
         org.omg.CORBA.Object objRef = stream.read_Object();
         objRefObj.setReference(objRef);
diff --git 
a/rt/bindings/corba/src/test/java/org/apache/cxf/binding/corba/runtime/CorbaObjectReaderTest.java
 
b/rt/bindings/corba/src/test/java/org/apache/cxf/binding/corba/runtime/CorbaObjectReaderTest.java
index 902fda9c667..be42ecf7bc4 100644
--- 
a/rt/bindings/corba/src/test/java/org/apache/cxf/binding/corba/runtime/CorbaObjectReaderTest.java
+++ 
b/rt/bindings/corba/src/test/java/org/apache/cxf/binding/corba/runtime/CorbaObjectReaderTest.java
@@ -25,12 +25,14 @@ import java.util.List;
 
 import javax.xml.namespace.QName;
 
+import org.apache.cxf.binding.corba.CorbaBindingException;
 import org.apache.cxf.binding.corba.types.CorbaArrayHandler;
 import org.apache.cxf.binding.corba.types.CorbaEnumHandler;
 import org.apache.cxf.binding.corba.types.CorbaExceptionHandler;
 import org.apache.cxf.binding.corba.types.CorbaFixedHandler;
 import org.apache.cxf.binding.corba.types.CorbaObjectHandler;
 import org.apache.cxf.binding.corba.types.CorbaObjectReferenceHandler;
+import org.apache.cxf.binding.corba.types.CorbaOctetSequenceHandler;
 import org.apache.cxf.binding.corba.types.CorbaPrimitiveHandler;
 import org.apache.cxf.binding.corba.types.CorbaSequenceHandler;
 import org.apache.cxf.binding.corba.types.CorbaStructHandler;
@@ -55,8 +57,10 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 
 public class CorbaObjectReaderTest {
@@ -548,4 +552,102 @@ public class CorbaObjectReaderTest {
         reader.readObjectReference(obj);
         assertTrue(obj.getReference()._is_equivalent(objRef));
     }
-}
\ No newline at end of file
+
+    @Test
+    public void testReadOctetSequence() {
+        // larger than the chunk size, so it is read in chunks
+        byte[] data = new byte[200000];
+        for (int i = 0; i < data.length; i++) {
+            data[i] = (byte)i;
+        }
+        OutputStream oStream = orb.create_output_stream();
+        oStream.write_ulong(data.length);
+        oStream.write_octet_array(data, 0, data.length);
+
+        CorbaOctetSequenceHandler obj = createOctetSequenceHandler(0);
+        new CorbaObjectReader(oStream.create_input_stream()).readSequence(obj);
+        assertArrayEquals(data, obj.getValue());
+    }
+
+    @Test
+    public void testReadOctetSequenceLengthLargerThanData() {
+        // the length in the message must not be allocated up front, reading 
just fails when the data runs out
+        OutputStream oStream = orb.create_output_stream();
+        oStream.write_ulong(Integer.MAX_VALUE);
+        oStream.write_octet_array(new byte[16], 0, 16);
+
+        CorbaObjectReader reader = new 
CorbaObjectReader(oStream.create_input_stream());
+        assertThrows(RuntimeException.class, () -> 
reader.readSequence(createOctetSequenceHandler(0)));
+    }
+
+    @Test
+    public void testReadSequenceLengthTooLarge() {
+        // 0xFFFFFFFF is larger than any Java array
+        OutputStream oStream = orb.create_output_stream();
+        oStream.write_ulong(-1);
+
+        CorbaObjectReader reader = new 
CorbaObjectReader(oStream.create_input_stream());
+        assertThrows(CorbaBindingException.class, () -> 
reader.readSequence(createOctetSequenceHandler(0)));
+    }
+
+    @Test
+    public void testReadSequenceExceedsBound() {
+        OutputStream oStream = orb.create_output_stream();
+        oStream.write_ulong(5);
+        oStream.write_octet_array(new byte[5], 0, 5);
+        CorbaObjectReader reader = new 
CorbaObjectReader(oStream.create_input_stream());
+        assertThrows(CorbaBindingException.class, () -> 
reader.readSequence(createOctetSequenceHandler(4)));
+
+        // a bounded sequence of strings, with more elements in the message 
than the bound
+        QName stringIdlType = new QName(CorbaConstants.NU_WSDL_CORBA, 
"string", CorbaConstants.NP_WSDL_CORBA);
+        QName seqIdlType = new QName(CorbaConstants.NU_WSDL_CORBA, "sequence", 
CorbaConstants.NP_WSDL_CORBA);
+        Sequence seqType = new Sequence();
+        seqType.setBound(2);
+        seqType.setElemtype(stringIdlType);
+        TypeCode seqTC = orb.create_sequence_tc(2, 
orb.get_primitive_tc(TCKind.tk_string));
+        CorbaSequenceHandler obj = new CorbaSequenceHandler(new QName("Seq"), 
seqIdlType, seqTC, seqType);
+        for (int i = 0; i < 2; ++i) {
+            obj.addElement(new CorbaPrimitiveHandler(new QName("item"), 
stringIdlType,
+                                                     
orb.get_primitive_tc(TCKind.tk_string), null));
+        }
+        oStream = orb.create_output_stream();
+        oStream.write_ulong(3);
+        for (int i = 0; i < 3; ++i) {
+            oStream.write_string("s" + i);
+        }
+        CorbaObjectReader stringReader = new 
CorbaObjectReader(oStream.create_input_stream());
+        assertThrows(CorbaBindingException.class, () -> 
stringReader.readSequence(obj));
+    }
+
+    @Test
+    public void testReadUnboundedSequenceLengthLargerThanData() {
+        // the list of elements must not be sized from the length in the 
message
+        QName longIdlType = new QName(CorbaConstants.NU_WSDL_CORBA, "long", 
CorbaConstants.NP_WSDL_CORBA);
+        QName seqIdlType = new QName(CorbaConstants.NU_WSDL_CORBA, "sequence", 
CorbaConstants.NP_WSDL_CORBA);
+        Sequence seqType = new Sequence();
+        seqType.setBound(0);
+        seqType.setElemtype(longIdlType);
+        TypeCode longTC = orb.get_primitive_tc(TCKind.tk_long);
+        CorbaSequenceHandler obj = new CorbaSequenceHandler(new QName("Seq"), 
seqIdlType,
+                                                            
orb.create_sequence_tc(0, longTC), seqType);
+        obj.setTemplateElement(new CorbaPrimitiveHandler(new QName("item"), 
longIdlType, longTC, null));
+
+        OutputStream oStream = orb.create_output_stream();
+        oStream.write_ulong(Integer.MAX_VALUE);
+        oStream.write_long(1);
+        oStream.write_long(2);
+        CorbaObjectReader reader = new 
CorbaObjectReader(oStream.create_input_stream());
+        assertThrows(RuntimeException.class, () -> reader.readSequence(obj));
+    }
+
+    private CorbaOctetSequenceHandler createOctetSequenceHandler(int bound) {
+        QName octetIdlType = new QName(CorbaConstants.NU_WSDL_CORBA, "octet", 
CorbaConstants.NP_WSDL_CORBA);
+        QName seqIdlType = new QName(CorbaConstants.NU_WSDL_CORBA, "sequence", 
CorbaConstants.NP_WSDL_CORBA);
+        Sequence seqType = new Sequence();
+        seqType.setBound(bound);
+        seqType.setElemtype(octetIdlType);
+        seqType.setType(new QName("http://www.w3.org/2001/XMLSchema";, 
"base64Binary"));
+        TypeCode seqTC = orb.create_sequence_tc(bound, 
orb.get_primitive_tc(TCKind.tk_octet));
+        return new CorbaOctetSequenceHandler(new QName("Seq"), seqIdlType, 
seqTC, seqType);
+    }
+}

Reply via email to