-
Notifications
You must be signed in to change notification settings - Fork 841
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
Hackage credentials file is world-readable when saved #2159
Comments
It's unclear to me how to safely create this credentials file (atomically with the right permissions) in a cross-platform fashion.
I've asked this on http://stackoverflow.com/questions/37304650/how-to-create-a-user-only-readable-file-cross-platform to get further input. It seems to me that without further info, we have to conditionally depend on the |
In addition to restricting the file permissions, I would suggest not saving the password by default. There could be a config option, or a prompt when first running |
Would it be safe to do something like |
Yes, that should work. I don't tink you necessarily need to create the temporary directory, putting your temp file into the same dir as the destination would also work. I see your key point though, this can be done using just functions from (To detail on the case of using |
Please note, since ca6ba8f (Stack 1.5.0) it is possible to pass: save-hackage-creds: false This is not the default (I believe) but a message is shown. I believe the permissions issue is still not addressed. I'll take a look. |
@nh2, I took a quick look again here ...
Hmm, but is this not the same as you mentioned in:
I'm really not sure what this would even mean on a Windows machine. I'm inclined to think this whole file saving is a bad idea now as mentioned in comments above. Especially thinking cross-platform. Could we not just load username/password in from environment variables (user can deal with this safely)? Any objections? |
FWIW I agree with @lwm - automatically having credentials stored in the clear doesn't seem worth the risk considering how little effort it would require for users who understood the vulnerability and wanted to set it up manually anyway. |
What about changing the default from "save to the file" to "prompt the user if the password should be saved"? We can also give the user a message that, if they're tired of getting prompted, they can set |
@nh2 ^^^ Seems like we remove the default of saving with this suggested approach. I see cabal also allows to save credentials in plain text in a configuration file. To be honest, the only decent way I've seen of keeping credentials safe on disk is using the |
@lwm The difference is the
Which part do you mean, the inotify bit? What I meant is simply that if somebody wants to snatch your password by exploiting the fact that it's world-readable for a short moment, it is easy to do so with tools like inotify.
@snoyberg Sounds good!
@lwm I may have been a bit unclear in titling the issue -- my problem isn't that stack saves passwords in plain text files (this is very common and useful, e.g. Whether we ask for the password to be saved seems completely orthogonal to this -- whenever we save credentials on disk, we should ensure that the permissions are right before the contents are written. The |
Thanks for clarifying! So, if I got this right then, here's what needs to be done:
|
Probably use |
Yes
@mat8913 Wait, I still don't get why we need the temporary directory. Why isn't the file enough? |
Because the file cannot be created with the correct permissions. At best you could do this:
However, an attacker could open the file between steps 1 and 2. They then wait for step 3 to finish before actually reading the opened file. |
Oh, wait a minute. I think I understand what you are saying. If I'm understanding correctly, your proposed strategy is:
However, I think file descriptors remain valid even if the file is moved, so maybe copying is the correct strategy. Edit: After further testing, I can confirm that file descriptors do remain valid after file is moved. Here's some C code for Linux (might also work on other POSIX systems):
Steps to reproduce:
You should see the data you wrote to the moved file output to the terminal. |
@mat8913 I just tried your C code + steps on FreeBSD with The code ran fine and I got the same behavior you described with file descriptors remaining valid once a file has been moved. |
@mat8913 You are right, what I wrote is rubbish. Another easy check with 2 terminals and python:
@mat8913 Do you know if your If it's not safe, why can't we just use |
As far as I can tell, my approach is safe. Here's the code I used to test:
Steps I followed:
If the C program manages to read the file, it will print it's contents. Otherwise, it will print the error. On my computer, it printed the error:
Windows does seem to support open, but it's deprecated. |
Following #2159 (comment), I've opened up #3436 to kill the first two points. @paulrzcz, I know you mentioned writing a C function to solve the security issue - any progress there (open for anyone else who wishes to delve in there)? |
@mat8913 I've looked into this in detail again; in addition to the (valid) C example you gave, here's an interleaving with Python 3 (
and
So indeed despite us opening the handle to the We would have to ensure though that when doing Of course that depends on the permissions that So I think for full generality we would need However, if
|
…readable Hackage creds are read-only (fixes #2159)
saveCreds
(stack/src/Stack/Upload.hs
Line 112 in bcf73ec
~/.stack/upload/credentials.json
(containing Hackage username and password) without explicit file permissions, which results in the defaultumask
being used, which is typicallyrw-rw-r
, e.g. on Ubuntu.This means that when your home directory is also world-readable (on Ubuntu it is,
rwxr-xr-x
), other users can grab your Hackage credentials.For single user systems this is less problematic but not ideal for e.g. university setups where all users' homes are mounted over NFS.
The fix would be for stack to create this file with
rw-------
permissions (and ideally check this when reading so that upgrades from old versions of stack can notice the problem).Independently, as a person who generally dislikes any on-disk plaintext passwords, I'd prefer if the default behaviour was not to save the credentials on disk, but use a flag for that.
The text was updated successfully, but these errors were encountered: