author | chunhan |
Thu, 30 May 2013 09:15:19 +0800 | |
changeset 13 | 7b5e9fbeaf93 |
parent 12 | 47a4b2ae0556 |
child 14 | cc1e46225a81 |
permissions | -rwxr-xr-x |
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
parents:
4
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
parents:
4
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>) = |
|
13 | 437 |
(procs_of_shm \<tau>) (h := (procs_of_shm \<tau> h) - {(p,flag) | flag. (p, flag) \<in> procs_of_shm \<tau> h})" |
1 | 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) |
|
13 | 441 |
then (procs_of_shm \<tau> h) \<union> {(p', flag) | flag. (p, flag) \<in> procs_of_shm \<tau> h} |
1 | 442 |
else procs_of_shm \<tau> h)" |
443 |
| "procs_of_shm (Execve p f fds # \<tau>) = |
|
13 | 444 |
(\<lambda> h. procs_of_shm \<tau> h - {(p, flag) | flag. (p, flag) \<in> procs_of_shm \<tau> h})" |
445 |
| "procs_of_shm (Kill p p' # s) = |
|
446 |
(\<lambda> h. (procs_of_shm s h) - {(p', flag) | flag. (p', flag) \<in> procs_of_shm s h})" |
|
447 |
| "procs_of_shm (Exit p # s) = |
|
448 |
(\<lambda> h. (procs_of_shm s h) - {(p, flag) | flag. (p, flag) \<in> procs_of_shm s h})" |
|
449 |
| "procs_of_shm (e # \<tau>) = procs_of_shm \<tau>" |
|
1 | 450 |
|
451 |
definition info_flow_shm :: "t_state \<Rightarrow> t_process \<Rightarrow> t_process \<Rightarrow> bool" |
|
452 |
where |
|
453 |
"info_flow_shm \<tau> from to \<equiv> (from = to) \<or> (\<exists> h \<in> current_shms \<tau>. \<exists> toflag. |
|
454 |
(((from, SHM_RDWR) \<in> procs_of_shm \<tau> h) \<and> ((to, toflag) \<in> procs_of_shm \<tau> h)))" |
|
455 |
||
456 |
fun current_msgqs :: "t_state \<Rightarrow> t_msg set" |
|
457 |
where |
|
458 |
"current_msgqs [] = init_msgqs" |
|
459 |
| "current_msgqs (CreateMsgq p q # \<tau>) = insert q (current_msgqs \<tau>)" |
|
460 |
| "current_msgqs (RemoveMsgq p q # \<tau>) = (current_msgqs \<tau>) - {q}" |
|
461 |
| "current_msgqs (_ # \<tau>) = current_msgqs \<tau>" |
|
462 |
||
463 |
fun msgs_of_queue :: "t_state \<Rightarrow> t_msgq \<Rightarrow> t_msg list" |
|
464 |
where |
|
465 |
"msgs_of_queue [] = init_msgs_of_queue" |
|
466 |
| "msgs_of_queue (CreateMsgq p q # \<tau>) = (msgs_of_queue \<tau>)(q := [])" |
|
467 |
| "msgs_of_queue (SendMsg p q m # \<tau>) = (msgs_of_queue \<tau>)(q := msgs_of_queue \<tau> q @ [m])" |
|
468 |
| "msgs_of_queue (RecvMsg p q m # \<tau>) = (msgs_of_queue \<tau>)(q := tl (msgs_of_queue \<tau> q))" |
|
469 |
| "msgs_of_queue (RemoveMsgq p q # \<tau>) = (msgs_of_queue \<tau>)(q := [])" |
|
470 |
| "msgs_of_queue (_ # \<tau>) = msgs_of_queue \<tau>" |
|
471 |
||
472 |
definition current_file_inums :: "t_state \<Rightarrow> t_inode_num set" |
|
473 |
where |
|
474 |
"current_file_inums \<tau> \<equiv> {im. \<exists> f. inum_of_file \<tau> f = Some im}" |
|
475 |
||
476 |
definition current_sock_inums :: "t_state \<Rightarrow> t_inode_num set" |
|
477 |
where |
|
478 |
"current_sock_inums \<tau> = {im. \<exists> s. inum_of_socket \<tau> s = Some im}" |
|
479 |
||
480 |
definition current_inode_nums :: "t_state \<Rightarrow> nat set" |
|
481 |
where |
|
482 |
"current_inode_nums \<tau> = current_file_inums \<tau> \<union> current_sock_inums \<tau>" |
|
483 |
||
2 | 484 |
definition same_inode_files :: "t_state \<Rightarrow> t_file \<Rightarrow> t_file set" |
485 |
where |
|
6
8779d321cc2e
remove duplicated pre-condition of current_files in OS_grant, since it is property if is_file/is_dir.
chunhan
parents:
4
diff
changeset
|
486 |
"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 | 487 |
|
1 | 488 |
fun flags_of_proc_fd :: "t_state \<Rightarrow> t_process \<Rightarrow> t_fd \<Rightarrow> t_open_flags option" |
489 |
where |
|
490 |
"flags_of_proc_fd [] p fd = init_oflags_of_proc_fd p fd" |
|
491 |
| "flags_of_proc_fd (Open p f flags fd iopt # \<tau>) p' fd' = |
|
492 |
(if (p = p' \<and> fd = fd') then Some flags else flags_of_proc_fd \<tau> p' fd')" |
|
493 |
| "flags_of_proc_fd (CloseFd p fd # \<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 (CreateSock p af st fd ino # \<tau>) p' fd' = |
|
496 |
(if (p = p' \<and> fd = fd') then None else flags_of_proc_fd \<tau> p' fd')" |
|
497 |
| "flags_of_proc_fd (Accept p fd addr lport' fd' ino # \<tau>) p' fd'' = |
|
498 |
(if (p = p' \<and> fd' = fd'') then None else flags_of_proc_fd \<tau> p' fd'')" |
|
499 |
| "flags_of_proc_fd (Clone p p' fds shms # \<tau>) p'' fd = |
|
500 |
(if (p' = p'' \<and> fd \<in> fds) then flags_of_proc_fd \<tau> p fd else flags_of_proc_fd \<tau> p'' fd)" |
|
501 |
| "flags_of_proc_fd (Execve p f fds # \<tau>) p' fd = |
|
502 |
(if (p' = p \<and> fd \<in> fds) then flags_of_proc_fd \<tau> p fd else flags_of_proc_fd \<tau> p' fd)" |
|
503 |
| "flags_of_proc_fd (Kill p\<^isub>1 p\<^isub>2 # \<tau>) p fd = |
|
504 |
(if (p = p\<^isub>2) then None else flags_of_proc_fd \<tau> p fd)" |
|
505 |
| "flags_of_proc_fd (Exit p # \<tau>) p' fd' = |
|
506 |
(if (p = p') then None else flags_of_proc_fd \<tau> p' fd')" |
|
507 |
| "flags_of_proc_fd (e # \<tau>) p fd = flags_of_proc_fd \<tau> p fd" |
|
508 |
||
509 |
(* |
|
510 |
fun file_at_writing_by:: "t_state \<Rightarrow> t_file \<Rightarrow> (t_process \<times> t_fd) set" |
|
511 |
where |
|
512 |
"file_at_writing_by [] f = init_file_at_writing_by f" |
|
513 |
| "file_at_writing_by (Open p f flags fd (Some inum) # \<tau>) f' = ( |
|
514 |
if (f' = f) |
|
515 |
then ( if (is_write_flag flags) |
|
516 |
then {(p, fd)} |
|
517 |
else {} ) |
|
518 |
else file_at_writing_by \<tau> f' )" |
|
519 |
| "file_at_writing_by (Open p f flags fd None # \<tau>) f' = ( |
|
520 |
if (f' = f \<and> is_write_flag flags) |
|
521 |
then insert (p, fd) (file_at_writing_by \<tau> f) |
|
522 |
else file_at_writing_by \<tau> f' )" |
|
523 |
| "file_at_writing_by (Mkdir p f inum # \<tau>) f' = |
|
524 |
(if (f' = f) then {} else file_at_writing_by \<tau> f')" |
|
525 |
| "file_at_writing_by (LinkHard p f f' # \<tau>) f'' = |
|
526 |
(if (f'' = f') then file_at_writing_by \<tau> f else file_at_writing_by \<tau> f'')" |
|
527 |
| "file_at_writing_by (Rename p f\<^isub>2 f\<^isub>3 # \<tau>) f = ( |
|
528 |
if (f\<^isub>3 \<preceq> f) then file_at_writing_by \<tau> (file_before_rename f\<^isub>2 f\<^isub>3 f) |
|
529 |
else file_at_writing_by \<tau> f )" |
|
530 |
| "file_at_writing_by (CloseFd p fd # \<tau>) f = |
|
531 |
(file_at_writing_by \<tau> f - {(p, fd)})" |
|
532 |
| "file_at_writing_by (Clone p p' fds shms # \<tau>) f = |
|
533 |
(file_at_writing_by \<tau> f) \<union> {(p',fd)| fd. fd \<in> fds \<and> (p, fd) \<in> file_at_writing_by \<tau> f}" |
|
534 |
| "file_at_writing_by (Execve p f fds # \<tau>) f' = |
|
535 |
(if (f' = f) then {} else file_at_writing_by \<tau> f')" |
|
536 |
| "file_at_writing_by (Exit p # \<tau>) f = |
|
537 |
(file_at_writing_by \<tau> f - {(p, fd)| fd. (p, fd) \<in> file_at_writing_by \<tau> f})" |
|
538 |
| "file_at_writing_by (Kill p p' # \<tau>) f = |
|
539 |
(file_at_writing_by \<tau> f - {(p', fd)| fd. (p', fd) \<in> file_at_writing_by \<tau> f})" |
|
540 |
| "file_at_writing_by (e # \<tau>) f = file_at_writing_by \<tau> f" |
|
541 |
*) |
|
542 |
||
543 |
definition current_users :: "t_state \<Rightarrow> user_t set" |
|
544 |
where |
|
545 |
"current_users \<tau> \<equiv> init_users" |
|
546 |
||
547 |
fun update_with_shuthow :: "t_sock_state option \<Rightarrow> t_shutdown_how \<Rightarrow> t_sock_state option" |
|
548 |
where |
|
549 |
"update_with_shuthow (Some (SS_trans Trans_Recv)) SHUT_RD = Some (SS_trans Trans_No)" |
|
550 |
| "update_with_shuthow (Some (SS_trans Trans_RS)) SHUT_RD = Some (SS_trans Trans_Send)" |
|
551 |
| "update_with_shuthow (Some (SS_trans Trans_Send)) SHUT_RD = Some (SS_trans Trans_Send)" |
|
552 |
| "update_with_shuthow (Some (SS_trans Trans_No)) SHUT_RD = Some (SS_trans Trans_No)" |
|
553 |
| "update_with_shuthow (Some (SS_trans Trans_Recv)) SHUT_WR = Some (SS_trans Trans_Recv)" |
|
554 |
| "update_with_shuthow (Some (SS_trans Trans_RS)) SHUT_WR = Some (SS_trans Trans_Recv)" |
|
555 |
| "update_with_shuthow (Some (SS_trans Trans_Send)) SHUT_WR = Some (SS_trans Trans_No)" |
|
556 |
| "update_with_shuthow (Some (SS_trans Trans_No)) SHUT_WR = Some (SS_trans Trans_No)" |
|
557 |
| "update_with_shuthow (Some (SS_trans Trans_Recv)) SHUT_RDWR = Some (SS_trans Trans_No)" |
|
558 |
| "update_with_shuthow (Some (SS_trans Trans_RS)) SHUT_RDWR = Some (SS_trans Trans_No)" |
|
559 |
| "update_with_shuthow (Some (SS_trans Trans_Send)) SHUT_RDWR = Some (SS_trans Trans_No)" |
|
560 |
| "update_with_shuthow (Some (SS_trans Trans_No)) SHUT_RDWR = Some (SS_trans Trans_No)" |
|
561 |
| "update_with_shuthow opt1 how = None" |
|
562 |
||
563 |
fun socket_state :: "t_state \<Rightarrow> t_socket \<Rightarrow> t_sock_state option" |
|
564 |
where |
|
565 |
"socket_state [] = init_socket_state" |
|
566 |
| "socket_state (CloseFd p fd # \<tau>) = |
|
567 |
(socket_state \<tau>) ((p, fd):= None)" |
|
568 |
| "socket_state (CreateSock p af st fd ino # \<tau>) = |
|
569 |
(socket_state \<tau>) ((p, fd) := Some SS_create)" |
|
570 |
| "socket_state (Bind p fd addr # \<tau>) = |
|
571 |
(socket_state \<tau>) ((p, fd) := Some SS_bind)" |
|
572 |
| "socket_state (Connect p fd addr # \<tau>) = |
|
573 |
(socket_state \<tau>) ((p, fd) := Some (SS_trans Trans_RS))" |
|
574 |
| "socket_state (Listen p fd # \<tau>) = |
|
575 |
(socket_state \<tau>) ((p, fd) := Some SS_listen)" |
|
576 |
| "socket_state (Accept p fd addr lport' fd' ino # \<tau>) = |
|
577 |
(socket_state \<tau>) ((p, fd') := Some (SS_trans Trans_RS))" |
|
578 |
| "socket_state (Shutdown p fd sh # \<tau>) = |
|
579 |
(socket_state \<tau>) ((p, fd) := update_with_shuthow (socket_state \<tau> (p, fd)) sh)" |
|
580 |
| "socket_state (Clone p p' fds shms # \<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 (Execve p f fds # \<tau>) = |
|
585 |
(\<lambda> (p', fd). if (p' = p \<and> fd \<in> fds) then socket_state \<tau> (p, fd) |
|
586 |
else if (p' = p) then None |
|
587 |
else socket_state \<tau> (p', fd))" |
|
588 |
| "socket_state (Kill p\<^isub>1 p\<^isub>2 # \<tau>) = |
|
589 |
(\<lambda> (p, fd). if (p = p\<^isub>2) then None else socket_state \<tau> (p, fd))" |
|
590 |
| "socket_state (Exit p # \<tau>) = |
|
591 |
(\<lambda> (p', fd'). if (p = p') then None else socket_state \<tau> (p', fd'))" |
|
592 |
| "socket_state (e # \<tau>) = socket_state \<tau>" |
|
593 |
||
594 |
definition socket_in_trans :: "t_state \<Rightarrow> t_socket \<Rightarrow> bool" |
|
595 |
where |
|
596 |
"socket_in_trans \<tau> s \<equiv> (case (socket_state \<tau> s) of |
|
597 |
Some st \<Rightarrow> (case st of |
|
598 |
SS_trans para \<Rightarrow> True |
|
599 |
| _ \<Rightarrow> False) |
|
600 |
| _ \<Rightarrow> False)" |
|
601 |
||
602 |
definition socket_in_sending :: "t_state \<Rightarrow> t_socket \<Rightarrow> bool" |
|
603 |
where |
|
604 |
"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))" |
|
605 |
||
606 |
definition socket_in_recving :: "t_state \<Rightarrow> t_socket \<Rightarrow> bool" |
|
607 |
where |
|
608 |
"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))" |
|
609 |
||
610 |
||
611 |
(******* admissable check by the OS *******) |
|
612 |
fun os_grant :: "t_state \<Rightarrow> t_event \<Rightarrow> bool" |
|
613 |
where |
|
614 |
"os_grant \<tau> (Open p f flags fd inumopt) = ( |
|
615 |
case inumopt of |
|
616 |
Some ino \<Rightarrow> |
|
617 |
(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
parents:
4
diff
changeset
|
618 |
(\<exists> pf. parent f = Some pf \<and> is_dir \<tau> pf \<and> pf \<notin> files_hung_by_del \<tau>) \<and> |
1 | 619 |
is_creat_flag flags \<and> fd \<notin> (current_proc_fds \<tau> p) \<and> ino \<notin> (current_inode_nums \<tau>)) |
620 |
| None \<Rightarrow> |
|
6
8779d321cc2e
remove duplicated pre-condition of current_files in OS_grant, since it is property if is_file/is_dir.
chunhan
parents:
4
diff
changeset
|
621 |
(p \<in> current_procs \<tau> \<and> \<not> is_creat_excl_flag flags \<and> is_file \<tau> f \<and> |
1 | 622 |
fd \<notin> (current_proc_fds \<tau> p)) )" |
623 |
(*(if (is_dir \<tau> f) then (is_dir_flag flags \<and> \<not> is_write_flag flags) else (\<not> is_dir_flag flags)), |
|
624 |
here open is not applied to directories, readdir is for that, but it is not security-related *) |
|
625 |
| "os_grant \<tau> (CloseFd p fd) = |
|
626 |
(p \<in> current_procs \<tau> \<and> fd \<in> current_proc_fds \<tau> p)" |
|
627 |
| "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
parents:
4
diff
changeset
|
628 |
(p \<in> current_procs \<tau> \<and> is_file \<tau> f \<and> f \<notin> files_hung_by_del \<tau>)" |
1 | 629 |
| "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
parents:
4
diff
changeset
|
630 |
(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 | 631 |
| "os_grant \<tau> (Mkdir p f ino) = |
632 |
(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
parents:
4
diff
changeset
|
633 |
(\<exists> pf. parent f = Some pf \<and> is_dir \<tau> pf \<and> pf \<notin> files_hung_by_del \<tau>) \<and> |
1 | 634 |
ino \<notin> (current_inode_nums \<tau>))" |
635 |
| "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
parents:
4
diff
changeset
|
636 |
(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
parents:
4
diff
changeset
|
637 |
(\<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 | 638 |
| "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
parents:
4
diff
changeset
|
639 |
(p \<in> current_procs \<tau> \<and> is_file \<tau> f)" |
1 | 640 |
(* |
641 |
| "os_grant \<tau> (FTruncate p fd len) = |
|
642 |
(p \<in> current_procs \<tau> \<and> fd \<in> current_proc_fds \<tau> p \<and> |
|
643 |
(\<exists> f. file_of_proc_fd \<tau> p fd = Some f \<and> f \<in> current_files \<tau> \<and> is_file \<tau> f) \<and> |
|
644 |
(\<exists> flags. flags_of_proc_fd \<tau> p fd = Some flags \<and> is_write_flag flags))" |
|
645 |
*) |
|
646 |
| "os_grant \<tau> (ReadFile 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_read_flag flags))" |
|
650 |
| "os_grant \<tau> (WriteFile p fd) = |
|
651 |
(p \<in> current_procs \<tau> \<and> fd \<in> current_proc_fds \<tau> p \<and> |
|
652 |
(\<exists> f. file_of_proc_fd \<tau> p fd = Some f \<and> f \<in> current_files \<tau>) \<and> |
|
653 |
(\<exists> flags. flags_of_proc_fd \<tau> p fd = Some flags \<and> is_write_flag flags))" |
|
654 |
| "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
parents:
4
diff
changeset
|
655 |
(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 | 656 |
(* |
657 |
| "os_grant \<tau> (Rename p f\<^isub>2 f\<^isub>3) = |
|
658 |
(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> |
|
659 |
(\<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> |
|
660 |
pf\<^isub>3 \<notin> files_hung_by_del \<tau>) )" |
|
661 |
*) |
|
662 |
| "os_grant \<tau> (CreateMsgq p q) = |
|
663 |
(p \<in> current_procs \<tau> \<and> q \<notin> (current_msgqs \<tau>))" |
|
664 |
| "os_grant \<tau> (SendMsg p q m) = |
|
665 |
(p \<in> current_procs \<tau> \<and> q \<in> current_msgqs \<tau> \<and> m \<notin> (set (msgs_of_queue \<tau> q)))" |
|
666 |
| "os_grant \<tau> (RecvMsg p q m) = |
|
667 |
(p \<in> current_procs \<tau> \<and> q \<in> current_msgqs \<tau> \<and> m = hd (msgs_of_queue \<tau> q))" |
|
668 |
| "os_grant \<tau> (RemoveMsgq p q) = |
|
669 |
(p \<in> current_procs \<tau> \<and> q \<in> current_msgqs \<tau>)" |
|
670 |
| "os_grant \<tau> (CreateShM p h) = |
|
671 |
(p \<in> current_procs \<tau> \<and> h \<notin> (current_shms \<tau>))" |
|
672 |
| "os_grant \<tau> (Attach p h flag) = |
|
673 |
(p \<in> current_procs \<tau> \<and> h \<in> current_shms \<tau> \<and> \<not> (\<exists> flag. (p, flag) \<in> procs_of_shm \<tau> h))" |
|
674 |
| "os_grant \<tau> (Detach p h) = |
|
675 |
(p \<in> current_procs \<tau> \<and> h \<in> current_shms \<tau> \<and> (\<exists> flag. (p, flag) \<in> procs_of_shm \<tau> h))" |
|
676 |
| "os_grant \<tau> (DeleteShM p h) = |
|
677 |
(p \<in> current_procs \<tau> \<and> h \<in> current_shms \<tau>)" |
|
678 |
| "os_grant \<tau> (CreateSock p af st fd ino) = |
|
679 |
(p \<in> current_procs \<tau> \<and> af = AF_INET \<and> fd \<notin> (current_proc_fds \<tau> p) \<and> |
|
680 |
ino \<notin> (current_inode_nums \<tau>))" |
|
681 |
| "os_grant \<tau> (Accept p fd addr port fd' ino) = |
|
682 |
(p \<in> current_procs \<tau> \<and> fd \<in> current_proc_fds \<tau> p \<and> (p, fd) \<in> current_sockets \<tau> \<and> |
|
683 |
fd' \<notin> (current_proc_fds \<tau> p) \<and> socket_state \<tau> (p, fd) = Some SS_listen \<and> |
|
684 |
is_tcp_sock \<tau> (p, fd) \<and> ino \<notin> (current_inode_nums \<tau>))" |
|
685 |
| "os_grant \<tau> (Bind p fd addr) = |
|
686 |
(p \<in> current_procs \<tau> \<and> fd \<in> current_proc_fds \<tau> p \<and> (p, fd) \<in> current_sockets \<tau> \<and> |
|
687 |
socket_state \<tau> (p, fd) = Some SS_create)" (* ?? !! (case addr of INET_ADDR ip port \<Rightarrow> ip \<in> local_ipaddrs)) *) |
|
688 |
| "os_grant \<tau> (Connect p fd addr) = |
|
689 |
(p \<in> current_procs \<tau> \<and> fd \<in> current_proc_fds \<tau> p \<and> (p, fd) \<in> current_sockets \<tau> \<and> |
|
690 |
socket_state \<tau> (p, fd) = Some SS_bind)" |
|
691 |
| "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
parents:
4
diff
changeset
|
692 |
(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
parents:
4
diff
changeset
|
693 |
socket_state \<tau> (p, fd) = Some SS_bind)" (* Listen and Accept should only be used for TCP sever side *) |
1 | 694 |
| "os_grant \<tau> (SendSock p fd) = |
695 |
(p \<in> current_procs \<tau> \<and> fd \<in> current_proc_fds \<tau> p \<and> (p, fd) \<in> current_sockets \<tau> \<and> |
|
696 |
socket_in_sending \<tau> (p, fd))" |
|
697 |
| "os_grant \<tau> (RecvSock p fd) = |
|
698 |
(p \<in> current_procs \<tau> \<and> fd \<in> current_proc_fds \<tau> p \<and> (p, fd) \<in> current_sockets \<tau> \<and> |
|
699 |
socket_in_recving \<tau> (p, fd))" |
|
700 |
| "os_grant \<tau> (Shutdown p fd sh) = |
|
701 |
(p \<in> current_procs \<tau> \<and> fd \<in> current_proc_fds \<tau> p \<and> (p, fd) \<in> current_sockets \<tau> \<and> |
|
702 |
socket_in_trans \<tau> (p, fd))" |
|
703 |
(* |
|
704 |
| "os_grant \<tau> (ChangeOwner p u) = (p \<in> current_procs \<tau> \<and> u \<in> current_users \<tau>)" |
|
705 |
*) |
|
706 |
| "os_grant \<tau> (Clone p p' fds shms) = |
|
707 |
(p \<in> current_procs \<tau> \<and> p' \<notin> (current_procs \<tau>) \<and> fds \<subseteq> current_proc_fds \<tau> p \<and> |
|
708 |
(\<forall> h \<in> shms. \<exists> flag. (p, flag) \<in> procs_of_shm \<tau> h))" |
|
709 |
| "os_grant \<tau> (Kill 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 kill itself right? *) |
|
711 |
| "os_grant \<tau> (Exit p) = |
|
712 |
(p \<in> current_procs \<tau>)" |
|
713 |
| "os_grant \<tau> (Ptrace p\<^isub>1 p\<^isub>2) = |
|
714 |
(p\<^isub>1 \<in> current_procs \<tau> \<and> p\<^isub>2 \<in> current_procs \<tau>)" (* a process can trace itself right? *) |
|
715 |
||
716 |
fun alive :: "t_state \<Rightarrow> t_object \<Rightarrow> bool" |
|
717 |
where |
|
718 |
"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
parents:
4
diff
changeset
|
719 |
| "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
parents:
4
diff
changeset
|
720 |
| "alive s (O_dir f) = (is_dir s f)" |
1 | 721 |
| "alive s (O_fd p fd) = (fd \<in> current_proc_fds s p)" |
722 |
| "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
parents:
4
diff
changeset
|
723 |
| "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
parents:
4
diff
changeset
|
724 |
| "alive s (O_udp_sock k) = (is_udp_sock s k)" |
1 | 725 |
| "alive s (O_shm h) = (h \<in> current_shms s)" |
726 |
| "alive s (O_msgq q) = (q \<in> current_msgqs s)" |
|
727 |
| "alive s (O_msg q m) = (m \<in> set (msgs_of_queue s q) \<and> q \<in> current_msgqs s)" |
|
728 |
||
729 |
(* deleted objects should be "taintable" objects, objects like fd should not be in consideration *) |
|
730 |
(* actually, deleted should be renamed as "vanished", cause "exit/closefd" *) |
|
731 |
fun deleted :: "t_object \<Rightarrow> t_event list \<Rightarrow> bool" |
|
732 |
where |
|
733 |
"deleted obj [] = False" |
|
734 |
| "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> |
|
735 |
(\<exists> fd. obj = O_tcp_sock (p', fd) \<and> is_tcp_sock s (p',fd)) \<or> |
|
736 |
(\<exists> fd. obj = O_udp_sock (p', fd) \<and> is_udp_sock s (p',fd)) \<or> |
|
737 |
deleted obj s)" |
|
738 |
| "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 | 739 |
(obj = O_udp_sock (p,fd) \<and> is_udp_sock s (p, fd)) \<or> |
740 |
(\<exists> f. obj = O_file f \<and> proc_fd_of_file s f = {(p, fd)} \<and> |
|
741 |
f \<in> files_hung_by_del s) \<or> deleted obj s)" |
|
1 | 742 |
| "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> |
743 |
(\<exists> fd. obj = O_tcp_sock (p, fd) \<and> is_tcp_sock s (p,fd) \<and> fd \<notin> fds) \<or> |
|
744 |
(\<exists> fd. obj = O_udp_sock (p, fd) \<and> is_udp_sock s (p,fd) \<and> fd \<notin> fds) \<or> |
|
745 |
deleted obj s)" |
|
4 | 746 |
| "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> |
747 |
deleted obj s)" |
|
8 | 748 |
| "deleted obj (UnLink p f # s) = ((proc_fd_of_file s f = {} \<and> obj = O_file f) \<or> deleted obj s)" |
1 | 749 |
| "deleted obj (Rmdir p f # s) = ((obj = O_dir f) \<or> deleted obj s)" |
750 |
| "deleted obj (Exit p # s) = ((obj = O_proc p) \<or> (\<exists> fd \<in> current_proc_fds s p. obj = O_fd p fd) \<or> |
|
751 |
(\<exists> fd. obj = O_tcp_sock (p, fd) \<and> is_tcp_sock s (p,fd)) \<or> |
|
752 |
(\<exists> fd. obj = O_udp_sock (p, fd) \<and> is_udp_sock s (p,fd)) \<or> deleted obj s)" |
|
753 |
(* |
|
754 |
| "deleted obj (Rename p f f' # s) = |
|
755 |
(case obj of |
|
756 |
O_file f'' \<Rightarrow> f \<preceq> f'' \<or> deleted obj s |
|
757 |
| O_dir f'' \<Rightarrow> f \<preceq> f'' \<or> deleted obj s |
|
758 |
| _ \<Rightarrow> deleted obj s)" |
|
759 |
*) |
|
760 |
| "deleted obj (RemoveMsgq p q # s) = (obj = O_msgq q \<or> (\<exists> m. obj = O_msg q m) \<or> deleted obj s)" |
|
761 |
| "deleted obj (DeleteShM p h # s) = (obj = O_shm h \<or> deleted obj s)" |
|
762 |
| "deleted obj (RecvMsg p q m # s) = (obj = O_msg q m \<or> deleted obj s)" |
|
763 |
| "deleted obj (_ # s) = deleted obj s" |
|
764 |
||
765 |
||
12 | 766 |
definition proc_file_fds :: "t_state \<Rightarrow> t_process \<Rightarrow> t_fd set" |
767 |
where |
|
768 |
"proc_file_fds s p \<equiv> {fd. \<exists> f. file_of_proc_fd s p fd = Some f}" |
|
769 |
||
770 |
||
771 |
definition init_proc_file_fds:: "t_process \<Rightarrow> t_fd set" |
|
13 | 772 |
where |
12 | 773 |
"init_proc_file_fds p \<equiv> {fd. \<exists> f. init_file_of_proc_fd p fd = Some f}" |
774 |
||
1 | 775 |
end |
776 |
||
777 |
||
778 |
||
779 |
locale flask = init + |
|
780 |
fixes |
|
781 |
(***************** Policy-specific Parameters *************) |
|
782 |
type_transition_rules :: "type_transition_rule_list_t" |
|
783 |
and allowed_rules :: "allowed_rule_list_t" |
|
784 |
and role_allowed_rules :: "role_allow_rule_list_t" |
|
785 |
and role_declaration_rules :: "role_declarations_list_t" |
|
786 |
and role_transition_rules:: "role_transition_rule_list_t" |
|
787 |
and constrains_rules :: "constrains_list_t" |
|
788 |
||
789 |
and init_type_of_obj :: "t_object \<rightharpoonup> type_t" |
|
790 |
and init_user_of_obj :: "t_object \<rightharpoonup> user_t" |
|
791 |
and init_role_of_proc :: "t_process \<rightharpoonup> role_t" |
|
792 |
||
793 |
assumes |
|
794 |
||
795 |
init_obj_has_type: "\<And> obj. init_alive obj \<Longrightarrow> \<exists> t. init_type_of_obj obj = Some t" |
|
796 |
and init_type_has_obj: "\<And> obj t. init_type_of_obj obj = Some t \<Longrightarrow> init_alive obj" |
|
797 |
and init_obj_has_user: "\<And> obj. init_alive obj \<Longrightarrow> \<exists> u. init_user_of_obj obj = Some u" |
|
798 |
and init_user_has_obj: "\<And> obj u. init_user_of_obj obj = Some u \<Longrightarrow> init_alive obj \<and> u \<in> init_users" |
|
799 |
and init_proc_has_role: "bidirect_in_init init_procs init_role_of_proc" |
|
800 |
||
801 |
begin |
|
802 |
||
803 |
(*********** policy-specified computations, not-event-related ************) |
|
804 |
||
805 |
(* security_transition_sid : type_transition_rules |
|
806 |
* It's a system-call offered by security-server? So access-control-checked too? |
|
807 |
* according to selinux, it deals only with execve and file-creation |
|
808 |
* is_execve: if is the execve case |
|
809 |
* is_file: if is the creation of file/dir |
|
810 |
* change from is_execve to is_file, because the new message by default use |
|
811 |
* the type of the sending process too. |
|
812 |
*) |
|
813 |
||
814 |
definition type_transition :: "type_t \<Rightarrow> type_t \<Rightarrow> security_class_t \<Rightarrow> bool \<Rightarrow> type_t" |
|
815 |
where |
|
816 |
"type_transition st rt tc IS_file \<equiv> ( |
|
817 |
case (lookup type_transition_rules |
|
818 |
(\<lambda> (stp, rtp, tc', dt). patt_match stp st \<and> patt_match rtp rt \<and> tc = tc')) of |
|
819 |
None \<Rightarrow> (if IS_file then rt else st) |
|
820 |
| Some (stp,rtp,tc',dt) \<Rightarrow> dt)" |
|
821 |
||
822 |
(* allowed_rule_list *) |
|
823 |
definition TE_allow :: "type_t \<Rightarrow> type_t \<Rightarrow> security_class_t \<Rightarrow> permission_t \<Rightarrow> bool" |
|
824 |
where |
|
825 |
"TE_allow st tt tc perm \<equiv> member allowed_rules |
|
826 |
(\<lambda> (stp, ttp, tc', pp). tc = tc' \<and> patt_match stp st \<and> patt_match_self ttp tt st \<and> |
|
827 |
patt_match pp perm)" |
|
828 |
||
829 |
(* role_allow_rule_list_t *) |
|
830 |
definition role_transition :: "role_t \<Rightarrow> type_t \<Rightarrow> role_t option" |
|
831 |
where |
|
832 |
"role_transition r t \<equiv> |
|
833 |
(case (lookup role_transition_rules (\<lambda> (pr, ft, nr). pr = r \<and> t = ft)) of |
|
834 |
None \<Rightarrow> None |
|
835 |
| Some (pr,ft,nr) \<Rightarrow> Some nr)" |
|
836 |
||
837 |
definition role_decl_check :: "role_t \<Rightarrow> type_t \<Rightarrow> bool" |
|
838 |
where |
|
839 |
"role_decl_check r t \<equiv> member role_declaration_rules (\<lambda> (role, types). r = role \<and> t \<in> types)" |
|
840 |
||
841 |
definition role_allow :: "role_t \<Rightarrow> role_t \<Rightarrow> bool" |
|
842 |
where |
|
843 |
"role_allow r nr \<equiv> member role_allowed_rules (\<lambda> (from, to). r \<in> from \<and> nr \<in> to)" |
|
844 |
||
845 |
(* here don't use lookup, because constrains should all be satisfied, |
|
846 |
* while transitions always choose the "first" one |
|
847 |
*) |
|
848 |
definition constrains_satisfied :: |
|
849 |
"security_context_t \<Rightarrow> security_context_t \<Rightarrow> security_class_t \<Rightarrow> permission_t \<Rightarrow> bool" |
|
850 |
where |
|
851 |
"constrains_satisfied sctxt tctxt c p \<equiv> |
|
852 |
(fold (\<lambda> (cset, pset, P) res. res \<and> (if (c \<in> cset \<and> p \<in> pset) |
|
853 |
then P sctxt tctxt else True)) |
|
854 |
constrains_rules True)" |
|
855 |
||
856 |
(* main interface for TE_allow check and constrains check *) |
|
857 |
fun permission_check:: |
|
858 |
"security_context_t \<Rightarrow> security_context_t \<Rightarrow> security_class_t \<Rightarrow> permission_t \<Rightarrow> bool" |
|
859 |
where |
|
860 |
"permission_check (su,sr,st) (tu,tr,tt) c p = |
|
861 |
((TE_allow st tt c p) \<and> (constrains_satisfied (su,sr,st) (tu,tr,tt) c p))" |
|
862 |
declare permission_check.simps [simp del] |
|
863 |
||
864 |
(* no changeowner ??? : by adding "relabel obj sectxt" event or "login" event? |
|
865 |
* or this explanation: the running of the sever will not grant any login but running |
|
866 |
* of the server-programs! |
|
867 |
*) |
|
868 |
||
869 |
fun user_of_obj :: "t_state \<Rightarrow> t_object \<Rightarrow> user_t option" |
|
870 |
where |
|
871 |
"user_of_obj [] = init_user_of_obj" |
|
872 |
| "user_of_obj (Clone p p' fds shms # s) = |
|
873 |
(\<lambda> obj. case obj of |
|
874 |
O_proc p'' \<Rightarrow> if (p'' = p') then user_of_obj s (O_proc p) else user_of_obj s obj |
|
875 |
| O_fd p'' fd \<Rightarrow> if (p'' = p' \<and> fd \<in> fds) then user_of_obj s (O_fd p fd) |
|
876 |
else if (p'' = p') then None |
|
877 |
else user_of_obj s obj |
|
878 |
| O_tcp_sock (p'',fd) \<Rightarrow> if (p'' = p' \<and> fd \<in> fds) then user_of_obj s (O_tcp_sock (p, fd)) |
|
879 |
else if (p'' = p') then None |
|
880 |
else user_of_obj s obj |
|
881 |
| O_udp_sock (p'',fd) \<Rightarrow> if (p'' = p' \<and> fd \<in> fds) then user_of_obj s (O_udp_sock (p, fd)) |
|
882 |
else if (p'' = p') then None |
|
883 |
else user_of_obj s obj |
|
884 |
| _ \<Rightarrow> user_of_obj s obj)" |
|
885 |
| "user_of_obj (Open p f flags fd iopt # s) = |
|
886 |
(case iopt of |
|
887 |
None \<Rightarrow> (user_of_obj s) ((O_fd p fd) := user_of_obj s (O_proc p)) |
|
888 |
| _ \<Rightarrow> ((user_of_obj s) ((O_fd p fd) := user_of_obj s (O_proc p))) |
|
889 |
((O_file f) := user_of_obj s (O_proc p)))" |
|
890 |
| "user_of_obj (Mkdir p f inum # s) = |
|
891 |
(user_of_obj s) ((O_dir f) := user_of_obj s (O_proc p))" |
|
892 |
| "user_of_obj (LinkHard p f f' # s) = |
|
893 |
(user_of_obj s) ((O_file f') := user_of_obj s (O_proc p))" |
|
894 |
(* |
|
895 |
| "user_of_obj (Rename p f2 f3 # s) = |
|
896 |
(\<lambda> obj. case obj of |
|
897 |
O_file f \<Rightarrow> if (f3 \<preceq> f) then user_of_obj s (O_file (file_before_rename f2 f3 f)) |
|
898 |
else user_of_obj s obj |
|
899 |
| O_dir f \<Rightarrow> if (f3 \<preceq> f) then user_of_obj s (O_dir (file_before_rename f2 f3 f)) |
|
900 |
else user_of_obj s obj |
|
901 |
| _ \<Rightarrow> user_of_obj s obj)" |
|
902 |
*) |
|
903 |
| "user_of_obj (CreateMsgq p q # s) = |
|
904 |
(user_of_obj s) ((O_msgq q) := user_of_obj s (O_proc p))" |
|
905 |
| "user_of_obj (SendMsg p q m # s) = |
|
906 |
(user_of_obj s) ((O_msg q m) := user_of_obj s (O_proc p))" |
|
907 |
| "user_of_obj (CreateShM p h # s) = |
|
908 |
(user_of_obj s) ((O_shm h) := user_of_obj s (O_proc p))" |
|
909 |
| "user_of_obj (CreateSock p af st fd inum # s) = (\<lambda> obj. |
|
910 |
case obj of |
|
911 |
O_fd p' fd' \<Rightarrow> if (p' = p \<and> fd' = fd) then user_of_obj s (O_proc p) |
|
912 |
else user_of_obj s obj |
|
913 |
| O_tcp_sock (p', fd') \<Rightarrow> if (p' = p \<and> fd' = fd \<and> st = STREAM) then user_of_obj s (O_proc p) |
|
914 |
else user_of_obj s obj |
|
915 |
| O_udp_sock (p', fd') \<Rightarrow> if (p' = p \<and> fd' = fd \<and> st = DGRAM) then user_of_obj s (O_proc p) |
|
916 |
else user_of_obj s obj |
|
917 |
| _ \<Rightarrow> user_of_obj s obj )" |
|
918 |
| "user_of_obj (Accept p fd addr port fd' inum # s) = (\<lambda> obj. |
|
919 |
case obj of |
|
920 |
O_fd p' fd'' \<Rightarrow> if (p' = p \<and> fd'' = fd') then user_of_obj s (O_proc p) |
|
921 |
else user_of_obj s obj |
|
922 |
| O_tcp_sock (p', fd'') \<Rightarrow> if (p' = p \<and> fd'' = fd') then user_of_obj s (O_proc p) |
|
923 |
else user_of_obj s obj |
|
924 |
| _ \<Rightarrow> user_of_obj s obj )" |
|
925 |
| "user_of_obj (_ # s) = user_of_obj s" |
|
926 |
||
927 |
fun type_of_obj :: "t_state \<Rightarrow> (t_object \<Rightarrow> type_t option)" |
|
928 |
where |
|
929 |
"type_of_obj [] = init_type_of_obj" |
|
930 |
| "type_of_obj (Clone p p' fds shms # s) = (\<lambda> obj. |
|
931 |
case obj of |
|
932 |
O_proc p'' \<Rightarrow> if (p'' = p') then type_of_obj s (O_proc p) else type_of_obj s obj |
|
933 |
| O_fd p'' fd \<Rightarrow> if (p'' = p' \<and> fd \<in> fds) then type_of_obj s (O_fd p fd) |
|
934 |
else if (p'' = p') then None |
|
935 |
else type_of_obj s obj |
|
936 |
| O_tcp_sock (p'', fd) \<Rightarrow> if (p'' = p' \<and> fd \<in> fds) then type_of_obj s (O_tcp_sock (p, fd)) |
|
937 |
else if (p'' = p') then None |
|
938 |
else type_of_obj s obj |
|
939 |
| O_udp_sock (p'', fd) \<Rightarrow> if (p'' = p' \<and> fd \<in> fds) then type_of_obj s (O_udp_sock (p, fd)) |
|
940 |
else if (p'' = p') then None |
|
941 |
else type_of_obj s obj |
|
942 |
| _ \<Rightarrow> type_of_obj s obj )" |
|
943 |
| "type_of_obj (Execve p f fds # s) = (type_of_obj s) ((O_proc p) := |
|
944 |
(case (type_of_obj s (O_proc p), type_of_obj s (O_file f)) of |
|
945 |
(Some tp, Some tf) \<Rightarrow> Some (type_transition tp tf C_process False) |
|
946 |
| _ \<Rightarrow> None) )" |
|
947 |
| "type_of_obj (Open p f flags fd (Some inum) # s) = ((type_of_obj s) ((O_file f) := |
|
948 |
(case (parent f) of |
|
949 |
Some pf \<Rightarrow> (case (type_of_obj s (O_proc p), type_of_obj s (O_dir pf)) of |
|
950 |
(Some tp, Some tpf) \<Rightarrow> Some (type_transition tp tpf C_file True) |
|
951 |
| _ \<Rightarrow> None) |
|
952 |
| _ \<Rightarrow> None) )) ((O_fd p fd) := |
|
953 |
type_of_obj s (O_proc p))" |
|
954 |
| "type_of_obj (Open p f flags fd None # s) = (type_of_obj s) ((O_fd p fd) := |
|
955 |
type_of_obj s (O_proc p))" |
|
956 |
| "type_of_obj (Mkdir p f inum # s) = (type_of_obj s) ((O_dir f) := |
|
957 |
(case (parent f) of |
|
958 |
Some pf \<Rightarrow> (case (type_of_obj s (O_proc p), type_of_obj s (O_dir pf)) of |
|
959 |
(Some tp, Some tpf) \<Rightarrow> Some (type_transition tp tpf C_dir True) |
|
960 |
| _ \<Rightarrow> None) |
|
961 |
| _ \<Rightarrow> None) )" |
|
962 |
| "type_of_obj (LinkHard p f f' # s) = (type_of_obj s) ((O_file f') := |
|
963 |
(case (parent f') of |
|
964 |
Some pf \<Rightarrow> (case (type_of_obj s (O_proc p), type_of_obj s (O_dir pf)) of |
|
965 |
(Some tp, Some tpf) \<Rightarrow> Some (type_transition tp tpf C_file True) |
|
966 |
| _ \<Rightarrow> None) |
|
967 |
| _ \<Rightarrow> None) )" |
|
968 |
(* |
|
969 |
| "type_of_obj (Rename p f f' # s) = (\<lambda> obj. |
|
970 |
case obj of |
|
971 |
O_file f'' \<Rightarrow> (if (f' \<preceq> f'') then type_of_obj s (O_file (file_before_rename f f' f'')) |
|
972 |
else type_of_obj s (O_file f'')) |
|
973 |
| O_dir f'' \<Rightarrow> (if (f' \<preceq> f'') then type_of_obj s (O_file (file_before_rename f f' f'')) |
|
974 |
else type_of_obj s (O_file f'')) |
|
975 |
| _ \<Rightarrow> type_of_obj s obj )" |
|
976 |
*) |
|
977 |
| "type_of_obj (CreateMsgq p q # s) = (type_of_obj s) ((O_msgq q) := |
|
978 |
(case (type_of_obj s (O_proc p)) of |
|
979 |
Some tp \<Rightarrow> Some tp |
|
980 |
| _ \<Rightarrow> None) )" |
|
981 |
| "type_of_obj (SendMsg p q m # s) = (type_of_obj s) ((O_msg q m) := |
|
982 |
(case (type_of_obj s (O_proc p), type_of_obj s (O_msgq q)) of |
|
983 |
(Some tp, Some tq) \<Rightarrow> Some (type_transition tp tq C_msg False) |
|
984 |
| _ \<Rightarrow> None) )" |
|
985 |
| "type_of_obj (CreateShM p h # s) = (type_of_obj s) ((O_shm h) := |
|
986 |
(case (type_of_obj s (O_proc p)) of |
|
987 |
Some tp \<Rightarrow> Some tp |
|
988 |
| _ \<Rightarrow> None) )" |
|
989 |
| "type_of_obj (CreateSock p af st fd inum # s) = (\<lambda> obj. |
|
990 |
case obj of |
|
991 |
O_fd p' fd' \<Rightarrow> if (p' = p \<and> fd' = fd) then type_of_obj s (O_proc p) |
|
992 |
else type_of_obj s obj |
|
993 |
| O_tcp_sock (p', fd') \<Rightarrow> if (p' = p \<and> fd' = fd \<and> st = STREAM) then type_of_obj s (O_proc p) |
|
994 |
else type_of_obj s obj |
|
995 |
| O_udp_sock (p', fd') \<Rightarrow> if (p' = p \<and> fd' = fd \<and> st = DGRAM) then type_of_obj s (O_proc p) |
|
996 |
else type_of_obj s obj |
|
997 |
| _ \<Rightarrow> type_of_obj s obj )" |
|
998 |
| "type_of_obj (Accept p fd addr port fd' inum # s) = (\<lambda> obj. |
|
999 |
case obj of |
|
1000 |
O_fd p' fd'' \<Rightarrow> if (p' = p \<and> fd'' = fd') then type_of_obj s (O_proc p) |
|
1001 |
else type_of_obj s obj |
|
1002 |
| O_tcp_sock (p', fd'') \<Rightarrow> if (p' = p \<and> fd'' = fd') then type_of_obj s (O_proc p) |
|
1003 |
else type_of_obj s obj |
|
1004 |
| _ \<Rightarrow> type_of_obj s obj )" |
|
1005 |
| "type_of_obj (_ # s) = type_of_obj s" |
|
1006 |
||
1007 |
fun role_of_proc :: "t_state \<Rightarrow> (t_process \<Rightarrow> role_t option)" |
|
1008 |
where |
|
1009 |
"role_of_proc [] = init_role_of_proc" |
|
1010 |
| "role_of_proc (Clone p p' fds shms # s) = |
|
1011 |
(role_of_proc s) (p' := role_of_proc s p)" |
|
1012 |
| "role_of_proc (Execve p f fds # s) = (role_of_proc s) (p := |
|
1013 |
(case (role_of_proc s p, type_of_obj s (O_file f)) of |
|
1014 |
(Some r, Some ft) \<Rightarrow> role_transition r ft |
|
1015 |
| _ \<Rightarrow> None) )" |
|
1016 |
| "role_of_proc (_ # s) = role_of_proc s" |
|
1017 |
||
1018 |
fun role_of_obj :: "t_state \<Rightarrow> t_object \<Rightarrow> role_t option" |
|
1019 |
where |
|
1020 |
"role_of_obj s (O_proc p) = role_of_proc s p" |
|
1021 |
| "role_of_obj s _ = Some R_object" (* object_r *) |
|
1022 |
||
1023 |
definition sectxt_of_obj :: "t_state \<Rightarrow> t_object \<Rightarrow> security_context_t option" |
|
1024 |
where |
|
1025 |
"sectxt_of_obj s obj = ( |
|
1026 |
case (user_of_obj s obj, role_of_obj s obj, type_of_obj s obj) of |
|
1027 |
(Some u, Some r, Some t) \<Rightarrow> Some (u, r, t) |
|
1028 |
| _ \<Rightarrow> None)" |
|
1029 |
||
7 | 1030 |
fun init_role_of_obj :: "t_object \<Rightarrow> role_t option" |
1031 |
where |
|
1032 |
"init_role_of_obj (O_proc p) = init_role_of_proc p" |
|
1033 |
| "init_role_of_obj _ = Some R_object" |
|
1034 |
||
1035 |
definition init_sectxt_of_obj :: "t_object \<Rightarrow> security_context_t option" |
|
1036 |
where |
|
1037 |
"init_sectxt_of_obj obj \<equiv> |
|
1038 |
(case (init_user_of_obj obj, init_role_of_obj obj, init_type_of_obj obj) of |
|
1039 |
(Some u, Some r, Some t) \<Rightarrow> Some (u,r,t) |
|
1040 |
| _ \<Rightarrow> None)" |
|
1041 |
||
1042 |
definition sec_of_root :: "security_context_t" |
|
1043 |
where |
|
1044 |
"sec_of_root \<equiv> (case (init_user_of_obj (O_dir []), init_type_of_obj (O_dir [])) of |
|
1045 |
(Some u, Some t) \<Rightarrow> (u, R_object, t))" |
|
1046 |
||
1 | 1047 |
(******* flask granting ******** |
1048 |
* involves three kinds of rules: |
|
1049 |
* 1. allow rule of types, allowed_rule_list_t, main |
|
1050 |
* 2. allow rule of roles, role_allow_rule_list_t, mainly for execve call |
|
1051 |
* 3. constrains, section 3.4.5, user-specifically defined. |
|
1052 |
* Passed all these 3, then grant Yes, else No |
|
1053 |
*) |
|
1054 |
||
1055 |
definition search_check_allp :: "security_context_t \<Rightarrow> security_context_t set \<Rightarrow> bool" |
|
1056 |
where |
|
1057 |
"search_check_allp pctxt sectxts = (\<forall> sec \<in> sectxts. permission_check pctxt sec C_dir P_search)" |
|
1058 |
||
1059 |
definition search_check_file :: "security_context_t \<Rightarrow> security_context_t \<Rightarrow> bool" |
|
1060 |
where |
|
1061 |
"search_check_file sctxt fctxt \<equiv> permission_check sctxt fctxt C_file P_search" |
|
1062 |
||
1063 |
definition search_check_dir :: "security_context_t \<Rightarrow> security_context_t \<Rightarrow> bool" |
|
1064 |
where |
|
1065 |
"search_check_dir sctxt fctxt \<equiv> permission_check sctxt fctxt C_dir P_search" |
|
1066 |
||
1067 |
fun get_parentfs_ctxts :: "t_state \<Rightarrow> t_file \<Rightarrow> (security_context_t list) option" |
|
1068 |
where |
|
1069 |
"get_parentfs_ctxts s [] = |
|
1070 |
(case (sectxt_of_obj s (O_dir [])) of |
|
1071 |
Some ctxt \<Rightarrow> Some [ctxt] |
|
1072 |
| _ \<Rightarrow> None)" |
|
1073 |
| "get_parentfs_ctxts s (f#pf) = |
|
1074 |
(case (get_parentfs_ctxts s pf, sectxt_of_obj s (O_dir (f#pf))) of |
|
1075 |
(Some ctxts, Some ctxt) \<Rightarrow> Some (ctxt#ctxts) |
|
1076 |
| _ \<Rightarrow> None)" |
|
1077 |
||
1078 |
fun search_check :: "t_state \<Rightarrow> security_context_t \<Rightarrow> t_file \<Rightarrow> bool" |
|
1079 |
where |
|
1080 |
"search_check s pctxt [] = |
|
1081 |
(case (sectxt_of_obj s (O_dir [])) of |
|
1082 |
Some fctxt \<Rightarrow> search_check_dir pctxt fctxt |
|
1083 |
| _ \<Rightarrow> False)" |
|
1084 |
| "search_check s pctxt (f#pf) = |
|
1085 |
(if (is_file s (f#pf)) |
|
1086 |
then (case (sectxt_of_obj s (O_file (f#pf)), get_parentfs_ctxts s pf) of |
|
1087 |
(Some fctxt, Some pfctxts) \<Rightarrow> (search_check_file pctxt fctxt \<and> search_check_allp pctxt (set pfctxts)) |
|
1088 |
| _ \<Rightarrow> False) |
|
1089 |
else (case (sectxt_of_obj s (O_dir (f#pf)), get_parentfs_ctxts s pf) of |
|
1090 |
(Some fctxt, Some pfctxts) \<Rightarrow> (search_check_dir pctxt fctxt \<and> search_check_allp pctxt (set pfctxts)) |
|
1091 |
| _ \<Rightarrow> False))" |
|
1092 |
||
1093 |
(* this means we should prove every current fd has a security context! *) |
|
1094 |
definition sectxts_of_fds :: "t_state \<Rightarrow> t_process \<Rightarrow> t_fd set \<Rightarrow> security_context_t set" |
|
1095 |
where |
|
1096 |
"sectxts_of_fds s p fds \<equiv> {ctxt. \<exists> fd \<in> fds. sectxt_of_obj s (O_fd p fd) = Some ctxt}" |
|
1097 |
||
1098 |
definition inherit_fds_check :: "t_state \<Rightarrow> security_context_t \<Rightarrow> t_process \<Rightarrow> t_fd set \<Rightarrow> bool" |
|
1099 |
where |
|
1100 |
"inherit_fds_check s npsectxt p fds \<equiv> |
|
1101 |
(\<forall> ctxt \<in> sectxts_of_fds s p fds. permission_check npsectxt ctxt C_fd P_inherit)" |
|
1102 |
||
1103 |
fun npctxt_execve :: "security_context_t \<Rightarrow> security_context_t \<Rightarrow> security_context_t option" |
|
1104 |
where |
|
1105 |
"npctxt_execve (pu,pr,pt) (fu,fr,ft) = |
|
1106 |
(case (role_transition pr ft) of |
|
1107 |
Some nr \<Rightarrow> Some (pu, nr, type_transition pt ft C_process False) |
|
1108 |
| _ \<Rightarrow> None)" |
|
1109 |
||
1110 |
fun nfctxt_create :: "security_context_t \<Rightarrow> security_context_t \<Rightarrow> security_class_t \<Rightarrow> security_context_t" |
|
1111 |
where |
|
1112 |
"nfctxt_create (pu,pr,pt) (fu,fr,ft) cls = (pu, R_object, type_transition pt ft cls True)" |
|
1113 |
||
1114 |
fun grant_execve :: |
|
1115 |
"security_context_t \<Rightarrow> security_context_t \<Rightarrow> security_context_t \<Rightarrow> bool" |
|
1116 |
where |
|
1117 |
"grant_execve (up,rp,tp) (uf,rf,tf) (up',nr,nt) = |
|
1118 |
(role_decl_check nr nt \<and> role_allow rp nr \<and> |
|
1119 |
permission_check (up,rp,tp) (uf,rf,tf) C_file P_execute \<and> |
|
1120 |
permission_check (up,nr,nt) (uf,rf,tf) C_file P_entrypoint \<and> |
|
1121 |
permission_check (up,rp,tp) (up,nr,nt) C_process P_transition \<and> |
|
1122 |
permission_check (up,nr,nt) (uf,rf,tf) C_process P_execute)" |
|
1123 |
||
1124 |
definition sectxts_of_shms :: "t_state \<Rightarrow> t_shm set \<Rightarrow> security_context_t set" |
|
1125 |
where |
|
1126 |
"sectxts_of_shms s shms \<equiv> {ctxt. \<exists> h \<in> shms. sectxt_of_obj s (O_shm h) = Some ctxt}" |
|
1127 |
||
1128 |
definition inherit_shms_check :: "t_state \<Rightarrow> security_context_t \<Rightarrow> t_shm set \<Rightarrow> bool" |
|
1129 |
where |
|
1130 |
"inherit_shms_check s npsectxt shms \<equiv> |
|
1131 |
(\<forall> ctxt \<in> sectxts_of_shms s shms. permission_check npsectxt ctxt C_shm P_inherit)" |
|
1132 |
||
1133 |
fun perm_of_mflag :: "t_open_must_flag \<Rightarrow> permission_t set" |
|
1134 |
where |
|
1135 |
"perm_of_mflag OF_RDONLY = {P_read}" |
|
1136 |
| "perm_of_mflag OF_WRONLY = {P_write}" |
|
1137 |
| "perm_of_mflag OF_RDWR = {P_read, P_write}" |
|
1138 |
||
1139 |
definition perm_of_oflag :: "t_open_option_flag set \<Rightarrow> permission_t set" |
|
1140 |
where |
|
1141 |
"perm_of_oflag oflags \<equiv> |
|
1142 |
(case (OF_APPEND \<in> oflags, OF_CREAT \<in> oflags) of |
|
1143 |
(True, True) \<Rightarrow> {P_append, P_create} |
|
1144 |
| (True, _ ) \<Rightarrow> {P_append} |
|
1145 |
| (_ , True) \<Rightarrow> {P_create} |
|
1146 |
| _ \<Rightarrow> {})" |
|
1147 |
||
1148 |
definition perms_of_flags :: "t_open_flags \<Rightarrow> permission_t set" |
|
1149 |
where |
|
1150 |
"perms_of_flags flags \<equiv> |
|
1151 |
(case flags of |
|
1152 |
(mflag,oflags) \<Rightarrow> (perm_of_mflag mflag \<union> perm_of_oflag oflags))" |
|
1153 |
||
1154 |
(* |
|
1155 |
definition class_of_flags :: "t_open_flags \<Rightarrow> security_class_t" |
|
1156 |
where |
|
1157 |
"class_of_flags flags \<equiv> |
|
1158 |
(case flags of |
|
1159 |
(mflag, oflags) \<Rightarrow> if (OF_DIRECTORY \<in> oflags) then C_dir else C_file)" |
|
1160 |
||
1161 |
definition obj_of_flags :: "t_open_flags \<Rightarrow> t_file \<Rightarrow> t_object" |
|
1162 |
where |
|
1163 |
"obj_of_flags flags f \<equiv> |
|
1164 |
(case flags of |
|
1165 |
(mflag, oflags) \<Rightarrow> if (OF_DIRECTORY \<in> oflags) then O_dir f else O_file f)" |
|
1166 |
*) |
|
1167 |
||
1168 |
definition oflags_check :: "t_open_flags \<Rightarrow> security_context_t \<Rightarrow> security_context_t \<Rightarrow> bool" |
|
1169 |
where |
|
1170 |
"oflags_check flags pctxt fctxt \<equiv> |
|
1171 |
\<forall> perm \<in> (perms_of_flags flags). permission_check pctxt fctxt C_file perm" |
|
1172 |
||
1173 |
fun grant :: "t_state \<Rightarrow> t_event \<Rightarrow> bool" |
|
1174 |
where |
|
1175 |
"grant s (Execve p f fds) = |
|
1176 |
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_file f)) of |
|
1177 |
(Some (up, rp, tp), Some (uf, rf, tf)) \<Rightarrow> |
|
1178 |
(case (npctxt_execve (up,rp,tp) (uf,rf,tf)) of |
|
1179 |
Some (pu,nr,nt) \<Rightarrow> ( |
|
1180 |
search_check s (up,rp,tp) f \<and> |
|
1181 |
grant_execve (up,rp,tp) (uf,rf,tf) (up,nr,nt) \<and> |
|
1182 |
inherit_fds_check s (up,nr,nt) p fds) |
|
1183 |
| _ \<Rightarrow> False) |
|
1184 |
| _ \<Rightarrow> False )" |
|
1185 |
| "grant s (Clone p p' fds shms) = |
|
1186 |
(case (sectxt_of_obj s (O_proc p)) of |
|
1187 |
Some (up, rp, tp) \<Rightarrow> |
|
1188 |
(permission_check (up,rp,tp) (up,rp,tp) C_process P_fork \<and> |
|
1189 |
inherit_fds_check s (up,rp,tp) p fds \<and> |
|
1190 |
inherit_shms_check s (up,rp,tp) shms) |
|
1191 |
| _ \<Rightarrow> False )" |
|
1192 |
| "grant s (Kill 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_sigkill |
|
1196 |
| _ \<Rightarrow> False)" |
|
1197 |
| "grant s (Ptrace p p') = |
|
1198 |
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_proc p')) of |
|
1199 |
(Some (su,sr,st), Some (tu,tr,tt)) \<Rightarrow> |
|
1200 |
permission_check (su,sr,st) (tu,tr,tt) C_process P_ptrace |
|
1201 |
| _ \<Rightarrow> False)" |
|
1202 |
| "grant s (Exit p) = True" |
|
1203 |
| "grant s (Open p f flags fd None) = |
|
1204 |
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_file f)) of |
|
1205 |
(Some (pu,pr,pt), Some (fu,fr,ft)) \<Rightarrow> |
|
1206 |
search_check s (pu,pr,pt) f \<and> |
|
1207 |
oflags_check flags (pu,pr,pt) (fu,fr,ft) \<and> |
|
1208 |
permission_check (pu,pr,pt) (pu,pr,pt) C_fd P_setattr |
|
1209 |
| _ \<Rightarrow>False)" |
|
1210 |
| "grant s (Open p f flags fd (Some inum)) = |
|
1211 |
(case (parent f) of |
|
1212 |
Some pf \<Rightarrow> (case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_dir pf)) of |
|
1213 |
(Some (pu,pr,pt), Some (pfu,pfr,pft)) \<Rightarrow> |
|
1214 |
(search_check s (pu,pr,pt) pf \<and> |
|
1215 |
oflags_check flags (pu,pr,pt) (nfctxt_create (pu,pr,pt) (pfu,pfr,pft) C_file) \<and> |
|
1216 |
permission_check (pu,pr,pt) (pfu,pfr,pft) C_dir P_add_name \<and> |
|
1217 |
permission_check (pu,pr,pt) (pu,pr,pt) C_fd P_setattr) |
|
1218 |
| _ \<Rightarrow> False) |
|
1219 |
| _ \<Rightarrow> False)" |
|
1220 |
| "grant s (ReadFile p fd) = |
|
1221 |
(case (file_of_proc_fd s p fd) of |
|
1222 |
Some f \<Rightarrow> |
|
1223 |
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_fd p fd), |
|
1224 |
sectxt_of_obj s (O_file f)) of |
|
1225 |
(Some (pu,pr,pt), Some (fdu,fdr,fdt), Some (fu,fr,ft)) \<Rightarrow> |
|
1226 |
(permission_check (pu,pr,pt) (fdu,fdr,fdt) C_fd P_setattr \<and> |
|
1227 |
permission_check (pu,pr,pt) (fu,fr,ft) C_file P_read) |
|
1228 |
| _ \<Rightarrow> False) |
|
1229 |
| _ \<Rightarrow> False)" |
|
1230 |
| "grant s (WriteFile p fd) = |
|
1231 |
(case (file_of_proc_fd s p fd) of |
|
1232 |
Some f \<Rightarrow> |
|
1233 |
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_fd p fd), |
|
1234 |
sectxt_of_obj s (O_file f)) of |
|
1235 |
(Some (pu,pr,pt), Some (fdu,fdr,fdt), Some (fu,fr,ft)) \<Rightarrow> |
|
1236 |
(permission_check (pu,pr,pt) (fdu,fdr,fdt) C_fd P_setattr \<and> |
|
1237 |
permission_check (pu,pr,pt) (fu,fr,ft) C_file P_write) |
|
1238 |
| _ \<Rightarrow> False) |
|
1239 |
| _ \<Rightarrow> False)" |
|
1240 |
| "grant s (CloseFd p fd) = True" |
|
1241 |
| "grant s (UnLink p f) = |
|
1242 |
(case (parent f) of |
|
1243 |
Some pf \<Rightarrow> |
|
1244 |
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_dir pf), |
|
1245 |
sectxt_of_obj s (O_file f)) of |
|
1246 |
(Some (pu,pr,pt), Some (du,dr,dt), Some (fu,fr,ft)) \<Rightarrow> |
|
1247 |
(search_check s (pu,pr,pt) f \<and> |
|
1248 |
permission_check (pu,pr,pt) (fu,fr,ft) C_file P_unlink \<and> |
|
1249 |
permission_check (pu,pr,pt) (du,dr,dt) C_dir P_remove_name) |
|
1250 |
| _ \<Rightarrow> False) |
|
1251 |
| _ \<Rightarrow> False)" |
|
1252 |
| "grant s (Rmdir p f) = |
|
1253 |
(case (parent f) of |
|
1254 |
Some pf \<Rightarrow> |
|
1255 |
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_dir pf), |
|
1256 |
sectxt_of_obj s (O_dir f)) of |
|
1257 |
(Some (pu,pr,pt), Some (du,dr,dt), Some (fu,fr,ft)) \<Rightarrow> |
|
1258 |
(search_check s (pu,pr,pt) f \<and> |
|
1259 |
permission_check (pu,pr,pt) (fu,fr,ft) C_dir P_rmdir \<and> |
|
1260 |
permission_check (pu,pr,pt) (du,dr,dt) C_dir P_remove_name) |
|
1261 |
| _ \<Rightarrow> False) |
|
1262 |
| _ \<Rightarrow> False)" |
|
1263 |
| "grant s (Mkdir p f inum) = |
|
1264 |
(case (parent f) of |
|
1265 |
Some pf \<Rightarrow> |
|
1266 |
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_dir pf)) of |
|
1267 |
(Some (pu,pr,pt), Some (du,dr,dt)) \<Rightarrow> |
|
1268 |
(search_check s (pu,pr,pt) f \<and> |
|
1269 |
permission_check (pu,pr,pt) (nfctxt_create (pu,pr,pt) (du,dr,dt) C_dir) C_dir P_create \<and> |
|
1270 |
permission_check (pu,pr,pt) (du,dr,dt) C_dir P_add_name) |
|
1271 |
| _ \<Rightarrow> False) |
|
1272 |
| _ \<Rightarrow> False)" |
|
1273 |
| "grant s (LinkHard p f f') = |
|
1274 |
(case (parent f') of |
|
1275 |
Some pf \<Rightarrow> |
|
1276 |
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_dir pf), |
|
1277 |
sectxt_of_obj s (O_file f)) of |
|
1278 |
(Some (pu,pr,pt), Some (du,dr,dt), Some (fu,fr,ft)) \<Rightarrow> |
|
1279 |
(search_check s (pu,pr,pt) f \<and> search_check s (pu,pr,pt) pf \<and> |
|
1280 |
permission_check (pu,pr,pt) (fu,fr,ft) C_file P_link \<and> |
|
1281 |
permission_check (pu,pr,pt) (du,dr,dt) C_dir P_add_name) |
|
1282 |
| _ \<Rightarrow> False) |
|
1283 |
| _ \<Rightarrow> False)" |
|
1284 |
| "grant s (Truncate p f len) = |
|
1285 |
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_file f)) of |
|
1286 |
(Some (pu,pr,pt), Some (fu,fr,ft)) \<Rightarrow> |
|
1287 |
(search_check s (pu,pr,pt) f \<and> |
|
1288 |
permission_check (pu,pr,pt) (fu,fr,ft) C_file P_setattr) |
|
1289 |
| _ \<Rightarrow> False)" |
|
1290 |
(* |
|
1291 |
| "grant s (Rename p f f') = |
|
1292 |
(case (parent f, parent f') of |
|
1293 |
(Some pf, Some pf') \<Rightarrow> |
|
1294 |
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_dir pf), |
|
1295 |
sectxt_of_obj s (O_file f), sectxt_of_obj s (O_dir pf')) of |
|
1296 |
(Some (pu,pr,pt), Some (pfu,pfr,pft), Some (fu,fr,ft), |
|
1297 |
Some (pfu',pfr',pft')) \<Rightarrow> |
|
1298 |
(search_check s (pu,pr,pt) f \<and> |
|
1299 |
permission_check (pu,pr,pt) (pfu,pfr,pft) C_dir P_remove_name \<and> |
|
1300 |
(if (is_file s f) |
|
1301 |
then permission_check (pu,pr,pt) (fu,fr,ft) C_file P_rename |
|
1302 |
else permission_check (pu,pr,pt) (fu,fr,ft) C_dir P_reparent) \<and> |
|
1303 |
search_check s (pu,pr,pt) pf' \<and> |
|
1304 |
permission_check (pu,pr,pt) (pfu',pfr',pft') C_dir P_add_name) |
|
1305 |
| _ \<Rightarrow> False) |
|
1306 |
| _ \<Rightarrow> False)" |
|
1307 |
*) |
|
1308 |
| "grant s (CreateMsgq p q) = |
|
1309 |
(case (sectxt_of_obj s (O_proc p)) of |
|
1310 |
Some (pu,pr,pt) \<Rightarrow> |
|
1311 |
(permission_check (pu,pr,pt) (pu,pr,pt) C_msgq P_associate \<and> |
|
1312 |
permission_check (pu,pr,pt) (pu,pr,pt) C_msgq P_create) |
|
1313 |
| _ \<Rightarrow> False)" |
|
1314 |
| "grant s (SendMsg p q m) = |
|
1315 |
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_msgq q)) of |
|
1316 |
(Some (pu,pr,pt), Some (qu,qr,qt)) \<Rightarrow> |
|
1317 |
(permission_check (pu,pr,pt) (qu,qr,qt) C_msgq P_enqueue \<and> |
|
1318 |
permission_check (pu,pr,pt) (qu,qr,qt) C_msgq P_write \<and> |
|
1319 |
permission_check (pu,pr,pt) (pu,pr,pt) C_msg P_send) |
|
1320 |
| _ \<Rightarrow> False)" |
|
1321 |
| "grant s (RecvMsg p q m) = |
|
1322 |
(case (sectxt_of_obj s (O_proc p),sectxt_of_obj s (O_msgq q),sectxt_of_obj s (O_msg q m)) of |
|
1323 |
(Some (pu,pr,pt), Some (qu,qr,qt), Some (mu,mr,mt)) \<Rightarrow> |
|
1324 |
permission_check (pu,pr,pt) (qu,qr,qt) C_msgq P_read \<and> |
|
1325 |
permission_check (pu,pr,pt) (mu,mr,mt) C_msg P_receive |
|
1326 |
| _ \<Rightarrow> False)" |
|
1327 |
| "grant s (RemoveMsgq p q) = |
|
1328 |
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_msgq q)) of |
|
1329 |
(Some (pu,pr,pt), Some (qu,qr,qt)) \<Rightarrow> |
|
1330 |
permission_check (pu,pr,pt) (qu,qr,qt) C_msgq P_destroy |
|
1331 |
| _ \<Rightarrow> False)" |
|
1332 |
| "grant s (CreateShM p h) = |
|
1333 |
(case (sectxt_of_obj s (O_proc p)) of |
|
1334 |
Some (pu,pr,pt) \<Rightarrow> |
|
1335 |
(permission_check (pu,pr,pt) (pu,pr,pt) C_shm P_associate \<and> |
|
1336 |
permission_check (pu,pr,pt) (pu,pr,pt) C_shm P_create) |
|
1337 |
| _ \<Rightarrow> False)" |
|
1338 |
| "grant s (Attach p h flag) = |
|
1339 |
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_shm h)) of |
|
1340 |
(Some (pu,pr,pt), Some (hu,hr,ht)) \<Rightarrow> |
|
1341 |
if flag = SHM_RDONLY |
|
1342 |
then permission_check (pu,pr,pt) (hu,hr,ht) C_shm P_read |
|
1343 |
else permission_check (pu,pr,pt) (hu,hr,ht) C_shm P_read \<and> |
|
1344 |
permission_check (pu,pr,pt) (hu,hr,ht) C_shm P_write |
|
1345 |
| _ \<Rightarrow> False)" |
|
1346 |
| "grant s (Detach p h) = True" (*?*) |
|
1347 |
| "grant s (DeleteShM p h) = |
|
1348 |
(case (sectxt_of_obj s (O_proc p), sectxt_of_obj s (O_shm h)) of |
|
1349 |
(Some (pu,pr,pt), Some (hu,hr,ht)) \<Rightarrow> |
|
1350 |
permission_check (pu,pr,pt) (hu,hr,ht) C_shm P_destroy |
|
1351 |
| _ \<Rightarrow> False)" |
|
1352 |
||
1353 |
||
1354 |
| "grant s _ = False" (* socket event is currently not in consideration *) |
|
1355 |
||
1356 |
inductive valid :: "t_state \<Rightarrow> bool" |
|
1357 |
where |
|
1358 |
"valid []" |
|
1359 |
| "\<lbrakk>valid s; os_grant s e; grant s e\<rbrakk> \<Longrightarrow> valid (e # s)" |
|
1360 |
||
1361 |
(* tobj: object that can be tainted *) |
|
1362 |
fun tobj_in_init :: "t_object \<Rightarrow> bool" |
|
1363 |
where |
|
1364 |
"tobj_in_init (O_proc p) = (p \<in> init_procs)" |
|
1365 |
| "tobj_in_init (O_file f) = (f \<in> init_files)" |
|
1366 |
(* directory is not taintable |
|
1367 |
| "tobj_in_init (O_dir f) = (f \<in> init_files)" |
|
1368 |
*) |
|
1369 |
| "tobj_in_init (O_node n) = (n \<in> init_nodes)" |
|
1370 |
| "tobj_in_init (O_msg q m) = (member (init_msgs_of_queue q) ((op =) m))" |
|
1371 |
| "tobj_in_init _ = False" (* other kind of obj cannot be tainted *) |
|
1372 |
||
1373 |
(* no use |
|
1374 |
fun no_del_event:: "t_event list \<Rightarrow> bool" |
|
1375 |
where |
|
1376 |
"no_del_event [] = True" |
|
1377 |
| "no_del_event (Kill p p' # \<tau>) = False" |
|
1378 |
| "no_del_event (CloseFd p fd # \<tau>) = False" |
|
1379 |
| "no_del_event (UnLink p f # \<tau>) = False" |
|
1380 |
| "no_del_event (Rmdir p f # \<tau>) = False" |
|
1381 |
(* |
|
1382 |
| "no_del_event (Rename p f f' # \<tau>) = False" |
|
1383 |
*) |
|
1384 |
| "no_del_event (RemoveMsgq p q # \<tau>) = False" |
|
1385 |
| "no_del_event (RecvMsg p q m # \<tau>) = False" |
|
1386 |
| "no_del_event (_ # \<tau>) = no_del_event \<tau>" |
|
1387 |
*) |
|
1388 |
||
1389 |
end |
|
1390 |
||
1391 |
locale tainting = flask + |
|
1392 |
||
1393 |
fixes seeds :: "t_object set" |
|
1394 |
assumes |
|
1395 |
seeds_in_init: "\<And> obj. obj \<in> seeds \<Longrightarrow> tobj_in_init obj" |
|
1396 |
||
1397 |
begin |
|
1398 |
||
1399 |
inductive tainted :: "t_object \<Rightarrow> t_state \<Rightarrow> bool" ("_ \<in> tainted _" [100, 100] 100) |
|
1400 |
where |
|
1401 |
t_init: "obj \<in> seeds \<Longrightarrow> obj \<in> tainted []" |
|
1402 |
| t_clone: "\<lbrakk>O_proc p \<in> tainted s; valid (Clone p p' fds shms # s)\<rbrakk> |
|
1403 |
\<Longrightarrow> O_proc p' \<in> tainted (Clone p p' fds shms # s)" |
|
1404 |
| t_execve: "\<lbrakk>O_file f \<in> tainted s; valid (Execve p f fds # s)\<rbrakk> |
|
1405 |
\<Longrightarrow> O_proc p \<in> tainted (Execve p f fds # s)" |
|
1406 |
| t_ptrace: "\<lbrakk>O_proc p \<in> tainted s; valid (Ptrace p p' # s); info_flow_shm s p' p''\<rbrakk> |
|
1407 |
\<Longrightarrow> O_proc p'' \<in> tainted (Ptrace p p' # s)" |
|
1408 |
| t_ptrace':"\<lbrakk>O_proc p' \<in> tainted s; valid (Ptrace p p' # s); info_flow_shm s p p''\<rbrakk> |
|
1409 |
\<Longrightarrow> O_proc p'' \<in> tainted (Ptrace p p' # s)" |
|
1410 |
| t_cfile: "\<lbrakk>O_proc p \<in> tainted s; valid (Open p f flags fd (Some inum) # s)\<rbrakk> |
|
1411 |
\<Longrightarrow> O_file f \<in> tainted (Open p f flags fd (Some inum) # s)" |
|
1412 |
| t_read: "\<lbrakk>O_file f \<in> tainted s; valid (ReadFile p fd # s); |
|
1413 |
file_of_proc_fd s p fd = Some f; info_flow_shm s p p'\<rbrakk> |
|
1414 |
\<Longrightarrow> O_proc p' \<in> tainted (ReadFile p fd # s)" |
|
1415 |
| t_write: "\<lbrakk>O_proc p \<in> tainted s; valid (WriteFile p fd # s); |
|
1416 |
file_of_proc_fd s p fd = Some f; has_same_inode s f f'\<rbrakk> |
|
1417 |
\<Longrightarrow> O_file f' \<in> tainted (WriteFile p fd # s)" |
|
1418 |
(* directory is not tainted |
|
1419 |
| t_mkdir: "\<lbrakk>O_proc p \<in> tainted s; valid (Mkdir p f inum # s)\<rbrakk> |
|
1420 |
\<Longrightarrow> O_dir f \<in> tainted (Mkdir p f inum # s)" |
|
1421 |
*) |
|
1422 |
| t_link: "\<lbrakk>O_file f \<in> tainted s; valid (LinkHard p f f' # s)\<rbrakk> |
|
1423 |
\<Longrightarrow> O_file f' \<in> tainted (LinkHard p f f' # s)" |
|
1424 |
| t_trunc: "\<lbrakk>O_proc p \<in> tainted s; valid (Truncate p f len # s); len > 0; has_same_inode s f f'\<rbrakk> |
|
1425 |
\<Longrightarrow> O_file f' \<in> tainted (Truncate p f len # s)" |
|
1426 |
(* |
|
1427 |
| t_rename: "\<lbrakk>O_file f'' \<in> tainted s; valid (Rename p f f' # s);f \<preceq> f''\<rbrakk> |
|
1428 |
\<Longrightarrow> O_file (file_after_rename f f' f'') \<in> tainted (Rename p f f' # s)" |
|
1429 |
| t_rename':"\<lbrakk>O_dir f'' \<in> tainted s; valid (Rename p f f' # s);f \<preceq> f''\<rbrakk> |
|
1430 |
\<Longrightarrow> O_dir (file_after_rename f f' f'') \<in> tainted (Rename p f f' # s)" |
|
1431 |
*) |
|
1432 |
| t_sendmsg:"\<lbrakk>O_proc p \<in> tainted s; valid (SendMsg p q m # s)\<rbrakk> |
|
1433 |
\<Longrightarrow> O_msg q m \<in> tainted (SendMsg p q m # s)" |
|
1434 |
| t_recvmsg:"\<lbrakk>O_msg q m \<in> tainted s; valid (RecvMsg p q m # s); info_flow_shm s p p'\<rbrakk> |
|
1435 |
\<Longrightarrow> O_proc p' \<in> tainted (RecvMsg p q m # s)" |
|
1436 |
| t_remain: "\<lbrakk>obj \<in> tainted s; valid (e # s); alive (e # s) obj\<rbrakk> |
|
1437 |
\<Longrightarrow> obj \<in> tainted (e # s)" |
|
1438 |
||
1439 |
definition taintable:: "t_object \<Rightarrow> bool" |
|
1440 |
where |
|
1441 |
"taintable obj \<equiv> init_alive obj \<and> (\<exists> s. obj \<in> tainted s)" |
|
1442 |
||
1443 |
definition undeletable :: "t_object \<Rightarrow> bool" |
|
1444 |
where |
|
1445 |
"undeletable obj \<equiv> init_alive obj \<and> \<not> (\<exists> s. valid s \<and> deleted obj s)" |
|
1446 |
||
1447 |
end |
|
1448 |
||
1449 |
end |