Exercise: Access Permissions
Moderate/Hard
Files in Unix-like systems contain access permissions, also called modes. Unix files can be made read-only, write-only, or execute-only, or a combination of these three.
In this exercise, we add a simple variant of file permissions to xv6 with only read and write permissions (i.e., all files will be executable by default).
We will represent file permissions by a field mode. The mode of a file determines whether it is readable and/or writable, and it will be represented as a uint. Bit 0 (the least significant bit or rightmost bit) of the mode field indicates whether a file is readable. Bit 1 of the mode field indicates whether a file is writable.
Suppose we store the value 2 in
mode. In binary,2is equal to000..00010. This means that a file with mode 2 is writable but not readable.
Part 1: Adding modes
To support permissions, we first need to add modes to files. Define the constants M_READ, M_WRITE, and M_ALL for the permissions in kernel/stat.h based on the binary values explained above (think about how, e.g., page table bits were defined).
Next, we have to store the mode for each file or directory.
Files are represented as a struct dinode on disk, a struct inode in-memory. In addition, the struct stat is used to store meta-information about files.
- Add a
modefield to these different representations of a file or file metadata inxv6. (Hint: can you arbitrarily change the size of all these structures? If you run into any errors, think about which field is the safest to reduce to make space for this new variable!) - Modify
iallocto create inodes withM_ALLby default (we will correctly initialize the mode later). Additionally, modify the functionsiupdate,ilock, andstatiinkernel/fs.cto correctly initialize the newmodefield. - The file
mkfs/mkfs.cis used by xv6 to create the initial file system. Addmodeas a parameter to theiallocfunction inmkfs.c. Then modifymkfs.cso that all executable files that are generated get the permissionsR/-(read only) and all other files get the permissionsR/W(read, write). (Hint: look at how this program currently detects executables.) - Run
make cleanbefore launching the QEMU instance to make sure your disk image is regenerated. - Extend the
lsprogram to show the permissions as follows:
$ ls
. 1 1 1024 rw
.. 1 1 1024 rw
README 2 2 2226 rw
cat 2 3 30320 r-
echo 2 4 29152 r-
forktest 2 5 13792 r-
grep 2 6 33680 r-
Part 2: Implementing chmod
-
Add a system call
int chmod(char *path, int mode);to xv6 (for example insysfile.c). This function takes the path of a file as the first parameter and the new file permissions as the second parameter. The system call ensures that the permissions of the given file are adjusted. Look at the other system calls insysfile.cand Chapter 10.14 of the book to see how file operations are handled. -
Add a userspace program called
chmodthat takes amodeas the first argument and the path to a file and then adjusts the permissions of this file as the second argument. For example:
$ chmod 1 README
$ ls
. 1 1 1024 rw
.. 1 1 1024 rw
README 2 2 2226 r-
Part 3: Checking permissions
At this point, permissions are being saved for the various files. We can view them with ls and adjust them with chmod. However, these permissions are not checked yet.
-
Edit the
opensyscall. The open syscall opens files with anomode. Find out how it is used. Make sure that this syscall only succeeds if the givenomodeis allowed by the file permissions. -
Test that your solution works!