Blame SOURCES/0001-Backport-fix-for-CVE-2017-5645.patch

cb5964
From ea4609eca531916ac347686c048bebdb7b4b6e0d Mon Sep 17 00:00:00 2001
cb5964
From: Michael Simacek <msimacek@redhat.com>
cb5964
Date: Fri, 2 Jun 2017 14:37:35 +0200
cb5964
Subject: [PATCH] Backport fix for CVE-2017-5645
cb5964
cb5964
---
cb5964
 .../apache/log4j/FilteredObjectInputStream.java    | 65 ++++++++++++++++++++++
cb5964
 src/main/java/org/apache/log4j/net/SocketNode.java | 17 +++++-
cb5964
 2 files changed, 80 insertions(+), 2 deletions(-)
cb5964
 create mode 100644 src/main/java/org/apache/log4j/FilteredObjectInputStream.java
cb5964
cb5964
diff --git a/src/main/java/org/apache/log4j/FilteredObjectInputStream.java b/src/main/java/org/apache/log4j/FilteredObjectInputStream.java
cb5964
new file mode 100644
cb5964
index 0000000..b9ef20c
cb5964
--- /dev/null
cb5964
+++ b/src/main/java/org/apache/log4j/FilteredObjectInputStream.java
cb5964
@@ -0,0 +1,65 @@
cb5964
+/*
cb5964
+ * Licensed to the Apache Software Foundation (ASF) under one or more
cb5964
+ * contributor license agreements. See the NOTICE file distributed with
cb5964
+ * this work for additional information regarding copyright ownership.
cb5964
+ * The ASF licenses this file to You under the Apache license, Version 2.0
cb5964
+ * (the "License"); you may not use this file except in compliance with
cb5964
+ * the License. You may obtain a copy of the License at
cb5964
+ *
cb5964
+ *      http://www.apache.org/licenses/LICENSE-2.0
cb5964
+ *
cb5964
+ * Unless required by applicable law or agreed to in writing, software
cb5964
+ * distributed under the License is distributed on an "AS IS" BASIS,
cb5964
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
cb5964
+ * See the license for the specific language governing permissions and
cb5964
+ * limitations under the license.
cb5964
+ */
cb5964
+package org.apache.log4j;
cb5964
+
cb5964
+import java.io.FileOutputStream;
cb5964
+import java.io.IOException;
cb5964
+import java.io.InputStream;
cb5964
+import java.io.InvalidObjectException;
cb5964
+import java.io.ObjectInputStream;
cb5964
+import java.io.ObjectStreamClass;
cb5964
+import java.util.Arrays;
cb5964
+import java.util.Collection;
cb5964
+import java.util.List;
cb5964
+
cb5964
+/**
cb5964
+ * Extended ObjectInputStream that only allows certain classes to be deserialized.
cb5964
+ *
cb5964
+ * Backported from 2.8.2
cb5964
+ */
cb5964
+public class FilteredObjectInputStream extends ObjectInputStream {
cb5964
+
cb5964
+    private static final List REQUIRED_JAVA_CLASSES = Arrays.asList(new String[] {
cb5964
+        // Types of non-trainsient fields of LoggingEvent
cb5964
+        "java.lang.String",
cb5964
+        "java.util.Hashtable",
cb5964
+        // ThrowableInformation
cb5964
+        "[Ljava.lang.String;"
cb5964
+    });
cb5964
+
cb5964
+    private final Collection allowedClasses;
cb5964
+
cb5964
+    public FilteredObjectInputStream(final InputStream in, final Collection allowedClasses) throws IOException {
cb5964
+        super(in);
cb5964
+        this.allowedClasses = allowedClasses;
cb5964
+    }
cb5964
+
cb5964
+    protected Class resolveClass(final ObjectStreamClass desc) throws IOException, ClassNotFoundException {
cb5964
+        String name = desc.getName();
cb5964
+        if (!(isAllowedByDefault(name) || allowedClasses.contains(name))) {
cb5964
+            throw new InvalidObjectException("Class is not allowed for deserialization: " + name);
cb5964
+        }
cb5964
+        return super.resolveClass(desc);
cb5964
+    }
cb5964
+
cb5964
+    private static boolean isAllowedByDefault(final String name) {
cb5964
+        return name.startsWith("org.apache.log4j.") ||
cb5964
+            name.startsWith("[Lorg.apache.log4j.") ||
cb5964
+            REQUIRED_JAVA_CLASSES.contains(name);
cb5964
+    }
cb5964
+
cb5964
+}
cb5964
diff --git a/src/main/java/org/apache/log4j/net/SocketNode.java b/src/main/java/org/apache/log4j/net/SocketNode.java
cb5964
index e977f13..f95bb10 100644
cb5964
--- a/src/main/java/org/apache/log4j/net/SocketNode.java
cb5964
+++ b/src/main/java/org/apache/log4j/net/SocketNode.java
cb5964
@@ -22,6 +22,10 @@ import java.io.IOException;
cb5964
 import java.io.InterruptedIOException;
cb5964
 import java.io.ObjectInputStream;
cb5964
 import java.net.Socket;
cb5964
+import java.util.ArrayList;
cb5964
+import java.util.Arrays;
cb5964
+import java.util.Collection;
cb5964
+import org.apache.log4j.FilteredObjectInputStream;
cb5964
 
cb5964
 import org.apache.log4j.Logger;
cb5964
 import org.apache.log4j.spi.LoggerRepository;
cb5964
@@ -53,8 +57,9 @@ public class SocketNode implements Runnable {
cb5964
     this.socket = socket;
cb5964
     this.hierarchy = hierarchy;
cb5964
     try {
cb5964
-      ois = new ObjectInputStream(
cb5964
-                         new BufferedInputStream(socket.getInputStream()));
cb5964
+      ois = new FilteredObjectInputStream(
cb5964
+                         new BufferedInputStream(socket.getInputStream()),
cb5964
+                         getAllowedClasses());
cb5964
     } catch(InterruptedIOException e) {
cb5964
       Thread.currentThread().interrupt();
cb5964
       logger.error("Could not open ObjectInputStream to "+socket, e);
cb5964
@@ -65,6 +70,14 @@ public class SocketNode implements Runnable {
cb5964
     }
cb5964
   }
cb5964
 
cb5964
+  private Collection getAllowedClasses() {
cb5964
+      Collection allowedClasses = new ArrayList();
cb5964
+      String property = System.getProperty("org.apache.log4j.net.allowedClasses");
cb5964
+      if (property != null)
cb5964
+          allowedClasses.addAll(Arrays.asList(property.split(",")));
cb5964
+      return allowedClasses;
cb5964
+  }
cb5964
+
cb5964
   //public
cb5964
   //void finalize() {
cb5964
   //System.err.println("-------------------------Finalize called");
cb5964
-- 
cb5964
2.9.4
cb5964