Repository navigation
restrict cache file mode and reject symlinks in FileResourceFactory - #3329
Conversation
|
@Hsnmohd Should not this be dependent on the runtime permissions of the user identity the JRE is running under? |
|
The requested mode is still filtered through the umask of the identity the JRE runs as: open(2) masks the creation mode, so rw------- can only ever lose bits, never gain them. What the patch removes is the library depending on umask to drop the group and other read bits. FileOutputStream asks for 0666, so with the common default of 022 the cached body of an authenticated response lands rw-r--r--, and the cache directory mode does not help because a new POSIX file does not inherit it. What it does take away is deliberately running umask 007 to share the cache directory with a service group. If that is worth keeping, I can add a constructor that takes the permissions and defaults to owner-only. The NOFOLLOW_LINKS half is separate from the mode: the file name is fully determined by the request URI and the ETag, so anyone able to write into the cache directory can plant a link at that name and have the next cache write land on its target. |
|
@Hsnmohd OK. Sounds reasonable. Last question. Are you reasonably sure this change is not going to break the file cache with non-posix JRE implementations? I no longer have a Windows machine to test with and our CI jobs run on Ubuntu and MacOS only. The change-set passing the tests in CI is not enough to be sure. |
arturobernalg
left a comment
There was a problem hiding this comment.
Symlinks are blocked, but any pre-existing regular file at the predictable path (pctEncode(etag)@pctEncode(requestId), no randomness) also makes Files.createFile throw FileAlreadyExistsException, which is caught and ignored — the rewrite reuses that inode and keeps its permissions. Verified end-to-end with a real server and
CachingHttpClients.createFileBound(): the authenticated body lands in the attacker's pre-planted file, permissions stay rw-rw-rw-.
|
@ok2c Reasonably sure, from the JDK sources rather than a Windows run, since I don't have one either. WindowsFileSystem reports basic, dos, acl, owner and user as attribute views and no posix, so on Windows the permission attribute is never passed. Passing it would throw UnsupportedOperationException from WindowsSecurityDescriptor.fromAttribute, which is exactly what the supportedFileAttributeViews check is for. On the open side, WindowsChannelFactory maps CREATE_NEW to the CREATE_NEW disposition plus FILE_FLAG_OPEN_REPARSE_POINT, so an existing reparse point fails the create the same way O_CREAT|O_EXCL does on Unix. On a non-POSIX JRE the write is therefore deleteIfExists plus an exclusive create, the same as the old FileOutputStream apart from the exclusive flag. @arturobernalg good catch, and a planted hard link was the same hole. Pushed a change that never reuses what's at the path: deleteIfExists, then CREATE_NEW|WRITE with the owner-only attribute in a single open, so there's no window between create and write. A planted file, hard link or symlink is now removed and replaced with a fresh owner-only file instead of being written through (the symlink case no longer throws). Added tests for the pre-existing file and the hard link; on the previous revision they fail with rw-rw-rw- and the outside file overwritten, and the cache module suite stays green. |
Cache files created world-readable and through symlinks
FileResourceFactory.generatewrites each cached body withnew FileOutputStream(file, false), so the file gets the process default mode: withCachingHttpClients.createFileBoundand the usual umask the file holding a response arrives rw-r--r--, readable by every local user even when the cached response was authenticated. The same call follows symbolic links, and the file name comes from the request URI and the response ETag, so anyone able to write into the cache directory can plant a link at that name and have the next cache write land on its target with a body the origin chose; verified by overwriting a file outside the cache directory.