Difference between revisions of "File Permissions Basics and ACL"
Line 38: | Line 38: | ||
==== Viewing them ==== | ==== Viewing them ==== | ||
+ | |||
+ | ===== Using ls ===== | ||
The simplest and most common way of viewing the basic permissions of a file is by using <code>ls</code>. As an example, if you run <code>ls -l</code> on a directory, you might see: | The simplest and most common way of viewing the basic permissions of a file is by using <code>ls</code>. As an example, if you run <code>ls -l</code> on a directory, you might see: | ||
Line 45: | Line 47: | ||
− | The first block (<code>-rw-r-x---+</code>) shows the permissions of the file for the three primary ACL tags (<code>ACL_USER_OBJ</code>, <code>ACL_GROUP_OBJ</code>, and <code>ACL_OTHER</code>). (described below). The file owner is shown as <code>jrwrigh7</code> and the file group is <code>a1983</code>. These correspond to <code>ACL_USER_OBJ</code> and <code>ACL_GROUP_OBJ</code> | + | The first block (<code>-rw-r-x---+</code>) shows the permissions of the file for the three primary ACL tags (<code>ACL_USER_OBJ</code>, <code>ACL_GROUP_OBJ</code>, and <code>ACL_OTHER</code>). (described below). The file owner is shown as <code>jrwrigh7</code> and the file group is <code>a1983</code>. These correspond to <code>ACL_USER_OBJ</code> and <code>ACL_GROUP_OBJ</code>. |
'''Permissions Block:''' | '''Permissions Block:''' | ||
Line 57: | Line 59: | ||
See [https://www.gnu.org/software/coreutils/manual/html_node/What-information-is-listed.html#What-information-is-listed the ls coreutils manual] for more information on the 'long' format for <code>ls</code>. | See [https://www.gnu.org/software/coreutils/manual/html_node/What-information-is-listed.html#What-information-is-listed the ls coreutils manual] for more information on the 'long' format for <code>ls</code>. | ||
+ | |||
+ | ===== Using getfacl ===== | ||
+ | |||
+ | You can also use <code>getfacl</code> to get a more detailed look at the permissions of a file or directory. Simply running <code>getfacl</code> with a file object as it's argument will show the permissions for that file object: | ||
+ | |||
+ | $ getfacl test2file | ||
+ | # file: test2file | ||
+ | # owner: jrwrigh7 | ||
+ | # group: a1983 | ||
+ | user::rw- | ||
+ | group::r-x | ||
+ | group:a1983:r-x | ||
+ | mask::r-x | ||
+ | other::--- | ||
+ | |||
+ | This argument will show all permissions for a file object, including custom ones. <code>user::</code>, <code>group::</code>, and <code>other::</code> refer to the permissions set for the <code>ACL_USER_OBJ</code>, <code>ACL_GROUP_OBJ</code>, and <code>ACL_OTHER</code>. | ||
+ | |||
+ | To be more specific, a group or user can be inserted between the colons. This can be seen in the line <code>group:a1983:</code> where the members of the group have <code>r-x</code> permissions to the file. These permissions fall under the <code>ACL_USER</code> and <code>ACL_GROUP</code> tags. | ||
+ | |||
+ | If default ACL entries are used, they will also be displayed here: | ||
+ | |||
+ | $ getfacl test | ||
+ | # file: test | ||
+ | # owner: jrwrigh7 | ||
+ | # group: a1983 | ||
+ | user::rwx | ||
+ | group::r-x | ||
+ | group:a1983:r-x | ||
+ | mask::r-x | ||
+ | other::--- | ||
+ | default:user::rwx | ||
+ | default:group::r-x | ||
+ | default:group:a1983:r-x | ||
+ | default:mask::r-x | ||
+ | default:other::--- | ||
+ | |||
==== Octal numbers ==== | ==== Octal numbers ==== | ||
Line 143: | Line 181: | ||
==== ACL Defaults ==== | ==== ACL Defaults ==== | ||
− | + | ACL can have default permissions set. This default entry is set for a directory and is used for files and subdirectories created inside that directory. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
=== How are new file permissions set? === | === How are new file permissions set? === |
Revision as of 10:59, 6 July 2020
This is an overview of how file permissions and Access-control Lists (ACL) work on POSIX-based file systems. This forms a primer to understanding how exactly the actions in Setting Default File Permissions work and how to debug them.
In short, ACL helps to control which users can read, write, or execute certain file objects, which are defined as anything in the file system (regular files, directories, symlinks, etc.). Note that ACL does not have complete authority on creating permissions for new file objects.
Basics of Unix/POSIX File Permissions
What are they?
- All files and directories have permissions assigned to them via ACL entries
- There are three different "levels" of file permissions in a standard POSIX file system: read (
r
), write (w
), and execute (x
).- Read allows viewing the contents of the file/directory, and copying the files
- Write allows rewriting and deleting files. For a directory with write permissions, it also allows creation of subdirectories and creation of new files
- Execute allows files to be executed directly.
- Note that for script files (such as
bash
orpython
), they can still be run by passing the file to it's interpreter if the file is readable (ie.bash non_executableScript.sh
is still possible ifnon_executableScript.sh
hasrw-
permissions).
- Note that for script files (such as
- ACL entries apply the three different "levels" of file permission onto 6 possible tags. From the ACL manpage, they are:
ACL_USER_OBJ The ACL_USER_OBJ entry denotes access rights for the file owner. ACL_USER ACL_USER entries denote access rights for users identified by the entry's qualifier. ACL_GROUP_OBJ The ACL_GROUP_OBJ entry denotes access rights for the file group. ACL_GROUP ACL_GROUP entries denote access rights for groups identified by the entry's qualifier. ACL_MASK The ACL_MASK entry denotes the maximum access rights that can be granted by entries of type ACL_USER, ACL_GROUP_OBJ, or ACL_GROUP. ACL_OTHER The ACL_OTHER entry denotes access rights for processes that do not match any other entry in the ACL.
ACL_USER_OBJ
, ACL_GROUP_OBJ
, and ACL_OTHER
correspond to the standard three user categories: file owner, file group, and "other". These three categories are the ones listed by ls -l
.
The file owner and file group are set to the user that originally created the file and their respective user group (though this can be changed using chown
).
"Others" simply refers to all other users that are not the file owner and are not members of the file group.
The other three tags, ACL_MASK
, ACL_GROUP
, and ACL_USER
, are only used for custom ACL entries that are set by a user.
- The file permissions for
ACL_USER_OBJ
,ACL_GROUP_OBJ
, andACL_OTHER
together form the filemode
parameter- This parameter is used by programs to set the permissions of files they create. This is discussed further in the Custom Permissions section.
Viewing them
Using ls
The simplest and most common way of viewing the basic permissions of a file is by using ls
. As an example, if you run ls -l
on a directory, you might see:
drwxr-x---+ 2 jrwrigh7 a1983 4.0K 2020-07-04 08:09 test2 -rw-r-x---+ 1 jrwrigh7 a1983 38 2020-07-02 12:38 test2file lrwxrwxrwx 1 jrwrigh7 a1983 9 2020-07-04 08:40 test2fileLink -> test2file
The first block (-rw-r-x---+
) shows the permissions of the file for the three primary ACL tags (ACL_USER_OBJ
, ACL_GROUP_OBJ
, and ACL_OTHER
). (described below). The file owner is shown as jrwrigh7
and the file group is a1983
. These correspond to ACL_USER_OBJ
and ACL_GROUP_OBJ
.
Permissions Block:
- First character displays what kind of file it is, be it a link (
l
), directory (d
), regular file (-
), etc. - The next 9 characters show the permissions for the
ACL_USER_OBJ
,ACL_GROUP_OBJ
, andACL_OTHER
- So for
test2file
in the above output:- File Owner:
rw-
- File Group:
r-x
- Others:
---
- File Owner:
- So for
- The last character is optional. A
+
means that there are other permission rules not displayed. This is where ACL rules come into play.
See the ls coreutils manual for more information on the 'long' format for ls
.
Using getfacl
You can also use getfacl
to get a more detailed look at the permissions of a file or directory. Simply running getfacl
with a file object as it's argument will show the permissions for that file object:
$ getfacl test2file # file: test2file # owner: jrwrigh7 # group: a1983 user::rw- group::r-x group:a1983:r-x mask::r-x other::---
This argument will show all permissions for a file object, including custom ones. user::
, group::
, and other::
refer to the permissions set for the ACL_USER_OBJ
, ACL_GROUP_OBJ
, and ACL_OTHER
.
To be more specific, a group or user can be inserted between the colons. This can be seen in the line group:a1983:
where the members of the group have r-x
permissions to the file. These permissions fall under the ACL_USER
and ACL_GROUP
tags.
If default ACL entries are used, they will also be displayed here:
$ getfacl test # file: test # owner: jrwrigh7 # group: a1983 user::rwx group::r-x group:a1983:r-x mask::r-x other::--- default:user::rwx default:group::r-x default:group:a1983:r-x default:mask::r-x default:other::---
Octal numbers
The file mode
is often conveyed in the form of three octal numbers (ie. base 8 numbers). It is very similar to how PHASTA handles specifying boundary conditions using bitwise logic.
The first bit handles read, the second bit handles write, and the third handles execute.
Permissions | Read bit | Write Bit | Execute Bit | Octal Number equivalent |
---|---|---|---|---|
---
|
0 | 0 | 0 | 0 |
r--
|
1 | 0 | 0 | 1 |
-w-
|
0 | 1 | 0 | 2 |
rw-
|
1 | 1 | 0 | 3 |
--x
|
0 | 0 | 1 | 4 |
r-x
|
1 | 0 | 1 | 5 |
-wx
|
0 | 1 | 1 | 6 |
rwx
|
1 | 1 | 1 | 7 |
Using test2file
as the example permissions block, it is stored as 110 101 000
. When translated into decimal, that equals 3 5 0
.
Another way to think about it is that the octal number equivalent of a permission set = 1*r + 2*w + 4*x, where r, w, and x equal 1 or 0 depending on whether they're set or not.
Custom Permissions
The other two tags, ACL_GROUP
and ACL_USER
, are for more custom permissions and are not set by default on a blank system.
The ACL_MASK
entry plays an important role in how new file permissions are set.
Note that the ACL_MASK
is only used when ACL_GROUP
or ACL_USER
are used for ACL entries.
What determines the permissions for a new file?
There are three sources that determine what file permissions will be set; umask
, the "mode" parameter used by the program creating the file, and ACL.
umask
umask
is a function that contains file permission settings for any file created by a user and it is unique to the user. The user can change it at anytime using the umask
command. To see what it is set to, simply run umask
with no arguments.
"Mode" Parameter
When a new file is created, the syscall
open()
(among others) is used and a file mode parameter must be chosen by that program. For example, `touch` will automatically apply the mode 666
to the file, which will make the file owner, file group, and "others" all have rw-
permissions
ACL Defaults
ACL can have default permissions set. This default entry is set for a directory and is used for files and subdirectories created inside that directory.
How are new file permissions set?
The manpage for ACL is a great resource to answering this question. Most of the information below is simply paraphrasing what is written in the "Object Creation and Default ACLs" section.
When creating a file/subdirectory in a parent directory:
- If the parent directory does have default ACL rules, only the ACL default rules and "mode" parameter are used:
- The new file/subdirectory first inherits the default ACL rules of its parent directory
- The new file/subdirectory has its ACL entries adjusted such that no permissions exceed the "mode" parameter.
- The
ACL_USER_OBJ
andACL_OTHER
are changed directly - The
ACL_GROUP
,ACL_GROUP_OBJ
, andACL_USER
are changed through adjusting theACL_MASK
- The
- Additionally, if a new subdirectory is created, it will inherit its parents default ACL rules. Note that a file cannot have default ACL rules set.
- If the parent directory does not have default ACL rules, only
umask
and "mode" parameter are used:- The new file/subdirectory's
ACL_USER_OBJ
,ACL_GROUP_OBJ
, andACL_OTHER
are set based on the permissions set byumask
- The new file/subdirectory's are then adjusted to limit permissions to be no looser than the "mode" parameter
- The new file/subdirectory's
Note that this all means that if you have a default ACL rule that gives execute permissions to a group, the group will not have execute permissions by default unless the "mode" parameter also has execute permissions. Most compilers will use the execute permission bit for the "mode" parameter of it's executable, but other files will not.