aboutsummaryrefslogtreecommitdiffstats
path: root/package/libnl-tiny/src/include/netlink/object-api.h
blob: 8a44fe90129fa4b119eb5ec143682428aa12dfdd (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
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
/*
 * netlink/object-api.c		Object API
 *
 *	This library is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU Lesser General Public
 *	License as published by the Free Software Foundation version 2.1
 *	of the License.
 *
 * Copyright (c) 2003-2007 Thomas Graf <tgraf@suug.ch>
 */

#ifndef NETLINK_OBJECT_API_H_
#define NETLINK_OBJECT_API_H_

#include <netlink/netlink.h>
#include <netlink/utils.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @ingroup object
 * @defgroup object_api Object API
 * @brief
 *
 * @par 1) Object Definition
 * @code
 * // Define your object starting with the common object header
 * struct my_obj {
 * 	NLHDR_COMMON
 * 	int		my_data;
 * };
 *
 * // Fill out the object operations structure
 * struct nl_object_ops my_ops = {
 * 	.oo_name	= "my_obj",
 * 	.oo_size	= sizeof(struct my_obj),
 * };
 *
 * // At this point the object can be allocated, you may want to provide a
 * // separate _alloc() function to ease allocting objects of this kind.
 * struct nl_object *obj = nl_object_alloc(&my_ops);
 *
 * // And release it again...
 * nl_object_put(obj);
 * @endcode
 *
 * @par 2) Allocating additional data
 * @code
 * // You may require to allocate additional data and store it inside
 * // object, f.e. assuming there is a field `ptr'.
 * struct my_obj {
 * 	NLHDR_COMMON
 * 	void *		ptr;
 * };
 *
 * // And at some point you may assign allocated data to this field:
 * my_obj->ptr = calloc(1, ...);
 *
 * // In order to not introduce any memory leaks you have to release
 * // this data again when the last reference is given back.
 * static void my_obj_free_data(struct nl_object *obj)
 * {
 * 	struct my_obj *my_obj = nl_object_priv(obj);
 *
 * 	free(my_obj->ptr);
 * }
 *
 * // Also when the object is cloned, you must ensure for your pointer
 * // stay valid even if one of the clones is freed by either making
 * // a clone as well or increase the reference count.
 * static int my_obj_clone(struct nl_object *src, struct nl_object *dst)
 * {
 * 	struct my_obj *my_src = nl_object_priv(src);
 * 	struct my_obj *my_dst = nl_object_priv(dst);
 *
 * 	if (src->ptr) {
 * 		dst->ptr = calloc(1, ...);
 * 		memcpy(dst->ptr, src->ptr, ...);
 * 	}
 * }
 *
 * struct nl_object_ops my_ops = {
 * 	...
 * 	.oo_free_data	= my_obj_free_data,
 * 	.oo_clone	= my_obj_clone,
 * };
 * @endcode
 *
 * @par 3) Object Dumping
 * @code
 * static int my_obj_dump_detailed(struct nl_object *obj,
 * 				   struct nl_dump_params *params)
 * {
 * 	struct my_obj *my_obj = nl_object_priv(obj);
 *
 * 	// It is absolutely essential to use nl_dump() when printing
 *	// any text to make sure the dumping parameters are respected.
 * 	nl_dump(params, "Obj Integer: %d\n", my_obj->my_int);
 *
 * 	// Before we can dump the next line, make sure to prefix
 *	// this line correctly.
 * 	nl_new_line(params);
 *
 * 	// You may also split a line into multiple nl_dump() calls.
 * 	nl_dump(params, "String: %s ", my_obj->my_string);
 * 	nl_dump(params, "String-2: %s\n", my_obj->another_string);
 * }
 *
 * struct nl_object_ops my_ops = {
 * 	...
 * 	.oo_dump[NL_DUMP_FULL]	= my_obj_dump_detailed,
 * };
 * @endcode
 *
 * @par 4) Object Attributes
 * @code
 * // The concept of object attributes is optional but can ease the typical
 * // case of objects that have optional attributes, e.g. a route may have a
 * // nexthop assigned but it is not required to.
 *
 * // The first step to define your object specific bitmask listing all
 * // attributes
 * #define MY_ATTR_FOO		(1<<0)
 * #define MY_ATTR_BAR		(1<<1)
 *
 * // When assigning an optional attribute to the object, make sure
 * // to mark its availability.
 * my_obj->foo = 123123;
 * my_obj->ce_mask |= MY_ATTR_FOO;
 *
 * // At any time you may use this mask to check for the availability
 * // of the attribute, e.g. while dumping
 * if (my_obj->ce_mask & MY_ATTR_FOO)
 * 	nl_dump(params, "foo %d ", my_obj->foo);
 *
 * // One of the big advantages of this concept is that it allows for
 * // standardized comparisons which make it trivial for caches to
 * // identify unique objects by use of unified comparison functions.
 * // In order for it to work, your object implementation must provide
 * // a comparison function and define a list of attributes which
 * // combined together make an object unique.
 *
 * static int my_obj_compare(struct nl_object *_a, struct nl_object *_b,
 * 			     uint32_t attrs, int flags)
 * {
 * 	struct my_obj *a = nl_object_priv(_a):
 * 	struct my_obj *b = nl_object_priv(_b):
 * 	int diff = 0;
 *
 * 	// We help ourselves in defining our own DIFF macro which will
 *	// call ATTR_DIFF() on both objects which will make sure to only
 *	// compare the attributes if required.
 * 	#define MY_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, MY_ATTR_##ATTR, a, b, EXPR)
 *
 * 	// Call our own diff macro for each attribute to build a bitmask
 *	// representing the attributes which mismatch.
 * 	diff |= MY_DIFF(FOO, a->foo != b->foo)
 * 	diff |= MY_DIFF(BAR, strcmp(a->bar, b->bar))
 *
 * 	return diff;
 * }
 *
 * // In order to identify identical objects with differing attributes
 * // you must specify the attributes required to uniquely identify
 * // your object. Make sure to not include too many attributes, this
 * // list is used when caches look for an old version of an object.
 * struct nl_object_ops my_ops = {
 * 	...
 * 	.oo_id_attrs		= MY_ATTR_FOO,
 * 	.oo_compare		= my_obj_compare,
 * };
 * @endcode
 * @{
 */

/**
 * Common Object Header
 *
 * This macro must be included as first member in every object
 * definition to allow objects to be cached.
 */
#define NLHDR_COMMON				\
	int			ce_refcnt;	\
	struct nl_object_ops *	ce_ops;		\
	struct nl_cache *	ce_cache;	\
	struct nl_list_head	ce_list;	\
	int			ce_msgtype;	\
	int			ce_flags;	\
	uint32_t		ce_mask;

/**
 * Return true if attribute is available in both objects
 * @arg A		an object
 * @arg B		another object
 * @arg ATTR		attribute bit
 *
 * @return True if the attribute is available, otherwise false is returned.
 */
#define AVAILABLE(A, B, ATTR)	(((A)->ce_mask & (B)->ce_mask) & (ATTR))

/**
 * Return true if attributes mismatch
 * @arg A		an object
 * @arg B		another object
 * @arg ATTR		attribute bit
 * @arg EXPR		Comparison expression
 *
 * This function will check if the attribute in question is available
 * in both objects, if not this will count as a mismatch.
 *
 * If available the function will execute the expression which must
 * return true if the attributes mismatch.
 *
 * @return True if the attribute mismatch, or false if they match.
 */
#define ATTR_MISMATCH(A, B, ATTR, EXPR)	(!AVAILABLE(A, B, ATTR) || (EXPR))

/**
 * Return attribute bit if attribute does not match
 * @arg LIST		list of attributes to be compared
 * @arg ATTR		attribute bit
 * @arg A		an object
 * @arg B		another object
 * @arg EXPR		Comparison expression
 *
 * This function will check if the attribute in question is available
 * in both objects, if not this will count as a mismatch.
 *
 * If available the function will execute the expression which must
 * return true if the attributes mismatch.
 *
 * In case the attributes mismatch, the attribute is returned, otherwise
 * 0 is returned.
 *
 * @code
 * diff |= ATTR_DIFF(attrs, MY_ATTR_FOO, a, b, a->foo != b->foo);
 * @endcode
 */
#define ATTR_DIFF(LIST, ATTR, A, B, EXPR) \
({	int diff = 0; \
	if (((LIST) & (ATTR)) && ATTR_MISMATCH(A, B, ATTR, EXPR)) \
		diff = ATTR; \
	diff; })

/**
 * Object Operations
 */
struct nl_object;
struct nl_object_ops
{
	/**
	 * Unique name of object type
	 *
	 * Must be in the form family/name, e.g. "route/addr"
	 */
	char *		oo_name;

	/** Size of object including its header */
	size_t		oo_size;

	/* List of attributes needed to uniquely identify the object */
	uint32_t	oo_id_attrs;

	/**
	 * Constructor function
	 *
	 * Will be called when a new object of this type is allocated.
	 * Can be used to initialize members such as lists etc.
	 */
	void  (*oo_constructor)(struct nl_object *);

	/**
	 * Destructor function
	 *
	 * Will be called when an object is freed. Must free all
	 * resources which may have been allocated as part of this
	 * object.
	 */
	void  (*oo_free_data)(struct nl_object *);

	/**
	 * Cloning function
	 *
	 * Will be called when an object needs to be cloned. Please
	 * note that the generic object code will make an exact
	 * copy of the object first, therefore you only need to take
	 * care of members which require reference counting etc.
	 *
	 * May return a negative error code to abort cloning.
	 */
	int  (*oo_clone)(struct nl_object *, struct nl_object *);

	/**
	 * Dumping functions
	 *
	 * Will be called when an object is dumped. The implementations
	 * have to use nl_dump(), nl_dump_line(), and nl_new_line() to
	 * dump objects.
	 *
	 * The functions must return the number of lines printed.
	 */
	void (*oo_dump[NL_DUMP_MAX+1])(struct nl_object *,
				       struct nl_dump_params *);

	/**
	 * Comparison function
	 *
	 * Will be called when two objects of the same type are
	 * compared. It takes the two objects in question, an object
	 * specific bitmask defining which attributes should be
	 * compared and flags to control the behaviour.
	 *
	 * The function must return a bitmask with the relevant bit
	 * set for each attribute that mismatches.
	 */
	int   (*oo_compare)(struct nl_object *, struct nl_object *,
			    uint32_t, int);


	char *(*oo_attrs2str)(int, char *, size_t);
};

/** @} */

#ifdef __cplusplus
}
#endif

#endif
rface allows policy tools in authorized * domains to interact with the Xen access control module * */ #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/guest_access.h> #include <public/xen.h> #include <xsm/acm/acm_core.h> #include <public/xsm/acm_ops.h> #include <xsm/acm/acm_hooks.h> #include <xsm/acm/acm_endian.h> #include <asm/current.h> static int acm_check_deleted_ssidrefs(struct acm_sized_buffer *dels, struct acm_sized_buffer *errors); static void acm_doms_change_ssidref(ssidref_t (*translator) (const struct acm_ssid_domain *, const struct acm_sized_buffer *), struct acm_sized_buffer *translation_map); static void acm_doms_restore_ssidref(void); static ssidref_t oldssid_to_newssid(const struct acm_ssid_domain *, const struct acm_sized_buffer *map); int acm_set_policy(XEN_GUEST_HANDLE_64(void) buf, u32 buf_size) { u8 *policy_buffer = NULL; int ret = -EFAULT; if ( buf_size < sizeof(struct acm_policy_buffer) ) return -EFAULT; /* copy buffer from guest domain */ if ( (policy_buffer = xmalloc_array(u8, buf_size)) == NULL ) return -ENOMEM; if ( copy_from_guest(policy_buffer, buf, buf_size) ) { printk("%s: Error copying!\n",__func__); goto error_free; } ret = do_acm_set_policy(policy_buffer, buf_size, 0, NULL, NULL, NULL); error_free: xfree(policy_buffer); return ret; } /* * Update the policy of the running system by: * - deleting ssidrefs that are not in the new policy anymore * -> no running domain may use such an ssidref * - assign new ssidrefs to domains based on their old ssidrefs * */ static int _acm_update_policy(void *buf, u32 buf_size, int is_bootpolicy, struct acm_policy_buffer *pol, struct acm_sized_buffer *deletions, struct acm_sized_buffer *ssidchanges, struct acm_sized_buffer *errors) { uint32_t offset, length; static int require_update = 0; write_lock(&acm_bin_pol_rwlock); if ( require_update != 0 && ( deletions == NULL || ssidchanges == NULL ) ) goto error_lock_free; require_update = 1; /* first some tests to check compatibility of new policy with current state of system/domains */ /* if ssidrefs are to be deleted, make sure no domain is using them */ if ( deletions != NULL ) if ( acm_check_deleted_ssidrefs(deletions, errors) ) goto error_lock_free_nossidchange; if ( (ssidchanges != NULL) && (ssidchanges->num_items > 0) ) /* assign all running domains new ssidrefs as requested */ acm_doms_change_ssidref(oldssid_to_newssid, ssidchanges); /* test primary policy data with the new ssidrefs */ offset = be32_to_cpu(pol->primary_buffer_offset); length = be32_to_cpu(pol->secondary_buffer_offset) - offset; if ( (offset + length) > buf_size || acm_primary_ops->test_binary_policy(buf + offset, length, is_bootpolicy, errors)) goto error_lock_free; /* test secondary policy data with the new ssidrefs */ offset = be32_to_cpu(pol->secondary_buffer_offset); length = be32_to_cpu(pol->len) - offset; if ( (offset + length) > buf_size || acm_secondary_ops->test_binary_policy(buf + offset, length, is_bootpolicy, errors)) goto error_lock_free; /* end of testing --- now real updates */ offset = be32_to_cpu(pol->policy_reference_offset); length = be32_to_cpu(pol->primary_buffer_offset) - offset; /* set label reference name */ if ( (offset + length) > buf_size || acm_set_policy_reference(buf + offset, length) ) goto error_lock_free; /* set primary policy data */ offset = be32_to_cpu(pol->primary_buffer_offset); length = be32_to_cpu(pol->secondary_buffer_offset) - offset; if ( acm_primary_ops->set_binary_policy(buf + offset, length) ) goto error_lock_free; /* set secondary policy data */ offset = be32_to_cpu(pol->secondary_buffer_offset); length = be32_to_cpu(pol->len) - offset; if ( acm_secondary_ops->set_binary_policy(buf + offset, length) ) goto error_lock_free; memcpy(&acm_bin_pol.xml_pol_version, &pol->xml_pol_version, sizeof(acm_bin_pol.xml_pol_version)); memcpy(&acm_bin_pol.xml_policy_hash, pol->xml_policy_hash, sizeof(acm_bin_pol.xml_policy_hash)); if ( acm_primary_ops->is_default_policy() && acm_secondary_ops->is_default_policy() ) require_update = 0; write_unlock(&acm_bin_pol_rwlock); return ACM_OK; error_lock_free: if ( (ssidchanges != NULL) && (ssidchanges->num_items > 0) ) { acm_doms_restore_ssidref(); } error_lock_free_nossidchange: do_chwall_init_state_curr(NULL); write_unlock(&acm_bin_pol_rwlock); return -EFAULT; } int do_acm_set_policy(void *buf, u32 buf_size, int is_bootpolicy, struct acm_sized_buffer *deletions, struct acm_sized_buffer *ssidchanges, struct acm_sized_buffer *errors) { struct acm_policy_buffer *pol = (struct acm_policy_buffer *)buf; /* some sanity checking */ if ( (be32_to_cpu(pol->magic) != ACM_MAGIC) || (buf_size != be32_to_cpu(pol->len)) || (be32_to_cpu(pol->policy_version) != ACM_POLICY_VERSION) ) { printk("%s: ERROR in Magic, Version, or buf size.\n", __func__); goto error_free; } if ( acm_active_security_policy == ACM_POLICY_UNDEFINED ) { /* setup the policy with the boot policy */ if ( acm_init_binary_policy( (be32_to_cpu(pol->secondary_policy_code) << 4) | be32_to_cpu(pol->primary_policy_code)) ) { goto error_free; } acm_active_security_policy = (acm_bin_pol.secondary_policy_code << 4) | acm_bin_pol.primary_policy_code; } /* once acm_active_security_policy is set, it cannot be changed */ if ( (be32_to_cpu(pol->primary_policy_code) != acm_bin_pol.primary_policy_code) || (be32_to_cpu(pol->secondary_policy_code) != acm_bin_pol.secondary_policy_code) ) { printkd("%s: Wrong policy type in boot policy!\n", __func__); goto error_free; } return _acm_update_policy(buf, buf_size, is_bootpolicy, pol, deletions, ssidchanges, errors); error_free: printk("%s: Error setting policy.\n", __func__); return -EFAULT; } int acm_get_policy(XEN_GUEST_HANDLE_64(void) buf, u32 buf_size) { u8 *policy_buffer; int ret; struct acm_policy_buffer *bin_pol; if ( buf_size < sizeof(struct acm_policy_buffer) ) return -EFAULT; if ( (policy_buffer = xmalloc_array(u8, buf_size)) == NULL ) return -ENOMEM; read_lock(&acm_bin_pol_rwlock); bin_pol = (struct acm_policy_buffer *)policy_buffer; bin_pol->magic = cpu_to_be32(ACM_MAGIC); bin_pol->primary_policy_code = cpu_to_be32(acm_bin_pol.primary_policy_code); bin_pol->secondary_policy_code = cpu_to_be32(acm_bin_pol.secondary_policy_code); bin_pol->len = cpu_to_be32(sizeof(struct acm_policy_buffer)); bin_pol->policy_reference_offset = cpu_to_be32(be32_to_cpu(bin_pol->len)); bin_pol->primary_buffer_offset = cpu_to_be32(be32_to_cpu(bin_pol->len)); bin_pol->secondary_buffer_offset = cpu_to_be32(be32_to_cpu(bin_pol->len)); memcpy(&bin_pol->xml_pol_version, &acm_bin_pol.xml_pol_version, sizeof(struct acm_policy_version)); memcpy(&bin_pol->xml_policy_hash, &acm_bin_pol.xml_policy_hash, sizeof(acm_bin_pol.xml_policy_hash)); ret = acm_dump_policy_reference( policy_buffer + be32_to_cpu(bin_pol->policy_reference_offset), buf_size - be32_to_cpu(bin_pol->policy_reference_offset)); if ( ret < 0 ) goto error_free_unlock; bin_pol->len = cpu_to_be32(be32_to_cpu(bin_pol->len) + ret); bin_pol->primary_buffer_offset = cpu_to_be32(be32_to_cpu(bin_pol->len)); ret = acm_primary_ops->dump_binary_policy( policy_buffer + be32_to_cpu(bin_pol->primary_buffer_offset), buf_size - be32_to_cpu(bin_pol->primary_buffer_offset)); if ( ret < 0 ) goto error_free_unlock; bin_pol->len = cpu_to_be32(be32_to_cpu(bin_pol->len) + ret); bin_pol->secondary_buffer_offset = cpu_to_be32(be32_to_cpu(bin_pol->len)); ret = acm_secondary_ops->dump_binary_policy( policy_buffer + be32_to_cpu(bin_pol->secondary_buffer_offset), buf_size - be32_to_cpu(bin_pol->secondary_buffer_offset)); if ( ret < 0 ) goto error_free_unlock; bin_pol->len = cpu_to_be32(be32_to_cpu(bin_pol->len) + ret); if ( copy_to_guest(buf, policy_buffer, be32_to_cpu(bin_pol->len)) ) goto error_free_unlock; read_unlock(&acm_bin_pol_rwlock); xfree(policy_buffer); return ACM_OK; error_free_unlock: read_unlock(&acm_bin_pol_rwlock); printk("%s: Error getting policy.\n", __func__); xfree(policy_buffer); return -EFAULT; } int acm_dump_statistics(XEN_GUEST_HANDLE_64(void) buf, u16 buf_size) { /* send stats to user space */ u8 *stats_buffer; int len1, len2; struct acm_stats_buffer acm_stats; if ( (stats_buffer = xmalloc_array(u8, buf_size)) == NULL ) return -ENOMEM; read_lock(&acm_bin_pol_rwlock); len1 = acm_primary_ops->dump_statistics( stats_buffer + sizeof(struct acm_stats_buffer), buf_size - sizeof(struct acm_stats_buffer)); if ( len1 < 0 ) goto error_lock_free; len2 = acm_secondary_ops->dump_statistics( stats_buffer + sizeof(struct acm_stats_buffer) + len1, buf_size - sizeof(struct acm_stats_buffer) - len1); if ( len2 < 0 ) goto error_lock_free; acm_stats.magic = cpu_to_be32(ACM_MAGIC); acm_stats.primary_policy_code = cpu_to_be32(acm_bin_pol.primary_policy_code); acm_stats.secondary_policy_code = cpu_to_be32(acm_bin_pol.secondary_policy_code); acm_stats.primary_stats_offset = cpu_to_be32(sizeof(struct acm_stats_buffer)); acm_stats.secondary_stats_offset = cpu_to_be32(sizeof(struct acm_stats_buffer) + len1); acm_stats.len = cpu_to_be32(sizeof(struct acm_stats_buffer) + len1 + len2); memcpy(stats_buffer, &acm_stats, sizeof(struct acm_stats_buffer)); if ( copy_to_guest(buf, stats_buffer, sizeof(struct acm_stats_buffer) + len1 + len2) ) goto error_lock_free; read_unlock(&acm_bin_pol_rwlock); xfree(stats_buffer); return ACM_OK; error_lock_free: read_unlock(&acm_bin_pol_rwlock); xfree(stats_buffer); return -EFAULT; } int acm_get_ssid(ssidref_t ssidref, XEN_GUEST_HANDLE_64(void) buf, u16 buf_size) { /* send stats to user space */ u8 *ssid_buffer; int ret; struct acm_ssid_buffer *acm_ssid; if ( buf_size < sizeof(struct acm_ssid_buffer) ) return -EFAULT; if ( (ssid_buffer = xmalloc_array(u8, buf_size)) == NULL ) return -ENOMEM; read_lock(&acm_bin_pol_rwlock); acm_ssid = (struct acm_ssid_buffer *)ssid_buffer; acm_ssid->len = sizeof(struct acm_ssid_buffer); acm_ssid->ssidref = ssidref; acm_ssid->primary_policy_code = acm_bin_pol.primary_policy_code; acm_ssid->secondary_policy_code = acm_bin_pol.secondary_policy_code; acm_ssid->policy_reference_offset = acm_ssid->len; ret = acm_dump_policy_reference( ssid_buffer + acm_ssid->policy_reference_offset, buf_size - acm_ssid->policy_reference_offset); if ( ret < 0 ) goto error_free_unlock; acm_ssid->len += ret; acm_ssid->primary_types_offset = acm_ssid->len; /* ret >= 0 --> ret == max_types */ ret = acm_primary_ops->dump_ssid_types( ACM_PRIMARY(ssidref), ssid_buffer + acm_ssid->primary_types_offset, buf_size - acm_ssid->primary_types_offset); if ( ret < 0 ) goto error_free_unlock; acm_ssid->len += ret; acm_ssid->primary_max_types = ret; acm_ssid->secondary_types_offset = acm_ssid->len; ret = acm_secondary_ops->dump_ssid_types( ACM_SECONDARY(ssidref), ssid_buffer + acm_ssid->secondary_types_offset, buf_size - acm_ssid->secondary_types_offset); if ( ret < 0 ) goto error_free_unlock; acm_ssid->len += ret; acm_ssid->secondary_max_types = ret; if ( copy_to_guest(buf, ssid_buffer, acm_ssid->len) ) goto error_free_unlock; read_unlock(&acm_bin_pol_rwlock); xfree(ssid_buffer); return ACM_OK; error_free_unlock: read_unlock(&acm_bin_pol_rwlock); printk("%s: Error getting ssid.\n", __func__); xfree(ssid_buffer); return -ENOMEM; } int acm_get_decision(ssidref_t ssidref1, ssidref_t ssidref2, u32 hook) { int ret = ACM_ACCESS_DENIED; read_lock(&acm_bin_pol_rwlock); switch ( hook ) { case ACMHOOK_sharing: /* Sharing hook restricts access in STE policy only */ ret = acm_sharing(ssidref1, ssidref2); break; case ACMHOOK_authorization: ret = acm_authorization(ssidref1, ssidref2); break; case ACMHOOK_conflictset: ret = acm_conflictset(ssidref1); default: /* deny */ break; } read_unlock(&acm_bin_pol_rwlock); printkd("%s: ssid1=%x, ssid2=%x, decision=%s.\n", __func__, ssidref1, ssidref2, (ret == ACM_ACCESS_PERMITTED) ? "GRANTED" : "DENIED"); return ret; } /* Check if an ssidref of the current policy type is being used by any domain. */ static int acm_check_used_ssidref(uint32_t policy_type, uint32_t search_ssidref, struct acm_sized_buffer *errors) { int rc = 0; struct acm_ssid_domain *rawssid; read_lock(&ssid_list_rwlock); for_each_acmssid( rawssid ) { ssidref_t ssidref; void *s = GET_SSIDP(policy_type, rawssid); if ( policy_type == ACM_CHINESE_WALL_POLICY ) { ssidref = ((struct chwall_ssid *)s)->chwall_ssidref; } else { ssidref = ((struct ste_ssid *)s)->ste_ssidref; } gdprintk(XENLOG_INFO,"domid=%d: search ssidref=%d, ssidref=%d\n", rawssid->domainid,search_ssidref,ssidref); if ( ssidref == search_ssidref ) { /* one is enough */ acm_array_append_tuple(errors, ACM_SSIDREF_IN_USE, search_ssidref); rc = 1; break; } } read_unlock(&ssid_list_rwlock); return rc; } /* * Translate a current ssidref into its future representation under * the new policy. * The map provides translation of ssidrefs from old to new in tuples * of (old ssidref, new ssidref). */ static ssidref_t oldssid_to_newssid(const struct acm_ssid_domain *rawssid, const struct acm_sized_buffer *map) { uint i; if ( rawssid != NULL ) { ssidref_t ssid = rawssid->ssidref & 0xffff; for ( i = 0; i + 1 < map->num_items; i += 2 ) { if ( map->array[i] == ssid ) { return (map->array[i+1] << 16 | map->array[i+1]); } } } return ACM_INVALID_SSIDREF; } /* * Assign an ssidref to the CHWALL policy component of the domain */ static void acm_pri_policy_assign_ssidref(struct acm_ssid_domain *rawssid, ssidref_t new_ssid) { struct chwall_ssid *chwall = (struct chwall_ssid *)rawssid->primary_ssid; chwall->chwall_ssidref = new_ssid; } /* * Assign an ssidref to the STE policy component of the domain */ static void acm_sec_policy_assign_ssidref(struct acm_ssid_domain *rawssid, ssidref_t new_ssid) { struct ste_ssid *ste = (struct ste_ssid *)rawssid->secondary_ssid; ste->ste_ssidref = new_ssid; } /* Change the ssidrefs on each domain using a passed translation function; */ static void acm_doms_change_ssidref(ssidref_t (*translator_fn) (const struct acm_ssid_domain *, const struct acm_sized_buffer *), struct acm_sized_buffer *translation_map) { struct acm_ssid_domain *rawssid; write_lock(&ssid_list_rwlock); for_each_acmssid( rawssid ) { ssidref_t new_ssid; rawssid->old_ssidref = rawssid->ssidref; new_ssid = translator_fn(rawssid, translation_map); if ( new_ssid == ACM_INVALID_SSIDREF ) { /* means no mapping found, so no change -- old = new */ continue; } acm_pri_policy_assign_ssidref(rawssid, ACM_PRIMARY (new_ssid) ); acm_sec_policy_assign_ssidref(rawssid, ACM_SECONDARY(new_ssid) ); rawssid->ssidref = new_ssid; } write_unlock(&ssid_list_rwlock); } /* * Restore the previous ssidref values on all domains */ static void acm_doms_restore_ssidref(void) { struct acm_ssid_domain *rawssid; write_lock(&ssid_list_rwlock); for_each_acmssid( rawssid ) { ssidref_t old_ssid; if ( rawssid->old_ssidref == rawssid->ssidref ) continue; old_ssid = rawssid->old_ssidref & 0xffff; rawssid->ssidref = rawssid->old_ssidref; acm_pri_policy_assign_ssidref(rawssid, old_ssid); acm_sec_policy_assign_ssidref(rawssid, old_ssid); } write_unlock(&ssid_list_rwlock); } /* Check the list of domains whether either one of them uses a to-be-deleted ssidref. */ static int acm_check_deleted_ssidrefs(struct acm_sized_buffer *dels, struct acm_sized_buffer *errors) { int rc = 0; uint idx; /* check for running domains that should not be there anymore */ for ( idx = 0; idx < dels->num_items; idx++ ) { if ( acm_check_used_ssidref(ACM_SIMPLE_TYPE_ENFORCEMENT_POLICY, dels->array[idx], errors) > 0 || acm_check_used_ssidref(ACM_CHINESE_WALL_POLICY, dels->array[idx], errors) > 0) { rc = ACM_ERROR; break; } } return rc; } /* * Change the policy of the system. */ int acm_change_policy(struct acm_change_policy *chgpolicy) { int rc = 0; u8 *binpolicy = NULL; struct acm_sized_buffer dels = { .array = NULL, }; struct acm_sized_buffer ssidmap = { .array = NULL, }; struct acm_sized_buffer errors = { .array = NULL, }; gdprintk(XENLOG_INFO, "change policy operation\n"); if ( (chgpolicy->delarray_size > 4096) || (chgpolicy->chgarray_size > 4096) || (chgpolicy->errarray_size > 4096)) { return ACM_ERROR; } dels.num_items = chgpolicy->delarray_size / sizeof(uint32_t); if ( dels.num_items > 0 ) { dels.array = xmalloc_array(uint32_t, dels.num_items); if ( dels.array == NULL ) { rc = -ENOMEM; goto acm_chg_policy_exit; } } ssidmap.num_items = chgpolicy->chgarray_size / sizeof(uint32_t); if ( ssidmap.num_items > 0 ) { ssidmap.array = xmalloc_array(uint32_t, ssidmap.num_items); if ( ssidmap.array == NULL ) { rc = -ENOMEM; goto acm_chg_policy_exit; } } errors.num_items = chgpolicy->errarray_size / sizeof(uint32_t); if ( errors.num_items > 0 ) { errors.array = xmalloc_array(uint32_t, errors.num_items); if ( errors.array == NULL ) { rc = -ENOMEM; goto acm_chg_policy_exit; } memset(errors.array, 0x0, sizeof(uint32_t) * errors.num_items); } binpolicy = xmalloc_array(u8, chgpolicy->policy_pushcache_size); if ( binpolicy == NULL ) { rc = -ENOMEM; goto acm_chg_policy_exit; } if ( copy_from_guest(dels.array, chgpolicy->del_array, dels.num_items) || copy_from_guest(ssidmap.array, chgpolicy->chg_array, ssidmap.num_items) || copy_from_guest(binpolicy, chgpolicy->policy_pushcache, chgpolicy->policy_pushcache_size )) { rc = -EFAULT; goto acm_chg_policy_exit; } rc = do_acm_set_policy(binpolicy, chgpolicy->policy_pushcache_size, 0, &dels, &ssidmap, &errors); if ( (errors.num_items > 0) && copy_to_guest(chgpolicy->err_array, errors.array, errors.num_items ) ) { rc = -EFAULT; goto acm_chg_policy_exit; } acm_chg_policy_exit: xfree(dels.array); xfree(ssidmap.array); xfree(errors.array); xfree(binpolicy); return rc; } /* * Lookup the new ssidref given the domain's id. * The translation map provides a list of tuples in the format * (domid, new ssidref). */ static ssidref_t domid_to_newssid(const struct acm_ssid_domain *rawssid, const struct acm_sized_buffer *map) { domid_t domid = rawssid->domainid; uint i; for ( i = 0; (i+1) < map->num_items; i += 2 ) { if ( map->array[i] == domid ) return (ssidref_t)map->array[i+1]; } return ACM_INVALID_SSIDREF; } int do_acm_relabel_doms(struct acm_sized_buffer *relabel_map, struct acm_sized_buffer *errors) { int rc = 0, irc; write_lock(&acm_bin_pol_rwlock); acm_doms_change_ssidref(domid_to_newssid, relabel_map); /* run tests; collect as much error info as possible */ irc = do_chwall_init_state_curr(errors); irc += do_ste_init_state_curr(errors); if ( irc != 0 ) { rc = -EFAULT; goto acm_relabel_doms_lock_err_exit; } write_unlock(&acm_bin_pol_rwlock); return rc; acm_relabel_doms_lock_err_exit: /* revert the new ssidref assignment */ acm_doms_restore_ssidref(); do_chwall_init_state_curr(NULL); write_unlock(&acm_bin_pol_rwlock); return rc; } int acm_relabel_domains(struct acm_relabel_doms *relabel) { int rc = ACM_OK; struct acm_sized_buffer relabels = { .array = NULL, }; struct acm_sized_buffer errors = { .array = NULL, }; if ( relabel->relabel_map_size > 4096 ) { return ACM_ERROR; } relabels.num_items = relabel->relabel_map_size / sizeof(uint32_t); if ( relabels.num_items > 0 ) { relabels.array = xmalloc_array(uint32_t, relabels.num_items); if ( relabels.array == NULL ) { rc = -ENOMEM; goto acm_relabel_doms_exit; } } errors.num_items = relabel->errarray_size / sizeof(uint32_t); if ( errors.num_items > 0 ) { errors.array = xmalloc_array(uint32_t, errors.num_items); if ( errors.array == NULL ) { rc = -ENOMEM; goto acm_relabel_doms_exit; } memset(errors.array, 0x0, sizeof(uint32_t) * errors.num_items); } if ( copy_from_guest(relabels.array, relabel->relabel_map, relabels.num_items) ) { rc = -EFAULT; goto acm_relabel_doms_exit; } rc = do_acm_relabel_doms(&relabels, &errors); if ( copy_to_guest(relabel->err_array, errors.array, errors.num_items ) ) rc = -EFAULT; acm_relabel_doms_exit: xfree(relabels.array); xfree(errors.array); return rc; } /* * Local variables: * mode: C * c-set-style: "BSD" * c-basic-offset: 4 * tab-width: 4 * indent-tabs-mode: nil * End: */