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

Use mktemp to create temporary files to hold ssh host keys and authorized keys #6732

Merged
merged 3 commits into from
Jun 2, 2015

Conversation

oconnorr
Copy link
Contributor

@oconnorr oconnorr commented Mar 9, 2015

Currently I'm making the temporary files in $TMP which is /tmp. I could also put them in /root, but I couldn't think of any reason why I should do that. Have any preferences?

@domenkozar
Copy link
Member

That's quite a security issue

@oconnorr
Copy link
Contributor Author

oconnorr commented Mar 9, 2015

What is quite the security issue and why?

@domenkozar
Copy link
Member

Someone could monitor /tmp and get access to root keys

@oconnorr
Copy link
Contributor Author

I'm happy to put the files into /root instead, but I still don't get the problem with using /tmp. These temporary files are only accessible by root both because of the umask 077.

@edolstra
Copy link
Member

I would use /run instead of /tmp. Then you don't have to worry about /tmp races.

@oconnorr
Copy link
Contributor Author

Okay, I'm using /run to write temporary files. I think this is good, but I still don't understand domenkozar's security concerns. Granted it is all a little academic since any rogue process can download the private ssh keys right from the metadata server.

chmod 600 /root/.ssh/authorized_keys
echo "obtaining SSH key..."
mkdir -m 0700 -p /root/.ssh
AUTH_KEYS=$(${mktemp}) && {
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens when mktemp fails? It seems like it would continue the script?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes you are right. It will skip the block of code that depends on the result of mktemp, which is good, but in the end the service should fail if mktemp fails.

I'll try to redesign the error handling, but if you have a recommended pattern to use in this case please post it. Ideally I want the outer file created by mktemp to clean up even when the inner mktemp fails.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, how much do we care about handling the case when mktemp fails. We currently don't do anything when wget fails. What do you think? Would you like me to add error messages for all the failing possibilities?

Copy link
Contributor

Choose a reason for hiding this comment

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

Service scripts run with the -e shell option, so execution should halt at the first unhandled non-zero return code. Replacing the mktemp && { ... } construct with a regular command sequence should give you the desired behaviour. The branch is pointless anyway: either mktemp succeeds and you're good to go or it fails and you might as well crash immediately. Cleanup seems unnecessary if you're writing to /run.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I understand. I can also get rid of checking return value of wget as well since if wget does fail, it will never get to the next line. I will also change the check of the file existence to check that the file is non-zero in size instead, since mktemp makes sure the file exists.

Copy link
Member

Choose a reason for hiding this comment

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

The difference is that /run is not world-writable, so you don't have to worry about malicious users.

Copy link
Contributor

Choose a reason for hiding this comment

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

@edolstra ah, of course, thanks for clarifying that for me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I'm still working on replacing the mktemp && { ... } construct.

I wanted to clarify how does an adversary race temp files? Isn't the whole point of mktemp to prevent races?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah that's true, mktemp makes it hard/impossible to create the temp file before an adversary can create it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I've removed the && { ... }. It should be good now.

…ized keys when downloading them from the metadata server.
Scripts are run with -e so will abort when a command fails.
@oconnorr
Copy link
Contributor Author

oconnorr commented Jun 2, 2015

Okay, I've removed the && { ... }. It should be good now.

wmertens added a commit that referenced this pull request Jun 2, 2015
Use mktemp to create temporary files to hold ssh host keys and authorized keys
@wmertens wmertens merged commit 0666ee4 into NixOS:master Jun 2, 2015
@wmertens
Copy link
Contributor

wmertens commented Jun 2, 2015

Thanks!

@copumpkin
Copy link
Member

Granted it is all a little academic since any rogue process can download the private ssh keys right from the metadata server.

@oconnorr you can restrict access to the metadata service via iptables based on user. I've been meaning to package the functionality up for EC2's metadata service, and I'm sure we could carry over the same rule to GCE.

@wmertens
Copy link
Contributor

wmertens commented Jun 5, 2015

@copumpkin that's a great idea!

On Tue, Jun 2, 2015, 9:10 PM Daniel Peebles notifications@github.com
wrote:

Granted it is all a little academic since any rogue process can download
the private ssh keys right from the metadata server.

@oconnorr https://github.com/oconnorr you can restrict access to the
metadata service via iptables based on user. I've been meaning to package
the functionality up for EC2's metadata service, and I'm sure we could
carry over the same rule to GCE.


Reply to this email directly or view it on GitHub
#6732 (comment).

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

Successfully merging this pull request may close these issues.

7 participants