Some quick background on my problem:
I'm writing a compiler that converts Domain Type Enforcement specs into Java Security Manager code. In a nutshell, DTE defines "types" (objects), assigns paths to those types; then defines "domains" (subjects), and defines what permissions (rwxdc) domains have to various types. I need to emulate this behavior as closely as possible in the JSM.
Currently I'm working on write permissions. I've overridden the JSM's checkWrite(String filename) method successfully. Next on the list is checkWrite(FileDescriptor filedescriptor) and this one is proving trickier. Due to the way DTE is defined, I need path information to determine whether a write action is permissible.
Is it possible to extract path data from a FileDescriptor? I'm guessing no -- I've checked the documentation and various tutorials, and I've found nothing to suggest that there's any way for me to obtain this information (I'd be delighted to be shown wrong, however; that would make my job easier).
If the answer to the above is NO, could anyone suggest a viable workaround? For example, is there some way I could write native code to do what I want and tie this into my custom JSM class? I'm fine with doing something "advanced" like that, but I'd need some idea how to get started.
Or is my only option basically to deny all write permissions that use a FileDescriptor? I'd very much like to avoid this because it's a crummy solution, but if that's the reality I need to know.
Thanks very much for your time.
The short answer is no, because a file is independent from the path used to access that file (at least on any OS that matters).
One possible work-around is to trap the calls that open files, using an aspect framework, and put the referenced file descriptors into a
WeakHashMap<FileDescriptor,File>
. Then you simply look at this map whenever you need to validate a write.the solution to get the path from FileDescriptor in java:
How it works:
We know that file descriptor contains descriptor id to locate the open file in the current process.
What are file descriptors, explained in simple terms?
if we know the descriptor id then we can easily find file path by following java code:
Here:
SafeFileDescriptor.java
getFDid()
is a native method used to get descriptor id of the given file descriptor objectfollowing code is implementation of
getFDid()
native methodSafeFileDescriptor.h
create SafeFileDescriptor.h from java file SafeFileDescriptor.java
replace "dir" with your directory to store SafeFileDescriptor.h
SafeFileDescriptor.c
compile SafeFileDescriptor.c
To add libSafeFileDescriptor.so file to java class file