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

change cd to handle not readable directories: chdir+open instead of open+fchdir #10436

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

TheJonny
Copy link

@TheJonny TheJonny commented Apr 11, 2024

The previous code for the cd builtin used open(.., O_RDONLY) + fchdir to change the working directory. This fails, if the user has no read permission on the directory (but has the x permission). Now we first chdir and then try to open the new workin directory. this might fail, but in this case cd was already successful.

The alternative would be to use O_PATH instead of O_RDONLY, but this is not portable. As an improvement, we could use O_PATH for supported platforms later.

Fixes issue #10432

TODOs:

  • Changes to fish usage are reflected in user documentation/manpages. (from the template, i think it does not apply here)
  • Tests have been added for regressions fixed
  • User-visible changes noted in CHANGELOG.rst
  • Check for / discuss now unused code

…pen+fchdir

The previous code for the cd builtin used open(.., O_RDONLY) + fchdir to change the working directory.
This fails, if the user has no read permission on the directory (but has the x permission).
Now we first chdir and then try to open the new workin directory. this might fail,
but in this case cd was already successful.

The alternative would be to use O_PATH instead of O_RDONLY, but this is not portable.
As an improvement, we could use O_PATH for supported platforms later.
@TheJonny TheJonny marked this pull request as ready for review April 11, 2024 16:14
@TheJonny
Copy link
Author

should we note the change in CHANGELOG.rst?
should we drop fds::wopen_dir (now unused) or keep it for future use?

@ridiculousfish ridiculousfish self-requested a review April 12, 2024 02:49
this prevents keeping a fd to the previous working directory
@TheJonny
Copy link
Author

The failed test is from the additional stderr line, from the testcase below.
Unable to open the current working directory: Permission denied
Do we want this output in cd?
also without this patch mkdir a; chmod -r a; sh -c 'cd a; fish' triggers this message

Copy link
Member

@ridiculousfish ridiculousfish left a comment

Choose a reason for hiding this comment

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

Thank you for the PR. The reason we store the file descriptor in the parser is that subprocesses will be implemented using multiple threads, each with their own fd for the cwd. For that reason we should not rely on the processes's implicit cwd (i.e. chdir) for anything, outside of startup, where we discover where we are.

That said, I would definitely like to see this fixed. I'm fine with using O_PATH on Linux (supported since 2014), and O_SEARCH / O_EXEC on BSDs / macOS. We can keep O_RDONLY as a fallback for any other platforms.

Note Nix doesn't seem to have up-to-date support for O_SEARCH / O_EXEC so we may need to fall back to libc or hard-coded constants.

@TheJonny
Copy link
Author

TheJonny commented Apr 14, 2024

thank you for the reply :)
the current implementation (without this PR) relies on the process cwd when opening relative paths before fchdir, so the implementation in this PR using chdir with the same relative path should not worsen anything right now.

@TheJonny
Copy link
Author

when using O_PATH / O_SEARCH, should we rename the [w]open_dir functions to open_path or add the flags as argument, to make clear when it is possible to readdir on the returned file descriptor?

@TheJonny
Copy link
Author

ok fascinating - macos and freebsd 13 have indeed O_SEARCH, it's just not in the manpages.

so i will have some #[cfg] fun.

@TheJonny
Copy link
Author

@ridiculousfish i implemented the alternative in #10442

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants