Penguin
Blame: PerUserTempDirs
EditPageHistoryDiffInfoLikePages
Annotated edit history of PerUserTempDirs version 13, including all changes. View license author blame.
Rev Author # Line
8 PhilMurray 1 This trick is for multiuser boxes to try and ameliorate issues with people creating insecure temporary files (and to make it obvious which applications don't respect TMPDIR). The idea is to create a seperate directory for every user on the machine that's 700 to that user and point TMPDIR at it. I think that this idea could(/should?) be used by default by distributions. This can be extended to work for other services (eg apache).
5 PerryLorier 2
3 Points for:
7 MikeBeattie 4 * It helps to protect against abusers with exploited non-root services from leveraging a tmpfile exploit to gain a users account.
5 PerryLorier 5 * It reduces the effects of /tmp becoming large and therefore slow to search.
6
7 The first part of the script goes in /etc/skel/.bash_profile before you create user accounts:
13 DanielLawson 8 <verbatim>
5 PerryLorier 9 # Set up each users own temp directory (kinda)
7 MikeBeattie 10 mkdir -p /tmp/$USER/create.$$ 2>/dev/null
13 DanielLawson 11 if [ -O /tmp/$USER ]; then
5 PerryLorier 12 TMPDIR=/tmp/$USER
13 else
14 TMPDIR=$(mktemp -d /tmp/${USER}.XXXXXX)
15 fi
16
17 touch $TMPDIR/.bash.$$
13 DanielLawson 18 [ -d $TMPDIR/create.$$ ] && rmdir $TMPDIR/create.$$
5 PerryLorier 19
20 TMP=$TMPDIR
21 TEMP=$TMPDIR
22
23 export TMPDIR TMP TEMP
13 DanielLawson 24 </verbatim>
5 PerryLorier 25
26 Then in .bash_logout:
12 DanielLawson 27 <pre>
5 PerryLorier 28 rm $TMPDIR/.bash.$$
29 rmdir $TMPDIR 2>/dev/null
12 DanielLawson 30 </pre>
5 PerryLorier 31
32 This means that it will remove the directory when the last shell is closed and there are no more files in the directory.
7 MikeBeattie 33
34 The reason for creating then deleting $TMPDIR/create.$$, as some may wonder, is to make the mkdir an atomic operation that should stop any shell that is logging out as you log in, from removing $TMPDIR before a file is created within it ($TMPDIR/.bash.$$)
5 PerryLorier 35
11 StuartYeates 36 If users want scratch space to copy files between users etc, they can use /tmp directly as LinuxFromScratch suggests, however conformant programs should use TMPDIR which now places the files in /tmp/''username'' or /tmp/''username''.''uniqueid''.
6 PerryLorier 37
38 The current flaw with this script is that it doesn't detect if /tmp/username and all the possible /tmp/username.''uniqueid''s have already been created by an attacker.
5 PerryLorier 39
40 Alternatively the first script can be placed in /etc/profile.d/ and the .bash_logout script can be ignored and the directories can be removed regularly from cron if necessary.
41
42 Ideally Linux could transparently produce a unique /tmp/ for each $USER on the system, but that would break LinuxStandardsBase compiliance and many applications.
9 AristotlePagaltzis 43
44 ----
45
46 No no no no. Don't have the users fiddling things. That's prone to problems, since you need to grant them permissions to be able to do that. The right way is to have the Hand Of God set things up for the users in a directory structure they have no permissions in. Basic setup:
47
12 DanielLawson 48 <pre>
9 AristotlePagaltzis 49 mkdir -m 711 /tmp-safe /tmp-safe/user
50 mkdir -m 1777 /tmp-safe/global
51 chown root.root /tmp-safe /tmp-safe/user /tmp-safe/global
52 ln -s /tmp /tmp-safe/global
12 DanielLawson 53 </pre>
9 AristotlePagaltzis 54
55 Now once you have that in place, invoke the following script instead of login(1):
56
12 DanielLawson 57 <pre>
9 AristotlePagaltzis 58 #!/bin/sh
59 # FIXME: assumes $1 == username, but login(1) takes options, so parse with getopts
60 mkdir -m 700 /tmp-safe/user/"$1" /tmp/"$1"
61 chown "$1": /tmp-safe/user/"$1" /tmp/"$1"
62 mount --bind /tmp-safe/user/"$1" /tmp/"$1" || exit 1
63 exec /bin/login "$@"
12 DanielLawson 64 </pre>
9 AristotlePagaltzis 65
10 AristotlePagaltzis 66 Now /tmp and /tmp/$USER have nothing whatsoever to do with each other, and since the latter is merely a mountpoint, whatever permissions a preexisting directory at that location might have had doesn't matter in the slightest. You can have a process cd'ed to /tmp/$USER sitting in the background as long as the user is logged in. If unmounting the bind succeeds, you can delete /tmp-safe/user/$USER
9 AristotlePagaltzis 67
10 AristotlePagaltzis 68 Personally I might leave out the /tmp/$USER thing entirely and just point TMPDIR to /tmp-safe/user/$USER. (Do the simplest thing that could possibly work.) You'll have to have cron periodically vacuum the place then of course.
9 AristotlePagaltzis 69
70 --AristotlePagaltzis
5 PerryLorier 71
72 ----
73 CategorySecurity