|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.javaagent; |
| 10 | + |
| 11 | +import org.opensearch.javaagent.bootstrap.AgentPolicy; |
| 12 | + |
| 13 | +import java.io.FilePermission; |
| 14 | +import java.lang.reflect.Method; |
| 15 | +import java.nio.file.Path; |
| 16 | +import java.nio.file.Paths; |
| 17 | +import java.security.Policy; |
| 18 | +import java.security.ProtectionDomain; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import net.bytebuddy.asm.Advice; |
| 22 | + |
| 23 | +/** |
| 24 | + * FileInterceptor |
| 25 | + */ |
| 26 | +public class FileInterceptor { |
| 27 | + /** |
| 28 | + * FileInterceptor |
| 29 | + */ |
| 30 | + public FileInterceptor() {} |
| 31 | + |
| 32 | + /** |
| 33 | + * Intercepts file operations |
| 34 | + * |
| 35 | + * @param args arguments |
| 36 | + * @param method method |
| 37 | + * @throws Exception exceptions |
| 38 | + */ |
| 39 | + @Advice.OnMethodEnter |
| 40 | + @SuppressWarnings("removal") |
| 41 | + @SuppressForbidden(reason = "Using FilePermissions directly is ok") |
| 42 | + public static void intercept(@Advice.AllArguments Object[] args, @Advice.Origin Method method) throws Exception { |
| 43 | + final Policy policy = AgentPolicy.getPolicy(); |
| 44 | + if (policy == null) { |
| 45 | + return; /* noop */ |
| 46 | + } |
| 47 | + |
| 48 | + String filePath = null; |
| 49 | + if (args.length > 0 && args[0] instanceof String pathStr) { |
| 50 | + filePath = Paths.get(pathStr).toAbsolutePath().toString(); |
| 51 | + } else if (args.length > 0 && args[0] instanceof Path path) { |
| 52 | + filePath = path.toAbsolutePath().toString(); |
| 53 | + } |
| 54 | + |
| 55 | + if (filePath == null) { |
| 56 | + return; // No valid file path found |
| 57 | + } |
| 58 | + |
| 59 | + final StackWalker walker = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE); |
| 60 | + final List<ProtectionDomain> callers = walker.walk(new StackCallerChainExtractor()); |
| 61 | + |
| 62 | + boolean isMutating = method.getName().startsWith("write") |
| 63 | + || method.getName().equals("createFile") |
| 64 | + || method.getName().equals("createDirectories") |
| 65 | + || method.getName().equals("createLink") |
| 66 | + || method.getName().equals("copy") |
| 67 | + || method.getName().equals("move") |
| 68 | + || method.getName().equals("newByteChannel"); |
| 69 | + boolean isDelete = method.getName().equals("delete") || method.getName().equals("deleteIfExists"); |
| 70 | + |
| 71 | + // Check each permission separately |
| 72 | + for (final ProtectionDomain domain : callers) { |
| 73 | + // Handle FileChannel.open() separately to check read/write permissions properly |
| 74 | + if (method.getName().equals("open")) { |
| 75 | + if (!policy.implies(domain, new FilePermission(filePath, "read, write"))) { |
| 76 | + throw new SecurityException("Denied OPEN access to file: " + filePath + ", domain: " + domain); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // File mutating operations |
| 81 | + if (isMutating && !policy.implies(domain, new FilePermission(filePath, "write"))) { |
| 82 | + throw new SecurityException("Denied WRITE access to file: " + filePath + ", domain: " + domain); |
| 83 | + } |
| 84 | + |
| 85 | + // File deletion operations |
| 86 | + if (isDelete && !policy.implies(domain, new FilePermission(filePath, "delete"))) { |
| 87 | + throw new SecurityException("Denied DELETE access to file: " + filePath + ", domain: " + domain); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments