Skip to content
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

File.mkdir_p: Fix TOCTOU issue & handle errors from parent mkdir #14242

Merged
merged 1 commit into from
Jan 30, 2025

Conversation

lanodan
Copy link
Contributor

@lanodan lanodan commented Jan 29, 2025

Fix TOCTOU, meaning :file.make_dir returning {:error, :eexists} is
now tested without having to trigger a race-condition.
Also change dir?() returning false from returning :eexist to :enotdir

And handling errors from parent directory creation allows to avoid
non-sensical errors like File.mkdir_p returning :enoent instead of :eacces

lib/elixir/lib/file.ex Outdated Show resolved Hide resolved
@josevalim
Copy link
Member

Thank you for the PR, can you please expand which errors/issues are you trying to avoid?

@lanodan
Copy link
Contributor Author

lanodan commented Jan 30, 2025

For TOCTOU, the first dir?() is effectively useless and hid false status from the second dir?() not being handled correctly as it would return {:error, :eexists} instead of the more proper {:error, :enotdir}, which happens when parallel calls to mkdir_p are done with shared parent directories which needs to be created.

And one problem which Pleroma has been hitting few times is one of the parent directories having wrong permissions,
currently Elixir just ignores the error and so ends up with "No such file or directory" errors (which makes no sense for mkdir_p) instead of "Permission denied".

@lanodan lanodan force-pushed the toctou-mkdir_p branch 2 times, most recently from 563c823 to b336838 Compare January 30, 2025 05:05
@josevalim
Copy link
Member

And one problem which Pleroma has been hitting few times is one of the parent directories having wrong permissions,
currently Elixir just ignores the error and so ends up with "No such file or directory" errors (which makes no sense for mkdir_p) instead of "Permission denied".

Fantastic, can we please try to add a test that reproduces this then? Thank you!

Fix TOCTOU, meaning :file.make_dir returning {:error, :eexists} is
now tested without having to trigger a race-condition.
Also change dir?() returning false from returning :eexist to :enotdir

And handling errors from parent directory creation allows to avoid
non-sensical errors like File.mkdir_p returning :enoent instead of :eacces
@josevalim josevalim merged commit 07afa71 into elixir-lang:main Jan 30, 2025
7 of 9 checks passed
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

iam-sysop pushed a commit to purg-com/pleroma-mirror that referenced this pull request Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants