Operating Systems 2021F: Tutorial 5

From Soma-notes
Jump to navigation Jump to search

In this tutorial you will learn about how accounts and logging in work through exploring 3000userlogin and ssh.

Logging in to a UNIX system

In order to log in to a UNIX system (Linux or otherwise), the following steps must occur (potentially not in this order).

  1. The user must authenticate themselves, proving their identity and that they are allowed to access the system. By default this is done through a username and password.
  2. A new process, U, should be created for the authenticated user.
  3. The login program must establish communication with the user via some communications channel. Normally this channel will be a device. Standard in, out, and error for U should be connected to this device.
  4. U changes uid and gid to that of the new user.
  5. U sets up other aspects of the user's context (mainly setting key environment variables).
  6. U does an execve of the user's chosen shell.

3000userlogin is a basic implementation of steps 4-6. (Steps 2 and 3 are accomplished by running 3000userlogin in a shell, as running an external command means the shell first creates a new process, as we have seen. We are skipping 1.)

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

 ./3000userlogin someuser

and you'll be logged in as "someuser", assuming someuser exists.

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.

If you just compile 3000userlogin normally, you won't be able to log in as anyone except the current user. To compile and set up 3000userlogin, do the following:

 gcc -O -Wall 3000userlogin.c -o 3000userlogin
 sudo chown root:root 3000userlogin
 sudo chmod u+s 3000userlogin

The chown command makes the binary owned by root, and the chmod command makes it setuid. Thus, when the program is execve'd it will have an effective user ID of root (euid=0).

Alternately, you can download the code and use the associated makefile to build by doing the following:

 wget https://homeostasis.scs.carleton.ca/~soma/os-2020w/code/tut4.tar.gz
 tar xzf tut4.tar.gz
 cd tut4
 make
 make setuid

SSH

Here you will be learning about ssh (openssh), the standard program for accessing UNIX systems remotely. It is a more secure replacement for older technologies such as rsh and telnet.

Setup

Install the openssh-server package (note on openstack it will already be installed, but on a default Ubuntu desktop install it won't be):

sudo apt-get install openssh-server

Create a second user in the virtual machine named "other" (or any other name you wish to use):

sudo adduser other

(Answer the subsequent prompts however you wish, just remember the password.)

At this point you should be able to log in to the "other" account using ssh:

ssh other@localhost

You'll have to enter your password.

If you don't get a password prompt, password authentication has probably been disabled. (Password authentication has been disabled on the openstack VMs.) To enable it, do the following

 sudo nano /etc/ssh/sshd_config    (or vi, or emacs)

In the editor change the line "PasswordAuthentication no" to "PasswordAuthentication yes". Then, to restart sshd:

 sudo service sshd restart

Be sure to change it back after you've set up public key authentication!

Public key authentication and ssh

Create a public key file for your account (as user student, ubuntu, or your personal account):

ssh-keygen

(Accept the default filename and choose at least a simple passphrase.)

You just created a certificate! (A certificate is just a public key with metadata.)

Copy the key to the other account:

cat ~/.ssh/id_rsa.pub >> authorized_keys
scp authorized_keys other@localhost:.
rm authorized_keys
ssh other@localhost
(as user other)
mkdir ~/.ssh (if it doesn't exist already)
chmod 700 ~/.ssh  (make it private)
mv ~/authorized_keys ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Now you can log in to user other by typing in the passphrase you used to lock the key you generated.

To avoid entering this passphrase every time, you can give it to the authentication agent (generally, ssh-agent) that was started when you logged in:

 ssh-add

Note I expect the above to be a bit confusing. Do look around for resources on public key cryptography; however, you may find that playing around with ssh authentication may help you understand things better. In particular, try using "-v" (verbose) with ssh.

Tasks

  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.
  2. What is returned as the user's password by getpwnam()? Is this what you expected?
  3. Compare the uid, gid, euid, egid when running 3000userlogin as a regular user, running it setuid root, and running it as root (i.e., running it from a root shell without the setuid bit set). Also check to see what happens when you set the setgid bit (with different group IDs on the file).
  4. Why does 3000userlogin change its gid before changing its uid? What happens if you switch the order of these operations?
  5. Make 3000userlogin use the shell that is specified in the user's password entry. Check by making a new user and setting its 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.
  6. 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?
  7. Does a user's default shell have to be a regular shell? Could it instead be an arbitrary program? How do you know?
  8. How important is each of the environment variables that is set by 3000userlogin? Are these the only environment variables that are set after you successfully login?
  9. 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.)
  10. Use bpftrace /usr/local/share/bpftrace/tools/setuids.bt to see what system calls 3000userlogin is using to change its uid. Is it what you expect given the source of 3000login? How does this compare to the system calls ssh uses when a user logs in?
  11. Setup password-less login to your local system between the student and other account (or any two other accounts), following the above instructions. Note that the "local" account is the one you are running the ssh command on.
  12. Setup password-less login to access.scs.carleton.ca, following the instructions above.
  13. Secure shell can be used to directly run a command on another program rather than the default shell. For example, try "ssh student@localhost bc -l" and see that bc is run. When you do this, does ssh directly run the specified command or does it first run a shell and have the shell run the command? Use opensnoop.bt or execsnoop.bt to verify.
  14. What files in /etc does bash access when you login via 3000userlogin? How does this compare to what bash opens when you log in via ssh?
  15. (Optional): Prompt for the user's password before logging the user in. 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. See online guides about how passwords are stored on Linux for help.

Code

Download code for this tutorial (3000userlogin.c and Makefile)

3000userlogin.c

Download 3000userlogin.c

 1 /* 3000userlogin.c */
 2 /* version 0.1 */
 3 
 4 #include <stdio.h>
 5 #include <sys/types.h>
 6 #include <unistd.h>
 7 #include <stdlib.h>
 8 #include <pwd.h>
 9 
10 int main(int argc, char *argv[])
11 {
12         int result;
13         char *shell_argv[3];
14         char *username;
15         extern char **environ;
16         struct passwd *pw_entry;
17                 
18         if (argc < 2) {
19                 fprintf(stderr, "Usage: %s <username>\n", argv[0]);
20                 exit(-1);
21         }
22 
23         printf("uid=%d, euid=%d, gid=%d, egid=%d\n",
24                getuid(), geteuid(), getgid(), getegid());
25 
26         username = argv[1];
27         
28         pw_entry = getpwnam(username);
29         if (pw_entry == NULL) {
30                 fprintf(stderr, "Could not find user %s.\n", username);
31                 exit(-2);
32         }
33                        
34         result = setgid(pw_entry->pw_gid);
35         if (result != 0) {
36                 fprintf(stderr, "Failed to change to gid %d\n",
37                         pw_entry->pw_gid);
38                 exit(-3);
39         }
40 
41         result = setuid(pw_entry->pw_uid);
42         if (result != 0) {
43                 fprintf(stderr, "Failed to change to uid %d\n",
44                         pw_entry->pw_uid);
45                 exit(-4);
46         }
47 
48         result = chdir(pw_entry->pw_dir);
49         if (result != 0) {
50                 fprintf(stderr, "Failed to change to home dir %s\n",
51                         pw_entry->pw_dir);
52                 exit(-5);
53         }
54 
55         shell_argv[0] = "bash";
56         shell_argv[1] = "--login";
57         shell_argv[2] = NULL;
58 
59         clearenv();
60         setenv("USERNAME", pw_entry->pw_name, 1);
61         setenv("PATH", "/usr/bin:/bin", 1);
62         setenv("SHELL", "/bin/bash", 1);
63         setenv("HOME", pw_entry->pw_dir, 1);
64         setenv("COMP3000", "yes", 1);
65         
66         execve("/bin/bash", shell_argv, environ);
67      
68         fprintf(stderr, "Failed to exec bash\n");
69         return -6;
70 }

Makefile

1 .PHONY: setuid
2 
3 3000userlogin: 3000userlogin.c
4 	gcc -Wall -O 3000userlogin.c -o 3000userlogin
5 
6 setuid: 3000userlogin
7 	@echo "The following changes ownership to root, group to root, and sets the setuid bit on 3000userlogin:"
8 	sudo chown root:root 3000userlogin && sudo chmod u+s 3000userlogin
9 	@echo "3000userlogin is now setuid root!"