2

I am writing a simple code about opening a file with O_CREAT | O_WDONLY | O_EXCL flags. The code looks like this:

int fd = open(fileName, O_WRONLY | O_CREAT | O_EXCL, 0777);

It works well, except for the mode (permission) part. What ever I put in the others part, it always creates a file with r-x.

For example, the permission of the file created from the above code looks like this.

rwxrwxr-x.

This isn't 777 isn't it ? What am I missing ?

5

Bits in the umask of the current process are cleared when the file is created. In this case, it looks like the umask is 0002 (no world write). Call umask() before calling open() to change that.

Soapbox: please don't create world-writable files unless you're really sure that's what you want. umask exists to save you and your users from exactly that type of vulnerability.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.