1 /* Rule-based Classification Engine (RBCE) module
3 * Copyright (C) Hubertus Franke, IBM Corp. 2003
4 * (C) Chandra Seetharaman, IBM Corp. 2003
5 * (C) Vivek Kashyap, IBM Corp. 2004
7 * Module for loading of classification policies and providing
8 * a user API for Class-based Kernel Resource Management (CKRM)
10 * Latest version, more details at http://ckrm.sf.net
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
22 * Created. First cut with much scope for cleanup !
24 * Made modifications to suit the new RBCE module.
25 * Made modifications to address sampling and delivery
27 * Integrated changes from original RBCE module
29 * Merged RBCE and CRBCE into common code base
31 * Incorporated listen call back and IPv4 match support
33 * Added Multi-Classtype Support
36 #include <linux/init.h>
37 #include <linux/module.h>
38 #include <linux/kernel.h>
40 #include <asm/uaccess.h>
42 #include <linux/mount.h>
43 #include <linux/proc_fs.h>
44 #include <linux/limits.h>
45 #include <linux/pid.h>
46 #include <linux/sysctl.h>
48 #include <linux/ckrm_rc.h>
49 #include <linux/ckrm_ce.h>
50 #include <linux/ckrm_net.h>
51 #include "bitvector.h"
56 MODULE_DESCRIPTION(RBCE_MOD_DESCR);
57 MODULE_AUTHOR("Hubertus Franke, Chandra Seetharaman (IBM)");
58 MODULE_LICENSE("GPL");
60 static char modname[] = RBCE_MOD_NAME;
62 /* ==================== typedef, global variables etc., ==================== */
63 struct named_obj_hdr {
64 struct list_head link;
69 #define GET_REF(x) ((x)->obj.referenced)
70 #define INC_REF(x) (GET_REF(x)++)
71 #define DEC_REF(x) (--GET_REF(x))
73 struct named_obj_hdr obj;
79 RBCE_RULE_CMD_PATH = 1, // full qualified path
80 RBCE_RULE_CMD, // basename of the command
81 RBCE_RULE_ARGS, // arguments of the command
82 RBCE_RULE_REAL_UID, // task's real uid
83 RBCE_RULE_REAL_GID, // task's real gid
84 RBCE_RULE_EFFECTIVE_UID, // task's effective uid
85 RBCE_RULE_EFFECTIVE_GID, // task's effective gid
86 RBCE_RULE_APP_TAG, // task's application tag
87 RBCE_RULE_IPV4, // IP address of listen(), ipv4 format
88 RBCE_RULE_IPV6, // IP address of listen(), ipv6 format
89 RBCE_RULE_XID, // VSERVER
90 RBCE_RULE_DEP_RULE, // dependent rule; must be the first term
91 RBCE_RULE_INVALID, // invalid, for filler
92 RBCE_RULE_INVALID2, // invalid, for filler
102 struct rbce_rule_term {
104 rbce_operator_t operator;
106 char *string; // path, cmd, arg, tag, ipv4 and ipv6
107 long id; // uid, gid, euid, egid
108 struct rbce_rule *deprule;
113 struct named_obj_hdr obj;
114 struct rbce_class *target_class;
117 int *terms; // vector of indices into the global term vector
118 int index; // index of this rule into the global term vector
119 int termflag; // which term ids would require a recalculation
120 int do_opt; // do we have to consider this rule during optimize
121 char *strtab; // string table to store the strings of all terms
122 int order; // order of execution of this rule
123 int state; // RBCE_RULE_ENABLED/RBCE_RULE_DISABLED
127 #define RBCE_RULE_DISABLED 0
128 #define RBCE_RULE_ENABLED 1
131 // Data structures and macros used for optimization
132 #define RBCE_TERM_CMD (0)
133 #define RBCE_TERM_UID (1)
134 #define RBCE_TERM_GID (2)
135 #define RBCE_TERM_TAG (3)
136 #define RBCE_TERM_IPV4 (4)
137 #define RBCE_TERM_IPV6 (5)
138 #define RBCE_TERM_XID (6)
140 #define NUM_TERM_MASK_VECTOR (7) // must be one more the last RBCE_TERM_...
142 // Rule flags. 1 bit for each type of rule term
143 #define RBCE_TERMFLAG_CMD (1 << RBCE_TERM_CMD)
144 #define RBCE_TERMFLAG_UID (1 << RBCE_TERM_UID)
145 #define RBCE_TERMFLAG_GID (1 << RBCE_TERM_GID)
146 #define RBCE_TERMFLAG_TAG (1 << RBCE_TERM_TAG)
147 #define RBCE_TERMFLAG_IPV4 (1 << RBCE_TERM_IPV4)
148 #define RBCE_TERMFLAG_IPV6 (1 << RBCE_TERM_IPV6)
149 #define RBCE_TERMFLAG_XID (1 << RBCE_TERM_XID)
150 #define RBCE_TERMFLAG_ALL (RBCE_TERMFLAG_CMD | RBCE_TERMFLAG_UID | \
151 RBCE_TERMFLAG_GID | RBCE_TERMFLAG_TAG | RBCE_TERMFLAG_XID | \
152 RBCE_TERMFLAG_IPV4 | RBCE_TERMFLAG_IPV6)
154 int termop_2_vecidx[RBCE_RULE_INVALID] = {
155 [RBCE_RULE_CMD_PATH] = RBCE_TERM_CMD,
156 [RBCE_RULE_CMD] = RBCE_TERM_CMD,
157 [RBCE_RULE_ARGS] = RBCE_TERM_CMD,
158 [RBCE_RULE_REAL_UID] = RBCE_TERM_UID,
159 [RBCE_RULE_REAL_GID] = RBCE_TERM_GID,
160 [RBCE_RULE_EFFECTIVE_UID] = RBCE_TERM_UID,
161 [RBCE_RULE_EFFECTIVE_GID] = RBCE_TERM_GID,
162 [RBCE_RULE_XID] = RBCE_TERM_XID,
163 [RBCE_RULE_APP_TAG] = RBCE_TERM_TAG,
164 [RBCE_RULE_IPV4] = RBCE_TERM_IPV4,
165 [RBCE_RULE_IPV6] = RBCE_TERM_IPV6,
166 [RBCE_RULE_DEP_RULE] = -1
169 #define TERMOP_2_TERMFLAG(x) (1 << termop_2_vecidx[x])
170 #define TERM_2_TERMFLAG(x) (1 << x)
172 #define POLICY_INC_NUMTERMS (BITS_PER_LONG) // No. of terms added at a time
173 #define POLICY_ACTION_NEW_VERSION 0x01 // Force reallocation
174 #define POLICY_ACTION_REDO_ALL 0x02 // Recompute all rule flags
175 #define POLICY_ACTION_PACK_TERMS 0x04 // Time to pack the terms
177 struct ckrm_eng_callback ckrm_ecbs;
181 static int gl_bitmap_version, gl_action, gl_num_terms;
182 static int gl_allocated, gl_released;
183 struct rbce_rule_term *gl_terms;
184 bitvector_t *gl_mask_vecs[NUM_TERM_MASK_VECTOR];
187 static void optimize_policy(void);
189 #ifndef CKRM_MAX_CLASSTYPES
190 #define CKRM_MAX_CLASSTYPES 32
193 struct list_head rules_list[CKRM_MAX_CLASSTYPES];
194 LIST_HEAD(class_list); // List of classes used
196 static int gl_num_rules;
197 static int gl_rules_version;
198 int rbce_enabled = 1;
199 static rwlock_t global_rwlock = RW_LOCK_UNLOCKED;
201 * One lock to protect them all !!!
202 * Additions, deletions to rules must
203 * happen with this lock being held in write mode.
204 * Access(read/write) to any of the data structures must happen
205 * with this lock held in read mode.
206 * Since, rule related changes do not happen very often it is ok to
207 * have single rwlock.
211 * data structure rbce_private_data holds the bit vector 'eval' which
212 * specifies if rules and terms of rules are evaluated against the task
213 * and if they were evaluated, bit vector 'true' holds the result of that
216 * This data structure is maintained in a task, and the bitvectors are
217 * updated only when needed.
219 * Each rule and each term of a rule has a corresponding bit in the vector.
222 struct rbce_private_data {
223 struct rbce_ext_private_data ext_data;
224 int evaluate; // whether to evaluate rules or not ?
225 int rules_version; // whether to evaluate rules or not ?
227 unsigned long bitmap_version;
230 char data[0]; // eval points to this variable size data array
233 #define RBCE_DATA(tsk) ((struct rbce_private_data*)((tsk)->ce_data))
234 #define RBCE_DATAP(tsk) ((tsk)->ce_data)
236 /* ======================= DEBUG Functions ========================= */
240 int rbcedebug = 0x00;
242 #define DBG_CLASSIFY_RES ( 0x01 )
243 #define DBG_CLASSIFY_DETAILS ( 0x02 )
244 #define DBG_OPTIMIZATION ( 0x04 )
245 #define DBG_SHOW_RESCTL ( 0x08 )
246 #define DBG_CLASS ( 0x10 )
247 #define DBG_RULE ( 0x20 )
248 #define DBG_POLICY ( 0x40 )
250 #define DPRINTK(x, y...) if (rbcedebug & (x)) printk(y)
251 // debugging selectively enabled through /proc/sys/debug/rbce
253 static void print_context_vectors(void)
257 if ((rbcedebug & DBG_OPTIMIZATION) == 0) {
260 for (i = 0; i < NUM_TERM_MASK_VECTOR; i++) {
262 bitvector_print(DBG_OPTIMIZATION, gl_mask_vecs[i]);
268 #define DPRINTK(x, y...)
269 #define print_context_vectors(x)
272 /* ====================== VSERVER support ========================== */
273 #define CONFIG_VSERVER
274 #ifdef CONFIG_VSERVER
275 #include <linux/vs_base.h>
277 typedef unsigned int xid_t;
278 #define vx_task_xid(t) (0)
281 /* ======================= Helper Functions ========================= */
285 static struct ckrm_core_class *rbce_classify(struct task_struct *,
286 struct ckrm_net_struct *,
287 unsigned long, int classtype);
289 static inline struct rbce_rule *find_rule_name(const char *name)
291 struct named_obj_hdr *pos;
294 for (i = 0; i < CKRM_MAX_CLASSTYPES; i++) {
295 list_for_each_entry(pos, &rules_list[i], link) {
296 if (!strcmp(pos->name, name)) {
297 return ((struct rbce_rule *)pos);
304 static inline struct rbce_class *find_class_name(const char *name)
306 struct named_obj_hdr *pos;
308 list_for_each_entry(pos, &class_list, link) {
309 if (!strcmp(pos->name, name))
310 return (struct rbce_class *)pos;
316 * Insert the given rule at the specified order
317 * order = -1 ==> insert at the tail.
319 * Caller must hold global_rwlock in write mode.
321 static int insert_rule(struct rbce_rule *rule, int order)
323 #define ORDER_COUNTER_INCR 10
324 static int order_counter;
326 struct list_head *head = &rules_list[rule->classtype];
327 struct list_head *insert = head;
328 struct rbce_rule *tmp;
330 if (gl_num_rules == 0) {
336 rule->order = order_counter;
337 // FIXME: order_counter overflow/wraparound!!
338 order_counter += ORDER_COUNTER_INCR;
341 old_counter = order_counter;
342 if (order_counter < order) {
343 order_counter = order;
346 order_counter += ORDER_COUNTER_INCR;
347 list_for_each_entry(tmp, head, obj.link) {
348 if (rule->order == tmp->order) {
349 order_counter = old_counter;
352 if (rule->order < tmp->order) {
353 insert = &tmp->obj.link;
358 list_add_tail(&rule->obj.link, insert);
359 // protect the module from removed when any rule is
361 try_module_get(THIS_MODULE);
368 * Remove the rule and reinsert at the specified order.
370 * Caller must hold global_rwlock in write mode.
372 static int reinsert_rule(struct rbce_rule *rule, int order)
374 list_del(&rule->obj.link);
377 module_put(THIS_MODULE);
378 return insert_rule(rule, order);
382 * Get a refernece to the class, create one if it doesn't exist
384 * Caller need to hold global_rwlock in write mode.
388 static struct rbce_class *create_rbce_class(const char *classname,
389 int classtype, void *classobj)
391 struct rbce_class *cls;
393 if (classtype >= CKRM_MAX_CLASSTYPES) {
395 "ckrm_classobj returned %d as classtype which cannot "
396 " be handled by RBCE\n", classtype);
400 cls = kmalloc(sizeof(struct rbce_class), GFP_ATOMIC);
404 cls->obj.name = kmalloc(strlen(classname) + 1, GFP_ATOMIC);
407 cls->classobj = classobj;
408 strcpy(cls->obj.name, classname);
409 list_add_tail(&cls->obj.link, &class_list);
410 cls->classtype = classtype;
418 static struct rbce_class *get_class(char *classname, int *classtype)
420 struct rbce_class *cls;
426 cls = find_class_name(classname);
430 *classtype = cls->classtype;
435 classobj = ckrm_classobj(classname, classtype);
440 return create_rbce_class(classname, *classtype, classobj);
444 * Drop a refernece to the class, create one if it doesn't exist
446 * Caller need to hold global_rwlock in write mode.
448 static void put_class(struct rbce_class *cls)
451 if (DEC_REF(cls) <= 0) {
452 list_del(&cls->obj.link);
453 kfree(cls->obj.name);
461 * Callback from core when a class is added
464 #ifdef RBCE_EXTENSION
465 static void rbce_class_addcb(const char *classname, void *clsobj, int classtype)
467 struct rbce_class *cls;
469 write_lock(&global_rwlock);
470 cls = find_class_name((char *)classname);
472 cls->classobj = clsobj;
474 cls = create_rbce_class(classname, classtype, clsobj);
477 notify_class_action(cls, 1);
478 write_unlock(&global_rwlock);
484 * Callback from core when a class is deleted.
487 rbce_class_deletecb(const char *classname, void *classobj, int classtype)
489 static struct rbce_class *cls;
490 struct named_obj_hdr *pos;
491 struct rbce_rule *rule;
493 write_lock(&global_rwlock);
494 cls = find_class_name(classname);
496 if (cls->classobj != classobj) {
497 printk(KERN_ERR "rbce: class %s changed identity\n",
500 notify_class_action(cls, 0);
501 cls->classobj = NULL;
502 list_for_each_entry(pos, &rules_list[cls->classtype], link) {
503 rule = (struct rbce_rule *)pos;
504 if (rule->target_class) {
506 (rule->target_class->obj.name, classname)) {
508 rule->target_class = NULL;
509 rule->classtype = -1;
514 if ((cls = find_class_name(classname)) != NULL) {
516 "rbce ERROR: class %s exists in rbce after "
517 "removal in core\n", classname);
520 write_unlock(&global_rwlock);
525 * Allocate an index in the global term vector
526 * On success, returns the index. On failure returns -errno.
527 * Caller must hold the global_rwlock in write mode as global data is
530 static int alloc_term_index(void)
532 int size = gl_allocated;
534 if (gl_num_terms >= size) {
536 struct rbce_rule_term *oldv, *newv;
537 int newsize = size + POLICY_INC_NUMTERMS;
541 kmalloc(newsize * sizeof(struct rbce_rule_term),
546 memcpy(newv, oldv, size * sizeof(struct rbce_rule_term));
547 for (i = size; i < newsize; i++) {
551 gl_allocated = newsize;
554 gl_action |= POLICY_ACTION_NEW_VERSION;
555 DPRINTK(DBG_OPTIMIZATION,
556 "alloc_term_index: Expanding size from %d to %d\n",
559 return gl_num_terms++;
563 * Release an index in the global term vector
565 * Caller must hold the global_rwlock in write mode as the global data
568 static void release_term_index(int idx)
570 if ((idx < 0) || (idx > gl_num_terms))
573 gl_terms[idx].op = -1;
575 if ((gl_released > POLICY_INC_NUMTERMS) &&
577 (gl_num_terms - gl_released + POLICY_INC_NUMTERMS))) {
578 gl_action |= POLICY_ACTION_PACK_TERMS;
584 * Release the indices, string memory, and terms associated with the given
587 * Caller should be holding global_rwlock
589 static void __release_rule(struct rbce_rule *rule)
591 int i, *terms = rule->terms;
593 // remove memory and references from other rules
594 for (i = rule->num_terms; --i >= 0;) {
595 struct rbce_rule_term *term = &gl_terms[terms[i]];
597 if (term->op == RBCE_RULE_DEP_RULE) {
598 DEC_REF(term->u.deprule);
600 release_term_index(terms[i]);
615 * delete the given rule and all memory associated with it.
617 * Caller is responsible for protecting the global data
619 static inline int __delete_rule(struct rbce_rule *rule)
621 // make sure we are not referenced by other rules
625 __release_rule(rule);
626 put_class(rule->target_class);
627 release_term_index(rule->index);
628 list_del(&rule->obj.link);
631 module_put(THIS_MODULE);
632 kfree(rule->obj.name);
638 * Optimize the rule evaluation logic
640 * Caller must hold global_rwlock in write mode.
642 static void optimize_policy(void)
645 struct rbce_rule *rule;
646 struct rbce_rule_term *terms;
649 bitvector_t **mask_vecs;
654 * Due to dynamic rule addition/deletion of rules the term
655 * vector can get sparse. As a result the bitvectors grow as we don't
656 * reuse returned indices. If it becomes sparse enough we pack them
660 pack_terms = (gl_action & POLICY_ACTION_PACK_TERMS);
661 DPRINTK(DBG_OPTIMIZATION,
662 "----- Optimize Policy ----- act=%x pt=%d (a=%d n=%d r=%d)\n",
663 gl_action, pack_terms, gl_allocated, gl_num_terms, gl_released);
666 int nsz = ALIGN((gl_num_terms - gl_released),
667 POLICY_INC_NUMTERMS);
669 struct rbce_rule_term *newterms;
673 kmalloc(nsz * sizeof(struct rbce_rule_term), GFP_ATOMIC);
675 for (ii = 0; ii < CKRM_MAX_CLASSTYPES; ii++) {
676 // FIXME: check only for task class types
677 list_for_each_entry_reverse(rule,
680 rule->index = newidx++;
681 for (i = rule->num_terms; --i >= 0;) {
682 int idx = rule->terms[i];
683 newterms[newidx] = terms[idx];
684 rule->terms[i] = newidx++;
691 gl_num_terms = newidx;
694 gl_action &= ~POLICY_ACTION_PACK_TERMS;
695 gl_action |= POLICY_ACTION_NEW_VERSION;
699 num_terms = gl_num_terms;
700 bsize = gl_allocated / 8 + sizeof(bitvector_t);
701 mask_vecs = gl_mask_vecs;
704 if (gl_action & POLICY_ACTION_NEW_VERSION) {
705 /* allocate new mask vectors */
706 char *temp = kmalloc(NUM_TERM_MASK_VECTOR * bsize, GFP_ATOMIC);
708 DPRINTK(DBG_OPTIMIZATION,
709 "------ allocmasks act=%x ------- ver=%d\n", gl_action,
714 if (mask_vecs[0]) {// index 0 has the alloc returned address
717 for (i = 0; i < NUM_TERM_MASK_VECTOR; i++) {
718 mask_vecs[i] = (bitvector_t *) (temp + i * bsize);
719 bitvector_init(mask_vecs[i], gl_allocated);
721 gl_action &= ~POLICY_ACTION_NEW_VERSION;
722 gl_action |= POLICY_ACTION_REDO_ALL;
726 /* We do two things here at once
727 * 1) recompute the rulemask for each required rule
728 * we guarantee proper dependency order during creation time and
729 * by reversely running through this list.
730 * 2) recompute the mask for each term and rule, if required
733 redoall = gl_action & POLICY_ACTION_REDO_ALL;
734 gl_action &= ~POLICY_ACTION_REDO_ALL;
736 DPRINTK(DBG_OPTIMIZATION, "------- run act=%x -------- redoall=%d\n",
738 for (ii = 0; ii < CKRM_MAX_CLASSTYPES; ii++) {
739 // FIXME: check only for task class types
740 list_for_each_entry_reverse(rule, &rules_list[ii], obj.link) {
741 unsigned long termflag;
743 if (!redoall && !rule->do_opt)
746 for (i = rule->num_terms; --i >= 0;) {
747 int j, idx = rule->terms[i];
748 struct rbce_rule_term *term = &terms[idx];
749 int vecidx = termop_2_vecidx[term->op];
752 termflag |= term->u.deprule->termflag;
753 /* mark this term belonging to all
754 contexts of deprule */
755 for (j = 0; j < NUM_TERM_MASK_VECTOR;
757 if (term->u.deprule->termflag
765 termflag |= TERM_2_TERMFLAG(vecidx);
766 /* mark this term belonging to
767 a particular context */
768 bitvector_set(idx, mask_vecs[vecidx]);
771 for (i = 0; i < NUM_TERM_MASK_VECTOR; i++) {
772 if (termflag & (1 << i)) {
773 bitvector_set(rule->index,
777 rule->termflag = termflag;
779 DPRINTK(DBG_OPTIMIZATION, "r-%s: %x %d\n",
780 rule->obj.name, rule->termflag, rule->index);
783 print_context_vectors();
787 /* ======================= Rule related Functions ========================= */
790 * Caller need to hold global_rwlock in write mode.
793 fill_rule(struct rbce_rule *newrule, struct rbce_rule_term *terms, int nterms)
795 char *class, *strtab;
796 int i, j, order, state, real_nterms, index;
797 int strtablen, rc = 0, counter;
798 struct rbce_rule_term *term = NULL;
799 struct rbce_class *targetcls = NULL;
800 struct rbce_rule *deprule;
805 // Digest filled terms.
807 strtab = class = NULL;
812 for (i = 0; i < nterms; i++) {
813 if (terms[i].op != RBCE_RULE_INVALID) {
816 switch (terms[i].op) {
817 case RBCE_RULE_DEP_RULE:
818 // check if the depend rule is valid
820 deprule = find_rule_name(terms[i].u.string);
821 if (!deprule || deprule == newrule) {
825 // make sure _a_ depend rule
826 // appears in only one term.
827 for (j = 0; j < i; j++) {
830 && terms[j].u.deprule ==
836 terms[i].u.deprule = deprule;
839 // +depend is acceptable and -depend is not
840 if (terms[i].operator != TOKEN_OP_DEP_DEL) {
841 terms[i].operator = RBCE_EQUAL;
848 case RBCE_RULE_CMD_PATH:
851 case RBCE_RULE_APP_TAG:
854 // sum up the string length
855 strtablen += strlen(terms[i].u.string) + 1;
862 switch (terms[i].operator) {
864 order = terms[i].u.id;
871 state = terms[i].u.id != 0;
874 class = terms[i].u.string;
882 // Check if class was specified
885 if ((targetcls = get_class(class, &classtype)) == NULL) {
889 put_class(newrule->target_class);
891 newrule->target_class = targetcls;
892 newrule->classtype = classtype;
894 if (!newrule->target_class) {
900 newrule->state = state;
903 newrule->order = order;
905 newrule->terms = kmalloc(real_nterms * sizeof(int), GFP_ATOMIC);
906 if (!newrule->terms) {
910 newrule->num_terms = real_nterms;
911 if (strtablen && ((strtab = kmalloc(strtablen, GFP_ATOMIC)) == NULL)) {
916 if (newrule->index == -1) {
917 index = alloc_term_index();
922 newrule->index = index;
923 term = &gl_terms[newrule->index];
924 term->op = RBCE_RULE_DEP_RULE;
925 term->u.deprule = newrule;
927 newrule->strtab = strtab;
928 newrule->termflag = 0;
930 // Fill the term vector
933 for (i = 0; i < nterms; i++) {
934 if (terms[i].op == RBCE_RULE_INVALID) {
938 newrule->terms[counter] = alloc_term_index();
939 if (newrule->terms[counter] < 0) {
940 for (j = 0; j < counter; j++) {
941 release_term_index(newrule->terms[j]);
946 term = &gl_terms[newrule->terms[counter]];
947 term->op = terms[i].op;
948 term->operator = terms[i].operator;
949 switch (terms[i].op) {
950 case RBCE_RULE_CMD_PATH:
953 case RBCE_RULE_APP_TAG:
956 term->u.string = &strtab[strtablen];
957 strcpy(term->u.string, terms[i].u.string);
958 strtablen = strlen(term->u.string) + 1;
961 case RBCE_RULE_REAL_UID:
962 case RBCE_RULE_REAL_GID:
963 case RBCE_RULE_EFFECTIVE_UID:
964 case RBCE_RULE_EFFECTIVE_GID:
966 term->u.id = terms[i].u.id;
969 case RBCE_RULE_DEP_RULE:
970 term->u.deprule = terms[i].u.deprule;
971 INC_REF(term->u.deprule);
982 put_class(targetcls);
985 release_term_index(index);
987 kfree(newrule->terms);
994 int change_rule(const char *rname, char *rdefn)
996 struct rbce_rule *rule = NULL, *deprule;
997 struct rbce_rule_term *new_terms = NULL, *term, *terms;
998 int nterms, new_term_mask = 0, oterms, tot_terms;
999 int i, j, k, rc, new_order = 0;
1001 if ((nterms = rules_parse(rdefn, &new_terms, &new_term_mask)) <= 0) {
1002 return !nterms ? -EINVAL : nterms;
1005 write_lock(&global_rwlock);
1006 rule = find_rule_name(rname);
1008 rule = kmalloc(sizeof(struct rbce_rule), GFP_ATOMIC);
1010 rule->obj.name = kmalloc(strlen(rname) + 1, GFP_ATOMIC);
1011 if (rule->obj.name) {
1012 strcpy(rule->obj.name, rname);
1016 rule->state = RBCE_RULE_ENABLED;
1017 rule->target_class = NULL;
1018 rule->classtype = -1;
1021 INIT_LIST_HEAD(&rule->obj.link);
1022 rc = fill_rule(rule, new_terms, nterms);
1028 rule->order)) == 0) {
1033 __delete_rule(rule);
1044 write_unlock(&global_rwlock);
1048 oterms = rule->num_terms;
1049 tot_terms = nterms + oterms;
1051 terms = kmalloc(tot_terms * sizeof(struct rbce_rule_term), GFP_ATOMIC);
1055 write_unlock(&global_rwlock);
1059 new_term_mask &= ~(1 << RBCE_RULE_DEP_RULE);
1060 //ignore the new deprule terms for the first iteration.
1061 // taken care of later.
1062 for (i = 0; i < oterms; i++) {
1063 term = &gl_terms[rule->terms[i]]; // old term
1065 if ((1 << term->op) & new_term_mask) {
1066 // newrule has this attr/value
1067 for (j = 0; j < nterms; j++) {
1068 if (term->op == new_terms[j].op) {
1069 terms[i].op = new_terms[j].op;
1070 terms[i].operator = new_terms[j].
1073 new_terms[j].u.string;
1074 new_terms[j].op = RBCE_RULE_INVALID2;
1079 terms[i].op = term->op;
1080 terms[i].operator = term->operator;
1081 terms[i].u.string = term->u.string;
1085 i = oterms; // for readability
1087 for (j = 0; j < nterms; j++) {
1088 // handled in the previous iteration
1089 if (new_terms[j].op == RBCE_RULE_INVALID2) {
1093 if (new_terms[j].op == RBCE_RULE_DEP_RULE) {
1094 if (new_terms[j].operator == TOKEN_OP_DEP) {
1095 // "depend=rule" deletes all depends in the
1096 // original rule so, delete all depend rule
1097 // terms in the original rule
1098 for (k = 0; k < oterms; k++) {
1099 if (terms[k].op == RBCE_RULE_DEP_RULE) {
1100 terms[k].op = RBCE_RULE_INVALID;
1103 // must copy the new deprule term
1105 // delete the depend rule term if was defined
1106 // in the original rule for both +depend
1108 deprule = find_rule_name(new_terms[j].u.string);
1110 for (k = 0; k < oterms; k++) {
1113 && terms[k].u.deprule ==
1121 if (new_terms[j].operator == TOKEN_OP_DEP_DEL) {
1122 // No need to copy the new deprule term
1127 if ((new_terms[j].op == RBCE_RULE_INVALID) &&
1128 (new_terms[j].operator == TOKEN_OP_ORDER)) {
1132 terms[i].op = new_terms[j].op;
1133 terms[i].operator = new_terms[j].operator;
1134 terms[i].u.string = new_terms[j].u.string;
1136 new_terms[j].op = RBCE_RULE_INVALID2;
1141 // convert old deprule pointers to name pointers.
1142 for (i = 0; i < oterms; i++) {
1143 if (terms[i].op != RBCE_RULE_DEP_RULE)
1145 terms[i].u.string = terms[i].u.deprule->obj.name;
1149 __release_rule(rule);
1152 rc = fill_rule(rule, terms, tot_terms);
1153 if (rc == 0 && new_order) {
1154 rc = reinsert_rule(rule, rule->order);
1156 if (rc != 0) { // rule creation/insertion failed
1157 __delete_rule(rule);
1162 write_unlock(&global_rwlock);
1169 * Delete the specified rule.
1172 int delete_rule(const char *rname)
1175 struct rbce_rule *rule;
1177 write_lock(&global_rwlock);
1179 if ((rule = find_rule_name(rname)) == NULL) {
1180 write_unlock(&global_rwlock);
1183 rc = __delete_rule(rule);
1184 if (rbce_enabled && (gl_action & POLICY_ACTION_PACK_TERMS)) {
1187 write_unlock(&global_rwlock);
1189 DPRINTK(DBG_RULE, "delete rule %s\n", rname);
1194 * copy the rule specified by rname and to the given result string.
1197 void get_rule(const char *rname, char *result)
1200 struct rbce_rule *rule;
1201 struct rbce_rule_term *term;
1202 char *cp = result, oper, idtype[3], str[5];
1204 read_lock(&global_rwlock);
1206 rule = find_rule_name(rname);
1208 for (i = 0; i < rule->num_terms; i++) {
1209 term = gl_terms + rule->terms[i];
1211 case RBCE_RULE_REAL_UID:
1212 strcpy(idtype, "u");
1214 case RBCE_RULE_REAL_GID:
1215 strcpy(idtype, "g");
1217 case RBCE_RULE_EFFECTIVE_UID:
1218 strcpy(idtype, "eu");
1220 case RBCE_RULE_EFFECTIVE_GID:
1221 strcpy(idtype, "eg");
1224 strcpy(idtype, "x");
1226 if (term->operator == RBCE_LESS_THAN) {
1228 } else if (term->operator == RBCE_GREATER_THAN) {
1230 } else if (term->operator == RBCE_NOT) {
1236 sprintf(cp, "%sid%c%ld,", idtype, oper,
1239 case RBCE_RULE_CMD_PATH:
1240 strcpy(str, "path");
1245 case RBCE_RULE_ARGS:
1246 strcpy(str, "args");
1248 case RBCE_RULE_APP_TAG:
1251 case RBCE_RULE_IPV4:
1252 strcpy(str, "ipv4");
1254 case RBCE_RULE_IPV6:
1255 strcpy(str, "ipv6");
1258 sprintf(cp, "%s=%s,", str, term->u.string);
1260 case RBCE_RULE_DEP_RULE:
1262 sprintf(cp, "depend=%s,",
1263 term->u.deprule->obj.name);
1269 if (!rule->num_terms) {
1270 cp += sprintf(cp, "***** no terms defined ***** ");
1274 sprintf(cp, "order=%d,state=%d,", rule->order, rule->state);
1276 sprintf(cp, "class=%s",
1277 rule->target_class ? rule->target_class->obj.
1278 name : "***** REMOVED *****");
1281 sprintf(result, "***** Rule %s doesn't exist *****", rname);
1284 read_unlock(&global_rwlock);
1289 * Change the name of the given rule "from_rname" to "to_rname"
1292 int rename_rule(const char *from_rname, const char *to_rname)
1294 struct rbce_rule *rule;
1295 int nlen, rc = -EINVAL;
1297 if (!to_rname || !*to_rname) {
1300 write_lock(&global_rwlock);
1302 rule = find_rule_name(from_rname);
1304 if ((nlen = strlen(to_rname)) > strlen(rule->obj.name)) {
1305 char *name = kmalloc(nlen + 1, GFP_ATOMIC);
1309 kfree(rule->obj.name);
1310 rule->obj.name = name;
1312 strcpy(rule->obj.name, to_rname);
1315 write_unlock(&global_rwlock);
1320 * Return TRUE if the given rule exists, FALSE otherwise
1323 int rule_exists(const char *rname)
1325 struct rbce_rule *rule;
1327 read_lock(&global_rwlock);
1328 rule = find_rule_name(rname);
1329 read_unlock(&global_rwlock);
1330 return rule != NULL;
1333 /*====================== Magic file handling =======================*/
1337 static struct rbce_private_data *create_private_data(struct rbce_private_data *,
1340 int rbce_ckrm_reclassify(int pid)
1342 printk("ckrm_reclassify_pid ignored\n");
1346 int reclassify_pid(int pid)
1348 struct task_struct *tsk;
1350 // FIXME: Need to treat -pid as process group
1356 rbce_ckrm_reclassify(0); // just reclassify all tasks.
1358 // if pid is +ve take control of the task, start evaluating it
1359 if ((tsk = find_task_by_pid(pid)) == NULL) {
1363 if (unlikely(!RBCE_DATA(tsk))) {
1364 RBCE_DATAP(tsk) = create_private_data(NULL, 0);
1365 if (!RBCE_DATA(tsk)) {
1369 RBCE_DATA(tsk)->evaluate = 1;
1370 rbce_ckrm_reclassify(pid);
1374 int set_tasktag(int pid, char *tag)
1377 struct task_struct *tsk;
1378 struct rbce_private_data *pdata;
1384 if ((tsk = find_task_by_pid(pid)) == NULL) {
1388 tp = kmalloc(strlen(tag) + 1, GFP_ATOMIC);
1394 if (unlikely(!RBCE_DATA(tsk))) {
1395 RBCE_DATAP(tsk) = create_private_data(NULL, 0);
1396 if (!RBCE_DATA(tsk)) {
1401 pdata = RBCE_DATA(tsk);
1402 if (pdata->app_tag) {
1403 kfree(pdata->app_tag);
1405 pdata->app_tag = tp;
1406 strcpy(pdata->app_tag, tag);
1407 rbce_ckrm_reclassify(pid);
1412 /*====================== Classification Functions =======================*/
1415 * Match the given full path name with the command expression.
1416 * This function treats the folowing 2 charaters as special if seen in
1417 * cmd_exp, all other chanracters are compared as is:
1418 * ? - compares to any one single character
1419 * * - compares to one or more single characters
1421 * If fullpath is 1, tsk_comm is compared in full. otherwise only the command
1422 * name (basename(tsk_comm)) is compared.
1424 static int match_cmd(const char *tsk_comm, const char *cmd_exp, int fullpath)
1426 const char *c, *t, *last_ast, *cmd = tsk_comm;
1429 // get the command name if we don't have to match the fullpath
1430 if (!fullpath && ((c = strrchr(tsk_comm, '/')) != NULL)) {
1434 /* now faithfully assume the entire pathname is in cmd */
1436 /* we now have to effectively implement a regular expression
1438 * '?' any single character
1439 * '*' one or more '?'
1445 if (t == NULL || c == NULL) {
1465 // eat up all '*' in c
1466 while (*(c + 1) == '*')
1470 //t++; // Add this for matching '*' with "one"
1472 while (*t && (*t != *(c + 1)) && *t != '/')
1474 if (*t == *(c + 1)) {
1502 /*FALLTHRU*/ default:
1503 if (*t == *c && next_c != *t) {
1507 /* reset to last asterix and
1508 continue from there */
1518 /* check for trailing "*" */
1522 return (!*c && !*t);
1525 static void reverse(char *str, int n)
1530 for (i = 0; i < j; i++, j--) {
1537 static int itoa(int n, char *str)
1542 str[i++] = n % 10 + '0';
1547 (void)reverse(str, sz);
1551 static int v4toa(__u32 y, char *a)
1556 for (i = 0; i < 4; i++) {
1557 size += itoa(y & 0xff, &a[size]);
1564 int match_ipv4(struct ckrm_net_struct *ns, char **string)
1566 char *ptr = *string;
1570 size = v4toa(ns->ns_daddrv4, a4);
1573 return !strncmp(a4, ptr, size);
1576 int match_port(struct ckrm_net_struct *ns, char *ptr)
1579 int size = itoa(ns->ns_dport, a);
1581 return !strncmp(a, ptr, size);
1584 static int __evaluate_rule(struct task_struct *tsk, struct ckrm_net_struct *ns,
1585 struct rbce_rule *rule, bitvector_t * vec_eval,
1586 bitvector_t * vec_true, char **filename);
1588 * evaluate the given task against the given rule with the vec_eval and
1589 * vec_true in context. Return 1 if the task satisfies the given rule, 0
1592 * If the bit corresponding to the rule is set in the vec_eval, then the
1593 * corresponding bit in vec_true is the result. If it is not set, evaluate
1594 * the rule and set the bits in both the vectors accordingly.
1596 * On return, filename will have the pointer to the pathname of the task's
1597 * executable, if the rule had any command related terms.
1599 * Caller must hold the global_rwlock atleast in read mode.
1602 evaluate_rule(struct task_struct *tsk, struct ckrm_net_struct *ns,
1603 struct rbce_rule *rule, bitvector_t * vec_eval,
1604 bitvector_t * vec_true, char **filename)
1606 int tidx = rule->index;
1608 if (!bitvector_test(tidx, vec_eval)) {
1610 (tsk, ns, rule, vec_eval, vec_true, filename)) {
1611 bitvector_set(tidx, vec_true);
1613 bitvector_set(tidx, vec_eval);
1615 return bitvector_test(tidx, vec_true);
1619 * evaluate the given task against every term in the given rule with
1620 * vec_eval and vec_true in context.
1622 * If the bit corresponding to a rule term is set in the vec_eval, then the
1623 * corresponding bit in vec_true is the result for taht particular. If it is
1624 * not set, evaluate the rule term and set the bits in both the vectors
1627 * This fucntions returns true only if all terms in the rule evaluate true.
1629 * On return, filename will have the pointer to the pathname of the task's
1630 * executable, if the rule had any command related terms.
1632 * Caller must hold the global_rwlock atleast in read mode.
1635 __evaluate_rule(struct task_struct *tsk, struct ckrm_net_struct *ns,
1636 struct rbce_rule *rule, bitvector_t * vec_eval,
1637 bitvector_t * vec_true, char **filename)
1642 for (i = rule->num_terms; --i >= 0;) {
1643 int rc = 1, tidx = rule->terms[i];
1645 if (!bitvector_test(tidx, vec_eval)) {
1646 struct rbce_rule_term *term = &gl_terms[tidx];
1650 case RBCE_RULE_CMD_PATH:
1653 if (!*filename) { /* get this once */
1656 GFP_ATOMIC)) == NULL)
1659 (tsk, *filename, NAME_MAX) < 0)) {
1664 rc = match_cmd(*filename, term->u.string,
1666 RBCE_RULE_CMD_PATH));
1668 rc = match_cmd(tsk->comm, term->u.string,
1670 RBCE_RULE_CMD_PATH));
1673 case RBCE_RULE_REAL_UID:
1674 if (term->operator == RBCE_LESS_THAN) {
1675 rc = (tsk->uid < term->u.id);
1676 } else if (term->operator == RBCE_GREATER_THAN){
1677 rc = (tsk->uid > term->u.id);
1678 } else if (term->operator == RBCE_NOT) {
1679 rc = (tsk->uid != term->u.id);
1681 rc = (tsk->uid == term->u.id);
1684 case RBCE_RULE_REAL_GID:
1685 if (term->operator == RBCE_LESS_THAN) {
1686 rc = (tsk->gid < term->u.id);
1687 } else if (term->operator == RBCE_GREATER_THAN){
1688 rc = (tsk->gid > term->u.id);
1689 } else if (term->operator == RBCE_NOT) {
1690 rc = (tsk->gid != term->u.id);
1692 rc = (tsk->gid == term->u.id);
1695 case RBCE_RULE_EFFECTIVE_UID:
1696 if (term->operator == RBCE_LESS_THAN) {
1697 rc = (tsk->euid < term->u.id);
1698 } else if (term->operator == RBCE_GREATER_THAN){
1699 rc = (tsk->euid > term->u.id);
1700 } else if (term->operator == RBCE_NOT) {
1701 rc = (tsk->euid != term->u.id);
1703 rc = (tsk->euid == term->u.id);
1706 case RBCE_RULE_EFFECTIVE_GID:
1707 if (term->operator == RBCE_LESS_THAN) {
1708 rc = (tsk->egid < term->u.id);
1709 } else if (term->operator == RBCE_GREATER_THAN){
1710 rc = (tsk->egid > term->u.id);
1711 } else if (term->operator == RBCE_NOT) {
1712 rc = (tsk->egid != term->u.id);
1714 rc = (tsk->egid == term->u.id);
1717 case RBCE_RULE_APP_TAG:
1718 rc = (RBCE_DATA(tsk)
1720 app_tag) ? !strcmp(RBCE_DATA(tsk)->
1722 term->u.string) : 0;
1724 case RBCE_RULE_DEP_RULE:
1725 rc = evaluate_rule(tsk, NULL, term->u.deprule,
1730 case RBCE_RULE_IPV4:
1731 // TBD: add NOT_EQUAL match. At present rbce
1732 // recognises EQUAL matches only.
1733 if (ns && term->operator == RBCE_EQUAL) {
1736 char *ptr = term->u.string;
1738 if (term->u.string[0] == '*')
1741 ma = match_ipv4(ns, &ptr);
1743 if (*ptr != '\\') { // error
1760 case RBCE_RULE_IPV6: // no support yet
1767 xid_t xid = vx_task_xid(tsk);
1769 if (term->operator == RBCE_LESS_THAN) {
1770 rc = (xid < term->u.id);
1771 } else if (term->operator == RBCE_GREATER_THAN) {
1772 rc = (xid > term->u.id);
1773 } else if (term->operator == RBCE_NOT) {
1774 rc = (xid != term->u.id);
1776 rc = (xid == term->u.id);
1783 printk(KERN_ERR "Error evaluate term op=%d\n",
1788 bitvector_clear(tidx, vec_true);
1790 bitvector_set(tidx, vec_true);
1792 bitvector_set(tidx, vec_eval);
1794 rc = bitvector_test(tidx, vec_true);
1803 //#define PDATA_DEBUG
1806 #define MAX_PDATA 10000
1807 void *pdata_arr[MAX_PDATA];
1808 int pdata_count, pdata_next;
1809 static spinlock_t pdata_lock = SPIN_LOCK_UNLOCKED;
1811 static inline int valid_pdata(struct rbce_private_data *pdata)
1818 spin_lock(&pdata_lock);
1819 for (i = 0; i < MAX_PDATA; i++) {
1820 if (pdata_arr[i] == pdata) {
1821 spin_unlock(&pdata_lock);
1825 spin_unlock(&pdata_lock);
1826 printk("INVALID/CORRUPT PDATA %p\n", pdata);
1830 static inline void store_pdata(struct rbce_private_data *pdata)
1835 spin_lock(&pdata_lock);
1837 while (i < MAX_PDATA) {
1838 if (pdata_arr[pdata_next] == NULL) {
1839 printk("storing %p at %d, count %d\n", pdata,
1840 pdata_next, pdata_count);
1841 pdata_arr[pdata_next++] = pdata;
1842 if (pdata_next == MAX_PDATA) {
1851 spin_unlock(&pdata_lock);
1853 if (i == MAX_PDATA) {
1854 printk("PDATA BUFFER FULL pdata_count %d pdata %p\n",
1855 pdata_count, pdata);
1859 static inline void unstore_pdata(struct rbce_private_data *pdata)
1863 spin_lock(&pdata_lock);
1864 for (i = 0; i < MAX_PDATA; i++) {
1865 if (pdata_arr[i] == pdata) {
1866 printk("unstoring %p at %d, count %d\n", pdata,
1868 pdata_arr[i] = NULL;
1874 spin_unlock(&pdata_lock);
1875 if (i == MAX_PDATA) {
1876 printk("pdata %p not found in the stored array\n",
1883 #else // PDATA_DEBUG
1885 #define valid_pdata(pdata) (1)
1886 #define store_pdata(pdata)
1887 #define unstore_pdata(pdata)
1889 #endif // PDATA_DEBUG
1891 const int use_persistent_state = 1;
1894 * Allocate and initialize a rbce_private_data data structure.
1896 * Caller must hold global_rwlock atleast in read mode.
1900 copy_ext_private_data(struct rbce_private_data *src,
1901 struct rbce_private_data *dst)
1904 dst->ext_data = src->ext_data;
1906 memset(&dst->ext_data, 0, sizeof(dst->ext_data));
1909 static struct rbce_private_data *create_private_data(struct rbce_private_data
1910 *src, int copy_sample)
1912 int vsize, psize, bsize;
1913 struct rbce_private_data *pdata;
1915 if (use_persistent_state) {
1916 vsize = gl_allocated;
1917 bsize = vsize / 8 + sizeof(bitvector_t);
1918 psize = sizeof(struct rbce_private_data) + 2 * bsize;
1920 psize = sizeof(struct rbce_private_data);
1923 pdata = kmalloc(psize, GFP_ATOMIC);
1924 if (pdata != NULL) {
1925 if (use_persistent_state) {
1926 pdata->bitmap_version = gl_bitmap_version;
1927 pdata->eval = (bitvector_t *) & pdata->data[0];
1928 pdata->true = (bitvector_t *) & pdata->data[bsize];
1929 if (src && (src->bitmap_version == gl_bitmap_version)) {
1930 memcpy(pdata->data, src->data, 2 * bsize);
1932 bitvector_init(pdata->eval, vsize);
1933 bitvector_init(pdata->true, vsize);
1936 copy_ext_private_data(src, pdata);
1937 //if (src) { // inherit evaluate and app_tag
1938 // pdata->evaluate = src->evaluate;
1939 // if(src->app_tag) {
1940 // int len = strlen(src->app_tag)+1;
1941 // printk("CREATE_PRIVATE: apptag %s len %d\n",
1942 // src->app_tag,len);
1943 // pdata->app_tag = kmalloc(len, GFP_ATOMIC);
1944 // if (pdata->app_tag) {
1945 // strcpy(pdata->app_tag, src->app_tag);
1949 pdata->evaluate = 1;
1950 pdata->rules_version = src ? src->rules_version : 0;
1951 pdata->app_tag = NULL;
1958 static inline void free_private_data(struct rbce_private_data *pdata)
1960 if (valid_pdata(pdata)) {
1961 unstore_pdata(pdata);
1966 static void free_all_private_data(void)
1968 struct task_struct *proc, *thread;
1970 read_lock(&tasklist_lock);
1971 do_each_thread(proc, thread) {
1972 struct rbce_private_data *pdata;
1974 pdata = RBCE_DATA(thread);
1975 RBCE_DATAP(thread) = NULL;
1976 free_private_data(pdata);
1977 } while_each_thread(proc, thread);
1978 read_unlock(&tasklist_lock);
1983 * reclassify function, which is called by all the callback functions.
1985 * Takes that task to be reclassified and ruleflags that indicates the
1986 * attributes that caused this reclassification request.
1988 * On success, returns the core class pointer to which the given task should
1991 static struct ckrm_core_class *rbce_classify(struct task_struct *tsk,
1992 struct ckrm_net_struct *ns,
1993 unsigned long termflag,
1997 struct rbce_rule *rule;
1998 bitvector_t *vec_true = NULL, *vec_eval = NULL;
1999 struct rbce_class *tgt = NULL;
2000 struct ckrm_core_class *cls = NULL;
2001 char *filename = NULL;
2003 if (!valid_pdata(RBCE_DATA(tsk))) {
2006 if (classtype >= CKRM_MAX_CLASSTYPES) {
2007 // can't handle more than CKRM_MAX_CLASSTYPES
2010 // fast path to avoid locking in case CE is not enabled or if no rules
2011 // are defined or if the tasks states that no evaluation is needed.
2012 if (!rbce_enabled || !gl_num_rules ||
2013 (RBCE_DATA(tsk) && !RBCE_DATA(tsk)->evaluate)) {
2016 // FIXME: optimize_policy should be called from here if
2017 // gl_action is non-zero. Also, it has to be called with the
2018 // global_rwlock held in write mode.
2020 read_lock(&global_rwlock);
2022 vec_eval = vec_true = NULL;
2023 if (use_persistent_state) {
2024 struct rbce_private_data *pdata = RBCE_DATA(tsk);
2028 && (gl_bitmap_version != pdata->bitmap_version))) {
2029 struct rbce_private_data *new_pdata =
2030 create_private_data(pdata, 1);
2034 new_pdata->rules_version =
2035 pdata->rules_version;
2036 new_pdata->evaluate = pdata->evaluate;
2037 new_pdata->app_tag = pdata->app_tag;
2038 free_private_data(pdata);
2040 pdata = RBCE_DATAP(tsk) = new_pdata;
2041 termflag = RBCE_TERMFLAG_ALL;
2042 // need to evaluate them all
2044 // we shouldn't free the pdata as it has more
2045 // details than the vectors. But, this
2046 // reclassification should go thru
2051 goto cls_determined;
2053 vec_eval = pdata->eval;
2054 vec_true = pdata->true;
2056 int bsize = gl_allocated;
2058 vec_eval = bitvector_alloc(bsize);
2059 vec_true = bitvector_alloc(bsize);
2061 if (vec_eval == NULL || vec_true == NULL) {
2062 goto cls_determined;
2064 termflag = RBCE_TERMFLAG_ALL;
2065 // need to evaluate all of them now
2069 * using bit ops invalidate all terms related to this termflag
2070 * context (only in per task vec)
2072 DPRINTK(DBG_CLASSIFY_DETAILS, "\nClassify: termflag=%lx\n", termflag);
2073 DPRINTK(DBG_CLASSIFY_DETAILS, " eval before: ");
2074 bitvector_print(DBG_CLASSIFY_DETAILS, vec_eval);
2075 DPRINTK(DBG_CLASSIFY_DETAILS, "\n true before: ");
2076 bitvector_print(DBG_CLASSIFY_DETAILS, vec_true);
2077 DPRINTK(DBG_CLASSIFY_DETAILS, "\n redo => ");
2079 if (termflag == RBCE_TERMFLAG_ALL) {
2080 DPRINTK(DBG_CLASSIFY_DETAILS, " redoall ");
2081 bitvector_zero(vec_eval);
2083 for (i = 0; i < NUM_TERM_MASK_VECTOR; i++) {
2084 if (test_bit(i, &termflag)) {
2085 bitvector_t *maskvec = gl_mask_vecs[i];
2087 DPRINTK(DBG_CLASSIFY_DETAILS, " mask(%d) ", i);
2088 bitvector_print(DBG_CLASSIFY_DETAILS, maskvec);
2089 bitvector_and_not(vec_eval, vec_eval, maskvec);
2093 bitvector_and(vec_true, vec_true, vec_eval);
2095 DPRINTK(DBG_CLASSIFY_DETAILS, "\n eval now: ");
2096 bitvector_print(DBG_CLASSIFY_DETAILS, vec_eval);
2097 DPRINTK(DBG_CLASSIFY_DETAILS, "\n");
2099 /* run through the rules in order and see what needs evaluation */
2100 list_for_each_entry(rule, &rules_list[classtype], obj.link) {
2101 if (rule->state == RBCE_RULE_ENABLED &&
2102 rule->target_class &&
2103 rule->target_class->classobj &&
2104 evaluate_rule(tsk, ns, rule, vec_eval, vec_true,
2106 tgt = rule->target_class;
2107 cls = rule->target_class->classobj;
2113 DPRINTK(DBG_CLASSIFY_RES,
2114 "==> |%s|; pid %d; euid %d; egid %d; ruid %d; rgid %d;"
2115 "tag |%s| ===> class |%s|\n",
2116 filename ? filename : tsk->comm,
2122 RBCE_DATA(tsk) ? RBCE_DATA(tsk)->app_tag : "",
2123 tgt ? tgt->obj.name : "");
2124 DPRINTK(DBG_CLASSIFY_DETAILS, " eval after: ");
2125 bitvector_print(DBG_CLASSIFY_DETAILS, vec_eval);
2126 DPRINTK(DBG_CLASSIFY_DETAILS, "\n true after: ");
2127 bitvector_print(DBG_CLASSIFY_DETAILS, vec_true);
2128 DPRINTK(DBG_CLASSIFY_DETAILS, "\n");
2130 if (!use_persistent_state) {
2132 bitvector_free(vec_eval);
2135 bitvector_free(vec_true);
2138 ckrm_core_grab(cls);
2139 read_unlock(&global_rwlock);
2143 if (RBCE_DATA(tsk)) {
2144 RBCE_DATA(tsk)->rules_version = gl_rules_version;
2149 /*****************************************************************************
2151 * Module specific utilization of core RBCE functionality
2153 * Includes support for the various classtypes
2154 * New classtypes will require extensions here
2156 *****************************************************************************/
2158 /* helper functions that are required in the extended version */
2160 static inline void rbce_tc_manual(struct task_struct *tsk)
2162 read_lock(&global_rwlock);
2164 if (!RBCE_DATA(tsk)) {
2166 (void *)create_private_data(RBCE_DATA(tsk->parent), 0);
2168 if (RBCE_DATA(tsk)) {
2169 RBCE_DATA(tsk)->evaluate = 0;
2171 read_unlock(&global_rwlock);
2175 /*****************************************************************************
2176 * load any extensions
2177 *****************************************************************************/
2179 #ifdef RBCE_EXTENSION
2180 #include "rbcemod_ext.c"
2183 /*****************************************************************************
2184 * VARIOUS CLASSTYPES
2185 *****************************************************************************/
2187 // to enable type coercion of the function pointers
2189 /*============================================================================
2190 * TASKCLASS CLASSTYPE
2191 *============================================================================*/
2193 int tc_classtype = -1;
2196 * fork callback to be registered with core module.
2198 inline static void *rbce_tc_forkcb(struct task_struct *tsk)
2200 int rule_version_changed = 1;
2201 struct ckrm_core_class *cls;
2202 read_lock(&global_rwlock);
2205 (void *)create_private_data(RBCE_DATA(tsk->parent), 0);
2206 read_unlock(&global_rwlock);
2208 if (RBCE_DATA(tsk->parent)) {
2209 rule_version_changed =
2210 (RBCE_DATA(tsk->parent)->rules_version != gl_rules_version);
2212 cls = rule_version_changed ?
2213 rbce_classify(tsk, NULL, RBCE_TERMFLAG_ALL, tc_classtype) : NULL;
2215 // note the fork notification to any user client will be sent through
2216 // the guaranteed fork-reclassification
2221 * exit callback to be registered with core module.
2223 static void rbce_tc_exitcb(struct task_struct *tsk)
2225 struct rbce_private_data *pdata;
2227 send_exit_notification(tsk);
2229 pdata = RBCE_DATA(tsk);
2230 RBCE_DATAP(tsk) = NULL;
2232 if (pdata->app_tag) {
2233 kfree(pdata->app_tag);
2235 free_private_data(pdata);
2240 #define AENT(x) [ CKRM_EVENT_##x] = #x
2241 static const char *event_names[CKRM_NUM_EVENTS] = {
2259 void *rbce_tc_classify(enum ckrm_event event, ...)
2263 struct task_struct *tsk;
2265 va_start(args, event);
2266 tsk = va_arg(args, struct task_struct *);
2269 /* we only have to deal with events between
2270 * [ CKRM_LATCHABLE_EVENTS .. CKRM_NONLATCHABLE_EVENTS )
2273 // printk("tc_classify %p:%d:%s '%s'\n",tsk,tsk->pid,
2274 // tsk->comm,event_names[event]);
2278 case CKRM_EVENT_FORK:
2279 cls = rbce_tc_forkcb(tsk);
2282 case CKRM_EVENT_EXIT:
2283 rbce_tc_exitcb(tsk);
2286 case CKRM_EVENT_EXEC:
2287 cls = rbce_classify(tsk, NULL, RBCE_TERMFLAG_CMD |
2288 RBCE_TERMFLAG_UID | RBCE_TERMFLAG_GID,
2292 case CKRM_EVENT_UID:
2293 cls = rbce_classify(tsk, NULL, RBCE_TERMFLAG_UID, tc_classtype);
2296 case CKRM_EVENT_GID:
2297 cls = rbce_classify(tsk, NULL, RBCE_TERMFLAG_GID, tc_classtype);
2300 case CKRM_EVENT_XID:
2301 cls = rbce_classify(tsk, NULL, RBCE_TERMFLAG_XID, tc_classtype);
2304 case CKRM_EVENT_LOGIN:
2305 case CKRM_EVENT_USERADD:
2306 case CKRM_EVENT_USERDEL:
2307 case CKRM_EVENT_LISTEN_START:
2308 case CKRM_EVENT_LISTEN_STOP:
2309 case CKRM_EVENT_APPTAG:
2310 /* no interest in this events .. */
2317 case CKRM_EVENT_RECLASSIFY:
2318 cls = rbce_classify(tsk, NULL, RBCE_TERMFLAG_ALL, tc_classtype);
2322 // printk("tc_classify %p:%d:%s '%s' ==> %p\n",tsk,tsk->pid,
2323 // tsk->comm,event_names[event],cls);
2328 #ifndef RBCE_EXTENSION
2329 static void rbce_tc_notify(int event, void *core, struct task_struct *tsk)
2331 printk("tc_manual %p:%d:%s '%s'\n", tsk, tsk->pid, tsk->comm,
2332 event_names[event]);
2333 if (event != CKRM_EVENT_MANUAL)
2335 rbce_tc_manual(tsk);
2339 static struct ckrm_eng_callback rbce_taskclass_ecbs = {
2340 .c_interest = (unsigned long)(-1), // set whole bitmap
2341 .classify = (ce_classify_fct_t) rbce_tc_classify,
2342 .class_delete = rbce_class_deletecb,
2343 #ifndef RBCE_EXTENSION
2344 .n_interest = (1 << CKRM_EVENT_MANUAL),
2345 .notify = (ce_notify_fct_t) rbce_tc_notify,
2346 .always_callback = 0,
2348 .n_interest = (unsigned long)(-1), // set whole bitmap
2349 .notify = (ce_notify_fct_t) rbce_tc_ext_notify,
2350 .class_add = rbce_class_addcb,
2351 .always_callback = 1,
2355 /*============================================================================
2357 *============================================================================*/
2359 int sc_classtype = -1;
2361 void *rbce_sc_classify(enum ckrm_event event, ...)
2363 // no special consideratation
2366 struct task_struct *tsk;
2367 struct ckrm_net_struct *ns;
2369 va_start(args, event);
2370 ns = va_arg(args, struct ckrm_net_struct *);
2371 tsk = va_arg(args, struct task_struct *);
2374 result = rbce_classify(tsk, ns, RBCE_TERMFLAG_ALL, sc_classtype);
2376 DPRINTK(DBG_CLASSIFY_RES,
2377 "==> %d.%d.%d.%d\\%d , %p:%d:%s '%s' => %p\n",
2378 NIPQUAD(ns->ns_daddrv4), ns->ns_dport,
2379 tsk, tsk ? tsk->pid : 0, tsk ? tsk->comm : "-",
2380 event_names[event], result);
2384 static struct ckrm_eng_callback rbce_acceptQclass_ecbs = {
2385 .c_interest = (unsigned long)(-1),
2386 .always_callback = 0, // enable during debugging only
2387 .classify = (ce_classify_fct_t) & rbce_sc_classify,
2388 .class_delete = rbce_class_deletecb,
2391 /*============================================================================
2392 * Module Initialization ...
2393 *============================================================================*/
2395 #define TASKCLASS_NAME "taskclass"
2396 #define SOCKCLASS_NAME "socket_class"
2398 struct ce_regtable_struct {
2400 struct ckrm_eng_callback *cbs;
2404 struct ce_regtable_struct ce_regtable[] = {
2405 {TASKCLASS_NAME, &rbce_taskclass_ecbs, &tc_classtype},
2406 {SOCKCLASS_NAME, &rbce_acceptQclass_ecbs, &sc_classtype},
2410 static int register_classtype_engines(void)
2413 struct ce_regtable_struct *ceptr = ce_regtable;
2415 while (ceptr->name) {
2416 rc = ckrm_register_engine(ceptr->name, ceptr->cbs);
2417 printk("ce register with <%s> typeId=%d\n", ceptr->name, rc);
2418 if ((rc < 0) && (rc != -ENOENT))
2421 *ceptr->clsvar = rc;
2427 static void unregister_classtype_engines(void)
2430 struct ce_regtable_struct *ceptr = ce_regtable;
2432 while (ceptr->name) {
2433 if (*ceptr->clsvar >= 0) {
2434 printk("ce unregister with <%s>\n", ceptr->name);
2435 rc = ckrm_unregister_engine(ceptr->name);
2436 printk("ce unregister with <%s> rc=%d\n", ceptr->name,
2438 *ceptr->clsvar = -1;
2444 // =========== /proc/sysctl/debug/rbce debug stuff =============
2447 static struct ctl_table_header *rbce_sysctl_table_header;
2449 #define CTL_RBCE_DEBUG (201) // picked some number.. dont know algo to pick
2450 static struct ctl_table rbce_entry_table[] = {
2452 .ctl_name = CTL_RBCE_DEBUG,
2455 .maxlen = sizeof(int),
2457 .proc_handler = &proc_dointvec,
2462 static struct ctl_table rbce_root_table[] = {
2464 .ctl_name = CTL_DEBUG,
2465 .procname = "debug",
2469 .child = rbce_entry_table},
2473 static inline void start_debug(void)
2475 rbce_sysctl_table_header = register_sysctl_table(rbce_root_table, 1);
2477 static inline void stop_debug(void)
2479 if (rbce_sysctl_table_header)
2480 unregister_sysctl_table(rbce_sysctl_table_header);
2485 static inline void start_debug(void)
2488 static inline void stop_debug(void)
2494 extern int rbce_mkdir(struct inode *, struct dentry *, int);
2495 extern int rbce_rmdir(struct inode *, struct dentry *);
2496 extern int rbce_create_magic(void);
2497 extern int rbce_clear_magic(void);
2499 rbce_eng_callback_t rcfs_ecbs = {
2506 /* ======================= Module definition Functions ====================== */
2512 printk("<1>\nInstalling \'%s\' module\n", modname);
2514 for (i = 0; i < CKRM_MAX_CLASSTYPES; i++) {
2515 INIT_LIST_HEAD(&rules_list[i]);
2518 rc = init_rbce_ext_pre();
2523 rc = register_classtype_engines();
2526 goto out_unreg_ckrm; // need to remove anyone opened
2528 /* register any other class type engine here */
2530 rc = rcfs_register_engine(&rcfs_ecbs);
2533 goto out_unreg_ckrm;
2536 rc = rbce_create_magic();
2539 goto out_unreg_rcfs;
2544 rc = init_rbce_ext_post();
2549 return 0; // SUCCESS
2555 rcfs_unregister_engine(&rcfs_ecbs);
2557 unregister_classtype_engines();
2561 printk("<1>%s: error installing rc=%d line=%d\n", __FUNCTION__, rc,
2566 void exit_rbce(void)
2570 printk("<1>Removing \'%s\' module\n", modname);
2575 // Print warnings if lists are not empty, which is a bug
2576 if (!list_empty(&class_list)) {
2577 printk("exit_rbce: Class list is not empty\n");
2580 for (i = 0; i < CKRM_MAX_CLASSTYPES; i++) {
2581 if (!list_empty(&rules_list[i])) {
2582 printk("exit_rbce: Rules list for classtype %d"
2583 " is not empty\n", i);
2590 rcfs_unregister_engine(&rcfs_ecbs);
2591 unregister_classtype_engines();
2592 free_all_private_data();
2595 EXPORT_SYMBOL(get_rule);
2596 EXPORT_SYMBOL(rule_exists);
2597 EXPORT_SYMBOL(change_rule);
2598 EXPORT_SYMBOL(delete_rule);
2599 EXPORT_SYMBOL(rename_rule);
2600 EXPORT_SYMBOL(reclassify_pid);
2601 EXPORT_SYMBOL(set_tasktag);
2603 module_init(init_rbce);
2604 module_exit(exit_rbce);