Skip to content

restrict cache file mode and reject symlinks in FileResourceFactory - #3329

Merged
ok2c merged 2 commits into
apache:masterfrom
Hsnmohd:cache-file-permissions
Dec 5, 2026
Merged

ok2c merged 2 commits into
apache:masterfrom
Hsnmohd:cache-file-permissions

Conversation

@Hsnmohd

@Hsnmohd Hsnmohd commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Cache files created world-readable and through symlinks

FileResourceFactory.generate writes each cached body with new FileOutputStream(file, false), so the file gets the process default mode: with CachingHttpClients.createFileBound and 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.

@ok2c

ok2c commented Nov 30, 2026

Copy link
Copy Markdown
Member

@Hsnmohd Should not this be dependent on the runtime permissions of the user identity the JRE is running under?

@Hsnmohd

Hsnmohd commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

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.

@ok2c

ok2c commented Dec 2, 2026

Copy link
Copy Markdown
Member

@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.

@ok2c ok2c added this to the 5.7-beta2 milestone Dec 2, 2026
@ok2c ok2c added the 5.7.x 5.7 release series label Dec 2, 2026

@arturobernalg arturobernalg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-.

@Hsnmohd

Hsnmohd commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

@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.

@ok2c
ok2c requested a review from arturobernalg December 5, 2026 08:30
@ok2c
ok2c merged commit f9da842 into apache:master Dec 5, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

5.7.x 5.7 release series

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants