1 /* Rule-based Classification Engine (RBCE) and
2 * Consolidated RBCE module code (combined)
4 * Copyright (C) Hubertus Franke, IBM Corp. 2003
5 * (C) Chandra Seetharaman, IBM Corp. 2003
6 * (C) Vivek Kashyap, IBM Corp. 2004
8 * Module for loading of classification policies and providing
9 * a user API for Class-based Kernel Resource Management (CKRM)
11 * Latest version, more details at http://ckrm.sf.net
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it would be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
27 * Created. First cut with much scope for cleanup !
29 * Made modifications to suit the new RBCE module.
30 * Made modifications to address sampling and delivery
32 * Integrated changes from original RBCE module
34 * Merged RBCE and CRBCE into common code base
36 * Incorporated listen call back and IPv4 match support
38 * Added Multi-Classtype Support
41 #include <linux/init.h>
42 #include <linux/module.h>
43 #include <linux/kernel.h>
45 #include <asm/uaccess.h>
47 #include <linux/mount.h>
48 #include <linux/proc_fs.h>
49 #include <linux/limits.h>
50 #include <linux/pid.h>
51 #include <linux/sysctl.h>
53 #include <linux/ckrm_rc.h>
54 #include <linux/ckrm_ce.h>
55 #include <linux/ckrm_net.h>
56 #include "bitvector.h"
57 #include <linux/rbce.h>
61 MODULE_DESCRIPTION(RBCE_MOD_DESCR);
62 MODULE_AUTHOR("Hubertus Franke, Chandra Seetharaman (IBM)");
63 MODULE_LICENSE("GPL");
65 static char modname[] = RBCE_MOD_NAME;
67 /* ==================== typedef, global variables etc., ==================== */
68 struct named_obj_hdr {
69 struct list_head link;
74 #define GET_REF(x) ((x)->obj.referenced)
75 #define INC_REF(x) (GET_REF(x)++)
76 #define DEC_REF(x) (--GET_REF(x))
78 struct named_obj_hdr obj;
84 RBCE_RULE_CMD_PATH = 1, // full qualified path
85 RBCE_RULE_CMD, // basename of the command
86 RBCE_RULE_ARGS, // arguments of the command
87 RBCE_RULE_REAL_UID, // task's real uid
88 RBCE_RULE_REAL_GID, // task's real gid
89 RBCE_RULE_EFFECTIVE_UID, // task's effective uid
90 RBCE_RULE_EFFECTIVE_GID, // task's effective gid
91 RBCE_RULE_APP_TAG, // task's application tag
92 RBCE_RULE_IPV4, // IP address of listen(), ipv4 format
93 RBCE_RULE_IPV6, // IP address of listen(), ipv6 format
94 RBCE_RULE_XID, // VSERVER
95 RBCE_RULE_DEP_RULE, // dependent rule; must be the first term
96 RBCE_RULE_INVALID, // invalid, for filler
97 RBCE_RULE_INVALID2, // invalid, for filler
107 struct rbce_rule_term {
109 rbce_operator_t operator;
111 char *string; // path, cmd, arg, tag, ipv4 and ipv6
112 long id; // uid, gid, euid, egid
113 struct rbce_rule *deprule;
118 struct named_obj_hdr obj;
119 struct rbce_class *target_class;
122 int *terms; // vector of indices into the global term vector
123 int index; // index of this rule into the global term vector
124 int termflag; // which term ids would require a recalculation
125 int do_opt; // do we have to consider this rule during optimize
126 char *strtab; // string table to store the strings of all terms
127 int order; // order of execution of this rule
128 int state; // RBCE_RULE_ENABLED/RBCE_RULE_DISABLED
132 #define RBCE_RULE_DISABLED 0
133 #define RBCE_RULE_ENABLED 1
136 // Data structures and macros used for optimization
137 #define RBCE_TERM_CMD (0)
138 #define RBCE_TERM_UID (1)
139 #define RBCE_TERM_GID (2)
140 #define RBCE_TERM_TAG (3)
141 #define RBCE_TERM_IPV4 (4)
142 #define RBCE_TERM_IPV6 (5)
143 #define RBCE_TERM_XID (6)
145 #define NUM_TERM_MASK_VECTOR (7) // must be one more the last RBCE_TERM_...
147 // Rule flags. 1 bit for each type of rule term
148 #define RBCE_TERMFLAG_CMD (1 << RBCE_TERM_CMD)
149 #define RBCE_TERMFLAG_UID (1 << RBCE_TERM_UID)
150 #define RBCE_TERMFLAG_GID (1 << RBCE_TERM_GID)
151 #define RBCE_TERMFLAG_TAG (1 << RBCE_TERM_TAG)
152 #define RBCE_TERMFLAG_IPV4 (1 << RBCE_TERM_IPV4)
153 #define RBCE_TERMFLAG_IPV6 (1 << RBCE_TERM_IPV6)
154 #define RBCE_TERMFLAG_XID (1 << RBCE_TERM_XID)
155 #define RBCE_TERMFLAG_ALL (RBCE_TERMFLAG_CMD | RBCE_TERMFLAG_UID | \
156 RBCE_TERMFLAG_GID | RBCE_TERMFLAG_TAG | RBCE_TERMFLAG_XID | \
157 RBCE_TERMFLAG_IPV4 | RBCE_TERMFLAG_IPV6)
159 int termop_2_vecidx[RBCE_RULE_INVALID] = {
160 [RBCE_RULE_CMD_PATH] = RBCE_TERM_CMD,
161 [RBCE_RULE_CMD] = RBCE_TERM_CMD,
162 [RBCE_RULE_ARGS] = RBCE_TERM_CMD,
163 [RBCE_RULE_REAL_UID] = RBCE_TERM_UID,
164 [RBCE_RULE_REAL_GID] = RBCE_TERM_GID,
165 [RBCE_RULE_EFFECTIVE_UID] = RBCE_TERM_UID,
166 [RBCE_RULE_EFFECTIVE_GID] = RBCE_TERM_GID,
167 [RBCE_RULE_XID] = RBCE_TERM_XID,
168 [RBCE_RULE_APP_TAG] = RBCE_TERM_TAG,
169 [RBCE_RULE_IPV4] = RBCE_TERM_IPV4,
170 [RBCE_RULE_IPV6] = RBCE_TERM_IPV6,
171 [RBCE_RULE_DEP_RULE] = -1
174 #define TERMOP_2_TERMFLAG(x) (1 << termop_2_vecidx[x])
175 #define TERM_2_TERMFLAG(x) (1 << x)
177 #define POLICY_INC_NUMTERMS (BITS_PER_LONG) // No. of terms added at a time
178 #define POLICY_ACTION_NEW_VERSION 0x01 // Force reallocation
179 #define POLICY_ACTION_REDO_ALL 0x02 // Recompute all rule flags
180 #define POLICY_ACTION_PACK_TERMS 0x04 // Time to pack the terms
182 const int use_persistent_state = 1;
184 struct ckrm_eng_callback ckrm_ecbs;
188 static int gl_bitmap_version, gl_action, gl_num_terms;
189 static int gl_allocated, gl_released;
190 struct rbce_rule_term *gl_terms;
191 bitvector_t *gl_mask_vecs[NUM_TERM_MASK_VECTOR];
194 static void optimize_policy(void);
196 #ifndef CKRM_MAX_CLASSTYPES
197 #define CKRM_MAX_CLASSTYPES 32
200 struct list_head rules_list[CKRM_MAX_CLASSTYPES];
201 LIST_HEAD(class_list); // List of classes used
203 static int gl_num_rules;
204 static int gl_rules_version;
205 int rbce_enabled = 1;
206 static rwlock_t global_rwlock = RW_LOCK_UNLOCKED;
208 * One lock to protect them all !!!
209 * Additions, deletions to rules must
210 * happen with this lock being held in write mode.
211 * Access(read/write) to any of the data structures must happen
212 * with this lock held in read mode.
213 * Since, rule related changes do not happen very often it is ok to
214 * have single rwlock.
218 * data structure rbce_private_data holds the bit vector 'eval' which
219 * specifies if rules and terms of rules are evaluated against the task
220 * and if they were evaluated, bit vector 'true' holds the result of that
223 * This data structure is maintained in a task, and the bitvectors are
224 * updated only when needed.
226 * Each rule and each term of a rule has a corresponding bit in the vector.
229 struct rbce_private_data {
230 struct rbce_ext_private_data ext_data;
231 int evaluate; // whether to evaluate rules or not ?
232 int rules_version; // whether to evaluate rules or not ?
234 unsigned long bitmap_version;
237 char data[0]; // eval points to this variable size data array
240 #define RBCE_DATA(tsk) ((struct rbce_private_data*)((tsk)->ce_data))
241 #define RBCE_DATAP(tsk) ((tsk)->ce_data)
243 /* ======================= DEBUG Functions ========================= */
247 int rbcedebug = 0x00;
249 #define DBG_CLASSIFY_RES ( 0x01 )
250 #define DBG_CLASSIFY_DETAILS ( 0x02 )
251 #define DBG_OPTIMIZATION ( 0x04 )
252 #define DBG_SHOW_RESCTL ( 0x08 )
253 #define DBG_CLASS ( 0x10 )
254 #define DBG_RULE ( 0x20 )
255 #define DBG_POLICY ( 0x40 )
257 #define DPRINTK(x, y...) if (rbcedebug & (x)) printk(KERN_DEBUG y)
258 // debugging selectively enabled through /proc/sys/debug/rbce
260 static void print_context_vectors(void)
264 if ((rbcedebug & DBG_OPTIMIZATION) == 0) {
267 for (i = 0; i < NUM_TERM_MASK_VECTOR; i++) {
268 printk(KERN_DEBUG "%d: ", i);
269 bitvector_print(DBG_OPTIMIZATION, gl_mask_vecs[i]);
270 printk(KERN_DEBUG "\n");
275 #define DPRINTK(x, y...)
276 #define print_context_vectors(x)
279 /* ====================== VSERVER support ========================== */
280 #define CONFIG_VSERVER
281 #ifdef CONFIG_VSERVER
282 #include <linux/vs_base.h>
284 typedef unsigned int xid_t;
285 #define vx_task_xid(t) (0)
288 /* ======================= Helper Functions ========================= */
292 static struct ckrm_core_class *rbce_classify(struct task_struct *,
293 struct ckrm_net_struct *,
294 unsigned long, int classtype);
296 static inline struct rbce_rule *find_rule_name(const char *name)
298 struct named_obj_hdr *pos;
301 for (i = 0; i < CKRM_MAX_CLASSTYPES; i++) {
302 list_for_each_entry(pos, &rules_list[i], link) {
303 if (!strcmp(pos->name, name)) {
304 return ((struct rbce_rule *)pos);
311 static inline struct rbce_class *find_class_name(const char *name)
313 struct named_obj_hdr *pos;
315 list_for_each_entry(pos, &class_list, link) {
316 if (!strcmp(pos->name, name))
317 return (struct rbce_class *)pos;
323 * Insert the given rule at the specified order
324 * order = -1 ==> insert at the tail.
326 * Caller must hold global_rwlock in write mode.
328 static int insert_rule(struct rbce_rule *rule, int order)
330 #define ORDER_COUNTER_INCR 10
331 static int order_counter;
333 struct list_head *head = &rules_list[rule->classtype];
334 struct list_head *insert = head;
335 struct rbce_rule *tmp;
337 if (gl_num_rules == 0) {
343 rule->order = order_counter;
344 // FIXME: order_counter overflow/wraparound!!
345 order_counter += ORDER_COUNTER_INCR;
348 old_counter = order_counter;
349 if (order_counter < order) {
350 order_counter = order;
353 order_counter += ORDER_COUNTER_INCR;
354 list_for_each_entry(tmp, head, obj.link) {
355 if (rule->order == tmp->order) {
356 order_counter = old_counter;
359 if (rule->order < tmp->order) {
360 insert = &tmp->obj.link;
365 list_add_tail(&rule->obj.link, insert);
366 // protect the module from removed when any rule is
368 try_module_get(THIS_MODULE);
375 * Remove the rule and reinsert at the specified order.
377 * Caller must hold global_rwlock in write mode.
379 static int reinsert_rule(struct rbce_rule *rule, int order)
381 list_del(&rule->obj.link);
384 module_put(THIS_MODULE);
385 return insert_rule(rule, order);
389 * Get a refernece to the class, create one if it doesn't exist
391 * Caller need to hold global_rwlock in write mode.
395 static struct rbce_class *create_rbce_class(const char *classname,
396 int classtype, void *classobj)
398 struct rbce_class *cls;
400 if (classtype >= CKRM_MAX_CLASSTYPES) {
402 "ckrm_classobj returned %d as classtype which cannot "
403 " be handled by RBCE\n", classtype);
407 cls = kmalloc(sizeof(struct rbce_class), GFP_ATOMIC);
411 cls->obj.name = kmalloc(strlen(classname) + 1, GFP_ATOMIC);
414 cls->classobj = classobj;
415 strcpy(cls->obj.name, classname);
416 list_add_tail(&cls->obj.link, &class_list);
417 cls->classtype = classtype;
425 static struct rbce_class *get_class(char *classname, int *classtype)
427 struct rbce_class *cls;
433 cls = find_class_name(classname);
437 *classtype = cls->classtype;
442 classobj = ckrm_classobj(classname, classtype);
447 return create_rbce_class(classname, *classtype, classobj);
451 * Drop a refernece to the class, create one if it doesn't exist
453 * Caller need to hold global_rwlock in write mode.
455 static void put_class(struct rbce_class *cls)
458 if (DEC_REF(cls) <= 0) {
459 list_del(&cls->obj.link);
460 kfree(cls->obj.name);
468 * Callback from core when a class is added
471 #ifdef RBCE_EXTENSION
472 static void rbce_class_addcb(const char *classname, void *clsobj, int classtype)
474 struct rbce_class *cls;
476 write_lock(&global_rwlock);
477 cls = find_class_name((char *)classname);
479 cls->classobj = clsobj;
481 cls = create_rbce_class(classname, classtype, clsobj);
484 notify_class_action(cls, 1);
485 write_unlock(&global_rwlock);
491 * Callback from core when a class is deleted.
494 rbce_class_deletecb(const char *classname, void *classobj, int classtype)
496 static struct rbce_class *cls;
497 struct named_obj_hdr *pos;
498 struct rbce_rule *rule;
500 write_lock(&global_rwlock);
501 cls = find_class_name(classname);
503 if (cls->classobj != classobj) {
504 printk(KERN_ERR "rbce: class %s changed identity\n",
507 notify_class_action(cls, 0);
508 cls->classobj = NULL;
509 list_for_each_entry(pos, &rules_list[classtype], link) {
510 rule = (struct rbce_rule *)pos;
511 if (rule->target_class) {
513 (rule->target_class->obj.name, classname)) {
515 rule->target_class = NULL;
516 rule->classtype = -1;
520 if ((cls = find_class_name(classname)) != NULL) {
522 "rbce ERROR: class %s exists in rbce after "
523 "removal in core\n", classname);
526 write_unlock(&global_rwlock);
531 * Allocate an index in the global term vector
532 * On success, returns the index. On failure returns -errno.
533 * Caller must hold the global_rwlock in write mode as global data is
536 static int alloc_term_index(void)
538 int size = gl_allocated;
540 if (gl_num_terms >= size) {
542 struct rbce_rule_term *oldv, *newv;
543 int newsize = size + POLICY_INC_NUMTERMS;
547 kmalloc(newsize * sizeof(struct rbce_rule_term),
552 memcpy(newv, oldv, size * sizeof(struct rbce_rule_term));
553 for (i = size; i < newsize; i++) {
557 gl_allocated = newsize;
560 gl_action |= POLICY_ACTION_NEW_VERSION;
561 DPRINTK(DBG_OPTIMIZATION,
562 "alloc_term_index: Expanding size from %d to %d\n",
565 return gl_num_terms++;
569 * Release an index in the global term vector
571 * Caller must hold the global_rwlock in write mode as the global data
574 static void release_term_index(int idx)
576 if ((idx < 0) || (idx > gl_num_terms))
579 gl_terms[idx].op = -1;
581 if ((gl_released > POLICY_INC_NUMTERMS) &&
583 (gl_num_terms - gl_released + POLICY_INC_NUMTERMS))) {
584 gl_action |= POLICY_ACTION_PACK_TERMS;
590 * Release the indices, string memory, and terms associated with the given
593 * Caller should be holding global_rwlock
595 static void __release_rule(struct rbce_rule *rule)
597 int i, *terms = rule->terms;
599 // remove memory and references from other rules
600 for (i = rule->num_terms; --i >= 0;) {
601 struct rbce_rule_term *term = &gl_terms[terms[i]];
603 if (term->op == RBCE_RULE_DEP_RULE) {
604 DEC_REF(term->u.deprule);
606 release_term_index(terms[i]);
621 * delete the given rule and all memory associated with it.
623 * Caller is responsible for protecting the global data
625 static inline int __delete_rule(struct rbce_rule *rule)
627 // make sure we are not referenced by other rules
631 __release_rule(rule);
632 put_class(rule->target_class);
633 release_term_index(rule->index);
634 list_del(&rule->obj.link);
637 module_put(THIS_MODULE);
638 kfree(rule->obj.name);
644 * Optimize the rule evaluation logic
646 * Caller must hold global_rwlock in write mode.
648 static void optimize_policy(void)
651 struct rbce_rule *rule;
652 struct rbce_rule_term *terms;
655 bitvector_t **mask_vecs;
660 * Due to dynamic rule addition/deletion of rules the term
661 * vector can get sparse. As a result the bitvectors grow as we don't
662 * reuse returned indices. If it becomes sparse enough we pack them
666 pack_terms = (gl_action & POLICY_ACTION_PACK_TERMS);
667 DPRINTK(DBG_OPTIMIZATION,
668 "----- Optimize Policy ----- act=%x pt=%d (a=%d n=%d r=%d)\n",
669 gl_action, pack_terms, gl_allocated, gl_num_terms, gl_released);
672 int nsz = ALIGN((gl_num_terms - gl_released),
673 POLICY_INC_NUMTERMS);
675 struct rbce_rule_term *newterms;
679 kmalloc(nsz * sizeof(struct rbce_rule_term), GFP_ATOMIC);
681 for (ii = 0; ii < CKRM_MAX_CLASSTYPES; ii++) {
682 // FIXME: check only for task class types
683 list_for_each_entry_reverse(rule,
686 rule->index = newidx++;
687 for (i = rule->num_terms; --i >= 0;) {
688 int idx = rule->terms[i];
689 newterms[newidx] = terms[idx];
690 rule->terms[i] = newidx++;
697 gl_num_terms = newidx;
700 gl_action &= ~POLICY_ACTION_PACK_TERMS;
701 gl_action |= POLICY_ACTION_NEW_VERSION;
705 num_terms = gl_num_terms;
706 bsize = gl_allocated / 8 + sizeof(bitvector_t);
707 mask_vecs = gl_mask_vecs;
710 if (gl_action & POLICY_ACTION_NEW_VERSION) {
711 /* allocate new mask vectors */
712 char *temp = kmalloc(NUM_TERM_MASK_VECTOR * bsize, GFP_ATOMIC);
714 DPRINTK(DBG_OPTIMIZATION,
715 "------ allocmasks act=%x ------- ver=%d\n", gl_action,
720 if (mask_vecs[0]) {// index 0 has the alloc returned address
723 for (i = 0; i < NUM_TERM_MASK_VECTOR; i++) {
724 mask_vecs[i] = (bitvector_t *) (temp + i * bsize);
725 bitvector_init(mask_vecs[i], gl_allocated);
727 gl_action &= ~POLICY_ACTION_NEW_VERSION;
728 gl_action |= POLICY_ACTION_REDO_ALL;
732 /* We do two things here at once
733 * 1) recompute the rulemask for each required rule
734 * we guarantee proper dependency order during creation time and
735 * by reversely running through this list.
736 * 2) recompute the mask for each term and rule, if required
739 redoall = gl_action & POLICY_ACTION_REDO_ALL;
740 gl_action &= ~POLICY_ACTION_REDO_ALL;
742 DPRINTK(DBG_OPTIMIZATION, "------- run act=%x -------- redoall=%d\n",
744 for (ii = 0; ii < CKRM_MAX_CLASSTYPES; ii++) {
745 // FIXME: check only for task class types
746 list_for_each_entry_reverse(rule, &rules_list[ii], obj.link) {
747 unsigned long termflag;
749 if (!redoall && !rule->do_opt)
752 for (i = rule->num_terms; --i >= 0;) {
753 int j, idx = rule->terms[i];
754 struct rbce_rule_term *term = &terms[idx];
755 int vecidx = termop_2_vecidx[term->op];
758 termflag |= term->u.deprule->termflag;
759 /* mark this term belonging to all
760 contexts of deprule */
761 for (j = 0; j < NUM_TERM_MASK_VECTOR;
763 if (term->u.deprule->termflag
771 termflag |= TERM_2_TERMFLAG(vecidx);
772 /* mark this term belonging to
773 a particular context */
774 bitvector_set(idx, mask_vecs[vecidx]);
777 for (i = 0; i < NUM_TERM_MASK_VECTOR; i++) {
778 if (termflag & (1 << i)) {
779 bitvector_set(rule->index,
783 rule->termflag = termflag;
785 DPRINTK(DBG_OPTIMIZATION, "r-%s: %x %d\n",
786 rule->obj.name, rule->termflag, rule->index);
789 print_context_vectors();
793 /* ======================= Rule related Functions ========================= */
796 * Caller need to hold global_rwlock in write mode.
799 fill_rule(struct rbce_rule *newrule, struct rbce_rule_term *terms, int nterms)
801 char *class, *strtab;
802 int i, j, order, state, real_nterms, index;
803 int strtablen, rc = 0, counter;
804 struct rbce_rule_term *term = NULL;
805 struct rbce_class *targetcls = NULL;
806 struct rbce_rule *deprule;
811 // Digest filled terms.
813 strtab = class = NULL;
818 for (i = 0; i < nterms; i++) {
819 if (terms[i].op != RBCE_RULE_INVALID) {
822 switch (terms[i].op) {
823 case RBCE_RULE_DEP_RULE:
824 // check if the depend rule is valid
826 deprule = find_rule_name(terms[i].u.string);
827 if (!deprule || deprule == newrule) {
831 // make sure _a_ depend rule
832 // appears in only one term.
833 for (j = 0; j < i; j++) {
836 && terms[j].u.deprule ==
842 terms[i].u.deprule = deprule;
845 // +depend is acceptable and -depend is not
846 if (terms[i].operator != TOKEN_OP_DEP_DEL) {
847 terms[i].operator = RBCE_EQUAL;
854 case RBCE_RULE_CMD_PATH:
857 case RBCE_RULE_APP_TAG:
860 // sum up the string length
861 strtablen += strlen(terms[i].u.string) + 1;
868 switch (terms[i].operator) {
870 order = terms[i].u.id;
877 state = terms[i].u.id != 0;
880 class = terms[i].u.string;
888 // Check if class was specified
891 if ((targetcls = get_class(class, &classtype)) == NULL) {
895 put_class(newrule->target_class);
897 newrule->target_class = targetcls;
898 newrule->classtype = classtype;
900 if (!newrule->target_class) {
906 newrule->state = state;
909 newrule->order = order;
911 newrule->terms = kmalloc(real_nterms * sizeof(int), GFP_ATOMIC);
912 if (!newrule->terms) {
916 newrule->num_terms = real_nterms;
917 if (strtablen && ((strtab = kmalloc(strtablen, GFP_ATOMIC)) == NULL)) {
922 if (newrule->index == -1) {
923 index = alloc_term_index();
928 newrule->index = index;
929 term = &gl_terms[newrule->index];
930 term->op = RBCE_RULE_DEP_RULE;
931 term->u.deprule = newrule;
933 newrule->strtab = strtab;
934 newrule->termflag = 0;
936 // Fill the term vector
939 for (i = 0; i < nterms; i++) {
940 if (terms[i].op == RBCE_RULE_INVALID) {
944 newrule->terms[counter] = alloc_term_index();
945 if (newrule->terms[counter] < 0) {
946 for (j = 0; j < counter; j++) {
947 release_term_index(newrule->terms[j]);
952 term = &gl_terms[newrule->terms[counter]];
953 term->op = terms[i].op;
954 term->operator = terms[i].operator;
955 switch (terms[i].op) {
956 case RBCE_RULE_CMD_PATH:
959 case RBCE_RULE_APP_TAG:
962 term->u.string = &strtab[strtablen];
963 strcpy(term->u.string, terms[i].u.string);
964 strtablen = strlen(term->u.string) + 1;
967 case RBCE_RULE_REAL_UID:
968 case RBCE_RULE_REAL_GID:
969 case RBCE_RULE_EFFECTIVE_UID:
970 case RBCE_RULE_EFFECTIVE_GID:
972 term->u.id = terms[i].u.id;
975 case RBCE_RULE_DEP_RULE:
976 term->u.deprule = terms[i].u.deprule;
977 INC_REF(term->u.deprule);
988 put_class(targetcls);
991 release_term_index(index);
993 kfree(newrule->terms);
1000 int change_rule(const char *rname, char *rdefn)
1002 struct rbce_rule *rule = NULL, *deprule;
1003 struct rbce_rule_term *new_terms = NULL, *term, *terms;
1004 int nterms, new_term_mask = 0, oterms, tot_terms;
1005 int i, j, k, rc, new_order = 0;
1007 if ((nterms = rules_parse(rdefn, &new_terms, &new_term_mask)) <= 0) {
1008 return !nterms ? -EINVAL : nterms;
1011 write_lock(&global_rwlock);
1012 rule = find_rule_name(rname);
1014 rule = kmalloc(sizeof(struct rbce_rule), GFP_ATOMIC);
1016 rule->obj.name = kmalloc(strlen(rname) + 1, GFP_ATOMIC);
1017 if (rule->obj.name) {
1018 strcpy(rule->obj.name, rname);
1022 rule->state = RBCE_RULE_ENABLED;
1023 rule->target_class = NULL;
1024 rule->classtype = -1;
1027 INIT_LIST_HEAD(&rule->obj.link);
1028 rc = fill_rule(rule, new_terms, nterms);
1034 rule->order)) == 0) {
1039 __delete_rule(rule);
1050 write_unlock(&global_rwlock);
1054 oterms = rule->num_terms;
1055 tot_terms = nterms + oterms;
1057 terms = kmalloc(tot_terms * sizeof(struct rbce_rule_term), GFP_ATOMIC);
1061 write_unlock(&global_rwlock);
1065 new_term_mask &= ~(1 << RBCE_RULE_DEP_RULE);
1066 //ignore the new deprule terms for the first iteration.
1067 // taken care of later.
1068 for (i = 0; i < oterms; i++) {
1069 term = &gl_terms[rule->terms[i]]; // old term
1071 if ((1 << term->op) & new_term_mask) {
1072 // newrule has this attr/value
1073 for (j = 0; j < nterms; j++) {
1074 if (term->op == new_terms[j].op) {
1075 terms[i].op = new_terms[j].op;
1076 terms[i].operator = new_terms[j].
1079 new_terms[j].u.string;
1080 new_terms[j].op = RBCE_RULE_INVALID2;
1085 terms[i].op = term->op;
1086 terms[i].operator = term->operator;
1087 terms[i].u.string = term->u.string;
1091 i = oterms; // for readability
1093 for (j = 0; j < nterms; j++) {
1094 // handled in the previous iteration
1095 if (new_terms[j].op == RBCE_RULE_INVALID2) {
1099 if (new_terms[j].op == RBCE_RULE_DEP_RULE) {
1100 if (new_terms[j].operator == TOKEN_OP_DEP) {
1101 // "depend=rule" deletes all depends in the
1102 // original rule so, delete all depend rule
1103 // terms in the original rule
1104 for (k = 0; k < oterms; k++) {
1105 if (terms[k].op == RBCE_RULE_DEP_RULE) {
1106 terms[k].op = RBCE_RULE_INVALID;
1109 // must copy the new deprule term
1111 // delete the depend rule term if was defined
1112 // in the original rule for both +depend
1114 deprule = find_rule_name(new_terms[j].u.string);
1116 for (k = 0; k < oterms; k++) {
1119 && terms[k].u.deprule ==
1127 if (new_terms[j].operator == TOKEN_OP_DEP_DEL) {
1128 // No need to copy the new deprule term
1133 if ((new_terms[j].op == RBCE_RULE_INVALID) &&
1134 (new_terms[j].operator == TOKEN_OP_ORDER)) {
1138 terms[i].op = new_terms[j].op;
1139 terms[i].operator = new_terms[j].operator;
1140 terms[i].u.string = new_terms[j].u.string;
1142 new_terms[j].op = RBCE_RULE_INVALID2;
1147 // convert old deprule pointers to name pointers.
1148 for (i = 0; i < oterms; i++) {
1149 if (terms[i].op != RBCE_RULE_DEP_RULE)
1151 terms[i].u.string = terms[i].u.deprule->obj.name;
1155 __release_rule(rule);
1158 rc = fill_rule(rule, terms, tot_terms);
1159 if (rc == 0 && new_order) {
1160 rc = reinsert_rule(rule, rule->order);
1162 if (rc != 0) { // rule creation/insertion failed
1163 __delete_rule(rule);
1168 write_unlock(&global_rwlock);
1175 * Delete the specified rule.
1178 int delete_rule(const char *rname)
1181 struct rbce_rule *rule;
1183 write_lock(&global_rwlock);
1185 if ((rule = find_rule_name(rname)) == NULL) {
1186 write_unlock(&global_rwlock);
1189 rc = __delete_rule(rule);
1190 if (rbce_enabled && (gl_action & POLICY_ACTION_PACK_TERMS)) {
1193 write_unlock(&global_rwlock);
1195 DPRINTK(DBG_RULE, "delete rule %s\n", rname);
1200 * copy the rule specified by rname and to the given result string.
1203 void get_rule(const char *rname, char *result)
1206 struct rbce_rule *rule;
1207 struct rbce_rule_term *term;
1208 char *cp = result, oper, idtype[3], str[5];
1210 read_lock(&global_rwlock);
1212 rule = find_rule_name(rname);
1214 for (i = 0; i < rule->num_terms; i++) {
1215 term = gl_terms + rule->terms[i];
1217 case RBCE_RULE_REAL_UID:
1218 strcpy(idtype, "u");
1220 case RBCE_RULE_REAL_GID:
1221 strcpy(idtype, "g");
1223 case RBCE_RULE_EFFECTIVE_UID:
1224 strcpy(idtype, "eu");
1226 case RBCE_RULE_EFFECTIVE_GID:
1227 strcpy(idtype, "eg");
1230 strcpy(idtype, "x");
1232 if (term->operator == RBCE_LESS_THAN) {
1234 } else if (term->operator == RBCE_GREATER_THAN) {
1236 } else if (term->operator == RBCE_NOT) {
1242 sprintf(cp, "%sid%c%ld,", idtype, oper,
1245 case RBCE_RULE_CMD_PATH:
1246 strcpy(str, "path");
1251 case RBCE_RULE_ARGS:
1252 strcpy(str, "args");
1254 case RBCE_RULE_APP_TAG:
1257 case RBCE_RULE_IPV4:
1258 strcpy(str, "ipv4");
1260 case RBCE_RULE_IPV6:
1261 strcpy(str, "ipv6");
1264 sprintf(cp, "%s=%s,", str, term->u.string);
1266 case RBCE_RULE_DEP_RULE:
1268 sprintf(cp, "depend=%s,",
1269 term->u.deprule->obj.name);
1275 if (!rule->num_terms) {
1276 cp += sprintf(cp, "***** no terms defined ***** ");
1280 sprintf(cp, "order=%d,state=%d,", rule->order, rule->state);
1282 sprintf(cp, "class=%s",
1283 rule->target_class ? rule->target_class->obj.
1284 name : "***** REMOVED *****");
1287 sprintf(result, "***** Rule %s doesn't exist *****", rname);
1290 read_unlock(&global_rwlock);
1295 * Change the name of the given rule "from_rname" to "to_rname"
1298 int rename_rule(const char *from_rname, const char *to_rname)
1300 struct rbce_rule *rule;
1301 int nlen, rc = -EINVAL;
1303 if (!to_rname || !*to_rname) {
1306 write_lock(&global_rwlock);
1308 rule = find_rule_name(from_rname);
1310 if ((nlen = strlen(to_rname)) > strlen(rule->obj.name)) {
1311 char *name = kmalloc(nlen + 1, GFP_ATOMIC);
1315 kfree(rule->obj.name);
1316 rule->obj.name = name;
1318 strcpy(rule->obj.name, to_rname);
1321 write_unlock(&global_rwlock);
1326 * Return TRUE if the given rule exists, FALSE otherwise
1329 int rule_exists(const char *rname)
1331 struct rbce_rule *rule;
1333 read_lock(&global_rwlock);
1334 rule = find_rule_name(rname);
1335 read_unlock(&global_rwlock);
1336 return rule != NULL;
1339 /*====================== Magic file handling =======================*/
1343 static struct rbce_private_data *create_private_data(struct rbce_private_data *,
1347 void reset_evaluation(struct rbce_private_data *pdata,int termflag)
1349 /* reset TAG ruleterm evaluation results to pick up
1350 * on next classification event
1352 if (use_persistent_state && gl_mask_vecs[termflag]) {
1353 bitvector_and_not( pdata->eval, pdata->eval,
1354 gl_mask_vecs[termflag] );
1355 bitvector_and_not( pdata->true, pdata->true,
1356 gl_mask_vecs[termflag] );
1360 int set_tasktag(int pid, char *tag)
1364 struct task_struct *tsk;
1365 struct rbce_private_data *pdata;
1371 len = strlen(tag) + 1;
1372 tp = kmalloc(len, GFP_ATOMIC);
1376 strncpy(tp,tag,len);
1378 read_lock(&tasklist_lock);
1379 if ((tsk = find_task_by_pid(pid)) == NULL) {
1384 if (unlikely(!RBCE_DATA(tsk))) {
1385 RBCE_DATAP(tsk) = create_private_data(NULL, 0);
1386 if (!RBCE_DATA(tsk)) {
1391 pdata = RBCE_DATA(tsk);
1392 if (pdata->app_tag) {
1393 kfree(pdata->app_tag);
1395 pdata->app_tag = tp;
1396 reset_evaluation(pdata,RBCE_TERMFLAG_TAG);
1399 read_unlock(&tasklist_lock);
1405 /*====================== Classification Functions =======================*/
1408 * Match the given full path name with the command expression.
1409 * This function treats the folowing 2 charaters as special if seen in
1410 * cmd_exp, all other chanracters are compared as is:
1411 * ? - compares to any one single character
1412 * * - compares to one or more single characters
1414 * If fullpath is 1, tsk_comm is compared in full. otherwise only the command
1415 * name (basename(tsk_comm)) is compared.
1417 static int match_cmd(const char *tsk_comm, const char *cmd_exp, int fullpath)
1419 const char *c, *t, *last_ast, *cmd = tsk_comm;
1422 // get the command name if we don't have to match the fullpath
1423 if (!fullpath && ((c = strrchr(tsk_comm, '/')) != NULL)) {
1427 /* now faithfully assume the entire pathname is in cmd */
1429 /* we now have to effectively implement a regular expression
1431 * '?' any single character
1432 * '*' one or more '?'
1438 if (t == NULL || c == NULL) {
1458 // eat up all '*' in c
1459 while (*(c + 1) == '*')
1463 //t++; // Add this for matching '*' with "one"
1465 while (*t && (*t != *(c + 1)) && *t != '/')
1467 if (*t == *(c + 1)) {
1495 /*FALLTHRU*/ default:
1496 if (*t == *c && next_c != *t) {
1500 /* reset to last asterix and
1501 continue from there */
1511 /* check for trailing "*" */
1515 return (!*c && !*t);
1518 static void reverse(char *str, int n)
1523 for (i = 0; i < j; i++, j--) {
1530 static int itoa(int n, char *str)
1535 str[i++] = n % 10 + '0';
1540 (void)reverse(str, sz);
1544 static int v4toa(__u32 y, char *a)
1549 for (i = 0; i < 4; i++) {
1550 size += itoa(y & 0xff, &a[size]);
1557 int match_ipv4(struct ckrm_net_struct *ns, char **string)
1559 char *ptr = *string;
1563 size = v4toa(ns->ns_daddrv4, a4);
1566 return !strncmp(a4, ptr, size);
1569 int match_port(struct ckrm_net_struct *ns, char *ptr)
1572 int size = itoa(ns->ns_dport, a);
1574 return !strncmp(a, ptr, size);
1577 static int __evaluate_rule(struct task_struct *tsk, struct ckrm_net_struct *ns,
1578 struct rbce_rule *rule, bitvector_t * vec_eval,
1579 bitvector_t * vec_true, char **filename);
1581 * evaluate the given task against the given rule with the vec_eval and
1582 * vec_true in context. Return 1 if the task satisfies the given rule, 0
1585 * If the bit corresponding to the rule is set in the vec_eval, then the
1586 * corresponding bit in vec_true is the result. If it is not set, evaluate
1587 * the rule and set the bits in both the vectors accordingly.
1589 * On return, filename will have the pointer to the pathname of the task's
1590 * executable, if the rule had any command related terms.
1592 * Caller must hold the global_rwlock atleast in read mode.
1595 evaluate_rule(struct task_struct *tsk, struct ckrm_net_struct *ns,
1596 struct rbce_rule *rule, bitvector_t * vec_eval,
1597 bitvector_t * vec_true, char **filename)
1599 int tidx = rule->index;
1601 if (!bitvector_test(tidx, vec_eval)) {
1603 (tsk, ns, rule, vec_eval, vec_true, filename)) {
1604 bitvector_set(tidx, vec_true);
1606 bitvector_set(tidx, vec_eval);
1608 return bitvector_test(tidx, vec_true);
1612 * evaluate the given task against every term in the given rule with
1613 * vec_eval and vec_true in context.
1615 * If the bit corresponding to a rule term is set in the vec_eval, then the
1616 * corresponding bit in vec_true is the result for taht particular. If it is
1617 * not set, evaluate the rule term and set the bits in both the vectors
1620 * This fucntions returns true only if all terms in the rule evaluate true.
1622 * On return, filename will have the pointer to the pathname of the task's
1623 * executable, if the rule had any command related terms.
1625 * Caller must hold the global_rwlock atleast in read mode.
1628 __evaluate_rule(struct task_struct *tsk, struct ckrm_net_struct *ns,
1629 struct rbce_rule *rule, bitvector_t * vec_eval,
1630 bitvector_t * vec_true, char **filename)
1635 for (i = rule->num_terms; --i >= 0;) {
1636 int rc = 1, tidx = rule->terms[i];
1638 if (!bitvector_test(tidx, vec_eval)) {
1639 struct rbce_rule_term *term = &gl_terms[tidx];
1643 case RBCE_RULE_CMD_PATH:
1646 if (!*filename) { /* get this once */
1649 GFP_ATOMIC)) == NULL)
1652 (tsk, *filename, NAME_MAX) < 0)) {
1657 rc = match_cmd(*filename, term->u.string,
1659 RBCE_RULE_CMD_PATH));
1661 rc = match_cmd(tsk->comm, term->u.string,
1663 RBCE_RULE_CMD_PATH));
1666 case RBCE_RULE_REAL_UID:
1667 if (term->operator == RBCE_LESS_THAN) {
1668 rc = (tsk->uid < term->u.id);
1669 } else if (term->operator == RBCE_GREATER_THAN){
1670 rc = (tsk->uid > term->u.id);
1671 } else if (term->operator == RBCE_NOT) {
1672 rc = (tsk->uid != term->u.id);
1674 rc = (tsk->uid == term->u.id);
1677 case RBCE_RULE_REAL_GID:
1678 if (term->operator == RBCE_LESS_THAN) {
1679 rc = (tsk->gid < term->u.id);
1680 } else if (term->operator == RBCE_GREATER_THAN){
1681 rc = (tsk->gid > term->u.id);
1682 } else if (term->operator == RBCE_NOT) {
1683 rc = (tsk->gid != term->u.id);
1685 rc = (tsk->gid == term->u.id);
1688 case RBCE_RULE_EFFECTIVE_UID:
1689 if (term->operator == RBCE_LESS_THAN) {
1690 rc = (tsk->euid < term->u.id);
1691 } else if (term->operator == RBCE_GREATER_THAN){
1692 rc = (tsk->euid > term->u.id);
1693 } else if (term->operator == RBCE_NOT) {
1694 rc = (tsk->euid != term->u.id);
1696 rc = (tsk->euid == term->u.id);
1699 case RBCE_RULE_EFFECTIVE_GID:
1700 if (term->operator == RBCE_LESS_THAN) {
1701 rc = (tsk->egid < term->u.id);
1702 } else if (term->operator == RBCE_GREATER_THAN){
1703 rc = (tsk->egid > term->u.id);
1704 } else if (term->operator == RBCE_NOT) {
1705 rc = (tsk->egid != term->u.id);
1707 rc = (tsk->egid == term->u.id);
1710 case RBCE_RULE_APP_TAG:
1711 rc = (RBCE_DATA(tsk)
1713 app_tag) ? !strcmp(RBCE_DATA(tsk)->
1715 term->u.string) : 0;
1717 case RBCE_RULE_DEP_RULE:
1718 rc = evaluate_rule(tsk, NULL, term->u.deprule,
1723 case RBCE_RULE_IPV4:
1724 // TBD: add NOT_EQUAL match. At present rbce
1725 // recognises EQUAL matches only.
1726 if (ns && term->operator == RBCE_EQUAL) {
1729 char *ptr = term->u.string;
1731 if (term->u.string[0] == '*')
1734 ma = match_ipv4(ns, &ptr);
1736 if (*ptr != '\\') { // error
1753 case RBCE_RULE_IPV6: // no support yet
1760 xid_t xid = vx_task_xid(tsk);
1762 if (term->operator == RBCE_LESS_THAN) {
1763 rc = (xid < term->u.id);
1764 } else if (term->operator == RBCE_GREATER_THAN) {
1765 rc = (xid > term->u.id);
1766 } else if (term->operator == RBCE_NOT) {
1767 rc = (xid != term->u.id);
1769 rc = (xid == term->u.id);
1776 printk(KERN_ERR "Error evaluate term op=%d\n",
1781 bitvector_clear(tidx, vec_true);
1783 bitvector_set(tidx, vec_true);
1785 bitvector_set(tidx, vec_eval);
1787 rc = bitvector_test(tidx, vec_true);
1796 //#define PDATA_DEBUG
1799 #define MAX_PDATA 10000
1800 void *pdata_arr[MAX_PDATA];
1801 int pdata_count, pdata_next;
1802 static spinlock_t pdata_lock = SPIN_LOCK_UNLOCKED;
1804 static inline int valid_pdata(struct rbce_private_data *pdata)
1811 spin_lock(&pdata_lock);
1812 for (i = 0; i < MAX_PDATA; i++) {
1813 if (pdata_arr[i] == pdata) {
1814 spin_unlock(&pdata_lock);
1818 spin_unlock(&pdata_lock);
1819 printk(KERN_WARNING "INVALID/CORRUPT PDATA %p\n", pdata);
1823 static inline void store_pdata(struct rbce_private_data *pdata)
1828 spin_lock(&pdata_lock);
1830 while (i < MAX_PDATA) {
1831 if (pdata_arr[pdata_next] == NULL) {
1832 printk(KERN_DEBUG "storing %p at %d, count %d\n", pdata,
1833 pdata_next, pdata_count);
1834 pdata_arr[pdata_next++] = pdata;
1835 if (pdata_next == MAX_PDATA) {
1844 spin_unlock(&pdata_lock);
1846 if (i == MAX_PDATA) {
1847 printk(KERN_DEBUG "PDATA BUFFER FULL pdata_count %d pdata %p\n",
1848 pdata_count, pdata);
1852 static inline void unstore_pdata(struct rbce_private_data *pdata)
1856 spin_lock(&pdata_lock);
1857 for (i = 0; i < MAX_PDATA; i++) {
1858 if (pdata_arr[i] == pdata) {
1859 printk(KERN_DEBUG "unstoring %p at %d, count %d\n", pdata,
1861 pdata_arr[i] = NULL;
1867 spin_unlock(&pdata_lock);
1868 if (i == MAX_PDATA) {
1869 printk(KERN_DEBUG "pdata %p not found in the stored array\n",
1876 #else // PDATA_DEBUG
1878 #define valid_pdata(pdata) (1)
1879 #define store_pdata(pdata)
1880 #define unstore_pdata(pdata)
1882 #endif // PDATA_DEBUG
1885 * Allocate and initialize a rbce_private_data data structure.
1887 * Caller must hold global_rwlock atleast in read mode.
1891 copy_ext_private_data(struct rbce_private_data *src,
1892 struct rbce_private_data *dst)
1895 dst->ext_data = src->ext_data;
1897 memset(&dst->ext_data, 0, sizeof(dst->ext_data));
1900 static struct rbce_private_data *create_private_data(struct rbce_private_data
1901 *src, int copy_sample)
1903 int vsize, psize, bsize;
1904 struct rbce_private_data *pdata;
1906 if (use_persistent_state) {
1907 vsize = gl_allocated;
1908 bsize = vsize / 8 + sizeof(bitvector_t);
1909 psize = sizeof(struct rbce_private_data) + 2 * bsize;
1911 psize = sizeof(struct rbce_private_data);
1914 pdata = kmalloc(psize, GFP_ATOMIC);
1915 if (pdata != NULL) {
1916 if (use_persistent_state) {
1917 pdata->bitmap_version = gl_bitmap_version;
1918 pdata->eval = (bitvector_t *) & pdata->data[0];
1919 pdata->true = (bitvector_t *) & pdata->data[bsize];
1920 if (src && (src->bitmap_version == gl_bitmap_version)) {
1921 memcpy(pdata->data, src->data, 2 * bsize);
1923 bitvector_init(pdata->eval, vsize);
1924 bitvector_init(pdata->true, vsize);
1927 copy_ext_private_data(src, pdata);
1928 //if (src) { // inherit evaluate and app_tag
1929 // pdata->evaluate = src->evaluate;
1930 // if(src->app_tag) {
1931 // int len = strlen(src->app_tag)+1;
1932 // printk(KERN_DEBUG "CREATE_PRIVATE: apptag %s len %d\n",
1933 // src->app_tag,len);
1934 // pdata->app_tag = kmalloc(len, GFP_ATOMIC);
1935 // if (pdata->app_tag) {
1936 // strcpy(pdata->app_tag, src->app_tag);
1940 pdata->evaluate = 1;
1941 pdata->rules_version = src ? src->rules_version : 0;
1942 pdata->app_tag = NULL;
1949 static inline void free_private_data(struct rbce_private_data *pdata)
1951 if (valid_pdata(pdata)) {
1952 unstore_pdata(pdata);
1957 static void free_all_private_data(void)
1959 struct task_struct *proc, *thread;
1961 read_lock(&tasklist_lock);
1962 do_each_thread(proc, thread) {
1963 struct rbce_private_data *pdata;
1965 pdata = RBCE_DATA(thread);
1966 RBCE_DATAP(thread) = NULL;
1967 free_private_data(pdata);
1968 } while_each_thread(proc, thread);
1969 read_unlock(&tasklist_lock);
1974 * reclassify function, which is called by all the callback functions.
1976 * Takes that task to be reclassified and ruleflags that indicates the
1977 * attributes that caused this reclassification request.
1979 * On success, returns the core class pointer to which the given task should
1982 static struct ckrm_core_class *rbce_classify(struct task_struct *tsk,
1983 struct ckrm_net_struct *ns,
1984 unsigned long termflag,
1988 struct rbce_rule *rule;
1989 bitvector_t *vec_true = NULL, *vec_eval = NULL;
1990 struct rbce_class *tgt = NULL;
1991 struct ckrm_core_class *cls = NULL;
1992 char *filename = NULL;
1994 if (!valid_pdata(RBCE_DATA(tsk))) {
1997 if (classtype >= CKRM_MAX_CLASSTYPES) {
1998 // can't handle more than CKRM_MAX_CLASSTYPES
2001 // fast path to avoid locking in case CE is not enabled or if no rules
2002 // are defined or if the tasks states that no evaluation is needed.
2003 if (!rbce_enabled || !gl_num_rules ||
2004 (RBCE_DATA(tsk) && !RBCE_DATA(tsk)->evaluate)) {
2007 // FIXME: optimize_policy should be called from here if
2008 // gl_action is non-zero. Also, it has to be called with the
2009 // global_rwlock held in write mode.
2011 read_lock(&global_rwlock);
2013 vec_eval = vec_true = NULL;
2014 if (use_persistent_state) {
2015 struct rbce_private_data *pdata = RBCE_DATA(tsk);
2019 && (gl_bitmap_version != pdata->bitmap_version))) {
2020 struct rbce_private_data *new_pdata =
2021 create_private_data(pdata, 1);
2025 new_pdata->rules_version =
2026 pdata->rules_version;
2027 new_pdata->evaluate = pdata->evaluate;
2028 new_pdata->app_tag = pdata->app_tag;
2029 free_private_data(pdata);
2031 pdata = RBCE_DATAP(tsk) = new_pdata;
2032 termflag = RBCE_TERMFLAG_ALL;
2033 // need to evaluate them all
2035 // we shouldn't free the pdata as it has more
2036 // details than the vectors. But, this
2037 // reclassification should go thru
2042 goto cls_determined;
2044 vec_eval = pdata->eval;
2045 vec_true = pdata->true;
2047 int bsize = gl_allocated;
2049 vec_eval = bitvector_alloc(bsize);
2050 vec_true = bitvector_alloc(bsize);
2052 if (vec_eval == NULL || vec_true == NULL) {
2053 goto cls_determined;
2055 termflag = RBCE_TERMFLAG_ALL;
2056 // need to evaluate all of them now
2060 * using bit ops invalidate all terms related to this termflag
2061 * context (only in per task vec)
2063 DPRINTK(DBG_CLASSIFY_DETAILS, "\nClassify: termflag=%lx\n", termflag);
2064 DPRINTK(DBG_CLASSIFY_DETAILS, " eval before: ");
2065 bitvector_print(DBG_CLASSIFY_DETAILS, vec_eval);
2066 DPRINTK(DBG_CLASSIFY_DETAILS, "\n true before: ");
2067 bitvector_print(DBG_CLASSIFY_DETAILS, vec_true);
2068 DPRINTK(DBG_CLASSIFY_DETAILS, "\n redo => ");
2070 if (termflag == RBCE_TERMFLAG_ALL) {
2071 DPRINTK(DBG_CLASSIFY_DETAILS, " redoall ");
2072 bitvector_zero(vec_eval);
2074 for (i = 0; i < NUM_TERM_MASK_VECTOR; i++) {
2075 if (test_bit(i, &termflag)) {
2076 bitvector_t *maskvec = gl_mask_vecs[i];
2078 DPRINTK(DBG_CLASSIFY_DETAILS, " mask(%d) ", i);
2079 bitvector_print(DBG_CLASSIFY_DETAILS, maskvec);
2080 bitvector_and_not(vec_eval, vec_eval, maskvec);
2084 bitvector_and(vec_true, vec_true, vec_eval);
2086 DPRINTK(DBG_CLASSIFY_DETAILS, "\n eval now: ");
2087 bitvector_print(DBG_CLASSIFY_DETAILS, vec_eval);
2088 DPRINTK(DBG_CLASSIFY_DETAILS, "\n");
2090 /* run through the rules in order and see what needs evaluation */
2091 list_for_each_entry(rule, &rules_list[classtype], obj.link) {
2092 if (rule->state == RBCE_RULE_ENABLED &&
2093 rule->target_class &&
2094 rule->target_class->classobj &&
2095 evaluate_rule(tsk, ns, rule, vec_eval, vec_true,
2097 tgt = rule->target_class;
2098 cls = rule->target_class->classobj;
2104 DPRINTK(DBG_CLASSIFY_RES,
2105 "==> |%s|; pid %d; euid %d; egid %d; ruid %d; rgid %d;"
2106 "tag |%s| ===> class |%s|\n",
2107 filename ? filename : tsk->comm,
2113 RBCE_DATA(tsk) ? RBCE_DATA(tsk)->app_tag : "",
2114 tgt ? tgt->obj.name : "");
2115 DPRINTK(DBG_CLASSIFY_DETAILS, " eval after: ");
2116 bitvector_print(DBG_CLASSIFY_DETAILS, vec_eval);
2117 DPRINTK(DBG_CLASSIFY_DETAILS, "\n true after: ");
2118 bitvector_print(DBG_CLASSIFY_DETAILS, vec_true);
2119 DPRINTK(DBG_CLASSIFY_DETAILS, "\n");
2121 if (!use_persistent_state) {
2123 bitvector_free(vec_eval);
2126 bitvector_free(vec_true);
2129 ckrm_core_grab(cls);
2130 read_unlock(&global_rwlock);
2134 if (RBCE_DATA(tsk)) {
2135 RBCE_DATA(tsk)->rules_version = gl_rules_version;
2140 /*****************************************************************************
2142 * Module specific utilization of core RBCE functionality
2144 * Includes support for the various classtypes
2145 * New classtypes will require extensions here
2147 *****************************************************************************/
2149 /* helper functions that are required in the extended version */
2151 static inline void rbce_tc_manual(struct task_struct *tsk)
2153 read_lock(&global_rwlock);
2155 if (!RBCE_DATA(tsk)) {
2157 (void *)create_private_data(RBCE_DATA(tsk->parent), 0);
2159 if (RBCE_DATA(tsk)) {
2160 RBCE_DATA(tsk)->evaluate = 0;
2162 read_unlock(&global_rwlock);
2166 /*****************************************************************************
2167 * load any extensions
2168 *****************************************************************************/
2170 #ifdef RBCE_EXTENSION
2171 #include "rbcemod_ext.c"
2174 /*****************************************************************************
2175 * VARIOUS CLASSTYPES
2176 *****************************************************************************/
2178 // to enable type coercion of the function pointers
2180 /*============================================================================
2181 * TASKCLASS CLASSTYPE
2182 *============================================================================*/
2184 int tc_classtype = -1;
2187 * fork callback to be registered with core module.
2189 inline static void *rbce_tc_forkcb(struct task_struct *tsk)
2191 int rule_version_changed = 1;
2192 struct ckrm_core_class *cls;
2193 read_lock(&global_rwlock);
2196 (void *)create_private_data(RBCE_DATA(tsk->parent), 0);
2197 read_unlock(&global_rwlock);
2199 if (RBCE_DATA(tsk->parent)) {
2200 rule_version_changed =
2201 (RBCE_DATA(tsk->parent)->rules_version != gl_rules_version);
2203 cls = rule_version_changed ?
2204 rbce_classify(tsk, NULL, RBCE_TERMFLAG_ALL, tc_classtype) : NULL;
2206 // note the fork notification to any user client will be sent through
2207 // the guaranteed fork-reclassification
2212 * exit callback to be registered with core module.
2214 static void rbce_tc_exitcb(struct task_struct *tsk)
2216 struct rbce_private_data *pdata;
2218 send_exit_notification(tsk);
2220 pdata = RBCE_DATA(tsk);
2221 RBCE_DATAP(tsk) = NULL;
2223 if (pdata->app_tag) {
2224 kfree(pdata->app_tag);
2226 free_private_data(pdata);
2231 #define AENT(x) [ CKRM_EVENT_##x] = #x
2232 static const char *event_names[CKRM_NUM_EVENTS] = {
2250 void *rbce_tc_classify(enum ckrm_event event, ...)
2254 struct task_struct *tsk;
2255 struct rbce_private_data *pdata;
2257 va_start(args, event);
2258 tsk = va_arg(args, struct task_struct *);
2261 /* we only have to deal with events between
2262 * [ CKRM_LATCHABLE_EVENTS .. CKRM_NONLATCHABLE_EVENTS )
2265 // printk(KERN_DEBUG "tc_classify %p:%d:%s '%s'\n",tsk,tsk->pid,
2266 // tsk->comm,event_names[event]);
2270 case CKRM_EVENT_FORK:
2271 cls = rbce_tc_forkcb(tsk);
2274 case CKRM_EVENT_EXIT:
2275 rbce_tc_exitcb(tsk);
2278 case CKRM_EVENT_EXEC:
2279 cls = rbce_classify(tsk, NULL, RBCE_TERMFLAG_CMD |
2280 RBCE_TERMFLAG_UID | RBCE_TERMFLAG_GID,
2284 case CKRM_EVENT_UID:
2285 cls = rbce_classify(tsk, NULL, RBCE_TERMFLAG_UID, tc_classtype);
2288 case CKRM_EVENT_GID:
2289 cls = rbce_classify(tsk, NULL, RBCE_TERMFLAG_GID, tc_classtype);
2292 case CKRM_EVENT_XID:
2293 cls = rbce_classify(tsk, NULL, RBCE_TERMFLAG_XID, tc_classtype);
2296 case CKRM_EVENT_LOGIN:
2297 case CKRM_EVENT_USERADD:
2298 case CKRM_EVENT_USERDEL:
2299 case CKRM_EVENT_LISTEN_START:
2300 case CKRM_EVENT_LISTEN_STOP:
2301 case CKRM_EVENT_APPTAG:
2302 /* no interest in this events .. */
2309 case CKRM_EVENT_RECLASSIFY:
2310 if ((pdata = (RBCE_DATA(tsk)))) {
2311 pdata->evaluate = 1;
2313 cls = rbce_classify(tsk, NULL, RBCE_TERMFLAG_ALL, tc_classtype);
2317 // printk(KERN_DEBUG "tc_classify %p:%d:%s '%s' ==> %p\n",tsk,tsk->pid,
2318 // tsk->comm,event_names[event],cls);
2323 #ifndef RBCE_EXTENSION
2324 static void rbce_tc_notify(int event, void *core, struct task_struct *tsk)
2326 printk(KERN_DEBUG "tc_manual %p:%d:%s '%s'\n", tsk, tsk->pid, tsk->comm,
2327 event_names[event]);
2328 if (event != CKRM_EVENT_MANUAL)
2330 rbce_tc_manual(tsk);
2334 static struct ckrm_eng_callback rbce_taskclass_ecbs = {
2335 .c_interest = (unsigned long)(-1), // set whole bitmap
2336 .classify = (ce_classify_fct_t) rbce_tc_classify,
2337 .class_delete = rbce_class_deletecb,
2338 #ifndef RBCE_EXTENSION
2339 .n_interest = (1 << CKRM_EVENT_MANUAL),
2340 .notify = (ce_notify_fct_t) rbce_tc_notify,
2341 .always_callback = 0,
2343 .n_interest = (unsigned long)(-1), // set whole bitmap
2344 .notify = (ce_notify_fct_t) rbce_tc_ext_notify,
2345 .class_add = rbce_class_addcb,
2346 .always_callback = 1,
2350 /*============================================================================
2352 *============================================================================*/
2354 int sc_classtype = -1;
2356 void *rbce_sc_classify(enum ckrm_event event, ...)
2358 // no special consideratation
2361 struct task_struct *tsk;
2362 struct ckrm_net_struct *ns;
2364 va_start(args, event);
2365 ns = va_arg(args, struct ckrm_net_struct *);
2366 tsk = va_arg(args, struct task_struct *);
2369 result = rbce_classify(tsk, ns, RBCE_TERMFLAG_ALL, sc_classtype);
2371 DPRINTK(DBG_CLASSIFY_RES,
2372 "==> %d.%d.%d.%d\\%d , %p:%d:%s '%s' => %p\n",
2373 NIPQUAD(ns->ns_daddrv4), ns->ns_dport,
2374 tsk, tsk ? tsk->pid : 0, tsk ? tsk->comm : "-",
2375 event_names[event], result);
2379 static struct ckrm_eng_callback rbce_acceptQclass_ecbs = {
2380 .c_interest = (unsigned long)(-1),
2381 .always_callback = 0, // enable during debugging only
2382 .classify = (ce_classify_fct_t) & rbce_sc_classify,
2383 .class_delete = rbce_class_deletecb,
2386 /*============================================================================
2387 * Module Initialization ...
2388 *============================================================================*/
2390 #define TASKCLASS_NAME "taskclass"
2391 #define SOCKCLASS_NAME "socket_class"
2393 struct ce_regtable_struct {
2395 struct ckrm_eng_callback *cbs;
2399 struct ce_regtable_struct ce_regtable[] = {
2400 {TASKCLASS_NAME, &rbce_taskclass_ecbs, &tc_classtype},
2401 {SOCKCLASS_NAME, &rbce_acceptQclass_ecbs, &sc_classtype},
2405 static void unregister_classtype_engines(void)
2408 struct ce_regtable_struct *ceptr = ce_regtable;
2410 while (ceptr->name) {
2411 if (*ceptr->clsvar >= 0) {
2412 printk(KERN_DEBUG "ce unregister with <%s>\n",ceptr->name);
2413 while ((rc = ckrm_unregister_engine(ceptr->name)) == -EAGAIN)
2415 printk(KERN_DEBUG "ce unregister with <%s> rc=%d\n",ceptr->name,rc);
2416 *ceptr->clsvar = -1;
2422 static int register_classtype_engines(void)
2425 struct ce_regtable_struct *ceptr = ce_regtable;
2427 while (ceptr->name) {
2428 rc = ckrm_register_engine(ceptr->name, ceptr->cbs);
2429 printk(KERN_DEBUG "ce register with <%s> typeId=%d\n",ceptr->name,rc);
2430 if ((rc < 0) && (rc != -ENOENT)) {
2431 unregister_classtype_engines();
2435 *ceptr->clsvar = rc;
2441 // =========== /proc/sysctl/debug/rbce debug stuff =============
2444 static struct ctl_table_header *rbce_sysctl_table_header;
2446 #define CTL_RBCE_DEBUG (201) // picked some number.. dont know algo to pick
2447 static struct ctl_table rbce_entry_table[] = {
2449 .ctl_name = CTL_RBCE_DEBUG,
2452 .maxlen = sizeof(int),
2454 .proc_handler = &proc_dointvec,
2459 static struct ctl_table rbce_root_table[] = {
2461 .ctl_name = CTL_DEBUG,
2462 .procname = "debug",
2466 .child = rbce_entry_table},
2470 static inline void start_debug(void)
2472 rbce_sysctl_table_header = register_sysctl_table(rbce_root_table, 1);
2474 static inline void stop_debug(void)
2476 if (rbce_sysctl_table_header)
2477 unregister_sysctl_table(rbce_sysctl_table_header);
2482 static inline void start_debug(void)
2485 static inline void stop_debug(void)
2491 extern int rbce_mkdir(struct inode *, struct dentry *, int);
2492 extern int rbce_rmdir(struct inode *, struct dentry *);
2493 extern int rbce_create_magic(void);
2494 extern int rbce_clear_magic(void);
2496 rbce_eng_callback_t rcfs_ecbs = {
2503 /* ======================= Module definition Functions ====================== */
2509 printk(KERN_DEBUG "<1>\nInstalling \'%s\' module\n", modname);
2511 for (i = 0; i < CKRM_MAX_CLASSTYPES; i++) {
2512 INIT_LIST_HEAD(&rules_list[i]);
2515 rc = init_rbce_ext_pre();
2520 rc = register_classtype_engines();
2523 goto out_unreg_ckrm; // need to remove anyone opened
2525 /* register any other class type engine here */
2527 rc = rcfs_register_engine(&rcfs_ecbs);
2530 goto out_unreg_ckrm;
2533 rc = rbce_create_magic();
2536 goto out_unreg_rcfs;
2541 rc = init_rbce_ext_post();
2546 return 0; // SUCCESS
2552 rcfs_unregister_engine(&rcfs_ecbs);
2554 unregister_classtype_engines();
2558 printk(KERN_DEBUG "<1>%s: error installing rc=%d line=%d\n", __FUNCTION__, rc,
2563 void exit_rbce(void)
2567 printk(KERN_DEBUG "<1>Removing \'%s\' module\n", modname);
2572 // Print warnings if lists are not empty, which is a bug
2573 if (!list_empty(&class_list)) {
2574 printk(KERN_DEBUG "exit_rbce: Class list is not empty\n");
2577 for (i = 0; i < CKRM_MAX_CLASSTYPES; i++) {
2578 if (!list_empty(&rules_list[i])) {
2579 printk(KERN_DEBUG "exit_rbce: Rules list for classtype %d"
2580 " is not empty\n", i);
2587 rcfs_unregister_engine(&rcfs_ecbs);
2588 unregister_classtype_engines();
2589 free_all_private_data();
2592 EXPORT_SYMBOL(get_rule);
2593 EXPORT_SYMBOL(rule_exists);
2594 EXPORT_SYMBOL(change_rule);
2595 EXPORT_SYMBOL(delete_rule);
2596 EXPORT_SYMBOL(rename_rule);
2597 EXPORT_SYMBOL(set_tasktag);
2599 module_init(init_rbce);
2600 module_exit(exit_rbce);