Monday, March 10, 2008

Securing tmp

What we are doing it creating a file that we will use to mount at /tmp.
CODE
cd /dev
Create 100MB file for our /tmp partition. If you need more space, make count size larger.
CODE
dd if=/dev/zero of=tmpMnt bs=1024 count=100000
Make an extended filesystem for our tmpMnt file
CODE
mke2fs /dev/tmpMnt
Backup your /tmp dir- I had mysql.sock file that I needed to recreate the symbolic link for. Other programs may use it to store cache files or whatever.
CODE
cd /
CODE
cp -R /tmp /tmp_backup
Mount the new /tmp filesystem with noexec
CODE
mount -o loop,noexec,nosuid,rw /dev/tmpMnt /tmp
CODE
chmod 0777 /tmp
Copy everything back to new /tmp and remove backup
CODE
cp -R /tmp_backup/* /tmp/
CODE
rm -rf /tmp_backup Now we need to add this to fstab so it mounts automatically on reboots.
CODE
pico -w /etc/fstab You should see something like this:
CODE
/dev/hda3 / ext3 defaults,usrquota 1 1
/dev/hda1 /boot ext3 defaults 1 2
none /dev/pts devpts gid=5,mode=620 0 0
none /proc proc defaults 0 0
none /dev/shm tmpfs defaults 0 0
/dev/hda2 swap swap defaults 0 0
At the bottom add
CODE
/dev/tmpMnt /tmp ext2 loop,noexec,nosuid,rw 0 0
(Each space is a tab)
Ctrl + X and Y
Your done- /tmp is now mounted as noexec. You can sleep a little bit safer tonight. I created a hello world c++ and compiled it then moved it to /tmp. Upon trying to run it (even chmod +x'ed), it gives the following error:
CODE
bash: ./a.out: Permission denied
YEAH! /tmp no longer has execute permissions

No comments: