2

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?

1 Answer 1

9

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?

3
  • 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, 2013 at 20:10
  • Glad to be of help! May 24, 2013 at 20:16
  • 5
    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, 2013 at 21:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .