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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
-- Scratchbox2 universal redirector dynamic path translation scripts
-- Copyright (C) 2006, 2007 Lauri Leukkunen
-- Licensed under MIT license.
-- escape_string() prefixes the magic pattern matching
-- characters ^$()%.[]*+-?) with '%'
function escape_string(a)
b = ""
for i = 1, string.len(a) do
c = string.sub(a, i, i)
-- escape the magic chars
if (c == "^" or
c == "$" or
c == "(" or
c == ")" or
c == "%" or
c == "." or
c == "[" or
c == "]" or
c == "*" or
c == "+" or
c == "-" or
c == "?") then
b = b .. "%"
end
b = b .. c
end
return b
end
function basename(path)
if (path == "/") then
return "/"
else
return string.match(path, "[^/]*$")
end
end
function dirname(path)
if (path == "/") then
return "/"
end
dir = string.match(path, ".*/")
if (dir == nil) then
return "."
end
if (dir == "/") then return dir end
-- chop off the trailing /
if (string.sub(dir, string.len(dir)) == "/") then
dir = string.sub(dir, 1, string.len(dir) - 1)
end
return dir
end
function isprefix(a, b)
if (not a or not b) then return false end
return string.sub(b, 1, string.len(a)) == a
end
-- Load mode-specific rules.
-- A mode file should define two variables:
-- 1. export_chains (array) contains mapping rule chains; this array
-- is searched sequentially with the original (unmapped) path as the key
-- 2. exec_policy_chains (array) contains default execution policies;
-- real path (mapped path) is used as the key.
--
function load_and_check_rules()
export_chains = {}
exec_policy_chains = {}
do_file(session_dir .. "/rules.lua")
-- export_chains variable contains now the mapping rule chains
-- from the chunk
for i = 1,table.maxn(export_chains) do
-- fill in the default values
if (not export_chains[i].rules) then
export_chains[i].rules = {}
end
-- loop through the rules
for r = 1, table.maxn(export_chains[i].rules) do
export_chains[i].rules[r].lua_script = filename
if (export_chains[i].binary) then
export_chains[i].rules[r].binary_name = export_chains[i].binary
else
export_chains[i].rules[r].binary_name = "nil"
end
if (export_chains[i].rules[r].name == nil) then
export_chains[i].rules[r].name =
string.format("rule:%d.%d",
i, r)
end
end
export_chains[i].lua_script = filename
table.insert(active_mode_mapping_rule_chains, export_chains[i])
end
-- Handle exec_policy_chains variable from the chunk
for i = 1,table.maxn(exec_policy_chains) do
table.insert(active_mode_exec_policy_chains,
exec_policy_chains[i])
end
end
target_root = sbox_target_root
if (not target_root or target_root == "") then
target_root = "/"
end
tools_root = sbox_tools_root
if (tools_root == "") then
tools_root = nil
end
-- make versions of tools_root and target_root safe
-- to use in match() functions
if (tools_root) then
esc_tools_root = escape_string(tools_root)
end
if (target_root) then
esc_target_root = escape_string(target_root)
end
active_mode_mapping_rule_chains = {}
active_mode_exec_policy_chains = {}
load_and_check_rules()
function sbox_execute_replace_rule(path, replacement, rule)
local ret = nil
if (debug_messages_enabled) then
sb.log("debug", string.format("replace:%s:%s", path, replacement))
end
if (rule.prefix) then
-- "path" may be shorter than prefix during path resolution
if ((rule.prefix ~= "") and
(isprefix(rule.prefix, path))) then
ret = replacement .. string.sub(path, string.len(rule.prefix)+1)
if (debug_messages_enabled) then
sb.log("debug", string.format("replaced (prefix) => %s", ret))
end
else
ret = ""
if (debug_messages_enabled) then
sb.log("debug", string.format("replacement failed (short path?)"))
end
end
elseif (rule.path) then
-- "path" may be shorter than prefix during path resolution
if (rule.path == path) then
ret = replacement
if (debug_messages_enabled) then
sb.log("debug", string.format("replaced (path) => %s", ret))
end
else
ret = ""
if (debug_messages_enabled) then
sb.log("debug", string.format("replacement failed (short path?)"))
end
end
else
sb.log("error", "error in rule: can't replace without 'prefix' or 'path'")
ret = path
end
return ret
end
-- returns path and readonly_flag
function sbox_execute_conditional_actions(binary_name,
func_name, work_dir, rp, path, rule)
local actions = rule.actions
local a
for a = 1, table.maxn(actions) do
if (debug_messages_enabled) then
sb.log("debug", string.format("try %d", a))
end
local ret_ro = false
if (actions[a].readonly) then
ret_ro = actions[a].readonly
end
-- first, if there are any unconditional actions:
if (actions[a].use_orig_path) then
return path, ret_ro
elseif (actions[a].map_to) then
return actions[a].map_to .. path, ret_ro
end
-- next try conditional destinations: build a path to
-- "tmp_dest", and if that destination exists, use that path.
local tmp_dest = nil
if (actions[a].if_exists_then_map_to) then
tmp_dest = actions[a].if_exists_then_map_to .. path
elseif (actions[a].if_exists_then_replace_by) then
tmp_dest = sbox_execute_replace_rule(path,
actions[a].if_exists_then_replace_by, rule)
end
if (tmp_dest ~= nil) then
if (sb.path_exists(tmp_dest)) then
if (debug_messages_enabled) then
sb.log("debug", string.format("target exists: => %s", tmp_dest))
end
return tmp_dest, ret_ro
end
else
sb.log("error", string.format("error in rule: no valid conditional actions for '%s'", path))
end
end
-- no valid action found. This should not happen.
sb.log("error", string.format("mapping rule for '%s': execution of conditional actions failed", path))
return path, false
end
-- returns exec_policy, path and readonly_flag
function sbox_execute_rule(binary_name, func_name, work_dir, rp, path, rule)
local ret_exec_policy = nil
local ret_path = nil
local ret_ro = false
local rule_name
if (rule.readonly ~= nil) then
ret_ro = rule.readonly
end
if (rule.exec_policy ~= nil) then
ret_exec_policy = rule.exec_policy
end
if (rule.use_orig_path) then
ret_path = path
elseif (rule.actions) then
-- FIXME: sbox_execute_conditional_actions should also
-- be able to return exec_policy
ret_path, ret_ro = sbox_execute_conditional_actions(binary_name,
func_name, work_dir, rp, path, rule)
elseif (rule.map_to) then
if (rule.map_to == "/") then
ret_path = path
else
ret_path = rule.map_to .. path
end
elseif (rule.replace_by) then
ret_path = sbox_execute_replace_rule(path, rule.replace_by, rule)
else
ret_path = path
if (rule.name) then
rule_name = rule.name
else
rule_name = "(no name)"
end
sb.log("error", string.format("mapping rule '%s' does not "..
"have any valid actions, path=%s", rule_name, path))
end
return ret_exec_policy, ret_path, ret_ro
end
-- returns rule and min_path_len, minimum length which is needed for
-- successfull mapping.
function find_rule(chain, func, full_path)
local i = 0
local wrk = chain
local min_path_len = 0
if (debug_messages_enabled) then
sb.log("noise", string.format("find_rule for (%s)", full_path))
end
while (wrk) do
-- travel the chains
for i = 1, table.maxn(wrk.rules) do
-- loop the rules in a chain
if ((not wrk.rules[i].func_name
or string.match(func, wrk.rules[i].func_name))) then
-- "prefix" rules:
-- compare prefix (only if a non-zero prefix)
if (wrk.rules[i].prefix and
(wrk.rules[i].prefix ~= "") and
(isprefix(wrk.rules[i].prefix, full_path))) then
if (debug_messages_enabled) then
sb.log("noise", string.format("selected prefix rule %d (%s)", i, wrk.rules[i].prefix))
end
min_path_len = string.len(wrk.rules[i].prefix)
return wrk.rules[i], min_path_len
end
-- "path" rules: (exact match)
if (wrk.rules[i].path == full_path) then
if (debug_messages_enabled) then
sb.log("noise", string.format("selected path rule %d (%s)", i, wrk.rules[i].path))
end
min_path_len = string.len(wrk.rules[i].path)
return wrk.rules[i], min_path_len
end
-- "match" rules use a lua "regexp".
-- these will be obsoleted, as this kind of rule
-- is almost impossible to reverse (backward mapping
-- is not possible as long as there are "match" rules)
if (wrk.rules[i].match) then
if (string.match(full_path, wrk.rules[i].match)) then
if (debug_messages_enabled) then
sb.log("noise", string.format("selected match rule %d (%s)", i, wrk.rules[i].match))
end
-- there is no easy and reliable
-- way to determine min_path_len
-- so leave it to zero
return wrk.rules[i], min_path_len
end
end
-- FIXME: Syntax checking should be added:
-- it should be tested that exactly one of
-- "prefix","path" or "match" was present
end
end
wrk = wrk.next_chain
end
if (debug_messages_enabled) then
sb.log("noise", string.format("rule not found"))
end
return nil, 0
end
-- returns the same values as sbox_translate_path
-- (rule,exec_policy,path,ro_flag):
function map_using_rule(rule, binary_name, func_name, work_dir, path)
local ret = path
local rp = path
local readonly_flag = false
local exec_policy = nil
if (not rule) then
-- error, not even a default rule found
sb.log("error", string.format("Unable to find a match at all: %s(%s)", func_name, path))
return nil, nil, path, readonly_flag
end
if (debug_messages_enabled) then
sb.log("noise", string.format("map:%s:%s", work_dir, path))
end
if (rule.log_level) then
if (rule.log_message) then
sb.log(rule.log_level, string.format("%s (%s)",
rule.log_message, path))
else
-- default message = log path
sb.log(rule.log_level, string.format("path=(%s)", path))
end
end
if (rule.custom_map_func ~= nil) then
ret = rule.custom_map_func(binary_name, func_name, work_dir, rp, path, rules[n])
if (rule.readonly ~= nil) then
readonly_flag = rule.readonly
end
else
exec_policy, ret, readonly_flag = sbox_execute_rule(binary_name, func_name, work_dir, rp, path, rule)
end
return rule, exec_policy, ret, readonly_flag
end
-- sbox_translate_path is the function called from libsb2.so
-- preload library and the FUSE system for each path that needs
-- translating.
--
-- returns:
-- 1. the rule used to perform the mapping
-- 2. exec_policy
-- 3. path (mapping result)
-- 4. "readonly" flag
function sbox_translate_path(rule, binary_name, func_name, work_dir, path)
return map_using_rule(rule, binary_name, func_name, work_dir, path)
end
function find_chain(chains_table, binary_name)
local n
for n=1,table.maxn(chains_table) do
if (not chains_table[n].noentry
and (not chains_table[n].binary
or binary_name == chains_table[n].binary)) then
return(chains_table[n])
end
end
end
-- sbox_get_mapping_requirements is called from libsb2.so before
-- path resolution takes place. The primary purpose of this is to
-- determine where to start resolving symbolic links; shorter paths than
-- "min_path_len" should not be given to sbox_translate_path()
-- returns "rule", "rule_found", "min_path_len"
function sbox_get_mapping_requirements(binary_name, func_name, work_dir, full_path)
-- loop through the chains, first match is used
local min_path_len = 0
local rule = nil
local chain
chain = find_chain(active_mode_mapping_rule_chains, binary_name)
if (chain == nil) then
sb.log("error", string.format("Unable to find chain for: %s(%s)",
func_name, full_path))
return nil, false, 0
end
rule, min_path_len = find_rule(chain, func_name, full_path)
if (not rule) then
-- error, not even a default rule found
sb.log("error", string.format("Unable to find rule for: %s(%s)", func_name, full_path))
return nil, false, 0
end
return rule, true, min_path_len
end
|