hello-stephen commented on code in PR #71291: URL: https://github.com/apache/doris/pull/71291#discussion_r4798040543
########## fe/be-java-extensions/jni-bootstrap/src/main/java/org/apache/doris/jni/bootstrap/DorisForkJoinWorkerThreadFactory.java: ########## @@ -0,0 +1,82 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.jni.bootstrap; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.ForkJoinWorkerThread; +import java.util.concurrent.ThreadLocalRandom; + +/** Keeps trusted scanner permissions and the JDK 17 common pool's thread-local cleanup. */ +@SuppressWarnings("removal") +public final class DorisForkJoinWorkerThreadFactory implements ForkJoinPool.ForkJoinWorkerThreadFactory { + private static final Field WORK_QUEUE; + private static final Field QUEUE_CONFIG; + private static final Method ERASE_THREAD_LOCALS; + private static final int INNOCUOUS; + + static { + try { + WORK_QUEUE = accessible(ForkJoinWorkerThread.class.getDeclaredField("workQueue")); Review Comment: [P2] Preserve common-pool startup under an existing SecurityManager Both BE launchers select this factory even in the default compatibility mode. A JVM policy that previously supported the BE can deny private JDK reflection to the bootstrap JAR. That causes an `ExceptionInInitializerError` while the JDK constructs the common pool. It escapes the factory fallback and breaks common-pool work process-wide, even before a downloaded UDF is loaded. Preserve the stock factory when the JVM already starts with an operator SecurityManager. [OpenJDK 17 common-pool initialization](https://github.com/openjdk/jdk17u/blob/jdk-17.0.2-ga/src/java.base/share/classes/java/util/concurrent/ForkJoinPool.java#L2460-L2487), [JDK 17 reflective construction](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/reflect/Constructor.html#newInstance(java.lang.Object...)) ########## be/src/util/jvm_launcher.cpp: ########## @@ -67,18 +68,37 @@ std::string join_options(const std::vector<std::string>& options) { return joined; } -// Every jar below `base_path`, in no particular order. +// Trusted jars below `base_path`, excluding user caches, in no particular order. void collect_jars(const std::string& base_path, std::string* class_path) { std::error_code ec; if (!std::filesystem::exists(base_path, ec)) { return; } - for (const auto& entry : std::filesystem::recursive_directory_iterator(base_path, ec)) { - if (entry.path().extension() == ".jar") { + auto user_cache = std::filesystem::absolute(config::user_function_dir).lexically_normal(); + // lib/moved-cache/ normalizes with an empty last component; directory entries do not. + // Drop it so a configured trailing separator cannot put cached user jars on the classpath. + if (user_cache.filename().empty()) { + user_cache = user_cache.parent_path(); + } + const auto legacy_user_cache = + std::filesystem::absolute(std::filesystem::path(env_value("DORIS_HOME")) / "lib" / + "udf") + .lexically_normal(); + const auto root = std::filesystem::absolute(base_path).lexically_normal(); + for (std::filesystem::recursive_directory_iterator it(root, ec), end; it != end; ++it) { + const auto path = it->path().lexically_normal(); + // UserFunctionLoaders owns these archives. A BE started without the shell script must + // not put cached user code on the trusted system classpath, including an old default + // cache left behind after user_function_dir was moved elsewhere. + if (path == user_cache || path == legacy_user_cache) { Review Comment: [P2] Exclude the UDF cache by directory identity A deployment can use a symlink for `DORIS_HOME` and a physical path in `user_function_dir` for a cache under its `lib` directory. `UserFunctionCache` follows that alias when opening its shards. The lexical comparison here sees two different paths and puts persisted function JARs on the native fallback system classpath. The recursive iterator also skips comparing the scan root itself. Compare directory identities, handle root overlap, and cover an aliased cache in `JvmLauncherOptionsTest`. ########## fe/be-java-extensions/java-udf/src/main/java/org/apache/doris/udf/UserFunctionClassLoader.java: ########## @@ -19,34 +19,62 @@ import java.net.URL; import java.net.URLClassLoader; +import java.security.AccessController; +import java.security.CodeSource; +import java.security.PermissionCollection; +import java.security.PrivilegedAction; +import java.util.Arrays; /** - * A user function's own jars, with {@link UdfContractClassLoader} behind them. + * Loads self-contained user jars, with {@link UdfContractClassLoader} providing shared API types. * - * <p>Ordinary {@link URLClassLoader} behaviour plus one thing: the class that could not be found - * anywhere is reported with what the search covered. This is the loader that has to say it - + * <p>User permissions come from the BE's local configuration. Lazy dependency resolution uses + * those permissions plus the loader's own bootstrap permissions, rather than the executor's full + * privileges. + * + * <p>A class that could not be found anywhere is reported with what the search covered. This is the + * loader that has to say it - * {@code ClassLoader.loadClass} discards whatever the parent threw and reports its own miss, so a * message attached further up never reaches anyone. */ +@SuppressWarnings("removal") final class UserFunctionClassLoader extends URLClassLoader { static { registerAsParallelCapable(); } - UserFunctionClassLoader(URL[] urls, ClassLoader contract) { + private final PermissionCollection userPermissions; + + private UserFunctionClassLoader(URL[] urls, ClassLoader contract, PermissionCollection userPermissions) { super(urls, contract); + this.userPermissions = userPermissions; + } + + static UserFunctionClassLoader create(URL[] urls, ClassLoader contract) { + UdfSecurity.initialize(); + PermissionCollection permissions = UdfSecurity.functionPermissions(urls); Review Comment: [P2] Preserve existing JVM policy grants in compatibility mode With the permission file absent or `allow_all_permissions=true`, `UdfSecurity.initialize()` keeps an existing JDK 17 SecurityManager and policy. This loader still gives downloaded classes `AllPermission` as part of their protection domain. JDK 17 combines loader-supplied grants with policy grants, so an existing policy that limits the archived code loses those restrictions on upgrade. Use the original loader permissions and creator context in compatibility mode. [OpenJDK protection domains](https://github.com/openjdk/jdk17u/blob/master/src/java.base/share/classes/java/security/ProtectionDomain.java#L278-L311), [OpenJDK policy grants](https://github.com/openjdk/jdk17u/blob/master/src/java.base/share/classes/sun/security/provider/PolicyFile.java#L989-L1007) ########## fe/be-java-extensions/jni-bootstrap/src/main/java/org/apache/doris/jni/bootstrap/TrustedClassLoaders.java: ########## @@ -0,0 +1,114 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.jni.bootstrap; + +import java.lang.ref.WeakReference; +import java.lang.reflect.Field; +import java.net.URLClassLoader; +import java.security.AccessControlContext; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.security.ProtectionDomain; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.function.Predicate; + +/** Bootstrap-owned trust identities, independent of plugin-private copies of the UDF policy. */ +@SuppressWarnings("removal") +public final class TrustedClassLoaders { + private static final RuntimePermission REGISTER = new RuntimePermission("doris.registerTrustedClassLoader"); + private static final ClassLoader SYSTEM = ClassLoader.getSystemClassLoader(); + private static final ClassLoader PLATFORM = SYSTEM.getParent(); + private static final List<WeakReference<ClassLoader>> PLUGINS = new CopyOnWriteArrayList<>(); + + private TrustedClassLoaders() { + } + + static void register(ClassLoader loader) { + // A UDF with createClassLoader/getClassLoader must not manufacture a trusted plugin loader. + SecurityManager manager = System.getSecurityManager(); + if (manager != null) { + manager.checkPermission(REGISTER); Review Comment: [P2] Preserve JNI plugin loading under existing JVM policies `PluginRuntime.loadDeployed()` constructs a `DorisPluginClassLoader` for each scanner, writer, and UDF plugin. Every construction now asks any installed SecurityManager for `doris.registerTrustedClassLoader`. An operator policy that already allows the BE's plugin operations can deny this new permission and cause `PluginRuntime` to cache a failed load, even when Java UDF resource restrictions are disabled. Scope the new registration requirement to the Doris-managed policy and cover bootstrap under an existing operator policy. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
