|
| 以 PDF 格式下载本书 (2413 KB)
Chapter 16 Securing Files (Tasks)This chapter describes the procedures for securing files in the Solaris environment. This is a list of the step-by-step instructions in this chapter. File Security FeaturesThis section describes the features that constitute a file's security. User ClassesFor each file, there are three classes of users that specify the levels of security:
Only the owner of the file or root can assign or modify file permissions. File PermissionsThe following table lists and describes the permissions that you can give to each user class for a file. Table 16–1 File Permissions
These file permissions apply to special files such as devices, sockets, and named pipes (FIFOs), as they do to regular files. For a symbolic link, the permissions that apply are the permissions of the file that the link points to. Directory PermissionsThe following table lists and describes the permissions that you can give to each user class for a directory. Table 16–2 Directory Permissions
You can protect the files in a directory (and in its subdirectories) by disallowing access to that directory by setting restrictive file permissions. Note, however, that superuser has access to all files and directories on the system. Special File Permissions (setuid, setgid and Sticky Bit)Three special types of permissions are available for executable files and public directories. When these permissions are set, any user who runs that executable file assumes the user ID of the owner (or group) of the executable file. You must be extremely careful when you set special permissions, because special permissions constitute a security risk. For example, a user can gain superuser privileges by executing a program that sets the user ID (UID) to root. Also, all users can set special permissions for files they own, which constitutes another security concern. You should monitor your system for any unauthorized use of the setuid and setgid permissions to gain superuser privileges. To search for and list all of the files that use these permissions, see How to Find Files With setuid Permissions. A suspicious listing grants ownership of such a program to a user rather than to root or bin. setuid PermissionWhen set-user identification (setuid) permission is set on an executable file, a process that runs this file is granted access based on the owner of the file (usually root), rather than the user who is running the executable file. This special permission allows a user to access files and directories that are normally only available to the owner. For example, the setuid permission on the passwd command makes it possible for a user to change passwords, assuming the permissions of the root ID:
This special permission presents a security risk, because some determined users can find a way to maintain the permissions that are granted to them by the setuid process even after the process has finished executing. Note – The use of setuid permissions with the reserved UIDs (0–100) from a program might not set the effective UID correctly. Use a shell script instead or avoid using the reserved UIDs with setuid permissions. setgid PermissionThe set-group identification (setgid) permission is similar to setuid, except that the process's effective group ID (GID) is changed to the group owner of the file, and a user is granted access based on permissions granted to that group. The /usr/bin/mail command has setgid permissions:
When setgid permission is applied to a directory, files that were created in this directory belong to the group to which the directory belongs, not the group to which the creating process belongs. Any user who has write and execute permissions in the directory can create a file there. However, the file belongs to the group that owns the directory, not to the user's group ownership. You should monitor your system for any unauthorized use of the setuid and setgid permissions to gain superuser privileges. To search for and list all of the files that use these permissions, see How to Find Files With setuid Permissions. A suspicious listing grants group ownership of such a program to a user rather than to root or bin. Sticky BitThe sticky bit is a permission bit that protects the files within a directory. If the directory has the sticky bit set, a file can be deleted only by the owner of the file, the owner of the directory, or by root. This special permission prevents a user from deleting other users' files from public directories such as /tmp:
Be sure to set the sticky bit manually when you set up a public directory on a TMPFS file system. Default umask SettingWhen you create a file or directory, it has a default set of permissions. These default permissions are determined by the umask setting in the /etc/profile file, or in your .cshrc or .login file. By default, the system sets the permissions on a text file to 666, which grants read and write permission to user, group, and others, and to 777 on a directory or executable file. The value assigned by the umask command is subtracted from the default. This process has the effect of denying permissions in the same way that the chmod command grants them. For example, while the chmod 022 command grants write permission to group and others, the umask 022 command denies write permission for group and others. The following table shows some typical umask settings, and the effect on an executable file. Table 16–3 umask Settings for Different Security Levels
For more information on setting the umask value, see the umask(1) man page. Displaying File InformationThis section describes how to display file information. How to Display File InformationDisplay information about all the files in a directory by using the ls command.
Each line in the display has the following information about a file:
Example—Displaying File InformationThe following example displays the partial list of the files in the /sbin directory.
Changing File OwnershipThis section describes how to change the ownership and group ownership of a file. By default, the owner cannot use the chown command to change the owner of a file or directory. However, you can enable the owner to use the chown command by adding the following line to the system's /etc/system file and rebooting the system.
For more information, see chown(1). In addition, the owner can only use the chgrp command to change the group of a file to a group in which the owner belongs by default. For example, if the owner of a file only belongs to the staff and sysadm groups, the owner can only change the group of a file to staff or sysadm group. However, you can enable the owner to change the group of a file to a group in which the owner doesn't belong by adding the following line to the system's /etc/system file and rebooting the system.
For more information, see chgrp(1). Also, be aware that there can be other restrictions on changing ownership and groups on NFS-mounted file systems. How to Change the Owner of a FileUse the following procedure to change the ownership of a file.
Example—Changing the Owner of a FileIn the following example, the ownership on myfile is changed to the user rimmer.
How to Change Group Ownership of a FileUse the following procedure to change the group ownership of a file.
Example—Changing Group Ownership of a FileIn the following example, the group ownership on myfile is changed to the group scifi.
Changing File PermissionsThe chmod command enables you to change the permissions on a file. You must be superuser or the owner of a file or directory to change its permissions. You can use the chmod command to set permissions in either of two modes:
The following table lists the octal values for setting file permissions in absolute mode. You use these numbers in sets of three to set permissions for owner, group, and other (in that order). For example, the value 644 sets read and write permissions for owner, and read-only permissions for group and other. Table 16–5 Setting File Permissions in Absolute Mode
You can set special permissions on a file in absolute or symbolic modes. However, you cannot set or remove setuid permissions on a directory by using absolute mode. You must use symbolic mode. In absolute mode, you set special permissions by adding a new octal value to the left of the permission triplet. The following table lists the octal values to set special permissions on a file. Table 16–6 Setting Special Permissions in Absolute Mode
The following table lists the symbols for setting file permissions in symbolic mode. Symbols can specify whose permissions are to be set or changed, the operation to be performed, and the permissions that are being assigned or changed. Table 16–7 Setting File Permissions in Symbolic Mode
The who operator permission designations in the function column specifies the symbols that change the permissions on the file or directory.
How to Change Permissions in Absolute ModeUse the following procedure to change permissions in absolute mode.
Example—Changing Permissions in Absolute ModeIn the following example, the permissions of a public directory are changed from 744 (read, write, execute; read-only; and read-only) to 755 (read, write, execute; read and execute; and read and execute).
In the following example, the permissions of an executable shell script are changed from read and write to read, write, and execute.
How to Change Special Permissions in Absolute ModeUse the following procedure to change special permissions in absolute mode.
Examples—Setting Special Permissions in Absolute ModeIn the following example, the setuid permission is set on the dbprog file.
In the following example, the setgid permission is set on the dbprog2 file.
In the following example, the sticky bit permission is set on the public_dir directory.
How to Change Permissions in Symbolic ModeUse the following procedure to change permissions in symbolic mode.
Examples—Changing Permissions in Symbolic ModeIn the following example, read permission are taken away from others.
In the following example, read and execute permissions are added for user, group, and others.
In the following example, read, write, and execute permissions are assigned to group.
Searching for Special PermissionsYou should monitor your system for any unauthorized use of the setuid and setgid permissions on programs to gain superuser privileges. A suspicious listing grants ownership of such a program to a user rather than to root or bin. How to Find Files With setuid PermissionsUse the following procedure to find files with setuid permissions.
Example—Finding Files With setuid Permissions
This output shows that a user named rar has made a personal copy of /usr/bin/sh, and has set the permissions as setuid to root. As a result, rar can execute /usr/rar/bin/sh and become the privileged user. If you want to save this output for future reference, move the file out of the /tmp directory. Executable Stacks and SecurityA number of security bugs are related to default executable stacks when their permissions are set to read, write, and execute. While stacks with execute permissions are allowed, most programs can function correctly without using executable stacks. The noexec_user_stack variable (available starting in the Solaris 2.6 release) enables you to specify whether stack mappings are executable. By default, the variable is set to zero, except on 64-bit applications, which provides ABI-compliant behavior. If the variable is set to non-zero, the system marks the stack of every process in the system as readable and writable, but not executable. Once this variable is set, programs that attempt to execute code on their stack are sent a SIGSEGV signal, which usually results in the program terminating with a core dump. Such programs also generate a warning message that includes the name of the offending program, the process ID, and the real UID of the user who ran the program. For example:
The message is logged by the syslog daemon when the syslog kern facility is set to notice level. This logging is set by default in the syslog.conf file, which means that the message is sent to both the console and to the /var/adm/messages file. For more information, see the syslogd(1M) and syslog.conf(4) man pages. This message is useful for observing potential security problems, as well as to identify valid programs that depend upon executable stacks that have been prevented from correct operation by setting this variable. If the administrator does not want any messages logged, then the noexec_user_stack_log variable can be set to zero to disable it in the /etc/system file, though the SIGSEGV signal can continue to cause the executing program to core dump. You can use mprotect if you want programs to explicitly mark their stack as executable. For more information, see the mprotect(2) man page. Because of hardware limitations, the capability of catching and reporting executable stack problems is only available on sun4m and sun4u platforms. How to Disable Programs From Using Executable Stacks
How to Disable Executable Stack Message Logging
Using Access Control Lists (ACLs)Traditional UNIX file protection provides read, write, and execute permissions for the three user classes: file owner, file group, and other. An ACL provides better file security by enabling you to define file permissions for the file owner, file group, other, specific users and groups, and default permissions for each of those categories. For example, if you wanted everyone in a group to be able to read a file, you would simply give group read permissions on that file. Now, assume you wanted only one person in the group to be able to write to that file. Standard UNIX doesn't provide that level of file security. However, this dilemma is perfect for ACLs. ACL entries are the way to define an ACL on a file, and they are set through the setfacl command. ACL entries consist of the following fields separated by colons:
The following example shows an ACL entry that sets read and write permissions for the user nathan.
UFS file system attributes such as ACLs are supported in UFS file systems only. Thus, if you restore or copy files with ACL entries into the /tmp directory, which is usually mounted as a TMPFS file system, the ACL entries will be lost. Use the /var/tmp directory for temporary storage of UFS files. ACL Entries for FilesThe following table lists the valid ACL entries that you might use when setting file ACLs. The first three ACL entries provide the basic UNIX file protection. Table 16–8 ACL Entries for Files
ACL Entries for DirectoriesIn addition to the ACL entries that are described in Table 16–8, you can set default ACL entries on a directory. Files or directories created in a directory that has default ACL entries will have the same ACL entries as the default ACL entries. Table 16–9 lists the default ACL entries for directories. When you set default ACL entries for specific users and groups on a directory for the first time, you must also set default ACL entries for the file owner, file group, others, and the ACL mask. These entries are required and are the first four default ACL entries in the following table. Table 16–9 Default ACL Entries for Directories
How to Set an ACL on a File
Examples—Setting an ACL on a FileIn the following example, the file owner permissions are set to read and write, file group permissions are set to read only, and other permissions are set to none on the ch1.doc file. In addition, the user george is given read and write permissions on the file, and the ACL mask permissions are set to read and write, which means that no user or group can have execute permissions.
In the following example, the file owner permissions are set to read, write, and execute, file group permissions are set to read only, other permissions are set to none, and the ACL mask permissions are set to read on the ch2.doc file. In addition, the user george is given read and write permissions. However, due to the ACL mask, the permissions for george are read only.
How to Copy an ACLCopy a file's ACL to another file by redirecting the getfacl output.
Example—Copying an ACLIn the following example, the ACL on ch2.doc is copied to ch3.doc.
How to Check If a File Has an ACLCheck if a file has an ACL by using the ls command.
filename specifies the file or directory. In the output, a plus sign (+) to the right of the mode field indicates that the file has an ACL. Note – Unless you have added ACL entries for additional users or groups on a file, a file is considered to be a “trivial” ACL and the plus sign (+) will not display. Example—Checking If a File Has an ACLThe following example shows that the ch1.doc file has an ACL, because the listing has a plus sign (+) to the right of the mode field.
How to Modify ACL Entries on a File
Examples—Modifying ACL Entries on a FileIn the following example, the permissions for the user george are modified to read and write.
In the following example, the default permissions for the group staff are modified to read and the default ACL mask permissions are modified to read and write on the book directory.
How to Delete ACL Entries From a File
Example—Deleting ACL Entries on a FileIn the following example, the user george is deleted from the ch4.doc file.
How to Display ACL Entries for a FileDisplay ACL entries for a file by using the getfacl command.
If you specify multiple file names on the command line, the ACL entries are displayed with a blank line between each entry. Examples—Displaying ACL Entries for a FileThe following example shows all the ACL entries for the ch1.doc file. The #effective: note beside the user and group entries indicates what the permissions are after being modified by the ACL mask.
The following example shows the default ACL entries for the book directory.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||