|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | + |
| 26 | +package com.sun.naming.internal; |
| 27 | + |
| 28 | +import sun.security.util.SecurityProperties; |
| 29 | + |
| 30 | +import javax.naming.Reference; |
| 31 | +import java.io.ObjectInputFilter; |
| 32 | +import java.io.ObjectInputFilter.FilterInfo; |
| 33 | +import java.io.ObjectInputFilter.Status; |
| 34 | + |
| 35 | +/** |
| 36 | + * This class implements the filter that validates object factories classes instantiated |
| 37 | + * during {@link Reference} lookups. |
| 38 | + * There is one system-wide filter instance per VM that can be set via |
| 39 | + * the {@code "jdk.jndi.object.factoriesFilter"} system property value, or via |
| 40 | + * setting the property in the security properties file. The system property value supersedes |
| 41 | + * the security property value. If none of the properties are specified the default |
| 42 | + * "*" value is used. |
| 43 | + * The filter is implemented as {@link ObjectInputFilter} with capabilities limited to the |
| 44 | + * validation of a factory's class types only ({@linkplain FilterInfo#serialClass()}). |
| 45 | + * Array length, number of object references, depth, and stream size filtering capabilities are |
| 46 | + * not supported by the filter. |
| 47 | + */ |
| 48 | +public final class ObjectFactoriesFilter { |
| 49 | + |
| 50 | + /** |
| 51 | + * Checks if serial filter configured with {@code "jdk.jndi.object.factoriesFilter"} |
| 52 | + * system property value allows instantiation of the specified objects factory class. |
| 53 | + * If the filter result is not {@linkplain Status#REJECTED REJECTED}, the filter will |
| 54 | + * allow the instantiation of objects factory class. |
| 55 | + * |
| 56 | + * @param factoryClass objects factory class |
| 57 | + * @return true - if the factory is allowed to be instantiated; false - otherwise |
| 58 | + */ |
| 59 | + public static boolean canInstantiateObjectsFactory(Class<?> factoryClass) { |
| 60 | + return checkInput(() -> factoryClass); |
| 61 | + } |
| 62 | + |
| 63 | + private static boolean checkInput(FactoryInfo factoryInfo) { |
| 64 | + Status result = GLOBAL.checkInput(factoryInfo); |
| 65 | + return result != Status.REJECTED; |
| 66 | + } |
| 67 | + |
| 68 | + // FilterInfo to check if objects factory class is allowed by the system-wide |
| 69 | + // filter. Array length, number of object references, depth, and stream size |
| 70 | + // capabilities are ignored. |
| 71 | + @FunctionalInterface |
| 72 | + private interface FactoryInfo extends FilterInfo { |
| 73 | + @Override |
| 74 | + default long arrayLength() { |
| 75 | + return -1; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + default long depth() { |
| 80 | + return 1; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + default long references() { |
| 85 | + return 0; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + default long streamBytes() { |
| 90 | + return 0; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // Prevent instantiation of the factories filter class |
| 95 | + private ObjectFactoriesFilter() { |
| 96 | + throw new InternalError("Not instantiable"); |
| 97 | + } |
| 98 | + |
| 99 | + // System property name that contains the patterns to filter object factory names |
| 100 | + private static final String FACTORIES_FILTER_PROPNAME = "jdk.jndi.object.factoriesFilter"; |
| 101 | + |
| 102 | + // Default system property value that allows the load of any object factory classes |
| 103 | + private static final String DEFAULT_SP_VALUE = "*"; |
| 104 | + |
| 105 | + // System wide object factories filter constructed from the system property |
| 106 | + private static final ObjectInputFilter GLOBAL = |
| 107 | + ObjectInputFilter.Config.createFilter(getFilterPropertyValue()); |
| 108 | + |
| 109 | + // Get security or system property value |
| 110 | + private static String getFilterPropertyValue() { |
| 111 | + String propVal = SecurityProperties.privilegedGetOverridable(FACTORIES_FILTER_PROPNAME); |
| 112 | + return propVal != null ? propVal : DEFAULT_SP_VALUE; |
| 113 | + } |
| 114 | +} |
0 commit comments