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
|
-- Copyright (C) 2007 Lauri Leukkunen <lle@rahina.org>
-- Portion Copyright (c) 2008 Nokia Corporation.
-- (exec postprocessing code written by Lauri T. Aarnio at Nokia)
--
-- Licensed under MIT license
argvmods = {}
-- only map gcc & friends if a cross compiler has been defined,
-- and it has not been disabled by the mapping rules:
if (sbox_cross_gcc_dir ~= nil and sbox_cross_gcc_dir ~= "" and
enable_cross_gcc_toolchain == true) then
-- this generates gcc related argv/envp manglings
do_file(session_dir .. "/lua_scripts/argvenvp_gcc.lua")
end
-- regular mangling rules go here
-- syntax is of the form:
--
-- rule = {
-- name = "binary-name",
-- add_head = {"list", "of", "args", "to", "prepend"},
-- add_tail = {"these", "are", "appended"},
-- remove = {"args", "to", "remove"},
-- new_filename = "exec-this-binary-instead",
-- disable_mapping = 1 -- set this to disable mappings
-- }
-- argvmods[rule.name] = rule
--
-- Environment modifications are not supported yet, except for disabling
-- mappings.
--
-- TODO:
--
-- * new_filename should probably be replaced by integrating argv/envp
-- mangling with the path mapping machinery.
dpkg_architecture = {
name = "dpkg-architecture",
remove = {"-f"}
}
argvmods[dpkg_architecture.name] = dpkg_architecture
-- ------------------------------------
-- Exec preprocessing.
-- function sb_execve_preprocess is called to decide WHAT FILE
-- should be started (see description of the algorithm in sb_exec.c)
-- (this also typically adds, deletes, or modifies arguments whenever needed)
--
-- returns: err, file, argc, argv, envc, envp
-- (zero as "err" means "OK")
function sbox_execve_preprocess(filename, argv, envp)
local new_argv = {}
local new_envp = {}
local binaryname = string.match(filename, "[^/]+$")
local new_filename = filename
-- print(string.format("sbox_execve_preprocess(): %s\n", filename))
new_envp = envp
am = argvmods[binaryname]
if (am and not am.remove) then am.remove = {} end
if (am and not am.add_head) then am.add_head = {} end
if (am and not am.add_tail) then am.add_tail = {} end
if (am ~= nil) then
-- head additions
for i = 1, table.maxn(am.add_head) do
table.insert(new_argv, am.add_head[i])
end
-- populate new_argv, skip those that are to be removed
for i = 1, table.maxn(argv) do
local match = 0
for j = 1, table.maxn(am.remove) do
if (argv[i] == am.remove[j]) then
match = 1
end
end
if (match == 0) then
table.insert(new_argv, argv[i])
end
end
-- tail additions
for i = 1, table.maxn(am.add_tail) do
table.insert(new_argv, am.add_tail[i])
end
if (am.new_filename) then
-- print(string.format("changing to: %s\n", am.new_filename))
new_filename = am.new_filename
new_argv[1] = am.new_filename
end
if (am.disable_mapping) then
table.insert(new_envp, "SBOX_DISABLE_MAPPING=1")
table.insert(new_envp, "SBOX_DISABLE_ARGVENVP=1")
end
else
new_argv = argv
end
return 0, new_filename, #new_argv, new_argv, #new_envp, new_envp
end
-- ------------------------------------
-- Exec postprocessing.
-- function sb_execve_postprocess is called to decide HOW the executable
-- should be started (see description of the algorithm in sb_exec.c)
--
-- returns: status, mapped_file, file, argc, argv, envc, envp
-- "status":
-- -1 = do not execute.
-- 0 = argc&argv were updated, OK to execute with the new params
-- 1 = ok to exec directly with orig.arguments
function sb_execve_postprocess_native_executable(rule, exec_policy,
exec_type, mapped_file, filename, argv, envp)
-- Native binary. See what we need to do with it...
sb.log("debug", string.format("sb_execve_postprocess: Native binary"))
sb.log("debug", "Rule: apply exec_policy")
if (rule.prefix ~= nil) then
sb.log("debug", string.format("rule.prefix=%s", rule.prefix))
end
if (rule.path ~= nil) then
sb.log("debug", string.format("rule.path=%s", rule.path))
end
local new_argv = {}
local new_envp = envp
local new_filename = filename
local new_mapped_file = mapped_file
-- by default, copy argv from index 1 (refers to argv[0])
local first_argv_element_to_copy = 1
local updated_args = 0
if (exec_policy.native_app_ld_so ~= nil) then
-- we need to use ld.so for starting the binary,
-- instead of starting it directly:
new_mapped_file = exec_policy.native_app_ld_so
table.insert(new_argv, exec_policy.native_app_ld_so)
if (exec_policy.native_app_ld_library_path ~= nil) then
table.insert(new_argv, "--library-path")
table.insert(new_argv, exec_policy.native_app_ld_library_path)
end
-- NOTE/WARNING: The default ld.so (ld-linux.so) will loose
-- argv[0], when the binary is executed by ld.so's
-- command line (which we will be doing). It will always copy
-- the filename to argv[0].
--
-- We now have a patch for ld.so which introduces a new
-- option, "--argv0 argument", and a flag is used to tell
-- if a patched ld.so is available (the "sb2" script finds
-- that out during startup phase).
--
if (exec_policy.native_app_ld_so_supports_argv0) then
table.insert(new_argv, "--argv0")
-- C's argv[0] is in argv[1] here!
table.insert(new_argv, argv[1])
table.insert(new_argv, mapped_file)
else
-- Replace argv[0] by pathname:
table.insert(new_argv, mapped_file)
end
first_argv_element_to_copy = 2
updated_args = 1
elseif (exec_policy.native_app_ld_library_path ~= nil) then
-- Start the binary with a nonstandard LD_LIBRARY_PATH
local lib_path_found = 0
for j = 1, table.maxn(new_envp) do
if (string.match(new_envp[j], "^LD_LIBRARY_PATH=")) then
new_envp[j] = exec_policy.native_app_ld_library_path
sb.log("debug", string.format(
"Replaced LD_LIBRARY_PATH=%s",
new_envp[j]))
local lib_path_found = 1
end
end
if (lib_path_found == 0) then
table.insert(new_envp,
"LD_LIBRARY_PATH="..exec_policy.native_app_ld_library_path)
sb.log("debug", "Added LD_LIBRARY_PATH")
end
updated_args = 1
end
if (updated_args == 1) then
-- Add components from original argv[]
local i
for i = first_argv_element_to_copy, table.maxn(argv) do
table.insert(new_argv, argv[i])
end
return 0, new_mapped_file, new_filename, #new_argv, new_argv, #new_envp, new_envp
end
-- else args not modified.
return 1, mapped_file, filename, #argv, argv, #envp, envp
end
if string.match(sbox_cputransparency_method, "qemu") then
cputransparency_method_is_qemu = true
end
function sb_execve_postprocess_cpu_transparency_executable(rule, exec_policy,
exec_type, mapped_file, filename, argv, envp)
sb.log("debug", "postprocessing cpu_transparency for " .. filename)
if cputransparency_method_is_qemu then
local new_envp = {}
local new_argv = {}
local new_filename = sbox_cputransparency_method
new_argv[1] = sbox_cputransparency_method
-- drop LD_PRELOAD env.var.
new_argv[2] = "-drop-ld-preload"
-- target runtime linker comes from /
new_argv[3] = "-L"
new_argv[4] = "/"
if conf_cputransparency_has_argv0_flag then
-- set target argv[0]
new_argv[5] = "-0"
new_argv[6] = argv[1]
end
if conf_cputransparency_qemu_has_env_control_flags then
for i = 1, #envp do
-- drop LD_TRACE_ from target environment
if not string.match(envp[i], "^LD_TRACE_.*") then
table.insert(new_envp, envp[i])
else
-- .. and move it to qemu command line
table.insert(new_argv, "-E")
table.insert(new_argv, envp[i])
end
end
else
-- copy environment. Some things will be broken with
-- this qemu (for example, prelinking won't work)
new_envp = envp
end
-- unmapped file is exec'd
table.insert(new_argv, filename)
--
-- Append arguments for target process (skip argv[0]
-- as this is done using -0 switch).
--
for i = 2, #argv do
table.insert(new_argv, argv[i])
end
-- environment&args were changed
return 0, new_filename, filename, #new_argv, new_argv,
#new_envp, new_envp
-- FIXME: here we should have "elseif cputransparency_method_is_sbrsh"..
end
-- no changes
return 1, mapped_file, filename, #argv, argv, #envp, envp
end
-- This is called from C:
function sb_execve_postprocess(rule, exec_policy, exec_type,
mapped_file, filename, binaryname, argv, envp)
-- First, if either rule or the exec policy is a string, something
-- has failed during the previous steps or mapping has been disabled;
-- in both cases postprocessing is not needed, exec must be allowed.
if ((type(rule) == "string") or (type(exec_policy) == "string")) then
local rs = ""
local eps = ""
if (type(rule) == "string") then
rs = rule
end
if (type(exec_policy) == "string") then
eps = exec_policy
end
sb.log("debug", "sb_execve_postprocess: "..rs..";"..eps..
" (going to exec with orig.args)")
return 1, mapped_file, filename, #argv, argv, #envp, envp
end
-- if exec_policy was not provided by the caller (i.e. not
-- provided by the mapping rule), look up the policy from
-- exec_policy_chains array.
if (exec_policy == nil) then
local rule = nil
local chain = nil
sb.log("debug", "trying exec_policy_chains..")
chain = find_chain(active_mode_exec_policy_chains, binaryname)
if (chain ~= nil) then
sb.log("debug", "chain found, find rule for "..mapped_file)
rule = find_rule(chain, func_name, mapped_file)
end
if (rule ~= nil) then
sb.log("debug", "rule found..")
exec_policy = rule.exec_policy
end
if (exec_policy == nil) then
-- there is no default policy for this mode
sb.log("notice",
"sb_execve_postprocess: No exec_policy for "..filename)
return 1, mapped_file, filename, #argv, argv, #envp, envp
end
end
-- Exec policy found.
if (exec_policy.log_level ~= nil) then
sb.log(exec_policy.log_level, exec_policy.log_message)
end
if (exec_policy.deny_exec == true) then
return -1, mapped_file, filename, #argv, argv, #envp, envp
end
if (exec_policy.name == nil) then
sb.log("debug", "Applying nameless exec_policy")
else
sb.log("debug", string.format("Applying exec_policy '%s'",
exec_policy.name))
end
sb.log("debug", string.format("sb_execve_postprocess:type=%s",
exec_type))
-- End of generic part. Rest of postprocessing depends on type of
-- the executable.
if (exec_type == "native") then
return sb_execve_postprocess_native_executable(rule,
exec_policy, exec_type, mapped_file,
filename, argv, envp)
elseif (exec_type == "cpu_transparency") then
return sb_execve_postprocess_cpu_transparency_executable(rule,
exec_policy, exec_type, mapped_file,
filename, argv, envp)
else
-- all other exec_types: allow exec with orig.args
return 1, mapped_file, filename, #argv, argv, #envp, envp
end
end
|