aboutsummaryrefslogtreecommitdiffstats
path: root/xen/acm/acm_core.c
blob: fe5bacdb6d324f89ff7a57b2bc26c19badf0ad50 (plain)
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
/****************************************************************
 * acm_core.c
 * 
 * Copyright (C) 2005 IBM Corporation
 *
 * Author:
 * Reiner Sailer <sailer@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 <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 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 */
rwlock_t acm_bin_pol_rwlock = RW_LOCK_UNLOCKED;

/* until we have endian support in Xen, we discover it at runtime */
u8 little_endian = 1;
void acm_set_endian(void)
{
    u32 test = 1;
    if (*((u8 *)&test) == 1) {
      	printk("ACM module running in LITTLE ENDIAN.\n");
	little_endian = 1;
    } else {
	printk("ACM module running in BIG ENDIAN.\n");
	little_endian = 0;
    }
}

/* initialize global security policy for Xen; policy write-locked already */
static void
acm_init_binary_policy(void *primary, void *secondary)
{
	acm_bin_pol.primary_policy_code = 0;
	acm_bin_pol.secondary_policy_code = 0;
	acm_bin_pol.primary_binary_policy = primary;
	acm_bin_pol.secondary_binary_policy = secondary;
}

int
acm_init(void)
{
	int ret = -EINVAL;

	acm_set_endian();
	write_lock(&acm_bin_pol_rwlock);

	if (ACM_USE_SECURITY_POLICY == ACM_CHINESE_WALL_POLICY) {
		acm_init_binary_policy(NULL, NULL);
		acm_init_chwall_policy();
		acm_bin_pol.primary_policy_code = ACM_CHINESE_WALL_POLICY;
		acm_primary_ops = &acm_chinesewall_ops;
		acm_bin_pol.secondary_policy_code = ACM_NULL_POLICY;
		acm_secondary_ops = &acm_null_ops;
		ret = ACM_OK;
	} else if (ACM_USE_SECURITY_POLICY == ACM_SIMPLE_TYPE_ENFORCEMENT_POLICY) {
		acm_init_binary_policy(NULL, NULL);
		acm_init_ste_policy();
		acm_bin_pol.primary_policy_code = ACM_SIMPLE_TYPE_ENFORCEMENT_POLICY;
		acm_primary_ops = &acm_simple_type_enforcement_ops;
		acm_bin_pol.secondary_policy_code = ACM_NULL_POLICY;
		acm_secondary_ops = &acm_null_ops;
		ret = ACM_OK;
	} else if (ACM_USE_SECURITY_POLICY == ACM_CHINESE_WALL_AND_SIMPLE_TYPE_ENFORCEMENT_POLICY) {
		acm_init_binary_policy(NULL, NULL);
		acm_init_chwall_policy();
		acm_init_ste_policy();
		acm_bin_pol.primary_policy_code = ACM_CHINESE_WALL_POLICY;
		acm_primary_ops = &acm_chinesewall_ops;
		acm_bin_pol.secondary_policy_code = ACM_SIMPLE_TYPE_ENFORCEMENT_POLICY;
		acm_secondary_ops = &acm_simple_type_enforcement_ops;
		ret = ACM_OK;
	} else if (ACM_USE_SECURITY_POLICY == ACM_NULL_POLICY) {
		acm_init_binary_policy(NULL, NULL);
		acm_bin_pol.primary_policy_code = ACM_NULL_POLICY;
		acm_primary_ops = &acm_null_ops;
		acm_bin_pol.secondary_policy_code = ACM_NULL_POLICY;
		acm_secondary_ops = &acm_null_ops;
		ret = ACM_OK;
	}
	write_unlock(&acm_bin_pol_rwlock);

	if (ret != ACM_OK)
		return -EINVAL;		
	printk("%s: Enforcing Primary %s, Secondary %s.\n", __func__, 
	       ACM_POLICY_NAME(acm_bin_pol.primary_policy_code), ACM_POLICY_NAME(acm_bin_pol.secondary_policy_code));
	return ACM_OK;
}


int
acm_init_domain_ssid(domid_t id, ssidref_t ssidref)
{
	struct acm_ssid_domain *ssid;
	struct domain *subj = find_domain_by_id(id);
	int ret1, ret2;
	
	if (subj == NULL) {
		printk("%s: ACM_NULL_POINTER ERROR (id=%x).\n", __func__, id);
		return ACM_NULL_POINTER_ERROR;
	}
	if ((ssid = xmalloc(struct acm_ssid_domain)) == NULL)
		return ACM_INIT_SSID_ERROR;

	ssid->datatype       = DOMAIN;
	ssid->subject  	     = subj;
	ssid->domainid	     = subj->domain_id;
	ssid->primary_ssid   = NULL;
	ssid->secondary_ssid = NULL;

	if (ACM_USE_SECURITY_POLICY != ACM_NULL_POLICY)
		ssid->ssidref = ssidref;
	else
		ssid->ssidref = ACM_DEFAULT_SSID;

	subj->ssid           = ssid;
	/* now fill in primary and secondary parts; we only get here through hooks */
	if (acm_primary_ops->init_domain_ssid != NULL)
		ret1 = acm_primary_ops->init_domain_ssid(&(ssid->primary_ssid), ssidref);
	else
		ret1 = ACM_OK;

	if (acm_secondary_ops->init_domain_ssid != NULL)
		ret2 = acm_secondary_ops->init_domain_ssid(&(ssid->secondary_ssid), ssidref);
	else
		ret2 = ACM_OK;

	if ((ret1 != ACM_OK) || (ret2 != ACM_OK)) {
		printk("%s: ERROR instantiating individual ssids for domain 0x%02x.\n",
		       __func__, subj->domain_id);
		acm_free_domain_ssid(ssid);	
	        put_domain(subj);
		return ACM_INIT_SSID_ERROR;
	}
	printk("%s: assigned domain %x the ssidref=%x.\n", __func__, id, ssid->ssidref);
	put_domain(subj);
	return ACM_OK;
}


int
acm_free_domain_ssid(struct acm_ssid_domain *ssid)
{
	domid_t id;

	/* domain is already gone, just ssid is left */
	if (ssid == NULL) {
		printk("%s: ACM_NULL_POINTER ERROR.\n", __func__);
		return ACM_NULL_POINTER_ERROR;
	}
       	id = ssid->domainid;
	ssid->subject  	     = NULL;

	if (acm_primary_ops->free_domain_ssid != NULL) /* null policy */
		acm_primary_ops->free_domain_ssid(ssid->primary_ssid);
	ssid->primary_ssid = NULL;
	if (acm_secondary_ops->free_domain_ssid != NULL)
		acm_secondary_ops->free_domain_ssid(ssid->secondary_ssid);
	ssid->secondary_ssid = NULL;
	xfree(ssid);
	printkd("%s: Freed individual domain ssid (domain=%02x).\n",__func__, id);
	return ACM_OK;
}