Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I noticed that in ubuntu, mktemp allows a user to make temporary files in a directory even if write permissions are not allowed for that user. I thus expected the permissions on mktemp to read something like this, giving the user temporary root privileges while running it:

-rwsr-xr-x 1 root root   35392 Nov 19  2012 mktemp

but instead they read something like this:

-rwxr-xr-x 1 root root   35392 Nov 19  2012 mktemp

Where I thought there was an 's', there is an 'x', meaning mktemp does not run with root privileges. How is mktemp able to create temporary files in a directory without having the permission to do so?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

mktemp, by default, creates a temporary directory under /tmp, which by default is writable by anyone; if you pass the --tmpdir option to mktemp (or set the TMPDIR environment variable), it will attempt to create a temporary directory in the directory given by the option's argument, and that will fail unless you have write permission in the given directory. For example:

[me@box] $ mktemp
/tmp/tmp.sL1g7rRGQv
[me@box] $ mktemp --tmpdir=/root
mktemp: failed to create file via template `/tmp.XXXXXXXXXXX': Permission denied

If you're doing, e.g., mktemp --tmpdir=/root as a non-root user and it doesn't fail, then something very weird is going on. Are you sure that's what you're seeing?

share|improve this answer
    
I was just using default flags. I thought it put files into ./tmp , not /tmp because I apparently wasn't paying attention. Thanks! –  MYV May 24 '13 at 20:10
    
Glad to be of help! –  Aaron Miller May 24 '13 at 20:16
2  
A little nit-picking: IMHO /tmp has usually the sticky bit (t) set: drwxrwxrwt root root 0 May 24 23:52 /tmp. I. e. everybody can create files/dirs in it, but cannot delete/rename files owned by someone else -- even it is formally world writable as indicated by ls. For more info see e.g. en.wikipedia.org/wiki/Sticky_bit –  mpy May 24 '13 at 21:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.