coursework/so04.tex
author Christian Urban <urbanc@in.tum.de>
Mon, 06 Nov 2017 10:40:23 +0000
changeset 561 17cd7fdee7c8
parent 560 85521d542da4
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
558
86334134abe5 updated cw
cu
parents:
diff changeset
     1
\documentclass{article}
86334134abe5 updated cw
cu
parents:
diff changeset
     2
\usepackage{../style}
86334134abe5 updated cw
cu
parents:
diff changeset
     3
\usepackage{../langs}
86334134abe5 updated cw
cu
parents:
diff changeset
     4
86334134abe5 updated cw
cu
parents:
diff changeset
     5
\begin{document}
86334134abe5 updated cw
cu
parents:
diff changeset
     6
560
85521d542da4 updated cw
cu
parents: 558
diff changeset
     7
\section*{Hints for Solving the Coursework}
558
86334134abe5 updated cw
cu
parents:
diff changeset
     8
86334134abe5 updated cw
cu
parents:
diff changeset
     9
\begin{flushright}
86334134abe5 updated cw
cu
parents:
diff changeset
    10
\it ``I have no special talents.\\
86334134abe5 updated cw
cu
parents:
diff changeset
    11
I am only passionately curious.''\\
86334134abe5 updated cw
cu
parents:
diff changeset
    12
\small--- Albert Einstein
86334134abe5 updated cw
cu
parents:
diff changeset
    13
\end{flushright}\medskip
86334134abe5 updated cw
cu
parents:
diff changeset
    14
86334134abe5 updated cw
cu
parents:
diff changeset
    15
\noindent Many students seem to have some difficulties with this coursework.
86334134abe5 updated cw
cu
parents:
diff changeset
    16
While it can be solved with just logical
86334134abe5 updated cw
cu
parents:
diff changeset
    17
reasoning, this seems to me like learning swimming on dry land.
86334134abe5 updated cw
cu
parents:
diff changeset
    18
Why not trying out what an actual UNIX system has to say?
86334134abe5 updated cw
cu
parents:
diff changeset
    19
Seems obvious isn't it? ;o)
86334134abe5 updated cw
cu
parents:
diff changeset
    20
86334134abe5 updated cw
cu
parents:
diff changeset
    21
\subsection*{Environment}
86334134abe5 updated cw
cu
parents:
diff changeset
    22
86334134abe5 updated cw
cu
parents:
diff changeset
    23
I know at least three ways of how to set up a testing
86334134abe5 updated cw
cu
parents:
diff changeset
    24
environment without affecting my main computer, and which
86334134abe5 updated cw
cu
parents:
diff changeset
    25
should work regardless of whether you have a Windows, MacOSX
86334134abe5 updated cw
cu
parents:
diff changeset
    26
or Linux machine:
86334134abe5 updated cw
cu
parents:
diff changeset
    27
86334134abe5 updated cw
cu
parents:
diff changeset
    28
\begin{enumerate}
86334134abe5 updated cw
cu
parents:
diff changeset
    29
\item You can download Oracle's VirtualBox
86334134abe5 updated cw
cu
parents:
diff changeset
    30
86334134abe5 updated cw
cu
parents:
diff changeset
    31
\begin{center}
86334134abe5 updated cw
cu
parents:
diff changeset
    32
\url{https://www.virtualbox.org}
86334134abe5 updated cw
cu
parents:
diff changeset
    33
\end{center} 
86334134abe5 updated cw
cu
parents:
diff changeset
    34
86334134abe5 updated cw
cu
parents:
diff changeset
    35
      There are binaries for Windows and MacOSX (I only tried
86334134abe5 updated cw
cu
parents:
diff changeset
    36
      out MacOSX). In addition, you need to download a Linux
86334134abe5 updated cw
cu
parents:
diff changeset
    37
      distribution. I used a recent iso-file of an Ubuntu
86334134abe5 updated cw
cu
parents:
diff changeset
    38
      distribution. All components are free.
86334134abe5 updated cw
cu
parents:
diff changeset
    39
86334134abe5 updated cw
cu
parents:
diff changeset
    40
\item If you happen to have a Raspberry Pi laying around (I
86334134abe5 updated cw
cu
parents:
diff changeset
    41
      have two for playing music as well as for all sorts of
86334134abe5 updated cw
cu
parents:
diff changeset
    42
      rainy-afternoon distractions). The cheapest model of a
86334134abe5 updated cw
cu
parents:
diff changeset
    43
      Raspberry Pi costs around \pounds{7}. More expensive
86334134abe5 updated cw
cu
parents:
diff changeset
    44
      versions cost around \pounds{20}.
86334134abe5 updated cw
cu
parents:
diff changeset
    45
      You also need an
86334134abe5 updated cw
cu
parents:
diff changeset
    46
      SD memory card of at least 4GB, which can be bought for
86334134abe5 updated cw
cu
parents:
diff changeset
    47
      \pounds{5} or less. Some SD cards come pre-installed
86334134abe5 updated cw
cu
parents:
diff changeset
    48
      with Linux, but all can be easily loaded with Linux. The
86334134abe5 updated cw
cu
parents:
diff changeset
    49
      good thing about Raspberry Pi's is that despite their
86334134abe5 updated cw
cu
parents:
diff changeset
    50
      miniature size and small cost, they are full-fledged
86334134abe5 updated cw
cu
parents:
diff changeset
    51
      Linux computers\ldots{}exactly what is needed for such
86334134abe5 updated cw
cu
parents:
diff changeset
    52
      experiments. There are plenty Linux distributions on the
86334134abe5 updated cw
cu
parents:
diff changeset
    53
      Net that are tailored to work ``out of the box'' with
86334134abe5 updated cw
cu
parents:
diff changeset
    54
      Raspberry Pi's. 
86334134abe5 updated cw
cu
parents:
diff changeset
    55
      
86334134abe5 updated cw
cu
parents:
diff changeset
    56
\item If you have a spare memory stick laying 
86334134abe5 updated cw
cu
parents:
diff changeset
    57
      around, you can try out any of the live USB-versions
86334134abe5 updated cw
cu
parents:
diff changeset
    58
      of Linux.
86334134abe5 updated cw
cu
parents:
diff changeset
    59
      
86334134abe5 updated cw
cu
parents:
diff changeset
    60
      \begin{center}
86334134abe5 updated cw
cu
parents:
diff changeset
    61
      \url{https://en.wikipedia.org/wiki/Live_USB}
86334134abe5 updated cw
cu
parents:
diff changeset
    62
      \end{center} 
86334134abe5 updated cw
cu
parents:
diff changeset
    63
   
86334134abe5 updated cw
cu
parents:
diff changeset
    64
      The idea is to upload Linux on the USB stick, you plug
86334134abe5 updated cw
cu
parents:
diff changeset
    65
      it into your computer and boot up a Linux system without
86334134abe5 updated cw
cu
parents:
diff changeset
    66
      having to download anything to your computer. A notable
86334134abe5 updated cw
cu
parents:
diff changeset
    67
      live USB version of Linux is called Tails
86334134abe5 updated cw
cu
parents:
diff changeset
    68
      
86334134abe5 updated cw
cu
parents:
diff changeset
    69
      \begin{center}
86334134abe5 updated cw
cu
parents:
diff changeset
    70
      \url{https://tails.boum.org}
86334134abe5 updated cw
cu
parents:
diff changeset
    71
      \end{center}
86334134abe5 updated cw
cu
parents:
diff changeset
    72
86334134abe5 updated cw
cu
parents:
diff changeset
    73
      which comes with Tor pre-installed and is for people who
86334134abe5 updated cw
cu
parents:
diff changeset
    74
      need a maximum of privacy and anonymity (whistleblowers,
86334134abe5 updated cw
cu
parents:
diff changeset
    75
      dissidents). It is being said that journalists Laura
86334134abe5 updated cw
cu
parents:
diff changeset
    76
      Poitras and Glenn Greenwald used it when talking to
86334134abe5 updated cw
cu
parents:
diff changeset
    77
      Edward Snowden. Tails gives them anonymity even if their
86334134abe5 updated cw
cu
parents:
diff changeset
    78
      main system is compromised by malicious software, for
86334134abe5 updated cw
cu
parents:
diff changeset
    79
      example installed by the NSA.
86334134abe5 updated cw
cu
parents:
diff changeset
    80
86334134abe5 updated cw
cu
parents:
diff changeset
    81
      However, a live USB Linux will need some support from
86334134abe5 updated cw
cu
parents:
diff changeset
    82
      the computer (BIOS) where you plug in the USB stick. I
86334134abe5 updated cw
cu
parents:
diff changeset
    83
      know Apple computers are a bit ``special'' with this and
86334134abe5 updated cw
cu
parents:
diff changeset
    84
      would need a 3rd-party boot loader for loading operating
86334134abe5 updated cw
cu
parents:
diff changeset
    85
      systems from an USB memory stick. 
86334134abe5 updated cw
cu
parents:
diff changeset
    86
      
86334134abe5 updated cw
cu
parents:
diff changeset
    87
      An alternative is to burn a CD/DVD with a live Linux
86334134abe5 updated cw
cu
parents:
diff changeset
    88
      distribution. But perhaps CDs/DVDs are already obsolete
86334134abe5 updated cw
cu
parents:
diff changeset
    89
      technology not available to everyone. The point is that
86334134abe5 updated cw
cu
parents:
diff changeset
    90
      loading an operating system from such a media is/was
86334134abe5 updated cw
cu
parents:
diff changeset
    91
      much better supported by various computers.
86334134abe5 updated cw
cu
parents:
diff changeset
    92
86334134abe5 updated cw
cu
parents:
diff changeset
    93
\end{enumerate}
86334134abe5 updated cw
cu
parents:
diff changeset
    94
86334134abe5 updated cw
cu
parents:
diff changeset
    95
\noindent For my experiments below, I used option 2. In
86334134abe5 updated cw
cu
parents:
diff changeset
    96
earlier versions of this module I have used option 1. I have
86334134abe5 updated cw
cu
parents:
diff changeset
    97
not tried in a while option 3, but know that in the past I had
86334134abe5 updated cw
cu
parents:
diff changeset
    98
a dedicated bootloader on an Apple computer just for the
86334134abe5 updated cw
cu
parents:
diff changeset
    99
purpose of running operating systems from external disks. I
86334134abe5 updated cw
cu
parents:
diff changeset
   100
also for a long time had spare CDs laying around just for the
86334134abe5 updated cw
cu
parents:
diff changeset
   101
purpose that my (Linux) operating system got trashed enough so
86334134abe5 updated cw
cu
parents:
diff changeset
   102
that it had to be rebooted externally.
86334134abe5 updated cw
cu
parents:
diff changeset
   103
86334134abe5 updated cw
cu
parents:
diff changeset
   104
\subsection*{Setup}
86334134abe5 updated cw
cu
parents:
diff changeset
   105
86334134abe5 updated cw
cu
parents:
diff changeset
   106
Once you have Linux up and running, there are a few commands
86334134abe5 updated cw
cu
parents:
diff changeset
   107
you need to know in order to replicate the ownerships and
86334134abe5 updated cw
cu
parents:
diff changeset
   108
permissions from the question: 
86334134abe5 updated cw
cu
parents:
diff changeset
   109
86334134abe5 updated cw
cu
parents:
diff changeset
   110
\begin{itemize}
86334134abe5 updated cw
cu
parents:
diff changeset
   111
\item \texttt{useradd} creates a new user
86334134abe5 updated cw
cu
parents:
diff changeset
   112
\item \texttt{groupadd} creates a new group
86334134abe5 updated cw
cu
parents:
diff changeset
   113
\item \texttt{adduser} adds a user to a group
86334134abe5 updated cw
cu
parents:
diff changeset
   114
\item \texttt{chmod} changes the permissions of a file
86334134abe5 updated cw
cu
parents:
diff changeset
   115
\item \texttt{chown}, \texttt{chgrp} change the ownership and 
86334134abe5 updated cw
cu
parents:
diff changeset
   116
group of a file
86334134abe5 updated cw
cu
parents:
diff changeset
   117
\end{itemize}
86334134abe5 updated cw
cu
parents:
diff changeset
   118
86334134abe5 updated cw
cu
parents:
diff changeset
   119
\noindent There is also a choice to be made what to use as
86334134abe5 updated cw
cu
parents:
diff changeset
   120
microedit. If you do not want to make your hands dirty and
86334134abe5 updated cw
cu
parents:
diff changeset
   121
write a test program yourself, I recommended to use the
86334134abe5 updated cw
cu
parents:
diff changeset
   122
editors \texttt{vi} or \texttt{vim}, which is available on
86334134abe5 updated cw
cu
parents:
diff changeset
   123
pretty much every UNIX system. For a first try out, this is a
86334134abe5 updated cw
cu
parents:
diff changeset
   124
helpful choice for solving the question. However, it has a
86334134abe5 updated cw
cu
parents:
diff changeset
   125
disadvantage: it will always assume you have read permissions
86334134abe5 updated cw
cu
parents:
diff changeset
   126
to a file. To use these editors, I made a copy of them
86334134abe5 updated cw
cu
parents:
diff changeset
   127
and renamed them to \texttt{microedit}. Be careful to set the
86334134abe5 updated cw
cu
parents:
diff changeset
   128
setuid bit for \texttt{microedit}.
86334134abe5 updated cw
cu
parents:
diff changeset
   129
86334134abe5 updated cw
cu
parents:
diff changeset
   130
86334134abe5 updated cw
cu
parents:
diff changeset
   131
\subsection*{Permission Basics}
86334134abe5 updated cw
cu
parents:
diff changeset
   132
86334134abe5 updated cw
cu
parents:
diff changeset
   133
The absolute basics is how the permissions are organised
86334134abe5 updated cw
cu
parents:
diff changeset
   134
in essentially four blocks
86334134abe5 updated cw
cu
parents:
diff changeset
   135
86334134abe5 updated cw
cu
parents:
diff changeset
   136
\begin{center}
86334134abe5 updated cw
cu
parents:
diff changeset
   137
${\underbrace{\huge\texttt{-}}_{\text{\makebox[0mm]{directory}}}}
86334134abe5 updated cw
cu
parents:
diff changeset
   138
 {\underbrace{\huge\texttt{-{}-{}-}}_{\text{user}}}\,
86334134abe5 updated cw
cu
parents:
diff changeset
   139
 {\underbrace{\huge\texttt{-{}-{}-}}_{\text{group}}}\,
86334134abe5 updated cw
cu
parents:
diff changeset
   140
 {\underbrace{\huge\texttt{-{}-{}-}}_{\text{other}}}$
86334134abe5 updated cw
cu
parents:
diff changeset
   141
\end{center}
86334134abe5 updated cw
cu
parents:
diff changeset
   142
86334134abe5 updated cw
cu
parents:
diff changeset
   143
\noindent This seems to be the knowledge everybody has. But
86334134abe5 updated cw
cu
parents:
diff changeset
   144
already difficulties arise with the following fact, which
86334134abe5 updated cw
cu
parents:
diff changeset
   145
could easily be resolved by a little experiment: assume a file
86334134abe5 updated cw
cu
parents:
diff changeset
   146
is owned by Bob with permissions
86334134abe5 updated cw
cu
parents:
diff changeset
   147
86334134abe5 updated cw
cu
parents:
diff changeset
   148
\begin{center}
86334134abe5 updated cw
cu
parents:
diff changeset
   149
$\texttt{-{}r-{}-{}rw-{}rwx\;\;bob\;students\;\;file\_name}$
86334134abe5 updated cw
cu
parents:
diff changeset
   150
\end{center}
86334134abe5 updated cw
cu
parents:
diff changeset
   151
86334134abe5 updated cw
cu
parents:
diff changeset
   152
\noindent The UNIX access rules imply that Bob will only have
86334134abe5 updated cw
cu
parents:
diff changeset
   153
read access to this file, even if he is in the group students
86334134abe5 updated cw
cu
parents:
diff changeset
   154
and the group access permissions allow read and write.
86334134abe5 updated cw
cu
parents:
diff changeset
   155
Similarly every member in the students group who is not Bob,
86334134abe5 updated cw
cu
parents:
diff changeset
   156
will only have read-write access permissions, not
86334134abe5 updated cw
cu
parents:
diff changeset
   157
read-write-execute.
86334134abe5 updated cw
cu
parents:
diff changeset
   158
86334134abe5 updated cw
cu
parents:
diff changeset
   159
The question asked whether Ping, Bob and Emma can read or write
86334134abe5 updated cw
cu
parents:
diff changeset
   160
the given files \underline{\smash{using}} the program
86334134abe5 updated cw
cu
parents:
diff changeset
   161
microedit. This means we will call on the command line 
86334134abe5 updated cw
cu
parents:
diff changeset
   162
86334134abe5 updated cw
cu
parents:
diff changeset
   163
\begin{center}
86334134abe5 updated cw
cu
parents:
diff changeset
   164
$\texttt{>}\;\;\texttt{microedit}\;\textit{file\_name}$
86334134abe5 updated cw
cu
parents:
diff changeset
   165
\end{center}
86334134abe5 updated cw
cu
parents:
diff changeset
   166
86334134abe5 updated cw
cu
parents:
diff changeset
   167
\noindent for all files and for Bob, Ping and Emma. So if you
86334134abe5 updated cw
cu
parents:
diff changeset
   168
want to find out whether Bob, say, can read or write a file,
86334134abe5 updated cw
cu
parents:
diff changeset
   169
you need to find out what the access permissions with which
86334134abe5 updated cw
cu
parents:
diff changeset
   170
\texttt{microedit} is run. This would be easy, if
86334134abe5 updated cw
cu
parents:
diff changeset
   171
\texttt{microedit} did not have the setuid bit set. Then it
86334134abe5 updated cw
cu
parents:
diff changeset
   172
would be just the rights of the caller (Ping, Bob or Emma).
86334134abe5 updated cw
cu
parents:
diff changeset
   173
But your friendly lecturer arranged the question so that it
86334134abe5 updated cw
cu
parents:
diff changeset
   174
has the setuid bit. 
86334134abe5 updated cw
cu
parents:
diff changeset
   175
86334134abe5 updated cw
cu
parents:
diff changeset
   176
Recall that the setuid bit gives the program the ability to 
86334134abe5 updated cw
cu
parents:
diff changeset
   177
run with the permissions of the owner \texttt{microedit}
86334134abe5 updated cw
cu
parents:
diff changeset
   178
file, not the permissions of the caller. I wrote in the
86334134abe5 updated cw
cu
parents:
diff changeset
   179
handout
86334134abe5 updated cw
cu
parents:
diff changeset
   180
86334134abe5 updated cw
cu
parents:
diff changeset
   181
\begin{quote}\it
86334134abe5 updated cw
cu
parents:
diff changeset
   182
``The fundamental idea behind the setuid attribute is that a
86334134abe5 updated cw
cu
parents:
diff changeset
   183
file will be able to run not with the callers access rights,
86334134abe5 updated cw
cu
parents:
diff changeset
   184
but with the rights of the owner of the file.''
86334134abe5 updated cw
cu
parents:
diff changeset
   185
\end{quote}
86334134abe5 updated cw
cu
parents:
diff changeset
   186
86334134abe5 updated cw
cu
parents:
diff changeset
   187
\noindent Something similar is written in the Wikipedia
86334134abe5 updated cw
cu
parents:
diff changeset
   188
entry for setuid
86334134abe5 updated cw
cu
parents:
diff changeset
   189
86334134abe5 updated cw
cu
parents:
diff changeset
   190
\begin{center}
86334134abe5 updated cw
cu
parents:
diff changeset
   191
\url{http://en.wikipedia.org/wiki/Setuid}
86334134abe5 updated cw
cu
parents:
diff changeset
   192
\end{center} 
86334134abe5 updated cw
cu
parents:
diff changeset
   193
 
86334134abe5 updated cw
cu
parents:
diff changeset
   194
\noindent This implies for deciding whether \textit{file} is
86334134abe5 updated cw
cu
parents:
diff changeset
   195
readable or writable is not determined by the caller, but by
86334134abe5 updated cw
cu
parents:
diff changeset
   196
the permissions with which \texttt{microedit} runs. As you
86334134abe5 updated cw
cu
parents:
diff changeset
   197
might know already, and can also see in the Figure~\ref{test}
86334134abe5 updated cw
cu
parents:
diff changeset
   198
shown later, any \textit{file\_name} given on the command line
86334134abe5 updated cw
cu
parents:
diff changeset
   199
will be handed over to microedit as string. It is the
86334134abe5 updated cw
cu
parents:
diff changeset
   200
``responsibility'' of \texttt{microedit} what to do with it.
86334134abe5 updated cw
cu
parents:
diff changeset
   201
86334134abe5 updated cw
cu
parents:
diff changeset
   202
86334134abe5 updated cw
cu
parents:
diff changeset
   203
There is one caveat however: We need to find out first whether
86334134abe5 updated cw
cu
parents:
diff changeset
   204
the caller (Bob, Ping or Emma) can actually run
86334134abe5 updated cw
cu
parents:
diff changeset
   205
\texttt{microedit}---that is has execute permissions for
86334134abe5 updated cw
cu
parents:
diff changeset
   206
\texttt{microedit}. Once \texttt{microedit} runs, it will
86334134abe5 updated cw
cu
parents:
diff changeset
   207
assume the permissions of the owner of \texttt{microedit}. The
86334134abe5 updated cw
cu
parents:
diff changeset
   208
question is now whether these permissions are sufficient to
86334134abe5 updated cw
cu
parents:
diff changeset
   209
read or write the file \textit{file\_name}. The hints so far
86334134abe5 updated cw
cu
parents:
diff changeset
   210
should already be useful for answering the first three
86334134abe5 updated cw
cu
parents:
diff changeset
   211
columns.
86334134abe5 updated cw
cu
parents:
diff changeset
   212
86334134abe5 updated cw
cu
parents:
diff changeset
   213
For the other two files we have to take into account that they
86334134abe5 updated cw
cu
parents:
diff changeset
   214
are inside a directory. For directories apply special access
86334134abe5 updated cw
cu
parents:
diff changeset
   215
rules. In the handout I wrote
86334134abe5 updated cw
cu
parents:
diff changeset
   216
86334134abe5 updated cw
cu
parents:
diff changeset
   217
\begin{quote}\it
86334134abe5 updated cw
cu
parents:
diff changeset
   218
``There are already some special rules for directories and
86334134abe5 updated cw
cu
parents:
diff changeset
   219
links. If the execute attribute of a directory is \emph{not}
86334134abe5 updated cw
cu
parents:
diff changeset
   220
set, then one cannot change into the directory and one cannot
86334134abe5 updated cw
cu
parents:
diff changeset
   221
access any file inside it. If the write attribute is
86334134abe5 updated cw
cu
parents:
diff changeset
   222
\emph{not} set, then one can change existing files (provide
86334134abe5 updated cw
cu
parents:
diff changeset
   223
they are changeable), but one cannot create new files. If the
86334134abe5 updated cw
cu
parents:
diff changeset
   224
read attribute is \emph{not} set, one cannot search inside the
86334134abe5 updated cw
cu
parents:
diff changeset
   225
directory (\texttt{ls -la} does not work) but one can access an
86334134abe5 updated cw
cu
parents:
diff changeset
   226
existing file, provided one knows its name.''
86334134abe5 updated cw
cu
parents:
diff changeset
   227
\end{quote}
86334134abe5 updated cw
cu
parents:
diff changeset
   228
86334134abe5 updated cw
cu
parents:
diff changeset
   229
\noindent With this also the last two columns can be filled 
86334134abe5 updated cw
cu
parents:
diff changeset
   230
in.
86334134abe5 updated cw
cu
parents:
diff changeset
   231
86334134abe5 updated cw
cu
parents:
diff changeset
   232
% \subsection*{Advanced Permissions}
86334134abe5 updated cw
cu
parents:
diff changeset
   233
86334134abe5 updated cw
cu
parents:
diff changeset
   234
% While all hints so far should get you very close to the
86334134abe5 updated cw
cu
parents:
diff changeset
   235
% intended answers, there is one further complication arising
86334134abe5 updated cw
cu
parents:
diff changeset
   236
% from the setuid bit. The question asked:
86334134abe5 updated cw
cu
parents:
diff changeset
   237
86334134abe5 updated cw
cu
parents:
diff changeset
   238
% \begin{quote}\it 
86334134abe5 updated cw
cu
parents:
diff changeset
   239
% \ldots{}whether Ping, Bob, or Emma \underline{are able} to obtain 
86334134abe5 updated cw
cu
parents:
diff changeset
   240
% the right to read (R) or replace (W) its contents using 
86334134abe5 updated cw
cu
parents:
diff changeset
   241
% the editor microedit.
86334134abe5 updated cw
cu
parents:
diff changeset
   242
% \end{quote} 
86334134abe5 updated cw
cu
parents:
diff changeset
   243
 
86334134abe5 updated cw
cu
parents:
diff changeset
   244
% \noindent Note the underlined phrase. That means we need to
86334134abe5 updated cw
cu
parents:
diff changeset
   245
% ensure that there is no other way for Bob, Ping and Emma to
86334134abe5 updated cw
cu
parents:
diff changeset
   246
% obtain reading or writing permissions with \texttt{microedit}.
86334134abe5 updated cw
cu
parents:
diff changeset
   247
% Actually there is. Any file that has the setuid bit set will
86334134abe5 updated cw
cu
parents:
diff changeset
   248
% be called with the permissions of the owner, but once it has done
86334134abe5 updated cw
cu
parents:
diff changeset
   249
% the work, it can ``lower'' the permissions again to the
86334134abe5 updated cw
cu
parents:
diff changeset
   250
% callers rights. This is a second possibility we have to check
86334134abe5 updated cw
cu
parents:
diff changeset
   251
% whether the files become readable or writable when the 
86334134abe5 updated cw
cu
parents:
diff changeset
   252
% permissions of the caller are re-instated. In the handout
86334134abe5 updated cw
cu
parents:
diff changeset
   253
% I wrote about the setuid-program \texttt{passwd}:
86334134abe5 updated cw
cu
parents:
diff changeset
   254
86334134abe5 updated cw
cu
parents:
diff changeset
   255
% \begin{quote}\it 
86334134abe5 updated cw
cu
parents:
diff changeset
   256
% ``As an example consider again the \texttt{passwd}
86334134abe5 updated cw
cu
parents:
diff changeset
   257
% program. When started by, say the user \texttt{foo}, it has at
86334134abe5 updated cw
cu
parents:
diff changeset
   258
% the beginning the identities:
86334134abe5 updated cw
cu
parents:
diff changeset
   259
86334134abe5 updated cw
cu
parents:
diff changeset
   260
% \begin{itemize}
86334134abe5 updated cw
cu
parents:
diff changeset
   261
% \item \emph{real identity}: \texttt{foo}\\
86334134abe5 updated cw
cu
parents:
diff changeset
   262
% \emph{effective identity}: \texttt{foo}\\ 
86334134abe5 updated cw
cu
parents:
diff changeset
   263
% \emph{saved identity}: \texttt{root}
86334134abe5 updated cw
cu
parents:
diff changeset
   264
% \end{itemize}
86334134abe5 updated cw
cu
parents:
diff changeset
   265
86334134abe5 updated cw
cu
parents:
diff changeset
   266
% \noindent It is then allowed to change the effective
86334134abe5 updated cw
cu
parents:
diff changeset
   267
% identity to the saved identity to have
86334134abe5 updated cw
cu
parents:
diff changeset
   268
86334134abe5 updated cw
cu
parents:
diff changeset
   269
% \begin{itemize}
86334134abe5 updated cw
cu
parents:
diff changeset
   270
% \item \emph{real identity}: \texttt{foo}\\
86334134abe5 updated cw
cu
parents:
diff changeset
   271
% \emph{effective identity}: \texttt{root}\\ 
86334134abe5 updated cw
cu
parents:
diff changeset
   272
% \emph{saved identity}: \texttt{root}
86334134abe5 updated cw
cu
parents:
diff changeset
   273
% \end{itemize}
86334134abe5 updated cw
cu
parents:
diff changeset
   274
86334134abe5 updated cw
cu
parents:
diff changeset
   275
% \noindent It can now read and write the file
86334134abe5 updated cw
cu
parents:
diff changeset
   276
% \texttt{/etc/passwd}. After finishing the job it is supposed to
86334134abe5 updated cw
cu
parents:
diff changeset
   277
% drop the effective identity back to \texttt{foo}. This is the
86334134abe5 updated cw
cu
parents:
diff changeset
   278
% responsibility of the programmers who wrote \texttt{passwd}.
86334134abe5 updated cw
cu
parents:
diff changeset
   279
% Notice that the effective identity is not automatically
86334134abe5 updated cw
cu
parents:
diff changeset
   280
% elevated to \texttt{root}, but the program itself must make
86334134abe5 updated cw
cu
parents:
diff changeset
   281
% this change. After it has done the work, the effective
86334134abe5 updated cw
cu
parents:
diff changeset
   282
% identity should go back to the real identity.
86334134abe5 updated cw
cu
parents:
diff changeset
   283
% ''
86334134abe5 updated cw
cu
parents:
diff changeset
   284
% \end{quote}
86334134abe5 updated cw
cu
parents:
diff changeset
   285
86334134abe5 updated cw
cu
parents:
diff changeset
   286
% \noindent It was hoped by your friendly lecturer that any of
86334134abe5 updated cw
cu
parents:
diff changeset
   287
% the students would have consciously considered this
86334134abe5 updated cw
cu
parents:
diff changeset
   288
% possibility, but alas nobody did\ldots{} 
86334134abe5 updated cw
cu
parents:
diff changeset
   289
86334134abe5 updated cw
cu
parents:
diff changeset
   290
\subsection*{A Program in C}
86334134abe5 updated cw
cu
parents:
diff changeset
   291
86334134abe5 updated cw
cu
parents:
diff changeset
   292
I suggested above to use a copy of the editors \texttt{vm} or
86334134abe5 updated cw
cu
parents:
diff changeset
   293
\texttt{vim} for \texttt{microedit}. This works reasonably
86334134abe5 updated cw
cu
parents:
diff changeset
   294
well, except for one instance: if a file is not readable, then
86334134abe5 updated cw
cu
parents:
diff changeset
   295
these editors will not be helpful for checking whether the
86334134abe5 updated cw
cu
parents:
diff changeset
   296
file is writable. Giving out such a permission is a perfectly
86334134abe5 updated cw
cu
parents:
diff changeset
   297
``normal'' situation in many large UNIX systems. A user might
86334134abe5 updated cw
cu
parents:
diff changeset
   298
be allowed to write into central log files, but should not be
86334134abe5 updated cw
cu
parents:
diff changeset
   299
able to read them (otherwise they can find out what other
86334134abe5 updated cw
cu
parents:
diff changeset
   300
users did). To get around this problem, I brushed up my C
86334134abe5 updated cw
cu
parents:
diff changeset
   301
knowledge from school days and googled around for how to read
86334134abe5 updated cw
cu
parents:
diff changeset
   302
and write files. Typing in ``read write in C'' in the
86334134abe5 updated cw
cu
parents:
diff changeset
   303
all-knowing search engine, I obtained the link
86334134abe5 updated cw
cu
parents:
diff changeset
   304
86334134abe5 updated cw
cu
parents:
diff changeset
   305
\begin{center}
86334134abe5 updated cw
cu
parents:
diff changeset
   306
\url{https://www.cs.bu.edu/teaching/c/file-io/intro/}
86334134abe5 updated cw
cu
parents:
diff changeset
   307
\end{center}
86334134abe5 updated cw
cu
parents:
diff changeset
   308
86334134abe5 updated cw
cu
parents:
diff changeset
   309
\noindent which tells you pretty much everything what there is
86334134abe5 updated cw
cu
parents:
diff changeset
   310
about opening a file in C for reading and writing. (There are
86334134abe5 updated cw
cu
parents:
diff changeset
   311
certainly more and better sources for finding out how to read
86334134abe5 updated cw
cu
parents:
diff changeset
   312
and write files. This was just at my finger tips.) A little
86334134abe5 updated cw
cu
parents:
diff changeset
   313
bit more googling helped me to display the user that
86334134abe5 updated cw
cu
parents:
diff changeset
   314
determines the access permissions. Being lazy, I did not spend
86334134abe5 updated cw
cu
parents:
diff changeset
   315
a thought of refactoring the file to be as small as possible,
86334134abe5 updated cw
cu
parents:
diff changeset
   316
and also did not go the extra mile to convert the ID of the
86334134abe5 updated cw
cu
parents:
diff changeset
   317
user into a clear name.
86334134abe5 updated cw
cu
parents:
diff changeset
   318
86334134abe5 updated cw
cu
parents:
diff changeset
   319
The resulting little C program is shown in Figure~\ref{test}.
86334134abe5 updated cw
cu
parents:
diff changeset
   320
It explicitly checks for readability and writability of files.
86334134abe5 updated cw
cu
parents:
diff changeset
   321
The \pcode{main} function is organised into two parts: the
86334134abe5 updated cw
cu
parents:
diff changeset
   322
first checks readability and writability with the permissions
86334134abe5 updated cw
cu
parents:
diff changeset
   323
according to a potential setuid bit, and the second (starting
86334134abe5 updated cw
cu
parents:
diff changeset
   324
in Line 34) when the permissions are lowered to the caller.
86334134abe5 updated cw
cu
parents:
diff changeset
   325
Note that this program has one problem as well: it only gives
86334134abe5 updated cw
cu
parents:
diff changeset
   326
a reliable answer in cases a file is {\bf not} readable or
86334134abe5 updated cw
cu
parents:
diff changeset
   327
{\bf not} writable. In these cases it returns an error code 13
86334134abe5 updated cw
cu
parents:
diff changeset
   328
(permission denied). It sometimes claims a file is not
86334134abe5 updated cw
cu
parents:
diff changeset
   329
writable, say, but with an error code 26 (text file busy).
86334134abe5 updated cw
cu
parents:
diff changeset
   330
This is unrelated to the permissions of the file.
86334134abe5 updated cw
cu
parents:
diff changeset
   331
86334134abe5 updated cw
cu
parents:
diff changeset
   332
\begin{figure}[t]
86334134abe5 updated cw
cu
parents:
diff changeset
   333
\small\mbox{}\\[-14mm]
86334134abe5 updated cw
cu
parents:
diff changeset
   334
\lstinputlisting[language=C]{../progs/read.c}\mbox{}\\[-13mm]
86334134abe5 updated cw
cu
parents:
diff changeset
   335
\caption{A read/write test program in C. It returns errno = 13 
86334134abe5 updated cw
cu
parents:
diff changeset
   336
in cases when permission is denied.\label{test}}
86334134abe5 updated cw
cu
parents:
diff changeset
   337
\end{figure}
86334134abe5 updated cw
cu
parents:
diff changeset
   338
86334134abe5 updated cw
cu
parents:
diff changeset
   339
%\subsection*{Solution}
86334134abe5 updated cw
cu
parents:
diff changeset
   340
%
86334134abe5 updated cw
cu
parents:
diff changeset
   341
%\begin{center}
86334134abe5 updated cw
cu
parents:
diff changeset
   342
%\begin{tabular}{r|c|c|c|c|c}
86334134abe5 updated cw
cu
parents:
diff changeset
   343
%      & manual.txt & report.txt & microedit & src/code.c & src/code.h \\\hline
86334134abe5 updated cw
cu
parents:
diff changeset
   344
%ping  & R-         & R-         & RW        & --         & --\\\hline
86334134abe5 updated cw
cu
parents:
diff changeset
   345
%bob   & R-         & R-         & RW        & --         & --\\\hline
86334134abe5 updated cw
cu
parents:
diff changeset
   346
%emma  & --         & --         & --        & --         & --\\
86334134abe5 updated cw
cu
parents:
diff changeset
   347
%\end{tabular}
86334134abe5 updated cw
cu
parents:
diff changeset
   348
%\end{center}
86334134abe5 updated cw
cu
parents:
diff changeset
   349
%
86334134abe5 updated cw
cu
parents:
diff changeset
   350
%\begin{center}
86334134abe5 updated cw
cu
parents:
diff changeset
   351
%\begin{tabular}{r|c|c|c|c|c}
86334134abe5 updated cw
cu
parents:
diff changeset
   352
%      & manual.txt & report.txt & microedit & src/code.c & src/code.h \\\hline
86334134abe5 updated cw
cu
parents:
diff changeset
   353
%ping  & RW         & -W         & RW        & R-         & --\\\hline
86334134abe5 updated cw
cu
parents:
diff changeset
   354
%bob   & R-         & R-         & RW        & --         & --\\\hline
86334134abe5 updated cw
cu
parents:
diff changeset
   355
%emma  & --         & --         & --        & --         & --\\
86334134abe5 updated cw
cu
parents:
diff changeset
   356
%\end{tabular}
86334134abe5 updated cw
cu
parents:
diff changeset
   357
%\end{center}
86334134abe5 updated cw
cu
parents:
diff changeset
   358
86334134abe5 updated cw
cu
parents:
diff changeset
   359
86334134abe5 updated cw
cu
parents:
diff changeset
   360
\end{document}
86334134abe5 updated cw
cu
parents:
diff changeset
   361
86334134abe5 updated cw
cu
parents:
diff changeset
   362
%%% Local Variables: 
86334134abe5 updated cw
cu
parents:
diff changeset
   363
%%% mode: latex
86334134abe5 updated cw
cu
parents:
diff changeset
   364
%%% TeX-master: t
86334134abe5 updated cw
cu
parents:
diff changeset
   365
%%% End: