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