/*1* A policy database (policydb) specifies the2* configuration data for the security policy.3*4* Author : Stephen Smalley, <[email protected]>5*/67/*8* Updated: Trusted Computer Solutions, Inc. <[email protected]>9*10* Support for enhanced MLS infrastructure.11*12* Updated: Frank Mayer <[email protected]> and Karl MacMillan <[email protected]>13*14* Added conditional policy language extensions15*16* Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.17* Copyright (C) 2003 - 2004 Tresys Technology, LLC18* This program is free software; you can redistribute it and/or modify19* it under the terms of the GNU General Public License as published by20* the Free Software Foundation, version 2.21*/2223#ifndef _SS_POLICYDB_H_24#define _SS_POLICYDB_H_2526#include <linux/flex_array.h>2728#include "symtab.h"29#include "avtab.h"30#include "sidtab.h"31#include "ebitmap.h"32#include "mls_types.h"33#include "context.h"34#include "constraint.h"3536/*37* A datum type is defined for each kind of symbol38* in the configuration data: individual permissions,39* common prefixes for access vectors, classes,40* users, roles, types, sensitivities, categories, etc.41*/4243/* Permission attributes */44struct perm_datum {45u32 value; /* permission bit + 1 */46};4748/* Attributes of a common prefix for access vectors */49struct common_datum {50u32 value; /* internal common value */51struct symtab permissions; /* common permissions */52};5354/* Class attributes */55struct class_datum {56u32 value; /* class value */57char *comkey; /* common name */58struct common_datum *comdatum; /* common datum */59struct symtab permissions; /* class-specific permission symbol table */60struct constraint_node *constraints; /* constraints on class permissions */61struct constraint_node *validatetrans; /* special transition rules */62};6364/* Role attributes */65struct role_datum {66u32 value; /* internal role value */67u32 bounds; /* boundary of role */68struct ebitmap dominates; /* set of roles dominated by this role */69struct ebitmap types; /* set of authorized types for role */70};7172struct role_trans {73u32 role; /* current role */74u32 type; /* program executable type, or new object type */75u32 tclass; /* process class, or new object class */76u32 new_role; /* new role */77struct role_trans *next;78};7980struct filename_trans {81u32 stype; /* current process */82u32 ttype; /* parent dir context */83u16 tclass; /* class of new object */84const char *name; /* last path component */85};8687struct filename_trans_datum {88u32 otype; /* expected of new object */89};9091struct role_allow {92u32 role; /* current role */93u32 new_role; /* new role */94struct role_allow *next;95};9697/* Type attributes */98struct type_datum {99u32 value; /* internal type value */100u32 bounds; /* boundary of type */101unsigned char primary; /* primary name? */102unsigned char attribute;/* attribute ?*/103};104105/* User attributes */106struct user_datum {107u32 value; /* internal user value */108u32 bounds; /* bounds of user */109struct ebitmap roles; /* set of authorized roles for user */110struct mls_range range; /* MLS range (min - max) for user */111struct mls_level dfltlevel; /* default login MLS level for user */112};113114115/* Sensitivity attributes */116struct level_datum {117struct mls_level *level; /* sensitivity and associated categories */118unsigned char isalias; /* is this sensitivity an alias for another? */119};120121/* Category attributes */122struct cat_datum {123u32 value; /* internal category bit + 1 */124unsigned char isalias; /* is this category an alias for another? */125};126127struct range_trans {128u32 source_type;129u32 target_type;130u32 target_class;131};132133/* Boolean data type */134struct cond_bool_datum {135__u32 value; /* internal type value */136int state;137};138139struct cond_node;140141/*142* The configuration data includes security contexts for143* initial SIDs, unlabeled file systems, TCP and UDP port numbers,144* network interfaces, and nodes. This structure stores the145* relevant data for one such entry. Entries of the same kind146* (e.g. all initial SIDs) are linked together into a list.147*/148struct ocontext {149union {150char *name; /* name of initial SID, fs, netif, fstype, path */151struct {152u8 protocol;153u16 low_port;154u16 high_port;155} port; /* TCP or UDP port information */156struct {157u32 addr;158u32 mask;159} node; /* node information */160struct {161u32 addr[4];162u32 mask[4];163} node6; /* IPv6 node information */164} u;165union {166u32 sclass; /* security class for genfs */167u32 behavior; /* labeling behavior for fs_use */168} v;169struct context context[2]; /* security context(s) */170u32 sid[2]; /* SID(s) */171struct ocontext *next;172};173174struct genfs {175char *fstype;176struct ocontext *head;177struct genfs *next;178};179180/* symbol table array indices */181#define SYM_COMMONS 0182#define SYM_CLASSES 1183#define SYM_ROLES 2184#define SYM_TYPES 3185#define SYM_USERS 4186#define SYM_BOOLS 5187#define SYM_LEVELS 6188#define SYM_CATS 7189#define SYM_NUM 8190191/* object context array indices */192#define OCON_ISID 0 /* initial SIDs */193#define OCON_FS 1 /* unlabeled file systems */194#define OCON_PORT 2 /* TCP and UDP port numbers */195#define OCON_NETIF 3 /* network interfaces */196#define OCON_NODE 4 /* nodes */197#define OCON_FSUSE 5 /* fs_use */198#define OCON_NODE6 6 /* IPv6 nodes */199#define OCON_NUM 7200201/* The policy database */202struct policydb {203int mls_enabled;204205/* symbol tables */206struct symtab symtab[SYM_NUM];207#define p_commons symtab[SYM_COMMONS]208#define p_classes symtab[SYM_CLASSES]209#define p_roles symtab[SYM_ROLES]210#define p_types symtab[SYM_TYPES]211#define p_users symtab[SYM_USERS]212#define p_bools symtab[SYM_BOOLS]213#define p_levels symtab[SYM_LEVELS]214#define p_cats symtab[SYM_CATS]215216/* symbol names indexed by (value - 1) */217struct flex_array *sym_val_to_name[SYM_NUM];218219/* class, role, and user attributes indexed by (value - 1) */220struct class_datum **class_val_to_struct;221struct role_datum **role_val_to_struct;222struct user_datum **user_val_to_struct;223struct flex_array *type_val_to_struct_array;224225/* type enforcement access vectors and transitions */226struct avtab te_avtab;227228/* role transitions */229struct role_trans *role_tr;230231/* file transitions with the last path component */232/* quickly exclude lookups when parent ttype has no rules */233struct ebitmap filename_trans_ttypes;234/* actual set of filename_trans rules */235struct hashtab *filename_trans;236237/* bools indexed by (value - 1) */238struct cond_bool_datum **bool_val_to_struct;239/* type enforcement conditional access vectors and transitions */240struct avtab te_cond_avtab;241/* linked list indexing te_cond_avtab by conditional */242struct cond_node *cond_list;243244/* role allows */245struct role_allow *role_allow;246247/* security contexts of initial SIDs, unlabeled file systems,248TCP or UDP port numbers, network interfaces and nodes */249struct ocontext *ocontexts[OCON_NUM];250251/* security contexts for files in filesystems that cannot support252a persistent label mapping or use another253fixed labeling behavior. */254struct genfs *genfs;255256/* range transitions table (range_trans_key -> mls_range) */257struct hashtab *range_tr;258259/* type -> attribute reverse mapping */260struct flex_array *type_attr_map_array;261262struct ebitmap policycaps;263264struct ebitmap permissive_map;265266/* length of this policy when it was loaded */267size_t len;268269unsigned int policyvers;270271unsigned int reject_unknown : 1;272unsigned int allow_unknown : 1;273274u16 process_class;275u32 process_trans_perms;276};277278extern void policydb_destroy(struct policydb *p);279extern int policydb_load_isids(struct policydb *p, struct sidtab *s);280extern int policydb_context_isvalid(struct policydb *p, struct context *c);281extern int policydb_class_isvalid(struct policydb *p, unsigned int class);282extern int policydb_type_isvalid(struct policydb *p, unsigned int type);283extern int policydb_role_isvalid(struct policydb *p, unsigned int role);284extern int policydb_read(struct policydb *p, void *fp);285extern int policydb_write(struct policydb *p, void *fp);286287#define PERM_SYMTAB_SIZE 32288289#define POLICYDB_CONFIG_MLS 1290291/* the config flags related to unknown classes/perms are bits 2 and 3 */292#define REJECT_UNKNOWN 0x00000002293#define ALLOW_UNKNOWN 0x00000004294295#define OBJECT_R "object_r"296#define OBJECT_R_VAL 1297298#define POLICYDB_MAGIC SELINUX_MAGIC299#define POLICYDB_STRING "SE Linux"300301struct policy_file {302char *data;303size_t len;304};305306struct policy_data {307struct policydb *p;308void *fp;309};310311static inline int next_entry(void *buf, struct policy_file *fp, size_t bytes)312{313if (bytes > fp->len)314return -EINVAL;315316memcpy(buf, fp->data, bytes);317fp->data += bytes;318fp->len -= bytes;319return 0;320}321322static inline int put_entry(const void *buf, size_t bytes, int num, struct policy_file *fp)323{324size_t len = bytes * num;325326memcpy(fp->data, buf, len);327fp->data += len;328fp->len -= len;329330return 0;331}332333static inline char *sym_name(struct policydb *p, unsigned int sym_num, unsigned int element_nr)334{335struct flex_array *fa = p->sym_val_to_name[sym_num];336337return flex_array_get_ptr(fa, element_nr);338}339340extern u16 string_to_security_class(struct policydb *p, const char *name);341extern u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name);342343#endif /* _SS_POLICYDB_H_ */344345346347