================ Unix Permissions ================ -IAN! idallen@idallen.ca Unix is a multi-user system. Processes belonging to different users have restrictions on how they can interact. Files belong to different users, and users can control how much access other users have to their files. The system provides security among users and between the system and users. This means Unix can protect itself against programs run by ordinary users. ------------------------- Unix user ID and group ID ------------------------- Each Unix process (including your shell and each command and process that you run when logged in) runs as its own unique numeric user ID and belongs to one or more numeric group IDs. The numeric user and group IDs also have names; the Unix password file /etc/passwd maps the user ID number to a name; the file /etc/group matches each group ID number to a name. The name is a convenience for people; the processes calculate permissions based on the numerid IDs. When you log in, the system starts up a shell that runs as your unique numeric user ID and has the permissions of all your numeric group IDs (one or more). Almost all the commands that you run inherit your user ID and your one or more group IDs. The processes appear to run as "you". ------------------------------------------ Unix directories are only names and inodes ------------------------------------------ Unix directories are very simple; they only contain names and inode numbers. Nothing in a directory directly controls what access permissions you have to Unix file system objects that are named in that directory. (The permissions on the directory itself may prevent you from getting to an object using that directory; but, once you get to the object, it is the object inode permissions that apply. No object permissions are stored in the directory itself.) To determine what access permissions you have with respect to some Unix file system object, you must use the object's name to pass through the directory and go to the actual inode for the object. Permissions are not kept in the directory; they are kept in the inode for the object. Only the objet's name and inode number are kept in the directory. The inode containing the permissions for an object is separate from the directory inode containing the name of the object. To access an object, you need to pass through the directory (you must have "search permission") and then have appropriate permissions on the inode that is the object itself. -------------------- Unix file system IDs -------------------- Each Unix inode, whether it is a file inode, a directory inode, or some other special node, has the following properties: - the inode is "owned" by exactly one numeric Unix userid - the inode belongs to exactly one numeric Unix group (Note that processes may be in many groups simultaneously. All inodes, whether files, directories, or other things, can only be in one group.) ----------------------------------- Determining which permissions apply ----------------------------------- When a Unix process tries to access any kind of Unix file system inode (a file, a directory, or some other file system object), Unix permissions determine if the access succeeds. Each inode has room for three sets of Unix permissions: "user", "group", and "other". The "ls" command displays these as "rwxrwxrwx" at the start of a long listing. Here is what happens when a userid tries to access a file system inode: 1) If the unique user ID of the process doing the access matches the user ID that owns the inode, the "user/owner" permissions field of the inode determines whether or not the access succeeds. If the user IDs do not match, move to part #2: 2) (Only done if the user IDs do not match:) If any of the several group IDs of the process doing the access match the single group ID of the inode, the "group" permissions field determines whether or not the access succeeds. If the group IDs do not match, move to part #3: 3) (Only done if both the user IDs and group IDs do not match:) The "other" permissions field determines whether or not the access succeeds. -------------------------- The three permissions: rwx -------------------------- The permissions that can be controlled in each of the above three sets are three: Read, Write, and eXecute/search. These appear as the letters "rwx" in the output from "ls -l": $ ls -l /bin/ls -rwxr-xr-x 1 root root 62552 May 9 2002 /bin/ls The first character in the "ls" listing is the type of the inode. (A dash indicates it is a plain file.) The next nine characters are the "rwx" permissions that apply to each of "user", "group", and "other". The user ID and group ID(s) of the process are matched against the user ID and group ID of the inode to determine which of the three sets of permissions apply: "user", "group", or "other". Once a set is chosen, the "rwx" permissions are used to limit what the process can do to that inode: Read Permission: The process can read the data in the inode. For a file, the process can read the data contained in the file. For a directory, the process can read only the names contained in the directory. Read permission on a directory *only* gives the process access to the names in the directory itself. (The process needs further permission to follow the inode numbers to have access to the actual inodes and content of the items named in the directory. You may be able to see a file's name but not have permission to see the file's owner or contents.) Write Permission: The process can write to the inode. For a file, this means that the process can alter the data contained in the file. For a directory (which only contains names and inode numbers), the process can add, remove, or alter (rename) the names in the directory. Write permission on a directory *only* gives the process the ability to change the *names* in the directory. (The process may not have permission to alter the data in the inodes of the items named in the directory. You may be able to rename or delete a file name but not have permissions to read or write the file data itself.) eXecute/Search Permissions: For a file, the process may load the binary image of the file into memory and execute it. If the file is executable but not binary, i.e. a script file, it is the program specified in the "#!" line at the top of the script file that is loaded into memory instead. For a directory, the process may "pass through" the directory to access the inode numbers and content of the items named in the directory. Without search permissions on a directory, the process cannot access any of the inodes and content named in the directory. ------------------------------- Names are separate from content ------------------------------- The permissions that govern access to a directory are stored in the inode for that directory. The permissions for the items *named* in the directory are *not* kept in the directory; they are stored in the inodes for the items being named. Because names are stored separately from the things they name, the permissions you may have to alter the names in the directory are *not* the same as the permissions you have to alter the actual things being named. Changing the permissions of a directory only affects what you can do to the directory; it doesn't directly affect what you can do to the things named in the directory. You may be able to write the inode of the directory but unable to write the inodes of anything named in the directory, or vice-versa. You may have a directory that you can modify ("rwx") that contains names for things that you cannot even read ("---"). You can rename the things or remove their names from your directory; but, you cannot read the things or change them in any way. You may have a directory that you cannot modify ("r-x") that contains names for things that you have full permissions to modify ("rw-"). You have full permissions to modify the item; but, you cannot remove its name from the directory or change its name (because you cannot write the directory where the name is kept). When a file system is damaged and a directory inode is lost, the only things that go missing are the names of the items in the directory. Everything else about the items - the permissions, owner, group, modify time, size, etc. - is stored in the items' inodes, separate from the lost directory inode. --------------------------------- Search permission for directories --------------------------------- If you have only "r--" permissions on a directory (only read permissions), you can only see the names in the directory; you cannot "pass through" the directory to the inodes of the things named in the directory. You cannot find out the kind of things to which the names are attached, nor can you access the content of those things. You only have permission to see the names in the directory, not to use them. "ls" works, but not "ls -l" (which requires access to the inodes of the items in the directory). If you have only "--x" permissions on a directory (only search permissions), you can "pass through" the directory to the inodes of anything contained in the directory; but, you cannot read any of the names in the directory. You have permissions to use the names, not to see them. (Of course, the only way you can use a name that you can't see is if you already know that the name is in the directory.) A directory with "-wx" permissions lets a process enter files into it; but, the process cannot read the names of the files back from the directory. (If the file names are random and guaranteed unique, it's a form of digital "drop box" where items can be created and written by any process but whose names cannot be snooped by other processes.) A directory with only "-w-" permissions is useless, though you might think it would permit you to create new names or to rename existing names if you know them. The implementation is such that any action that requires write permission on a directory also requires search permission. (Consider that any action that writes a name in a directory also has to have access to the inode of the thing being named, and that inode access requires search permissions on the directory.) Similarly, having "rw-" permissions is the same as "r--" permissions; without search permissions, write permissions don't work. ---------- Hard links ---------- When you create a hard link to a file, you create another name for the file's unique inode in a directory. Since the file permissions are stored with the file inode, not in the directory, all the names for a file lead to the same inode and thus to the same permissions. Changing the permissions of a file using one of the hard-linked names changes the permissions for the inode and thus for all the names; all the names lead to the same unique inode and the same permissions. Creating, renaming, or removing a name (a hard link) is a directory operation. The permissions on the directory inode apply, not the permissions on the inode of the thing to which you are making a link. You can create in your own directory a hard link to a file even if you have no permissions to read the file itself. You can remove or rename the name of a file in your directory if you have permission on the directory; you don't need any permissions at all on the inode of the file itself. --------------------- The current directory --------------------- What permissions are needed to list the contents of the current directory? The current directory is accessible using the name ".". To get a listing of the contents of the current directory, you must first have permission to use the name "." to get to the directory. The name "." is itself stored in the current directory. To use ("pass through to") any name in a directory, you must have search permissions on the directory ("--x"). Once you are at the inode that is the current directory, you must be able to read the names from the directory to generate a listing of its contents. Reading the names in a directory requires read permission on the directory ("r--"). Thus, to get a listing of the current directory, on the directory you must have both search permissions (to use the name ".") and read permissions (to get a list of the names in the directory). Result - you need: "r-x" Without search permissions, you cannot use the name ".". Without read permissions, you cannot get a list of the names in the directory. You need both. ------------- Long listings ------------- What permissions are set on directory "dir", below? $ ls dir x y z $ ls -l dir /bin/ls: dir/x: Permission denied /bin/ls: dir/y: Permission denied /bin/ls: dir/z: Permission denied Answer: Read permissions but not search permissions. The "ls" command without any options simply lists the names in the directory. This only requires read permissions on the directory. The simple "ls" succeeded; the directory had read permissions. With the "-l" option, "ls" must use each name in the directory to access the inode associated with that name to find out the permissions, owner, group, times, etc. for that inode. To use a name - to "pass through" a directory - requires search permissions on the directory, which were not enabled. For each name in the directory, the permission to access "dir/name" for the long listing was denied. The directory had read permissions but no search permissions. --------- Exercises --------- Given the following inode permissions: ---x------ 1 dar staff 123 Apr 1 2003 dar1 dr---wx-w- 1 dar dat2330 512 Apr 1 2003 dar2 -r---wx-w- 1 les dat2330 123 Apr 1 2003 les1 drwxrw-r-x 1 les alumni 512 Apr 1 2003 les2 -rwxrw-r-x 1 pat alumni 123 Apr 1 2003 pat1 d--x------ 1 pat staff 512 Apr 1 2003 pat2 -rw-r--r-- 1 root system 123 Apr 1 2003 root1 drwx----wx 1 root system 512 Apr 1 2003 root2 And these users: root - super user pat - in groups "student" and "dat2330" les - in groups "alumni" and "staff" dar - in groups "alumni" and "dat2330" kai - in groups "alumni" and "system" tam - in groups "system" and "staff" dod - in groups "nobody" Answer these questions: *) What type of inode is each file? (directory, file, other...) *) What permissions do each of the users have on each file inode? - which set of permissions apply to each user? - can the user read the file? - can the user write the file? - can the user execute the file? *) What permissions do each of the users have on each directory inode? - which set of permissions apply to each user? - can the user read the directory names? - can write the directory (create new names, delete names, rename)? - can search (pass through) the directory to the contained inodes?