Using the shared /tmp directory is known to have lead to many security vulnerabilities when predictable filenames have been used. And randomly generated names aren't really nice looking.

I am thinking that maybe it would be better to use a per user temporary directory instead. Many applications will use the TMPDIR environment variable in order to decide where temporary files goes.

On login I could simply set TMPDIR=/temp/$USER where /temp would then have to contain a directory for each user with that directory being writable to that user and nobody else.

But in that case I would still like /temp to be a tmpfs mountpoint, which means that the subdirectories would not exist after a reboot and need to be recreated somehow.

Is there any (de-facto) standard for how to create a tmpfs with per user subdirectories? Or would I have to come up with my own non-standard tools to dynamically generate such directories?

  • I now found that Ubuntu has /run/user which looks almost like what I was looking for (except that /run by default can only use 1/5 the space that other tmpfs are allowed to). – kasperd Mar 25 '15 at 14:55
  • If you want more room in /run/user then probably mount -o remount,size=larger% /run/user/uid should work, though, if on ubuntu, you'll probably want to escalate with sudo for that. – mikeserv Mar 25 '15 at 17:11

You can use pam-tmpdir for this. It creates a directory for each user that logs in, at the start of their PAM session.

See How to remount filesystem at logout? for a little more context...

In Debian, Ubuntu and derivatives it's available in libpam-tmpdir.

  • Defaulting to /tmp/user for the location does not fully meet my requirements. so at the very least I'd have to reconfigure it to use a different path. I suppose that means there isn't another standard location for this kind of thing, and I'd have to make up my own idea about what path to use. – kasperd Mar 25 '15 at 13:57
  • It's actually /tmp/user/$UID, so each user gets their own directory. – Stephen Kitt Mar 25 '15 at 14:00
  • Sure, but it is still possible for somebody to grab the name /tmp/user before root. – kasperd Mar 25 '15 at 14:01
  • If you want to avoid that, you can configure libpam-tmpdir to fail by specifying "required" instead of "optional", which prevents login in an unsafe context. root gets /tmp/user/0, not /tmp/user. – Stephen Kitt Mar 25 '15 at 14:02
  • I'd still rather have it use a location, where that possibility doesn't even exist. That would be the case if I used /run rather than /tmp. It turns out that /run/user already exists and is created in a way quite similar to what I am looking for. – kasperd Mar 25 '15 at 14:20

You can just put it in /etc/fstab. A line for a particular user might look like:

tmpfs /home/username/tmp tmpfs size=[num]%,uid=[num],gid=[num],noauto,user,mode=0700

Putting a line like that for each user you want to be able to mount a tmpfs in their home directory will make it so. The user username could do just do mount ~/tmp to mount a tmpfs over their home directory ~/tmp at a 10% current system memory capacity. The mode is set to 0700 so only that user (and root of course) can do anything with the contents of that mount. And the mount is not made automatically - the user has to request it.

You'll want to replace all of that for the relevant values. For example, for yourself you might do:

tmpfs /home/kasperd/tmp tmpfs size=25%,uid=1000,gid=1000,noauto,user,mode=0700 0 1

...if your user and group id are both 1000.

  • 2
    I could imagine that mounting it on a sub directory of home directory could lead to security issues if the user replace the mount point with a symlink. It also doesn't scale well with multiple users. And getting it to work together with ecryptfs could be tricky. – kasperd Mar 25 '15 at 18:07
  • 1
    @kasperd - Suit yourself - I don't understand where you're going with any of that. All of the things you mention should just work - you can have mountpoints of different kinds in the same tree - mounting a tmpfs to an ecryptfs shouldn't be any issue at all. Replacing a mount-point w/ a symlink won't give anyone else access to the mountpoint if its mode is 0700. And it will scale just as well as you can script it - it's a simple script, though, just comparing lines in fstab to registered ~. You can get more elaborate of course, but I don't see the need. – mikeserv Mar 25 '15 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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