Repository navigation
don't follow symlinks or trust another user's file in FS.open - #1628
netliomax25-code wants to merge 7 commits into
Conversation
|
Is this standard practice in other projects using cache files? Would it be easier to put the cache files somewhere else? |
|
Both fair questions.
The two aren't exclusive, and they're not the same size of change. I'm happy to convert this PR to a per-user cache directory, or leave it as the narrow fix and do the relocation separately. Whichever you'd rather carry. Validation on the branch: full suite green with and without atomicwrites installed (1315 tests), CI passing. |
|
any preference? |
|
I think we should consider doing both. The change from this PR on it's own is a good idea already. |
|
The current implementation does not refuse to open files that have incorrect permissions for reading, it just changes them. Is this intentional? And don't worry about cache invalidation, a new lark version is going to invalidate all caches anyway. |
The write path still tightens to 0600 rather than refusing, which seems right since it truncates and we own it. Added On the relocation: agreed, I'll keep this as the narrow fix and open the per-user cache directory as a separate PR. |
|
@erezsh I can't approve workflows AFAICT. |
|
@MegaIng I changed your role from “triage” to “write". Let me know if it still doesn't work. |
|
@erezsh Works, thank you. I am going to leave merging to you, but I think this PR is ready. |
|
Thanks, I haven't had time to look into it yet, but I'll do it soon. |
|
Fable had some comments, and at a glance they seem right. I would appreciate if you could address them.
(I'm not sure 2 is a bad thing, but worth keeping in mind.) |
…w read check to write bits
|
Thanks, all four look right. Addressed them:
Validation: full suite green with and without atomicwrites installed (1320 tests), mypy clean on the changed files. |
2360cd3 to
7ad4b28
Compare
|
I made small changes. Let me know if you agree @MegaIng & @netliomax25-code |
|
Looks good to me. The save-path PermissionError branch is the half I'd only done on the load side, so catching it there is the right symmetry, and the reworded warnings are clearer. I pulled the branch and ran the full suite green (1321 tests, with and without atomicwrites), and the new test passes both ways. Happy for it to go in. |
|
any update? |
|
A question that just occurred to me, should we make it possible to disable this protection? Because some users might not care about it, and might not need it, while it could break some legitimate workflows. (at least, legitimate in the context of personal use) |
|
I think yes, as long as it stays on by default and the opt-out is explicit.
I can add that to this PR with a test for both settings, or it could go in separately so this one isn't held up further. The default-on behavior here doesn't change either way. |
|
I think |
|
I don't quite like using "safe" as a term for a functionality that still loads arbitrary data. This doesn't fix the attack vector, it just makes it harder to use. |
|
Maybe unguarded_cache is a better term? Anyway, I'm open to suggestions. |
|
Validation: the new tests cover both settings (a 0666 cache is refused and rebuilt by default, and loaded with |
|
gentle ping |
|
Thanks, this is close. However, I did another pass with Fable, and here's what it had to say:
Optional nit: NetBSD reports O_NOFOLLOW on a symlink as EFTYPE, so getattr(errno, "EFTYPE", -1) could join the ELOOP/EMLINK check. Please address these points! (if you think some of them are wrong or irrelevant, include your reasoning) |
A hard link to one of the user's own files passes the ownership check, so refuse st_nlink != 1. Open with O_NONBLOCK so a FIFO can't hang Lark(), refuse anything that isn't S_ISREG after the fstat, and turn EISDIR, ENXIO and EOPNOTSUPP (socket on macOS) from os.open into PermissionError, along with EFTYPE for a symlink on NetBSD. Tests now expect PermissionError, cover hard links, FIFOs and directories, and run cache=True end to end against a patched temp dir.
|
Pushed b28bc87. It covers all five points and the nit.
Nit: Validation: full suite passes with and without atomicwrites (1329 tests on 3.12, 1323 on 3.9), and mypy is clean. I've only run this on macOS. The Linux ENXIO case for sockets is going by open(2), and the Windows job still needs a CI run. |
|
Thanks, the follow-up looks good. I think we're getting really close. One more issue from a reviewer: on save, _open_private accepts a cache we own that others can write (the 0o022 check only applies on read), then fchmods it to 0600 and rewrites it in place. A writer who already has that file open keeps their descriptor and can overwrite the pickle after we write it, and the next load will trust it. Rather than refusing such files on write, which would leave a legacy cache unusable forever, please write to a tempfile.mkstemp file in the same directory and os.replace it onto the cache path, which is what atomicwrites does. That lets the atomicwrites branch in FS.open go away too. The read path and its checks stay as they are. A test that a self-owned 0666 cache ends up as a new 0600 inode after save would cover it. |
cache=Truethe cache path is derived only from the grammar, the options and the version strings, so<tmpdir>/.lark_cache_<user>_<sha256>_3_12.tmpis predictable to anyone else with access to the temp directory.pickle.loadinlark.py:377before the sha256 line is compared on 378, and since that digest sits in the same file it is a cache key rather than an authenticity check, so the load is arbitrary code execution as whoever runs the parser.Sent the non-atomicwrites branch of
FS.openthrough anO_NOFOLLOWopen that also refuses a file it doesn't own and keeps the cache at 0600, which is roughly what the atomicwrites branch gets frommkstemptoday. A refused cache just falls through to the existing handlers, so the grammar is parsed normally. Three tests intests/test_cache.pycover the load path, the save path and the file mode; they fail on master and pass here, and the rest of the suite is green with and withoutatomicwritesinstalled.