policydb.h 9.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * A policy database (policydb) specifies the
 * configuration data for the security policy.
 *
 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
 */

/*
 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
 *
 *	Support for enhanced MLS infrastructure.
 *
 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
 *
15
 *	Added conditional policy language extensions
L
Linus Torvalds 已提交
16 17 18 19
 *
 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
 *	This program is free software; you can redistribute it and/or modify
20
 *	it under the terms of the GNU General Public License as published by
L
Linus Torvalds 已提交
21 22 23 24 25 26
 *	the Free Software Foundation, version 2.
 */

#ifndef _SS_POLICYDB_H_
#define _SS_POLICYDB_H_

27 28
#include <linux/flex_array.h>

L
Linus Torvalds 已提交
29 30 31
#include "symtab.h"
#include "avtab.h"
#include "sidtab.h"
32 33
#include "ebitmap.h"
#include "mls_types.h"
L
Linus Torvalds 已提交
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
#include "context.h"
#include "constraint.h"

/*
 * A datum type is defined for each kind of symbol
 * in the configuration data:  individual permissions,
 * common prefixes for access vectors, classes,
 * users, roles, types, sensitivities, categories, etc.
 */

/* Permission attributes */
struct perm_datum {
	u32 value;		/* permission bit + 1 */
};

/* Attributes of a common prefix for access vectors */
struct common_datum {
	u32 value;			/* internal common value */
	struct symtab permissions;	/* common permissions */
};

/* Class attributes */
struct class_datum {
	u32 value;			/* class value */
	char *comkey;			/* common name */
	struct common_datum *comdatum;	/* common datum */
	struct symtab permissions;	/* class-specific permission symbol table */
	struct constraint_node *constraints;	/* constraints on class permissions */
	struct constraint_node *validatetrans;	/* special transition rules */
};

/* Role attributes */
struct role_datum {
	u32 value;			/* internal role value */
68
	u32 bounds;			/* boundary of role */
L
Linus Torvalds 已提交
69 70 71 72 73 74
	struct ebitmap dominates;	/* set of roles dominated by this role */
	struct ebitmap types;		/* set of authorized types for role */
};

struct role_trans {
	u32 role;		/* current role */
75 76
	u32 type;		/* program executable type, or new object type */
	u32 tclass;		/* process class, or new object class */
L
Linus Torvalds 已提交
77 78 79 80
	u32 new_role;		/* new role */
	struct role_trans *next;
};

81 82 83 84 85
struct filename_trans {
	u32 stype;		/* current process */
	u32 ttype;		/* parent dir context */
	u16 tclass;		/* class of new object */
	const char *name;	/* last path component */
86 87 88
};

struct filename_trans_datum {
89 90 91
	u32 otype;		/* expected of new object */
};

L
Linus Torvalds 已提交
92 93 94 95 96 97 98 99 100
struct role_allow {
	u32 role;		/* current role */
	u32 new_role;		/* new role */
	struct role_allow *next;
};

/* Type attributes */
struct type_datum {
	u32 value;		/* internal type value */
101
	u32 bounds;		/* boundary of type */
L
Linus Torvalds 已提交
102
	unsigned char primary;	/* primary name? */
103
	unsigned char attribute;/* attribute ?*/
L
Linus Torvalds 已提交
104 105 106 107 108
};

/* User attributes */
struct user_datum {
	u32 value;			/* internal user value */
109
	u32 bounds;			/* bounds of user */
L
Linus Torvalds 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	struct ebitmap roles;		/* set of authorized roles for user */
	struct mls_range range;		/* MLS range (min - max) for user */
	struct mls_level dfltlevel;	/* default login MLS level for user */
};


/* Sensitivity attributes */
struct level_datum {
	struct mls_level *level;	/* sensitivity and associated categories */
	unsigned char isalias;	/* is this sensitivity an alias for another? */
};

/* Category attributes */
struct cat_datum {
	u32 value;		/* internal category bit + 1 */
	unsigned char isalias;  /* is this category an alias for another? */
};

struct range_trans {
129 130 131
	u32 source_type;
	u32 target_type;
	u32 target_class;
L
Linus Torvalds 已提交
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
};

/* Boolean data type */
struct cond_bool_datum {
	__u32 value;		/* internal type value */
	int state;
};

struct cond_node;

/*
 * The configuration data includes security contexts for
 * initial SIDs, unlabeled file systems, TCP and UDP port numbers,
 * network interfaces, and nodes.  This structure stores the
 * relevant data for one such entry.  Entries of the same kind
 * (e.g. all initial SIDs) are linked together into a list.
 */
struct ocontext {
	union {
		char *name;	/* name of initial SID, fs, netif, fstype, path */
		struct {
			u8 protocol;
			u16 low_port;
			u16 high_port;
		} port;		/* TCP or UDP port information */
		struct {
			u32 addr;
			u32 mask;
		} node;		/* node information */
		struct {
			u32 addr[4];
			u32 mask[4];
		} node6;        /* IPv6 node information */
	} u;
	union {
		u32 sclass;  /* security class for genfs */
		u32 behavior;  /* labeling behavior for fs_use */
	} v;
	struct context context[2];	/* security context(s) */
	u32 sid[2];	/* SID(s) */
	struct ocontext *next;
};

struct genfs {
	char *fstype;
	struct ocontext *head;
	struct genfs *next;
};

/* symbol table array indices */
#define SYM_COMMONS 0
#define SYM_CLASSES 1
#define SYM_ROLES   2
#define SYM_TYPES   3
#define SYM_USERS   4
#define SYM_BOOLS   5
#define SYM_LEVELS  6
#define SYM_CATS    7
#define SYM_NUM     8

/* object context array indices */
#define OCON_ISID  0	/* initial SIDs */
#define OCON_FS    1	/* unlabeled file systems */
#define OCON_PORT  2	/* TCP and UDP port numbers */
#define OCON_NETIF 3	/* network interfaces */
#define OCON_NODE  4	/* nodes */
#define OCON_FSUSE 5	/* fs_use */
#define OCON_NODE6 6	/* IPv6 nodes */
#define OCON_NUM   7

/* The policy database */
struct policydb {
204 205
	int mls_enabled;

L
Linus Torvalds 已提交
206 207 208 209 210 211 212 213 214 215 216 217
	/* symbol tables */
	struct symtab symtab[SYM_NUM];
#define p_commons symtab[SYM_COMMONS]
#define p_classes symtab[SYM_CLASSES]
#define p_roles symtab[SYM_ROLES]
#define p_types symtab[SYM_TYPES]
#define p_users symtab[SYM_USERS]
#define p_bools symtab[SYM_BOOLS]
#define p_levels symtab[SYM_LEVELS]
#define p_cats symtab[SYM_CATS]

	/* symbol names indexed by (value - 1) */
218
	struct flex_array *sym_val_to_name[SYM_NUM];
L
Linus Torvalds 已提交
219 220 221 222 223

	/* class, role, and user attributes indexed by (value - 1) */
	struct class_datum **class_val_to_struct;
	struct role_datum **role_val_to_struct;
	struct user_datum **user_val_to_struct;
224
	struct flex_array *type_val_to_struct_array;
L
Linus Torvalds 已提交
225 226 227 228 229 230 231

	/* type enforcement access vectors and transitions */
	struct avtab te_avtab;

	/* role transitions */
	struct role_trans *role_tr;

232
	/* file transitions with the last path component */
233 234
	/* quickly exclude lookups when parent ttype has no rules */
	struct ebitmap filename_trans_ttypes;
235 236
	/* actual set of filename_trans rules */
	struct hashtab *filename_trans;
237

L
Linus Torvalds 已提交
238 239 240 241 242
	/* bools indexed by (value - 1) */
	struct cond_bool_datum **bool_val_to_struct;
	/* type enforcement conditional access vectors and transitions */
	struct avtab te_cond_avtab;
	/* linked list indexing te_cond_avtab by conditional */
243
	struct cond_node *cond_list;
L
Linus Torvalds 已提交
244 245 246 247 248 249 250 251

	/* role allows */
	struct role_allow *role_allow;

	/* security contexts of initial SIDs, unlabeled file systems,
	   TCP or UDP port numbers, network interfaces and nodes */
	struct ocontext *ocontexts[OCON_NUM];

252
	/* security contexts for files in filesystems that cannot support
L
Linus Torvalds 已提交
253 254
	   a persistent label mapping or use another
	   fixed labeling behavior. */
255
	struct genfs *genfs;
L
Linus Torvalds 已提交
256

257 258
	/* range transitions table (range_trans_key -> mls_range) */
	struct hashtab *range_tr;
L
Linus Torvalds 已提交
259

260
	/* type -> attribute reverse mapping */
261
	struct flex_array *type_attr_map_array;
262

263 264
	struct ebitmap policycaps;

E
Eric Paris 已提交
265 266
	struct ebitmap permissive_map;

267 268 269
	/* length of this policy when it was loaded */
	size_t len;

L
Linus Torvalds 已提交
270
	unsigned int policyvers;
271 272 273

	unsigned int reject_unknown : 1;
	unsigned int allow_unknown : 1;
274 275 276

	u16 process_class;
	u32 process_trans_perms;
L
Linus Torvalds 已提交
277 278 279 280 281
};

extern void policydb_destroy(struct policydb *p);
extern int policydb_load_isids(struct policydb *p, struct sidtab *s);
extern int policydb_context_isvalid(struct policydb *p, struct context *c);
282 283 284
extern int policydb_class_isvalid(struct policydb *p, unsigned int class);
extern int policydb_type_isvalid(struct policydb *p, unsigned int type);
extern int policydb_role_isvalid(struct policydb *p, unsigned int role);
L
Linus Torvalds 已提交
285
extern int policydb_read(struct policydb *p, void *fp);
286
extern int policydb_write(struct policydb *p, void *fp);
L
Linus Torvalds 已提交
287 288 289 290 291

#define PERM_SYMTAB_SIZE 32

#define POLICYDB_CONFIG_MLS    1

292 293 294 295
/* the config flags related to unknown classes/perms are bits 2 and 3 */
#define REJECT_UNKNOWN	0x00000002
#define ALLOW_UNKNOWN	0x00000004

L
Linus Torvalds 已提交
296 297 298 299 300 301 302 303 304 305 306
#define OBJECT_R "object_r"
#define OBJECT_R_VAL 1

#define POLICYDB_MAGIC SELINUX_MAGIC
#define POLICYDB_STRING "SE Linux"

struct policy_file {
	char *data;
	size_t len;
};

307 308 309 310 311
struct policy_data {
	struct policydb *p;
	void *fp;
};

L
Linus Torvalds 已提交
312 313 314 315 316 317 318 319 320 321 322
static inline int next_entry(void *buf, struct policy_file *fp, size_t bytes)
{
	if (bytes > fp->len)
		return -EINVAL;

	memcpy(buf, fp->data, bytes);
	fp->data += bytes;
	fp->len -= bytes;
	return 0;
}

323
static inline int put_entry(const void *buf, size_t bytes, int num, struct policy_file *fp)
324 325 326 327 328 329 330 331 332 333
{
	size_t len = bytes * num;

	memcpy(fp->data, buf, len);
	fp->data += len;
	fp->len -= len;

	return 0;
}

334 335 336 337 338 339 340
static inline char *sym_name(struct policydb *p, unsigned int sym_num, unsigned int element_nr)
{
	struct flex_array *fa = p->sym_val_to_name[sym_num];

	return flex_array_get_ptr(fa, element_nr);
}

341 342 343
extern u16 string_to_security_class(struct policydb *p, const char *name);
extern u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name);

L
Linus Torvalds 已提交
344 345
#endif	/* _SS_POLICYDB_H_ */