16cd6a6acSopenharmony_ci/* Authors: Karl MacMillan <kmacmillan@tresys.com> 26cd6a6acSopenharmony_ci * Frank Mayer <mayerf@tresys.com> 36cd6a6acSopenharmony_ci * 46cd6a6acSopenharmony_ci * Copyright (C) 2003 - 2005 Tresys Technology, LLC 56cd6a6acSopenharmony_ci * 66cd6a6acSopenharmony_ci * This library is free software; you can redistribute it and/or 76cd6a6acSopenharmony_ci * modify it under the terms of the GNU Lesser General Public 86cd6a6acSopenharmony_ci * License as published by the Free Software Foundation; either 96cd6a6acSopenharmony_ci * version 2.1 of the License, or (at your option) any later version. 106cd6a6acSopenharmony_ci * 116cd6a6acSopenharmony_ci * This library is distributed in the hope that it will be useful, 126cd6a6acSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 136cd6a6acSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 146cd6a6acSopenharmony_ci * Lesser General Public License for more details. 156cd6a6acSopenharmony_ci * 166cd6a6acSopenharmony_ci * You should have received a copy of the GNU Lesser General Public 176cd6a6acSopenharmony_ci * License along with this library; if not, write to the Free Software 186cd6a6acSopenharmony_ci * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 196cd6a6acSopenharmony_ci */ 206cd6a6acSopenharmony_ci 216cd6a6acSopenharmony_ci#ifndef _SEPOL_POLICYDB_CONDITIONAL_H_ 226cd6a6acSopenharmony_ci#define _SEPOL_POLICYDB_CONDITIONAL_H_ 236cd6a6acSopenharmony_ci 246cd6a6acSopenharmony_ci#include <sepol/policydb/flask_types.h> 256cd6a6acSopenharmony_ci#include <sepol/policydb/avtab.h> 266cd6a6acSopenharmony_ci#include <sepol/policydb/symtab.h> 276cd6a6acSopenharmony_ci#include <sepol/policydb/policydb.h> 286cd6a6acSopenharmony_ci 296cd6a6acSopenharmony_ci#ifdef __cplusplus 306cd6a6acSopenharmony_ciextern "C" { 316cd6a6acSopenharmony_ci#endif 326cd6a6acSopenharmony_ci 336cd6a6acSopenharmony_ci#define COND_EXPR_MAXDEPTH 10 346cd6a6acSopenharmony_ci 356cd6a6acSopenharmony_ci/* this is the max unique bools in a conditional expression 366cd6a6acSopenharmony_ci * for which we precompute all outcomes for the expression. 376cd6a6acSopenharmony_ci * 386cd6a6acSopenharmony_ci * NOTE - do _NOT_ use value greater than 5 because 396cd6a6acSopenharmony_ci * cond_node_t->expr_pre_comp can only hold at most 32 values 406cd6a6acSopenharmony_ci */ 416cd6a6acSopenharmony_ci#define COND_MAX_BOOLS 5 426cd6a6acSopenharmony_ci 436cd6a6acSopenharmony_ci/* 446cd6a6acSopenharmony_ci * A conditional expression is a list of operators and operands 456cd6a6acSopenharmony_ci * in reverse polish notation. 466cd6a6acSopenharmony_ci */ 476cd6a6acSopenharmony_citypedef struct cond_expr { 486cd6a6acSopenharmony_ci#define COND_BOOL 1 /* plain bool */ 496cd6a6acSopenharmony_ci#define COND_NOT 2 /* !bool */ 506cd6a6acSopenharmony_ci#define COND_OR 3 /* bool || bool */ 516cd6a6acSopenharmony_ci#define COND_AND 4 /* bool && bool */ 526cd6a6acSopenharmony_ci#define COND_XOR 5 /* bool ^ bool */ 536cd6a6acSopenharmony_ci#define COND_EQ 6 /* bool == bool */ 546cd6a6acSopenharmony_ci#define COND_NEQ 7 /* bool != bool */ 556cd6a6acSopenharmony_ci#define COND_LAST COND_NEQ 566cd6a6acSopenharmony_ci uint32_t expr_type; 576cd6a6acSopenharmony_ci uint32_t bool; 586cd6a6acSopenharmony_ci struct cond_expr *next; 596cd6a6acSopenharmony_ci} cond_expr_t; 606cd6a6acSopenharmony_ci 616cd6a6acSopenharmony_ci/* 626cd6a6acSopenharmony_ci * Each cond_node_t contains a list of rules to be enabled/disabled 636cd6a6acSopenharmony_ci * depending on the current value of the conditional expression. This 646cd6a6acSopenharmony_ci * struct is for that list. 656cd6a6acSopenharmony_ci */ 666cd6a6acSopenharmony_citypedef struct cond_av_list { 676cd6a6acSopenharmony_ci avtab_ptr_t node; 686cd6a6acSopenharmony_ci struct cond_av_list *next; 696cd6a6acSopenharmony_ci} cond_av_list_t; 706cd6a6acSopenharmony_ci 716cd6a6acSopenharmony_ci/* 726cd6a6acSopenharmony_ci * A cond node represents a conditional block in a policy. It 736cd6a6acSopenharmony_ci * contains a conditional expression, the current state of the expression, 746cd6a6acSopenharmony_ci * two lists of rules to enable/disable depending on the value of the 756cd6a6acSopenharmony_ci * expression (the true list corresponds to if and the false list corresponds 766cd6a6acSopenharmony_ci * to else).. 776cd6a6acSopenharmony_ci */ 786cd6a6acSopenharmony_citypedef struct cond_node { 796cd6a6acSopenharmony_ci int cur_state; 806cd6a6acSopenharmony_ci cond_expr_t *expr; 816cd6a6acSopenharmony_ci /* these true/false lists point into te_avtab when that is used */ 826cd6a6acSopenharmony_ci cond_av_list_t *true_list; 836cd6a6acSopenharmony_ci cond_av_list_t *false_list; 846cd6a6acSopenharmony_ci /* and these are used during parsing and for modules */ 856cd6a6acSopenharmony_ci avrule_t *avtrue_list; 866cd6a6acSopenharmony_ci avrule_t *avfalse_list; 876cd6a6acSopenharmony_ci /* these fields are not written to binary policy */ 886cd6a6acSopenharmony_ci unsigned int nbools; 896cd6a6acSopenharmony_ci uint32_t bool_ids[COND_MAX_BOOLS]; 906cd6a6acSopenharmony_ci uint32_t expr_pre_comp; 916cd6a6acSopenharmony_ci struct cond_node *next; 926cd6a6acSopenharmony_ci /* a tunable conditional, calculated and used at expansion */ 936cd6a6acSopenharmony_ci#define COND_NODE_FLAGS_TUNABLE UINT32_C(0x01) 946cd6a6acSopenharmony_ci uint32_t flags; 956cd6a6acSopenharmony_ci} cond_node_t; 966cd6a6acSopenharmony_ci 976cd6a6acSopenharmony_ciextern int cond_evaluate_expr(policydb_t * p, cond_expr_t * expr); 986cd6a6acSopenharmony_ciextern cond_expr_t *cond_copy_expr(cond_expr_t * expr); 996cd6a6acSopenharmony_ci 1006cd6a6acSopenharmony_ciextern int cond_expr_equal(cond_node_t * a, cond_node_t * b); 1016cd6a6acSopenharmony_ciextern int cond_normalize_expr(policydb_t * p, cond_node_t * cn); 1026cd6a6acSopenharmony_ciextern void cond_node_destroy(cond_node_t * node); 1036cd6a6acSopenharmony_ciextern void cond_expr_destroy(cond_expr_t * expr); 1046cd6a6acSopenharmony_ci 1056cd6a6acSopenharmony_ciextern cond_node_t *cond_node_find(policydb_t * p, 1066cd6a6acSopenharmony_ci cond_node_t * needle, cond_node_t * haystack, 1076cd6a6acSopenharmony_ci int *was_created); 1086cd6a6acSopenharmony_ci 1096cd6a6acSopenharmony_ciextern cond_node_t *cond_node_create(policydb_t * p, cond_node_t * node); 1106cd6a6acSopenharmony_ci 1116cd6a6acSopenharmony_ciextern cond_node_t *cond_node_search(policydb_t * p, cond_node_t * list, 1126cd6a6acSopenharmony_ci cond_node_t * cn); 1136cd6a6acSopenharmony_ci 1146cd6a6acSopenharmony_ciextern int evaluate_conds(policydb_t * p); 1156cd6a6acSopenharmony_ci 1166cd6a6acSopenharmony_ciextern avtab_datum_t *cond_av_list_search(avtab_key_t * key, 1176cd6a6acSopenharmony_ci cond_av_list_t * cond_list); 1186cd6a6acSopenharmony_ci 1196cd6a6acSopenharmony_ciextern void cond_av_list_destroy(cond_av_list_t * list); 1206cd6a6acSopenharmony_ci 1216cd6a6acSopenharmony_ciextern void cond_optimize_lists(cond_list_t * cl); 1226cd6a6acSopenharmony_ci 1236cd6a6acSopenharmony_ciextern int cond_policydb_init(policydb_t * p); 1246cd6a6acSopenharmony_ciextern void cond_policydb_destroy(policydb_t * p); 1256cd6a6acSopenharmony_ciextern void cond_list_destroy(cond_list_t * list); 1266cd6a6acSopenharmony_ci 1276cd6a6acSopenharmony_ciextern int cond_init_bool_indexes(policydb_t * p); 1286cd6a6acSopenharmony_ciextern int cond_destroy_bool(hashtab_key_t key, hashtab_datum_t datum, void *p); 1296cd6a6acSopenharmony_ci 1306cd6a6acSopenharmony_ciextern int cond_index_bool(hashtab_key_t key, hashtab_datum_t datum, 1316cd6a6acSopenharmony_ci void *datap); 1326cd6a6acSopenharmony_ci 1336cd6a6acSopenharmony_ciextern int cond_read_bool(policydb_t * p, hashtab_t h, struct policy_file *fp); 1346cd6a6acSopenharmony_ci 1356cd6a6acSopenharmony_ciextern int cond_read_list(policydb_t * p, cond_list_t ** list, void *fp); 1366cd6a6acSopenharmony_ci 1376cd6a6acSopenharmony_ciextern void cond_compute_av(avtab_t * ctab, avtab_key_t * key, 1386cd6a6acSopenharmony_ci struct sepol_av_decision *avd); 1396cd6a6acSopenharmony_ci 1406cd6a6acSopenharmony_ci#ifdef __cplusplus 1416cd6a6acSopenharmony_ci} 1426cd6a6acSopenharmony_ci#endif 1436cd6a6acSopenharmony_ci 1446cd6a6acSopenharmony_ci#endif /* _CONDITIONAL_H_ */ 145