Hacking - The Art of Exploitation, 2nd Edition

(Romina) #1
Programming 87

0x282 File Permissions


If the O_CREAT flag is used in access mode for the open() function, an additional


argument is needed to define the file permissions of the newly created file.


This argument uses bit flags defined in sys/stat.h, which can be combined


with each other using bitwise OR logic.


S_IRUSR Give the file read permission for the user (owner).


S_IWUSR Give the file write permission for the user (owner).


S_IXUSR Give the file execute permission for the user (owner).


S_IRGRP Give the file read permission for the group.


S_IWGRP Give the file write permission for the group.


S_IXGRP Give the file execute permission for the group.


S_IROTH Give the file read permission for other (anyone).


S_IWOTH Give the file write permission for other (anyone).


S_IXOTH Give the file execute permission for other (anyone).


If you are already familiar with Unix file permissions, those flags should


make perfect sense to you. If they don’t make sense, here’s a crash course in


Unix file permissions.


Every file has an owner and a group. These values can be displayed using


ls -l and are shown below in the following output.


reader@hacking:~/booksrc $ ls -l /etc/passwd simplenote*
-rw-r--r-- 1 root root 1424 2007-09-06 09:45 /etc/passwd
-rwxr-xr-x 1 reader reader 8457 2007-09-07 02:51 simplenote
-rw------- 1 reader reader 1872 2007-09-07 02:51 simplenote.c
reader@hacking:~/booksrc $


For the /etc/passwd file, the owner is root and the group is also root. For


the other two simplenote files, the owner is reader and the group is users.


Read, write, and execute permissions can be turned on and off for three


different fields: user, group, and other. User permissions describe what the


owner of the file can do (read, write, and/or execute), group permissions


describe what users in that group can do, and other permissions describe


what everyone else can do. These fields are also displayed in the front of the


ls -l output. First, the user read/write/execute permissions are displayed,


using r for read, w for write, x for execute, and - for off. The next three


characters display the group permissions, and the last three characters are


for the other permissions. In the output above, the simplenote program has


all three user permissions turned on (shown in bold). Each permission cor-


responds to a bit flag; read is 4 (100 in binary), write is 2 (010 in binary), and


execute is 1 (001 in binary). Since each value only contains unique bits,


a bitwise OR operation achieves the same result as adding these numbers


together does. These values can be added together to define permissions for


user, group, and other using the chmod command.

Free download pdf