Operating Systems 2021F: Tutorial 7

From Soma-notes
Jump to navigation Jump to search

In this tutorial you'll be learning about special files and how to create one kind of special file, a character device, using Linux kernel modules.

Special Files

In this part your goal is to learn how special files are similar and different from regular files.

Learning Objectives

  • What does a special file represent?
  • Can you have multiple special files that are somehow "the same"? What does it mean to "copy" a special file?
  • What are character devices used for? How can you interact with them?
  • What are named pipes? How can they be used?
  • What kind of device is a tty? How are tty's different from regular files? How are they similar?

Important Tips

  • There is a chance your VM will crash or your ssh session will become unresponsive when messing with the kernel. To recover, you can reboot your VM from the OpenStack web console.
  • Definitely do not under any circumstances attempt this on your own system. This is your last warning.
  • If you run out of disk space, your kernel logs are probably too big or you have other large files. Be sure to free disk space using these instructions.
  • Remember to recompile and insert your module after making changes.
  • Work incrementally and be prepared for your system to crash.
  • It might be wise to code on your own computer and use scp or sshfs to transfer the files to your VM.
  • https://elixir.bootlin.com/ is a great resource for learning about kernel functions and data structures.

A: Understanding special files

  1. Try the following commands as a non-privileged user. What does each do? How do the files f1-f5 compare? How do they compare to /dev/urandom? Remember you can get output from /dev/urandom using cat or dd. If you use cat, make sure to pipe it to less!
    1. cp /dev/urandom f1
    2. cp -a /dev/urandom f2
    3. sudo cp -a /dev/urandom f3
    4. mknod f4 c 1 9
    5. sudo mknod f5 c 1 9

    Note that some commands may run for a long time and may create large files. If they do, you'll want to terminate them and delete any files they create (after examining them).

  2. Make named pipes using mknod and mkfifo. Use them to simulate ls | wc using just the > and < operators.
  3. Make a block device myroot representing your machine's root filesystem. Get info on its filesystem using dumpe2fs myroot. Do you need to be root to do these operations? (Hint: if you use df to find the device, you may have to follow a symlink to get to the real block device.)
  4. Use mknod to make a copy of your current terminal's tty. Examine its characteristics using stty --file=mytty. Do the same for the original tty. How can you figure out what your current tty is? (NOTE: On modern Linux systems the copy often doesn't behave like the original. Why do you think this is the case?)
  5. Run stty --help to see what you can do with stty. Try disabling local echo. How does the shell behave with echo disabled? How can you restore echo without logging out and back in?

Linux kernel modules

In this part of the tutorial you will be building and installing kernel modules. You will need root access to install kernel modules.

It is highly recommended that you use a comp3000 openstack instance for the exercises below for two reasons. First, you may have difficulties compiling kernel modules on other systems. Second, these operations are potentially dangerous and mistakes could destroy all data on the Linux system. Consider yourself warned!

This is the first tutorial where you are seeing kernel code. By the end of tutorial you should be able to run all of the code here and be able to make trivial modifications. Lectures later this week will go through this code in detail. Learning as much as you can about the code now will help you prepare for the upcoming lectures. In particular, try to figure out what is different about this code.

If you cannot build a module you may have installed a version of Ubuntu that is too minimal. But you can fix it by installing the right packages. Do the following:

 sudo apt update
 sudo apt dist-upgrade
 sudo apt install build-essential
 sudo apt clean

When upgrading you may get a message when upgrading grub about which boot device to use. Select /dev/vda or /dev/sda - don’t select the devices that end with a number.

If your build failed before doing this and again after, delete the downloaded code and unpack the zip file again. (The build process generates hidden files which can mess up later builds.)

Learning Objectives

  • How do you compile and install kernel modules? How does this process differ from userspace C programs?
  • How does the source and build process of kernel modules differ from C programs?
  • Understand when module code is and isn't run.
  • Understand how kernel modules can access and manipulate kernel data structures and call kernel functions.
  • Understand how making system calls from userspace can cause module functions to be called.
  • Understand how character devices can be implemented in a module and thus better understand what a character device is.
  • Understand how processes are represented by task_struct's in the Linux kernel, what kinds of information is stored in a task struct, and how to access the task_struct of a process that made a system call.

B: A simple kernel module

  1. Download the source for this simple module, unpack, and build it by typing "make". Use wget to download the zip file.
  2. Install the module using "sudo insmod simple.ko". The hello message is recorded in the kernel logs. How do you view the kernel logs?
  3. Check to see that the module has been loaded. How do you do this?
  4. Remove the module from the kernel. What did you do?
  5. Open a second terminal and run trace 'p::printk printf "%s" arg1'. Repeat the above three questions and note the output in the terminal running trace. What does this trace command do?

C: A character device kernel module

  1. Download the source for ones, a kernel module implementing a character device that ouputs an unbounded string of "1"'s. Build, compile, and run it as before.
  2. What kernel messages does the module generate? Does it create any new files (other than /dev/ones)? If so, where?
  3. What happens when you "cat" the device /dev/ones? How can you limit the output?
  4. How can you modify your module to generate a kernel "Oops" as reported in the kernel logs or outright crash the kernel?
  5. Before inserting your module, run trace -K 'p::__register_chrdev' in another terminal to print the kernel stack trace for __register_chrdev. Insert your module and examine the output from your trace command. Did you expect all those function calls? What do you think they do?

D: Getting process information from a module

To answer these questions on newgetpid.c, you'll need to refer to the Linux kernel source, particularly kernel/sys.c. (These are links to a cross-referenced version of the Linux kernel source, a key resource for understanding and developing kernel code.)

  1. Download the source newgetpid.c. Build and run it as before.
  2. What type is "current"? How can you figure this out?
  3. Modify newgetpid.c so that it creates a device file /dev/describe rather than /dev/newgetpid.
  4. Make /dev/describe output the calling process's parent ID (ppid), user ID (uid), group ID (gid), effective user ID (euid), and effective group ID (egid).
  5. (Advanced) Modify /dev/describe so that if you write a process ID to it, it will output the information on the provided process. To make this work, you'll need to:
    • Add a write method by adding a write operation to the file operations struct. Write operations have the same prototype as read operations, except the buffer is marked constant (because it shouldn't be modified).
    • Convert the written text to an integer and store in a global variable (to the module). Check out kstrtoint_from_user().
    • Find the right task struct. See the implementation of the kill system call, and how it looks up the pid struct and then gets the right task struct using that pid struct.
    • After returning info on the selected process, further calls should return info on the current process. You can do this by setting the global process ID to 0 and checking this value, using the current task if it is zero.

Code

simple module

 1 #include <linux/kernel.h>
 2 #include <linux/init.h>
 3 #include <linux/module.h>
 4 
 5 MODULE_LICENSE("GPL");
 6 MODULE_AUTHOR("Anil Somayaji <soma@scs.carleton.ca>");
 7 MODULE_DESCRIPTION("A simple module");
 8 
 9 static int simple_init(void)
10 {
11         pr_info("Hello kernel world!\n");
12         return 0;
13 }
14 
15 static void simple_exit(void)
16 {
17         pr_info("Goodbye kernel world.\n");
18         return;
19 }
20 
21 module_init(simple_init);
22 module_exit(simple_exit);

ones module

  1 /* Code derived from:
  2   https://appusajeev.wordpress.com/2011/06/18/writing-a-linux-character-device-driver/
  3   and
  4   http://pete.akeo.ie/2011/08/writing-linux-device-driver-for-kernels.html
  5 */
  6 
  7 #include <linux/module.h>
  8 #include <linux/string.h>
  9 #include <linux/fs.h>
 10 #include <linux/device.h>
 11 #include <linux/init.h>
 12 #include <linux/kernel.h>
 13 #include <asm/uaccess.h>
 14 
 15 #define dbg(format, arg...) do { if (debug) pr_info(CLASS_NAME ": %s: " format, __FUNCTION__, ## arg); } while (0)
 16 #define err(format, arg...) pr_err(CLASS_NAME ": " format, ## arg)
 17 #define info(format, arg...) pr_info(CLASS_NAME ": " format, ## arg)
 18 #define warn(format, arg...) pr_warn(CLASS_NAME ": " format, ## arg)
 19 #define alert(format, arg...) pr_alert(CLASS_NAME ": " format, ## arg)
 20 
 21 #define DEVICE_NAME "ones"
 22 #define CLASS_NAME "comp3000"
 23 
 24 static struct class* ones_class = NULL;
 25 static struct device* ones_device = NULL;
 26 static int ones_major;
 27 
 28 static int ones_open(struct inode *the_inode, struct file *f)
 29 {
 30         return 0;
 31 }
 32 
 33 static ssize_t ones_read(struct file *f, char *buf, size_t len, loff_t *offset)
 34 {
 35         size_t i;
 36 
 37         for (i = 0; i < len; i++) {
 38                 put_user('1', buf++);
 39         }
 40 
 41         return i;
 42 }
 43 
 44 static int ones_release(struct inode *the_inode, struct file *f)
 45 {
 46         alert("Ones device closed\n");
 47         return 0;
 48 }
 49 
 50 
 51 static struct file_operations ones_fops = {
 52         .open = ones_open,
 53         .read = ones_read,
 54         .release = ones_release,
 55 };
 56 
 57 
 58 static char *ones_devnode(struct device *dev, umode_t *mode)
 59 {
 60         if (mode)
 61 	        *mode = 0444;
 62         return NULL;
 63 }
 64 
 65 static int ones_init(void)
 66 {
 67         int retval;
 68   
 69         ones_major = register_chrdev(0, DEVICE_NAME, &ones_fops);
 70         if (ones_major < 0) {
 71                 err("failed to register device: error %d\n", ones_major);
 72                 retval = ones_major;
 73                 goto failed_chrdevreg;
 74         }
 75  
 76         ones_class = class_create(THIS_MODULE, CLASS_NAME);
 77         if (IS_ERR(ones_class)) {
 78                 err("failed to register device class '%s'\n", CLASS_NAME);
 79                 retval = PTR_ERR(ones_class);
 80                 goto failed_classreg;
 81         }
 82  
 83 	ones_class->devnode = ones_devnode;
 84 
 85         ones_device = device_create(ones_class, NULL, MKDEV(ones_major, 0),
 86                                     NULL, DEVICE_NAME);
 87 
 88         if (IS_ERR(ones_device)) {
 89                 err("failed to create device '%s'\n", DEVICE_NAME);
 90                 retval = PTR_ERR(ones_device);
 91                 goto failed_devreg;
 92         }
 93         
 94         info("Ones device registered using major %d.\n", ones_major);
 95         
 96         return 0;
 97         
 98  failed_devreg:
 99         class_unregister(ones_class);
100  failed_classreg:
101         unregister_chrdev(ones_major, DEVICE_NAME);
102  failed_chrdevreg:
103         return -1;
104 }
105 
106 static void ones_exit(void)
107 {
108         device_destroy(ones_class, MKDEV(ones_major, 0));
109         class_unregister(ones_class);
110         unregister_chrdev(ones_major, "ones");
111         info("Unloading Ones module.\n");
112         return;
113 }
114 
115 module_init(ones_init);
116 module_exit(ones_exit);
117 
118 MODULE_LICENSE("GPL");
119 MODULE_AUTHOR("Anil Somayaji <soma@scs.carleton.ca>");
120 MODULE_DESCRIPTION("A write ones character device module");

newgetpid module

  1 /* Code derived from:
  2   https://appusajeev.wordpress.com/2011/06/18/writing-a-linux-character-device-driver/
  3   and
  4   http://pete.akeo.ie/2011/08/writing-linux-device-driver-for-kernels.html
  5 */
  6 
  7 #include <linux/module.h>
  8 #include <linux/string.h>
  9 #include <linux/fs.h>
 10 #include <linux/device.h>
 11 #include <linux/init.h>
 12 #include <linux/kernel.h>
 13 #include <linux/sched.h>
 14 #include <asm/uaccess.h>
 15 
 16 #define dbg(format, arg...) do { if (debug) pr_info(CLASS_NAME ": %s: " format, __FUNCTION__, ## arg); } while (0)
 17 #define err(format, arg...) pr_err(CLASS_NAME ": " format, ## arg)
 18 #define info(format, arg...) pr_info(CLASS_NAME ": " format, ## arg)
 19 #define warn(format, arg...) pr_warn(CLASS_NAME ": " format, ## arg)
 20 #define alert(format, arg...) pr_alert(CLASS_NAME ": " format, ## arg)
 21 
 22 
 23 #define DEVICE_NAME "newgetpid"
 24 #define CLASS_NAME "comp3000"
 25 
 26 static struct class* newgetpid_class = NULL;
 27 static struct device* newgetpid_device = NULL;
 28 static int newgetpid_major;
 29 
 30 static int newgetpid_open(struct inode *the_inode, struct file *f)
 31 {
 32         return 0;
 33 }
 34 
 35 static ssize_t newgetpid_read(struct file *f, char *buf, size_t len, loff_t *offset)
 36 {
 37         size_t i, msglen;
 38         pid_t thepid;
 39 
 40         char message[100];
 41         
 42         if (*offset > 0) {
 43                 return 0;
 44         }
 45         
 46         thepid = task_tgid_vnr(current);
 47 
 48         snprintf(message, 100, "Your PID is %d!\n", thepid);
 49         
 50         msglen = strlen(message);
 51 
 52         if (len < msglen) {
 53                 msglen = len;
 54         }
 55 
 56         for (i = 0; i < msglen; i++) {
 57                 put_user(message[i], buf++);
 58         }
 59 
 60         *offset = i;
 61 
 62         return i;
 63 }
 64 
 65 static int newgetpid_release(struct inode *the_inode, struct file *f)
 66 {
 67         alert("Newgetpid device closed\n");
 68         return 0;
 69 }
 70 
 71 
 72 static struct file_operations newgetpid_fops = {
 73         .open = newgetpid_open,
 74         .read = newgetpid_read,
 75         .release = newgetpid_release,
 76 };
 77 
 78 
 79 static char *newgetpid_devnode(struct device *dev, umode_t *mode)
 80 {
 81         if (mode)
 82 	        *mode = 0444;
 83         return NULL;
 84 }
 85 
 86 static int __init newgetpid_init(void)
 87 {
 88         int retval;
 89   
 90         newgetpid_major = register_chrdev(0, DEVICE_NAME, &newgetpid_fops);
 91         if (newgetpid_major < 0) {
 92                 err("failed to register device: error %d\n", newgetpid_major);
 93                 retval = newgetpid_major;
 94                 goto failed_chrdevreg;
 95         }
 96  
 97         newgetpid_class = class_create(THIS_MODULE, CLASS_NAME);
 98         if (IS_ERR(newgetpid_class)) {
 99                 err("failed to register device class '%s'\n", CLASS_NAME);
100                 retval = PTR_ERR(newgetpid_class);
101                 goto failed_classreg;
102         }
103  
104 	newgetpid_class->devnode = newgetpid_devnode;
105 
106         newgetpid_device = device_create(newgetpid_class, NULL, MKDEV(newgetpid_major, 0),
107                                     NULL, DEVICE_NAME);
108 
109         if (IS_ERR(newgetpid_device)) {
110                 err("failed to create device '%s'\n", DEVICE_NAME);
111                 retval = PTR_ERR(newgetpid_device);
112                 goto failed_devreg;
113         }
114         
115         info("Newgetpid device registered using major %d.\n", newgetpid_major);
116         
117         return 0;
118         
119  failed_devreg:
120         class_unregister(newgetpid_class);
121  failed_classreg:
122         unregister_chrdev(newgetpid_major, DEVICE_NAME);
123  failed_chrdevreg:
124         return -1;
125 }
126 
127 static void __exit newgetpid_exit(void)
128 {
129         device_destroy(newgetpid_class, MKDEV(newgetpid_major, 0));
130         class_unregister(newgetpid_class);
131         unregister_chrdev(newgetpid_major, "newgetpid");
132         info("Unloading Newgetpid module.\n");
133         return;
134 }
135 
136 module_init(newgetpid_init);
137 module_exit(newgetpid_exit);
138 
139 MODULE_LICENSE("GPL");
140 MODULE_AUTHOR("Anil Somayaji <soma@scs.carleton.ca>");
141 MODULE_DESCRIPTION("A write newgetpid character device module");

Makefile

1 KDIR := /lib/modules/$(shell uname -r)/build
2 
3 kbuild:
4 	make -C $(KDIR) M=`pwd`
5 
6 clean:
7 	make -C $(KDIR) M=`pwd` clean

You also need a Kbuild file with the following:

1 obj-m := module.o

Replace "module.o" above with "simple.o", "ones.o", or "newgetpid.o" as appropriate.