COMP3000 Operating Systems W22: Tutorial 4

From Soma-notes
Revision as of 23:40, 28 January 2022 by Lianyingzhao (talk | contribs)
Jump to navigation Jump to search

In this tutorial, you will learn about how user accounts and logging in work through exploring 3000userlogin.c. You’ll also have a better understanding of the permission system and the shell/terminal environment.

Make sure you use the original code from 3000userlogin for each question/task.

Tutorials are graded based on participation and effort (so no need to try to have the “correct” answers — what matters is the process), but you should still turn in your work. Submit your answers on Brightspace as a single text file named "<username>-comp3000-t4.txt" (where username is your MyCarletonOne username). The first four lines of this file should be "COMP 3000 Tutorial 4", your name, student number, and the date of submission.

The deadline is usually four days after the tutorial date (see the actual due date and time on the submission entry). Note that the submission entry is enforced by the system, so you may fail to get the effort marks even if it is one minute past the deadline.

You should also check in with your assigned TA online (by responding to the poll in the Teams channel tutorials-public or the private channel). Your TA will be your first point of contact when you have questions or encounter any issues during the tutorial session.

You get 1.5 marks for submitting answers that shows your effort and 0.5 for checking in, making this tutorial worth 2 points total.

Logging in to a Linux system

As mentioned in the lecture, in order to log in to a Linux-like system (including UNIX), the following steps must occur (potentially not in this order).

  1. The user must use a terminal to connect to the system, locally or remotely (e.g., initiated by /usr/sbin/getty)
  2. The user must authenticate themselves, proving an identify with authorized access to the system. By default, this is done through a username and password (e.g., by exec’ing /usr/bin/login).
  3. After authentication, login changes uid and gid to that of the new user.
  4. Login sets up other aspects of the user's context (mainly setting key environment variables).
  5. Login does an exec of the user's chosen/default shell.

3000userlogin is a basic implementation of steps 3-5. (Steps 1 and 2 are accomplished by running 3000userlogin in a shell, as running an external command means the shell also creates a new process for it, assuming an authenticated session with a terminal.)

When 3000userlogin is properly compiled and set up, you can run:

./3000userlogin student

and you'll be logged in (again) as "student". This only works when you’re already logged in as student (see below).

You can add a user with the adduser command. For example, to create the user "someuser":

sudo adduser someuser

Note you'll have to answer several questions. Then you need to switch to someuser (su someuser).

If you just compile 3000userlogin normally, you won't be able to log in as anyone except the current user (be it student or someuser). To set up 3000userlogin properly, do the following:

sudo chown root:root 3000userlogin
sudo chmod u+s 3000userlogin

Feel free to read the man pages for both commands to learn more.

The chown command makes the binary owned by root, and the chmod command makes it setuid. Thus, when the program is exec'd it will have an effective user ID of root (euid=0). To facilitate the following tasks, you may also want to create different versions of 3000userlogin by naming them, e.g., 3000userlogin.setuid and 3000userlogin.orig, of your choice.

Tasks/Questions

  1. Compile and setup 3000userlogin as described above. Create a new user account. Verify that you can use 3000userlogin to login as the new user without typing the password of the new user (without logging in beforehand). Think about why you are able to avoid typing the password and still get authenticated.
  2. Why is the syntax highlighting gone (coloring characters)? What simple change (one line) can you make to have 3000userlogin preserve the colors? (Hint: check the environment variables)
  3. What is returned as the user's password by getpwnam() (in terms of type)? Is this what you expected? Read the manual page as you find necessary.
  4. Compare the uid, gid, euid, egid when running 3000userlogin as a regular user (this means without the “chown” and “chmod” commands above), running it setuid root, and running it as root (i.e., running it from a root shell without the setuid bit set, e.g., “sudo ./3000userlogin someuser”). Also check to see what happens when you set the setgid bit.
  5. Why does 3000userlogin change its gid before changing its uid? What happens if you switch the order of these operations?
  6. Make 3000userlogin use the shell that is specified in the user's password entry (which is actually read from /etc/passwd). Check by setting someuser’s shell to a new shell and then see if that new shell runs when you run 3000userlogin. You can change a user's shell with the chsh command.
  7. Can you set 3000shell to be a user's default shell? What changes do you have to make for chsh to accept 3000shell? Does anything obvious break when running 3000shell this way, and how can you change 3000userlogin to fix it?
  8. Does a user's default shell have to be a regular shell? Could it instead be an arbitrary program? How do you know?
  9. Are the environment variables set by 3000userlogin the only environment variables that are set (visible) after you successfully login? How do you know?
  10. Note that 3000userlogin uses environ not envp (as an argument to main()) to access environment variables. Why not use envp? (Try changing the code to use envp and see what happens: worse or better?)

Optional/advanced tasks

  1. Prompt for the user's password before logging the user in (Step 2 above). To do this, you'll need to get the right password hash and then figure out how to hash the entered password to check if it matches. Recall the format of /etc/shadow and you may need to learn how to use the openssl passwd command.

Note: you are not required to complete and submit work for this optional task. Doing it will enhance your understanding of how authentication works.