21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

efore creating the file or to use fchmod( ) to change the permissions after the file is<br />

created.<br />

In most cases, you’ll be attempting to loosen restrictions, but consider what happens<br />

when fopen( ) is used to create a new file. The fopen( ) function provides no way to<br />

specify the permissions to use for the new file, and it always uses 0666, which grants<br />

read and write access to the owning user, the owning group, and everyone else.<br />

Again, the only way to modify this behavior is either to set the umask before calling<br />

fopen( ) or to use fchmod( ) after the file is created.<br />

Using fchmod( ) to change the permissions of a file after it is created is not a good<br />

idea because it introduces a race condition. Between the time the file is created and<br />

the time the permissions are modified, an attacker could possibly gain unauthorized<br />

access to the file. The proper solution is therefore to modify the umask before creating<br />

the file.<br />

Properly using umasks in your program can be a bit complicated, but here are some<br />

general guidelines:<br />

• If you are creating files that contain sensitive data, always create them readable<br />

and writable by only the file owner, and deny access to group members and all<br />

other users.<br />

• Be aware that files that do not contain sensitive data may be readable by other<br />

users on the system. If the user wants to stop this behavior, the umask can be set<br />

appropriately before starting your program.<br />

• Avoid setting execute permissions on files, especially group and world execute. If<br />

your program generates files that are meant to be executable, set the execute bit<br />

only for the file owner.<br />

• Create directories that may contain files used to store sensitive information such<br />

that only the owner of the directory has read, write, and execute permissions for<br />

the directory. This allows only the owner of the directory to enter the directory<br />

or view or change its contents, but no other users can view or otherwise access<br />

the directory. (See the discussion of secure directories in Recipe 2.4 for more<br />

information on the importance of this requirement.)<br />

• Create directories that are not intended to store sensitive files such that the<br />

owner has read, write, and execute permissions, while group members and<br />

everyone else has only read and execute permissions. If the user wants to stop<br />

this behavior, the umask can be set appropriately before starting your program.<br />

• Do not rely on setting the umask to a “secure” value once at the beginning of the<br />

program and then calling all file or directory creation functions with overly permissive<br />

file modes. Explicitly set the mode of the file at the point of creation.<br />

There are two reasons to do this. First, it makes the code clear; your intent concerning<br />

permissions is obvious. Second, if an attacker managed to somehow<br />

56 | Chapter 2: Access Control<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!