Overview

In this lab we will:

A. Unix file permissions

The following shell commands may be of use to you in carrying out the operations below (look them up in the man pages if you do not know what they do):

Use the above commands to help you answer the following questions:

B. Setuid programming

To do any meaningful setuid programming we need to work with files that are not owned by us and that have their setuid bit turned on. That presents us with a problem since we need root privileges to change file ownership. We cannot give out root privileges on lab machines. So what can we do?

Well, you'll find /usr/local/bin/chruth may help. Look at its permissions and verify that it is a setuid root program. (If you find any vulnerabilities in it, let me know!) The program takes a file as its single input parameter. If the file is an executable, chruth will change its owner to ruth and turn on its setuid bit. (You are doing setuid ruth rather than setuid root programming!) If the file is not an executable, chruth will change its owner to ruth and give only ruth read permission on the file.

To start off create a directory under /tmp where you will do the exercise. You must carry out all setuid programming in a directory off /tmp or the exercises will not work! Next create a file called suidruth.c which does nothing (i.e. its main is empty). Now we can make our program setuid ruth by running chruth against it.

$ gcc -o suidruth suidruth.c
$ chruth suidruth
$ ls -l suidruth
-rwsrwxrwx 1 ruth mse 8588 2008-10-20 13:39 suidruth

Each time you build a new version of suidruth you will have to run chruth against it in order to test it. Sorry!

Now by following the steps outlined below we can practise some setuid programming inside setuidruth.c. If you are unsure on how to carry out any of these steps then look at your setuid lecture notes and the man pages for the various setuid system calls.

If the man pages have not been installed on the machines in the lab you should find them on the web. Verify each setuid call works before proceeding to the next, do this by running your program and having it print out the three user IDs along the way.

When finished, running your program you should see something like the following:

$ ./suidruth
Real uid: 100 # This is your user ID
Effective uid: 13753 # This is ruth's user ID
Saved uid: 13753
Hit return to continue
Real uid: 100
Effective uid: 100
Saved uid: 13753
Hit return to continue
Real uid: 100
Effective uid: 13753
Saved uid: 13753
Hit return to continue
Real uid: 100
Effective uid: 100
Saved uid: 100

C. Launching a bash shell from a setuid program

Now write a setuid ruth program which exec's a bash shell. Since privileges are carried across an exec your bash shell should run with elevated privileges (in this case as ruth). Typing whoami at the prompt will tell you whether or not this is the case.

D. File descriptors, setuid and exec

Create a new file called readme and put a few lines of text inside it. Use chruth to give only ruth read permission on the file. Write a setuid ruth program called suidopen which opens the readme (before you make it setuid verify that, when you run it, it fails citing a permission denied error on trying to open the readme file).

You should see something like the following:

$ echo "This is a secret" > readme # Create the readme file
$ chruth readme
$ gcc -o suidopen suidopen.c
$ chruth suidopen
$ gcc -o reader reader.c
$ ls -l
-rwxr-xr-x 1 dobrien users 9224 2008-10-20 16:08 reader
-rw--w--w- 1 ruth mse 46 2008-10-20 12:10 readme
-rwsrwxrwx 1 ruth mse 9250 2008-10-20 16:08 suidopen
$ ./suidopen
This is a secret