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

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