Set User ID and Set Group ID

    There are 6 or more IDs associated with each process, as shown in the following table:
ID class illustrate
Real User ID and Real Group ID who we actually are
Effective User ID, Effective Group ID, and Affiliate Group ID For file access permission check
Saved Settings User ID and Saved Settings Group ID Saved by the exec function

    Where:
    (1) The real user ID and real group ID identify who we really are. These two fields are taken from the login entry in the password file at login time. Normally, these values ​​do not change during a login session, but there are ways for the superuser process to change them.
    (2) The effective user ID, effective group ID and subgroup ID determine our file access rights. Typically, the effective user ID and effective group ID are equal to the real user ID and real group ID.
    (3) The saved setup user ID and the saved setup group ID contain copies of the effective user ID and effective group ID when a program is executed.
    Each file has an owner and group owner, specified by st_uid and st_gid in the stat structure, respectively. Typically, when a program file is executed, the effective user ID and effective group ID are equal to the real user ID and real group ID. But you can set a special flag in st_mode in the stat structure, which means "when executing this file, set the effective user ID of the process to the user ID (st_uid) of the owner of the file". Similarly, another bit in st_mode can be set to change the effective group ID of the process executing the file to the file's group owner ID (st_gid). These two bits in the file mode word (st_mode) are called the set user ID bit and the set group ID bit, and can be tested with the constants S_ISUID and S_ISGID, respectively.
    For example, if the file owner is superuser and the file's set user ID bit is set, then when a process executes the file, the process has superuser privileges, regardless of the process' actual user ID. The passwd command program is a set user ID program because it allows any user process to write new passwords into the /etc/passwd or /etc/shadow password files that only superusers have write access to. Because processes that run programs that set a user ID typically receive additional privileges, special care must be taken when writing such programs.
    The st_mode value also contains the access permission bits for the file. All file types have access rights, each file has 9 access rights bits, which can be divided into 3 categories, as shown in the following table:

    The 3 types of access rights in the table (ie read, write and execute) are used in various ways by different functions , these methods are summarized as follows:
    1. When opening a file of any type with a name, each level of directory contained in the name, including the current working directory it may imply, should have execute permission. This is why the execute permission bit for a directory is often referred to as the search bit. For example, in order to open the file /usr/include/stdio.h, execute permission is required on the directories /, /usr, and /usr/include. Then, you need to have the appropriate permissions on the file itself. If the current directory is /usr/include, then execute permission is required on the current directory, which is an example of implying the current directory. Another example of referring to an implied directory is if the PATH environment variable specifies a directory that we do not have execute permission to, the shell will never find the executable in that directory. Note that read permissions and execute permissions on a directory have different meanings. Read permission allows us to read a directory and get a list of all filenames in that directory; execute permission allows us to go through the directory (that is, search the directory for a characteristic filename).
    2. The read permission of a file determines whether we can open the file for read operation, which is related to the O_RDONLY and O_RDWR flags of the open function.
    3. The write permission to a file determines whether we can open the file for writing, which is related to the O_WRONLY and O_RDWR flags of the open function.
    4. The O_TRUNC flag can be specified in the open function only if the file has write permission.
    5. To create a new file in a directory, you must have write and execute permissions on the directory.
    6. To delete a file, you must have write permission and execute permission on the directory containing the file, regardless of whether the file itself has read or write permission.
    7. If you want to execute a file with any one of the seven exec functions, you must have execute permission on the file, and the file must also be a normal file.
    Every time a process creates, opens, or deletes a file, the kernel performs a file access permission test, which may involve the file's owner (st_uid and st_gid), the process's effective ID (effective user ID and effective group ID), and The affiliation group ID of the process (if supported). The two owner IDs are properties of the file, while the two effective IDs and the affiliate group ID are properties of the process. The tests performed by the kernel are as follows:
    1. If the effective user ID of the process is 0 (superuser), access is allowed. This gives the superuser maximum freedom to work with the entire filesystem.
    2. If the effective user ID of the process is equal to the owner ID of the file (that is, the process owns the file), then access is allowed if the owner's appropriate access permission bits are set; otherwise, access is denied. The appropriate access bit means that if the process opens the file for reading, the user read bit should be 1; if it is opened for writing, the user write bit should be 1; if the process will execute the file, the user executes bit should be 1.
    3. If the process's effective group ID or one of the process's subgroup IDs is equal to the file's group ID, then access is permitted if the appropriate access permission bits for the group are set; otherwise, access is denied.
    4. If the appropriate access permission bits of other users are set, the access is allowed; otherwise, the access is denied.
    For a new file or directory, its user ID is set to the effective user ID of the process, and the group ID is set to the effective group ID of the process or the group ID of the directory it is in (FreeBSD 8.0 and Mac OS X 10.6.8 Always use the group ID of the directory as the group ID for new files. Some Linux filesystems allow selection with the mount command option. For Linux 3.2.0 and Solaris 10, by default, the group ID of a new file depends on where it is located Whether the directory's set group ID bit is set. If this bit is set, the new file's group ID is set to the directory's group ID; otherwise, it is set to the process's effective group ID).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326229833&siteId=291194637