| author | wenzelm |
| Wed, 25 Jan 2006 00:21:35 +0100 | |
| changeset 18777 | 9d98d5705433 |
| parent 18730 | 843da46f89ac |
| child 19086 | 1b3780be6cc2 |
| permissions | -rw-r--r-- |
| 10966 | 1 |
(* Title: HOL/Unix/Unix.thy |
2 |
ID: $Id$ |
|
3 |
Author: Markus Wenzel, TU Muenchen |
|
4 |
*) |
|
5 |
||
6 |
header {* Unix file-systems \label{sec:unix-file-system} *}
|
|
7 |
||
| 17455 | 8 |
theory Unix |
9 |
imports Nested_Environment List_Prefix |
|
10 |
begin |
|
| 10966 | 11 |
|
12 |
text {*
|
|
13 |
We give a simple mathematical model of the basic structures |
|
14 |
underlying the Unix file-system, together with a few fundamental |
|
15 |
operations that could be imagined to be performed internally by the |
|
16 |
Unix kernel. This forms the basis for the set of Unix system-calls |
|
17 |
to be introduced later (see \secref{sec:unix-trans}), which are the
|
|
18 |
actual interface offered to processes running in user-space. |
|
19 |
||
20 |
\medskip Basically, any Unix file is either a \emph{plain file} or a
|
|
21 |
\emph{directory}, consisting of some \emph{content} plus
|
|
22 |
\emph{attributes}. The content of a plain file is plain text. The
|
|
23 |
content of a directory is a mapping from names to further |
|
24 |
files.\footnote{In fact, this is the only way that names get
|
|
25 |
associated with files. In Unix files do \emph{not} have a name in
|
|
26 |
itself. Even more, any number of names may be associated with the |
|
27 |
very same file due to \emph{hard links} (although this is excluded
|
|
28 |
from our model).} Attributes include information to control various |
|
29 |
ways to access the file (read, write etc.). |
|
30 |
||
31 |
Our model will be quite liberal in omitting excessive detail that is |
|
32 |
easily seen to be ``irrelevant'' for the aspects of Unix |
|
33 |
file-systems to be discussed here. First of all, we ignore |
|
34 |
character and block special files, pipes, sockets, hard links, |
|
35 |
symbolic links, and mount points. |
|
36 |
*} |
|
37 |
||
38 |
||
39 |
subsection {* Names *}
|
|
40 |
||
41 |
text {*
|
|
42 |
User ids and file name components shall be represented by natural |
|
43 |
numbers (without loss of generality). We do not bother about |
|
44 |
encoding of actual names (e.g.\ strings), nor a mapping between user |
|
45 |
names and user ids as would be present in a reality. |
|
46 |
*} |
|
47 |
||
48 |
types |
|
49 |
uid = nat |
|
50 |
name = nat |
|
51 |
path = "name list" |
|
52 |
||
53 |
||
54 |
subsection {* Attributes *}
|
|
55 |
||
56 |
text {*
|
|
57 |
Unix file attributes mainly consist of \emph{owner} information and
|
|
58 |
a number of \emph{permission} bits which control access for
|
|
59 |
``user'', ``group'', and ``others'' (see the Unix man pages @{text
|
|
60 |
"chmod(2)"} and @{text "stat(2)"} for more details).
|
|
61 |
||
62 |
\medskip Our model of file permissions only considers the ``others'' |
|
63 |
part. The ``user'' field may be omitted without loss of overall |
|
64 |
generality, since the owner is usually able to change it anyway by |
|
65 |
performing @{text chmod}.\footnote{The inclined Unix expert may try
|
|
66 |
to figure out some exotic arrangements of a real-world Unix |
|
67 |
file-system such that the owner of a file is unable to apply the |
|
68 |
@{text chmod} system call.} We omit ``group'' permissions as a
|
|
69 |
genuine simplification as we just do not intend to discuss a model |
|
70 |
of multiple groups and group membership, but pretend that everyone |
|
71 |
is member of a single global group.\footnote{A general HOL model of
|
|
72 |
user group structures and related issues is given in |
|
73 |
\cite{Naraschewski:2001}.}
|
|
74 |
*} |
|
75 |
||
76 |
datatype perm = |
|
77 |
Readable |
|
78 |
| Writable |
|
79 |
| Executable -- "(ignored)" |
|
80 |
||
81 |
types perms = "perm set" |
|
82 |
||
83 |
record att = |
|
84 |
owner :: uid |
|
85 |
others :: perms |
|
86 |
||
87 |
text {*
|
|
88 |
For plain files @{term Readable} and @{term Writable} specify read
|
|
89 |
and write access to the actual content, i.e.\ the string of text |
|
90 |
stored here. For directories @{term Readable} determines if the set
|
|
91 |
of entry names may be accessed, and @{term Writable} controls the
|
|
92 |
ability to create or delete any entries (both plain files or |
|
93 |
sub-directories). |
|
94 |
||
95 |
As another simplification, we ignore the @{term Executable}
|
|
96 |
permission altogether. In reality it would indicate executable |
|
97 |
plain files (also known as ``binaries''), or control actual lookup |
|
98 |
of directory entries (recall that mere directory browsing is |
|
99 |
controlled via @{term Readable}). Note that the latter means that
|
|
100 |
in order to perform any file-system operation whatsoever, all |
|
101 |
directories encountered on the path would have to grant @{term
|
|
102 |
Executable}. We ignore this detail and pretend that all directories |
|
103 |
give @{term Executable} permission to anybody.
|
|
104 |
*} |
|
105 |
||
106 |
||
107 |
subsection {* Files *}
|
|
108 |
||
109 |
text {*
|
|
110 |
In order to model the general tree structure of a Unix file-system |
|
111 |
we use the arbitrarily branching datatype @{typ "('a, 'b, 'c) env"}
|
|
112 |
from the standard library of Isabelle/HOL |
|
| 13380 | 113 |
\cite{Bauer-et-al:2002:HOL-Library}. This type provides
|
| 10966 | 114 |
constructors @{term Val} and @{term Env} as follows:
|
115 |
||
116 |
\medskip |
|
117 |
{\def\isastyleminor{\isastyle}
|
|
118 |
\begin{tabular}{l}
|
|
119 |
@{term [source] "Val :: 'a \<Rightarrow> ('a, 'b, 'c) env"} \\
|
|
120 |
@{term [source] "Env :: 'b \<Rightarrow> ('c \<Rightarrow> ('a, 'b, 'c) env option) \<Rightarrow> ('a, 'b, 'c) env"} \\
|
|
121 |
\end{tabular}}
|
|
122 |
\medskip |
|
123 |
||
124 |
Here the parameter @{typ 'a} refers to plain values occurring at
|
|
125 |
leaf positions, parameter @{typ 'b} to information kept with inner
|
|
126 |
branch nodes, and parameter @{typ 'c} to the branching type of the
|
|
127 |
tree structure. For our purpose we use the type instance with @{typ
|
|
128 |
"att \<times> string"} (representing plain files), @{typ att} (for
|
|
129 |
attributes of directory nodes), and @{typ name} (for the index type
|
|
130 |
of directory nodes). |
|
131 |
*} |
|
132 |
||
133 |
types |
|
|
17146
67e9b86ed211
Put quotation marks around some occurrences of "file", since it is now
berghofe
parents:
16670
diff
changeset
|
134 |
"file" = "(att \<times> string, att, name) env" |
| 10966 | 135 |
|
136 |
text {*
|
|
137 |
\medskip The HOL library also provides @{term lookup} and @{term
|
|
138 |
update} operations for general tree structures with the subsequent |
|
139 |
primitive recursive characterizations. |
|
140 |
||
141 |
\medskip |
|
142 |
{\def\isastyleminor{\isastyle}
|
|
143 |
\begin{tabular}{l}
|
|
144 |
@{term [source] "lookup :: ('a, 'b, 'c) env \<Rightarrow> 'c list \<Rightarrow> ('a, 'b, 'c) env option"} \\
|
|
145 |
@{term [source]
|
|
146 |
"update :: 'c list \<Rightarrow> ('a, 'b, 'c) env option \<Rightarrow> ('a, 'b, 'c) env \<Rightarrow> ('a, 'b, 'c) env"} \\
|
|
147 |
\end{tabular}}
|
|
148 |
||
149 |
@{thm [display, indent = 2, eta_contract = false] lookup_eq [no_vars]}
|
|
150 |
@{thm [display, indent = 2, eta_contract = false] update_eq [no_vars]}
|
|
151 |
||
152 |
Several further properties of these operations are proven in |
|
| 13380 | 153 |
\cite{Bauer-et-al:2002:HOL-Library}. These will be routinely used
|
| 10966 | 154 |
later on without further notice. |
155 |
||
|
17146
67e9b86ed211
Put quotation marks around some occurrences of "file", since it is now
berghofe
parents:
16670
diff
changeset
|
156 |
\bigskip Apparently, the elements of type @{typ "file"} contain an
|
| 10966 | 157 |
@{typ att} component in either case. We now define a few auxiliary
|
158 |
operations to manipulate this field uniformly, following the |
|
159 |
conventions for record types in Isabelle/HOL |
|
160 |
\cite{Nipkow-et-al:2000:HOL}.
|
|
161 |
*} |
|
162 |
||
163 |
constdefs |
|
164 |
attributes :: "file \<Rightarrow> att" |
|
165 |
"attributes file \<equiv> |
|
166 |
(case file of |
|
167 |
Val (att, text) \<Rightarrow> att |
|
168 |
| Env att dir \<Rightarrow> att)" |
|
169 |
||
170 |
attributes_update :: "att \<Rightarrow> file \<Rightarrow> file" |
|
171 |
"attributes_update att file \<equiv> |
|
172 |
(case file of |
|
173 |
Val (att', text) \<Rightarrow> Val (att, text) |
|
174 |
| Env att' dir \<Rightarrow> Env att dir)" |
|
175 |
||
176 |
lemma [simp]: "attributes (Val (att, text)) = att" |
|
177 |
by (simp add: attributes_def) |
|
178 |
||
179 |
lemma [simp]: "attributes (Env att dir) = att" |
|
180 |
by (simp add: attributes_def) |
|
181 |
||
182 |
lemma [simp]: "attributes (file \<lparr>attributes := att\<rparr>) = att" |
|
|
17146
67e9b86ed211
Put quotation marks around some occurrences of "file", since it is now
berghofe
parents:
16670
diff
changeset
|
183 |
by (cases "file") (simp_all add: attributes_def attributes_update_def |
| 10966 | 184 |
split_tupled_all) |
185 |
||
186 |
lemma [simp]: "(Val (att, text)) \<lparr>attributes := att'\<rparr> = Val (att', text)" |
|
187 |
by (simp add: attributes_update_def) |
|
188 |
||
189 |
lemma [simp]: "(Env att dir) \<lparr>attributes := att'\<rparr> = Env att' dir" |
|
190 |
by (simp add: attributes_update_def) |
|
191 |
||
192 |
||
193 |
subsection {* Initial file-systems *}
|
|
194 |
||
195 |
text {*
|
|
196 |
Given a set of \emph{known users} a file-system shall be initialized
|
|
197 |
by providing an empty home directory for each user, with read-only |
|
198 |
access for everyone else. (Note that we may directly use the user |
|
199 |
id as home directory name, since both types have been identified.) |
|
200 |
Certainly, the very root directory is owned by the super user (who |
|
201 |
has user id 0). |
|
202 |
*} |
|
203 |
||
204 |
constdefs |
|
205 |
init :: "uid set \<Rightarrow> file" |
|
206 |
"init users \<equiv> |
|
207 |
Env \<lparr>owner = 0, others = {Readable}\<rparr>
|
|
208 |
(\<lambda>u. if u \<in> users then Some (Env \<lparr>owner = u, others = {Readable}\<rparr> empty)
|
|
209 |
else None)" |
|
210 |
||
211 |
||
212 |
subsection {* Accessing file-systems *}
|
|
213 |
||
214 |
text {*
|
|
215 |
The main internal file-system operation is access of a file by a |
|
216 |
user, requesting a certain set of permissions. The resulting @{typ
|
|
217 |
"file option"} indicates if a file had been present at the |
|
218 |
corresponding @{typ path} and if access was granted according to the
|
|
219 |
permissions recorded within the file-system. |
|
220 |
||
221 |
Note that by the rules of Unix file-system security (e.g.\ |
|
222 |
\cite{Tanenbaum:1992}) both the super-user and owner may always
|
|
223 |
access a file unconditionally (in our simplified model). |
|
224 |
*} |
|
225 |
||
226 |
constdefs |
|
227 |
access :: "file \<Rightarrow> path \<Rightarrow> uid \<Rightarrow> perms \<Rightarrow> file option" |
|
228 |
"access root path uid perms \<equiv> |
|
229 |
(case lookup root path of |
|
230 |
None \<Rightarrow> None |
|
231 |
| Some file \<Rightarrow> |
|
232 |
if uid = 0 |
|
233 |
\<or> uid = owner (attributes file) |
|
234 |
\<or> perms \<subseteq> others (attributes file) |
|
235 |
then Some file |
|
236 |
else None)" |
|
237 |
||
238 |
text {*
|
|
239 |
\medskip Successful access to a certain file is the main |
|
240 |
prerequisite for system-calls to be applicable (cf.\ |
|
241 |
\secref{sec:unix-trans}). Any modification of the file-system is
|
|
242 |
then performed using the basic @{term update} operation.
|
|
243 |
||
244 |
\medskip We see that @{term access} is just a wrapper for the basic
|
|
245 |
@{term lookup} function, with additional checking of
|
|
246 |
attributes. Subsequently we establish a few auxiliary facts that |
|
247 |
stem from the primitive @{term lookup} used within @{term access}.
|
|
248 |
*} |
|
249 |
||
250 |
lemma access_empty_lookup: "access root path uid {} = lookup root path"
|
|
251 |
by (simp add: access_def split: option.splits) |
|
252 |
||
253 |
lemma access_some_lookup: |
|
254 |
"access root path uid perms = Some file \<Longrightarrow> |
|
255 |
lookup root path = Some file" |
|
256 |
by (simp add: access_def split: option.splits if_splits) |
|
257 |
||
| 18372 | 258 |
lemma access_update_other: |
259 |
assumes parallel: "path' \<parallel> path" |
|
260 |
shows "access (update path' opt root) path uid perms = access root path uid perms" |
|
| 10966 | 261 |
proof - |
| 18372 | 262 |
from parallel obtain y z xs ys zs where |
| 11072 | 263 |
"y \<noteq> z" and "path' = xs @ y # ys" and "path = xs @ z # zs" |
| 10966 | 264 |
by (blast dest: parallel_decomp) |
| 18372 | 265 |
then have "lookup (update path' opt root) path = lookup root path" |
| 10966 | 266 |
by (blast intro: lookup_update_other) |
| 18372 | 267 |
then show ?thesis by (simp only: access_def) |
| 10966 | 268 |
qed |
269 |
||
270 |
||
271 |
section {* File-system transitions \label{sec:unix-trans} *}
|
|
272 |
||
273 |
subsection {* Unix system calls \label{sec:unix-syscall} *}
|
|
274 |
||
275 |
text {*
|
|
276 |
According to established operating system design (cf.\ |
|
277 |
\cite{Tanenbaum:1992}) user space processes may only initiate system
|
|
278 |
operations by a fixed set of \emph{system-calls}. This enables the
|
|
279 |
kernel to enforce certain security policies in the first |
|
280 |
place.\footnote{Incidently, this is the very same principle employed
|
|
281 |
by any ``LCF-style'' theorem proving system according to Milner's |
|
282 |
principle of ``correctness by construction'', such as Isabelle/HOL |
|
283 |
itself.} |
|
284 |
||
285 |
\medskip In our model of Unix we give a fixed datatype @{text
|
|
286 |
operation} for the syntax of system-calls, together with an |
|
287 |
inductive definition of file-system state transitions of the form |
|
288 |
@{text "root \<midarrow>x\<rightarrow> root'"} for the operational semantics.
|
|
289 |
*} |
|
290 |
||
291 |
datatype operation = |
|
292 |
Read uid string path |
|
293 |
| Write uid string path |
|
294 |
| Chmod uid perms path |
|
295 |
| Creat uid perms path |
|
296 |
| Unlink uid path |
|
297 |
| Mkdir uid perms path |
|
298 |
| Rmdir uid path |
|
299 |
| Readdir uid "name set" path |
|
300 |
||
301 |
text {*
|
|
302 |
The @{typ uid} field of an operation corresponds to the
|
|
303 |
\emph{effective user id} of the underlying process, although our
|
|
304 |
model never mentions processes explicitly. The other parameters are |
|
305 |
provided as arguments by the caller; the @{term path} one is common
|
|
306 |
to all kinds of system-calls. |
|
307 |
*} |
|
308 |
||
309 |
consts |
|
310 |
uid_of :: "operation \<Rightarrow> uid" |
|
311 |
primrec |
|
312 |
"uid_of (Read uid text path) = uid" |
|
313 |
"uid_of (Write uid text path) = uid" |
|
314 |
"uid_of (Chmod uid perms path) = uid" |
|
315 |
"uid_of (Creat uid perms path) = uid" |
|
316 |
"uid_of (Unlink uid path) = uid" |
|
317 |
"uid_of (Mkdir uid path perms) = uid" |
|
318 |
"uid_of (Rmdir uid path) = uid" |
|
319 |
"uid_of (Readdir uid names path) = uid" |
|
320 |
||
321 |
consts |
|
322 |
path_of :: "operation \<Rightarrow> path" |
|
323 |
primrec |
|
324 |
"path_of (Read uid text path) = path" |
|
325 |
"path_of (Write uid text path) = path" |
|
326 |
"path_of (Chmod uid perms path) = path" |
|
327 |
"path_of (Creat uid perms path) = path" |
|
328 |
"path_of (Unlink uid path) = path" |
|
329 |
"path_of (Mkdir uid perms path) = path" |
|
330 |
"path_of (Rmdir uid path) = path" |
|
331 |
"path_of (Readdir uid names path) = path" |
|
332 |
||
333 |
text {*
|
|
334 |
\medskip Note that we have omitted explicit @{text Open} and @{text
|
|
335 |
Close} operations, pretending that @{term Read} and @{term Write}
|
|
336 |
would already take care of this behind the scenes. Thus we have |
|
337 |
basically treated actual sequences of real system-calls @{text
|
|
| 13380 | 338 |
"open"}--@{text read}/@{text write}--@{text close} as atomic.
|
| 10966 | 339 |
|
340 |
In principle, this could make big a difference in a model with |
|
341 |
explicit concurrent processes. On the other hand, even on a real |
|
| 13380 | 342 |
Unix system the exact scheduling of concurrent @{text "open"} and
|
| 10966 | 343 |
@{text close} operations does \emph{not} directly affect the success
|
344 |
of corresponding @{text read} or @{text write}. Unix allows several
|
|
345 |
processes to have files opened at the same time, even for writing! |
|
346 |
Certainly, the result from reading the contents later may be hard to |
|
347 |
predict, but the system-calls involved here will succeed in any |
|
348 |
case. |
|
349 |
||
350 |
\bigskip The operational semantics of system calls is now specified |
|
351 |
via transitions of the file-system configuration. This is expressed |
|
352 |
as an inductive relation (although there is no actual recursion |
|
353 |
involved here). |
|
354 |
*} |
|
355 |
||
356 |
consts |
|
357 |
transition :: "(file \<times> operation \<times> file) set" |
|
358 |
||
359 |
syntax |
|
360 |
"_transition" :: "file \<Rightarrow> operation \<Rightarrow> file \<Rightarrow> bool" |
|
361 |
("_ \<midarrow>_\<rightarrow> _" [90, 1000, 90] 100)
|
|
362 |
translations |
|
363 |
"root \<midarrow>x\<rightarrow> root'" \<rightleftharpoons> "(root, x, root') \<in> transition" |
|
364 |
||
365 |
inductive transition |
|
366 |
intros |
|
367 |
||
368 |
read: |
|
369 |
"access root path uid {Readable} = Some (Val (att, text)) \<Longrightarrow>
|
|
370 |
root \<midarrow>(Read uid text path)\<rightarrow> root" |
|
371 |
write: |
|
372 |
"access root path uid {Writable} = Some (Val (att, text')) \<Longrightarrow>
|
|
373 |
root \<midarrow>(Write uid text path)\<rightarrow> update path (Some (Val (att, text))) root" |
|
374 |
||
375 |
chmod: |
|
376 |
"access root path uid {} = Some file \<Longrightarrow>
|
|
377 |
uid = 0 \<or> uid = owner (attributes file) \<Longrightarrow> |
|
378 |
root \<midarrow>(Chmod uid perms path)\<rightarrow> update path |
|
379 |
(Some (file \<lparr>attributes := attributes file \<lparr>others := perms\<rparr>\<rparr>)) root" |
|
380 |
||
381 |
creat: |
|
382 |
"path = parent_path @ [name] \<Longrightarrow> |
|
383 |
access root parent_path uid {Writable} = Some (Env att parent) \<Longrightarrow>
|
|
384 |
access root path uid {} = None \<Longrightarrow>
|
|
385 |
root \<midarrow>(Creat uid perms path)\<rightarrow> update path |
|
386 |
(Some (Val (\<lparr>owner = uid, others = perms\<rparr>, []))) root" |
|
387 |
unlink: |
|
388 |
"path = parent_path @ [name] \<Longrightarrow> |
|
389 |
access root parent_path uid {Writable} = Some (Env att parent) \<Longrightarrow>
|
|
390 |
access root path uid {} = Some (Val plain) \<Longrightarrow>
|
|
391 |
root \<midarrow>(Unlink uid path)\<rightarrow> update path None root" |
|
392 |
||
393 |
mkdir: |
|
394 |
"path = parent_path @ [name] \<Longrightarrow> |
|
395 |
access root parent_path uid {Writable} = Some (Env att parent) \<Longrightarrow>
|
|
396 |
access root path uid {} = None \<Longrightarrow>
|
|
397 |
root \<midarrow>(Mkdir uid perms path)\<rightarrow> update path |
|
398 |
(Some (Env \<lparr>owner = uid, others = perms\<rparr> empty)) root" |
|
399 |
rmdir: |
|
400 |
"path = parent_path @ [name] \<Longrightarrow> |
|
401 |
access root parent_path uid {Writable} = Some (Env att parent) \<Longrightarrow>
|
|
402 |
access root path uid {} = Some (Env att' empty) \<Longrightarrow>
|
|
403 |
root \<midarrow>(Rmdir uid path)\<rightarrow> update path None root" |
|
404 |
||
405 |
readdir: |
|
406 |
"access root path uid {Readable} = Some (Env att dir) \<Longrightarrow>
|
|
407 |
names = dom dir \<Longrightarrow> |
|
408 |
root \<midarrow>(Readdir uid names path)\<rightarrow> root" |
|
409 |
||
410 |
text {*
|
|
411 |
\medskip Certainly, the above specification is central to the whole |
|
412 |
formal development. Any of the results to be established later on |
|
413 |
are only meaningful to the outside world if this transition system |
|
414 |
provides an adequate model of real Unix systems. This kind of |
|
415 |
``reality-check'' of a formal model is the well-known problem of |
|
416 |
\emph{validation}.
|
|
417 |
||
418 |
If in doubt, one may consider to compare our definition with the |
|
419 |
informal specifications given the corresponding Unix man pages, or |
|
420 |
even peek at an actual implementation such as |
|
421 |
\cite{Torvalds-et-al:Linux}. Another common way to gain confidence
|
|
422 |
into the formal model is to run simple simulations (see |
|
423 |
\secref{sec:unix-examples}), and check the results with that of
|
|
424 |
experiments performed on a real Unix system. |
|
425 |
*} |
|
426 |
||
427 |
||
428 |
subsection {* Basic properties of single transitions \label{sec:unix-single-trans} *}
|
|
429 |
||
430 |
text {*
|
|
431 |
The transition system @{text "root \<midarrow>x\<rightarrow> root'"} defined above
|
|
432 |
determines a unique result @{term root'} from given @{term root} and
|
|
433 |
@{term x} (this is holds rather trivially, since there is even only
|
|
434 |
one clause for each operation). This uniqueness statement will |
|
435 |
simplify our subsequent development to some extent, since we only |
|
436 |
have to reason about a partial function rather than a general |
|
437 |
relation. |
|
438 |
*} |
|
439 |
||
| 18372 | 440 |
theorem transition_uniq: |
441 |
assumes root': "root \<midarrow>x\<rightarrow> root'" |
|
442 |
and root'': "root \<midarrow>x\<rightarrow> root''" |
|
443 |
shows "root' = root''" |
|
444 |
using root'' |
|
445 |
proof cases |
|
446 |
case read |
|
447 |
with root' show ?thesis by cases auto |
|
448 |
next |
|
449 |
case write |
|
450 |
with root' show ?thesis by cases auto |
|
451 |
next |
|
452 |
case chmod |
|
453 |
with root' show ?thesis by cases auto |
|
454 |
next |
|
455 |
case creat |
|
456 |
with root' show ?thesis by cases auto |
|
457 |
next |
|
458 |
case unlink |
|
459 |
with root' show ?thesis by cases auto |
|
460 |
next |
|
461 |
case mkdir |
|
462 |
with root' show ?thesis by cases auto |
|
463 |
next |
|
464 |
case rmdir |
|
465 |
with root' show ?thesis by cases auto |
|
466 |
next |
|
467 |
case readdir |
|
468 |
with root' show ?thesis by cases fastsimp+ |
|
| 10966 | 469 |
qed |
470 |
||
471 |
text {*
|
|
472 |
\medskip Apparently, file-system transitions are \emph{type-safe} in
|
|
473 |
the sense that the result of transforming an actual directory yields |
|
474 |
again a directory. |
|
475 |
*} |
|
476 |
||
477 |
theorem transition_type_safe: |
|
| 18372 | 478 |
assumes tr: "root \<midarrow>x\<rightarrow> root'" |
479 |
and inv: "\<exists>att dir. root = Env att dir" |
|
480 |
shows "\<exists>att dir. root' = Env att dir" |
|
481 |
proof (cases "path_of x") |
|
482 |
case Nil |
|
483 |
with tr inv show ?thesis |
|
484 |
by cases (auto simp add: access_def split: if_splits) |
|
485 |
next |
|
486 |
case Cons |
|
487 |
from tr obtain opt where |
|
488 |
"root' = root \<or> root' = update (path_of x) opt root" |
|
489 |
by cases auto |
|
490 |
with inv Cons show ?thesis |
|
491 |
by (auto simp add: update_eq split: list.splits) |
|
| 10966 | 492 |
qed |
493 |
||
494 |
text {*
|
|
495 |
The previous result may be seen as the most basic invariant on the |
|
496 |
file-system state that is enforced by any proper kernel |
|
497 |
implementation. So user processes --- being bound to the |
|
498 |
system-call interface --- may never mess up a file-system such that |
|
499 |
the root becomes a plain file instead of a directory, which would be |
|
500 |
a strange situation indeed. |
|
501 |
*} |
|
502 |
||
503 |
||
504 |
subsection {* Iterated transitions *}
|
|
505 |
||
506 |
text {*
|
|
507 |
Iterated system transitions via finite sequences of system |
|
508 |
operations are modeled inductively as follows. In a sense, this |
|
509 |
relation describes the cumulative effect of the sequence of |
|
510 |
system-calls issued by a number of running processes over a finite |
|
511 |
amount of time. |
|
512 |
*} |
|
513 |
||
514 |
consts |
|
515 |
transitions :: "(file \<times> operation list \<times> file) set" |
|
516 |
||
517 |
syntax |
|
518 |
"_transitions" :: "file \<Rightarrow> operation list \<Rightarrow> file \<Rightarrow> bool" |
|
519 |
("_ =_\<Rightarrow> _" [90, 1000, 90] 100)
|
|
520 |
translations |
|
521 |
"root =xs\<Rightarrow> root'" \<rightleftharpoons> "(root, xs, root') \<in> transitions" |
|
522 |
||
523 |
inductive transitions |
|
524 |
intros |
|
525 |
nil: "root =[]\<Rightarrow> root" |
|
526 |
cons: "root \<midarrow>x\<rightarrow> root' \<Longrightarrow> root' =xs\<Rightarrow> root'' \<Longrightarrow> root =(x # xs)\<Rightarrow> root''" |
|
527 |
||
528 |
text {*
|
|
529 |
\medskip We establish a few basic facts relating iterated |
|
530 |
transitions with single ones, according to the recursive structure |
|
531 |
of lists. |
|
532 |
*} |
|
533 |
||
534 |
lemma transitions_nil_eq: "root =[]\<Rightarrow> root' = (root = root')" |
|
535 |
proof |
|
536 |
assume "root =[]\<Rightarrow> root'" |
|
| 18372 | 537 |
then show "root = root'" by cases simp_all |
| 10966 | 538 |
next |
539 |
assume "root = root'" |
|
| 18372 | 540 |
then show "root =[]\<Rightarrow> root'" by (simp only: transitions.nil) |
| 10966 | 541 |
qed |
542 |
||
543 |
lemma transitions_cons_eq: |
|
544 |
"root =(x # xs)\<Rightarrow> root'' = (\<exists>root'. root \<midarrow>x\<rightarrow> root' \<and> root' =xs\<Rightarrow> root'')" |
|
545 |
proof |
|
546 |
assume "root =(x # xs)\<Rightarrow> root''" |
|
| 18372 | 547 |
then show "\<exists>root'. root \<midarrow>x\<rightarrow> root' \<and> root' =xs\<Rightarrow> root''" |
| 10966 | 548 |
by cases auto |
549 |
next |
|
550 |
assume "\<exists>root'. root \<midarrow>x\<rightarrow> root' \<and> root' =xs\<Rightarrow> root''" |
|
| 18372 | 551 |
then show "root =(x # xs)\<Rightarrow> root''" |
| 10966 | 552 |
by (blast intro: transitions.cons) |
553 |
qed |
|
554 |
||
555 |
text {*
|
|
556 |
The next two rules show how to ``destruct'' known transition |
|
557 |
sequences. Note that the second one actually relies on the |
|
558 |
uniqueness property of the basic transition system (see |
|
559 |
\secref{sec:unix-single-trans}).
|
|
560 |
*} |
|
561 |
||
562 |
lemma transitions_nilD: "root =[]\<Rightarrow> root' \<Longrightarrow> root' = root" |
|
563 |
by (simp add: transitions_nil_eq) |
|
564 |
||
565 |
lemma transitions_consD: |
|
| 18372 | 566 |
assumes list: "root =(x # xs)\<Rightarrow> root''" |
567 |
and hd: "root \<midarrow>x\<rightarrow> root'" |
|
568 |
shows "root' =xs\<Rightarrow> root''" |
|
| 10966 | 569 |
proof - |
| 18372 | 570 |
from list obtain r' where r': "root \<midarrow>x\<rightarrow> r'" and root'': "r' =xs\<Rightarrow> root''" |
| 10966 | 571 |
by cases simp_all |
| 18372 | 572 |
from r' hd have "r' = root'" by (rule transition_uniq) |
| 10966 | 573 |
with root'' show "root' =xs\<Rightarrow> root''" by simp |
574 |
qed |
|
575 |
||
576 |
text {*
|
|
577 |
\medskip The following fact shows how an invariant @{term Q} of
|
|
578 |
single transitions with property @{term P} may be transferred to
|
|
579 |
iterated transitions. The proof is rather obvious by rule induction |
|
580 |
over the definition of @{term "root =xs\<Rightarrow> root'"}.
|
|
581 |
*} |
|
582 |
||
583 |
lemma transitions_invariant: |
|
| 18372 | 584 |
assumes r: "\<And>r x r'. r \<midarrow>x\<rightarrow> r' \<Longrightarrow> Q r \<Longrightarrow> P x \<Longrightarrow> Q r'" |
585 |
and trans: "root =xs\<Rightarrow> root'" |
|
586 |
shows "Q root \<Longrightarrow> \<forall>x \<in> set xs. P x \<Longrightarrow> Q root'" |
|
587 |
using trans |
|
588 |
proof induct |
|
589 |
case nil |
|
590 |
show ?case by assumption |
|
591 |
next |
|
592 |
case (cons root root' root'' x xs) |
|
593 |
note P = `\<forall>x \<in> set (x # xs). P x` |
|
594 |
then have "P x" by simp |
|
595 |
with `root \<midarrow>x\<rightarrow> root'` and `Q root` have Q': "Q root'" by (rule r) |
|
596 |
from P have "\<forall>x \<in> set xs. P x" by simp |
|
597 |
with Q' show "Q root''" by (rule cons.hyps) |
|
| 10966 | 598 |
qed |
599 |
||
600 |
text {*
|
|
601 |
As an example of applying the previous result, we transfer the basic |
|
602 |
type-safety property (see \secref{sec:unix-single-trans}) from
|
|
603 |
single transitions to iterated ones, which is a rather obvious |
|
604 |
result indeed. |
|
605 |
*} |
|
606 |
||
607 |
theorem transitions_type_safe: |
|
| 16670 | 608 |
assumes "root =xs\<Rightarrow> root'" |
609 |
and "\<exists>att dir. root = Env att dir" |
|
610 |
shows "\<exists>att dir. root' = Env att dir" |
|
611 |
using transition_type_safe and prems |
|
612 |
proof (rule transitions_invariant) |
|
613 |
show "\<forall>x \<in> set xs. True" by blast |
|
| 10966 | 614 |
qed |
615 |
||
616 |
||
617 |
section {* Executable sequences *}
|
|
618 |
||
619 |
text {*
|
|
| 17455 | 620 |
An inductively defined relation such as the one of @{text "root \<midarrow>x\<rightarrow>
|
621 |
root'"} (see \secref{sec:unix-syscall}) has two main aspects. First
|
|
622 |
of all, the resulting system admits a certain set of transition |
|
623 |
rules (introductions) as given in the specification. Furthermore, |
|
624 |
there is an explicit least-fixed-point construction involved, which |
|
625 |
results in induction (and case-analysis) rules to eliminate known |
|
626 |
transitions in an exhaustive manner. |
|
| 10966 | 627 |
|
628 |
\medskip Subsequently, we explore our transition system in an |
|
629 |
experimental style, mainly using the introduction rules with basic |
|
630 |
algebraic properties of the underlying structures. The technique |
|
631 |
closely resembles that of Prolog combined with functional evaluation |
|
632 |
in a very simple manner. |
|
633 |
||
634 |
Just as the ``closed-world assumption'' is left implicit in Prolog, |
|
635 |
we do not refer to induction over the whole transition system here. |
|
636 |
So this is still purely positive reasoning about possible |
|
637 |
executions; exhaustive reasoning will be employed only later on (see |
|
638 |
\secref{sec:unix-bogosity}), when we shall demonstrate that certain
|
|
639 |
behavior is \emph{not} possible.
|
|
640 |
*} |
|
641 |
||
642 |
||
643 |
subsection {* Possible transitions *}
|
|
644 |
||
645 |
text {*
|
|
646 |
Rather obviously, a list of system operations can be executed within |
|
647 |
a certain state if there is a result state reached by an iterated |
|
648 |
transition. |
|
649 |
*} |
|
650 |
||
651 |
constdefs |
|
652 |
can_exec :: "file \<Rightarrow> operation list \<Rightarrow> bool" |
|
653 |
"can_exec root xs \<equiv> \<exists>root'. root =xs\<Rightarrow> root'" |
|
654 |
||
655 |
lemma can_exec_nil: "can_exec root []" |
|
| 18730 | 656 |
unfolding can_exec_def by (blast intro: transitions.intros) |
| 10966 | 657 |
|
658 |
lemma can_exec_cons: |
|
659 |
"root \<midarrow>x\<rightarrow> root' \<Longrightarrow> can_exec root' xs \<Longrightarrow> can_exec root (x # xs)" |
|
| 18730 | 660 |
unfolding can_exec_def by (blast intro: transitions.intros) |
| 10966 | 661 |
|
662 |
text {*
|
|
663 |
\medskip In case that we already know that a certain sequence can be |
|
664 |
executed we may destruct it backwardly into individual transitions. |
|
665 |
*} |
|
666 |
||
| 18372 | 667 |
lemma can_exec_snocD: "can_exec root (xs @ [y]) |
| 10966 | 668 |
\<Longrightarrow> \<exists>root' root''. root =xs\<Rightarrow> root' \<and> root' \<midarrow>y\<rightarrow> root''" |
| 18372 | 669 |
proof (induct xs fixing: root) |
670 |
case Nil |
|
671 |
then show ?case |
|
672 |
by (simp add: can_exec_def transitions_nil_eq transitions_cons_eq) |
|
673 |
next |
|
674 |
case (Cons x xs) |
|
675 |
from `can_exec root ((x # xs) @ [y])` obtain r root'' where |
|
676 |
x: "root \<midarrow>x\<rightarrow> r" and |
|
677 |
xs_y: "r =(xs @ [y])\<Rightarrow> root''" |
|
678 |
by (auto simp add: can_exec_def transitions_nil_eq transitions_cons_eq) |
|
679 |
from xs_y Cons.hyps obtain root' r' where xs: "r =xs\<Rightarrow> root'" and y: "root' \<midarrow>y\<rightarrow> r'" |
|
| 18730 | 680 |
unfolding can_exec_def by blast |
| 18372 | 681 |
from x xs have "root =(x # xs)\<Rightarrow> root'" |
682 |
by (rule transitions.cons) |
|
683 |
with y show ?case by blast |
|
| 10966 | 684 |
qed |
685 |
||
686 |
||
687 |
subsection {* Example executions \label{sec:unix-examples} *}
|
|
688 |
||
689 |
text {*
|
|
690 |
We are now ready to perform a few experiments within our formal |
|
691 |
model of Unix system-calls. The common technique is to alternate |
|
692 |
introduction rules of the transition system (see |
|
693 |
\secref{sec:unix-trans}), and steps to solve any emerging side
|
|
694 |
conditions using algebraic properties of the underlying file-system |
|
695 |
structures (see \secref{sec:unix-file-system}).
|
|
696 |
*} |
|
697 |
||
698 |
lemmas eval = access_def init_def |
|
699 |
||
700 |
theorem "u \<in> users \<Longrightarrow> can_exec (init users) |
|
701 |
[Mkdir u perms [u, name]]" |
|
702 |
apply (rule can_exec_cons) |
|
703 |
-- {* back-chain @{term can_exec} (of @{term [source] Cons}) *}
|
|
704 |
apply (rule mkdir) |
|
705 |
-- {* back-chain @{term Mkdir} *}
|
|
706 |
apply (force simp add: eval)+ |
|
707 |
-- {* solve preconditions of @{term Mkdir} *}
|
|
708 |
apply (simp add: eval) |
|
709 |
-- {* peek at resulting dir (optional) *}
|
|
710 |
txt {* @{subgoals [display]} *}
|
|
711 |
apply (rule can_exec_nil) |
|
712 |
-- {* back-chain @{term can_exec} (of @{term [source] Nil}) *}
|
|
713 |
done |
|
714 |
||
715 |
text {*
|
|
716 |
By inspecting the result shown just before the final step above, we |
|
717 |
may gain confidence that our specification of Unix system-calls |
|
718 |
actually makes sense. Further common errors are usually exhibited |
|
719 |
when preconditions of transition rules fail unexpectedly. |
|
720 |
||
721 |
\medskip Here are a few further experiments, using the same |
|
722 |
techniques as before. |
|
723 |
*} |
|
724 |
||
725 |
theorem "u \<in> users \<Longrightarrow> can_exec (init users) |
|
726 |
[Creat u perms [u, name], |
|
727 |
Unlink u [u, name]]" |
|
728 |
apply (rule can_exec_cons) |
|
729 |
apply (rule creat) |
|
730 |
apply (force simp add: eval)+ |
|
731 |
apply (simp add: eval) |
|
732 |
apply (rule can_exec_cons) |
|
733 |
apply (rule unlink) |
|
734 |
apply (force simp add: eval)+ |
|
735 |
apply (simp add: eval) |
|
736 |
txt {* peek at result: @{subgoals [display]} *}
|
|
737 |
apply (rule can_exec_nil) |
|
738 |
done |
|
739 |
||
| 17455 | 740 |
theorem "u \<in> users \<Longrightarrow> Writable \<in> perms\<^isub>1 \<Longrightarrow> |
741 |
Readable \<in> perms\<^isub>2 \<Longrightarrow> name\<^isub>3 \<noteq> name\<^isub>4 \<Longrightarrow> |
|
| 10966 | 742 |
can_exec (init users) |
| 17455 | 743 |
[Mkdir u perms\<^isub>1 [u, name\<^isub>1], |
744 |
Mkdir u' perms\<^isub>2 [u, name\<^isub>1, name\<^isub>2], |
|
745 |
Creat u' perms\<^isub>3 [u, name\<^isub>1, name\<^isub>2, name\<^isub>3], |
|
746 |
Creat u' perms\<^isub>3 [u, name\<^isub>1, name\<^isub>2, name\<^isub>4], |
|
747 |
Readdir u {name\<^isub>3, name\<^isub>4} [u, name\<^isub>1, name\<^isub>2]]"
|
|
| 10966 | 748 |
apply (rule can_exec_cons, rule transition.intros, |
749 |
(force simp add: eval)+, (simp add: eval)?)+ |
|
750 |
txt {* peek at result: @{subgoals [display]} *}
|
|
751 |
apply (rule can_exec_nil) |
|
752 |
done |
|
753 |
||
| 17455 | 754 |
theorem "u \<in> users \<Longrightarrow> Writable \<in> perms\<^isub>1 \<Longrightarrow> Readable \<in> perms\<^isub>3 \<Longrightarrow> |
| 10966 | 755 |
can_exec (init users) |
| 17455 | 756 |
[Mkdir u perms\<^isub>1 [u, name\<^isub>1], |
757 |
Mkdir u' perms\<^isub>2 [u, name\<^isub>1, name\<^isub>2], |
|
758 |
Creat u' perms\<^isub>3 [u, name\<^isub>1, name\<^isub>2, name\<^isub>3], |
|
759 |
Write u' ''foo'' [u, name\<^isub>1, name\<^isub>2, name\<^isub>3], |
|
760 |
Read u ''foo'' [u, name\<^isub>1, name\<^isub>2, name\<^isub>3]]" |
|
| 10966 | 761 |
apply (rule can_exec_cons, rule transition.intros, |
762 |
(force simp add: eval)+, (simp add: eval)?)+ |
|
763 |
txt {* peek at result: @{subgoals [display]} *}
|
|
764 |
apply (rule can_exec_nil) |
|
765 |
done |
|
766 |
||
767 |
||
768 |
section {* Odd effects --- treated formally \label{sec:unix-bogosity} *}
|
|
769 |
||
770 |
text {*
|
|
771 |
We are now ready to give a completely formal treatment of the |
|
772 |
slightly odd situation discussed in the introduction (see |
|
773 |
\secref{sec:unix-intro}): the file-system can easily reach a state
|
|
774 |
where a user is unable to remove his very own directory, because it |
|
775 |
is still populated by items placed there by another user in an |
|
776 |
uncouth manner. |
|
777 |
*} |
|
778 |
||
779 |
subsection {* The general procedure \label{sec:unix-inv-procedure} *}
|
|
780 |
||
781 |
text {*
|
|
782 |
The following theorem expresses the general procedure we are |
|
783 |
following to achieve the main result. |
|
784 |
*} |
|
785 |
||
786 |
theorem general_procedure: |
|
| 18372 | 787 |
assumes cannot_y: "\<And>r r'. Q r \<Longrightarrow> r \<midarrow>y\<rightarrow> r' \<Longrightarrow> False" |
788 |
and init_inv: "\<And>root. init users =bs\<Rightarrow> root \<Longrightarrow> Q root" |
|
789 |
and preserve_inv: "\<And>r x r'. r \<midarrow>x\<rightarrow> r' \<Longrightarrow> Q r \<Longrightarrow> P x \<Longrightarrow> Q r'" |
|
790 |
and init_result: "init users =bs\<Rightarrow> root" |
|
791 |
shows "\<not> (\<exists>xs. (\<forall>x \<in> set xs. P x) \<and> can_exec root (xs @ [y]))" |
|
| 10966 | 792 |
proof - |
793 |
{
|
|
794 |
fix xs |
|
795 |
assume Ps: "\<forall>x \<in> set xs. P x" |
|
796 |
assume can_exec: "can_exec root (xs @ [y])" |
|
797 |
then obtain root' root'' where |
|
798 |
xs: "root =xs\<Rightarrow> root'" and y: "root' \<midarrow>y\<rightarrow> root''" |
|
799 |
by (blast dest: can_exec_snocD) |
|
800 |
from init_result have "Q root" by (rule init_inv) |
|
801 |
from preserve_inv xs this Ps have "Q root'" |
|
802 |
by (rule transitions_invariant) |
|
803 |
from this y have False by (rule cannot_y) |
|
804 |
} |
|
| 18372 | 805 |
then show ?thesis by blast |
| 10966 | 806 |
qed |
807 |
||
808 |
text {*
|
|
809 |
Here @{prop "P x"} refers to the restriction on file-system
|
|
810 |
operations that are admitted after having reached the critical |
|
811 |
configuration; according to the problem specification this will |
|
| 17455 | 812 |
become @{prop "uid_of x = user\<^isub>1"} later on. Furthermore,
|
813 |
@{term y} refers to the operations we claim to be impossible to
|
|
814 |
perform afterwards, we will take @{term Rmdir} later. Moreover
|
|
815 |
@{term Q} is a suitable (auxiliary) invariant over the file-system;
|
|
816 |
choosing @{term Q} adequately is very important to make the proof
|
|
817 |
work (see \secref{sec:unix-inv-lemmas}).
|
|
| 10966 | 818 |
*} |
819 |
||
820 |
||
| 12079 | 821 |
subsection {* The particular situation *}
|
| 10966 | 822 |
|
823 |
text {*
|
|
824 |
We introduce a few global declarations and axioms to describe our |
|
| 12079 | 825 |
particular situation considered here. Thus we avoid excessive use |
826 |
of local parameters in the subsequent development. |
|
| 10966 | 827 |
*} |
828 |
||
| 12079 | 829 |
locale situation = |
830 |
fixes users :: "uid set" |
|
| 17455 | 831 |
and user\<^isub>1 :: uid |
832 |
and user\<^isub>2 :: uid |
|
833 |
and name\<^isub>1 :: name |
|
834 |
and name\<^isub>2 :: name |
|
835 |
and name\<^isub>3 :: name |
|
836 |
and perms\<^isub>1 :: perms |
|
837 |
and perms\<^isub>2 :: perms |
|
838 |
assumes user\<^isub>1_known: "user\<^isub>1 \<in> users" |
|
839 |
and user\<^isub>1_not_root: "user\<^isub>1 \<noteq> 0" |
|
840 |
and users_neq: "user\<^isub>1 \<noteq> user\<^isub>2" |
|
841 |
and perms\<^isub>1_writable: "Writable \<in> perms\<^isub>1" |
|
842 |
and perms\<^isub>2_not_writable: "Writable \<notin> perms\<^isub>2" |
|
843 |
notes facts = user\<^isub>1_known user\<^isub>1_not_root users_neq |
|
844 |
perms\<^isub>1_writable perms\<^isub>2_not_writable |
|
| 10966 | 845 |
|
| 12079 | 846 |
fixes bogus :: "operation list" |
847 |
and bogus_path :: path |
|
848 |
defines "bogus \<equiv> |
|
| 17455 | 849 |
[Mkdir user\<^isub>1 perms\<^isub>1 [user\<^isub>1, name\<^isub>1], |
850 |
Mkdir user\<^isub>2 perms\<^isub>2 [user\<^isub>1, name\<^isub>1, name\<^isub>2], |
|
851 |
Creat user\<^isub>2 perms\<^isub>2 [user\<^isub>1, name\<^isub>1, name\<^isub>2, name\<^isub>3]]" |
|
852 |
and "bogus_path \<equiv> [user\<^isub>1, name\<^isub>1, name\<^isub>2]" |
|
| 10966 | 853 |
|
854 |
text {*
|
|
| 12079 | 855 |
The @{term bogus} operations are the ones that lead into the uncouth
|
856 |
situation; @{term bogus_path} is the key position within the
|
|
857 |
file-system where things go awry. |
|
| 10966 | 858 |
*} |
859 |
||
860 |
||
861 |
subsection {* Invariance lemmas \label{sec:unix-inv-lemmas} *}
|
|
862 |
||
863 |
text {*
|
|
864 |
The following invariant over the root file-system describes the |
|
865 |
bogus situation in an abstract manner: located at a certain @{term
|
|
866 |
path} within the file-system is a non-empty directory that is |
|
| 17455 | 867 |
neither owned and nor writable by @{term user\<^isub>1}.
|
| 10966 | 868 |
*} |
869 |
||
| 12079 | 870 |
locale invariant = situation + |
871 |
fixes invariant :: "file \<Rightarrow> path \<Rightarrow> bool" |
|
872 |
defines "invariant root path \<equiv> |
|
| 10966 | 873 |
(\<exists>att dir. |
| 17455 | 874 |
access root path user\<^isub>1 {} = Some (Env att dir) \<and> dir \<noteq> empty \<and>
|
875 |
user\<^isub>1 \<noteq> owner att \<and> |
|
876 |
access root path user\<^isub>1 {Writable} = None)"
|
|
| 10966 | 877 |
|
878 |
text {*
|
|
879 |
Following the general procedure (see |
|
| 17455 | 880 |
\secref{sec:unix-inv-procedure}) we will now establish the three key
|
881 |
lemmas required to yield the final result. |
|
| 10966 | 882 |
|
883 |
\begin{enumerate}
|
|
884 |
||
885 |
\item The invariant is sufficiently strong to entail the |
|
| 17455 | 886 |
pathological case that @{term user\<^isub>1} is unable to remove the
|
887 |
(owned) directory at @{term "[user\<^isub>1, name\<^isub>1]"}.
|
|
| 10966 | 888 |
|
889 |
\item The invariant does hold after having executed the @{term
|
|
890 |
bogus} list of operations (starting with an initial file-system |
|
891 |
configuration). |
|
892 |
||
893 |
\item The invariant is preserved by any file-system operation |
|
| 17455 | 894 |
performed by @{term user\<^isub>1} alone, without any help by other
|
895 |
users. |
|
| 10966 | 896 |
|
897 |
\end{enumerate}
|
|
898 |
||
899 |
As the invariant appears both as assumptions and conclusions in the |
|
900 |
course of proof, its formulation is rather critical for the whole |
|
901 |
development to work out properly. In particular, the third step is |
|
902 |
very sensitive to the invariant being either too strong or too weak. |
|
903 |
Moreover the invariant has to be sufficiently abstract, lest the |
|
904 |
proof become cluttered by confusing detail. |
|
905 |
||
906 |
\medskip The first two lemmas are technically straight forward --- |
|
907 |
we just have to inspect rather special cases. |
|
908 |
*} |
|
909 |
||
| 18372 | 910 |
lemma (in invariant) cannot_rmdir: |
911 |
assumes inv: "invariant root bogus_path" |
|
912 |
and rmdir: "root \<midarrow>(Rmdir user\<^isub>1 [user\<^isub>1, name\<^isub>1])\<rightarrow> root'" |
|
913 |
shows False |
|
| 10966 | 914 |
proof - |
| 18372 | 915 |
from inv obtain "file" where "access root bogus_path user\<^isub>1 {} = Some file"
|
| 18730 | 916 |
unfolding invariant_def by blast |
| 10966 | 917 |
moreover |
| 18372 | 918 |
from rmdir obtain att where |
| 17455 | 919 |
"access root [user\<^isub>1, name\<^isub>1] user\<^isub>1 {} = Some (Env att empty)"
|
| 10966 | 920 |
by cases auto |
| 18372 | 921 |
then have "access root ([user\<^isub>1, name\<^isub>1] @ [name\<^isub>2]) user\<^isub>1 {} = empty name\<^isub>2"
|
| 10966 | 922 |
by (simp only: access_empty_lookup lookup_append_some) simp |
923 |
ultimately show False by (simp add: bogus_path_def) |
|
924 |
qed |
|
925 |
||
| 12079 | 926 |
lemma (in invariant) |
| 18372 | 927 |
assumes init: "init users =bogus\<Rightarrow> root" |
928 |
notes eval = facts access_def init_def |
|
929 |
shows init_invariant: "invariant root bogus_path" |
|
930 |
using init |
|
931 |
apply (unfold bogus_def bogus_path_def) |
|
932 |
apply (drule transitions_consD, rule transition.intros, |
|
| 10966 | 933 |
(force simp add: eval)+, (simp add: eval)?)+ |
| 18372 | 934 |
-- "evaluate all operations" |
935 |
apply (drule transitions_nilD) |
|
936 |
-- "reach final result" |
|
937 |
apply (simp add: invariant_def eval) |
|
938 |
-- "check the invariant" |
|
939 |
done |
|
| 10966 | 940 |
|
941 |
text {*
|
|
942 |
\medskip At last we are left with the main effort to show that the |
|
943 |
``bogosity'' invariant is preserved by any file-system operation |
|
| 17455 | 944 |
performed by @{term user\<^isub>1} alone. Note that this holds for
|
945 |
any @{term path} given, the particular @{term bogus_path} is not
|
|
| 10966 | 946 |
required here. |
| 11004 | 947 |
*} |
| 10966 | 948 |
|
| 18372 | 949 |
lemma (in invariant) preserve_invariant: |
950 |
assumes tr: "root \<midarrow>x\<rightarrow> root'" |
|
951 |
and inv: "invariant root path" |
|
952 |
and uid: "uid_of x = user\<^isub>1" |
|
953 |
shows "invariant root' path" |
|
| 10966 | 954 |
proof - |
955 |
from inv obtain att dir where |
|
| 17455 | 956 |
inv1: "access root path user\<^isub>1 {} = Some (Env att dir)" and
|
| 10966 | 957 |
inv2: "dir \<noteq> empty" and |
| 17455 | 958 |
inv3: "user\<^isub>1 \<noteq> owner att" and |
959 |
inv4: "access root path user\<^isub>1 {Writable} = None"
|
|
| 10966 | 960 |
by (auto simp add: invariant_def) |
961 |
||
962 |
from inv1 have lookup: "lookup root path = Some (Env att dir)" |
|
963 |
by (simp only: access_empty_lookup) |
|
964 |
||
| 17455 | 965 |
from inv1 inv3 inv4 and user\<^isub>1_not_root |
| 10966 | 966 |
have not_writable: "Writable \<notin> others att" |
967 |
by (auto simp add: access_def split: option.splits if_splits) |
|
968 |
||
969 |
show ?thesis |
|
970 |
proof cases |
|
971 |
assume "root' = root" |
|
972 |
with inv show "invariant root' path" by (simp only:) |
|
973 |
next |
|
974 |
assume changed: "root' \<noteq> root" |
|
975 |
with tr obtain opt where root': "root' = update (path_of x) opt root" |
|
976 |
by cases auto |
|
977 |
show ?thesis |
|
978 |
proof (rule prefix_cases) |
|
979 |
assume "path_of x \<parallel> path" |
|
980 |
with inv root' |
|
| 17455 | 981 |
have "\<And>perms. access root' path user\<^isub>1 perms = access root path user\<^isub>1 perms" |
| 10966 | 982 |
by (simp only: access_update_other) |
983 |
with inv show "invariant root' path" |
|
984 |
by (simp only: invariant_def) |
|
985 |
next |
|
986 |
assume "path_of x \<le> path" |
|
987 |
then obtain ys where path: "path = path_of x @ ys" .. |
|
988 |
||
989 |
show ?thesis |
|
990 |
proof (cases ys) |
|
991 |
assume "ys = []" |
|
| 17455 | 992 |
with tr uid inv2 inv3 lookup changed path and user\<^isub>1_not_root |
| 10966 | 993 |
have False |
994 |
by cases (auto simp add: access_empty_lookup dest: access_some_lookup) |
|
| 18372 | 995 |
then show ?thesis .. |
| 10966 | 996 |
next |
997 |
fix z zs assume ys: "ys = z # zs" |
|
998 |
have "lookup root' path = lookup root path" |
|
999 |
proof - |
|
1000 |
from inv2 lookup path ys |
|
1001 |
have look: "lookup root (path_of x @ z # zs) = Some (Env att dir)" |
|
1002 |
by (simp only:) |
|
1003 |
then obtain att' dir' file' where |
|
1004 |
look': "lookup root (path_of x) = Some (Env att' dir')" and |
|
1005 |
dir': "dir' z = Some file'" and |
|
1006 |
file': "lookup file' zs = Some (Env att dir)" |
|
1007 |
by (blast dest: lookup_some_upper) |
|
1008 |
||
1009 |
from tr uid changed look' dir' obtain att'' where |
|
1010 |
look'': "lookup root' (path_of x) = Some (Env att'' dir')" |
|
1011 |
by cases (auto simp add: access_empty_lookup lookup_update_some |
|
1012 |
dest: access_some_lookup) |
|
1013 |
with dir' file' have "lookup root' (path_of x @ z # zs) = |
|
1014 |
Some (Env att dir)" |
|
1015 |
by (simp add: lookup_append_some) |
|
1016 |
with look path ys show ?thesis |
|
1017 |
by simp |
|
1018 |
qed |
|
1019 |
with inv show "invariant root' path" |
|
1020 |
by (simp only: invariant_def access_def) |
|
1021 |
qed |
|
1022 |
next |
|
1023 |
assume "path < path_of x" |
|
1024 |
then obtain y ys where path: "path_of x = path @ y # ys" .. |
|
1025 |
||
1026 |
obtain dir' where |
|
1027 |
lookup': "lookup root' path = Some (Env att dir')" and |
|
1028 |
inv2': "dir' \<noteq> empty" |
|
1029 |
proof (cases ys) |
|
1030 |
assume "ys = []" |
|
1031 |
with path have parent: "path_of x = path @ [y]" by simp |
|
|
17146
67e9b86ed211
Put quotation marks around some occurrences of "file", since it is now
berghofe
parents:
16670
diff
changeset
|
1032 |
with tr uid inv4 changed obtain "file" where |
| 10966 | 1033 |
"root' = update (path_of x) (Some file) root" |
1034 |
by cases auto |
|
| 10979 | 1035 |
with lookup parent have "lookup root' path = Some (Env att (dir(y\<mapsto>file)))" |
| 10966 | 1036 |
by (simp only: update_append_some update_cons_nil_env) |
1037 |
moreover have "dir(y\<mapsto>file) \<noteq> empty" by simp |
|
1038 |
ultimately show ?thesis .. |
|
1039 |
next |
|
1040 |
fix z zs assume ys: "ys = z # zs" |
|
1041 |
with lookup root' path |
|
1042 |
have "lookup root' path = Some (update (y # ys) opt (Env att dir))" |
|
1043 |
by (simp only: update_append_some) |
|
1044 |
also obtain file' where |
|
1045 |
"update (y # ys) opt (Env att dir) = Env att (dir(y\<mapsto>file'))" |
|
1046 |
proof - |
|
1047 |
have "dir y \<noteq> None" |
|
1048 |
proof - |
|
1049 |
have "dir y = lookup (Env att dir) [y]" |
|
1050 |
by (simp split: option.splits) |
|
1051 |
also from lookup have "\<dots> = lookup root (path @ [y])" |
|
1052 |
by (simp only: lookup_append_some) |
|
1053 |
also have "\<dots> \<noteq> None" |
|
1054 |
proof - |
|
1055 |
from ys obtain us u where rev_ys: "ys = us @ [u]" |
|
| 13601 | 1056 |
by (cases ys rule: rev_cases) fastsimp+ |
| 10966 | 1057 |
with tr path |
1058 |
have "lookup root ((path @ [y]) @ (us @ [u])) \<noteq> None \<or> |
|
1059 |
lookup root ((path @ [y]) @ us) \<noteq> None" |
|
1060 |
by cases (auto dest: access_some_lookup) |
|
| 18447 | 1061 |
then show ?thesis |
| 18576 | 1062 |
by (simp, blast dest!: lookup_some_append) |
| 10966 | 1063 |
qed |
1064 |
finally show ?thesis . |
|
1065 |
qed |
|
1066 |
with ys show ?thesis |
|
1067 |
by (insert that, auto simp add: update_cons_cons_env) |
|
1068 |
qed |
|
1069 |
also have "dir(y\<mapsto>file') \<noteq> empty" by simp |
|
1070 |
ultimately show ?thesis .. |
|
1071 |
qed |
|
1072 |
||
| 17455 | 1073 |
from lookup' have inv1': "access root' path user\<^isub>1 {} = Some (Env att dir')"
|
| 10966 | 1074 |
by (simp only: access_empty_lookup) |
1075 |
||
| 17455 | 1076 |
from inv3 lookup' and not_writable user\<^isub>1_not_root |
1077 |
have "access root' path user\<^isub>1 {Writable} = None"
|
|
| 10966 | 1078 |
by (simp add: access_def) |
| 18730 | 1079 |
with inv1' inv2' inv3 show ?thesis unfolding invariant_def by blast |
| 10966 | 1080 |
qed |
1081 |
qed |
|
1082 |
qed |
|
1083 |
||
1084 |
||
1085 |
subsection {* Putting it all together \label{sec:unix-main-result} *}
|
|
1086 |
||
1087 |
text {*
|
|
1088 |
The main result is now imminent, just by composing the three |
|
1089 |
invariance lemmas (see \secref{sec:unix-inv-lemmas}) according the the
|
|
1090 |
overall procedure (see \secref{sec:unix-inv-procedure}).
|
|
1091 |
*} |
|
1092 |
||
| 13380 | 1093 |
corollary result: |
1094 |
includes invariant |
|
1095 |
assumes bogus: "init users =bogus\<Rightarrow> root" |
|
| 17455 | 1096 |
shows "\<not> (\<exists>xs. (\<forall>x \<in> set xs. uid_of x = user\<^isub>1) \<and> |
1097 |
can_exec root (xs @ [Rmdir user\<^isub>1 [user\<^isub>1, name\<^isub>1]]))" |
|
| 10966 | 1098 |
proof - |
| 13380 | 1099 |
from cannot_rmdir init_invariant preserve_invariant |
1100 |
and bogus show ?thesis by (rule general_procedure) |
|
| 10966 | 1101 |
qed |
1102 |
||
| 13380 | 1103 |
text {*
|
1104 |
So this is our final result: |
|
1105 |
||
| 17455 | 1106 |
@{thm [display, show_question_marks = false] result [OF
|
1107 |
invariant.intro, OF situation.intro]} |
|
| 13380 | 1108 |
*} |
1109 |
||
| 10966 | 1110 |
end |