|
| 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.File; |
| 14 | +import java.io.FilePermission; |
| 15 | +import java.lang.reflect.Method; |
| 16 | +import java.security.Policy; |
| 17 | +import java.security.ProtectionDomain; |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +import net.bytebuddy.asm.Advice; |
| 21 | +import net.bytebuddy.asm.Advice.Origin; |
| 22 | + |
| 23 | +/** |
| 24 | + * FileInterceptor |
| 25 | + */ |
| 26 | +public class FileInterceptor { |
| 27 | + /** |
| 28 | + * FileInterceptor |
| 29 | + */ |
| 30 | + public FileInterceptor() {} |
| 31 | + |
| 32 | + /** |
| 33 | + * Intercepts file operations |
| 34 | + * @param args arguments |
| 35 | + * @param method method |
| 36 | + * @throws Exception exceptions |
| 37 | + */ |
| 38 | + @Advice.OnMethodEnter |
| 39 | + @SuppressWarnings("removal") |
| 40 | + public static void intercept(@Advice.AllArguments Object[] args, @Origin Method method) throws Exception { |
| 41 | + final Policy policy = AgentPolicy.getPolicy(); |
| 42 | + if (policy == null) { |
| 43 | + return; /* noop */ |
| 44 | + } |
| 45 | + |
| 46 | + String filePath = null; |
| 47 | + if (args.length > 0 && args[0] instanceof String path) { |
| 48 | + filePath = path; |
| 49 | + } else if (args.length > 0 && args[0] instanceof File file) { |
| 50 | + filePath = file.getAbsolutePath(); |
| 51 | + } |
| 52 | + |
| 53 | + if (filePath == null) { |
| 54 | + return; // No valid file path found |
| 55 | + } |
| 56 | + |
| 57 | + final StackWalker walker = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE); |
| 58 | + final List<ProtectionDomain> callers = walker.walk(new StackCallerChainExtractor()); |
| 59 | + |
| 60 | + boolean isMutating = method.getName().startsWith("write") |
| 61 | + || method.getName().equals("createNewFile") |
| 62 | + || method.getName().equals("mkdirs") |
| 63 | + || method.getName().equals("mkdir") |
| 64 | + || method.getName().equals("setWritable"); |
| 65 | + boolean isDelete = method.getName().equals("delete"); |
| 66 | + |
| 67 | + // Check each permission separately |
| 68 | + for (final ProtectionDomain domain : callers) { |
| 69 | + // FileInputStream operations |
| 70 | + if (!policy.implies(domain, new FilePermission(filePath, "read"))) { |
| 71 | + throw new SecurityException("Denied READ access to file: " + filePath + ", domain: " + domain); |
| 72 | + } |
| 73 | + // FileOutputStream operations |
| 74 | + if (!policy.implies(domain, new FilePermission(filePath, "write"))) { |
| 75 | + throw new SecurityException("Denied WRITE access to file: " + filePath + ", domain: " + domain); |
| 76 | + } |
| 77 | + // File mutating operations |
| 78 | + if (isMutating && !policy.implies(domain, new FilePermission(filePath, "write"))) { |
| 79 | + throw new SecurityException("Denied WRITE (mutating) operation to file: " + filePath + ", domain: " + domain); |
| 80 | + } |
| 81 | + // File deletion operations |
| 82 | + if (isDelete && !policy.implies(domain, new FilePermission(filePath, "delete"))) { |
| 83 | + throw new SecurityException("Denied DELETE access to file: " + filePath + ", domain: " + domain); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments