martes, 25 de diciembre de 2007

Sticky Bit

El permiso sticky bit (t) nos sirve para establecer que un determinado usuario solo pueda borrar los archivos que le pertenecen en un directorio.

Comúnmente en los ambientes de programación y desarrollo necesitamos que varios usuarios trabajen sobre el mismo directorio, pero como podemos hacer para mantener la integridad y que puedan manejar sus archivos sobre el mismo directorio.

Con sticky bit podemos establecer esto, teniendo así que ningún usuario podrá borrar ficheros que pertenezcan a algún otro usuario, veamos como hacer esto.

Primero echemos un vistazo a los permisos del directorio en cuestión (prueba)

rooter@rooterlap:~$ ls -l

total 188

drwx------ 3 rooter users 4096 2007-12-17 15:03 Desktop

drwxr-xr-x 2 rooter users 4096 2007-10-03 21:06 Documents

drwxr-xr-x 3 rooter users 4096 2007-10-13 14:27 coding

drwxrwxrwx 3 rooter users 4096 2007-12-25 14:21 prueba

drwxr-xr-x 5 rooter users 4096 2007-12-17 14:51 security exposed

rooter@rooterlap:~$ cd prueba

rooter@rooterlap:~prueba$ ls -l

total 8

-rw-r--r-- 1 root root 0 2007-12-25 14:14 bit.txt

-rw-r--r-- 1 tux users 0 2007-12-25 14:21 carta.txt

-rwxr--r-- 1 rooter users 18 2007-10-28 13:15 file

-rw-r--r-- 1 root root 0 2007-12-25 14:14 hola.txt

drwxr-xr-x 2 rooter users 4096 2007-12-17 15:49 homie

-rw-r--r-- 1 tux users 0 2007-12-25 14:21 libro.txt

-rw-r--r-- 1 root root 0 2007-12-25 14:15 shellcode.txt

Aquí podemos ver que en el directorio de prueba hay varios archivos que pertenecen a 3 usuarios en especificos (root, rooter y tux).

Pero que tal si el usuario tux decide borrar algunos archivos, veamos.

tux@rooterlap:/home/rooter/prueba$ rm carta.txt file hola.txt

rm: remove write-protected regular file `file'? y

rm: remove write-protected regular empty file `hola.txt'? y

tux@rooterlap:/home/rooter/prueba$ ls -l

total 4

-rw-r--r-- 1 root root 0 2007-12-25 14:14 bit.txt

drwxr-xr-x 2 rooter users 4096 2007-12-17 15:49 homie

-rw-r--r-- 1 tux users 0 2007-12-25 14:21 libro.txt

-rw-r--r-- 1 root root 0 2007-12-25 14:15 shellcode.txt

Con esto el usuario tux borro el archivo carta.txt que le pertenecía a él, pero no solo eso también borro el archivo file que era del usuario rooter, pero lo peor de todo es que pudo borrar incluso el archivo hola.txt que era propiedad del usuario root, como podemos ver esto es algo muy peligroso, que nos puede llegar a ocurrir si no tenemos los permisos adecuados.

Ahora veamos que pasa al establecer el permiso del sticky bit con chmod

rooter@rooterlap:~$ chmod +t prueba/

rooter@rooterlap:~$ ls -l

total 188

drwx------ 3 rooter users 4096 2007-12-17 15:03 Desktop

drwxr-xr-x 2 rooter users 4096 2007-10-03 21:06 Documents

drwxr-xr-x 3 rooter users 4096 2007-10-13 14:27 coding

drwxrwxrwt 3 rooter users 4096 2007-12-25 14:30 prueba

drwxr-xr-x 5 rooter users 4096 2007-12-17 14:51 security exposed


El ultimo carácter del directorio prueba cambio de x a t, esto quiere decir que el sticky bit fue establecido.

Ahora veamos otro intento del usuario tux para borrar algunos archivos

tux@rooterlap:/home/rooter/prueba$ ls -l

total 4

-rw-r--r-- 1 root root 0 2007-12-25 14:14 bit.txt

drwxr-xr-x 2 rooter users 4096 2007-12-17 15:49 homie

-rw-r--r-- 1 tux users 0 2007-12-25 14:40 libro.txt

-rw-r--r-- 1 rooter users 0 2007-12-25 14:40 php.txt

-rw-r--r-- 1 root root 0 2007-12-25 14:15 shellcode.txt


tux@rooterlap:/home/rooter/prueba$ rm libro.txt

tux@rooterlap:/home/rooter/prueba$ rm php.txt

rm: remove write-protected regular empty file `php.txt'? y

rm: cannot remove `php.txt': Operation not permitted

tux@rooterlap:/home/rooter/prueba$ rm shellcode.txt

rm: remove write-protected regular empty file `shellcode.txt'? y

rm: cannot remove `shellcode.txt': Operation not permitted


tux@rooterlap:/home/rooter/prueba$ ls -l

total 4

-rw-r--r-- 1 root root 0 2007-12-25 14:14 bit.txt

drwxr-xr-x 2 rooter users 4096 2007-12-17 15:49 homie

-rw-r--r-- 1 rooter users 0 2007-12-25 14:40 php.txt

-rw-r--r-- 1 root root 0 2007-12-25 14:15 shellcode.txt

Nuestro usuario tux pudo borrar el archivo libro.txt ya que era de su propiedad, pero ahora no pudo borrar ningún otro archivo perteneciente a otro usuario.

Así que debemos de establecer los permisos adecuados para tener un buen uso de los ficheros en el sistema.