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