Working with Linux Files – Access Control Lists

In the last post, we looked at basic file permissions. The ideas covered in that post are probably enough to get you through a large portion of the real world scenario’s that you will encounter. There are some special cases, however. One of them being access control lists (ACL) which I will discuss in this post.

Access Control List – ACL

As we saw in part 1 every file on a Linux system has an owner and a group associated with it, each of which has separate permissions. But what if a user or group needed read permissions to a file that they do not own? You could modify permissions to allow all users access to a file with  chmod o+r but this is not ideal on a system with multiple users where data needs to be kept confidential. Instead of going this route, which is insecure, we can add a list of users, or groups, or both that have permissions to a file that are separate from the standard owner/group in basic POSIX permissions.

Using ACL’s

So let’s look at a simple example. Batman and the finance group need to be able to read and write a payroll document, Superman and Robin need to be able to write, but not read, the payroll document (we don’t want them to know that Batman makes more money than they do).

First, let’s set up our environment:

sudo useradd batman
sudo useradd superman
sudo useradd robin
sudo groupadd finance
sudo usermod -aG finance batman

Set a password for each user. I’m just going to use the username for each as the password to make it easy for this example. It should go without saying that this wouldn’t be an acceptable password policy for real world use.

sudo passwd batman
New password: 
BAD PASSWORD: it is based on a dictionary word
BAD PASSWORD: is too simple

sudo passwd superman
New password: 
BAD PASSWORD: it is based on a dictionary word
BAD PASSWORD: is too simple

sudo passwd robin
New password: 
BAD PASSWORD: it is based on a dictionary word
BAD PASSWORD: is too simple

Switch to the user batman and create a file called jlpayroll.txt in the /tmp directory.

su - batman
cd /tmp
newgrp finance
touch jlpayroll.txt
chmod 660 jlpayroll.txt
ls -l jlpayroll.txt
-rw-rw---- 1 batman finance 0 Feb  5 09:32 jlpayroll.txt

At this point, batman can read and write to the jlpayroll.txt file. Now add batman’s pay to this file.

echo "Batman Week One: 2,000,000" >> jlpayroll.txt 
cat jlpayroll.txt 
Batman Week One: 2,000,000

Now we need to add ACL’s to allow superman and robin to add their payroll information, while also ensuring that they can’t read the file. Batman can do that with the  setfacl command that he keeps in his utility belt.

setfacl -m u:superman:w -m u:robin:w jlpayroll.txt

Take a look at this command for a minute. First, notice that you can add multiple users at the same time each separated by the -m option. In the setfacl command -m means modify.

Next, notice the format that is used to identify users. The  setfacl  command expects 3 values separated by colons:

<type>:<name>:<permission> – the type can be “u” for user or “g” for group, the name is the group or username, and the permissions are the standard read, write, and execute permissions that we looked at previously.

So how does this affect the way permissions are displayed?

ls -l jlpayroll.txt -rw-rw----+ 1 batman finance 27 Feb 5 09:43 jlpayroll.txt

The change is subtle but it’s there. Notice the “+” after “-rw-rw—-“. That + sign indicates that permissions have been altered by access control lists.

How to display the ACL’s on a file.

Access control list’s can be displayed with the  getfacl command.

getfacl jlpayroll.txt 
# file: jlpayroll.txt
# owner: batman
# group: finance
user::rw-
user:superman:-w-
user:robin:-w-
group::rw-
mask::rw-
other::---

The output here is a detailed look at exactly who has what level of access to this file. It lists the file name, owner, and group. Then each user and group have individual access level’s displayed.

Testing the ACL’s

Switch to the user superman and make sure he can write to the jlpayroll.txt file.

su - superman
cd /tmp
id
uid=1007(superman) gid=100(users) groups=100(users)
ls -l jlpayroll.txt
-rw-rw----+ 1 batman finance 27 Feb  5 09:43 jlpayroll.txt

We can see that superman is not in the finance group and he is not the owner of this file. Normally he wouldn’t have any access to this file at all. But with the ACL we set up earlier he should be able to write to this file but not read it.

cat jlpayroll.txt 
cat: jlpayroll.txt: Permission denied

As you can see superman cannot read the jlpayroll.txt file. Can he write to it?

echo "Superman Week One: 400" >> jlpayroll.txt

As long as that command doesn’t return an error then it should’ve worked. Let’s do the same thing for robin

su - robin
cd /tmp
echo "Robin Week One: 4,000" >> jlpayroll.txt

Since payroll is done for the week lets make sure that batman can see each entry. If you’ve been following along you can get back to batman using the “exit” command twice.

cat jlpayroll.txt 
Batman Week One: 2,000,000
Superman Week One: 400
Robin Week One: 4,000

Access control lists allow an administrator to exercise fine-grained control over files and directories that would not be possible using only POSIX permissions. This was a pretty simple example but it should give you an idea about how ACL’s work, how to set them, and how to discover what ACL’s are set on a file.

 Clean up

When you are finished with this example make sure you clean up your system to get rid of the users and group we made at the beginning of the tutorial. Switch back to your normal user account with sudo privileges.

sudo userdel batman
sudo userdel superman
sudo userdel robin
sudo groupdel finance
sudo rm /tmp/jlpayroll.txt

Luke has an RHCSA for Red Hat Enterpirse Linux 7 and currently works as a Linux Systems Adminstrator in Ohio.

This post, re-published here with permission, was originally published on Luke’s site here.



Be the first to comment

Leave a Reply

Your email address will not be published.


*