hello-stephen commented on code in PR #71291: URL: https://github.com/apache/doris/pull/71291#discussion_r4768251939
########## fe/be-java-extensions/java-udf/src/main/java/org/apache/doris/udf/UdfPermissionConfig.java: ########## @@ -0,0 +1,300 @@ +// 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.udf; + +import java.io.FilePermission; +import java.io.IOException; +import java.io.Reader; +import java.net.NetPermission; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.AllPermission; +import java.security.Permission; +import java.security.PermissionCollection; +import java.security.Permissions; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.List; +import java.util.Properties; +import java.util.PropertyPermission; +import java.util.Set; + +/** Fixed operator allow settings, parsed once into a read-only permission collection. */ +final class UdfPermissionConfig { + private static final String FILE_NAME = "java_udf_security.properties"; + private final PermissionCollection permissions; + + private UdfPermissionConfig(PermissionCollection permissions) { + this.permissions = permissions; + } + + static UdfPermissionConfig loadConfigured() { + String home = System.getenv("DORIS_HOME"); + if (home == null || home.isEmpty()) { + throw new IllegalArgumentException("Set DORIS_HOME to locate the Java UDF permission file"); + } + // start_be.sh sets DORIS_HOME to the deployed BE root, e.g. output/be, not output. + return load(Paths.get(home, "conf", FILE_NAME)); + } + + static UdfPermissionConfig load(Path path) { + Properties properties = new Properties(); + try (Reader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) { Review Comment: [P1] Preserve Java UDF loading for upgrades that keep conf/ An in-place upgrade that keeps the existing `conf/` directory (the case already documented at `bin/start_be.sh:639-646`) does not have `java_udf_security.properties`. Every Java UDF now reaches this unconditional open through `UserFunctionClassLoader.create` → `UdfSecurity.install`, so existing scalar functions and UDAFs fail with a missing-file error after upgrading, despite the intended `AllPermission` default. Adding the file to the release package does not migrate an existing `conf/`. Please preserve the compatibility default when the new file is absent, or install it through the upgrade path before UDF loading. ########## fe/be-java-extensions/java-udf/src/main/java/org/apache/doris/udf/UdfSecurity.java: ########## @@ -0,0 +1,181 @@ +// 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.udf; + +import org.apache.doris.jni.spi.DorisForkJoinWorkerThreadFactory; + +import java.io.File; +import java.io.FilePermission; +import java.net.URISyntaxException; +import java.net.URL; +import java.security.AccessControlContext; +import java.security.AllPermission; +import java.security.Permission; +import java.security.PermissionCollection; +import java.security.Permissions; +import java.security.Policy; +import java.security.ProtectionDomain; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Enumeration; +import java.util.List; +import java.util.concurrent.ForkJoinPool; + +/** + * JDK 17 resource permissions for CREATE FUNCTION's Java code, not for the Java plugin itself. + * + * <p>The security manager is JVM-wide. Only domains tagged by UserFunctionClassLoader are restricted; + * the executor, JNI SPI and scanner plugins retain their existing process privileges. Standard stack + * inspection also checks an inherited AccessControlContext when user code starts another thread. + * No thread-local flag or temporary installation is involved. + */ +@SuppressWarnings("removal") +final class UdfSecurity { + private static final PermissionCollection TRUSTED_PERMISSIONS = trustedPermissions(); + private static SecurityManager installedManager; + private static PermissionCollection configuredPermissions; + + private UdfSecurity() { + } + + static synchronized void install() { + SecurityManager current = System.getSecurityManager(); + if (installedManager != null) { + if (current != installedManager) { + throw new IllegalStateException("The Java UDF security manager has been replaced"); + } + return; + } + if (current != null) { + throw new IllegalStateException("Java UDF permissions cannot replace an existing SecurityManager"); + } + if (!(ForkJoinPool.commonPool().getFactory() instanceof DorisForkJoinWorkerThreadFactory)) { Review Comment: [P2] Configure the UDF test JVM for the mandatory worker factory Four existing `JavaUdfPluginTest` tests construct real executors and now reach this check, but their Surefire JVM never sets `java.util.concurrent.ForkJoinPool.common.threadFactory`. The property is only added by `start_be.sh`, which `run-fe-ut.sh` does not execute. Thus the normal `java-udf` unit suite throws `IllegalStateException` before its loader/cache assertions. Please supply the required property when starting the test JVM, before the common pool is initialized, together with a permissions-file fixture for standalone test runs. ########## fe/be-java-extensions/jni-spi/src/main/java/org/apache/doris/jni/spi/DorisForkJoinWorkerThreadFactory.java: ########## @@ -0,0 +1,46 @@ +// 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.spi; + +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.ForkJoinWorkerThread; + +/** + * Keeps the BE's common pool usable by trusted scanner plugins after Java UDF permissions are enabled. + * JDK 17's default factory switches to innocuous workers when a SecurityManager is installed: those + * workers cannot install a plugin context classloader and carry an empty inherited permission set. + * Set the common.threadFactory JVM property before the common pool is initialized. + */ +@SuppressWarnings("removal") +public final class DorisForkJoinWorkerThreadFactory implements ForkJoinPool.ForkJoinWorkerThreadFactory { Review Comment: [P2] Account for the new class in the frozen JNI SPI surface This public type is automatically discovered by `JniPluginSurfaceTest.frozenTypes()`, so `pluginApiSurfaceMatchesRecordedBaseline` now reports its class, constructor and `newThread` method as unexpected additions. The PR updates neither `jni-plugin-surface.txt` nor the API version/pinned version test, which remain at `6.0`. Please either keep this startup helper outside the frozen SPI package or update the baseline and version together under the existing SPI contract; otherwise the existing `jni-spi` suite fails. -- 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]
