Sunday, September 4, 2011

How does linux system set permission of files and directories while it uses default mask?

Ans : Kernel system call(open and mkdir) passes mode/permission 0666 to file and 0777 to directory during creation of file or directory. These are default value. As per value of umask it calculates permission by doing NOT AND logic operation. I shall describe how permissions are set. As per this mode, file won't get execution any time but directory will get.

Bash and console program uses 666 for file and 777 for directory. To confirm this I have analysed one umask value and calculated exact permission of file and directory.

Lets say we set umask 0007 at console.

Analysis for FILE : Here umask=0007 (set umask like :# umask 007):

Note : "Resultant permissions are calculated via the bitwise AND of the unary complement of the argument (using bitwise NOT) and the permissions specified by the program. Bash uses 666 for files, and 777 for directories. Remember that permission to execute a directory means being able to list it."

Example :

666 = 110 110 110 //since console uses 666 for file
007= 000 000 111 //(for NOT AND, bit will be reversed and anded)
AND = 000 000 110 = 006
NOTAND= 110 110 000 = 660=rw-rw----
rwx rwx rwx

Analysis For DIR : Here umask=0007, bash,console use 666 for its file, use 777 for directory. So directory will get 770 as calculated it here.

777 = 111 111 111
007 = 000 000 111 (for NOT AND bit will be reversed and anded)
AND = 000 000 000 = 000
NOT_AND 111 111 000 = 770 = drwxrwx---
rwx rwx rwx

Testing :

[root@vm46 log]# umask 0007
[root@vm46 log]# mkdir test123
[root@vm46 log]# touch hello
[root@vm46 log]# ls -ld test123
drwxrwx--- 2 root root 4096 Aug 31 20:31 test123
[root@vm46 log]# ls -al hello
-rw-rw---- 1 root root 0 Aug 31 20:31 hello
[root@vm46 log]# umask
0007
[root@vm46 log]#

So, example shows directory got drwxrwx--- and file got -rw-rw---- . This confirms above logic analysis.

No comments:

Post a Comment