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.

:bulb: Suppose we store the value 2 in mode. In binary, 2 is equal to 000..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.

  1. Add a mode field to these different representations of a file or file metadata in xv6. (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!)
  2. Modify ialloc to create inodes with M_ALL by default (we will correctly initialize the mode later). Additionally, modify the functions iupdate, ilock, and stati in kernel/fs.c to correctly initialize the new mode field.
  3. The file mkfs/mkfs.c is used by xv6 to create the initial file system. Add mode as a parameter to the ialloc function in mkfs.c. Then modify mkfs.c so that all executable files that are generated get the permissions R/- (read only) and all other files get the permissions R/W (read, write). (Hint: look at how this program currently detects executables.)
  4. Run make clean before launching the QEMU instance to make sure your disk image is regenerated.
  5. Extend the ls program 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

  1. Add a system call int chmod(char *path, int mode); to xv6 (for example in sysfile.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 in sysfile.c and Chapter 10.14 of the book to see how file operations are handled.

  2. Add a userspace program called chmod that takes a mode as 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.

  1. Edit the open syscall. The open syscall opens files with an omode. Find out how it is used. Make sure that this syscall only succeeds if the given omode is allowed by the file permissions.

  2. Test that your solution works!


This site uses Just the Docs, a documentation theme for Jekyll.