File Attributes

Anatomy

# Example file attributes broken down
-rw-rw-r-- 2 mat mat 12 May 26 19:50 original.txt
# ┌----- type ----┐     ┌----------  mode ----------------┐
[ 4 bits: file type ][ 3 bits: special ][ 9 bits: permissions ]
        1000                000               110110100
 codes = regular file   n/a for this          rw-rw-r--

Type

4 bits: File Type

binarymeaningls char
1000regular file-
0100directoryd
1010symlinkl
0110block deviceb
0010char devicec
0001named pipep
1100sockets

Mode

9 bits: Permissions

  • r the read bit allows the file to be opened and read
  • w write bit controls modifying or truncating a file’s contents. Deleting or renaming it requires write permission on the parent directory, since that’s where the filename-to-inode mapping is stored — each directory manages only its own entries.
  • e executable bit allows files to be executed, there two types binaries that run on the cpu directly and scripts which need to be interpreted by a shell or other program

chmod change mode (permissions)

OctalBinaryPerms
0000
1001–x
2010-w-
3011-wx
4100r–
5101r-x
6110rw-
7111rwx
Bash
# Options for chmod targeting
u = user (owner)
g = group
o = other (everyone)
a = all 3

# The main ways to use chmod
chmod u+w       # write access for owner
chmod ug=rw,o=r # r/w to user and group, read for others
chmod a-x       # remove execute for all
chmod g=u       # group gets the permissions user has

# recursively give group write to all its contents, preserving bits that were not explicitly set
chmod -R g+w mydir
# -> warning execute on a directory has different effects
A note about x permissions
  • x execute on a directory means you can traverse (cd) in
  • chmod 711 myprog gives all perms to the owner and exec x for everyone else. if myprog was a shell script it would need read and exec permissions so it can be read by the interpreter. Binary files only need exec permissions.

chown - change owner

To change a file’s group, you must either be the superuser or be the owner of the file and belong to the group you’re changing.

Bash
chown # change a files user and group
# sudo chown user:group file
sudo chown -R mat backup/        # just the user
sudo chown -R matt:staff backup/ # user and group

# This command works too although it can be done with chown
# It's still useful in a scripting setting
sudo chgrp group file

umask

umask is a shell command to influence the default permissions given to the files you create.
You define as three digit octal value that represents the permissions to take away

Bash
umask 027 # allow all permissoins for owner but forbid write perms to the group and no perms for anyone else
# default is typically 022 = 755

3 bits: Special

binarymeaningls char
100setuids or S
010setgids or S
001stickyt or T

setuid / setguid

Pithy

setuid/setguid allows an executable to have some code that allows the calling user to access files or processes that would otherwise be off-limits to the caller.

This is different from running as sudo, these files while at installation time may have required special permissions, their purpose is to avoid requiring the user provide special permissions once setup and instead allow elevated permissions at runtime.

setuid = set user id (run as user ID)
setguid = set group id (run as group ID)

Example of setuid file:

# owner's execute bit is S meaning "everyone" who executes will get the owners system permissions
# s = setuid and executable
# S = setuid set, but no execute permission
-rwSrw-r-x 1 mat mat 0 May 27 11:18 testfile
# Remember, ls is just formatting it this way the "x" still exists theres always 16 bits

Real setuid examples

passwd
Shell
# passwd binary needs to edit /etc/passwd which requires root user access
# Thus somewhere in this program is upgrades to those rights and then downgrades ASAP for security
/usr/bin/passwd
-rwsr-xr-x 1 root root /usr/bin/passwd

# Search for other setuid and setguid binaries on your system
find / -perm /6000 -type f 2>/dev/null
sudo

Another prolific example sudo, which checks a users group and then escalates their permissions to the owner of the sudo binary

# To demonstrate, if you chown this binary and didn't have the root password
# you'd have to boot to single user mode to recover root access, since you 
# wouldn't be able to elavate your permissons beyond the owner of the binary
-rwsr-xr-x 1 root root /usr/bin/sudo

sticky

sticky bit is only useful on directories (the name is historical).
It makes it so you can only delete or rename a file if you are the own of the dir/file or are the root user. This is used by the /tmp directory to make it more private and secure

bonus flags

Linux has bonus flags to use on files for edge cases

if a file is behaving strangely check it with lsattr to see it has one or more flags enabled

lsattr   # list bonus flags
chattr # change bonus flags

Example using the bonus i flag = immutable

chattr +i testfile                                       
lsattr testfile 
----i---------e------- testfile  # flag is now set
rm testfile                      
rm: cannot remove 'testfile'     
sudo rm testfile
rm: cannot remove 'testfile'     # even sudo can't remove it

sudo chattr -i testfile          # removing the flag
lsattr testfile        
--------------e------- testfile
rm testfile       

Some of the optional flags

FlagFSaMeaning
AXBENever update access time (st_atime; for performance)
aXBEAllow writing only in append modeb
CBDisable copy-on-write updates
cBCompress contents
dXBEDo not back up; backup utilities should ignore this file
iXBEMake file immutable and undeletableb
XBAvoid data compression if it is the default

a X = XFS, B = Btrfs, E = ext3 and ext4
b Can be set only by root

ls list

Bash
ls -a  # a = all
ls -l  # l = long list
ls -ld # d = directory      -> list attributes for the directory itself
ls -t  # t = time           -> sort by modifcation time
ls -tr # tr = time reversed -> sort by modifcation time REVERSED
ls -F  #                    -> puts a slash after directory names (easier determine file vs dir)
ls -R  # R = recursive
ls -h  # h = human readable -> converts file sizes

Breakdown of ls output

-rwSrw-r-x 2 mat admins 49 May 27 11:18 testfile

-             # type
rwSrw-rwx     # mode

2             # link count (hard links) <- directories always have 2 parent dir and the special file .
mat           # owner        ---┐ if text versions can't be determined
admins        # group owner  ---┙ ls shows the fields as their uid numbers
              #    this can happen if the user or group is deleted from /etc/passwd or /etc/group

49            # size in bytes
May 27 11:18  # date of last modification
testfile      # name of file

Git

Git facts

  • Git does not track modes, owners, or modification times.
  • Git tracks the x executable bit on a file

Git branch modeling