1
+ − 1
theory Flask
+ − 2
imports Main Flask_type OS_type_def File_renaming
+ − 3
begin
+ − 4
+ − 5
(****** Policy-language-level rule match/auxiliary functions ******)
+ − 6
+ − 7
fun patt_match :: "'a patt \<Rightarrow> 'a \<Rightarrow> bool"
+ − 8
where
+ − 9
"patt_match (Single x) y = (x = y)"
+ − 10
| "patt_match (Set s) x = (x \<in> s)"
+ − 11
| "patt_match (Tilde s) x = (x \<notin> s)"
+ − 12
| "patt_match Asterisk x = True"
+ − 13
+ − 14
(* match a target with a patt, but including the SELF special
+ − 15
* patt_match_self patt target source
+ − 16
*)
+ − 17
fun patt_match_self :: "(type_t patt) target_with_self_t \<Rightarrow> type_t \<Rightarrow> type_t \<Rightarrow> bool"
+ − 18
where
+ − 19
"patt_match_self Self tt st = (tt = st)"
+ − 20
| "patt_match_self (Not_self (Single p)) tt st = (tt = p)"
+ − 21
| "patt_match_self (Not_self (Set p)) tt st = (tt \<in> p)"
+ − 22
| "patt_match_self (Not_self (Tilde p)) tt st = (tt \<notin> p)"
+ − 23
| "patt_match_self (Not_self Asterisk) tt st = True"
+ − 24
+ − 25
(* list lookup: find the first one *)
+ − 26
fun lookup :: "'a list \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> 'a option"
+ − 27
where
+ − 28
"lookup [] P = None"
+ − 29
| "lookup (x # xs) P = (if (P x) then Some x else lookup xs P)"
+ − 30
+ − 31
(* list member: if exists an element satisfy predicate P *)
+ − 32
fun member :: "'a list \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
+ − 33
where
+ − 34
"member [] P = False"
+ − 35
| "member (x # xs) P = (if (P x) then True else member xs P)"
+ − 36
+ − 37
(******* socket related aux functions ********)
+ − 38
fun is_remote_request :: "t_event \<Rightarrow> bool"
+ − 39
where
+ − 40
"is_remote_request (Accept p fd addr port fd' i) = True"
+ − 41
| "is_remote_request (Connect p fd addr) = True"
+ − 42
| "is_remote_request (SendSock p fd) = True"
+ − 43
| "is_remote_request (RecvSock p fd) = True"
+ − 44
| "is_remote_request e = False"
+ − 45
+ − 46
fun ip_of_addr:: "t_sock_addr \<Rightarrow> t_inet_addr"
+ − 47
where
+ − 48
"ip_of_addr (INET_ADDR saddr sport) = saddr"
+ − 49
+ − 50
fun port_of_addr:: "t_sock_addr \<Rightarrow> t_socket_port"
+ − 51
where
+ − 52
"port_of_addr (INET_ADDR saddr sport) = sport"
+ − 53
+ − 54
fun after_bind:: "t_sock_state option \<Rightarrow> bool"
+ − 55
where
+ − 56
"after_bind None = False"
+ − 57
| "after_bind (Some SS_create) = False"
+ − 58
| "after_bind _= True" (* after bind , it should have local addr *)
+ − 59
+ − 60
fun after_conacc:: "t_sock_state option \<Rightarrow> bool"
+ − 61
where
+ − 62
"after_conacc None = False"
+ − 63
| "after_conacc (Some SS_create) = False"
+ − 64
| "after_conacc (Some SS_bind) = False"
+ − 65
| "after_conacc (Some SS_listen) = False"
+ − 66
| "after_conacc _ = True" (* after connect or accept, it should have remote addr *)
+ − 67
+ − 68
(* inet_addr_compare addr1 addr2 \<equiv> addr1 \<le> addr2; but in \<le>'s def,there is a \<exists>, is not a function*)
+ − 69
fun subnet_addr :: "t_inet_addr \<Rightarrow> t_inet_addr \<Rightarrow> bool"
+ − 70
where
+ − 71
"subnet_addr [] addr = True"
+ − 72
| "subnet_addr (x#xs) [] = False"
+ − 73
| "subnet_addr (x#xs) (y#ys) = ((x = y) \<and> subnet_addr xs ys)"
+ − 74
+ − 75
definition port_in_range :: "t_socket_port \<Rightarrow> t_socket_port \<Rightarrow> t_socket_port \<Rightarrow> bool"
+ − 76
where
+ − 77
"port_in_range minp maxp p \<equiv> ((p < maxp \<or> p = maxp) \<and> (p > minp \<or> p = minp))"
+ − 78
+ − 79
(* initial value of initiate state constrains aux function *)
+ − 80
definition bidirect_in_init :: "'a set \<Rightarrow> ('a \<Rightarrow> 'b option) \<Rightarrow> bool"
+ − 81
where
+ − 82
"bidirect_in_init S f \<equiv> (\<forall> a. a \<in> S \<longrightarrow> (\<exists> b. f a = Some b)) \<and>
+ − 83
(\<forall> a b. f a = Some b \<longrightarrow> a \<in> S)"
+ − 84
+ − 85
fun is_file_itag :: "t_inode_tag \<Rightarrow> bool"
+ − 86
where
+ − 87
"is_file_itag Tag_FILE = True"
+ − 88
| "is_file_itag tag = False"
+ − 89
+ − 90
fun is_dir_itag :: "t_inode_tag \<Rightarrow> bool"
+ − 91
where
+ − 92
"is_dir_itag Tag_DIR = True"
+ − 93
| "is_dir_itag tag = False"
+ − 94
+ − 95
fun is_file_dir_itag :: "t_inode_tag \<Rightarrow> bool"
+ − 96
where
+ − 97
"is_file_dir_itag Tag_FILE = True"
+ − 98
| "is_file_dir_itag Tag_DIR = True"
+ − 99
| "is_file_dir_itag tag = False"
+ − 100
+ − 101
fun is_tcp_itag :: "t_inode_tag \<Rightarrow> bool"
+ − 102
where
+ − 103
"is_tcp_itag Tag_TCP_SOCK = True"
+ − 104
| "is_tcp_itag tag = False"
+ − 105
+ − 106
fun is_udp_itag :: "t_inode_tag \<Rightarrow> bool"
+ − 107
where
+ − 108
"is_udp_itag Tag_UDP_SOCK = True"
+ − 109
| "is_udp_itag tag = False"
+ − 110
+ − 111
fun is_sock_itag :: "t_inode_tag \<Rightarrow> bool"
+ − 112
where
+ − 113
"is_sock_itag Tag_TCP_SOCK = True"
+ − 114
| "is_sock_itag Tag_UDP_SOCK = True"
+ − 115
| "is_sock_itag tag = False"
+ − 116
+ − 117
+ − 118
locale init =
+ − 119
fixes
+ − 120
init_files :: "t_file set"
+ − 121
and init_procs :: "t_process set"
+ − 122
and init_nodes :: "t_node set"
+ − 123
and init_sockets :: "t_socket set"
+ − 124
and init_users :: "user_t set"
+ − 125
+ − 126
and init_file_of_proc_fd :: "t_process \<Rightarrow> t_fd \<rightharpoonup> t_file"
+ − 127
and init_fds_of_proc :: "t_process \<Rightarrow> t_fd set"
+ − 128
and init_oflags_of_proc_fd:: "t_process \<Rightarrow> t_fd \<rightharpoonup> t_open_flags"
+ − 129
and init_inum_of_file :: "t_file \<rightharpoonup> t_inode_num"
+ − 130
and init_inum_of_socket:: "t_socket \<rightharpoonup> t_inode_num"
+ − 131
and init_itag_of_inum:: "t_inode_num \<rightharpoonup> t_inode_tag"
+ − 132
and init_files_hung_by_del :: "t_file set"
+ − 133
(*
+ − 134
and init_file_at_writing_by:: "t_file \<Rightarrow> (t_process \<times> t_fd) set"
+ − 135
*)
+ − 136
and init_socket_state :: "t_socket \<rightharpoonup> t_sock_state"
+ − 137
+ − 138
and init_msgqs :: "t_msgq set"
+ − 139
and init_msgs_of_queue:: "t_msgq \<Rightarrow> t_msg list"
+ − 140
and init_shms :: "t_shm set"
+ − 141
and init_procs_of_shm :: "t_shm \<Rightarrow> (t_process \<times> t_shm_attach_flag) set"
+ − 142
+ − 143
assumes
+ − 144
parent_file_in_init: "\<And> f pf. \<lbrakk>parent f = Some pf; f \<in> init_files\<rbrakk> \<Longrightarrow> pf \<in> init_files"
+ − 145
and root_is_dir: "\<exists> i. init_inum_of_file [] = Some i \<and> init_itag_of_inum i = Some Tag_DIR"
+ − 146
and init_file_has_inum: "\<And> f. f \<in> init_files \<Longrightarrow> \<exists> im. init_inum_of_file f = Some im"
+ − 147
and inof_has_file_tag: "\<And> f im. init_inum_of_file f = Some im \<Longrightarrow> f \<in> init_files \<and> (\<exists> tag. init_itag_of_inum im = Some tag \<and> is_file_dir_itag tag)"
+ − 148
and init_file_no_son: "\<And> f im. \<lbrakk>init_inum_of_file f = Some im; init_itag_of_inum im = Some Tag_FILE\<rbrakk> \<Longrightarrow> \<not> (\<exists> f' \<in> init_files. parent f' = Some f)"
+ − 149
and init_parentf_is_dir: "\<And> f pf im. \<lbrakk>parent f = Some pf; f \<in> init_files; init_inum_of_file pf = Some im\<rbrakk> \<Longrightarrow> init_itag_of_inum im = Some Tag_DIR"
+ − 150
+ − 151
and init_files_hung_valid: "\<And> f. f \<in> init_files_hung_by_del \<Longrightarrow> f \<in> init_files \<and> (\<exists> p fd. init_file_of_proc_fd p fd = Some f)"
+ − 152
and init_files_hung_valid': "\<And> f im. \<lbrakk>f \<in> init_files_hung_by_del; init_inum_of_file f = Some im\<rbrakk> \<Longrightarrow>
+ − 153
init_itag_of_inum im = Some Tag_FILE \<or> (init_itag_of_inum im = Some Tag_DIR \<and> \<not> (\<exists> f'. f' \<in> init_files \<and> f \<prec> f'))"
+ − 154
+ − 155
and init_procfds_valid: "\<And> p fd. fd \<in> init_fds_of_proc p \<Longrightarrow> p \<in> init_procs \<and> ((\<exists> f \<in> init_files. init_file_of_proc_fd p fd = Some f) \<or> (p, fd) \<in> init_sockets)"
+ − 156
and init_filefd_valid: "\<And> p fd f. init_file_of_proc_fd p fd = Some f \<Longrightarrow> fd \<in> init_fds_of_proc p \<and> (\<exists> im. init_inum_of_file f = Some im \<and> init_itag_of_inum im = Some Tag_FILE) \<and> p \<in> init_procs \<and> (\<exists> flags. init_oflags_of_proc_fd p fd = Some flags)"
+ − 157
and init_fileflag_valid: "\<And> p fd flags. init_oflags_of_proc_fd p fd = Some flags \<Longrightarrow> \<exists> f. init_file_of_proc_fd p fd = Some f"
+ − 158
+ − 159
and init_procs_has_shm: "\<And> p h flag. (p,flag) \<in> init_procs_of_shm h \<Longrightarrow> p \<in> init_procs \<and> h \<in> init_shms"
+ − 160
and init_msgs_distinct: "\<And> q. distinct (init_msgs_of_queue q)"
4
+ − 161
and init_msgs_valid: "\<And> m q. m \<in> set (init_msgs_of_queue q) \<Longrightarrow> q \<in> init_msgqs"
1
+ − 162
+ − 163
and init_socket_has_inode: "\<And> p fd. (p, fd) \<in> init_sockets \<Longrightarrow> \<exists> im. init_inum_of_socket (p, fd) = Some im \<and> p \<in> init_procs \<and> fd \<in> init_fds_of_proc p"
+ − 164
and inos_has_sock_tag: "\<And> s im. init_inum_of_socket s = Some im \<Longrightarrow> s \<in> init_sockets \<and> (\<exists> tag. init_itag_of_inum im = Some tag \<and> is_sock_itag tag)"
+ − 165
(*
+ − 166
and init_netobj_has_state: "bidirect_in_init init_netobjs init_netobj_state"
+ − 167
and init_netobj_has_socktype:"bidirect_in_init init_netobjs init_netobj_socktype"
+ − 168
and init_netobj_has_laddr: "\<And> s. after_bind (init_netobj_state s) \<Longrightarrow> init_netobj_localaddr s \<noteq> None"
+ − 169
and init_netobj_has_raddr: "\<And> s. after_conacc (init_netobj_state s) \<Longrightarrow> init_netobj_remoteaddr s \<noteq> None"
+ − 170
*)
+ − 171
+ − 172
and init_finite_sets: "finite init_files \<and> finite init_procs \<and> (\<forall> p. finite (init_fds_of_proc p)) \<and> finite init_shms \<and> finite init_msgqs \<and> finite init_users"
+ − 173
+ − 174
begin
+ − 175
+ − 176
definition is_init_file:: "t_file \<Rightarrow> bool"
+ − 177
where
+ − 178
"is_init_file f \<equiv> (case (init_inum_of_file f) of
+ − 179
Some i \<Rightarrow> (case (init_itag_of_inum i) of
+ − 180
Some Tag_FILE \<Rightarrow> True
+ − 181
| _ \<Rightarrow> False)
+ − 182
| _ \<Rightarrow> False)"
+ − 183
+ − 184
definition is_init_dir:: "t_file \<Rightarrow> bool"
+ − 185
where
+ − 186
"is_init_dir f \<equiv> (case (init_inum_of_file f) of
+ − 187
Some i \<Rightarrow> (case (init_itag_of_inum i) of
+ − 188
Some Tag_DIR \<Rightarrow> True
+ − 189
| _ \<Rightarrow> False)
+ − 190
| _ \<Rightarrow> False)"
+ − 191
+ − 192
definition is_init_tcp_sock:: "t_socket \<Rightarrow> bool"
+ − 193
where
+ − 194
"is_init_tcp_sock s \<equiv> (case (init_inum_of_socket s) of
+ − 195
Some i \<Rightarrow> (case (init_itag_of_inum i) of
+ − 196
Some Tag_TCP_SOCK \<Rightarrow> True
+ − 197
| _ \<Rightarrow> False)
+ − 198
| _ \<Rightarrow> False)"
+ − 199
+ − 200
definition is_init_udp_sock:: "t_socket \<Rightarrow> bool"
+ − 201
where
+ − 202
"is_init_udp_sock s \<equiv> (case (init_inum_of_socket s) of
+ − 203
Some i \<Rightarrow> (case (init_itag_of_inum i) of
+ − 204
Some Tag_UDP_SOCK \<Rightarrow> True
+ − 205
| _ \<Rightarrow> False)
+ − 206
| _ \<Rightarrow> False)"
+ − 207
2
+ − 208
definition init_same_inode_files :: "t_file \<Rightarrow> t_file set"
+ − 209
where
6
8779d321cc2e
remove duplicated pre-condition of current_files in OS_grant, since it is property if is_file/is_dir.
chunhan
diff
changeset
+ − 210
"init_same_inode_files f \<equiv> if is_init_file f then {f'. init_inum_of_file f = init_inum_of_file f'} else {}"
2
+ − 211
1
+ − 212
fun init_alive :: "t_object \<Rightarrow> bool"
+ − 213
where
+ − 214
"init_alive (O_proc p) = (p \<in> init_procs)"
+ − 215
| "init_alive (O_file f) = (is_init_file f)"
+ − 216
| "init_alive (O_dir f) = (is_init_dir f)"
+ − 217
| "init_alive (O_fd p fd) = (fd \<in> init_fds_of_proc p)"
+ − 218
| "init_alive (O_node n) = (n \<in> init_nodes)"
+ − 219
| "init_alive (O_tcp_sock k) = (is_init_tcp_sock k)"
+ − 220
| "init_alive (O_udp_sock k) = (is_init_udp_sock k)"
+ − 221
| "init_alive (O_shm h) = (h \<in> init_shms)"
+ − 222
| "init_alive (O_msgq q) = (q \<in> init_msgqs)"
+ − 223
| "init_alive (O_msg q m) = (m \<in> set (init_msgs_of_queue q) \<and> q \<in> init_msgqs)"
+ − 224
+ − 225
+ − 226
(************ system listeners, event-related ***********)
+ − 227
+ − 228
fun file_of_proc_fd :: "t_state \<Rightarrow> t_process \<Rightarrow> t_fd \<Rightarrow> t_file option"
+ − 229
where
+ − 230
"file_of_proc_fd [] p' fd' = (init_file_of_proc_fd p' fd')"
+ − 231
| "file_of_proc_fd (Open p f flags fd iopt # \<tau>) p' fd' =
+ − 232
(if (p' = p \<and> fd = fd') then Some f else file_of_proc_fd \<tau> p' fd')"
+ − 233
| "file_of_proc_fd (CloseFd p fd # \<tau>) p' fd' =
+ − 234
(if (p = p' \<and> fd = fd') then None else file_of_proc_fd \<tau> p' fd')"
+ − 235
(*deleted: if (f\<^isub>3 \<preceq> f\<^isub>1) then None else *)
+ − 236
(*
+ − 237
| "file_of_proc_fd (Rename p f\<^isub>2 f\<^isub>3 # \<tau>) p' fd =
+ − 238
(case (file_of_proc_fd \<tau> p' fd) of
+ − 239
Some f\<^isub>1 \<Rightarrow> (if (f\<^isub>2 \<preceq> f\<^isub>1) then Some (file_after_rename f\<^isub>2 f\<^isub>3 f\<^isub>1)
+ − 240
else Some f\<^isub>1)
+ − 241
| None \<Rightarrow> None )"
+ − 242
*)
+ − 243
| "file_of_proc_fd (Execve p f fds # \<tau>) p' fd =
+ − 244
(if (p' = p \<and> fd \<in> fds) then file_of_proc_fd \<tau> p fd
+ − 245
else if (p' = p) then None
+ − 246
else file_of_proc_fd \<tau> p' fd) "
+ − 247
| "file_of_proc_fd (Clone p p' fds shms # \<tau>) p'' fd =
+ − 248
(if (p'' = p' \<and> fd \<in> fds) then file_of_proc_fd \<tau> p fd
+ − 249
else if (p'' = p') then None
+ − 250
else file_of_proc_fd \<tau> p'' fd)"
+ − 251
| "file_of_proc_fd (Kill p\<^isub> p\<^isub>' # \<tau>) p'' fd =
+ − 252
(if (p'' = p\<^isub>') then None else file_of_proc_fd \<tau> p'' fd)"
+ − 253
| "file_of_proc_fd (Exit p # \<tau>) p' fd =
+ − 254
(if (p = p') then None else file_of_proc_fd \<tau> p' fd)"
+ − 255
| "file_of_proc_fd (_ # \<tau>) p fd = file_of_proc_fd \<tau> p fd"
+ − 256
+ − 257
definition proc_fd_of_file :: "t_state \<Rightarrow> t_file \<Rightarrow> (t_process \<times> t_fd) set"
+ − 258
where
+ − 259
"proc_fd_of_file \<tau> f = {(p, fd) | p fd. file_of_proc_fd \<tau> p fd = Some f}"
+ − 260
+ − 261
+ − 262
(****** files and directories ******)
+ − 263
+ − 264
fun files_hung_by_del :: "t_state \<Rightarrow> t_file set"
+ − 265
where
+ − 266
"files_hung_by_del [] = init_files_hung_by_del"
+ − 267
| "files_hung_by_del (UnLink p f # \<tau>) = (
+ − 268
if (proc_fd_of_file \<tau> f = {}) then files_hung_by_del \<tau>
+ − 269
else insert f (files_hung_by_del \<tau>))"
+ − 270
(* | files_hung_by_del (Rmdir p f) is not need, for open can only apply to file not dir *)
+ − 271
| "files_hung_by_del (CloseFd p fd # \<tau>) = (
+ − 272
case (file_of_proc_fd \<tau> p fd) of
+ − 273
Some f \<Rightarrow> (if (proc_fd_of_file \<tau> f = {(p, fd)})
+ − 274
then files_hung_by_del \<tau> - {f}
+ − 275
else files_hung_by_del \<tau>)
+ − 276
| None \<Rightarrow> files_hung_by_del \<tau> )"
+ − 277
(*
+ − 278
| "files_hung_by_del (Rename p f\<^isub>2 f\<^isub>3 # \<tau>) = {file_after_rename f\<^isub>2 f\<^isub>3 f\<^isub>1| f\<^isub>1. f\<^isub>1 \<in> files_hung_by_del \<tau>}" (* for rename(2) does not infect on fd of the file, see man -S 2 rename *)
+ − 279
*)
+ − 280
| "files_hung_by_del (e # \<tau>) = files_hung_by_del \<tau>"
+ − 281
declare files_hung_by_del.simps [simp del]
+ − 282
+ − 283
fun inum_of_file :: "t_state \<Rightarrow> (t_file \<rightharpoonup> t_inode_num)"
+ − 284
where
+ − 285
"inum_of_file [] = init_inum_of_file"
+ − 286
| "inum_of_file (Open p f flags fd (Some inum) # \<tau>) = (inum_of_file \<tau>) (f := Some inum)"
+ − 287
| "inum_of_file (CloseFd p fd # \<tau>) = (
+ − 288
case (file_of_proc_fd \<tau> p fd) of
+ − 289
Some f \<Rightarrow> ( if ((proc_fd_of_file \<tau> f = {(p, fd)}) \<and> (f \<in> files_hung_by_del \<tau>))
+ − 290
then (inum_of_file \<tau>) (f := None)
+ − 291
else inum_of_file \<tau> )
+ − 292
| _ \<Rightarrow> (inum_of_file \<tau>) )"
+ − 293
| "inum_of_file (UnLink p f # \<tau>) = (
+ − 294
if (proc_fd_of_file \<tau> f = {})
+ − 295
then (inum_of_file \<tau>) (f := None)
+ − 296
else inum_of_file \<tau> )"
8
+ − 297
| "inum_of_file (Rmdir p f # \<tau>) = (inum_of_file \<tau>) (f := None)"
1
+ − 298
| "inum_of_file (Mkdir p f ino # \<tau>) = (inum_of_file \<tau>) (f:= Some ino)"
+ − 299
| "inum_of_file (LinkHard p f\<^isub>1 f\<^isub>2 # \<tau>) = (inum_of_file \<tau>) (f\<^isub>2 := inum_of_file \<tau> f\<^isub>1)"
+ − 300
(*
+ − 301
| "inum_of_file (Rename p f\<^isub>2 f\<^isub>3 # \<tau>) = (\<lambda> f.
+ − 302
if (f\<^isub>2 \<preceq> f) then None
+ − 303
else if (f\<^isub>3 \<preceq> f) then inum_of_file \<tau> (file_before_rename f\<^isub>2 f\<^isub>3 f)
+ − 304
else inum_of_file \<tau> f )"
+ − 305
*)
+ − 306
| "inum_of_file (e # \<tau>) = inum_of_file \<tau>"
+ − 307
+ − 308
definition current_files :: "t_state \<Rightarrow> t_file set"
+ − 309
where
+ − 310
"current_files \<tau> = {f. \<exists> i. inum_of_file \<tau> f = Some i}"
+ − 311
+ − 312
definition has_same_inode :: "t_state \<Rightarrow> t_file \<Rightarrow> t_file \<Rightarrow> bool"
+ − 313
where
+ − 314
"has_same_inode \<tau> fa fb =
+ − 315
(\<exists>i. inum_of_file \<tau> fa = Some i \<and> inum_of_file \<tau> fb = Some i)"
+ − 316
+ − 317
fun inum_of_socket :: "t_state \<Rightarrow> (t_socket \<rightharpoonup> t_inode_num)"
+ − 318
where
+ − 319
"inum_of_socket [] = init_inum_of_socket"
+ − 320
| "inum_of_socket (CloseFd p fd # \<tau>) =
+ − 321
(inum_of_socket \<tau>) ((p, fd):= None)"
+ − 322
| "inum_of_socket (CreateSock p af st fd ino # \<tau>) =
+ − 323
(inum_of_socket \<tau>) ((p, fd) := Some ino)"
+ − 324
| "inum_of_socket (Accept p fd addr lport' fd' ino # \<tau>) =
+ − 325
(inum_of_socket \<tau>) ((p, fd') := Some ino)"
+ − 326
| "inum_of_socket (Clone p p' fds shms # \<tau>) =
+ − 327
(\<lambda> (p'', fd). if (p'' = p' \<and> fd \<in> fds) then inum_of_socket \<tau> (p, fd)
+ − 328
else if (p'' = p') then None
+ − 329
else inum_of_socket \<tau> (p'',fd))"
+ − 330
| "inum_of_socket (Execve p f fds # \<tau>) =
+ − 331
(\<lambda> (p', fd). if (p' = p \<and> fd \<in> fds) then inum_of_socket \<tau> (p, fd)
+ − 332
else if (p' = p) then None
+ − 333
else inum_of_socket \<tau> (p', fd))"
+ − 334
| "inum_of_socket (Kill p\<^isub>1 p\<^isub>2 # \<tau>) =
+ − 335
(\<lambda> (p, fd). if (p = p\<^isub>2) then None else inum_of_socket \<tau> (p, fd) )"
+ − 336
| "inum_of_socket (Exit p # \<tau>) =
+ − 337
(\<lambda> (p', fd). if (p' = p) then None else inum_of_socket \<tau> (p', fd))"
+ − 338
| "inum_of_socket (_ # \<tau>) = inum_of_socket \<tau>"
+ − 339
+ − 340
definition current_sockets :: "t_state \<Rightarrow> t_socket set"
+ − 341
where
+ − 342
"current_sockets \<tau> = {s. \<exists> i. inum_of_socket \<tau> s = Some i}"
+ − 343
+ − 344
(* experimentary: for the delete obj's (file or socket) inode num, it is unnecessary for itag_of_inum to be NONE, cause this is the situation blocked by inum_of_file / inum_of_socket !!! *)
+ − 345
(* delete a file, just recycle its inode num, but not destroy its inode \<dots>, so it is irrelative to these events, like closeFd, Rmdir, UnLink \<dots>*)
+ − 346
fun itag_of_inum :: "t_state \<Rightarrow> (t_inode_num \<rightharpoonup> t_inode_tag)"
+ − 347
where
+ − 348
"itag_of_inum [] = init_itag_of_inum"
+ − 349
| "itag_of_inum (Open p f flags fd (Some ino) # \<tau>) = (itag_of_inum \<tau>) (ino := Some Tag_FILE)"
+ − 350
| "itag_of_inum (Mkdir p f ino # \<tau>) = (itag_of_inum \<tau>) (ino := Some Tag_DIR)"
+ − 351
| "itag_of_inum (CreateSock p af st fd ino # \<tau>) =
+ − 352
(case st of
+ − 353
STREAM \<Rightarrow> (itag_of_inum \<tau>) (ino := Some Tag_TCP_SOCK)
+ − 354
| DGRAM \<Rightarrow> (itag_of_inum \<tau>) (ino := Some Tag_UDP_SOCK) )"
+ − 355
| "itag_of_inum (Accept p fd addr lport' fd' ino # \<tau>) =
+ − 356
(itag_of_inum \<tau>) (ino := Some Tag_TCP_SOCK)"
6
8779d321cc2e
remove duplicated pre-condition of current_files in OS_grant, since it is property if is_file/is_dir.
chunhan
diff
changeset
+ − 357
| "itag_of_inum (_ # \<tau>) = itag_of_inum \<tau>"
1
+ − 358
+ − 359
definition is_file:: "t_state \<Rightarrow> t_file \<Rightarrow> bool"
+ − 360
where
+ − 361
"is_file \<tau> f \<equiv> (case (inum_of_file \<tau> f) of
+ − 362
Some i \<Rightarrow> (case (itag_of_inum \<tau> i) of
+ − 363
Some Tag_FILE \<Rightarrow> True
+ − 364
| _ \<Rightarrow> False)
+ − 365
| _ \<Rightarrow> False)"
+ − 366
+ − 367
definition is_dir:: "t_state \<Rightarrow> t_file \<Rightarrow> bool"
+ − 368
where
+ − 369
"is_dir \<tau> f \<equiv> (case (inum_of_file \<tau> f) of
+ − 370
Some i \<Rightarrow> (case (itag_of_inum \<tau> i) of
+ − 371
Some Tag_DIR \<Rightarrow> True
+ − 372
| _ \<Rightarrow> False)
+ − 373
| _ \<Rightarrow> False)"
+ − 374
+ − 375
definition dir_is_empty :: "t_state \<Rightarrow> t_file \<Rightarrow> bool"
+ − 376
where
+ − 377
"dir_is_empty \<tau> f \<equiv> is_dir \<tau> f \<and> \<not> (\<exists> f'. f' \<in> current_files \<tau> \<and> f \<prec> f')"
+ − 378
+ − 379
definition is_tcp_sock :: "t_state \<Rightarrow> t_socket \<Rightarrow> bool"
+ − 380
where
+ − 381
"is_tcp_sock \<tau> s \<equiv> (case (inum_of_socket \<tau> s) of
+ − 382
Some i \<Rightarrow> (case (itag_of_inum \<tau> i) of
+ − 383
Some Tag_TCP_SOCK \<Rightarrow> True
+ − 384
| _ \<Rightarrow> False)
+ − 385
| _ \<Rightarrow> False)"
+ − 386
+ − 387
definition is_udp_sock :: "t_state \<Rightarrow> t_socket \<Rightarrow> bool"
+ − 388
where
+ − 389
"is_udp_sock \<tau> s \<equiv> (case (inum_of_socket \<tau> s) of
+ − 390
Some i \<Rightarrow> (case (itag_of_inum \<tau> i) of
+ − 391
Some Tag_UDP_SOCK \<Rightarrow> True
+ − 392
| _ \<Rightarrow> False)
+ − 393
| _ \<Rightarrow> False)"
+ − 394
+ − 395
fun current_procs :: "t_state \<Rightarrow> t_process set"
+ − 396
where
+ − 397
"current_procs [] = init_procs"
+ − 398
| "current_procs (Clone p p' fds shms # \<tau>) = (insert p' (current_procs \<tau>))"
+ − 399
| "current_procs (Exit p # \<tau>) = (current_procs \<tau> - {p})"
+ − 400
| "current_procs (Kill p p' # \<tau>) = (current_procs \<tau> - {p'})"
+ − 401
| "current_procs (_ # \<tau>) = current_procs \<tau>"
+ − 402
+ − 403
fun current_proc_fds :: "t_state \<Rightarrow> t_process \<Rightarrow> t_fd set"
+ − 404
where
+ − 405
"current_proc_fds [] = init_fds_of_proc"
+ − 406
| "current_proc_fds (Open p f flags fd iopt # \<tau>) =
+ − 407
(current_proc_fds \<tau>) (p:= insert fd (current_proc_fds \<tau> p))"
+ − 408
| "current_proc_fds (CreateSock p sf st newfd newi # \<tau>) =
+ − 409
(current_proc_fds \<tau>) (p:= insert newfd (current_proc_fds \<tau> p))"
+ − 410
| "current_proc_fds (Accept p fd raddr port fd' ino # \<tau>) =
+ − 411
(current_proc_fds \<tau>) (p:= insert fd' (current_proc_fds \<tau> p))"
+ − 412
| "current_proc_fds (CloseFd p fd # \<tau>) =
+ − 413
(current_proc_fds \<tau>) (p:= (current_proc_fds \<tau> p) - {fd})"
+ − 414
| "current_proc_fds (Clone p p' fds shms # \<tau>) =
+ − 415
(current_proc_fds \<tau>) (p':= fds)"
+ − 416
| "current_proc_fds (Execve p f fds # \<tau>) =
+ − 417
(current_proc_fds \<tau>) (p:= fds)"
+ − 418
| "current_proc_fds (Exit p # \<tau>) = (current_proc_fds \<tau>) (p := {})"
+ − 419
| "current_proc_fds (Kill p p' # \<tau>) = (current_proc_fds \<tau>) (p' := {})"
+ − 420
| "current_proc_fds (_ # \<tau>) = current_proc_fds \<tau>"
+ − 421
+ − 422
fun current_shms :: "t_state \<Rightarrow> t_shm set"
+ − 423
where
+ − 424
"current_shms [] = init_shms"
+ − 425
| "current_shms (CreateShM p newshm # \<tau>) = insert newshm (current_shms \<tau>)"
+ − 426
| "current_shms (DeleteShM p s # \<tau>) = (current_shms \<tau>) - {s}"
+ − 427
| "current_shms (e # \<tau>) = current_shms \<tau> "
+ − 428
+ − 429
fun procs_of_shm :: "t_state \<Rightarrow> t_shm \<Rightarrow> (t_process \<times> t_shm_attach_flag) set"
+ − 430
where
+ − 431
"procs_of_shm [] = init_procs_of_shm"
+ − 432
| "procs_of_shm (CreateShM p h # \<tau>) =
+ − 433
(procs_of_shm \<tau>) (h := {})" (* creator may not be the sharer of the shm *)
+ − 434
| "procs_of_shm (Attach p h flag # \<tau>) =
+ − 435
(procs_of_shm \<tau>) (h := insert (p, flag) (procs_of_shm \<tau> h))"
+ − 436
| "procs_of_shm (Detach p h # \<tau>) =
+ − 437
(procs_of_shm \<tau>) (h := (procs_of_shm \<tau> h) - {(p,flag). \<exists> flag. (p, flag) \<in> procs_of_shm \<tau> h})"
+ − 438
| "procs_of_shm (DeleteShM p h # \<tau>) = (procs_of_shm \<tau>) (h := {})"
+ − 439
| "procs_of_shm (Clone p p' fds shms # \<tau>) =
+ − 440
(\<lambda> h. if (h \<in> shms)
+ − 441
then (procs_of_shm \<tau> h) \<union> {(p', flag). \<exists> flag. (p, flag) \<in> procs_of_shm \<tau> h}
+ − 442
else procs_of_shm \<tau> h)"
+ − 443
| "procs_of_shm (Execve p f fds # \<tau>) =
+ − 444
(\<lambda> h. procs_of_shm \<tau> h - {(p, flag). \<exists> flag. (p, flag) \<in> procs_of_shm \<tau> h})"
+ − 445
| "procs_of_shm (e # \<tau>) = procs_of_shm \<tau>"
+ − 446
+ − 447
definition info_flow_shm :: "t_state \<Rightarrow> t_process \<Rightarrow> t_process \<Rightarrow> bool"
+ − 448
where
+ − 449
"info_flow_shm \<tau> from to \<equiv> (from = to) \<or> (\<exists> h \<in> current_shms \<tau>. \<exists> toflag.
+ − 450
(((from, SHM_RDWR) \<in> procs_of_shm \<tau> h) \<and> ((to, toflag) \<in> procs_of_shm \<tau> h)))"
+ − 451
+ − 452
fun current_msgqs :: "t_state \<Rightarrow> t_msg set"
+ − 453
where
+ − 454
"current_msgqs [] = init_msgqs"
+ − 455
| "current_msgqs (CreateMsgq p q # \<tau>) = insert q (current_msgqs \<tau>)"
+ − 456
| "current_msgqs (RemoveMsgq p q # \<tau>) = (current_msgqs \<tau>) - {q}"
+ − 457
| "current_msgqs (_ # \<tau>) = current_msgqs \<tau>"
+ − 458
+ − 459
fun msgs_of_queue :: "t_state \<Rightarrow> t_msgq \<Rightarrow> t_msg list"
+ − 460
where
+ − 461
"msgs_of_queue [] = init_msgs_of_queue"
+ − 462
| "msgs_of_queue (CreateMsgq p q # \<tau>) = (msgs_of_queue \<tau>)(q := [])"
+ − 463
| "msgs_of_queue (SendMsg p q m # \<tau>) = (msgs_of_queue \<tau>)(q := msgs_of_queue \<tau> q @ [m])"
+ − 464
| "msgs_of_queue (RecvMsg p q m # \<tau>) = (msgs_of_queue \<tau>)(q := tl (msgs_of_queue \<tau> q))"
+ − 465
| "msgs_of_queue (RemoveMsgq p q # \<tau>) = (msgs_of_queue \<tau>)(q := [])"
+ − 466
| "msgs_of_queue (_ # \<tau>) = msgs_of_queue \<tau>"
+ − 467
+ − 468
definition current_file_inums :: "t_state \<Rightarrow> t_inode_num set"
+ − 469
where
+ − 470
"current_file_inums \<tau> \<equiv> {im. \<exists> f. inum_of_file \<tau> f = Some im}"
+ − 471
+ − 472
definition current_sock_inums :: "t_state \<Rightarrow> t_inode_num set"
+ − 473
where
+ − 474
"current_sock_inums \<tau> = {im. \<exists> s. inum_of_socket \<tau> s = Some im}"
+ − 475
+ − 476
definition current_inode_nums :: "t_state \<Rightarrow> nat set"
+ − 477
where
+ − 478
"current_inode_nums \<tau> = current_file_inums \<tau> \<union> current_sock_inums \<tau>"
+ − 479
2
+ − 480
definition same_inode_files :: "t_state \<Rightarrow> t_file \<Rightarrow> t_file set"
+ − 481
where
6
8779d321cc2e
remove duplicated pre-condition of current_files in OS_grant, since it is property if is_file/is_dir.
chunhan
diff
changeset
+ − 482
"same_inode_files s f \<equiv> if is_file s f then {f'. inum_of_file s f = inum_of_file s f'} else {}"
2
+ − 483
1
+ − 484
fun flags_of_proc_fd :: "t_state \<Rightarrow> t_process \<Rightarrow> t_fd \<Rightarrow> t_open_flags option"
+ − 485
where
+ − 486
"flags_of_proc_fd [] p fd = init_oflags_of_proc_fd p fd"
+ − 487
| "flags_of_proc_fd (Open p f flags fd iopt # \<tau>) p' fd' =
+ − 488
(if (p = p' \<and> fd = fd') then Some flags else flags_of_proc_fd \<tau> p' fd')"
+ − 489
| "flags_of_proc_fd (CloseFd p fd # \<tau>) p' fd' =
+ − 490
(if (p = p' \<and> fd = fd') then None else flags_of_proc_fd \<tau> p' fd')"
+ − 491
| "flags_of_proc_fd (CreateSock p af st fd ino # \<tau>) p' fd' =
+ − 492
(if (p = p' \<and> fd = fd') then None else flags_of_proc_fd \<tau> p' fd')"
+ − 493
| "flags_of_proc_fd (Accept p fd addr lport' fd' ino # \<tau>) p' fd'' =
+ − 494
(if (p = p' \<and> fd' = fd'') then None else flags_of_proc_fd \<tau> p' fd'')"
+ − 495
| "flags_of_proc_fd (Clone p p' fds shms # \<tau>) p'' fd =
+ − 496
(if (p' = p'' \<and> fd \<in> fds) then flags_of_proc_fd \<tau> p fd else flags_of_proc_fd \<tau> p'' fd)"
+ − 497
| "flags_of_proc_fd (Execve p f fds # \<tau>) p' fd =
+ − 498
(if (p' = p \<and> fd \<in> fds) then flags_of_proc_fd \<tau> p fd else flags_of_proc_fd \<tau> p' fd)"
+ − 499
| "flags_of_proc_fd (Kill p\<^isub>1 p\<^isub>2 # \<tau>) p fd =
+ − 500
(if (p = p\<^isub>2) then None else flags_of_proc_fd \<tau> p fd)"
+ − 501
| "flags_of_proc_fd (Exit p # \<tau>) p' fd' =
+ − 502
(if (p = p') then None else flags_of_proc_fd \<tau> p' fd')"
+ − 503
| "flags_of_proc_fd (e # \<tau>) p fd = flags_of_proc_fd \<tau> p fd"
+ − 504
+ − 505
(*
+ − 506
fun file_at_writing_by:: "t_state \<Rightarrow> t_file \<Rightarrow> (t_process \<times> t_fd) set"
+ − 507
where
+ − 508
"file_at_writing_by [] f = init_file_at_writing_by f"
+ − 509
| "file_at_writing_by (Open p f flags fd (Some inum) # \<tau>) f' = (
+ − 510
if (f' = f)
+ − 511
then ( if (is_write_flag flags)
+ − 512
then {(p, fd)}
+ − 513
else {} )
+ − 514
else file_at_writing_by \<tau> f' )"
+ − 515
| "file_at_writing_by (Open p f flags fd None # \<tau>) f' = (
+ − 516
if (f' = f \<and> is_write_flag flags)
+ − 517
then insert (p, fd) (file_at_writing_by \<tau> f)
+ − 518
else file_at_writing_by \<tau> f' )"
+ − 519
| "file_at_writing_by (Mkdir p f inum # \<tau>) f' =
+ − 520
(if (f' = f) then {} else file_at_writing_by \<tau> f')"
+ − 521
| "file_at_writing_by (LinkHard p f f' # \<tau>) f'' =
+ − 522
(if (f'' = f') then file_at_writing_by \<tau> f else file_at_writing_by \<tau> f'')"
+ − 523
| "file_at_writing_by (Rename p f\<^isub>2 f\<^isub>3 # \<tau>) f = (
+ − 524
if (f\<^isub>3 \<preceq> f) then file_at_writing_by \<tau> (file_before_rename f\<^isub>2 f\<^isub>3 f)
+ − 525
else file_at_writing_by \<tau> f )"
+ − 526
| "file_at_writing_by (CloseFd p fd # \<tau>) f =
+ − 527
(file_at_writing_by \<tau> f - {(p, fd)})"
+ − 528
| "file_at_writing_by (Clone p p' fds shms # \<tau>) f =
+ − 529
(file_at_writing_by \<tau> f) \<union> {(p',fd)| fd. fd \<in> fds \<and> (p, fd) \<in> file_at_writing_by \<tau> f}"
+ − 530
| "file_at_writing_by (Execve p f fds # \<tau>) f' =
+ − 531
(if (f' = f) then {} else file_at_writing_by \<tau> f')"
+ − 532
| "file_at_writing_by (Exit p # \<tau>) f =
+ − 533
(file_at_writing_by \<tau> f - {(p, fd)| fd. (p, fd) \<in> file_at_writing_by \<tau> f})"
+ − 534
| "file_at_writing_by (Kill p p' # \<tau>) f =
+ − 535
(file_at_writing_by \<tau> f - {(p', fd)| fd. (p', fd) \<in> file_at_writing_by \<tau> f})"
+ − 536
| "file_at_writing_by (e # \<tau>) f = file_at_writing_by \<tau> f"
+ − 537
*)
+ − 538
+ − 539
definition current_users :: "t_state \<Rightarrow> user_t set"
+ − 540
where
+ − 541
"current_users \<tau> \<equiv> init_users"
+ − 542
+ − 543
fun update_with_shuthow :: "t_sock_state option \<Rightarrow> t_shutdown_how \<Rightarrow> t_sock_state option"
+ − 544
where
+ − 545
"update_with_shuthow (Some (SS_trans Trans_Recv)) SHUT_RD = Some (SS_trans Trans_No)"
+ − 546
| "update_with_shuthow (Some (SS_trans Trans_RS)) SHUT_RD = Some (SS_trans Trans_Send)"
+ − 547
| "update_with_shuthow (Some (SS_trans Trans_Send)) SHUT_RD = Some (SS_trans Trans_Send)"
+ − 548
| "update_with_shuthow (Some (SS_trans Trans_No)) SHUT_RD = Some (SS_trans Trans_No)"
+ − 549
| "update_with_shuthow (Some (SS_trans Trans_Recv)) SHUT_WR = Some (SS_trans Trans_Recv)"
+ − 550
| "update_with_shuthow (Some (SS_trans Trans_RS)) SHUT_WR = Some (SS_trans Trans_Recv)"
+ − 551
| "update_with_shuthow (Some (SS_trans Trans_Send)) SHUT_WR = Some (SS_trans Trans_No)"
+ − 552
| "update_with_shuthow (Some (SS_trans Trans_No)) SHUT_WR = Some (SS_trans Trans_No)"
+ − 553
| "update_with_shuthow (Some (SS_trans Trans_Recv)) SHUT_RDWR = Some (SS_trans Trans_No)"
+ − 554
| "update_with_shuthow (Some (SS_trans Trans_RS)) SHUT_RDWR = Some (SS_trans Trans_No)"
+ − 555
| "update_with_shuthow (Some (SS_trans Trans_Send)) SHUT_RDWR = Some (SS_trans Trans_No)"
+ − 556
| "update_with_shuthow (Some (SS_trans Trans_No)) SHUT_RDWR = Some (SS_trans Trans_No)"
+ − 557
| "update_with_shuthow opt1 how = None"
+ − 558
+ − 559
fun socket_state :: "t_state \<Rightarrow> t_socket \<Rightarrow> t_sock_state option"
+ − 560
where
+ − 561
"socket_state [] = init_socket_state"
+ − 562
| "socket_state (CloseFd p fd # \<tau>) =
+ − 563
(socket_state \<tau>) ((p, fd):= None)"
+ − 564
| "socket_state (CreateSock p af st fd ino # \<tau>) =
+ − 565
(socket_state \<tau>) ((p, fd) := Some SS_create)"
+ − 566
| "socket_state (Bind p fd addr # \<tau>) =
+ − 567
(socket_state \<tau>) ((p, fd) := Some SS_bind)"
+ − 568
| "socket_state (Connect p fd addr # \<tau>) =
+ − 569
(socket_state \<tau>) ((p, fd) := Some (SS_trans Trans_RS))"
+ − 570
| "socket_state (Listen p fd # \<tau>) =
+ − 571
(socket_state \<tau>) ((p, fd) := Some SS_listen)"
+ − 572
| "socket_state (Accept p fd addr lport' fd' ino # \<tau>) =
+ − 573
(socket_state \<tau>) ((p, fd') := Some (SS_trans Trans_RS))"
+ − 574
| "socket_state (Shutdown p fd sh # \<tau>) =
+ − 575
(socket_state \<tau>) ((p, fd) := update_with_shuthow (socket_state \<tau> (p, fd)) sh)"
+ − 576
| "socket_state (Clone p p' fds shms # \<tau>) =
+ − 577
(\<lambda> (p'', fd). if (p'' = p' \<and> fd \<in> fds) then socket_state \<tau> (p, fd)
+ − 578
else if (p'' = p') then None
+ − 579
else socket_state \<tau> (p'', fd))"
+ − 580
| "socket_state (Execve p f fds # \<tau>) =
+ − 581
(\<lambda> (p', fd). if (p' = p \<and> fd \<in> fds) then socket_state \<tau> (p, fd)
+ − 582
else if (p' = p) then None
+ − 583
else socket_state \<tau> (p', fd))"
+ − 584
| "socket_state (Kill p\<^isub>1 p\<^isub>2 # \<tau>) =
+ − 585
(\<lambda> (p, fd). if (p = p\<^isub>2) then None else socket_state \<tau> (p, fd))"
+ − 586
| "socket_state (Exit p # \<tau>) =
+ − 587
(\<lambda> (p', fd'). if (p = p') then None else socket_state \<tau> (p', fd'))"
+ − 588
| "socket_state (e # \<tau>) = socket_state \<tau>"
+ − 589
+ − 590
definition socket_in_trans :: "t_state \<Rightarrow> t_socket \<Rightarrow> bool"
+ − 591
where
+ − 592
"socket_in_trans \<tau> s \<equiv> (case (socket_state \<tau> s) of
+ − 593
Some st \<Rightarrow> (case st of
+ − 594
SS_trans para \<Rightarrow> True
+ − 595
| _ \<Rightarrow> False)
+ − 596
| _ \<Rightarrow> False)"
+ − 597
+ − 598
definition socket_in_sending :: "t_state \<Rightarrow> t_socket \<Rightarrow> bool"
+ − 599
where
+ − 600
"socket_in_sending \<tau> s \<equiv> (socket_state \<tau> s = Some (SS_trans Trans_Send)) \<or> (socket_state \<tau> s = Some (SS_trans Trans_RS))"
+ − 601
+ − 602
definition socket_in_recving :: "t_state \<Rightarrow> t_socket \<Rightarrow> bool"
+ − 603
where
+ − 604
"socket_in_recving \<tau> s \<equiv> (socket_state \<tau> s = Some (SS_trans Trans_Recv)) \<or> (socket_state \<tau> s = Some (SS_trans Trans_RS))"
+ − 605
+ − 606
+ − 607
(******* admissable check by the OS *******)
+ − 608
fun os_grant :: "t_state \<Rightarrow> t_event \<Rightarrow> bool"
+ − 609
where
+ − 610
"os_grant \<tau> (Open p f flags fd inumopt) = (
+ − 611
case inumopt of
+ − 612
Some ino \<Rightarrow>
+ − 613
(p \<in> current_procs \<tau> \<and> f \<notin> current_files \<tau> \<and>
6
8779d321cc2e
remove duplicated pre-condition of current_files in OS_grant, since it is property if is_file/is_dir.
chunhan
diff
changeset
+ − 614
(\<exists> pf. parent f = Some pf \<and> is_dir \<tau> pf \<and> pf \<notin> files_hung_by_del \<tau>) \<and>
1
+ − 615
is_creat_flag flags \<and> fd \<notin> (current_proc_fds \<tau> p) \<and> ino \<notin> (current_inode_nums \<tau>))
+ − 616
| None \<Rightarrow>
6
8779d321cc2e
remove duplicated pre-condition of current_files in OS_grant, since it is property if is_file/is_dir.
chunhan
diff
changeset
+ − 617
(p \<in> current_procs \<tau> \<and> \<not> is_creat_excl_flag flags \<and> is_file \<tau> f \<and>
1
+ − 618
fd \<notin> (current_proc_fds \<tau> p)) )"
+ − 619
(*(if (is_dir \<tau> f) then (is_dir_flag flags \<and> \<not> is_write_flag flags) else (\<not> is_dir_flag flags)),
+ − 620
here open is not applied to directories, readdir is for that, but it is not security-related *)
+ − 621
| "os_grant \<tau> (CloseFd p fd) =
+ − 622
(p \<in> current_procs \<tau> \<and> fd \<in> current_proc_fds \<tau> p)"
+ − 623
| "os_grant \<tau> (UnLink p f) =
6
8779d321cc2e
remove duplicated pre-condition of current_files in OS_grant, since it is property if is_file/is_dir.
chunhan
diff
changeset
+ − 624
(p \<in> current_procs \<tau> \<and> is_file \<tau> f \<and> f \<notin> files_hung_by_del \<tau>)"
1
+ − 625
| "os_grant \<tau> (Rmdir p f) =
6
8779d321cc2e
remove duplicated pre-condition of current_files in OS_grant, since it is property if is_file/is_dir.
chunhan
diff
changeset
+ − 626
(p \<in> current_procs \<tau> \<and> dir_is_empty \<tau> f \<and> f \<notin> files_hung_by_del \<tau> \<and> f \<noteq> [])" (* root file cannot be deleted *)
1
+ − 627
| "os_grant \<tau> (Mkdir p f ino) =
+ − 628
(p \<in> current_procs \<tau> \<and> f \<notin> current_files \<tau> \<and>
6
8779d321cc2e
remove duplicated pre-condition of current_files in OS_grant, since it is property if is_file/is_dir.
chunhan
diff
changeset
+ − 629
(\<exists> pf. parent f = Some pf \<and> is_dir \<tau> pf \<and> pf \<notin> files_hung_by_del \<tau>) \<and>
1
+ − 630
ino \<notin> (current_inode_nums \<tau>))"
+ − 631
| "os_grant \<tau> (LinkHard p f\<^isub>1 f\<^isub>2) =
6
8779d321cc2e
remove duplicated pre-condition of current_files in OS_grant, since it is property if is_file/is_dir.
chunhan
diff
changeset
+ − 632
(p \<in> current_procs \<tau> \<and> is_file \<tau> f\<^isub>1 \<and> f\<^isub>2 \<notin> current_files \<tau> \<and>
8779d321cc2e
remove duplicated pre-condition of current_files in OS_grant, since it is property if is_file/is_dir.
chunhan
diff
changeset
+ − 633
(\<exists> pf\<^isub>2. parent f\<^isub>2 = Some pf\<^isub>2 \<and> is_dir \<tau> pf\<^isub>2 \<and> pf\<^isub>2 \<notin> files_hung_by_del \<tau>))"
1
+ − 634
| "os_grant \<tau> (Truncate p f len) =
6
8779d321cc2e
remove duplicated pre-condition of current_files in OS_grant, since it is property if is_file/is_dir.
chunhan
diff
changeset
+ − 635
(p \<in> current_procs \<tau> \<and> is_file \<tau> f)"
1
+ − 636
(*
+ − 637
| "os_grant \<tau> (FTruncate p fd len) =
+ − 638
(p \<in> current_procs \<tau> \<and> fd \<in> current_proc_fds \<tau> p \<and>
+ − 639
(\<exists> f. file_of_proc_fd \<tau> p fd = Some f \<and> f \<in> current_files \<tau> \<and> is_file \<tau> f) \<and>
+ − 640
(\<exists> flags. flags_of_proc_fd \<tau> p fd = Some flags \<and> is_write_flag flags))"
+ − 641
*)
+ − 642
| "os_grant \<tau> (ReadFile p fd) =
+ − 643
(p \<in> current_procs \<tau> \<and> fd \<in> current_proc_fds \<tau> p \<and>
+ − 644
(\<exists> f. file_of_proc_fd \<tau> p fd = Some f \<and> f \<in> current_files \<tau>) \<and>
+ − 645
(\<exists> flags. flags_of_proc_fd \<tau> p fd = Some flags \<and> is_read_flag flags))"
+ − 646
| "os_grant \<tau> (WriteFile p fd) =
+ − 647
(p \<in> current_procs \<tau> \<and> fd \<in> current_proc_fds \<tau> p \<and>
+ − 648
(\<exists> f. file_of_proc_fd \<tau> p fd = Some f \<and> f \<in> current_files \<tau>) \<and>
+ − 649
(\<exists> flags. flags_of_proc_fd \<tau> p fd = Some flags \<and> is_write_flag flags))"
+ − 650
| "os_grant \<tau> (Execve p f fds) =
6
8779d321cc2e
remove duplicated pre-condition of current_files in OS_grant, since it is property if is_file/is_dir.
chunhan
diff
changeset
+ − 651
(p \<in> current_procs \<tau> \<and> is_file \<tau> f \<and> fds \<subseteq> current_proc_fds \<tau> p)" (* file_at_writing_by \<tau> f = {} *)
1
+ − 652
(*
+ − 653
| "os_grant \<tau> (Rename p f\<^isub>2 f\<^isub>3) =
+ − 654
(p \<in> current_procs \<tau> \<and> f\<^isub>2 \<in> current_files \<tau> \<and> \<not>(f\<^isub>2 \<preceq> f\<^isub>3) \<and> f\<^isub>3 \<notin> current_files \<tau> \<and>
+ − 655
(\<exists> pf\<^isub>3. parent f\<^isub>3 = Some pf\<^isub>3 \<and> pf\<^isub>3 \<in> current_files \<tau> \<and> is_dir \<tau> pf\<^isub>3 \<and>
+ − 656
pf\<^isub>3 \<notin> files_hung_by_del \<tau>) )"
+ − 657
*)
+ − 658
| "os_grant \<tau> (CreateMsgq p q) =
+ − 659
(p \<in> current_procs \<tau> \<and> q \<notin> (current_msgqs \<tau>))"
+ − 660
| "os_grant \<tau> (SendMsg p q m) =
+ − 661
(p \<in> current_procs \<tau> \<and> q \<in> current_msgqs \<tau> \<and> m \<notin> (set (msgs_of_queue \<tau> q)))"
+ − 662
| "os_grant \<tau> (RecvMsg p q m) =
+ − 663
(p \<in> current_procs \<tau> \<and> q \<in> current_msgqs \<tau> \<and> m = hd (msgs_of_queue \<tau> q))"
+ − 664
| "os_grant \<tau> (RemoveMsgq p q) =
+ − 665
(p \<in> current_procs \<tau> \<and> q \<in> current_msgqs \<tau>)"
+ − 666
| "os_grant \<tau> (CreateShM p h) =
+ − 667
(p \<in> current_procs \<tau> \<and> h \<notin> (current_shms \<tau>))"
+ − 668
| "os_grant \<tau> (Attach p h flag) =
+ − 669
(p \<in> current_procs \<tau> \<and> h \<in> current_shms \<tau> \<and> \<not> (\<exists> flag. (p, flag) \<in> procs_of_shm \<tau> h))"
+ − 670
| "os_grant \<tau> (Detach p h) =
+ − 671
(p \<in> current_procs \<tau> \<and> h \<in> current_shms \<tau> \<and> (\<exists> flag. (p, flag) \<in> procs_of_shm \<tau> h))"
+ − 672
| "os_grant \<tau> (DeleteShM p h) =
+ − 673
(p \<in> current_procs \<tau> \<and> h \<in> current_shms \<tau>)"
+ − 674
| "os_grant \<tau> (CreateSock p af st fd ino) =
+ − 675
(p \<in> current_procs \<tau> \<and> af = AF_INET \<and> fd \<notin> (current_proc_fds \<tau> p) \<and>
+ − 676
ino \<notin> (current_inode_nums \<tau>))"
+ − 677
| "os_grant \<tau> (Accept p fd addr port fd' ino) =
+ − 678
(p \<in> current_procs \<tau> \<and> fd \<in> current_proc_fds \<tau> p \<and> (p, fd) \<in> current_sockets \<tau> \<and>
+ − 679
fd' \<notin> (current_proc_fds \<tau> p) \<and> socket_state \<tau> (p, fd) = Some SS_listen \<and>
+ − 680
is_tcp_sock \<tau> (p, fd) \<and> ino \<notin> (current_inode_nums \<tau>))"
+ − 681
| "os_grant \<tau> (Bind p fd addr) =
+ − 682
(p \<in> current_procs \<tau> \<and> fd \<in> current_proc_fds \<tau> p \<and> (p, fd) \<in> current_sockets \<tau> \<and>
+ − 683
socket_state \<tau> (p, fd) = Some SS_create)" (* ?? !! (case addr of INET_ADDR ip port \<Rightarrow> ip \<in> local_ipaddrs)) *)
+ − 684
| "os_grant \<tau> (Connect p fd addr) =
+ − 685
(p \<in> current_procs \<tau> \<and> fd \<in> current_proc_fds \<tau> p \<and> (p, fd) \<in> current_sockets \<tau> \<and>
+ − 686
socket_state \<tau> (p, fd) = Some SS_bind)"
+ − 687
| "os_grant \<tau> (Listen p fd) =
6
8779d321cc2e
remove duplicated pre-condition of current_files in OS_grant, since it is property if is_file/is_dir.
chunhan
diff
changeset
+ − 688
(p \<in> current_procs \<tau> \<and> fd \<in> current_proc_fds \<tau> p \<and> is_tcp_sock \<tau> (p, fd) \<and>
8779d321cc2e
remove duplicated pre-condition of current_files in OS_grant, since it is property if is_file/is_dir.
chunhan
diff
changeset
+ − 689
socket_state \<tau> (p, fd) = Some SS_bind)" (* Listen and Accept should only be used for TCP sever side *)
1
+ − 690
| "os_grant \<tau> (SendSock p fd) =
+ − 691
(p \<in> current_procs \<tau> \<and> fd \<in> current_proc_fds \<tau> p \<and> (p, fd) \<in> current_sockets \<tau> \<and>
+ − 692
socket_in_sending \<tau> (p, fd))"
+ − 693
| "os_grant \<tau> (RecvSock p fd) =
+ − 694
(p \<in> current_procs \<tau> \<and> fd \<in> current_proc_fds \<tau> p \<and> (p, fd) \<in> current_sockets \<tau> \<and>
+ − 695
socket_in_recving \<tau> (p, fd))"
+ − 696
| "os_grant \<tau> (Shutdown p fd sh) =
+ − 697
(p \<in> current_procs \<tau> \<and> fd \<in> current_proc_fds \<tau> p \<and> (p, fd) \<in> current_sockets \<tau> \<and>
+ − 698
socket_in_trans \<tau> (p, fd))"
+ − 699
(*
+ − 700
| "os_grant \<tau> (ChangeOwner p u) = (p \<in> current_procs \<tau> \<and> u \<in> current_users \<tau>)"
+ − 701
*)
+ − 702
| "os_grant \<tau> (Clone p p' fds shms) =
+ − 703
(p \<in> current_procs \<tau> \<and> p' \<notin> (current_procs \<tau>) \<and> fds \<subseteq> current_proc_fds \<tau> p \<and>
+ − 704
(\<forall> h \<in> shms. \<exists> flag. (p, flag) \<in> procs_of_shm \<tau> h))"
+ − 705
| "os_grant \<tau> (Kill p\<^isub>1 p\<^isub>2) =
+ − 706
(p\<^isub>1 \<in> current_procs \<tau> \<and> p\<^isub>2 \<in> current_procs \<tau>)" (* a process can kill itself right? *)
+ − 707
| "os_grant \<tau> (Exit p) =
+ − 708
(p \<in> current_procs \<tau>)"
+ − 709
| "os_grant \<tau> (Ptrace p\<^isub>1 p\<^isub>2) =
+ − 710
(p\<^isub>1 \<in> current_procs \<tau> \<and> p\<^isub>2 \<in> current_procs \<tau>)" (* a process can trace itself right? *)
+ − 711
+ − 712
fun alive :: "t_state \<Rightarrow> t_object \<Rightarrow> bool"
+ − 713
where
+ − 714
"alive s (O_proc p) = (p \<in> current_procs s)"
6
8779d321cc2e
remove duplicated pre-condition of current_files in OS_grant, since it is property if is_file/is_dir.
chunhan
diff
changeset
+ − 715
| "alive s (O_file f) = (is_file s f)"
8779d321cc2e
remove duplicated pre-condition of current_files in OS_grant, since it is property if is_file/is_dir.
chunhan
diff
changeset
+ − 716
| "alive s (O_dir f) = (is_dir s f)"
1
+ − 717
| "alive s (O_fd p fd) = (fd \<in> current_proc_fds s p)"
+ − 718
| "alive s (O_node n) = (n \<in> init_nodes)"
6
8779d321cc2e
remove duplicated pre-condition of current_files in OS_grant, since it is property if is_file/is_dir.
chunhan
diff
changeset
+ − 719
| "alive s (O_tcp_sock k) = (is_tcp_sock s k)"
8779d321cc2e
remove duplicated pre-condition of current_files in OS_grant, since it is property if is_file/is_dir.
chunhan
diff
changeset
+ − 720
| "alive s (O_udp_sock k) = (is_udp_sock s k)"
1
+ − 721
| "alive s (O_shm h) = (h \<in> current_shms s)"
+ − 722
| "alive s (O_msgq q) = (q \<in> current_msgqs s)"
+ − 723
| "alive s (O_msg q m) = (m \<in> set (msgs_of_queue s q) \<and> q \<in> current_msgqs s)"
+ − 724
+ − 725
(* deleted objects should be "taintable" objects, objects like fd should not be in consideration *)
+ − 726
(* actually, deleted should be renamed as "vanished", cause "exit/closefd" *)
+ − 727
fun deleted :: "t_object \<Rightarrow> t_event list \<Rightarrow> bool"
+ − 728
where
+ − 729
"deleted obj [] = False"
+ − 730
| "deleted obj (Kill p p' # s) = ((obj = O_proc p') \<or> (\<exists> fd \<in> current_proc_fds s p'. obj = O_fd p' fd) \<or>
+ − 731
(\<exists> fd. obj = O_tcp_sock (p', fd) \<and> is_tcp_sock s (p',fd)) \<or>
+ − 732
(\<exists> fd. obj = O_udp_sock (p', fd) \<and> is_udp_sock s (p',fd)) \<or>
+ − 733
deleted obj s)"
+ − 734
| "deleted obj (CloseFd p fd # s) = ((obj = O_fd p fd) \<or> (obj = O_tcp_sock (p,fd) \<and> is_tcp_sock s (p,fd)) \<or>
4
+ − 735
(obj = O_udp_sock (p,fd) \<and> is_udp_sock s (p, fd)) \<or>
+ − 736
(\<exists> f. obj = O_file f \<and> proc_fd_of_file s f = {(p, fd)} \<and>
+ − 737
f \<in> files_hung_by_del s) \<or> deleted obj s)"
1
+ − 738
| "deleted obj (Execve p f fds # s) = ((\<exists> fd \<in> current_proc_fds s p. obj = O_fd p fd \<and> fd \<notin> fds) \<or>
+ − 739
(\<exists> fd. obj = O_tcp_sock (p, fd) \<and> is_tcp_sock s (p,fd) \<and> fd \<notin> fds) \<or>
+ − 740
(\<exists> fd. obj = O_udp_sock (p, fd) \<and> is_udp_sock s (p,fd) \<and> fd \<notin> fds) \<or>
+ − 741
deleted obj s)"
4
+ − 742
| "deleted obj (Clone p p' fds shms # s) = ((\<exists> fd \<in> current_proc_fds s p. obj = O_fd p' fd \<and> fd \<notin> fds) \<or>
+ − 743
deleted obj s)"
8
+ − 744
| "deleted obj (UnLink p f # s) = ((proc_fd_of_file s f = {} \<and> obj = O_file f) \<or> deleted obj s)"
1
+ − 745
| "deleted obj (Rmdir p f # s) = ((obj = O_dir f) \<or> deleted obj s)"
+ − 746
| "deleted obj (Exit p # s) = ((obj = O_proc p) \<or> (\<exists> fd \<in> current_proc_fds s p. obj = O_fd p fd) \<or>
+ − 747
(\<exists> fd. obj = O_tcp_sock (p, fd) \<and> is_tcp_sock s (p,fd)) \<or>
+ − 748
(\<exists> fd. obj = O_udp_sock (p, fd) \<and> is_udp_sock s (p,fd)) \<or> deleted obj s)"
+ − 749
(*
+ − 750
| "deleted obj (Rename p f f' # s) =
+ − 751
(case obj of
+ − 752
O_file f'' \<Rightarrow> f \<preceq> f'' \<or> deleted obj s
+ − 753
| O_dir f'' \<Rightarrow> f \<preceq> f'' \<or> deleted obj s
+ − 754
| _ \<Rightarrow> deleted obj s)"
+ − 755
*)
+ − 756
| "deleted obj (RemoveMsgq p q # s) = (obj = O_msgq q \<or> (\<exists> m. obj = O_msg q m) \<or> deleted obj s)"
+ − 757
| "deleted obj (DeleteShM p h # s) = (obj = O_shm h \<or> deleted obj s)"
+ − 758
| "deleted obj (RecvMsg p q m # s) = (obj = O_msg q m \<or> deleted obj s)"
+ − 759
| "deleted obj (_ # s) = deleted obj s"
+ − 760
+ − 761
12
+ − 762
definition proc_file_fds :: "t_state \<Rightarrow> t_process \<Rightarrow> t_fd set"
+ − 763
where
+ − 764
"proc_file_fds s p \<equiv> {fd. \<exists> f. file_of_proc_fd s p fd = Some f}"
+ − 765
+ − 766
+ − 767
definition init_proc_file_fds:: "t_process \<Rightarrow> t_fd set"
+ − 768
"init_proc_file_fds p \<equiv> {fd. \<exists> f. init_file_of_proc_fd p fd = Some f}"
+ − 769
1
+ − 770
end
+ − 771
+ − 772
+ − 773
+ − 774
locale flask = init +
+ − 775
fixes
+ − 776
(***************** Policy-specific Parameters *************)
+ − 777
type_transition_rules :: "type_transition_rule_list_t"
+ − 778
and allowed_rules :: "allowed_rule_list_t"
+ − 779
and role_allowed_rules :: "role_allow_rule_list_t"
+ − 780
and role_declaration_rules :: "role_declarations_list_t"
+ − 781
and role_transition_rules:: "role_transition_rule_list_t"
+ − 782
and constrains_rules :: "constrains_list_t"
+ − 783
+ − 784
and init_type_of_obj :: "t_object \<rightharpoonup> type_t"
+ − 785
and init_user_of_obj :: "t_object \<rightharpoonup> user_t"
+ − 786
and init_role_of_proc :: "t_process \<rightharpoonup> role_t"
+ − 787
+ − 788
assumes
+ − 789
+ − 790
init_obj_has_type: "\<And> obj. init_alive obj \<Longrightarrow> \<exists> t. init_type_of_obj obj = Some t"
+ − 791
and init_type_has_obj: "\<And> obj t. init_type_of_obj obj = Some t \<Longrightarrow> init_alive obj"
+ − 792
and init_obj_has_user: "\<And> obj. init_alive obj \<Longrightarrow> \<exists> u. init_user_of_obj obj = Some u"
+ − 793
and init_user_has_obj: "\<And> obj u. init_user_of_obj obj = Some u \<Longrightarrow> init_alive obj \<and> u \<in> init_users"
+ − 794
and init_proc_has_role: "bidirect_in_init init_procs init_role_of_proc"
+ − 795
+ − 796
begin
+ − 797
+ − 798
(*********** policy-specified computations, not-event-related ************)
+ − 799
+ − 800
(* security_transition_sid : type_transition_rules
+ − 801
* It's a system-call offered by security-server? So access-control-checked too?
+ − 802
* according to selinux, it deals only with execve and file-creation
+ − 803
* is_execve: if is the execve case
+ − 804
* is_file: if is the creation of file/dir
+ − 805
* change from is_execve to is_file, because the new message by default use
+ − 806
* the type of the sending process too.
+ − 807
*)
+ − 808
+ − 809
definition type_transition :: "type_t \<Rightarrow> type_t \<Rightarrow> security_class_t \<Rightarrow> bool \<Rightarrow> type_t"
+ − 810
where
+ − 811
"type_transition st rt tc IS_file \<equiv> (
+ − 812
case (lookup type_transition_rules
+ − 813
(\<lambda> (stp, rtp, tc', dt). patt_match stp st \<and> patt_match rtp rt \<and> tc = tc')) of
+ − 814
None \<Rightarrow> (if IS_file then rt else st)
+ − 815
| Some (stp,rtp,tc',dt) \<Rightarrow> dt)"
+ − 816
+ − 817
(* allowed_rule_list *)
+ − 818
definition TE_allow :: "type_t \<Rightarrow> type_t \<Rightarrow> security_class_t \<Rightarrow> permission_t \<Rightarrow> bool"
+ − 819
where
+ − 820
"TE_allow st tt tc perm \<equiv> member allowed_rules
+ − 821
(\<lambda> (stp, ttp, tc', pp). tc = tc' \<and> patt_match stp st \<and> patt_match_self ttp tt st \<and>
+ − 822
patt_match pp perm)"
+ − 823
+ − 824
(* role_allow_rule_list_t *)
+ − 825
definition role_transition :: "role_t \<Rightarrow> type_t \<Rightarrow> role_t option"
+ − 826
where
+ − 827
"role_transition r t \<equiv>
+ − 828
(case (lookup role_transition_rules (\<lambda> (pr, ft, nr). pr = r \<and> t = ft)) of
+ − 829
None \<Rightarrow> None
+ − 830
| Some (pr,ft,nr) \<Rightarrow> Some nr)"
+ − 831
+ − 832
definition role_decl_check :: "role_t \<Rightarrow> type_t \<Rightarrow> bool"
+ − 833
where
+ − 834
"role_decl_check r t \<equiv> member role_declaration_rules (\<lambda> (role, types). r = role \<and> t \<in> types)"
+ − 835
+ − 836
definition role_allow :: "role_t \<Rightarrow> role_t \<Rightarrow> bool"
+ − 837
where
+ − 838
"role_allow r nr \<equiv> member role_allowed_rules (\<lambda> (from, to). r \<in> from \<and> nr \<in> to)"
+ − 839
+ − 840
(* here don't use lookup, because constrains should all be satisfied,
+ − 841
* while transitions always choose the "first" one
+ − 842
*)
+ − 843
definition constrains_satisfied ::
+ − 844
"security_context_t \<Rightarrow> security_context_t \<Rightarrow> security_class_t \<Rightarrow> permission_t \<Rightarrow> bool"
+ − 845
where
+ − 846
"constrains_satisfied sctxt tctxt c p \<equiv>
+ − 847
(fold (\<lambda> (cset, pset, P) res. res \<and> (if (c \<in> cset \<and> p \<in> pset)
+ − 848
then P sctxt tctxt else True))
+ − 849
constrains_rules True)"
+ − 850
+ − 851
(* main interface for TE_allow check and constrains check *)
+ − 852
fun permission_check::
+ − 853
"security_context_t \<Rightarrow> security_context_t \<Rightarrow> security_class_t \<Rightarrow> permission_t \<Rightarrow> bool"
+ − 854
where
+ − 855
"permission_check (su,sr,st) (tu,tr,tt) c p =
+ − 856
((TE_allow st tt c p) \<and> (constrains_satisfied (su,sr,st) (tu,tr,tt) c p))"
+ − 857
declare permission_check.simps [simp del]
+ − 858
+ − 859
(* no changeowner ??? : by adding "relabel obj sectxt" event or "login" event?
+ − 860
* or this explanation: the running of the sever will not grant any login but running
+ − 861
* of the server-programs!
+ − 862
*)
+ − 863
+ − 864
fun user_of_obj :: "t_state \<Rightarrow> t_object \<Rightarrow> user_t option"
+ − 865
where
+ − 866
"user_of_obj [] = init_user_of_obj"
+ − 867
| "user_of_obj (Clone p p' fds shms # s) =
+ − 868
(\<lambda> obj. case obj of
+ − 869
O_proc p'' \<Rightarrow> if (p'' = p') then user_of_obj s (O_proc p) else user_of_obj s obj
+ − 870
| O_fd p'' fd \<Rightarrow> if (p'' = p' \<and> fd \<in> fds) then user_of_obj s (O_fd p fd)
+ − 871
else if (p'' = p') then None
+ − 872
else user_of_obj s obj
+ − 873
| O_tcp_sock (p'',fd) \<Rightarrow> if (p'' = p' \<and> fd \<in> fds) then user_of_obj s (O_tcp_sock (p, fd))
+ − 874
else if (p'' = p') then None
+ − 875
else user_of_obj s obj
+ − 876
| O_udp_sock (p'',fd) \<Rightarrow> if (p'' = p' \<and> fd \<in> fds) then user_of_obj s (O_udp_sock (p, fd))
+ − 877
else if (p'' = p') then None
+ − 878
else user_of_obj s obj
+ − 879
| _ \<Rightarrow> user_of_obj s obj)"
+ − 880
| "user_of_obj (Open p f flags fd iopt # s) =
+ − 881
(case iopt of
+ − 882
None \<Rightarrow> (user_of_obj s) ((O_fd p fd) := user_of_obj s (O_proc p))
+ − 883
| _ \<Rightarrow> ((user_of_obj s) ((O_fd p fd) := user_of_obj s (O_proc p)))
+ − 884
((O_file f) := user_of_obj s (O_proc p)))"
+ − 885
| "user_of_obj (Mkdir p f inum # s) =
+ − 886
(user_of_obj s) ((O_dir f) := user_of_obj s (O_proc p))"
+ − 887
| "user_of_obj (LinkHard p f f' # s) =
+ − 888
(user_of_obj s) ((O_file f') := user_of_obj s (O_proc p))"
+ − 889
(*
+ − 890
| "user_of_obj (Rename p f2 f3 # s) =
+ − 891
(\<lambda> obj. case obj of
+ − 892
O_file f \<Rightarrow> if (f3 \<preceq> f) then user_of_obj s (O_file (file_before_rename f2 f3 f))
+ − 893
else user_of_obj s obj
+ − 894
| O_dir f \<Rightarrow> if (f3 \<preceq> f) then user_of_obj s (O_dir (file_before_rename f2 f3 f))
+ − 895
else user_of_obj s obj
+ − 896
| _ \<Rightarrow> user_of_obj s obj)"
+ − 897
*)
+ − 898
| "user_of_obj (CreateMsgq p q # s) =
+ − 899
(user_of_obj s) ((O_msgq q) := user_of_obj s (O_proc p))"
+ − 900
| "user_of_obj (SendMsg p q m # s) =
+ − 901
(user_of_obj s) ((O_msg q m) := user_of_obj s (O_proc p))"
+ − 902
| "user_of_obj (CreateShM p h # s) =
+ − 903
(user_of_obj s) ((O_shm h) := user_of_obj s (O_proc p))"
+ − 904
| "user_of_obj (CreateSock p af st fd inum # s) = (\<lambda> obj.
+ − 905
case obj of
+ − 906
O_fd p' fd' \<Rightarrow> if (p' = p \<and> fd' = fd) then user_of_obj s (O_proc p)
+ − 907
else user_of_obj s obj
+ − 908
| O_tcp_sock (p', fd') \<Rightarrow> if (p' = p \<and> fd' = fd \<and> st = STREAM) then user_of_obj s (O_proc p)
+ − 909
else user_of_obj s obj
+ − 910
| O_udp_sock (p', fd') \<Rightarrow> if (p' = p \<and> fd' = fd \<and> st = DGRAM) then user_of_obj s (O_proc p)
+ − 911
else user_of_obj s obj
+ − 912
| _ \<Rightarrow> user_of_obj s obj )"
+ − 913
| "user_of_obj (Accept p fd addr port fd' inum # s) = (\<lambda> obj.
+ − 914
case obj of
+ − 915
O_fd p' fd'' \<Rightarrow> if (p' = p \<and> fd'' = fd') then user_of_obj s (O_proc p)
+ − 916
else user_of_obj s obj
+ − 917
| O_tcp_sock (p', fd'') \<Rightarrow> if (p' = p \<and> fd'' = fd') then user_of_obj s (O_proc p)
+ − 918
else user_of_obj s obj
+ − 919
| _ \<Rightarrow> user_of_obj s obj )"
+ − 920
| "user_of_obj (_ # s) = user_of_obj s"
+ − 921
+ − 922
fun type_of_obj :: "t_state \<Rightarrow> (t_object \<Rightarrow> type_t option)"
+ − 923
where
+ − 924
"type_of_obj [] = init_type_of_obj"
+ − 925
| "type_of_obj (Clone p p' fds shms # s) = (\<lambda> obj.
+ − 926
case obj of
+ − 927
O_proc p'' \<Rightarrow> if (p'' = p') then type_of_obj s (O_proc p) else type_of_obj s obj
+ − 928
| O_fd p'' fd \<Rightarrow> if (p'' = p' \<and> fd \<in> fds) then type_of_obj s (O_fd p fd)
+ − 929
else if (p'' = p') then None
+ − 930
else type_of_obj s obj
+ − 931
| O_tcp_sock (p'', fd) \<Rightarrow> if (p'' = p' \<and> fd \<in> fds) then type_of_obj s (O_tcp_sock (p, fd))
+ − 932
else if (p'' = p') then None
+ − 933
else type_of_obj s obj
+ − 934
| O_udp_sock (p'', fd) \<Rightarrow> if (p'' = p' \<and> fd \<in> fds) then type_of_obj s (O_udp_sock (p, fd))
+ − 935
else if (p'' = p') then None
+ − 936
else type_of_obj s obj
+ − 937
| _ \<Rightarrow> type_of_obj s obj )"
+ − 938
| "type_of_obj (Execve p f fds # s) = (type_of_obj s) ((O_proc p) :=
+ − 939
(case (type_of_obj s (O_proc p), type_of_obj s (O_file f)) of
+ − 940
(Some tp, Some tf) \<Rightarrow> Some (type_transition tp tf C_process False)
+ − 941
| _ \<Rightarrow> None) )"
+ − 942
| "type_of_obj (Open p f flags fd (Some inum) # s) = ((type_of_obj s) ((O_file f) :=
+ − 943
(case (parent f) of
+ − 944
Some pf \<Rightarrow> (case (type_of_obj s (O_proc p), type_of_obj s (O_dir pf)) of
+ − 945
(Some tp, Some tpf) \<Rightarrow> Some (type_transition tp tpf C_file True)
+ − 946
| _ \<Rightarrow> None)
+ − 947
| _ \<Rightarrow> None) )) ((O_fd p fd) :=
+ − 948
type_of_obj s (O_proc p))"
+ − 949
| "type_of_obj (Open p f flags fd None # s) = (type_of_obj s) ((O_fd p fd) :=
+ − 950
type_of_obj s (O_proc p))"
+ − 951
| "type_of_obj (Mkdir p f inum # s) = (type_of_obj s) ((O_dir f) :=
+ − 952
(case (parent f) of
+ − 953
Some pf \<Rightarrow> (case (type_of_obj s (O_proc p), type_of_obj s (O_dir pf)) of
+ − 954
(Some tp, Some tpf) \<Rightarrow> Some (type_transition tp tpf C_dir True)
+ − 955
| _ \<Rightarrow> None)
+ − 956
| _ \<Rightarrow> None) )"
+ − 957
| "type_of_obj (LinkHard p f f' # s) = (type_of_obj s) ((O_file f') :=
+ − 958
(case (parent f') of
+ − 959
Some pf \<Rightarrow> (case (type_of_obj s (O_proc p), type_of_obj s (O_dir pf)) of
+ − 960
(Some tp, Some tpf) \<Rightarrow> Some (type_transition tp tpf C_file True)
+ − 961
| _ \<Rightarrow> None)
+ − 962
| _ \<Rightarrow> None) )"
+ − 963
(*
+ − 964
| "type_of_obj (Rename p f f' # s) = (\<lambda> obj.
+ − 965
case obj of
+ − 966
O_file f'' \<Rightarrow> (if (f' \<preceq> f'') then type_of_obj s (O_file (file_before_rename f f' f''))
+ − 967
else type_of_obj s (O_file f''))
+ − 968
| O_dir f'' \<Rightarrow> (if (f' \<preceq> f'') then type_of_obj s (O_file (file_before_rename f f' f''))
+ − 969
else type_of_obj s (O_file f''))
+ − 970
| _ \<Rightarrow> type_of_obj s obj )"
+ − 971
*)
+ − 972
| "type_of_obj (CreateMsgq p q # s) = (type_of_obj s) ((O_msgq q) :=
+ − 973
(case (type_of_obj s (O_proc p)) of
+ − 974
Some tp \<Rightarrow> Some tp
+ − 975
| _ \<Rightarrow> None) )"
+ − 976
| "type_of_obj (SendMsg p q m # s) = (type_of_obj s) ((O_msg q m) :=
+ − 977
(case (type_of_obj s (O_proc p), type_of_obj s (O_msgq q)) of
+ − 978
(Some tp, Some tq) \<Rightarrow> Some (type_transition tp tq C_msg False)
+ − 979
| _ \<Rightarrow> None) )"
+ − 980
| "type_of_obj (CreateShM p h # s) = (type_of_obj s) ((O_shm h) :=
+ − 981
(case (type_of_obj s (O_proc p)) of
+ − 982
Some tp \<Rightarrow> Some tp
+ − 983
| _ \<Rightarrow> None) )"
+ − 984
| "type_of_obj (CreateSock p af st fd inum # s) = (\<lambda> obj.
+ − 985
case obj of
+ − 986
O_fd p' fd' \<Rightarrow> if (p' = p \<and> fd' = fd) then type_of_obj s (O_proc p)
+ − 987
else type_of_obj s obj
+ − 988
| O_tcp_sock (p', fd') \<Rightarrow> if (p' = p \<and> fd' = fd \<and> st = STREAM) then type_of_obj s (O_proc p)
+ − 989
else type_of_obj s obj
+ − 990
| O_udp_sock (p', fd') \<Rightarrow> if (p' = p \<and> fd' = fd \<and> st = DGRAM) then type_of_obj s (O_proc p)
+ − 991
else type_of_obj s obj
+ − 992
| _ \<Rightarrow> type_of_obj s obj )"
+ − 993
| "type_of_obj (Accept p fd addr port fd' inum # s) = (\<lambda> obj.
+ − 994
case obj of
+ − 995
O_fd p' fd'' \<Rightarrow> if (p' = p \<and> fd'' = fd') then type_of_obj s (O_proc p)
+ − 996
else type_of_obj s obj
+ − 997
| O_tcp_sock (p', fd'') \<Rightarrow> if (p' = p \<and> fd'' = fd') then type_of_obj s (O_proc p)
+ − 998
else type_of_obj s obj
+ − 999
| _ \<Rightarrow> type_of_obj s obj )"
+ − 1000
| "type_of_obj (_ # s) = type_of_obj s"
+ − 1001
+ − 1002
fun role_of_proc :: "t_state \<Rightarrow> (t_process \<Rightarrow> role_t option)"
+ − 1003
where
+ − 1004
"role_of_proc [] = init_role_of_proc"
+ − 1005
| "role_of_proc (Clone p p' fds shms # s) =
+ − 1006
(role_of_proc s) (p' := role_of_proc s p)"
+ − 1007
| "role_of_proc (Execve p f fds # s) = (role_of_proc s) (p :=
+ − 1008
(case (role_of_proc s p, type_of_obj s (O_file f)) of
+ − 1009
(Some r, Some ft) \<Rightarrow> role_transition r ft
+ − 1010
| _ \<Rightarrow> None) )"
+ − 1011
| "role_of_proc (_ # s) = role_of_proc s"
+ − 1012
+ − 1013
fun role_of_obj :: "t_state \<Rightarrow> t_object \<Rightarrow> role_t option"
+ − 1014
where
+ − 1015
"role_of_obj s (O_proc p) = role_of_proc s p"
+ − 1016
| "role_of_obj s _ = Some R_object" (* object_r *)
+ − 1017
+ − 1018
definition sectxt_of_obj :: "t_state \<Rightarrow> t_object \<Rightarrow> security_context_t option"
+ − 1019
where
+ − 1020
"sectxt_of_obj s obj = (
+ − 1021
case (user_of_obj s obj, role_of_obj s obj, type_of_obj s obj) of
+ − 1022
(Some u, Some r, Some t) \<Rightarrow> Some (u, r, t)
+ − 1023
| _ \<Rightarrow> None)"
+ − 1024
7
+ − 1025
fun init_role_of_obj :: "t_object \<Rightarrow> role_t option"
+ − 1026
where
+ − 1027
"init_role_of_obj (O_proc p) = init_role_of_proc p"
+ − 1028
| "init_role_of_obj _ = Some R_object"
+ − 1029
+ − 1030
definition init_sectxt_of_obj :: "t_object \<Rightarrow> security_context_t option"
+ − 1031
where
+ − 1032
"init_sectxt_of_obj obj \<equiv>
+ − 1033
(case (init_user_of_obj obj, init_role_of_obj obj, init_type_of_obj obj) of
+ − 1034
(Some u, Some r, Some t) \<Rightarrow> Some (u,r,t)
+ − 1035
| _ \<Rightarrow> None)"
+ − 1036
+ − 1037
definition sec_of_root :: "security_context_t"
+ − 1038
where
+ − 1039
"sec_of_root \<equiv> (case (init_user_of_obj (O_dir []), init_type_of_obj (O_dir [])) of
+ − 1040
(Some u, Some t) \<Rightarrow> (u, R_object, t))"
+ − 1041
1
+ − 1042
(******* flask granting ********
+ − 1043
* involves three kinds of rules:
+ − 1044
* 1. allow rule of types, allowed_rule_list_t, main
+ − 1045
* 2. allow rule of roles, role_allow_rule_list_t, mainly for execve call
+ − 1046
* 3. constrains, section 3.4.5, user-specifically defined.
+ − 1047
* Passed all these 3, then grant Yes, else No
+ − 1048
*)
+ − 1049
+ − 1050
definition search_check_allp :: "security_context_t \<Rightarrow> security_context_t set \<Rightarrow> bool"
+ − 1051
where
+ − 1052
"search_check_allp pctxt sectxts = (\<forall> sec \<in> sectxts. permission_check pctxt sec C_dir P_search)"
+ − 1053
+ − 1054
definition search_check_file :: "security_context_t \<Rightarrow> security_context_t \<Rightarrow> bool"
+ − 1055
where
+ − 1056
"search_check_file sctxt fctxt \<equiv> permission_check sctxt fctxt C_file P_search"
+ − 1057
+ − 1058
definition search_check_dir :: "security_context_t \<Rightarrow> security_context_t \<Rightarrow> bool"
+ − 1059
where
+ − 1060
"search_check_dir sctxt fctxt \<equiv> permission_check sctxt fctxt C_dir P_search"
+ − 1061
+ − 1062
fun get_parentfs_ctxts :: "t_state \<Rightarrow> t_file \<Rightarrow> (security_context_t list) option"
+ − 1063
where
+ − 1064
"get_parentfs_ctxts s [] =
+ − 1065
(case (sectxt_of_obj s (O_dir [])) of
+ − 1066
Some ctxt \<Rightarrow> Some [ctxt]
+ − 1067
| _ \<Rightarrow> None)"
+ − 1068
| "get_parentfs_ctxts s (f#pf) =
+ − 1069
(case (get_parentfs_ctxts s pf, sectxt_of_obj s (O_dir (f#pf))) of
+ − 1070
(Some ctxts, Some ctxt) \<Rightarrow> Some (ctxt#ctxts)
+ − 1071
| _ \<Rightarrow> None)"
+ − 1072
+ − 1073
fun search_check :: "t_state \<Rightarrow> security_context_t \<Rightarrow> t_file \<Rightarrow> bool"
+ − 1074
where
+ − 1075
"search_check s pctxt [] =
+ − 1076
(case (sectxt_of_obj s (O_dir [])) of
+ − 1077
Some fctxt \<Rightarrow> search_check_dir pctxt fctxt
+ − 1078
| _ \<Rightarrow> False)"
+ − 1079
| "search_check s pctxt (f#pf) =
+ − 1080
(if (is_file s (f#pf))
+ − 1081
then (case (sectxt_of_obj s (O_file (f#pf)), get_parentfs_ctxts s pf) of
+ − 1082
(Some fctxt, Some pfctxts) \<Rightarrow> (search_check_file pctxt fctxt \<and> search_check_allp pctxt (set pfctxts))
+ − 1083
| _ \<Rightarrow> False)
+ − 1084
else (case (sectxt_of_obj s (O_dir (f#pf)), get_parentfs_ctxts s pf) of
+ − 1085
(Some fctxt, Some pfctxts) \<Rightarrow> (search_check_dir pctxt fctxt \<and> search_check_allp pctxt (set pfctxts))
+ − 1086
| _ \<Rightarrow> False))"
+ − 1087
+ − 1088
(* this means we should prove every current fd has a security context! *)
+ − 1089
definition sectxts_of_fds :: "t_state \<Rightarrow> t_process \<Rightarrow> t_fd set \<Rightarrow> security_context_t set"
+ − 1090
where
+ − 1091
"sectxts_of_fds s p fds \<equiv> {ctxt. \<exists> fd \<in> fds. sectxt_of_obj s (O_fd p fd) = Some ctxt}"
+ − 1092
+ − 1093
definition inherit_fds_check :: "t_state \<Rightarrow> security_context_t \<Rightarrow> t_process \<Rightarrow> t_fd set \<Rightarrow> bool"
+ − 1094
where
+ − 1095
"inherit_fds_check s npsectxt p fds \<equiv>
+ − 1096
(\<forall> ctxt \<in> sectxts_of_fds s p fds. permission_check npsectxt ctxt C_fd P_inherit)"
+ − 1097
+ − 1098
fun npctxt_execve :: "security_context_t \<Rightarrow> security_context_t \<Rightarrow> security_context_t option"
+ − 1099
where
+ − 1100
"npctxt_execve (pu,pr,pt) (fu,fr,ft) =
+ − 1101
(case (role_transition pr ft) of
+ − 1102
Some nr \<Rightarrow> Some (pu, nr, type_transition pt ft C_process False)
+ − 1103
| _ \<Rightarrow> None)"
+ − 1104
+ − 1105
fun nfctxt_create :: "security_context_t \<Rightarrow> security_context_t \<Rightarrow> security_class_t \<Rightarrow> security_context_t"
+ − 1106
where
+ − 1107
"nfctxt_create (pu,pr,pt) (fu,fr,ft) cls = (pu, R_object, type_transition pt ft cls True)"
+ − 1108
+ − 1109
fun grant_execve ::
+ − 1110
"security_context_t \<Rightarrow> security_context_t \<Rightarrow> security_context_t \<Rightarrow> bool"
+ − 1111
where
+ − 1112
"grant_execve (up,rp,tp) (uf,rf,tf) (up',nr,nt) =
+ − 1113
(role_decl_check nr nt \<and> role_allow rp nr \<and>
+ − 1114
permission_check (up,rp,tp) (uf,rf,tf) C_file P_execute \<and>
+ − 1115
permission_check (up,nr,nt) (uf,rf,tf) C_file P_entrypoint \<and>
+ − 1116
permission_check (up,rp,tp) (up,nr,nt) C_process P_transition \<and>
+ − 1117
permission_check (up,nr,nt) (uf,rf,tf) C_process P_execute)"
+ − 1118
+ − 1119
definition sectxts_of_shms :: "t_state \<Rightarrow> t_shm set \<Rightarrow> security_context_t set"
+ − 1120
where
+ − 1121
"sectxts_of_shms s shms \<equiv> {ctxt. \<exists> h \<in> shms. sectxt_of_obj s (O_shm h) = Some ctxt}"
+ − 1122
+ − 1123
definition inherit_shms_check :: "t_state \<Rightarrow> security_context_t \<Rightarrow> t_shm set \<Rightarrow> bool"
+ − 1124
where
+ − 1125
"inherit_shms_check s npsectxt shms \<equiv>
+ − 1126
(\<forall> ctxt \<in> sectxts_of_shms s shms. permission_check npsectxt ctxt C_shm P_inherit)"
+ − 1127
+ − 1128
fun perm_of_mflag :: "t_open_must_flag \<Rightarrow> permission_t set"
+ − 1129
where
+ − 1130
"perm_of_mflag OF_RDONLY = {P_read}"
+ − 1131
| "perm_of_mflag OF_WRONLY = {P_write}"
+ − 1132
| "perm_of_mflag OF_RDWR = {P_read, P_write}"
+ − 1133
+ − 1134
definition perm_of_oflag :: "t_open_option_flag set \<Rightarrow> permission_t set"
+ − 1135
where
+ − 1136
"perm_of_oflag oflags \<equiv>
+ − 1137
(case (OF_APPEND \<in> oflags, OF_CREAT \<in> oflags) of
+ − 1138
(True, True) \<Rightarrow> {P_append, P_create}
+ − 1139
| (True, _ ) \<Rightarrow> {P_append}
+ − 1140
| (_ , True) \<Rightarrow> {P_create}
+ − 1141
| _ \<Rightarrow> {})"
+ − 1142
+ − 1143
definition perms_of_flags :: "t_open_flags \<Rightarrow> permission_t set"
+ − 1144
where
+ − 1145
"perms_of_flags flags \<equiv>
+ − 1146
(case flags of
+ − 1147
(mflag,oflags) \<Rightarrow> (perm_of_mflag mflag \<union> perm_of_oflag oflags))"
+ − 1148
+ − 1149
(*
+ − 1150
definition class_of_flags :: "t_open_flags \<Rightarrow> security_class_t"
+ − 1151
where
+ − 1152
"class_of_flags flags \<equiv>
+ − 1153
(case flags of
+ − 1154
(mflag, oflags) \<Rightarrow> if (OF_DIRECTORY \<in> oflags) then C_dir else C_file)"
+ − 1155
+ − 1156
definition obj_of_flags :: "t_open_flags \<Rightarrow> t_file \<Rightarrow> t_object"
+ − 1157
where
+ − 1158
"obj_of_flags flags f \<equiv>
+ − 1159
(case flags of
+ − 1160
(mflag, oflags) \<Rightarrow> if (OF_DIRECTORY \<in> oflags) then O_dir f else O_file f)"
+ − 1161
*)
+ − 1162
+ − 1163
definition oflags_check :: "t_open_flags \<Rightarrow> security_context_t \<Rightarrow> security_context_t \<Rightarrow> bool"
+ − 1164
where
+ − 1165
"oflags_check flags pctxt fctxt \<equiv>
+ − 1166
\<forall> perm \<in> (perms_of_flags flags). permission_check pctxt fctxt C_file perm"
+ − 1167
+ − 1168
fun grant :: "t_state \<Rightarrow> t_event \<Rightarrow> bool"
+ − 1169
where
+ − 1170
"grant s (Execve p f fds) =
+ − 1171
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_file f)) of
+ − 1172
(Some (up, rp, tp), Some (uf, rf, tf)) \<Rightarrow>
+ − 1173
(case (npctxt_execve (up,rp,tp) (uf,rf,tf)) of
+ − 1174
Some (pu,nr,nt) \<Rightarrow> (
+ − 1175
search_check s (up,rp,tp) f \<and>
+ − 1176
grant_execve (up,rp,tp) (uf,rf,tf) (up,nr,nt) \<and>
+ − 1177
inherit_fds_check s (up,nr,nt) p fds)
+ − 1178
| _ \<Rightarrow> False)
+ − 1179
| _ \<Rightarrow> False )"
+ − 1180
| "grant s (Clone p p' fds shms) =
+ − 1181
(case (sectxt_of_obj s (O_proc p)) of
+ − 1182
Some (up, rp, tp) \<Rightarrow>
+ − 1183
(permission_check (up,rp,tp) (up,rp,tp) C_process P_fork \<and>
+ − 1184
inherit_fds_check s (up,rp,tp) p fds \<and>
+ − 1185
inherit_shms_check s (up,rp,tp) shms)
+ − 1186
| _ \<Rightarrow> False )"
+ − 1187
| "grant s (Kill p p') =
+ − 1188
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_proc p')) of
+ − 1189
(Some (su,sr,st), Some (tu,tr,tt)) \<Rightarrow>
+ − 1190
permission_check (su,sr,st) (tu,tr,tt) C_process P_sigkill
+ − 1191
| _ \<Rightarrow> False)"
+ − 1192
| "grant s (Ptrace p p') =
+ − 1193
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_proc p')) of
+ − 1194
(Some (su,sr,st), Some (tu,tr,tt)) \<Rightarrow>
+ − 1195
permission_check (su,sr,st) (tu,tr,tt) C_process P_ptrace
+ − 1196
| _ \<Rightarrow> False)"
+ − 1197
| "grant s (Exit p) = True"
+ − 1198
| "grant s (Open p f flags fd None) =
+ − 1199
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_file f)) of
+ − 1200
(Some (pu,pr,pt), Some (fu,fr,ft)) \<Rightarrow>
+ − 1201
search_check s (pu,pr,pt) f \<and>
+ − 1202
oflags_check flags (pu,pr,pt) (fu,fr,ft) \<and>
+ − 1203
permission_check (pu,pr,pt) (pu,pr,pt) C_fd P_setattr
+ − 1204
| _ \<Rightarrow>False)"
+ − 1205
| "grant s (Open p f flags fd (Some inum)) =
+ − 1206
(case (parent f) of
+ − 1207
Some pf \<Rightarrow> (case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_dir pf)) of
+ − 1208
(Some (pu,pr,pt), Some (pfu,pfr,pft)) \<Rightarrow>
+ − 1209
(search_check s (pu,pr,pt) pf \<and>
+ − 1210
oflags_check flags (pu,pr,pt) (nfctxt_create (pu,pr,pt) (pfu,pfr,pft) C_file) \<and>
+ − 1211
permission_check (pu,pr,pt) (pfu,pfr,pft) C_dir P_add_name \<and>
+ − 1212
permission_check (pu,pr,pt) (pu,pr,pt) C_fd P_setattr)
+ − 1213
| _ \<Rightarrow> False)
+ − 1214
| _ \<Rightarrow> False)"
+ − 1215
| "grant s (ReadFile p fd) =
+ − 1216
(case (file_of_proc_fd s p fd) of
+ − 1217
Some f \<Rightarrow>
+ − 1218
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_fd p fd),
+ − 1219
sectxt_of_obj s (O_file f)) of
+ − 1220
(Some (pu,pr,pt), Some (fdu,fdr,fdt), Some (fu,fr,ft)) \<Rightarrow>
+ − 1221
(permission_check (pu,pr,pt) (fdu,fdr,fdt) C_fd P_setattr \<and>
+ − 1222
permission_check (pu,pr,pt) (fu,fr,ft) C_file P_read)
+ − 1223
| _ \<Rightarrow> False)
+ − 1224
| _ \<Rightarrow> False)"
+ − 1225
| "grant s (WriteFile p fd) =
+ − 1226
(case (file_of_proc_fd s p fd) of
+ − 1227
Some f \<Rightarrow>
+ − 1228
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_fd p fd),
+ − 1229
sectxt_of_obj s (O_file f)) of
+ − 1230
(Some (pu,pr,pt), Some (fdu,fdr,fdt), Some (fu,fr,ft)) \<Rightarrow>
+ − 1231
(permission_check (pu,pr,pt) (fdu,fdr,fdt) C_fd P_setattr \<and>
+ − 1232
permission_check (pu,pr,pt) (fu,fr,ft) C_file P_write)
+ − 1233
| _ \<Rightarrow> False)
+ − 1234
| _ \<Rightarrow> False)"
+ − 1235
| "grant s (CloseFd p fd) = True"
+ − 1236
| "grant s (UnLink p f) =
+ − 1237
(case (parent f) of
+ − 1238
Some pf \<Rightarrow>
+ − 1239
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_dir pf),
+ − 1240
sectxt_of_obj s (O_file f)) of
+ − 1241
(Some (pu,pr,pt), Some (du,dr,dt), Some (fu,fr,ft)) \<Rightarrow>
+ − 1242
(search_check s (pu,pr,pt) f \<and>
+ − 1243
permission_check (pu,pr,pt) (fu,fr,ft) C_file P_unlink \<and>
+ − 1244
permission_check (pu,pr,pt) (du,dr,dt) C_dir P_remove_name)
+ − 1245
| _ \<Rightarrow> False)
+ − 1246
| _ \<Rightarrow> False)"
+ − 1247
| "grant s (Rmdir p f) =
+ − 1248
(case (parent f) of
+ − 1249
Some pf \<Rightarrow>
+ − 1250
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_dir pf),
+ − 1251
sectxt_of_obj s (O_dir f)) of
+ − 1252
(Some (pu,pr,pt), Some (du,dr,dt), Some (fu,fr,ft)) \<Rightarrow>
+ − 1253
(search_check s (pu,pr,pt) f \<and>
+ − 1254
permission_check (pu,pr,pt) (fu,fr,ft) C_dir P_rmdir \<and>
+ − 1255
permission_check (pu,pr,pt) (du,dr,dt) C_dir P_remove_name)
+ − 1256
| _ \<Rightarrow> False)
+ − 1257
| _ \<Rightarrow> False)"
+ − 1258
| "grant s (Mkdir p f inum) =
+ − 1259
(case (parent f) of
+ − 1260
Some pf \<Rightarrow>
+ − 1261
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_dir pf)) of
+ − 1262
(Some (pu,pr,pt), Some (du,dr,dt)) \<Rightarrow>
+ − 1263
(search_check s (pu,pr,pt) f \<and>
+ − 1264
permission_check (pu,pr,pt) (nfctxt_create (pu,pr,pt) (du,dr,dt) C_dir) C_dir P_create \<and>
+ − 1265
permission_check (pu,pr,pt) (du,dr,dt) C_dir P_add_name)
+ − 1266
| _ \<Rightarrow> False)
+ − 1267
| _ \<Rightarrow> False)"
+ − 1268
| "grant s (LinkHard p f f') =
+ − 1269
(case (parent f') of
+ − 1270
Some pf \<Rightarrow>
+ − 1271
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_dir pf),
+ − 1272
sectxt_of_obj s (O_file f)) of
+ − 1273
(Some (pu,pr,pt), Some (du,dr,dt), Some (fu,fr,ft)) \<Rightarrow>
+ − 1274
(search_check s (pu,pr,pt) f \<and> search_check s (pu,pr,pt) pf \<and>
+ − 1275
permission_check (pu,pr,pt) (fu,fr,ft) C_file P_link \<and>
+ − 1276
permission_check (pu,pr,pt) (du,dr,dt) C_dir P_add_name)
+ − 1277
| _ \<Rightarrow> False)
+ − 1278
| _ \<Rightarrow> False)"
+ − 1279
| "grant s (Truncate p f len) =
+ − 1280
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_file f)) of
+ − 1281
(Some (pu,pr,pt), Some (fu,fr,ft)) \<Rightarrow>
+ − 1282
(search_check s (pu,pr,pt) f \<and>
+ − 1283
permission_check (pu,pr,pt) (fu,fr,ft) C_file P_setattr)
+ − 1284
| _ \<Rightarrow> False)"
+ − 1285
(*
+ − 1286
| "grant s (Rename p f f') =
+ − 1287
(case (parent f, parent f') of
+ − 1288
(Some pf, Some pf') \<Rightarrow>
+ − 1289
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_dir pf),
+ − 1290
sectxt_of_obj s (O_file f), sectxt_of_obj s (O_dir pf')) of
+ − 1291
(Some (pu,pr,pt), Some (pfu,pfr,pft), Some (fu,fr,ft),
+ − 1292
Some (pfu',pfr',pft')) \<Rightarrow>
+ − 1293
(search_check s (pu,pr,pt) f \<and>
+ − 1294
permission_check (pu,pr,pt) (pfu,pfr,pft) C_dir P_remove_name \<and>
+ − 1295
(if (is_file s f)
+ − 1296
then permission_check (pu,pr,pt) (fu,fr,ft) C_file P_rename
+ − 1297
else permission_check (pu,pr,pt) (fu,fr,ft) C_dir P_reparent) \<and>
+ − 1298
search_check s (pu,pr,pt) pf' \<and>
+ − 1299
permission_check (pu,pr,pt) (pfu',pfr',pft') C_dir P_add_name)
+ − 1300
| _ \<Rightarrow> False)
+ − 1301
| _ \<Rightarrow> False)"
+ − 1302
*)
+ − 1303
| "grant s (CreateMsgq p q) =
+ − 1304
(case (sectxt_of_obj s (O_proc p)) of
+ − 1305
Some (pu,pr,pt) \<Rightarrow>
+ − 1306
(permission_check (pu,pr,pt) (pu,pr,pt) C_msgq P_associate \<and>
+ − 1307
permission_check (pu,pr,pt) (pu,pr,pt) C_msgq P_create)
+ − 1308
| _ \<Rightarrow> False)"
+ − 1309
| "grant s (SendMsg p q m) =
+ − 1310
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_msgq q)) of
+ − 1311
(Some (pu,pr,pt), Some (qu,qr,qt)) \<Rightarrow>
+ − 1312
(permission_check (pu,pr,pt) (qu,qr,qt) C_msgq P_enqueue \<and>
+ − 1313
permission_check (pu,pr,pt) (qu,qr,qt) C_msgq P_write \<and>
+ − 1314
permission_check (pu,pr,pt) (pu,pr,pt) C_msg P_send)
+ − 1315
| _ \<Rightarrow> False)"
+ − 1316
| "grant s (RecvMsg p q m) =
+ − 1317
(case (sectxt_of_obj s (O_proc p),sectxt_of_obj s (O_msgq q),sectxt_of_obj s (O_msg q m)) of
+ − 1318
(Some (pu,pr,pt), Some (qu,qr,qt), Some (mu,mr,mt)) \<Rightarrow>
+ − 1319
permission_check (pu,pr,pt) (qu,qr,qt) C_msgq P_read \<and>
+ − 1320
permission_check (pu,pr,pt) (mu,mr,mt) C_msg P_receive
+ − 1321
| _ \<Rightarrow> False)"
+ − 1322
| "grant s (RemoveMsgq p q) =
+ − 1323
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_msgq q)) of
+ − 1324
(Some (pu,pr,pt), Some (qu,qr,qt)) \<Rightarrow>
+ − 1325
permission_check (pu,pr,pt) (qu,qr,qt) C_msgq P_destroy
+ − 1326
| _ \<Rightarrow> False)"
+ − 1327
| "grant s (CreateShM p h) =
+ − 1328
(case (sectxt_of_obj s (O_proc p)) of
+ − 1329
Some (pu,pr,pt) \<Rightarrow>
+ − 1330
(permission_check (pu,pr,pt) (pu,pr,pt) C_shm P_associate \<and>
+ − 1331
permission_check (pu,pr,pt) (pu,pr,pt) C_shm P_create)
+ − 1332
| _ \<Rightarrow> False)"
+ − 1333
| "grant s (Attach p h flag) =
+ − 1334
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_shm h)) of
+ − 1335
(Some (pu,pr,pt), Some (hu,hr,ht)) \<Rightarrow>
+ − 1336
if flag = SHM_RDONLY
+ − 1337
then permission_check (pu,pr,pt) (hu,hr,ht) C_shm P_read
+ − 1338
else permission_check (pu,pr,pt) (hu,hr,ht) C_shm P_read \<and>
+ − 1339
permission_check (pu,pr,pt) (hu,hr,ht) C_shm P_write
+ − 1340
| _ \<Rightarrow> False)"
+ − 1341
| "grant s (Detach p h) = True" (*?*)
+ − 1342
| "grant s (DeleteShM p h) =
+ − 1343
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_shm h)) of
+ − 1344
(Some (pu,pr,pt), Some (hu,hr,ht)) \<Rightarrow>
+ − 1345
permission_check (pu,pr,pt) (hu,hr,ht) C_shm P_destroy
+ − 1346
| _ \<Rightarrow> False)"
+ − 1347
+ − 1348
+ − 1349
| "grant s _ = False" (* socket event is currently not in consideration *)
+ − 1350
+ − 1351
inductive valid :: "t_state \<Rightarrow> bool"
+ − 1352
where
+ − 1353
"valid []"
+ − 1354
| "\<lbrakk>valid s; os_grant s e; grant s e\<rbrakk> \<Longrightarrow> valid (e # s)"
+ − 1355
+ − 1356
(* tobj: object that can be tainted *)
+ − 1357
fun tobj_in_init :: "t_object \<Rightarrow> bool"
+ − 1358
where
+ − 1359
"tobj_in_init (O_proc p) = (p \<in> init_procs)"
+ − 1360
| "tobj_in_init (O_file f) = (f \<in> init_files)"
+ − 1361
(* directory is not taintable
+ − 1362
| "tobj_in_init (O_dir f) = (f \<in> init_files)"
+ − 1363
*)
+ − 1364
| "tobj_in_init (O_node n) = (n \<in> init_nodes)"
+ − 1365
| "tobj_in_init (O_msg q m) = (member (init_msgs_of_queue q) ((op =) m))"
+ − 1366
| "tobj_in_init _ = False" (* other kind of obj cannot be tainted *)
+ − 1367
+ − 1368
(* no use
+ − 1369
fun no_del_event:: "t_event list \<Rightarrow> bool"
+ − 1370
where
+ − 1371
"no_del_event [] = True"
+ − 1372
| "no_del_event (Kill p p' # \<tau>) = False"
+ − 1373
| "no_del_event (CloseFd p fd # \<tau>) = False"
+ − 1374
| "no_del_event (UnLink p f # \<tau>) = False"
+ − 1375
| "no_del_event (Rmdir p f # \<tau>) = False"
+ − 1376
(*
+ − 1377
| "no_del_event (Rename p f f' # \<tau>) = False"
+ − 1378
*)
+ − 1379
| "no_del_event (RemoveMsgq p q # \<tau>) = False"
+ − 1380
| "no_del_event (RecvMsg p q m # \<tau>) = False"
+ − 1381
| "no_del_event (_ # \<tau>) = no_del_event \<tau>"
+ − 1382
*)
+ − 1383
+ − 1384
end
+ − 1385
+ − 1386
locale tainting = flask +
+ − 1387
+ − 1388
fixes seeds :: "t_object set"
+ − 1389
assumes
+ − 1390
seeds_in_init: "\<And> obj. obj \<in> seeds \<Longrightarrow> tobj_in_init obj"
+ − 1391
+ − 1392
begin
+ − 1393
+ − 1394
inductive tainted :: "t_object \<Rightarrow> t_state \<Rightarrow> bool" ("_ \<in> tainted _" [100, 100] 100)
+ − 1395
where
+ − 1396
t_init: "obj \<in> seeds \<Longrightarrow> obj \<in> tainted []"
+ − 1397
| t_clone: "\<lbrakk>O_proc p \<in> tainted s; valid (Clone p p' fds shms # s)\<rbrakk>
+ − 1398
\<Longrightarrow> O_proc p' \<in> tainted (Clone p p' fds shms # s)"
+ − 1399
| t_execve: "\<lbrakk>O_file f \<in> tainted s; valid (Execve p f fds # s)\<rbrakk>
+ − 1400
\<Longrightarrow> O_proc p \<in> tainted (Execve p f fds # s)"
+ − 1401
| t_ptrace: "\<lbrakk>O_proc p \<in> tainted s; valid (Ptrace p p' # s); info_flow_shm s p' p''\<rbrakk>
+ − 1402
\<Longrightarrow> O_proc p'' \<in> tainted (Ptrace p p' # s)"
+ − 1403
| t_ptrace':"\<lbrakk>O_proc p' \<in> tainted s; valid (Ptrace p p' # s); info_flow_shm s p p''\<rbrakk>
+ − 1404
\<Longrightarrow> O_proc p'' \<in> tainted (Ptrace p p' # s)"
+ − 1405
| t_cfile: "\<lbrakk>O_proc p \<in> tainted s; valid (Open p f flags fd (Some inum) # s)\<rbrakk>
+ − 1406
\<Longrightarrow> O_file f \<in> tainted (Open p f flags fd (Some inum) # s)"
+ − 1407
| t_read: "\<lbrakk>O_file f \<in> tainted s; valid (ReadFile p fd # s);
+ − 1408
file_of_proc_fd s p fd = Some f; info_flow_shm s p p'\<rbrakk>
+ − 1409
\<Longrightarrow> O_proc p' \<in> tainted (ReadFile p fd # s)"
+ − 1410
| t_write: "\<lbrakk>O_proc p \<in> tainted s; valid (WriteFile p fd # s);
+ − 1411
file_of_proc_fd s p fd = Some f; has_same_inode s f f'\<rbrakk>
+ − 1412
\<Longrightarrow> O_file f' \<in> tainted (WriteFile p fd # s)"
+ − 1413
(* directory is not tainted
+ − 1414
| t_mkdir: "\<lbrakk>O_proc p \<in> tainted s; valid (Mkdir p f inum # s)\<rbrakk>
+ − 1415
\<Longrightarrow> O_dir f \<in> tainted (Mkdir p f inum # s)"
+ − 1416
*)
+ − 1417
| t_link: "\<lbrakk>O_file f \<in> tainted s; valid (LinkHard p f f' # s)\<rbrakk>
+ − 1418
\<Longrightarrow> O_file f' \<in> tainted (LinkHard p f f' # s)"
+ − 1419
| t_trunc: "\<lbrakk>O_proc p \<in> tainted s; valid (Truncate p f len # s); len > 0; has_same_inode s f f'\<rbrakk>
+ − 1420
\<Longrightarrow> O_file f' \<in> tainted (Truncate p f len # s)"
+ − 1421
(*
+ − 1422
| t_rename: "\<lbrakk>O_file f'' \<in> tainted s; valid (Rename p f f' # s);f \<preceq> f''\<rbrakk>
+ − 1423
\<Longrightarrow> O_file (file_after_rename f f' f'') \<in> tainted (Rename p f f' # s)"
+ − 1424
| t_rename':"\<lbrakk>O_dir f'' \<in> tainted s; valid (Rename p f f' # s);f \<preceq> f''\<rbrakk>
+ − 1425
\<Longrightarrow> O_dir (file_after_rename f f' f'') \<in> tainted (Rename p f f' # s)"
+ − 1426
*)
+ − 1427
| t_sendmsg:"\<lbrakk>O_proc p \<in> tainted s; valid (SendMsg p q m # s)\<rbrakk>
+ − 1428
\<Longrightarrow> O_msg q m \<in> tainted (SendMsg p q m # s)"
+ − 1429
| t_recvmsg:"\<lbrakk>O_msg q m \<in> tainted s; valid (RecvMsg p q m # s); info_flow_shm s p p'\<rbrakk>
+ − 1430
\<Longrightarrow> O_proc p' \<in> tainted (RecvMsg p q m # s)"
+ − 1431
| t_remain: "\<lbrakk>obj \<in> tainted s; valid (e # s); alive (e # s) obj\<rbrakk>
+ − 1432
\<Longrightarrow> obj \<in> tainted (e # s)"
+ − 1433
+ − 1434
definition taintable:: "t_object \<Rightarrow> bool"
+ − 1435
where
+ − 1436
"taintable obj \<equiv> init_alive obj \<and> (\<exists> s. obj \<in> tainted s)"
+ − 1437
+ − 1438
definition undeletable :: "t_object \<Rightarrow> bool"
+ − 1439
where
+ − 1440
"undeletable obj \<equiv> init_alive obj \<and> \<not> (\<exists> s. valid s \<and> deleted obj s)"
+ − 1441
+ − 1442
end
+ − 1443
+ − 1444
end