<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://homeostasis.scs.carleton.ca/wiki/index.php?action=history&amp;feed=atom&amp;title=Operating_Systems_2019W_Lecture_20</id>
	<title>Operating Systems 2019W Lecture 20 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://homeostasis.scs.carleton.ca/wiki/index.php?action=history&amp;feed=atom&amp;title=Operating_Systems_2019W_Lecture_20"/>
	<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=Operating_Systems_2019W_Lecture_20&amp;action=history"/>
	<updated>2026-04-05T17:00:50Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=Operating_Systems_2019W_Lecture_20&amp;diff=22295&amp;oldid=prev</id>
		<title>Soma: Created page with &quot;==Video==  Video from the lecture given on March 27, 2019 [https://homeostasis.scs.carleton.ca/~soma/os-2019w/lectures/comp3000-2019w-lec20-20190327.m4v is now available].  ==...&quot;</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=Operating_Systems_2019W_Lecture_20&amp;diff=22295&amp;oldid=prev"/>
		<updated>2019-03-27T21:33:43Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;==Video==  Video from the lecture given on March 27, 2019 [https://homeostasis.scs.carleton.ca/~soma/os-2019w/lectures/comp3000-2019w-lec20-20190327.m4v is now available].  ==...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
Video from the lecture given on March 27, 2019 [https://homeostasis.scs.carleton.ca/~soma/os-2019w/lectures/comp3000-2019w-lec20-20190327.m4v is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 20&lt;br /&gt;
----------&lt;br /&gt;
&lt;br /&gt;
Demonstration of TOCTTOU and its security implications&lt;br /&gt;
&lt;br /&gt;
log-append.c is the program&lt;br /&gt;
 * appends a given message to the end of a file specified on the command line&lt;br /&gt;
 * logs the append event to a sysem log file owned by root&lt;br /&gt;
&lt;br /&gt;
So we want the program to only append to files the user can access&lt;br /&gt;
...but we also want to modify a file that is owned by root&lt;br /&gt;
&lt;br /&gt;
Solution is to make the program setuid-root, but check ownership of file before&lt;br /&gt;
opening it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Journaled and Log-structured filesystems&lt;br /&gt;
----------------------------------------&lt;br /&gt;
&lt;br /&gt;
journaled filesystems are a solution to the slow fsck problem&lt;br /&gt;
&lt;br /&gt;
they exploit a quirk of mass storage devices&lt;br /&gt;
 - sequential writes are much faster than random writes&lt;br /&gt;
&lt;br /&gt;
Metadata (and sometimes data) writes occur twice on journaled filesystems&lt;br /&gt;
 - once to the journal&lt;br /&gt;
 - once to the regular filesystem&lt;br /&gt;
&lt;br /&gt;
On an unclean shutdown, you just have to check the journal&lt;br /&gt;
&lt;br /&gt;
Journaled filesystems make sense on a system dominated by reads&lt;br /&gt;
&lt;br /&gt;
If you have mostly writes, then you&amp;#039;ve lost half your performance&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Why not *only* use the journal, make it the entire disk?&lt;br /&gt;
 - that&amp;#039;s a log-structured filesystem&lt;br /&gt;
&lt;br /&gt;
Log-structured filesystems are hard because you have to track what data is current - and where to find it.  And what about running out of space?&lt;br /&gt;
 - if you clean up periodically, then it can work&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot; line&amp;gt;&lt;br /&gt;
/* log-append.c&lt;br /&gt;
&lt;br /&gt;
   A program to demonstrate TOCTTOU issues&lt;br /&gt;
&lt;br /&gt;
   Usage: log-append &amp;lt;file&amp;gt; &amp;lt;message&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   When run, adds message to the end of file.  It then also records a&lt;br /&gt;
   record of this action to /var/log/comp3000-append.log&lt;br /&gt;
&lt;br /&gt;
   Note the program must be setuid root in order to add entries to&lt;br /&gt;
   the log file (which is owned by root).  But, it should only add entries&lt;br /&gt;
   to files that the current user can write to.&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define LOGFILE &amp;quot;/var/log/append.log&amp;quot;&lt;br /&gt;
&lt;br /&gt;
void usage(char *message)&lt;br /&gt;
{&lt;br /&gt;
        fprintf(stderr, &amp;quot;ERROR: %s\n&amp;quot;, message);&lt;br /&gt;
        fprintf(stderr, &amp;quot;Usage:  log-append &amp;lt;file&amp;gt; &amp;lt;message&amp;gt;\n&amp;quot;);&lt;br /&gt;
        exit(-1);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void test_root_privileges(void)&lt;br /&gt;
{&lt;br /&gt;
        if (geteuid() != 0) {&lt;br /&gt;
                fprintf(stderr, &amp;quot;Not running with root privileges, exiting.\n&amp;quot;);&lt;br /&gt;
                exit(-1);&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
char *get_username(void)&lt;br /&gt;
{&lt;br /&gt;
        // FIXME: this should get the username from the password file!&lt;br /&gt;
&lt;br /&gt;
        char *username;&lt;br /&gt;
        char *default_username = &amp;quot;UNKNOWN&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
        username = getenv(&amp;quot;USER&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
        if (username == NULL) {&lt;br /&gt;
                username = default_username;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return username;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void log_append(char *username, char *filename)&lt;br /&gt;
{&lt;br /&gt;
        FILE *f;&lt;br /&gt;
        &lt;br /&gt;
        f = fopen(LOGFILE, &amp;quot;a&amp;quot;);&lt;br /&gt;
        if (!f) {&lt;br /&gt;
                fprintf(stderr, &amp;quot;Could not open log for writing.\n&amp;quot;);&lt;br /&gt;
                exit(-1);&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        fprintf(f, &amp;quot;%s appended to %s\n&amp;quot;, username, filename);&lt;br /&gt;
        fclose(f);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void safe_append(char *filename, char *message)&lt;br /&gt;
{&lt;br /&gt;
        FILE *f;&lt;br /&gt;
        int allowed;&lt;br /&gt;
&lt;br /&gt;
        if (access(filename, W_OK)) {&lt;br /&gt;
                fprintf(stderr, &amp;quot;You aren&amp;#039;t allowed to append to %s\n&amp;quot;,&lt;br /&gt;
                        filename);&lt;br /&gt;
                exit(-1);&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        f = fopen(filename, &amp;quot;a&amp;quot;);&lt;br /&gt;
        if (!f) {&lt;br /&gt;
                fprintf(stderr, &amp;quot;Could not open %s for appending.\n&amp;quot;, filename);&lt;br /&gt;
                exit(-1);&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        fprintf(f, &amp;quot;%s\n&amp;quot;, message);&lt;br /&gt;
        fclose(f);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
        char *username, *filename, *message;&lt;br /&gt;
&lt;br /&gt;
        test_root_privileges();&lt;br /&gt;
&lt;br /&gt;
        if (argc &amp;lt; 3) {&lt;br /&gt;
                usage(&amp;quot;Not enough arguments&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        filename = argv[1];&lt;br /&gt;
        message = argv[2];&lt;br /&gt;
        &lt;br /&gt;
        username = get_username();&lt;br /&gt;
        &lt;br /&gt;
        safe_append(filename, message);&lt;br /&gt;
        log_append(username, filename);&lt;br /&gt;
        &lt;br /&gt;
        printf(&amp;quot;Message appended to %s\n&amp;quot;, filename);&lt;br /&gt;
        &lt;br /&gt;
        return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Soma</name></author>
	</entry>
</feed>