-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add File Interceptor and Integ Tests #17760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1eac349
63cc7c2
bddb108
a7c48a8
1fff2f0
101cd26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.javaagent; | ||
|
|
||
| import org.opensearch.javaagent.bootstrap.AgentPolicy; | ||
|
|
||
| import java.io.FilePermission; | ||
| import java.lang.reflect.Method; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.security.Policy; | ||
| import java.security.ProtectionDomain; | ||
| import java.util.Collection; | ||
|
|
||
| import net.bytebuddy.asm.Advice; | ||
|
|
||
| /** | ||
| * FileInterceptor | ||
| */ | ||
| public class FileInterceptor { | ||
| /** | ||
| * FileInterceptor | ||
| */ | ||
| public FileInterceptor() {} | ||
|
|
||
| /** | ||
| * Intercepts file operations | ||
| * | ||
| * @param args arguments | ||
| * @param method method | ||
| * @throws Exception exceptions | ||
| */ | ||
| @Advice.OnMethodEnter | ||
| @SuppressWarnings({ "removal", "deprecation" }) | ||
| public static void intercept(@Advice.AllArguments Object[] args, @Advice.Origin Method method) throws Exception { | ||
| final Policy policy = AgentPolicy.getPolicy(); | ||
| if (policy == null) { | ||
| return; /* noop */ | ||
| } | ||
|
|
||
| String filePath = null; | ||
| if (args.length > 0 && args[0] instanceof String pathStr) { | ||
| filePath = Paths.get(pathStr).toAbsolutePath().toString(); | ||
| } else if (args.length > 0 && args[0] instanceof Path path) { | ||
| filePath = path.toAbsolutePath().toString(); | ||
| } | ||
|
|
||
| if (filePath == null) { | ||
| return; // No valid file path found | ||
| } | ||
|
|
||
| final StackWalker walker = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE); | ||
| final Collection<ProtectionDomain> callers = walker.walk(StackCallerProtectionDomainChainExtractor.INSTANCE); | ||
|
Check warning on line 59 in libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/FileInterceptor.java
|
||
|
|
||
| final String name = method.getName(); | ||
| final boolean isMutating = name.equals("copy") | ||
| || name.equals("move") | ||
| || name.equals("write") | ||
| || name.equals("newByteChannel") | ||
| || name.startsWith("create"); | ||
| final boolean isDelete = isMutating == false ? name.startsWith("delete") : false; | ||
|
|
||
| // Check each permission separately | ||
| for (final ProtectionDomain domain : callers) { | ||
| // Handle FileChannel.open() separately to check read/write permissions properly | ||
| if (method.getName().equals("open")) { | ||
| if (!policy.implies(domain, new FilePermission(filePath, "read,write"))) { | ||
| throw new SecurityException("Denied OPEN access to file: " + filePath + ", domain: " + domain); | ||
| } | ||
| } | ||
|
|
||
| // File mutating operations | ||
| if (isMutating && !policy.implies(domain, new FilePermission(filePath, "write"))) { | ||
| throw new SecurityException("Denied WRITE access to file: " + filePath + ", domain: " + domain); | ||
| } | ||
|
|
||
| // File deletion operations | ||
| if (isDelete && !policy.implies(domain, new FilePermission(filePath, "delete"))) { | ||
| throw new SecurityException("Denied DELETE access to file: " + filePath + ", domain: " + domain); | ||
| } | ||
| } | ||
| } | ||
|
Check warning on line 88 in libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/FileInterceptor.java
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.