天天看点

4.5. File Access Permissions

4.5. File Access Permissions

"All the file types (directories, character special files, and so on) have permissions. Many people think only of regular files as having access permissions."

Access permissions for a directory

Read permission: lets us read the directory, obtaining a list of all the filenames in the directory. Command ls(1) only cares about read permission.

Write permission:

Execute permission: lets us pass through the directory when it is a component of a pathname that we are trying to access. Command cd only cares about execute  permission.

Access permissions for a file

Read permission: determines whether we can open an existing file for reading: the O_RDONLY and O_RDWR flags for the open function.

Write permission: determines whether we can open an existing file for writing: the O_WRONLY and O_RDWR flags for the open function.

Execute permission: if we want to execute the file using any of the six exec functions. The file also has to be a regular file.

Two typical operations

Create a new file in a directory: both write permission and execute permission (wx) in the directory containing the file. We do not need read permission or write permission for the file itself.

Figure 1.1

Delete an existing file in a directory: both write permission and execute permission (wx) in the directory containing the file. We do not need read permission or write permission for the file itself.

Figure 1.2

-------------------------------------------------------------------------------------------

The owners of a file: owner, group owner (i_uid, i_gid; st_uid, st_gid)

[important]The difference between the two owner IDs (use ID of owner and group ID of group owner) and the two real IDs associated with a process.

The two owner IDs are properties of the file, whereas the two real IDs, two effective IDs and the supplementary group IDs are properties of the process.

-------------------------------------------------------------------------------------------

The tests (when a process accesses a file) performed by the kernel are as follows.

NOTE: 1.These four steps are tried in sequence. 2. Only cares about effective IDs of the process.

1. If the effective user ID of the process is 0 (the superuser), access is allowed. This gives the superuser free rein throughout the entire file system.

2. If the effective user ID of the process equals the owner ID of the file (i.e., the process owns the file), access is allowed if the appropriate user access permission bit is set. Otherwise, permission is denied. By appropriate access permission bit, we mean that if the process is opening the file for reading, the user-read bit(r) must be on. If the process is opening the file for writing, the user-write bit (w) must be on. If the process is executing the file, the user-execute bit(x) must be on. (Like rwx------).

3. If the effective group ID of the process or one of the supplementary group IDs of the process equals the group ID of the file, access is allowed if the appropriate group access permission bit is set. Otherwise, permission is denied.

4. If the appropriate other access permission bit is set, access is allowed. Otherwise, permission is denied.

if( effective user ID of the process == 0 )

else if( (effective user ID of the process == user ID of owner) && (owner access permission bits are appropriately set) )

else if( (effective group ID of the process == group ID of group owner) || (one of the supplementary group IDs of the process == group ID of group owner))

else if( other access permission bits are appropriately set )

Permission denied.

effective user ID->effective group ID->supplementary group IDs

This is why we said the 3 IDs are all used for access permission checks.

"Note that if the process owns the file (step 2), access is granted or denied based only on the user access permissions; the group permissions are never looked at. Similarly, if the process does not own the file, but belongs to an appropriate group, access is granted or denied based only on the group access permissions; the other permissions are not looked at."

继续阅读