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
|
/****************************************************************
* acm_core.c
*
* Copyright (C) 2005 IBM Corporation
*
* Author:
* Reiner Sailer <sailer@watson.ibm.com>
*
* Contributors:
* Stefan Berger <stefanb@watson.ibm.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, version 2 of the
* License.
*
* sHype access control module (ACM)
* This file handles initialization of the ACM
* as well as initializing/freeing security
* identifiers for domains (it calls on active
* policy hook functions).
*
*/
#include <xen/config.h>
#include <xen/errno.h>
#include <xen/types.h>
#include <xen/lib.h>
#include <xen/delay.h>
#include <xen/sched.h>
#include <xen/multiboot.h>
#include <acm/acm_hooks.h>
#include <acm/acm_endian.h>
/* debug:
* include/acm/acm_hooks.h defines a constant ACM_TRACE_MODE;
* define/undefine this constant to receive / suppress any
* security hook debug output of sHype
*
* include/public/acm.h defines a constant ACM_DEBUG
* define/undefine this constant to receive non-hook-related
* debug output.
*/
/* function prototypes */
void acm_init_chwall_policy(void);
void acm_init_ste_policy(void);
extern struct acm_operations acm_chinesewall_ops,
acm_simple_type_enforcement_ops, acm_null_ops;
/* global ACM policy (now dynamically determined at boot time) */
u16 acm_active_security_policy = ACM_POLICY_UNDEFINED;
/* global ops structs called by the hooks */
struct acm_operations *acm_primary_ops = NULL;
/* called in hook if-and-only-if primary succeeds */
struct acm_operations *acm_secondary_ops = NULL;
/* acm global binary policy (points to 'local' primary and secondary policies */
struct acm_binary_policy acm_bin_pol;
/* acm binary policy lock */
DEFINE_RWLOCK(acm_bin_pol_rwlock);
int
acm_set_policy_reference(u8 *buf, u32 buf_size)
{
struct acm_policy_reference_buffer *pr = (struct acm_policy_reference_buffer *)buf;
acm_bin_pol.policy_reference_name = (char *)xmalloc_array(u8, be32_to_cpu(pr->len));
if (!acm_bin_pol.policy_reference_name)
return -ENOMEM;
strlcpy(acm_bin_pol.policy_reference_name,
(char *)(buf + sizeof(struct acm_policy_reference_buffer)),
be32_to_cpu(pr->len));
printk("%s: Activating policy %s\n", __func__,
acm_bin_pol.policy_reference_name);
return 0;
}
int
acm_dump_policy_reference(u8 *buf, u32 buf_size)
{
struct acm_policy_reference_buffer *pr_buf = (struct acm_policy_reference_buffer *)buf;
int ret = sizeof(struct acm_policy_reference_buffer) + strlen(acm_bin_pol.policy_reference_name) + 1;
ret = (ret + 7) & ~7;
if (buf_size < ret)
return -EINVAL;
memset(buf, 0, ret);
pr_buf->len = cpu_to_be32(strlen(acm_bin_pol.policy_reference_name) + 1); /* including stringend '\0' */
strlcpy((char *)(buf + sizeof(struct acm_policy_reference_buffer)),
acm_bin_pol.policy_reference_name,
be32_to_cpu(pr_buf->len));
return ret;
}
int
acm_init_binary_policy(u32 policy_code)
{
int ret = ACM_OK;
acm_bin_pol.primary_policy_code = (policy_code & 0x0f);
acm_bin_pol.secondary_policy_code = (policy_code >> 4) & 0x0f;
write_lock(&acm_bin_pol_rwlock);
/* set primary policy component */
switch ((policy_code) & 0x0f)
{
case ACM_CHINESE_WALL_POLICY:
acm_init_chwall_policy();
acm_bin_pol.primary_policy_code = ACM_CHINESE_WALL_POLICY;
acm_primary_ops = &acm_chinesewall_ops;
break;
case ACM_SIMPLE_TYPE_ENFORCEMENT_POLICY:
acm_init_ste_policy();
acm_bin_pol.primary_policy_code = ACM_SIMPLE_TYPE_ENFORCEMENT_POLICY;
acm_primary_ops = &acm_simple_type_enforcement_ops;
break;
case ACM_NULL_POLICY:
acm_bin_pol.primary_policy_code = ACM_NULL_POLICY;
acm_primary_ops = &acm_null_ops;
break;
default:
/* Unknown policy not allowed primary */
ret = -EINVAL;
goto out;
}
/* secondary policy component part */
switch ((policy_code) >> 4)
{
case ACM_NULL_POLICY:
acm_bin_pol.secondary_policy_code = ACM_NULL_POLICY;
acm_secondary_ops = &acm_null_ops;
break;
case ACM_CHINESE_WALL_POLICY:
if (acm_bin_pol.primary_policy_code == ACM_CHINESE_WALL_POLICY)
{ /* not a valid combination */
ret = -EINVAL;
goto out;
}
acm_init_chwall_policy();
acm_bin_pol.secondary_policy_code = ACM_CHINESE_WALL_POLICY;
acm_secondary_ops = &acm_chinesewall_ops;
break;
case ACM_SIMPLE_TYPE_ENFORCEMENT_POLICY:
if (acm_bin_pol.primary_policy_code == ACM_SIMPLE_TYPE_ENFORCEMENT_POLICY)
{ /* not a valid combination */
ret = -EINVAL;
goto out;
}
acm_init_ste_policy();
acm_bin_pol.secondary_policy_code = ACM_SIMPLE_TYPE_ENFORCEMENT_POLICY;
acm_secondary_ops = &acm_simple_type_enforcement_ops;
break;
default:
ret = -EINVAL;
goto out;
}
out:
write_unlock(&acm_bin_pol_rwlock);
return ret;
}
int
acm_is_policy(char *buf, unsigned long len)
{
struct acm_policy_buffer *pol;
if (buf == NULL || len < sizeof(struct acm_policy_buffer))
return 0;
pol = (struct acm_policy_buffer *)buf;
return be32_to_cpu(pol->magic) == ACM_MAGIC;
}
static int
acm_setup(char *policy_start,
unsigned long policy_len)
{
int rc = ACM_OK;
struct acm_policy_buffer *pol;
if (policy_start == NULL || policy_len < sizeof(struct acm_policy_buffer))
return rc;
pol = (struct acm_policy_buffer *)policy_start;
if (be32_to_cpu(pol->magic) != ACM_MAGIC)
return rc;
rc = do_acm_set_policy((void *)policy_start, (u32)policy_len);
if (rc == ACM_OK)
{
printkd("Policy len 0x%lx, start at %p.\n",policy_len,policy_start);
}
else
{
printk("Invalid policy.\n");
/* load default policy later */
acm_active_security_policy = ACM_POLICY_UNDEFINED;
}
return rc;
}
int
acm_init(char *policy_start,
unsigned long policy_len)
{
int ret = ACM_OK;
/* first try to load the boot policy (uses its own locks) */
acm_setup(policy_start, policy_len);
if (acm_active_security_policy != ACM_POLICY_UNDEFINED)
{
printk("%s: Enforcing %s boot policy.\n", __func__,
ACM_POLICY_NAME(acm_active_security_policy));
goto out;
}
/* else continue with the minimal hardcoded default startup policy */
printk("%s: Loading default policy (%s).\n",
__func__, ACM_POLICY_NAME(ACM_DEFAULT_SECURITY_POLICY));
if (acm_init_binary_policy(ACM_DEFAULT_SECURITY_POLICY)) {
ret = -EINVAL;
goto out;
}
acm_active_security_policy = ACM_DEFAULT_SECURITY_POLICY;
if (acm_active_security_policy != ACM_NULL_POLICY)
acm_bin_pol.policy_reference_name = "DEFAULT";
else
acm_bin_pol.policy_reference_name = "NULL";
out:
if (ret != ACM_OK)
{
printk("%s: Error initializing policies.\n", __func__);
/* here one could imagine a clean panic */
return -EINVAL;
}
return ret;
}
int
acm_init_domain_ssid(domid_t id, ssidref_t ssidref)
{
struct acm_ssid_domain *ssid;
struct domain *subj = get_domain_by_id(id);
int ret1, ret2;
if (subj == NULL)
|