tags:

views:

176

answers:

3

I am reading some source code and I found at the very begin of the main routine this statement:

umask(077);

What could be the reason for that?

The man page (man 2 umask) states:

umask -- set file creation mode mask

This clearing allows each user to restrict the default access to his files

But is not clear to me why would do that? as a shortcut ?

+4  A: 

It needs for file system security. umask contains inverted number, using as file mode for new file. For example

dzen@DZeN ~ $ umask
022
dzen@DZeN ~ $ touch file
dzen@DZeN ~ $ ls -la file
-rw-r--r--  1 dzen  dzen  0  6 may 14:29 file
dzen@DZeN ~ $ umask 777
dzen@DZeN ~ $ umask      
0777
dzen@DZeN ~ $ touch file1
dzen@DZeN ~ $ ls -la file1
----------  1 dzen  dzen  0  6 may 14:30 file1
DZeN
+6  A: 

Setting umask(077) ensures that any files created by the program will only be accessible to their owner (0 in first position = all permissions potentially available) and nobody else (7 in second/third position = all permissions disallowed to group/other).

Dave Sherohman
You would expect to see this setting in a program which is generating highly sensitive files that should *never* be accessed by any other user, in any situation (`ssh-keygen` is a good example). Normal programs should respect the existing `umask()` that the user has set.
caf
A: 

You should definitely read a lot of linux basics: UNIX / Linux: Beginners Guide to File and Directory Permissions ( umask, chmod, read, write, execute )

thegeek