ia64/xen-unstable

view xen/acm/acm_core.c @ 6538:84ee014ebd41

Merge xen-vtx-unstable.hg
author adsharma@los-vmm.sc.intel.com
date Wed Aug 17 12:34:38 2005 -0800 (2005-08-17)
parents 23979fb12c49 f294acb25858
children 99914b54f7bf
line source
1 /****************************************************************
2 * acm_core.c
3 *
4 * Copyright (C) 2005 IBM Corporation
5 *
6 * Author:
7 * Reiner Sailer <sailer@watson.ibm.com>
8 *
9 * Contributors:
10 * Stefan Berger <stefanb@watson.ibm.com>
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation, version 2 of the
15 * License.
16 *
17 * sHype access control module (ACM)
18 * This file handles initialization of the ACM
19 * as well as initializing/freeing security
20 * identifiers for domains (it calls on active
21 * policy hook functions).
22 *
23 */
25 #include <xen/config.h>
26 #include <xen/errno.h>
27 #include <xen/types.h>
28 #include <xen/lib.h>
29 #include <xen/delay.h>
30 #include <xen/sched.h>
31 #include <xen/multiboot.h>
32 #include <acm/acm_hooks.h>
33 #include <acm/acm_endian.h>
35 /* debug:
36 * include/acm/acm_hooks.h defines a constant ACM_TRACE_MODE;
37 * define/undefine this constant to receive / suppress any
38 * security hook debug output of sHype
39 *
40 * include/public/acm.h defines a constant ACM_DEBUG
41 * define/undefine this constant to receive non-hook-related
42 * debug output.
43 */
45 /* function prototypes */
46 void acm_init_chwall_policy(void);
47 void acm_init_ste_policy(void);
49 extern struct acm_operations acm_chinesewall_ops,
50 acm_simple_type_enforcement_ops, acm_null_ops;
52 /* global ops structs called by the hooks */
53 struct acm_operations *acm_primary_ops = NULL;
54 /* called in hook if-and-only-if primary succeeds */
55 struct acm_operations *acm_secondary_ops = NULL;
57 /* acm global binary policy (points to 'local' primary and secondary policies */
58 struct acm_binary_policy acm_bin_pol;
59 /* acm binary policy lock */
60 rwlock_t acm_bin_pol_rwlock = RW_LOCK_UNLOCKED;
62 /* until we have endian support in Xen, we discover it at runtime */
63 u8 little_endian = 1;
64 void acm_set_endian(void)
65 {
66 u32 test = 1;
67 if (*((u8 *)&test) == 1) {
68 printk("ACM module running in LITTLE ENDIAN.\n");
69 little_endian = 1;
70 } else {
71 printk("ACM module running in BIG ENDIAN.\n");
72 little_endian = 0;
73 }
74 }
76 #if (ACM_USE_SECURITY_POLICY != ACM_NULL_POLICY)
78 /* initialize global security policy for Xen; policy write-locked already */
79 static void
80 acm_init_binary_policy(void *primary, void *secondary)
81 {
82 acm_bin_pol.primary_policy_code = 0;
83 acm_bin_pol.secondary_policy_code = 0;
84 acm_bin_pol.primary_binary_policy = primary;
85 acm_bin_pol.secondary_binary_policy = secondary;
86 }
88 static int
89 acm_setup(unsigned int *initrdidx,
90 const multiboot_info_t *mbi,
91 unsigned long initial_images_start)
92 {
93 int i;
94 module_t *mod = (module_t *)__va(mbi->mods_addr);
95 int rc = ACM_OK;
97 if (mbi->mods_count > 1)
98 *initrdidx = 1;
100 /*
101 * Try all modules and see whichever could be the binary policy.
102 * Adjust the initrdidx if module[1] is the binary policy.
103 */
104 for (i = mbi->mods_count-1; i >= 1; i--) {
105 struct acm_policy_buffer *pol;
106 char *_policy_start;
107 unsigned long _policy_len;
108 #if defined(__i386__)
109 _policy_start = (char *)(initial_images_start + (mod[i].mod_start-mod[0].mod_start));
110 #elif defined(__x86_64__)
111 _policy_start = __va(initial_images_start + (mod[i].mod_start-mod[0].mod_start));
112 #else
113 #error Architecture unsupported by sHype
114 #endif
115 _policy_len = mod[i].mod_end - mod[i].mod_start;
116 if (_policy_len < sizeof(struct acm_policy_buffer))
117 continue; /* not a policy */
119 pol = (struct acm_policy_buffer *)_policy_start;
120 if (ntohl(pol->magic) == ACM_MAGIC) {
121 rc = acm_set_policy((void *)_policy_start,
122 (u16)_policy_len,
123 0);
124 if (rc == ACM_OK) {
125 printf("Policy len 0x%lx, start at %p.\n",_policy_len,_policy_start);
126 if (i == 1) {
127 if (mbi->mods_count > 2) {
128 *initrdidx = 2;
129 } else {
130 *initrdidx = 0;
131 }
132 } else {
133 *initrdidx = 1;
134 }
135 break;
136 } else {
137 printk("Invalid policy. %d.th module line.\n", i+1);
138 }
139 } /* end if a binary policy definition, i.e., (ntohl(pol->magic) == ACM_MAGIC ) */
140 }
141 return rc;
142 }
145 int
146 acm_init(unsigned int *initrdidx,
147 const multiboot_info_t *mbi,
148 unsigned long initial_images_start)
149 {
150 int ret = -EINVAL;
152 acm_set_endian();
153 write_lock(&acm_bin_pol_rwlock);
155 if (ACM_USE_SECURITY_POLICY == ACM_CHINESE_WALL_POLICY) {
156 acm_init_binary_policy(NULL, NULL);
157 acm_init_chwall_policy();
158 acm_bin_pol.primary_policy_code = ACM_CHINESE_WALL_POLICY;
159 acm_primary_ops = &acm_chinesewall_ops;
160 acm_bin_pol.secondary_policy_code = ACM_NULL_POLICY;
161 acm_secondary_ops = &acm_null_ops;
162 ret = ACM_OK;
163 } else if (ACM_USE_SECURITY_POLICY == ACM_SIMPLE_TYPE_ENFORCEMENT_POLICY) {
164 acm_init_binary_policy(NULL, NULL);
165 acm_init_ste_policy();
166 acm_bin_pol.primary_policy_code = ACM_SIMPLE_TYPE_ENFORCEMENT_POLICY;
167 acm_primary_ops = &acm_simple_type_enforcement_ops;
168 acm_bin_pol.secondary_policy_code = ACM_NULL_POLICY;
169 acm_secondary_ops = &acm_null_ops;
170 ret = ACM_OK;
171 } else if (ACM_USE_SECURITY_POLICY == ACM_CHINESE_WALL_AND_SIMPLE_TYPE_ENFORCEMENT_POLICY) {
172 acm_init_binary_policy(NULL, NULL);
173 acm_init_chwall_policy();
174 acm_init_ste_policy();
175 acm_bin_pol.primary_policy_code = ACM_CHINESE_WALL_POLICY;
176 acm_primary_ops = &acm_chinesewall_ops;
177 acm_bin_pol.secondary_policy_code = ACM_SIMPLE_TYPE_ENFORCEMENT_POLICY;
178 acm_secondary_ops = &acm_simple_type_enforcement_ops;
179 ret = ACM_OK;
180 } else if (ACM_USE_SECURITY_POLICY == ACM_NULL_POLICY) {
181 acm_init_binary_policy(NULL, NULL);
182 acm_bin_pol.primary_policy_code = ACM_NULL_POLICY;
183 acm_primary_ops = &acm_null_ops;
184 acm_bin_pol.secondary_policy_code = ACM_NULL_POLICY;
185 acm_secondary_ops = &acm_null_ops;
186 ret = ACM_OK;
187 }
188 write_unlock(&acm_bin_pol_rwlock);
190 if (ret != ACM_OK)
191 return -EINVAL;
192 acm_setup(initrdidx, mbi, initial_images_start);
193 printk("%s: Enforcing Primary %s, Secondary %s.\n", __func__,
194 ACM_POLICY_NAME(acm_bin_pol.primary_policy_code), ACM_POLICY_NAME(acm_bin_pol.secondary_policy_code));
195 return ret;
196 }
199 #endif
201 int
202 acm_init_domain_ssid(domid_t id, ssidref_t ssidref)
203 {
204 struct acm_ssid_domain *ssid;
205 struct domain *subj = find_domain_by_id(id);
206 int ret1, ret2;
208 if (subj == NULL) {
209 printk("%s: ACM_NULL_POINTER ERROR (id=%x).\n", __func__, id);
210 return ACM_NULL_POINTER_ERROR;
211 }
212 if ((ssid = xmalloc(struct acm_ssid_domain)) == NULL)
213 return ACM_INIT_SSID_ERROR;
215 ssid->datatype = DOMAIN;
216 ssid->subject = subj;
217 ssid->domainid = subj->domain_id;
218 ssid->primary_ssid = NULL;
219 ssid->secondary_ssid = NULL;
221 if (ACM_USE_SECURITY_POLICY != ACM_NULL_POLICY)
222 ssid->ssidref = ssidref;
223 else
224 ssid->ssidref = ACM_DEFAULT_SSID;
226 subj->ssid = ssid;
227 /* now fill in primary and secondary parts; we only get here through hooks */
228 if (acm_primary_ops->init_domain_ssid != NULL)
229 ret1 = acm_primary_ops->init_domain_ssid(&(ssid->primary_ssid), ssidref);
230 else
231 ret1 = ACM_OK;
233 if (acm_secondary_ops->init_domain_ssid != NULL)
234 ret2 = acm_secondary_ops->init_domain_ssid(&(ssid->secondary_ssid), ssidref);
235 else
236 ret2 = ACM_OK;
238 if ((ret1 != ACM_OK) || (ret2 != ACM_OK)) {
239 printk("%s: ERROR instantiating individual ssids for domain 0x%02x.\n",
240 __func__, subj->domain_id);
241 acm_free_domain_ssid(ssid);
242 put_domain(subj);
243 return ACM_INIT_SSID_ERROR;
244 }
245 printk("%s: assigned domain %x the ssidref=%x.\n", __func__, id, ssid->ssidref);
246 put_domain(subj);
247 return ACM_OK;
248 }
251 int
252 acm_free_domain_ssid(struct acm_ssid_domain *ssid)
253 {
254 domid_t id;
256 /* domain is already gone, just ssid is left */
257 if (ssid == NULL) {
258 printk("%s: ACM_NULL_POINTER ERROR.\n", __func__);
259 return ACM_NULL_POINTER_ERROR;
260 }
261 id = ssid->domainid;
262 ssid->subject = NULL;
264 if (acm_primary_ops->free_domain_ssid != NULL) /* null policy */
265 acm_primary_ops->free_domain_ssid(ssid->primary_ssid);
266 ssid->primary_ssid = NULL;
267 if (acm_secondary_ops->free_domain_ssid != NULL)
268 acm_secondary_ops->free_domain_ssid(ssid->secondary_ssid);
269 ssid->secondary_ssid = NULL;
270 xfree(ssid);
271 printkd("%s: Freed individual domain ssid (domain=%02x).\n",__func__, id);
272 return ACM_OK;
273 }