Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Filesystem Security - Directory Permission

Back


Published blog:linux filesystem security: understanding directory permissions


Directory Permissions Overview

In Linux, every entity is treated as a file, and there are three main types of permissions for each file or directory: r, w, and x. While the impact of these permissions is straightforward for files, they behave differently for directories. In this blog, we’ll dive deeper into the effects of x and r permissions on directories.


x Permission on a Directory

For files, the x permission allows execution. But what does it mean for a directory? The x permission on a directory governs the ability to traverse it, i.e., access files or subdirectories within it by their names.


Lab: Understanding x Permission

Environment

su - root

# create user dirowner
sudo useradd dirowner
echo "dirowner:Linux101" | sudo chpasswd

# create user otheruser
sudo useradd otheruser
echo "otheruser:Linux101" | sudo chpasswd

# switch to dirowner
su - dirowner
whoami
# dirowner

# create target dir
mkdir -p /tmp/dir
# create file within the dir
touch /tmp/dir/file

# create script file and change mode
cat <<EOF > /tmp/dir/script.sh
#!/bin/bash
echo "Hello world"
EOF
cat /tmp/dir/script.sh
# #!/bin/bash
# echo "Hello world"

# Grant x on the script file
chmod o+x /tmp/dir/script.sh

# change mode of the target dir
chmod o-x /tmp/dir

# verify the mode
ls -ld /tmp/dir
# drwxrwxr--. 2 dirowner dirowner 35 Nov 24 16:56 /tmp/dir
ls -l /tmp/dir/file
# -rw-rw-r--. 1 dirowner dirowner 0 Nov 24 16:56 /tmp/dir/file
ls -l /tmp/dir/script.sh
# -rw-rw-r-x. 1 dirowner dirowner 31 Nov 24 16:56 /tmp/dir/script.sh

exit

# Test as otheruser
su - otheruser
whoami
# otheruser

# Test ls commands
ls -dl /tmp/dir
# drwxrwxr--. 2 dirowner dirowner 35 Nov 24 16:56 /tmp/dir

ls /tmp/dir
# ls: cannot access '/tmp/dir/file': Permission denied
# ls: cannot access '/tmp/dir/script.sh': Permission denied
# file  script.sh

ls -l /tmp/dir
# ls: cannot access '/tmp/dir/file': Permission denied
# ls: cannot access '/tmp/dir/script.sh': Permission denied
# total 0
# -????????? ? ? ? ?            ? file
# -????????? ? ? ? ?            ? script.sh

ls /tmp/dir/file
# ls: cannot access '/tmp/dir/file': Permission denied
ls -l /tmp/dir/file
# ls: cannot access '/tmp/dir/file': Permission denied
ls -l /tmp/dir/script.sh
# ls: cannot access '/tmp/dir/script.sh': Permission denied

bash /tmp/dir/script.sh
# bash: /tmp/dir/script.sh: Permission denied

exit

# Correct the directory permission
# switch to dirowner
su - dirowner
whoami
# dirowner
chmod o+x /tmp/dir

# verify mode
ls -dl /tmp/dir
# drwxrwxr-x. 2 dirowner dirowner 35 Nov 24 16:56 /tmp/dir
ls -l /tmp/dir
# total 4
# -rw-rw-r--. 1 dirowner dirowner  0 Nov 24 16:56 file
# -rw-rw-r-x. 1 dirowner dirowner 31 Nov 24 16:56 script.sh

exit

# Test after correction
# switch to otheruser
su - otheruser
whoami
# otheruser

ls -l /tmp/dir
# -rw-rw-r--. 1 dirowner dirowner  0 Nov 24 16:56 file
# -rw-rw-r-x. 1 dirowner dirowner 31 Nov 24 16:56 script.sh

bash /tmp/dir/script.sh
# Hello world

exit

Results

Without x Permission on the Directory

Command Execution File name
ls -dl dir Success -
ls dir Permission Denied List
ls -l dir Permission Denied List
ls dir/file Permission Denied -
ls -l dir/file Permission Denied -
bash /tmp/dir/script.sh Permission Denied -

With x Permission Restored

Command Execution
ls -l dir Success
bash /tmp/dir/script.sh Success

r Permission on a Directory

For files, the r permission allows reading their content. On a directory, r permission enables listing the names of files and subdirectories within it.


Lab: Understanding r Permission

Continue the lab on top of the above codes.

su - dirowner
whoami
# dirowner

# list the permission of target dir
ls -dl /tmp/dir
# drwxrwxr-x. 2 dirowner dirowner 35 Nov 24 16:56 /tmp/dir

# Revoking the r permission
chmod o-r /tmp/dir
# verify the mode
ls -dl /tmp/dir
# drwxrwx--x. 2 dirowner dirowner 35 Nov 24 16:56 /tmp/dir

exit

# switch user
su - otheruser
whoami
# otheruser

# test command
ls -ld /tmp/dir
# drwxrwx--x. 2 dirowner dirowner 35 Nov 24 16:56 /tmp/dir

ls /tmp/dir
# ls: cannot open directory '/tmp/dir': Permission denied
ls -l /tmp/dir
# ls: cannot open directory '/tmp/dir': Permission denied

ls /tmp/dir/file
# /tmp/dir/file
ls -l /tmp/dir/file
# -rw-rw-r--. 1 dirowner dirowner 0 Nov 24 16:56 /tmp/dir/file
ls -l /tmp/dir/script.sh
# -rw-rw-r-x. 1 dirowner dirowner 31 Nov 24 16:56 /tmp/dir/script.sh

bash /tmp/dir/script.sh
# Hello world

Result

Without r Permission on the Directory

Command Execution File name
ls -dl dir Success -
ls dir Permission Denied No List
ls -l dir Permission Denied No List
ls dir/file Success -
ls -l dir/file Success -
bash /tmp/dir/script.sh Success -

Comparison: x vs r Permissions on a Directory

Command Result without x Filename without x Result without r Filename without r
ls -dl dir Success - Success -

Command Result without x Filename without x Result without r Filename without r
ls dir Permission Denied List Permission Denied No List
ls -l dir Permission Denied List Permission Denied No List

Command Result without x Filename without x Result without r Filename without r
ls dir/file Permission Denied - Success -
ls -l dir/file Permission Denied - Success -

Command Result without x Filename without x Result without r Filename without r
bash /tmp/dir/script.sh Permission Denied - Success -

Key Foundations

To better understand the behavior of the ls command and the default permission settings, it’s essential to first establish some fundamental concepts about the file system and permissions:


Understanding the Role of Permissions in the ls Command


Default File and Directory Permissions


TOP