author | paulson |
Thu, 01 Dec 2005 15:45:54 +0100 | |
changeset 18315 | e52f867ab851 |
parent 17455 | 151e76f0e3c7 |
child 18372 | 2bffdf62fe7f |
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 |
||
258 |
lemma access_update_other: "path' \<parallel> path \<Longrightarrow> |
|
259 |
access (update path' opt root) path uid perms = access root path uid perms" |
|
260 |
proof - |
|
261 |
assume "path' \<parallel> path" |
|
262 |
then 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) |
265 |
hence "lookup (update path' opt root) path = lookup root path" |
|
266 |
by (blast intro: lookup_update_other) |
|
267 |
thus ?thesis by (simp only: access_def) |
|
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 |
||
440 |
theorem transition_uniq: "root \<midarrow>x\<rightarrow> root' \<Longrightarrow> root \<midarrow>x\<rightarrow> root'' \<Longrightarrow> root' = root''" |
|
441 |
proof - |
|
442 |
assume root: "root \<midarrow>x\<rightarrow> root'" |
|
443 |
assume "root \<midarrow>x\<rightarrow> root''" |
|
444 |
thus "root' = 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 |
|
17455 | 468 |
with root show ?thesis by cases fastsimp+ |
10966 | 469 |
qed |
470 |
qed |
|
471 |
||
472 |
text {* |
|
473 |
\medskip Apparently, file-system transitions are \emph{type-safe} in |
|
474 |
the sense that the result of transforming an actual directory yields |
|
475 |
again a directory. |
|
476 |
*} |
|
477 |
||
478 |
theorem transition_type_safe: |
|
479 |
"root \<midarrow>x\<rightarrow> root' \<Longrightarrow> \<exists>att dir. root = Env att dir |
|
480 |
\<Longrightarrow> \<exists>att dir. root' = Env att dir" |
|
481 |
proof - |
|
482 |
assume tr: "root \<midarrow>x\<rightarrow> root'" |
|
483 |
assume inv: "\<exists>att dir. root = Env att dir" |
|
484 |
show ?thesis |
|
485 |
proof (cases "path_of x") |
|
486 |
case Nil |
|
487 |
with tr inv show ?thesis |
|
488 |
by cases (auto simp add: access_def split: if_splits) |
|
489 |
next |
|
490 |
case Cons |
|
491 |
from tr obtain opt where |
|
492 |
"root' = root \<or> root' = update (path_of x) opt root" |
|
493 |
by cases auto |
|
494 |
with inv Cons show ?thesis |
|
495 |
by (auto simp add: update_eq split: list.splits) |
|
496 |
qed |
|
497 |
qed |
|
498 |
||
499 |
text {* |
|
500 |
The previous result may be seen as the most basic invariant on the |
|
501 |
file-system state that is enforced by any proper kernel |
|
502 |
implementation. So user processes --- being bound to the |
|
503 |
system-call interface --- may never mess up a file-system such that |
|
504 |
the root becomes a plain file instead of a directory, which would be |
|
505 |
a strange situation indeed. |
|
506 |
*} |
|
507 |
||
508 |
||
509 |
subsection {* Iterated transitions *} |
|
510 |
||
511 |
text {* |
|
512 |
Iterated system transitions via finite sequences of system |
|
513 |
operations are modeled inductively as follows. In a sense, this |
|
514 |
relation describes the cumulative effect of the sequence of |
|
515 |
system-calls issued by a number of running processes over a finite |
|
516 |
amount of time. |
|
517 |
*} |
|
518 |
||
519 |
consts |
|
520 |
transitions :: "(file \<times> operation list \<times> file) set" |
|
521 |
||
522 |
syntax |
|
523 |
"_transitions" :: "file \<Rightarrow> operation list \<Rightarrow> file \<Rightarrow> bool" |
|
524 |
("_ =_\<Rightarrow> _" [90, 1000, 90] 100) |
|
525 |
translations |
|
526 |
"root =xs\<Rightarrow> root'" \<rightleftharpoons> "(root, xs, root') \<in> transitions" |
|
527 |
||
528 |
inductive transitions |
|
529 |
intros |
|
530 |
nil: "root =[]\<Rightarrow> root" |
|
531 |
cons: "root \<midarrow>x\<rightarrow> root' \<Longrightarrow> root' =xs\<Rightarrow> root'' \<Longrightarrow> root =(x # xs)\<Rightarrow> root''" |
|
532 |
||
533 |
text {* |
|
534 |
\medskip We establish a few basic facts relating iterated |
|
535 |
transitions with single ones, according to the recursive structure |
|
536 |
of lists. |
|
537 |
*} |
|
538 |
||
539 |
lemma transitions_nil_eq: "root =[]\<Rightarrow> root' = (root = root')" |
|
540 |
proof |
|
541 |
assume "root =[]\<Rightarrow> root'" |
|
542 |
thus "root = root'" by cases simp_all |
|
543 |
next |
|
544 |
assume "root = root'" |
|
545 |
thus "root =[]\<Rightarrow> root'" by (simp only: transitions.nil) |
|
546 |
qed |
|
547 |
||
548 |
lemma transitions_cons_eq: |
|
549 |
"root =(x # xs)\<Rightarrow> root'' = (\<exists>root'. root \<midarrow>x\<rightarrow> root' \<and> root' =xs\<Rightarrow> root'')" |
|
550 |
proof |
|
551 |
assume "root =(x # xs)\<Rightarrow> root''" |
|
552 |
thus "\<exists>root'. root \<midarrow>x\<rightarrow> root' \<and> root' =xs\<Rightarrow> root''" |
|
553 |
by cases auto |
|
554 |
next |
|
555 |
assume "\<exists>root'. root \<midarrow>x\<rightarrow> root' \<and> root' =xs\<Rightarrow> root''" |
|
556 |
thus "root =(x # xs)\<Rightarrow> root''" |
|
557 |
by (blast intro: transitions.cons) |
|
558 |
qed |
|
559 |
||
560 |
text {* |
|
561 |
The next two rules show how to ``destruct'' known transition |
|
562 |
sequences. Note that the second one actually relies on the |
|
563 |
uniqueness property of the basic transition system (see |
|
564 |
\secref{sec:unix-single-trans}). |
|
565 |
*} |
|
566 |
||
567 |
lemma transitions_nilD: "root =[]\<Rightarrow> root' \<Longrightarrow> root' = root" |
|
568 |
by (simp add: transitions_nil_eq) |
|
569 |
||
570 |
lemma transitions_consD: |
|
571 |
"root =(x # xs)\<Rightarrow> root'' \<Longrightarrow> root \<midarrow>x\<rightarrow> root' \<Longrightarrow> root' =xs\<Rightarrow> root''" |
|
572 |
proof - |
|
573 |
assume "root =(x # xs)\<Rightarrow> root''" |
|
574 |
then obtain r' where r': "root \<midarrow>x\<rightarrow> r'" and root'': "r' =xs\<Rightarrow> root''" |
|
575 |
by cases simp_all |
|
576 |
assume "root \<midarrow>x\<rightarrow> root'" |
|
577 |
with r' have "r' = root'" by (rule transition_uniq) |
|
578 |
with root'' show "root' =xs\<Rightarrow> root''" by simp |
|
579 |
qed |
|
580 |
||
581 |
text {* |
|
582 |
\medskip The following fact shows how an invariant @{term Q} of |
|
583 |
single transitions with property @{term P} may be transferred to |
|
584 |
iterated transitions. The proof is rather obvious by rule induction |
|
585 |
over the definition of @{term "root =xs\<Rightarrow> root'"}. |
|
586 |
*} |
|
587 |
||
588 |
lemma transitions_invariant: |
|
589 |
"(\<And>r x r'. r \<midarrow>x\<rightarrow> r' \<Longrightarrow> Q r \<Longrightarrow> P x \<Longrightarrow> Q r') \<Longrightarrow> |
|
590 |
root =xs\<Rightarrow> root' \<Longrightarrow> Q root \<Longrightarrow> \<forall>x \<in> set xs. P x \<Longrightarrow> Q root'" |
|
591 |
proof - |
|
592 |
assume r: "\<And>r x r'. r \<midarrow>x\<rightarrow> r' \<Longrightarrow> Q r \<Longrightarrow> P x \<Longrightarrow> Q r'" |
|
593 |
assume "root =xs\<Rightarrow> root'" |
|
594 |
thus "Q root \<Longrightarrow> (\<forall>x \<in> set xs. P x) \<Longrightarrow> Q root'" (is "PROP ?P root xs root'") |
|
11809 | 595 |
proof (induct root xs root') |
10966 | 596 |
fix root assume "Q root" |
597 |
thus "Q root" . |
|
598 |
next |
|
599 |
fix root root' root'' and x xs |
|
600 |
assume root': "root \<midarrow>x\<rightarrow> root'" |
|
601 |
assume hyp: "PROP ?P root' xs root''" |
|
602 |
assume Q: "Q root" |
|
603 |
assume P: "\<forall>x \<in> set (x # xs). P x" |
|
604 |
hence "P x" by simp |
|
605 |
with root' Q have Q': "Q root'" by (rule r) |
|
606 |
from P have "\<forall>x \<in> set xs. P x" by simp |
|
607 |
with Q' show "Q root''" by (rule hyp) |
|
608 |
qed |
|
609 |
qed |
|
610 |
||
611 |
text {* |
|
612 |
As an example of applying the previous result, we transfer the basic |
|
613 |
type-safety property (see \secref{sec:unix-single-trans}) from |
|
614 |
single transitions to iterated ones, which is a rather obvious |
|
615 |
result indeed. |
|
616 |
*} |
|
617 |
||
618 |
theorem transitions_type_safe: |
|
16670 | 619 |
assumes "root =xs\<Rightarrow> root'" |
620 |
and "\<exists>att dir. root = Env att dir" |
|
621 |
shows "\<exists>att dir. root' = Env att dir" |
|
622 |
using transition_type_safe and prems |
|
623 |
proof (rule transitions_invariant) |
|
624 |
show "\<forall>x \<in> set xs. True" by blast |
|
10966 | 625 |
qed |
626 |
||
627 |
||
628 |
section {* Executable sequences *} |
|
629 |
||
630 |
text {* |
|
17455 | 631 |
An inductively defined relation such as the one of @{text "root \<midarrow>x\<rightarrow> |
632 |
root'"} (see \secref{sec:unix-syscall}) has two main aspects. First |
|
633 |
of all, the resulting system admits a certain set of transition |
|
634 |
rules (introductions) as given in the specification. Furthermore, |
|
635 |
there is an explicit least-fixed-point construction involved, which |
|
636 |
results in induction (and case-analysis) rules to eliminate known |
|
637 |
transitions in an exhaustive manner. |
|
10966 | 638 |
|
639 |
\medskip Subsequently, we explore our transition system in an |
|
640 |
experimental style, mainly using the introduction rules with basic |
|
641 |
algebraic properties of the underlying structures. The technique |
|
642 |
closely resembles that of Prolog combined with functional evaluation |
|
643 |
in a very simple manner. |
|
644 |
||
645 |
Just as the ``closed-world assumption'' is left implicit in Prolog, |
|
646 |
we do not refer to induction over the whole transition system here. |
|
647 |
So this is still purely positive reasoning about possible |
|
648 |
executions; exhaustive reasoning will be employed only later on (see |
|
649 |
\secref{sec:unix-bogosity}), when we shall demonstrate that certain |
|
650 |
behavior is \emph{not} possible. |
|
651 |
*} |
|
652 |
||
653 |
||
654 |
subsection {* Possible transitions *} |
|
655 |
||
656 |
text {* |
|
657 |
Rather obviously, a list of system operations can be executed within |
|
658 |
a certain state if there is a result state reached by an iterated |
|
659 |
transition. |
|
660 |
*} |
|
661 |
||
662 |
constdefs |
|
663 |
can_exec :: "file \<Rightarrow> operation list \<Rightarrow> bool" |
|
664 |
"can_exec root xs \<equiv> \<exists>root'. root =xs\<Rightarrow> root'" |
|
665 |
||
666 |
lemma can_exec_nil: "can_exec root []" |
|
667 |
by (unfold can_exec_def) (blast intro: transitions.intros) |
|
668 |
||
669 |
lemma can_exec_cons: |
|
670 |
"root \<midarrow>x\<rightarrow> root' \<Longrightarrow> can_exec root' xs \<Longrightarrow> can_exec root (x # xs)" |
|
671 |
by (unfold can_exec_def) (blast intro: transitions.intros) |
|
672 |
||
673 |
text {* |
|
674 |
\medskip In case that we already know that a certain sequence can be |
|
675 |
executed we may destruct it backwardly into individual transitions. |
|
676 |
*} |
|
677 |
||
678 |
lemma can_exec_snocD: "\<And>root. can_exec root (xs @ [y]) |
|
679 |
\<Longrightarrow> \<exists>root' root''. root =xs\<Rightarrow> root' \<and> root' \<midarrow>y\<rightarrow> root''" |
|
680 |
(is "PROP ?P xs" is "\<And>root. ?A root xs \<Longrightarrow> ?C root xs") |
|
681 |
proof (induct xs) |
|
682 |
fix root |
|
683 |
{ |
|
684 |
assume "?A root []" |
|
685 |
thus "?C root []" |
|
686 |
by (simp add: can_exec_def transitions_nil_eq transitions_cons_eq) |
|
687 |
next |
|
688 |
fix x xs |
|
689 |
assume hyp: "PROP ?P xs" |
|
690 |
assume asm: "?A root (x # xs)" |
|
691 |
show "?C root (x # xs)" |
|
692 |
proof - |
|
693 |
from asm obtain r root'' where x: "root \<midarrow>x\<rightarrow> r" and |
|
694 |
xs_y: "r =(xs @ [y])\<Rightarrow> root''" |
|
695 |
by (auto simp add: can_exec_def transitions_nil_eq transitions_cons_eq) |
|
696 |
from xs_y hyp obtain root' r' where xs: "r =xs\<Rightarrow> root'" and y: "root' \<midarrow>y\<rightarrow> r'" |
|
11758 | 697 |
by (unfold can_exec_def) blast |
10966 | 698 |
from x xs have "root =(x # xs)\<Rightarrow> root'" |
699 |
by (rule transitions.cons) |
|
700 |
with y show ?thesis by blast |
|
701 |
qed |
|
702 |
} |
|
703 |
qed |
|
704 |
||
705 |
||
706 |
subsection {* Example executions \label{sec:unix-examples} *} |
|
707 |
||
708 |
text {* |
|
709 |
We are now ready to perform a few experiments within our formal |
|
710 |
model of Unix system-calls. The common technique is to alternate |
|
711 |
introduction rules of the transition system (see |
|
712 |
\secref{sec:unix-trans}), and steps to solve any emerging side |
|
713 |
conditions using algebraic properties of the underlying file-system |
|
714 |
structures (see \secref{sec:unix-file-system}). |
|
715 |
*} |
|
716 |
||
717 |
lemmas eval = access_def init_def |
|
718 |
||
719 |
theorem "u \<in> users \<Longrightarrow> can_exec (init users) |
|
720 |
[Mkdir u perms [u, name]]" |
|
721 |
apply (rule can_exec_cons) |
|
722 |
-- {* back-chain @{term can_exec} (of @{term [source] Cons}) *} |
|
723 |
apply (rule mkdir) |
|
724 |
-- {* back-chain @{term Mkdir} *} |
|
725 |
apply (force simp add: eval)+ |
|
726 |
-- {* solve preconditions of @{term Mkdir} *} |
|
727 |
apply (simp add: eval) |
|
728 |
-- {* peek at resulting dir (optional) *} |
|
729 |
txt {* @{subgoals [display]} *} |
|
730 |
apply (rule can_exec_nil) |
|
731 |
-- {* back-chain @{term can_exec} (of @{term [source] Nil}) *} |
|
732 |
done |
|
733 |
||
734 |
text {* |
|
735 |
By inspecting the result shown just before the final step above, we |
|
736 |
may gain confidence that our specification of Unix system-calls |
|
737 |
actually makes sense. Further common errors are usually exhibited |
|
738 |
when preconditions of transition rules fail unexpectedly. |
|
739 |
||
740 |
\medskip Here are a few further experiments, using the same |
|
741 |
techniques as before. |
|
742 |
*} |
|
743 |
||
744 |
theorem "u \<in> users \<Longrightarrow> can_exec (init users) |
|
745 |
[Creat u perms [u, name], |
|
746 |
Unlink u [u, name]]" |
|
747 |
apply (rule can_exec_cons) |
|
748 |
apply (rule creat) |
|
749 |
apply (force simp add: eval)+ |
|
750 |
apply (simp add: eval) |
|
751 |
apply (rule can_exec_cons) |
|
752 |
apply (rule unlink) |
|
753 |
apply (force simp add: eval)+ |
|
754 |
apply (simp add: eval) |
|
755 |
txt {* peek at result: @{subgoals [display]} *} |
|
756 |
apply (rule can_exec_nil) |
|
757 |
done |
|
758 |
||
17455 | 759 |
theorem "u \<in> users \<Longrightarrow> Writable \<in> perms\<^isub>1 \<Longrightarrow> |
760 |
Readable \<in> perms\<^isub>2 \<Longrightarrow> name\<^isub>3 \<noteq> name\<^isub>4 \<Longrightarrow> |
|
10966 | 761 |
can_exec (init users) |
17455 | 762 |
[Mkdir u perms\<^isub>1 [u, name\<^isub>1], |
763 |
Mkdir u' perms\<^isub>2 [u, name\<^isub>1, name\<^isub>2], |
|
764 |
Creat u' perms\<^isub>3 [u, name\<^isub>1, name\<^isub>2, name\<^isub>3], |
|
765 |
Creat u' perms\<^isub>3 [u, name\<^isub>1, name\<^isub>2, name\<^isub>4], |
|
766 |
Readdir u {name\<^isub>3, name\<^isub>4} [u, name\<^isub>1, name\<^isub>2]]" |
|
10966 | 767 |
apply (rule can_exec_cons, rule transition.intros, |
768 |
(force simp add: eval)+, (simp add: eval)?)+ |
|
769 |
txt {* peek at result: @{subgoals [display]} *} |
|
770 |
apply (rule can_exec_nil) |
|
771 |
done |
|
772 |
||
17455 | 773 |
theorem "u \<in> users \<Longrightarrow> Writable \<in> perms\<^isub>1 \<Longrightarrow> Readable \<in> perms\<^isub>3 \<Longrightarrow> |
10966 | 774 |
can_exec (init users) |
17455 | 775 |
[Mkdir u perms\<^isub>1 [u, name\<^isub>1], |
776 |
Mkdir u' perms\<^isub>2 [u, name\<^isub>1, name\<^isub>2], |
|
777 |
Creat u' perms\<^isub>3 [u, name\<^isub>1, name\<^isub>2, name\<^isub>3], |
|
778 |
Write u' ''foo'' [u, name\<^isub>1, name\<^isub>2, name\<^isub>3], |
|
779 |
Read u ''foo'' [u, name\<^isub>1, name\<^isub>2, name\<^isub>3]]" |
|
10966 | 780 |
apply (rule can_exec_cons, rule transition.intros, |
781 |
(force simp add: eval)+, (simp add: eval)?)+ |
|
782 |
txt {* peek at result: @{subgoals [display]} *} |
|
783 |
apply (rule can_exec_nil) |
|
784 |
done |
|
785 |
||
786 |
||
787 |
section {* Odd effects --- treated formally \label{sec:unix-bogosity} *} |
|
788 |
||
789 |
text {* |
|
790 |
We are now ready to give a completely formal treatment of the |
|
791 |
slightly odd situation discussed in the introduction (see |
|
792 |
\secref{sec:unix-intro}): the file-system can easily reach a state |
|
793 |
where a user is unable to remove his very own directory, because it |
|
794 |
is still populated by items placed there by another user in an |
|
795 |
uncouth manner. |
|
796 |
*} |
|
797 |
||
798 |
subsection {* The general procedure \label{sec:unix-inv-procedure} *} |
|
799 |
||
800 |
text {* |
|
801 |
The following theorem expresses the general procedure we are |
|
802 |
following to achieve the main result. |
|
803 |
*} |
|
804 |
||
805 |
theorem general_procedure: |
|
806 |
"(\<And>r r'. Q r \<Longrightarrow> r \<midarrow>y\<rightarrow> r' \<Longrightarrow> False) \<Longrightarrow> |
|
807 |
(\<And>root. init users =bs\<Rightarrow> root \<Longrightarrow> Q root) \<Longrightarrow> |
|
808 |
(\<And>r x r'. r \<midarrow>x\<rightarrow> r' \<Longrightarrow> Q r \<Longrightarrow> P x \<Longrightarrow> Q r') \<Longrightarrow> |
|
809 |
init users =bs\<Rightarrow> root \<Longrightarrow> |
|
810 |
\<not> (\<exists>xs. (\<forall>x \<in> set xs. P x) \<and> can_exec root (xs @ [y]))" |
|
811 |
proof - |
|
812 |
assume cannot_y: "\<And>r r'. Q r \<Longrightarrow> r \<midarrow>y\<rightarrow> r' \<Longrightarrow> False" |
|
813 |
assume init_inv: "\<And>root. init users =bs\<Rightarrow> root \<Longrightarrow> Q root" |
|
814 |
assume preserve_inv: "\<And>r x r'. r \<midarrow>x\<rightarrow> r' \<Longrightarrow> Q r \<Longrightarrow> P x \<Longrightarrow> Q r'" |
|
815 |
assume init_result: "init users =bs\<Rightarrow> root" |
|
816 |
{ |
|
817 |
fix xs |
|
818 |
assume Ps: "\<forall>x \<in> set xs. P x" |
|
819 |
assume can_exec: "can_exec root (xs @ [y])" |
|
820 |
then obtain root' root'' where |
|
821 |
xs: "root =xs\<Rightarrow> root'" and y: "root' \<midarrow>y\<rightarrow> root''" |
|
822 |
by (blast dest: can_exec_snocD) |
|
823 |
from init_result have "Q root" by (rule init_inv) |
|
824 |
from preserve_inv xs this Ps have "Q root'" |
|
825 |
by (rule transitions_invariant) |
|
826 |
from this y have False by (rule cannot_y) |
|
827 |
} |
|
828 |
thus ?thesis by blast |
|
829 |
qed |
|
830 |
||
831 |
text {* |
|
832 |
Here @{prop "P x"} refers to the restriction on file-system |
|
833 |
operations that are admitted after having reached the critical |
|
834 |
configuration; according to the problem specification this will |
|
17455 | 835 |
become @{prop "uid_of x = user\<^isub>1"} later on. Furthermore, |
836 |
@{term y} refers to the operations we claim to be impossible to |
|
837 |
perform afterwards, we will take @{term Rmdir} later. Moreover |
|
838 |
@{term Q} is a suitable (auxiliary) invariant over the file-system; |
|
839 |
choosing @{term Q} adequately is very important to make the proof |
|
840 |
work (see \secref{sec:unix-inv-lemmas}). |
|
10966 | 841 |
*} |
842 |
||
843 |
||
12079 | 844 |
subsection {* The particular situation *} |
10966 | 845 |
|
846 |
text {* |
|
847 |
We introduce a few global declarations and axioms to describe our |
|
12079 | 848 |
particular situation considered here. Thus we avoid excessive use |
849 |
of local parameters in the subsequent development. |
|
10966 | 850 |
*} |
851 |
||
12079 | 852 |
locale situation = |
853 |
fixes users :: "uid set" |
|
17455 | 854 |
and user\<^isub>1 :: uid |
855 |
and user\<^isub>2 :: uid |
|
856 |
and name\<^isub>1 :: name |
|
857 |
and name\<^isub>2 :: name |
|
858 |
and name\<^isub>3 :: name |
|
859 |
and perms\<^isub>1 :: perms |
|
860 |
and perms\<^isub>2 :: perms |
|
861 |
assumes user\<^isub>1_known: "user\<^isub>1 \<in> users" |
|
862 |
and user\<^isub>1_not_root: "user\<^isub>1 \<noteq> 0" |
|
863 |
and users_neq: "user\<^isub>1 \<noteq> user\<^isub>2" |
|
864 |
and perms\<^isub>1_writable: "Writable \<in> perms\<^isub>1" |
|
865 |
and perms\<^isub>2_not_writable: "Writable \<notin> perms\<^isub>2" |
|
866 |
notes facts = user\<^isub>1_known user\<^isub>1_not_root users_neq |
|
867 |
perms\<^isub>1_writable perms\<^isub>2_not_writable |
|
10966 | 868 |
|
12079 | 869 |
fixes bogus :: "operation list" |
870 |
and bogus_path :: path |
|
871 |
defines "bogus \<equiv> |
|
17455 | 872 |
[Mkdir user\<^isub>1 perms\<^isub>1 [user\<^isub>1, name\<^isub>1], |
873 |
Mkdir user\<^isub>2 perms\<^isub>2 [user\<^isub>1, name\<^isub>1, name\<^isub>2], |
|
874 |
Creat user\<^isub>2 perms\<^isub>2 [user\<^isub>1, name\<^isub>1, name\<^isub>2, name\<^isub>3]]" |
|
875 |
and "bogus_path \<equiv> [user\<^isub>1, name\<^isub>1, name\<^isub>2]" |
|
10966 | 876 |
|
877 |
text {* |
|
12079 | 878 |
The @{term bogus} operations are the ones that lead into the uncouth |
879 |
situation; @{term bogus_path} is the key position within the |
|
880 |
file-system where things go awry. |
|
10966 | 881 |
*} |
882 |
||
883 |
||
884 |
subsection {* Invariance lemmas \label{sec:unix-inv-lemmas} *} |
|
885 |
||
886 |
text {* |
|
887 |
The following invariant over the root file-system describes the |
|
888 |
bogus situation in an abstract manner: located at a certain @{term |
|
889 |
path} within the file-system is a non-empty directory that is |
|
17455 | 890 |
neither owned and nor writable by @{term user\<^isub>1}. |
10966 | 891 |
*} |
892 |
||
12079 | 893 |
locale invariant = situation + |
894 |
fixes invariant :: "file \<Rightarrow> path \<Rightarrow> bool" |
|
895 |
defines "invariant root path \<equiv> |
|
10966 | 896 |
(\<exists>att dir. |
17455 | 897 |
access root path user\<^isub>1 {} = Some (Env att dir) \<and> dir \<noteq> empty \<and> |
898 |
user\<^isub>1 \<noteq> owner att \<and> |
|
899 |
access root path user\<^isub>1 {Writable} = None)" |
|
10966 | 900 |
|
901 |
text {* |
|
902 |
Following the general procedure (see |
|
17455 | 903 |
\secref{sec:unix-inv-procedure}) we will now establish the three key |
904 |
lemmas required to yield the final result. |
|
10966 | 905 |
|
906 |
\begin{enumerate} |
|
907 |
||
908 |
\item The invariant is sufficiently strong to entail the |
|
17455 | 909 |
pathological case that @{term user\<^isub>1} is unable to remove the |
910 |
(owned) directory at @{term "[user\<^isub>1, name\<^isub>1]"}. |
|
10966 | 911 |
|
912 |
\item The invariant does hold after having executed the @{term |
|
913 |
bogus} list of operations (starting with an initial file-system |
|
914 |
configuration). |
|
915 |
||
916 |
\item The invariant is preserved by any file-system operation |
|
17455 | 917 |
performed by @{term user\<^isub>1} alone, without any help by other |
918 |
users. |
|
10966 | 919 |
|
920 |
\end{enumerate} |
|
921 |
||
922 |
As the invariant appears both as assumptions and conclusions in the |
|
923 |
course of proof, its formulation is rather critical for the whole |
|
924 |
development to work out properly. In particular, the third step is |
|
925 |
very sensitive to the invariant being either too strong or too weak. |
|
926 |
Moreover the invariant has to be sufficiently abstract, lest the |
|
927 |
proof become cluttered by confusing detail. |
|
928 |
||
929 |
\medskip The first two lemmas are technically straight forward --- |
|
930 |
we just have to inspect rather special cases. |
|
931 |
*} |
|
932 |
||
12079 | 933 |
lemma (in invariant) |
934 |
cannot_rmdir: "invariant root bogus_path \<Longrightarrow> |
|
17455 | 935 |
root \<midarrow>(Rmdir user\<^isub>1 [user\<^isub>1, name\<^isub>1])\<rightarrow> root' \<Longrightarrow> False" |
10966 | 936 |
proof - |
937 |
assume "invariant root bogus_path" |
|
17455 | 938 |
then obtain "file" where "access root bogus_path user\<^isub>1 {} = Some file" |
10966 | 939 |
by (unfold invariant_def) blast |
940 |
moreover |
|
17455 | 941 |
assume "root \<midarrow>(Rmdir user\<^isub>1 [user\<^isub>1, name\<^isub>1])\<rightarrow> root'" |
10966 | 942 |
then obtain att where |
17455 | 943 |
"access root [user\<^isub>1, name\<^isub>1] user\<^isub>1 {} = Some (Env att empty)" |
10966 | 944 |
by cases auto |
17455 | 945 |
hence "access root ([user\<^isub>1, name\<^isub>1] @ [name\<^isub>2]) user\<^isub>1 {} = empty name\<^isub>2" |
10966 | 946 |
by (simp only: access_empty_lookup lookup_append_some) simp |
947 |
ultimately show False by (simp add: bogus_path_def) |
|
948 |
qed |
|
949 |
||
12079 | 950 |
lemma (in invariant) |
951 |
init_invariant: "init users =bogus\<Rightarrow> root \<Longrightarrow> invariant root bogus_path" |
|
10966 | 952 |
proof - |
12079 | 953 |
note eval = facts access_def init_def |
11549 | 954 |
case rule_context thus ?thesis |
10966 | 955 |
apply (unfold bogus_def bogus_path_def) |
956 |
apply (drule transitions_consD, rule transition.intros, |
|
957 |
(force simp add: eval)+, (simp add: eval)?)+ |
|
958 |
-- "evaluate all operations" |
|
959 |
apply (drule transitions_nilD) |
|
960 |
-- "reach final result" |
|
961 |
apply (simp add: invariant_def eval) |
|
962 |
-- "check the invariant" |
|
963 |
done |
|
964 |
qed |
|
965 |
||
966 |
text {* |
|
967 |
\medskip At last we are left with the main effort to show that the |
|
968 |
``bogosity'' invariant is preserved by any file-system operation |
|
17455 | 969 |
performed by @{term user\<^isub>1} alone. Note that this holds for |
970 |
any @{term path} given, the particular @{term bogus_path} is not |
|
10966 | 971 |
required here. |
11004 | 972 |
*} |
10966 | 973 |
|
12079 | 974 |
lemma (in invariant) |
975 |
preserve_invariant: "root \<midarrow>x\<rightarrow> root' \<Longrightarrow> |
|
17455 | 976 |
invariant root path \<Longrightarrow> uid_of x = user\<^isub>1 \<Longrightarrow> invariant root' path" |
10966 | 977 |
proof - |
978 |
assume tr: "root \<midarrow>x\<rightarrow> root'" |
|
979 |
assume inv: "invariant root path" |
|
17455 | 980 |
assume uid: "uid_of x = user\<^isub>1" |
10966 | 981 |
|
982 |
from inv obtain att dir where |
|
17455 | 983 |
inv1: "access root path user\<^isub>1 {} = Some (Env att dir)" and |
10966 | 984 |
inv2: "dir \<noteq> empty" and |
17455 | 985 |
inv3: "user\<^isub>1 \<noteq> owner att" and |
986 |
inv4: "access root path user\<^isub>1 {Writable} = None" |
|
10966 | 987 |
by (auto simp add: invariant_def) |
988 |
||
989 |
from inv1 have lookup: "lookup root path = Some (Env att dir)" |
|
990 |
by (simp only: access_empty_lookup) |
|
991 |
||
17455 | 992 |
from inv1 inv3 inv4 and user\<^isub>1_not_root |
10966 | 993 |
have not_writable: "Writable \<notin> others att" |
994 |
by (auto simp add: access_def split: option.splits if_splits) |
|
995 |
||
996 |
show ?thesis |
|
997 |
proof cases |
|
998 |
assume "root' = root" |
|
999 |
with inv show "invariant root' path" by (simp only:) |
|
1000 |
next |
|
1001 |
assume changed: "root' \<noteq> root" |
|
1002 |
with tr obtain opt where root': "root' = update (path_of x) opt root" |
|
1003 |
by cases auto |
|
1004 |
show ?thesis |
|
1005 |
proof (rule prefix_cases) |
|
1006 |
assume "path_of x \<parallel> path" |
|
1007 |
with inv root' |
|
17455 | 1008 |
have "\<And>perms. access root' path user\<^isub>1 perms = access root path user\<^isub>1 perms" |
10966 | 1009 |
by (simp only: access_update_other) |
1010 |
with inv show "invariant root' path" |
|
1011 |
by (simp only: invariant_def) |
|
1012 |
next |
|
1013 |
assume "path_of x \<le> path" |
|
1014 |
then obtain ys where path: "path = path_of x @ ys" .. |
|
1015 |
||
1016 |
show ?thesis |
|
1017 |
proof (cases ys) |
|
1018 |
assume "ys = []" |
|
17455 | 1019 |
with tr uid inv2 inv3 lookup changed path and user\<^isub>1_not_root |
10966 | 1020 |
have False |
1021 |
by cases (auto simp add: access_empty_lookup dest: access_some_lookup) |
|
1022 |
thus ?thesis .. |
|
1023 |
next |
|
1024 |
fix z zs assume ys: "ys = z # zs" |
|
1025 |
have "lookup root' path = lookup root path" |
|
1026 |
proof - |
|
1027 |
from inv2 lookup path ys |
|
1028 |
have look: "lookup root (path_of x @ z # zs) = Some (Env att dir)" |
|
1029 |
by (simp only:) |
|
1030 |
then obtain att' dir' file' where |
|
1031 |
look': "lookup root (path_of x) = Some (Env att' dir')" and |
|
1032 |
dir': "dir' z = Some file'" and |
|
1033 |
file': "lookup file' zs = Some (Env att dir)" |
|
1034 |
by (blast dest: lookup_some_upper) |
|
1035 |
||
1036 |
from tr uid changed look' dir' obtain att'' where |
|
1037 |
look'': "lookup root' (path_of x) = Some (Env att'' dir')" |
|
1038 |
by cases (auto simp add: access_empty_lookup lookup_update_some |
|
1039 |
dest: access_some_lookup) |
|
1040 |
with dir' file' have "lookup root' (path_of x @ z # zs) = |
|
1041 |
Some (Env att dir)" |
|
1042 |
by (simp add: lookup_append_some) |
|
1043 |
with look path ys show ?thesis |
|
1044 |
by simp |
|
1045 |
qed |
|
1046 |
with inv show "invariant root' path" |
|
1047 |
by (simp only: invariant_def access_def) |
|
1048 |
qed |
|
1049 |
next |
|
1050 |
assume "path < path_of x" |
|
1051 |
then obtain y ys where path: "path_of x = path @ y # ys" .. |
|
1052 |
||
1053 |
obtain dir' where |
|
1054 |
lookup': "lookup root' path = Some (Env att dir')" and |
|
1055 |
inv2': "dir' \<noteq> empty" |
|
1056 |
proof (cases ys) |
|
1057 |
assume "ys = []" |
|
1058 |
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
|
1059 |
with tr uid inv4 changed obtain "file" where |
10966 | 1060 |
"root' = update (path_of x) (Some file) root" |
1061 |
by cases auto |
|
10979 | 1062 |
with lookup parent have "lookup root' path = Some (Env att (dir(y\<mapsto>file)))" |
10966 | 1063 |
by (simp only: update_append_some update_cons_nil_env) |
1064 |
moreover have "dir(y\<mapsto>file) \<noteq> empty" by simp |
|
1065 |
ultimately show ?thesis .. |
|
1066 |
next |
|
1067 |
fix z zs assume ys: "ys = z # zs" |
|
1068 |
with lookup root' path |
|
1069 |
have "lookup root' path = Some (update (y # ys) opt (Env att dir))" |
|
1070 |
by (simp only: update_append_some) |
|
1071 |
also obtain file' where |
|
1072 |
"update (y # ys) opt (Env att dir) = Env att (dir(y\<mapsto>file'))" |
|
1073 |
proof - |
|
1074 |
have "dir y \<noteq> None" |
|
1075 |
proof - |
|
1076 |
have "dir y = lookup (Env att dir) [y]" |
|
1077 |
by (simp split: option.splits) |
|
1078 |
also from lookup have "\<dots> = lookup root (path @ [y])" |
|
1079 |
by (simp only: lookup_append_some) |
|
1080 |
also have "\<dots> \<noteq> None" |
|
1081 |
proof - |
|
1082 |
from ys obtain us u where rev_ys: "ys = us @ [u]" |
|
13601 | 1083 |
by (cases ys rule: rev_cases) fastsimp+ |
10966 | 1084 |
with tr path |
1085 |
have "lookup root ((path @ [y]) @ (us @ [u])) \<noteq> None \<or> |
|
1086 |
lookup root ((path @ [y]) @ us) \<noteq> None" |
|
1087 |
by cases (auto dest: access_some_lookup) |
|
1088 |
thus ?thesis by (blast dest!: lookup_some_append) |
|
1089 |
qed |
|
1090 |
finally show ?thesis . |
|
1091 |
qed |
|
1092 |
with ys show ?thesis |
|
1093 |
by (insert that, auto simp add: update_cons_cons_env) |
|
1094 |
qed |
|
1095 |
also have "dir(y\<mapsto>file') \<noteq> empty" by simp |
|
1096 |
ultimately show ?thesis .. |
|
1097 |
qed |
|
1098 |
||
17455 | 1099 |
from lookup' have inv1': "access root' path user\<^isub>1 {} = Some (Env att dir')" |
10966 | 1100 |
by (simp only: access_empty_lookup) |
1101 |
||
17455 | 1102 |
from inv3 lookup' and not_writable user\<^isub>1_not_root |
1103 |
have "access root' path user\<^isub>1 {Writable} = None" |
|
10966 | 1104 |
by (simp add: access_def) |
1105 |
with inv1' inv2' inv3 show ?thesis by (unfold invariant_def) blast |
|
1106 |
qed |
|
1107 |
qed |
|
1108 |
qed |
|
1109 |
||
1110 |
||
1111 |
subsection {* Putting it all together \label{sec:unix-main-result} *} |
|
1112 |
||
1113 |
text {* |
|
1114 |
The main result is now imminent, just by composing the three |
|
1115 |
invariance lemmas (see \secref{sec:unix-inv-lemmas}) according the the |
|
1116 |
overall procedure (see \secref{sec:unix-inv-procedure}). |
|
1117 |
*} |
|
1118 |
||
13380 | 1119 |
corollary result: |
1120 |
includes invariant |
|
1121 |
assumes bogus: "init users =bogus\<Rightarrow> root" |
|
17455 | 1122 |
shows "\<not> (\<exists>xs. (\<forall>x \<in> set xs. uid_of x = user\<^isub>1) \<and> |
1123 |
can_exec root (xs @ [Rmdir user\<^isub>1 [user\<^isub>1, name\<^isub>1]]))" |
|
10966 | 1124 |
proof - |
13380 | 1125 |
from cannot_rmdir init_invariant preserve_invariant |
1126 |
and bogus show ?thesis by (rule general_procedure) |
|
10966 | 1127 |
qed |
1128 |
||
13380 | 1129 |
text {* |
1130 |
So this is our final result: |
|
1131 |
||
17455 | 1132 |
@{thm [display, show_question_marks = false] result [OF |
1133 |
invariant.intro, OF situation.intro]} |
|
13380 | 1134 |
*} |
1135 |
||
10966 | 1136 |
end |