forked from SeattleTestbed/repy_v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestrictions.py
executable file
·402 lines (251 loc) · 11 KB
/
restrictions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
"""
Author: Justin Cappos
Start Date: 27 June 2008
Description:
This class handles access control for functions, objects, etc.
Many of the functions here are intentionally written in a braindead way.
This module is supposed to be readable and obviously correct. I could have
used the re module, etc. and made this much cleverer than it needs to be.
"""
# this does the resource tracking / process stopping
import nanny
# Used to handle internal errors
import tracebackrepy
"""
The restrictions file format consists of lines that look like this:
Comment Lines:
# This is a comment
Resource restrictions: what the program is allowed to utilize
Usage: resource resourcename limit
Example:
resource CPU 0.1 # Can have %10 CPU utilization
resource memory 33554432 # Can have 32 MB of memory
resource outsockets 1 # Can initiate one outgoing comm
resource insocket 2 # Can listen for 2 incoming comms
resource messport_2023 # Can use messageport 2023
Call restrictions: perform some action when a call occurs
Usage: call callname [arg restrictions] [allow / deny / prompt]
restrictions are of the form: [[arg num is string | noargs is num ] ... ]
Example:
call exitall allow # always allow exitall calls
call sleep arg 0 is 0.1 allow # allow sleep calls of len 0.1 second
call open arg 1 is r allow # allow read of any file in their dir
call open arg 0 is foo arg 1 is w allow # allow write if it is to foo
call open arg 1 is w deny # deny any other writes
call file.write noargs is 2 deny # deny any call to write with 2 args
Notes:
Lines are parsed in order and the first rule that applies to a call is used.
For example:
call open arg 1 is foo allow
call open arg 2 is w deny
These rules will allow 'foo' to be opened for writing.
Any unmatched call is automatically denied.
Wildcards may not be used.
There is no support for the 'or' operation.
Floating point numbers and integers must match their string representation!!!
I.e. 0.1 not .1 and 2 not 02.
"""
class ParseError(Exception):
pass
valid_actions = [ "allow", "deny", "prompt" ]
known_calls = [ "canceltimer", "exitall", "file.close", "file.flush",
"file.__init__", "file.next", "file.read", "file.readline",
"file.readlines", "file.seek", "file.write", "file.writelines",
"listdir", "removefile", "gethostbyname_ex", "getmyip", "open",
"openconn", "recvmess", "sendmess", "settimer", "sleep",
"socket.close", "socket.recv", "socket.send", "stopcomm",
"waitforconn", "log.write", "log.writelines", "randomfloat",
"getruntime", "getlock","get_thread_name","VirtualNamespace"
]
# Each rule can either be matched or not. Matched rules apply to the call
# this table is indexed by call name and contains tuples of (rule, action)
call_rule_table = {}
############################## Parsing ###############################
# given a list of tokens, produce a rule
def get_rule(rulelist):
if len(rulelist) == 0:
return []
# I'm going to walk through the current list and consume tokens as I build
# the rule
currentlist = rulelist
myrule = []
while currentlist != []:
if currentlist[0] == 'arg':
# it's of the format: arg num is val
# must have at least 4 args
if len(currentlist)<4:
raise ParseError, "Not enough tokens for 'arg'"
# second arg must be an integer value
try:
int(currentlist[1])
except ValueError:
raise ParseError, "invalid argument number '"+currentlist[1]+"'"
# third arg must be 'is'
if currentlist[2] != 'is':
raise ParseError, "arg missing 'is' keyword, instead found '"+currentlist[2]+"'"
# add this to the rule...
myrule.append(('arg',int(currentlist[1]),currentlist[3]))
# remove these tokens from the currentlist
currentlist = currentlist[4:]
continue
elif currentlist[0] == 'noargs':
# it's of the format: noargs is val
# must have at least 3 args
if len(currentlist)<3:
raise ParseError, "Not enough tokens for 'noargs'"
# second arg must be 'is'
if currentlist[1] != 'is':
raise ParseError, "arg missing 'is' keyword, instead found '"+currentlist[1]+"'"
# third arg must be an integer value
try:
int(currentlist[2])
except ValueError:
raise ParseError, "invalid argument number '"+currentlist[2]+"'"
# add this to the rule...
myrule.append(('noargs',int(currentlist[2])))
# remove these tokens from the currentlist
currentlist = currentlist[3:]
continue
else:
raise ParseError, "invalid rule type '"+currentlist[0]+"'"
return myrule
# This is a braindead parser. It's supposed to be readable and obviously
# correct, not "clever"
def init_restriction_tables(filename):
# create an empty rule for each call
for callname in known_calls:
call_rule_table[callname] = []
for line in open(filename):
# remove any comments
noncommentline = line.split('#')[0]
# the items are all separated by spaces
tokenlist = noncommentline.split()
if len(tokenlist) == 0:
# This was a blank or comment line
continue
# should be either a resource or a call line
if tokenlist[0] != 'resource' and tokenlist[0] != 'call':
raise ParseError, "Line '"+line+"' not understood in file '"+filename+"'"
if tokenlist[0] == 'resource':
####### Okay, it's a resource. It must have two other tokens!
if len(tokenlist) != 3:
raise ParseError, "Line '"+line+"' has wrong number of items in '"+filename+"'"
# and the second token must be a known resource
if tokenlist[1] not in nanny.known_resources:
raise ParseError, "Line '"+line+"' has an unknown resource '"+tokenlist[1]+"' in '"+filename+"'"
# and the last item should be a valid float
try:
float(tokenlist[2])
except ValueError:
raise ParseError, "Line '"+line+"' has an invalid resource value '"+tokenlist[2]+"' in '"+filename+"'"
# if it's an individual_item_resource, we'll handle it here...
if tokenlist[1] in nanny.individual_item_resources:
nanny.resource_restriction_table[tokenlist[1]].add(float(tokenlist[2]))
continue
# if normal that resource should not have been previously assigned
if tokenlist[1] in nanny.resource_restriction_table:
raise ParseError, "Line '"+line+"' has a duplicate resource rule for '"+tokenlist[1]+"' in '"+filename+"'"
# Finally, we assign it to the table
nanny.resource_restriction_table[tokenlist[1]] = float(tokenlist[2])
# Let's do the next line!
continue
elif tokenlist[0] == 'call':
################# it was a call...
# The second token should be a valid call name
if tokenlist[1] not in known_calls:
raise ParseError, "Line '"+line+"' has an unknown call '"+tokenlist[1]+"' in '"+filename+"'"
# The last token should be a valid action
if tokenlist[-1] not in valid_actions:
raise ParseError, "Line '"+line+"' has an unknown action '"+tokenlist[-1]+"' in '"+filename+"'"
# Get the rule for this action
try:
rule = get_rule(tokenlist[2:-1])
except ValueError, e:
raise ParseError, "Line '"+line+"' has error '"+str(e)+"' in '"+filename+"'"
# append a tuple containing the rule and action
call_rule_table[tokenlist[1]].append((rule, tokenlist[-1]))
# Let's do the next line!
continue
else:
raise ParseError, "Internal error for '"+line+"' in file '"+filename+"'"
# make sure that if there are required resources, they are defined
for resource in nanny.must_assign_resources:
if resource not in nanny.resource_restriction_table:
raise ParseError, "Missing required resource '"+resource+"' in file '"+filename+"'"
# give any remaining resources an entry with 0.0 as the value
for resource in nanny.known_resources:
if resource not in nanny.resource_restriction_table:
nanny.resource_restriction_table[resource] = 0.0
######################### Rule Parsing ##############################
# a rulelist looks like [ ('arg',2,'foo'), ('noargs',1), ... ]
def match_rule(rulelist, args):
# always match the empty rule
if rulelist == []:
return (True, '', 'Always match the empty rule')
# go through the rules. If we fail to match one then return False
for rule in rulelist:
# rule type is an 'arg': 'arg', pos, 'value'
if rule[0] == 'arg':
if len(args) <= rule[1]:
# Can't be a match, we don't have this arg
return (False, rule, 'Missing arg')
if str(args[rule[1]]) != rule[2]:
# Doesn't match the value specified
return (False, rule, 'Value not allowed')
# rule type is an 'noargs': 'noargs', count
elif rule[0] == 'noargs':
if len(args) != rule[1]:
# Wrong number of args
return (False, rule, 'Wrong args count')
# we must not have failed any rule...
return (True, '', 'All rules satisfied')
# rulesets look like: [ ([('arg',2,'foo'),('noargs',1)],'allow'), ([],'deny') ]
# I'm going to write this recursively
def find_action(ruleset, args):
# There wasn't a matching rule so deny
if ruleset == []:
return [('deny', '', 'No matching rule found')]
# this should be a tuple of two arguments, a rule and an action
thisrule, thisaction = ruleset[0]
match = match_rule(thisrule, args)
matched, matched_rule, reasoning = match
if matched:
return [(thisaction, matched_rule, reasoning)]
# do the recursive bit...
return find_action(ruleset[1:], args)+[match]
####################### Externally called bits ############################
# allow restrictions to be disabled to fix #919
disablerestrictions = False
def assertisallowed(call,*args):
if disablerestrictions:
return True
# let's pre-reject certain open / file calls
#print call_rule_table[call]
matches = find_action(call_rule_table[call], args)
action, matched_rule, reasoning = matches[0]
if action == 'allow':
return True
elif action == 'deny':
matches.reverse()
estr = "Call '"+str(call)+"' with args "+str(args)+" not allowed\n"
estr += "Matching dump:\n"
for action, matched_rule, reasoning in matches:
if matched_rule != '':
estr += "rule: "+str(matched_rule) + ", "
estr += str(reasoning) + "\n"
raise Exception, estr
elif action == 'prompt':
# would do an upcall here...
raise Exception, "Call '"+ str(call)+"' not allowed"
else:
# This will cause the program to exit and log things if logging is
# enabled. -Brent
tracebackrepy.handle_internalerror("find_action returned '" + str(action) +
"' for call '" + str(call) + "'", 31)
def init_restrictions(filename):
# Set up tables that list the rules and resource restrictions
init_restriction_tables(filename)
# This flushes and initializes the tables that track resource consumption
nanny.initialize_consumed_resource_tables()
# Start the nanny to check resource use...
nanny.start_resource_nanny()