by Joker_vD · 2y ago · 0 comments
Something like
tmpdir := "/tmp/${USERNAME}"
loop:
rmdir(tmpdir, recurse=true)
while not mkdir(tmpdir, 0o700, must-create=true)
chown(tmpdir, user=$USERNAME, group=$USERGROUP)
export("TMPDIR", tmpdir)
with /tmp having root:root owner with 0o775 permissions on it? Yeah, would've been nice.https://magnusviri.com/what-is-var-folders.html
That's not the reason for /private though. Rather, /private is a holdover from NeXTSTEP days which could mount the OS via NFS (NetBoot), and where /private was local to the machine:
"Each NetBoot client will share the server's root file system, but there are several administrative files (such as the NetInfo database, log files, and the swapfile) that must be unique to each client. The server must have a separate directory tree for each client, which the client mounts on its own /private directory during startup. This lets a client keep its own files separate from those of other clients."
https://www.nextcomputers.org/files/manuals/nsa/13_NetBoot.h...
$HOME/.tmp for user operations and /tmp for system operations?
EDIT: I see from other posters it can be done. Why the heck isn't this the default?!
In other words, for many systems, a home-level temp directory is virtually the same as /tmp anyway since other than system daemons, all applications are being started as a single user anyway.
And that might be a security regression. For servers you're spinning up most services at bootup and those should either be running fully sandboxed from each other (containerization) or at least as separate system users.
But malware doesn't necessarily need root, or a daemon process user id to inflict harm if it's running as the human user's id and all temp files are in $HOME/.tmp.
What you really want is transient application-specific disk storage that is isolated to the running process and protected, so that any malware that tries to attack another running application's temp files can't since they don't have permission even when both processes are running under the same user id.
At that point malware requires privilege escalation to root first to be able to attack temp files. And again, if we're talking about a server, you're better off running your services in sandboxes when you can because then even root privilege escalation limits the blast radius.
In these systems, the responsibility passes to EDRs or similar. But neither a $HOME/.tmp or /tmp matter in these scenarios. _Shared_ systems are where the concept of $HOME/.tmp might be more interesting.
Very true, and this is a real weakness of the UNIX (and Windows, even worse!) style security model in the modern environment. Android/iOS do a lot better.
They would if they were designed with the user's security in mind, instead of Google's/Apple's control.
But I disagree, they don't do better at all. Any software that wants to get access to everything just needs to insist.
In principle, there's nothing precluding e.g. having a separate user per app on Linux, either...
If the user tmp files were placed in /tmp/${USER}/ then that would achieve the same goal.
Not to say they couldn't have one!
Anything that requires login(8) or PAM to make it happen is insufficient. This has to happen in environments like Kubernetes too.
joker@e2509h:~/test_tmp$ ll
total 12K
drwxr-xr-x 3 joker joker 4.0K Oct 22 22:12 ./
drwxr-x--- 11 joker joker 4.0K Oct 22 22:12 ../
drwxr-xr-x 3 root root 4.0K Oct 22 22:13 tmp/
joker@e2509h:~/test_tmp$ cd tmp
joker@e2509h:~/test_tmp/tmp$ ll
total 12K
drwxr-xr-x 3 root root 4.0K Oct 22 22:13 ./
drwxr-xr-x 3 joker joker 4.0K Oct 22 22:12 ../
drwxr-xr-x 2 joker joker 4.0K Oct 22 22:13 joker/
-rw-r--r-- 1 root root 0 Oct 22 22:15 z
joker@e2509h:~/test_tmp/tmp$ touch x
touch: cannot touch 'x': Permission denied
joker@e2509h:~/test_tmp/tmp$ rm z
rm: remove write-protected regular empty file 'z'? y
rm: cannot remove 'z': Permission denied
joker@e2509h:~/test_tmp/tmp$ touch joker/x
joker@e2509h:~/test_tmp/tmp$ ll joker
total 8.0K
drwxr-xr-x 2 joker joker 4.0K Oct 22 22:13 ./
drwxr-xr-x 3 root root 4.0K Oct 22 22:15 ../
-rw-r--r-- 1 joker joker 0 Oct 22 22:13 x
joker@e2509h:~/test_tmp/tmp$ rm joker/x
joker@e2509h:~/test_tmp/tmp$
Looks like it works just fine.