-
Notifications
You must be signed in to change notification settings - Fork 253
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
Dircache should handle fd == 0 as a valid file descriptor #320
Comments
Well, check this out: https://play.golang.org/p/gG0FOnelmzR Compile and run it with fds 0,1,2 closed:
Now let's look at `strace -p $(pgrep printloop):
This means fmt.Printf and log.Printfs blindly write into fds 1 and 2. So if the user closes all fds, they will have a bigger problem than the panic, because log output will end up in random files that happen to get opened at fd 1 or 2. Note that this can only happen with If the user only closes fd 0 and uses |
Hmm. I guess we should check at least fds 1,2 on startup, and exit with an error if they are closed, or open /dev/null. And while we are at it, we can do the same thing for 0. |
What about adding code early in the startup path to make sure those fds are registered? I am also using something like this in my security critical projects (and Wine uses it aswell):
|
For reference, here is what Wine does: The code relies on the fact that |
Note that in your case, checking |
Good idea. |
The Go stdlib, as well as the gocryptfs code, relies on the fact that fds 0,1,2 are always open. See #320 for details.
Pushed as ad15ad9 .
|
Hmm, unless the Stdlib decided to close that fd at some time. That would be a problem. |
Would it make a difference when we move it to the |
Good idea, but looks the same. Also, I am opening /tmp/ensureStdFds instead of /dev/null, and, interestingly, somebody else seems to open /dev/null first:
|
can we somehow inject our own init function (e.g., as a separate package) and ensure that it is executed before anything else? |
I think this is where the eventpoll fd comes from: https://github.com/golang/go/blob/35f4ec152b44ae5fc83aaf68e2eb3aa1a778e5cd/src/runtime/netpoll_epoll.go#L26 |
Do you know if there is actually any way to influence the but it doesn't seem to make a difference. |
Unverfied, but https://medium.com/@markbates/go-init-order-dafa89fcef22 suggests
Does this mean we have no influence since |
Also tried that, did not make a difference, hanwen runs first (yes, maybe alphabetic) |
Another problem: when we damonize, we overwrite fds 0,1,2. This means Go loses the eventpoll fd. |
Aaand it's gone:
|
Oh, correction, it's all good (4 -> 'anon_inode:[eventpoll]'). |
It still feels a bit error-prone, and its a bit surprising that Go doesn't offer any clean way to handle init priorities or something like that. The following patch seems to work, but I have no idea why?! |
strace output:
|
My interpretation would be, that |
Well, awesome!
|
Awesome! I've created a pull request (but feel free to adjust the commit if you would like to call the package differently or change anything else). |
Fixed by 7e05e80 |
While rather unlikely in practice, a file descriptor of 0 is completely valid and should not be treated as an error [1]. Both
open
andDup
typically return the smallest available file descriptor and only negative values are errors. If the user starts gocryptfs with stdin (fd == 0) closed and triggers the dircache code fast enough (not sure if that is actually possible), this could lead to a panic / unexpected behavior (e.g., in the fd validation inStore
).[1] https://unix.stackexchange.com/questions/100611/aix-open-file-descriptor-is-zero
The text was updated successfully, but these errors were encountered: