CVE-2023-42809: Deserialization of Untrusted Data (CWE-502)
value: Redisson is a Java Redis client that uses the Netty framework. Prior to version 3.22.0, some of the messages received from the Redis server contain Java objects that the client deserializes without further validation. Attackers that manage to trick clients into communicating with a malicious server can include especially crafted objects in its responses that, once deserialized by the client, force it to execute arbitrary code. This can be abused to take control of the machine the client is running in. Version 3.22.0 contains a patch for this issue.

Some post-fix advice is available. Do NOT use `Kryo5Codec` as deserialization codec, as it is still vulnerable to arbitrary object deserialization due to the `setRegistrationRequired(false)` call. On the contrary, `KryoCodec` is safe to use. The fix applied to `SerializationCodec` only consists of adding an optional allowlist of class names, even though making this behavior the default is recommended. When instantiating `SerializationCodec` please use the `SerializationCodec(ClassLoader classLoader, Set<String> allowedClasses)` constructor to restrict the allowed classes for deserialization.
CWE description: The product deserializes untrusted data without sufficiently verifying that the resulting data will be valid.
CVSS v3 base score: 8.8
Published: 2023-10-04T20:15Z
Repository: https://github.com/redisson/redisson
Language: Java
Files changed: redisson/src/main/java/org/redisson/codec/CustomObjectInputStream.java, redisson/src/main/java/org/redisson/codec/SerializationCodec.java
Commit date: 2023-06-01 10:08:05 +0300
Commit message: Feature - allowedClasses setting added to SerializationCodec https://github.com/redisson/redisson/security/code-scanning/4

Diff: diff --git a/redisson/src/main/java/org/redisson/codec/CustomObjectInputStream.java b/redisson/src/main/java/org/redisson/codec/CustomObjectInputStream.java
index 827627968..14e093adf 100644
--- a/redisson/src/main/java/org/redisson/codec/CustomObjectInputStream.java
+++ b/redisson/src/main/java/org/redisson/codec/CustomObjectInputStream.java
@@ -13,36 +13,44 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.redisson.codec;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.ObjectInputStream;
-import java.io.ObjectStreamClass;
+import java.io.*;
 import java.lang.reflect.Proxy;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Set;
 
 /**
  * 
  * @author Nikita Koksharov
  *
  */
 public class CustomObjectInputStream extends ObjectInputStream {
 
     private final ClassLoader classLoader;
-    
+    private Set<String> allowedClasses;
+
+    public CustomObjectInputStream(ClassLoader classLoader, InputStream in,Set<String> allowedClasses) throws IOException {
+        super(in);
+        this.classLoader = classLoader;
+        this.allowedClasses = allowedClasses;
+    }
+
     public CustomObjectInputStream(ClassLoader classLoader, InputStream in) throws IOException {
         super(in);
         this.classLoader = classLoader;
     }
     
     @Override
     protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
         try {
             String name = desc.getName();
+            if (allowedClasses != null && !allowedClasses.contains(name)) {
+                throw new InvalidClassException("Class " + name + " isn't allowed");
+            }
             return Class.forName(name, false, classLoader);
         } catch (ClassNotFoundException e) {
             return super.resolveClass(desc);
         }
     }
@@ -54,9 +62,9 @@ public class CustomObjectInputStream extends ObjectInputStream {
         for (String name : interfaces) {
             Class<?> clazz = Class.forName(name, false, classLoader);
             loadedClasses.add(clazz);
         }
         
-        return Proxy.getProxyClass(classLoader, loadedClasses.toArray(new Class[loadedClasses.size()]));
+        return Proxy.getProxyClass(classLoader, loadedClasses.toArray(new Class[0]));
     }
     
 }
diff --git a/redisson/src/main/java/org/redisson/codec/SerializationCodec.java b/redisson/src/main/java/org/redisson/codec/SerializationCodec.java
index 62374a4bf..35b1da431 100644
--- a/redisson/src/main/java/org/redisson/codec/SerializationCodec.java
+++ b/redisson/src/main/java/org/redisson/codec/SerializationCodec.java
@@ -13,23 +13,23 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.redisson.codec;
 
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufAllocator;
+import io.netty.buffer.ByteBufInputStream;
+import io.netty.buffer.ByteBufOutputStream;
 import org.redisson.client.codec.BaseCodec;
 import org.redisson.client.handler.State;
 import org.redisson.client.protocol.Decoder;
 import org.redisson.client.protocol.Encoder;
 
-import io.netty.buffer.ByteBuf;
-import io.netty.buffer.ByteBufAllocator;
-import io.netty.buffer.ByteBufInputStream;
-import io.netty.buffer.ByteBufOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.Set;
 
 /**
  * JDK's serialization codec.
  * <p>
  * Fully thread-safe.*
@@ -49,11 +49,11 @@ public class SerializationCodec extends BaseCodec {
                 try {
                     ByteBufInputStream in = new ByteBufInputStream(buf);
                     ObjectInputStream inputStream;
                     if (classLoader != null) {
                         Thread.currentThread().setContextClassLoader(classLoader);
-                        inputStream = new CustomObjectInputStream(classLoader, in);
+                        inputStream = new CustomObjectInputStream(classLoader, in, allowedClasses);
                     } else {
                         inputStream = new ObjectInputStream(in);
                     }
                     return inputStream.readObject();
                 } finally {
@@ -82,11 +82,12 @@ public class SerializationCodec extends BaseCodec {
                 out.release();
                 throw e;
             }
         }
     };
-    
+
+    private Set<String> allowedClasses;
     private final ClassLoader classLoader;
 
     public SerializationCodec() {
         this(null);
     }
@@ -95,10 +96,16 @@ public class SerializationCodec extends BaseCodec {
         this.classLoader = classLoader;
     }
 
     public SerializationCodec(ClassLoader classLoader, SerializationCodec codec) {
         this.classLoader = classLoader;
+        this.allowedClasses = codec.allowedClasses;
+    }
+
+    public SerializationCodec(ClassLoader classLoader, Set<String> allowedClasses) {
+        this.classLoader = classLoader;
+        this.allowedClasses = allowedClasses;
     }
     
     @Override
     public Decoder<Object> getValueDecoder() {
         return decoder;

Vulnerable code: import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;
    
        return Proxy.getProxyClass(classLoader, loadedClasses.toArray(new Class[loadedClasses.size()]));
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
                        inputStream = new CustomObjectInputStream(classLoader, in);
    
Fixed code: import java.io.*;
import java.util.Set;
    private Set<String> allowedClasses;

    public CustomObjectInputStream(ClassLoader classLoader, InputStream in,Set<String> allowedClasses) throws IOException {
        super(in);
        this.classLoader = classLoader;
        this.allowedClasses = allowedClasses;
    }

            if (allowedClasses != null && !allowedClasses.contains(name)) {
                throw new InvalidClassException("Class " + name + " isn't allowed");
            }
        return Proxy.getProxyClass(classLoader, loadedClasses.toArray(new Class[0]));
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Set;
                        inputStream = new CustomObjectInputStream(classLoader, in, allowedClasses);

    private Set<String> allowedClasses;
        this.allowedClasses = codec.allowedClasses;
    }

    public SerializationCodec(ClassLoader classLoader, Set<String> allowedClasses) {
        this.classLoader = classLoader;
        this.allowedClasses = allowedClasses;