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>
59 #warning MEF I cannot believe that vserver changes force the following include statement: FIX THIS!
60 #include <linux/vs_cvirt.h>
65 MODULE_DESCRIPTION(RBCE_MOD_DESCR);
66 MODULE_AUTHOR("Hubertus Franke, Chandra Seetharaman (IBM)");
67 MODULE_LICENSE("GPL");
69 static char modname[] = RBCE_MOD_NAME;
71 /* ==================== typedef, global variables etc., ==================== */
72 struct named_obj_hdr {
73 struct list_head link;
78 #define GET_REF(x) ((x)->obj.referenced)
79 #define INC_REF(x) (GET_REF(x)++)
80 #define DEC_REF(x) (--GET_REF(x))
82 struct named_obj_hdr obj;
88 RBCE_RULE_CMD_PATH = 1, // full qualified path
89 RBCE_RULE_CMD, // basename of the command
90 RBCE_RULE_ARGS, // arguments of the command
91 RBCE_RULE_REAL_UID, // task's real uid
92 RBCE_RULE_REAL_GID, // task's real gid
93 RBCE_RULE_EFFECTIVE_UID, // task's effective uid
94 RBCE_RULE_EFFECTIVE_GID, // task's effective gid
95 RBCE_RULE_APP_TAG, // task's application tag
96 RBCE_RULE_IPV4, // IP address of listen(), ipv4 format
97 RBCE_RULE_IPV6, // IP address of listen(), ipv6 format
98 RBCE_RULE_XID, // VSERVER
99 RBCE_RULE_DEP_RULE, // dependent rule; must be the first term
100 RBCE_RULE_INVALID, // invalid, for filler
101 RBCE_RULE_INVALID2, // invalid, for filler
111 struct rbce_rule_term {
113 rbce_operator_t operator;
115 char *string; // path, cmd, arg, tag, ipv4 and ipv6
116 long id; // uid, gid, euid, egid
117 struct rbce_rule *deprule;
122 struct named_obj_hdr obj;
123 struct rbce_class *target_class;
126 int *terms; // vector of indices into the global term vector
127 int index; // index of this rule into the global term vector
128 int termflag; // which term ids would require a recalculation
129 int do_opt; // do we have to consider this rule during optimize
130 char *strtab; // string table to store the strings of all terms
131 int order; // order of execution of this rule
132 int state; // RBCE_RULE_ENABLED/RBCE_RULE_DISABLED
136 #define RBCE_RULE_DISABLED 0
137 #define RBCE_RULE_ENABLED 1
140 // Data structures and macros used for optimization
141 #define RBCE_TERM_CMD (0)
142 #define RBCE_TERM_UID (1)
143 #define RBCE_TERM_GID (2)
144 #define RBCE_TERM_TAG (3)
145 #define RBCE_TERM_IPV4 (4)
146 #define RBCE_TERM_IPV6 (5)
147 #define RBCE_TERM_XID (6)
149 #define NUM_TERM_MASK_VECTOR (7) // must be one more the last RBCE_TERM_...
151 // Rule flags. 1 bit for each type of rule term
152 #define RBCE_TERMFLAG_CMD (1 << RBCE_TERM_CMD)
153 #define RBCE_TERMFLAG_UID (1 << RBCE_TERM_UID)
154 #define RBCE_TERMFLAG_GID (1 << RBCE_TERM_GID)
155 #define RBCE_TERMFLAG_TAG (1 << RBCE_TERM_TAG)
156 #define RBCE_TERMFLAG_IPV4 (1 << RBCE_TERM_IPV4)
157 #define RBCE_TERMFLAG_IPV6 (1 << RBCE_TERM_IPV6)
158 #define RBCE_TERMFLAG_XID (1 << RBCE_TERM_XID)
159 #define RBCE_TERMFLAG_ALL (RBCE_TERMFLAG_CMD | RBCE_TERMFLAG_UID | \
160 RBCE_TERMFLAG_GID | RBCE_TERMFLAG_TAG | RBCE_TERMFLAG_XID | \
161 RBCE_TERMFLAG_IPV4 | RBCE_TERMFLAG_IPV6)
163 int termop_2_vecidx[RBCE_RULE_INVALID] = {
164 [RBCE_RULE_CMD_PATH] = RBCE_TERM_CMD,
165 [RBCE_RULE_CMD] = RBCE_TERM_CMD,
166 [RBCE_RULE_ARGS] = RBCE_TERM_CMD,
167 [RBCE_RULE_REAL_UID] = RBCE_TERM_UID,
168 [RBCE_RULE_REAL_GID] = RBCE_TERM_GID,
169 [RBCE_RULE_EFFECTIVE_UID] = RBCE_TERM_UID,
170 [RBCE_RULE_EFFECTIVE_GID] = RBCE_TERM_GID,
171 [RBCE_RULE_XID] = RBCE_TERM_XID,
172 [RBCE_RULE_APP_TAG] = RBCE_TERM_TAG,
173 [RBCE_RULE_IPV4] = RBCE_TERM_IPV4,
174 [RBCE_RULE_IPV6] = RBCE_TERM_IPV6,
175 [RBCE_RULE_DEP_RULE] = -1
178 #define TERMOP_2_TERMFLAG(x) (1 << termop_2_vecidx[x])
179 #define TERM_2_TERMFLAG(x) (1 << x)
181 #define POLICY_INC_NUMTERMS (BITS_PER_LONG) // No. of terms added at a time
182 #define POLICY_ACTION_NEW_VERSION 0x01 // Force reallocation
183 #define POLICY_ACTION_REDO_ALL 0x02 // Recompute all rule flags
184 #define POLICY_ACTION_PACK_TERMS 0x04 // Time to pack the terms
186 const int use_persistent_state = 1;
188 struct ckrm_eng_callback ckrm_ecbs;
192 static int gl_bitmap_version, gl_action, gl_num_terms;
193 static int gl_allocated, gl_released;
194 struct rbce_rule_term *gl_terms;
195 bitvector_t *gl_mask_vecs[NUM_TERM_MASK_VECTOR];
198 static void optimize_policy(void);
200 #ifndef CKRM_MAX_CLASSTYPES
201 #define CKRM_MAX_CLASSTYPES 32
204 struct list_head rules_list[CKRM_MAX_CLASSTYPES];
205 LIST_HEAD(class_list); // List of classes used
207 static int gl_num_rules;
208 static int gl_rules_version;
209 int rbce_enabled = 1;
210 static rwlock_t global_rwlock = RW_LOCK_UNLOCKED;
212 * One lock to protect them all !!!
213 * Additions, deletions to rules must
214 * happen with this lock being held in write mode.
215 * Access(read/write) to any of the data structures must happen
216 * with this lock held in read mode.
217 * Since, rule related changes do not happen very often it is ok to
218 * have single rwlock.
222 * data structure rbce_private_data holds the bit vector 'eval' which
223 * specifies if rules and terms of rules are evaluated against the task
224 * and if they were evaluated, bit vector 'true' holds the result of that
227 * This data structure is maintained in a task, and the bitvectors are
228 * updated only when needed.
230 * Each rule and each term of a rule has a corresponding bit in the vector.
233 struct rbce_private_data {
234 struct rbce_ext_private_data ext_data;
235 int evaluate; // whether to evaluate rules or not ?
236 int rules_version; // whether to evaluate rules or not ?
238 unsigned long bitmap_version;
241 char data[0]; // eval points to this variable size data array
244 #define RBCE_DATA(tsk) ((struct rbce_private_data*)((tsk)->ce_data))
245 #define RBCE_DATAP(tsk) ((tsk)->ce_data)
247 /* ======================= DEBUG Functions ========================= */
251 int rbcedebug = 0x00;
253 #define DBG_CLASSIFY_RES ( 0x01 )
254 #define DBG_CLASSIFY_DETAILS ( 0x02 )
255 #define DBG_OPTIMIZATION ( 0x04 )
256 #define DBG_SHOW_RESCTL ( 0x08 )
257 #define DBG_CLASS ( 0x10 )
258 #define DBG_RULE ( 0x20 )
259 #define DBG_POLICY ( 0x40 )
261 #define DPRINTK(x, y...) if (rbcedebug & (x)) printk(KERN_DEBUG y)
262 // debugging selectively enabled through /proc/sys/debug/rbce
264 static void print_context_vectors(void)
268 if ((rbcedebug & DBG_OPTIMIZATION) == 0) {
271 for (i = 0; i < NUM_TERM_MASK_VECTOR; i++) {
272 printk(KERN_DEBUG "%d: ", i);
273 bitvector_print(DBG_OPTIMIZATION, gl_mask_vecs[i]);
274 printk(KERN_DEBUG "\n");
279 #define DPRINTK(x, y...)
280 #define print_context_vectors(x)
283 /* ====================== VSERVER support ========================== */
284 #define CONFIG_VSERVER
285 #ifdef CONFIG_VSERVER
286 #include <linux/vs_base.h>
288 typedef unsigned int xid_t;
289 #define vx_task_xid(t) (0)
292 /* ======================= Helper Functions ========================= */
296 static struct ckrm_core_class *rbce_classify(struct task_struct *,
297 struct ckrm_net_struct *,
298 unsigned long, int classtype);
300 static inline struct rbce_rule *find_rule_name(const char *name)
302 struct named_obj_hdr *pos;
305 for (i = 0; i < CKRM_MAX_CLASSTYPES; i++) {
306 list_for_each_entry(pos, &rules_list[i], link) {
307 if (!strcmp(pos->name, name)) {
308 return ((struct rbce_rule *)pos);
315 static inline struct rbce_class *find_class_name(const char *name)
317 struct named_obj_hdr *pos;
319 list_for_each_entry(pos, &class_list, link) {
320 if (!strcmp(pos->name, name))
321 return (struct rbce_class *)pos;
327 * Insert the given rule at the specified order
328 * order = -1 ==> insert at the tail.
330 * Caller must hold global_rwlock in write mode.
332 static int insert_rule(struct rbce_rule *rule, int order)
334 #define ORDER_COUNTER_INCR 10
335 static int order_counter;
337 struct list_head *head = &rules_list[rule->classtype];
338 struct list_head *insert = head;
339 struct rbce_rule *tmp;
341 if (gl_num_rules == 0) {
347 rule->order = order_counter;
348 // FIXME: order_counter overflow/wraparound!!
349 order_counter += ORDER_COUNTER_INCR;
352 old_counter = order_counter;
353 if (order_counter < order) {
354 order_counter = order;
357 order_counter += ORDER_COUNTER_INCR;
358 list_for_each_entry(tmp, head, obj.link) {
359 if (rule->order == tmp->order) {
360 order_counter = old_counter;
363 if (rule->order < tmp->order) {
364 insert = &tmp->obj.link;
369 list_add_tail(&rule->obj.link, insert);
370 // protect the module from removed when any rule is
372 try_module_get(THIS_MODULE);
379 * Remove the rule and reinsert at the specified order.
381 * Caller must hold global_rwlock in write mode.
383 static int reinsert_rule(struct rbce_rule *rule, int order)
385 list_del(&rule->obj.link);
388 module_put(THIS_MODULE);
389 return insert_rule(rule, order);
393 * Get a refernece to the class, create one if it doesn't exist
395 * Caller need to hold global_rwlock in write mode.
399 static struct rbce_class *create_rbce_class(const char *classname,
400 int classtype, void *classobj)
402 struct rbce_class *cls;
404 if (classtype >= CKRM_MAX_CLASSTYPES) {
406 "ckrm_classobj returned %d as classtype which cannot "
407 " be handled by RBCE\n", classtype);
411 cls = kmalloc(sizeof(struct rbce_class), GFP_ATOMIC);
415 cls->obj.name = kmalloc(strlen(classname) + 1, GFP_ATOMIC);
418 cls->classobj = classobj;
419 strcpy(cls->obj.name, classname);
420 list_add_tail(&cls->obj.link, &class_list);
421 cls->classtype = classtype;
429 static struct rbce_class *get_class(char *classname, int *classtype)
431 struct rbce_class *cls;
437 cls = find_class_name(classname);
441 *classtype = cls->classtype;
446 classobj = ckrm_classobj(classname, classtype);
451 return create_rbce_class(classname, *classtype, classobj);
455 * Drop a refernece to the class, create one if it doesn't exist
457 * Caller need to hold global_rwlock in write mode.
459 static void put_class(struct rbce_class *cls)
462 if (DEC_REF(cls) <= 0) {
463 list_del(&cls->obj.link);
464 kfree(cls->obj.name);
472 * Callback from core when a class is added
475 #ifdef RBCE_EXTENSION
476 static void rbce_class_addcb(const char *classname, void *clsobj, int classtype)
478 struct rbce_class *cls;
480 write_lock(&global_rwlock);
481 cls = find_class_name((char *)classname);
483 cls->classobj = clsobj;
485 cls = create_rbce_class(classname, classtype, clsobj);
488 notify_class_action(cls, 1);
489 write_unlock(&global_rwlock);
495 * Callback from core when a class is deleted.
498 rbce_class_deletecb(const char *classname, void *classobj, int classtype)
500 static struct rbce_class *cls;
501 struct named_obj_hdr *pos;
502 struct rbce_rule *rule;
504 write_lock(&global_rwlock);
505 cls = find_class_name(classname);
507 if (cls->classobj != classobj) {
508 printk(KERN_ERR "rbce: class %s changed identity\n",
511 notify_class_action(cls, 0);
512 cls->classobj = NULL;
513 list_for_each_entry(pos, &rules_list[classtype], link) {
514 rule = (struct rbce_rule *)pos;
515 if (rule->target_class) {
517 (rule->target_class->obj.name, classname)) {
519 rule->target_class = NULL;
520 rule->classtype = -1;
524 if ((cls = find_class_name(classname)) != NULL) {
526 "rbce ERROR: class %s exists in rbce after "
527 "removal in core\n", classname);
530 write_unlock(&global_rwlock);
535 * Allocate an index in the global term vector
536 * On success, returns the index. On failure returns -errno.
537 * Caller must hold the global_rwlock in write mode as global data is
540 static int alloc_term_index(void)
542 int size = gl_allocated;
544 if (gl_num_terms >= size) {
546 struct rbce_rule_term *oldv, *newv;
547 int newsize = size + POLICY_INC_NUMTERMS;
551 kmalloc(newsize * sizeof(struct rbce_rule_term),
556 memcpy(newv, oldv, size * sizeof(struct rbce_rule_term));
557 for (i = size; i < newsize; i++) {
561 gl_allocated = newsize;
564 gl_action |= POLICY_ACTION_NEW_VERSION;
565 DPRINTK(DBG_OPTIMIZATION,
566 "alloc_term_index: Expanding size from %d to %d\n",
569 return gl_num_terms++;
573 * Release an index in the global term vector
575 * Caller must hold the global_rwlock in write mode as the global data
578 static void release_term_index(int idx)
580 if ((idx < 0) || (idx > gl_num_terms))
583 gl_terms[idx].op = -1;
585 if ((gl_released > POLICY_INC_NUMTERMS) &&
587 (gl_num_terms - gl_released + POLICY_INC_NUMTERMS))) {
588 gl_action |= POLICY_ACTION_PACK_TERMS;
594 * Release the indices, string memory, and terms associated with the given
597 * Caller should be holding global_rwlock
599 static void __release_rule(struct rbce_rule *rule)
601 int i, *terms = rule->terms;
603 // remove memory and references from other rules
604 for (i = rule->num_terms; --i >= 0;) {
605 struct rbce_rule_term *term = &gl_terms[terms[i]];
607 if (term->op == RBCE_RULE_DEP_RULE) {
608 DEC_REF(term->u.deprule);
610 release_term_index(terms[i]);
625 * delete the given rule and all memory associated with it.
627 * Caller is responsible for protecting the global data
629 static inline int __delete_rule(struct rbce_rule *rule)
631 // make sure we are not referenced by other rules
635 __release_rule(rule);
636 put_class(rule->target_class);
637 release_term_index(rule->index);
638 list_del(&rule->obj.link);
641 module_put(THIS_MODULE);
642 kfree(rule->obj.name);
648 * Optimize the rule evaluation logic
650 * Caller must hold global_rwlock in write mode.
652 static void optimize_policy(void)
655 struct rbce_rule *rule;
656 struct rbce_rule_term *terms;
659 bitvector_t **mask_vecs;
664 * Due to dynamic rule addition/deletion of rules the term
665 * vector can get sparse. As a result the bitvectors grow as we don't
666 * reuse returned indices. If it becomes sparse enough we pack them
670 pack_terms = (gl_action & POLICY_ACTION_PACK_TERMS);
671 DPRINTK(DBG_OPTIMIZATION,
672 "----- Optimize Policy ----- act=%x pt=%d (a=%d n=%d r=%d)\n",
673 gl_action, pack_terms, gl_allocated, gl_num_terms, gl_released);
676 int nsz = ALIGN((gl_num_terms - gl_released),
677 POLICY_INC_NUMTERMS);
679 struct rbce_rule_term *newterms;
683 kmalloc(nsz * sizeof(struct rbce_rule_term), GFP_ATOMIC);
685 for (ii = 0; ii < CKRM_MAX_CLASSTYPES; ii++) {
686 // FIXME: check only for task class types
687 list_for_each_entry_reverse(rule,
690 rule->index = newidx++;
691 for (i = rule->num_terms; --i >= 0;) {
692 int idx = rule->terms[i];
693 newterms[newidx] = terms[idx];
694 rule->terms[i] = newidx++;
701 gl_num_terms = newidx;
704 gl_action &= ~POLICY_ACTION_PACK_TERMS;
705 gl_action |= POLICY_ACTION_NEW_VERSION;
709 num_terms = gl_num_terms;
710 bsize = gl_allocated / 8 + sizeof(bitvector_t);
711 mask_vecs = gl_mask_vecs;
714 if (gl_action & POLICY_ACTION_NEW_VERSION) {
715 /* allocate new mask vectors */
716 char *temp = kmalloc(NUM_TERM_MASK_VECTOR * bsize, GFP_ATOMIC);
718 DPRINTK(DBG_OPTIMIZATION,
719 "------ allocmasks act=%x ------- ver=%d\n", gl_action,
724 if (mask_vecs[0]) {// index 0 has the alloc returned address
727 for (i = 0; i < NUM_TERM_MASK_VECTOR; i++) {
728 mask_vecs[i] = (bitvector_t *) (temp + i * bsize);
729 bitvector_init(mask_vecs[i], gl_allocated);
731 gl_action &= ~POLICY_ACTION_NEW_VERSION;
732 gl_action |= POLICY_ACTION_REDO_ALL;
736 /* We do two things here at once
737 * 1) recompute the rulemask for each required rule
738 * we guarantee proper dependency order during creation time and
739 * by reversely running through this list.
740 * 2) recompute the mask for each term and rule, if required
743 redoall = gl_action & POLICY_ACTION_REDO_ALL;
744 gl_action &= ~POLICY_ACTION_REDO_ALL;
746 DPRINTK(DBG_OPTIMIZATION, "------- run act=%x -------- redoall=%d\n",
748 for (ii = 0; ii < CKRM_MAX_CLASSTYPES; ii++) {
749 // FIXME: check only for task class types
750 list_for_each_entry_reverse(rule, &rules_list[ii], obj.link) {
751 unsigned long termflag;
753 if (!redoall && !rule->do_opt)
756 for (i = rule->num_terms; --i >= 0;) {
757 int j, idx = rule->terms[i];
758 struct rbce_rule_term *term = &terms[idx];
759 int vecidx = termop_2_vecidx[term->op];
762 termflag |= term->u.deprule->termflag;
763 /* mark this term belonging to all
764 contexts of deprule */
765 for (j = 0; j < NUM_TERM_MASK_VECTOR;
767 if (term->u.deprule->termflag
775 termflag |= TERM_2_TERMFLAG(vecidx);
776 /* mark this term belonging to
777 a particular context */
778 bitvector_set(idx, mask_vecs[vecidx]);
781 for (i = 0; i < NUM_TERM_MASK_VECTOR; i++) {
782 if (termflag & (1 << i)) {
783 bitvector_set(rule->index,
787 rule->termflag = termflag;
789 DPRINTK(DBG_OPTIMIZATION, "r-%s: %x %d\n",
790 rule->obj.name, rule->termflag, rule->index);
793 print_context_vectors();
797 /* ======================= Rule related Functions ========================= */
800 * Caller need to hold global_rwlock in write mode.
803 fill_rule(struct rbce_rule *newrule, struct rbce_rule_term *terms, int nterms)
805 char *class, *strtab;
806 int i, j, order, state, real_nterms, index;
807 int strtablen, rc = 0, counter;
808 struct rbce_rule_term *term = NULL;
809 struct rbce_class *targetcls = NULL;
810 struct rbce_rule *deprule;
815 // Digest filled terms.
817 strtab = class = NULL;
822 for (i = 0; i < nterms; i++) {
823 if (terms[i].op != RBCE_RULE_INVALID) {
826 switch (terms[i].op) {
827 case RBCE_RULE_DEP_RULE:
828 // check if the depend rule is valid
830 deprule = find_rule_name(terms[i].u.string);
831 if (!deprule || deprule == newrule) {
835 // make sure _a_ depend rule
836 // appears in only one term.
837 for (j = 0; j < i; j++) {
840 && terms[j].u.deprule ==
846 terms[i].u.deprule = deprule;
849 // +depend is acceptable and -depend is not
850 if (terms[i].operator != TOKEN_OP_DEP_DEL) {
851 terms[i].operator = RBCE_EQUAL;
858 case RBCE_RULE_CMD_PATH:
861 case RBCE_RULE_APP_TAG:
864 // sum up the string length
865 strtablen += strlen(terms[i].u.string) + 1;
872 switch (terms[i].operator) {
874 order = terms[i].u.id;
881 state = terms[i].u.id != 0;
884 class = terms[i].u.string;
892 // Check if class was specified
895 if ((targetcls = get_class(class, &classtype)) == NULL) {
899 put_class(newrule->target_class);
901 newrule->target_class = targetcls;
902 newrule->classtype = classtype;
904 if (!newrule->target_class) {
910 newrule->state = state;
913 newrule->order = order;
915 newrule->terms = kmalloc(real_nterms * sizeof(int), GFP_ATOMIC);
916 if (!newrule->terms) {
920 newrule->num_terms = real_nterms;
921 if (strtablen && ((strtab = kmalloc(strtablen, GFP_ATOMIC)) == NULL)) {
926 if (newrule->index == -1) {
927 index = alloc_term_index();
932 newrule->index = index;
933 term = &gl_terms[newrule->index];
934 term->op = RBCE_RULE_DEP_RULE;
935 term->u.deprule = newrule;
937 newrule->strtab = strtab;
938 newrule->termflag = 0;
940 // Fill the term vector
943 for (i = 0; i < nterms; i++) {
944 if (terms[i].op == RBCE_RULE_INVALID) {
948 newrule->terms[counter] = alloc_term_index();
949 if (newrule->terms[counter] < 0) {
950 for (j = 0; j < counter; j++) {
951 release_term_index(newrule->terms[j]);
956 term = &gl_terms[newrule->terms[counter]];
957 term->op = terms[i].op;
958 term->operator = terms[i].operator;
959 switch (terms[i].op) {
960 case RBCE_RULE_CMD_PATH:
963 case RBCE_RULE_APP_TAG:
966 term->u.string = &strtab[strtablen];
967 strcpy(term->u.string, terms[i].u.string);
968 strtablen = strlen(term->u.string) + 1;
971 case RBCE_RULE_REAL_UID:
972 case RBCE_RULE_REAL_GID:
973 case RBCE_RULE_EFFECTIVE_UID:
974 case RBCE_RULE_EFFECTIVE_GID:
976 term->u.id = terms[i].u.id;
979 case RBCE_RULE_DEP_RULE:
980 term->u.deprule = terms[i].u.deprule;
981 INC_REF(term->u.deprule);
992 put_class(targetcls);
995 release_term_index(index);
997 kfree(newrule->terms);
1004 int change_rule(const char *rname, char *rdefn)
1006 struct rbce_rule *rule = NULL, *deprule;
1007 struct rbce_rule_term *new_terms = NULL, *term, *terms;
1008 int nterms, new_term_mask = 0, oterms, tot_terms;
1009 int i, j, k, rc, new_order = 0;
1011 if ((nterms = rules_parse(rdefn, &new_terms, &new_term_mask)) <= 0) {
1012 return !nterms ? -EINVAL : nterms;
1015 write_lock(&global_rwlock);
1016 rule = find_rule_name(rname);
1018 rule = kmalloc(sizeof(struct rbce_rule), GFP_ATOMIC);
1020 rule->obj.name = kmalloc(strlen(rname) + 1, GFP_ATOMIC);
1021 if (rule->obj.name) {
1022 strcpy(rule->obj.name, rname);
1026 rule->state = RBCE_RULE_ENABLED;
1027 rule->target_class = NULL;
1028 rule->classtype = -1;
1031 INIT_LIST_HEAD(&rule->obj.link);
1032 rc = fill_rule(rule, new_terms, nterms);
1038 rule->order)) == 0) {
1043 __delete_rule(rule);
1054 write_unlock(&global_rwlock);
1058 oterms = rule->num_terms;
1059 tot_terms = nterms + oterms;
1061 terms = kmalloc(tot_terms * sizeof(struct rbce_rule_term), GFP_ATOMIC);
1065 write_unlock(&global_rwlock);
1069 new_term_mask &= ~(1 << RBCE_RULE_DEP_RULE);
1070 //ignore the new deprule terms for the first iteration.
1071 // taken care of later.
1072 for (i = 0; i < oterms; i++) {
1073 term = &gl_terms[rule->terms[i]]; // old term
1075 if ((1 << term->op) & new_term_mask) {
1076 // newrule has this attr/value
1077 for (j = 0; j < nterms; j++) {
1078 if (term->op == new_terms[j].op) {
1079 terms[i].op = new_terms[j].op;
1080 terms[i].operator = new_terms[j].
1083 new_terms[j].u.string;
1084 new_terms[j].op = RBCE_RULE_INVALID2;
1089 terms[i].op = term->op;
1090 terms[i].operator = term->operator;
1091 terms[i].u.string = term->u.string;
1095 i = oterms; // for readability
1097 for (j = 0; j < nterms; j++) {
1098 // handled in the previous iteration
1099 if (new_terms[j].op == RBCE_RULE_INVALID2) {
1103 if (new_terms[j].op == RBCE_RULE_DEP_RULE) {
1104 if (new_terms[j].operator == TOKEN_OP_DEP) {
1105 // "depend=rule" deletes all depends in the
1106 // original rule so, delete all depend rule
1107 // terms in the original rule
1108 for (k = 0; k < oterms; k++) {
1109 if (terms[k].op == RBCE_RULE_DEP_RULE) {
1110 terms[k].op = RBCE_RULE_INVALID;
1113 // must copy the new deprule term
1115 // delete the depend rule term if was defined
1116 // in the original rule for both +depend
1118 deprule = find_rule_name(new_terms[j].u.string);
1120 for (k = 0; k < oterms; k++) {
1123 && terms[k].u.deprule ==
1131 if (new_terms[j].operator == TOKEN_OP_DEP_DEL) {
1132 // No need to copy the new deprule term
1137 if ((new_terms[j].op == RBCE_RULE_INVALID) &&
1138 (new_terms[j].operator == TOKEN_OP_ORDER)) {
1142 terms[i].op = new_terms[j].op;
1143 terms[i].operator = new_terms[j].operator;
1144 terms[i].u.string = new_terms[j].u.string;
1146 new_terms[j].op = RBCE_RULE_INVALID2;
1151 // convert old deprule pointers to name pointers.
1152 for (i = 0; i < oterms; i++) {
1153 if (terms[i].op != RBCE_RULE_DEP_RULE)
1155 terms[i].u.string = terms[i].u.deprule->obj.name;
1159 __release_rule(rule);
1162 rc = fill_rule(rule, terms, tot_terms);
1163 if (rc == 0 && new_order) {
1164 rc = reinsert_rule(rule, rule->order);
1166 if (rc != 0) { // rule creation/insertion failed
1167 __delete_rule(rule);
1172 write_unlock(&global_rwlock);
1179 * Delete the specified rule.
1182 int delete_rule(const char *rname)
1185 struct rbce_rule *rule;
1187 write_lock(&global_rwlock);
1189 if ((rule = find_rule_name(rname)) == NULL) {
1190 write_unlock(&global_rwlock);
1193 rc = __delete_rule(rule);
1194 if (rbce_enabled && (gl_action & POLICY_ACTION_PACK_TERMS)) {
1197 write_unlock(&global_rwlock);
1199 DPRINTK(DBG_RULE, "delete rule %s\n", rname);
1204 * copy the rule specified by rname and to the given result string.
1207 void get_rule(const char *rname, char *result)
1210 struct rbce_rule *rule;
1211 struct rbce_rule_term *term;
1212 char *cp = result, oper, idtype[3], str[5];
1214 read_lock(&global_rwlock);
1216 rule = find_rule_name(rname);
1218 for (i = 0; i < rule->num_terms; i++) {
1219 term = gl_terms + rule->terms[i];
1221 case RBCE_RULE_REAL_UID:
1222 strcpy(idtype, "u");
1224 case RBCE_RULE_REAL_GID:
1225 strcpy(idtype, "g");
1227 case RBCE_RULE_EFFECTIVE_UID:
1228 strcpy(idtype, "eu");
1230 case RBCE_RULE_EFFECTIVE_GID:
1231 strcpy(idtype, "eg");
1234 strcpy(idtype, "x");
1236 if (term->operator == RBCE_LESS_THAN) {
1238 } else if (term->operator == RBCE_GREATER_THAN) {
1240 } else if (term->operator == RBCE_NOT) {
1246 sprintf(cp, "%sid%c%ld,", idtype, oper,
1249 case RBCE_RULE_CMD_PATH:
1250 strcpy(str, "path");
1255 case RBCE_RULE_ARGS:
1256 strcpy(str, "args");
1258 case RBCE_RULE_APP_TAG:
1261 case RBCE_RULE_IPV4:
1262 strcpy(str, "ipv4");
1264 case RBCE_RULE_IPV6:
1265 strcpy(str, "ipv6");
1268 sprintf(cp, "%s=%s,", str, term->u.string);
1270 case RBCE_RULE_DEP_RULE:
1272 sprintf(cp, "depend=%s,",
1273 term->u.deprule->obj.name);
1279 if (!rule->num_terms) {
1280 cp += sprintf(cp, "***** no terms defined ***** ");
1284 sprintf(cp, "order=%d,state=%d,", rule->order, rule->state);
1286 sprintf(cp, "class=%s",
1287 rule->target_class ? rule->target_class->obj.
1288 name : "***** REMOVED *****");
1291 sprintf(result, "***** Rule %s doesn't exist *****", rname);
1294 read_unlock(&global_rwlock);
1299 * Change the name of the given rule "from_rname" to "to_rname"
1302 int rename_rule(const char *from_rname, const char *to_rname)
1304 struct rbce_rule *rule;
1305 int nlen, rc = -EINVAL;
1307 if (!to_rname || !*to_rname) {
1310 write_lock(&global_rwlock);
1312 rule = find_rule_name(from_rname);
1314 if ((nlen = strlen(to_rname)) > strlen(rule->obj.name)) {
1315 char *name = kmalloc(nlen + 1, GFP_ATOMIC);
1319 kfree(rule->obj.name);
1320 rule->obj.name = name;
1322 strcpy(rule->obj.name, to_rname);
1325 write_unlock(&global_rwlock);
1330 * Return TRUE if the given rule exists, FALSE otherwise
1333 int rule_exists(const char *rname)
1335 struct rbce_rule *rule;
1337 read_lock(&global_rwlock);
1338 rule = find_rule_name(rname);
1339 read_unlock(&global_rwlock);
1340 return rule != NULL;
1343 /*====================== Magic file handling =======================*/
1347 static struct rbce_private_data *create_private_data(struct rbce_private_data *,
1351 void reset_evaluation(struct rbce_private_data *pdata,int termflag)
1353 /* reset TAG ruleterm evaluation results to pick up
1354 * on next classification event
1356 if (use_persistent_state && gl_mask_vecs[termflag]) {
1357 bitvector_and_not( pdata->eval, pdata->eval,
1358 gl_mask_vecs[termflag] );
1359 bitvector_and_not( pdata->true, pdata->true,
1360 gl_mask_vecs[termflag] );
1364 int set_tasktag(int pid, char *tag)
1368 struct task_struct *tsk;
1369 struct rbce_private_data *pdata;
1375 len = strlen(tag) + 1;
1376 tp = kmalloc(len, GFP_ATOMIC);
1380 strncpy(tp,tag,len);
1382 read_lock(&tasklist_lock);
1383 if ((tsk = find_task_by_pid(pid)) == NULL) {
1388 if (unlikely(!RBCE_DATA(tsk))) {
1389 RBCE_DATAP(tsk) = create_private_data(NULL, 0);
1390 if (!RBCE_DATA(tsk)) {
1395 pdata = RBCE_DATA(tsk);
1396 if (pdata->app_tag) {
1397 kfree(pdata->app_tag);
1399 pdata->app_tag = tp;
1400 reset_evaluation(pdata,RBCE_TERMFLAG_TAG);
1403 read_unlock(&tasklist_lock);
1409 /*====================== Classification Functions =======================*/
1412 * Match the given full path name with the command expression.
1413 * This function treats the folowing 2 charaters as special if seen in
1414 * cmd_exp, all other chanracters are compared as is:
1415 * ? - compares to any one single character
1416 * * - compares to one or more single characters
1418 * If fullpath is 1, tsk_comm is compared in full. otherwise only the command
1419 * name (basename(tsk_comm)) is compared.
1421 static int match_cmd(const char *tsk_comm, const char *cmd_exp, int fullpath)
1423 const char *c, *t, *last_ast, *cmd = tsk_comm;
1426 // get the command name if we don't have to match the fullpath
1427 if (!fullpath && ((c = strrchr(tsk_comm, '/')) != NULL)) {
1431 /* now faithfully assume the entire pathname is in cmd */
1433 /* we now have to effectively implement a regular expression
1435 * '?' any single character
1436 * '*' one or more '?'
1442 if (t == NULL || c == NULL) {
1462 // eat up all '*' in c
1463 while (*(c + 1) == '*')
1467 //t++; // Add this for matching '*' with "one"
1469 while (*t && (*t != *(c + 1)) && *t != '/')
1471 if (*t == *(c + 1)) {
1499 /*FALLTHRU*/ default:
1500 if (*t == *c && next_c != *t) {
1504 /* reset to last asterix and
1505 continue from there */
1515 /* check for trailing "*" */
1519 return (!*c && !*t);
1522 static void reverse(char *str, int n)
1527 for (i = 0; i < j; i++, j--) {
1534 static int itoa(int n, char *str)
1539 str[i++] = n % 10 + '0';
1544 (void)reverse(str, sz);
1548 static int v4toa(__u32 y, char *a)
1553 for (i = 0; i < 4; i++) {
1554 size += itoa(y & 0xff, &a[size]);
1561 int match_ipv4(struct ckrm_net_struct *ns, char **string)
1563 char *ptr = *string;
1567 size = v4toa(ns->ns_daddrv4, a4);
1570 return !strncmp(a4, ptr, size);
1573 int match_port(struct ckrm_net_struct *ns, char *ptr)
1576 int size = itoa(ns->ns_dport, a);
1578 return !strncmp(a, ptr, size);
1581 static int __evaluate_rule(struct task_struct *tsk, struct ckrm_net_struct *ns,
1582 struct rbce_rule *rule, bitvector_t * vec_eval,
1583 bitvector_t * vec_true, char **filename);
1585 * evaluate the given task against the given rule with the vec_eval and
1586 * vec_true in context. Return 1 if the task satisfies the given rule, 0
1589 * If the bit corresponding to the rule is set in the vec_eval, then the
1590 * corresponding bit in vec_true is the result. If it is not set, evaluate
1591 * the rule and set the bits in both the vectors accordingly.
1593 * On return, filename will have the pointer to the pathname of the task's
1594 * executable, if the rule had any command related terms.
1596 * Caller must hold the global_rwlock atleast in read mode.
1599 evaluate_rule(struct task_struct *tsk, struct ckrm_net_struct *ns,
1600 struct rbce_rule *rule, bitvector_t * vec_eval,
1601 bitvector_t * vec_true, char **filename)
1603 int tidx = rule->index;
1605 if (!bitvector_test(tidx, vec_eval)) {
1607 (tsk, ns, rule, vec_eval, vec_true, filename)) {
1608 bitvector_set(tidx, vec_true);
1610 bitvector_set(tidx, vec_eval);
1612 return bitvector_test(tidx, vec_true);
1616 * evaluate the given task against every term in the given rule with
1617 * vec_eval and vec_true in context.
1619 * If the bit corresponding to a rule term is set in the vec_eval, then the
1620 * corresponding bit in vec_true is the result for taht particular. If it is
1621 * not set, evaluate the rule term and set the bits in both the vectors
1624 * This fucntions returns true only if all terms in the rule evaluate true.
1626 * On return, filename will have the pointer to the pathname of the task's
1627 * executable, if the rule had any command related terms.
1629 * Caller must hold the global_rwlock atleast in read mode.
1632 __evaluate_rule(struct task_struct *tsk, struct ckrm_net_struct *ns,
1633 struct rbce_rule *rule, bitvector_t * vec_eval,
1634 bitvector_t * vec_true, char **filename)
1639 for (i = rule->num_terms; --i >= 0;) {
1640 int rc = 1, tidx = rule->terms[i];
1642 if (!bitvector_test(tidx, vec_eval)) {
1643 struct rbce_rule_term *term = &gl_terms[tidx];
1647 case RBCE_RULE_CMD_PATH:
1650 if (!*filename) { /* get this once */
1653 GFP_ATOMIC)) == NULL)
1656 (tsk, *filename, NAME_MAX) < 0)) {
1661 rc = match_cmd(*filename, term->u.string,
1663 RBCE_RULE_CMD_PATH));
1665 rc = match_cmd(tsk->comm, term->u.string,
1667 RBCE_RULE_CMD_PATH));
1670 case RBCE_RULE_REAL_UID:
1671 if (term->operator == RBCE_LESS_THAN) {
1672 rc = (tsk->uid < term->u.id);
1673 } else if (term->operator == RBCE_GREATER_THAN){
1674 rc = (tsk->uid > term->u.id);
1675 } else if (term->operator == RBCE_NOT) {
1676 rc = (tsk->uid != term->u.id);
1678 rc = (tsk->uid == term->u.id);
1681 case RBCE_RULE_REAL_GID:
1682 if (term->operator == RBCE_LESS_THAN) {
1683 rc = (tsk->gid < term->u.id);
1684 } else if (term->operator == RBCE_GREATER_THAN){
1685 rc = (tsk->gid > term->u.id);
1686 } else if (term->operator == RBCE_NOT) {
1687 rc = (tsk->gid != term->u.id);
1689 rc = (tsk->gid == term->u.id);
1692 case RBCE_RULE_EFFECTIVE_UID:
1693 if (term->operator == RBCE_LESS_THAN) {
1694 rc = (tsk->euid < term->u.id);
1695 } else if (term->operator == RBCE_GREATER_THAN){
1696 rc = (tsk->euid > term->u.id);
1697 } else if (term->operator == RBCE_NOT) {
1698 rc = (tsk->euid != term->u.id);
1700 rc = (tsk->euid == term->u.id);
1703 case RBCE_RULE_EFFECTIVE_GID:
1704 if (term->operator == RBCE_LESS_THAN) {
1705 rc = (tsk->egid < term->u.id);
1706 } else if (term->operator == RBCE_GREATER_THAN){
1707 rc = (tsk->egid > term->u.id);
1708 } else if (term->operator == RBCE_NOT) {
1709 rc = (tsk->egid != term->u.id);
1711 rc = (tsk->egid == term->u.id);
1714 case RBCE_RULE_APP_TAG:
1715 rc = (RBCE_DATA(tsk)
1717 app_tag) ? !strcmp(RBCE_DATA(tsk)->
1719 term->u.string) : 0;
1721 case RBCE_RULE_DEP_RULE:
1722 rc = evaluate_rule(tsk, NULL, term->u.deprule,
1727 case RBCE_RULE_IPV4:
1728 // TBD: add NOT_EQUAL match. At present rbce
1729 // recognises EQUAL matches only.
1730 if (ns && term->operator == RBCE_EQUAL) {
1733 char *ptr = term->u.string;
1735 if (term->u.string[0] == '*')
1738 ma = match_ipv4(ns, &ptr);
1740 if (*ptr != '\\') { // error
1757 case RBCE_RULE_IPV6: // no support yet
1764 xid_t xid = vx_task_xid(tsk);
1766 if (term->operator == RBCE_LESS_THAN) {
1767 rc = (xid < term->u.id);
1768 } else if (term->operator == RBCE_GREATER_THAN) {
1769 rc = (xid > term->u.id);
1770 } else if (term->operator == RBCE_NOT) {
1771 rc = (xid != term->u.id);
1773 rc = (xid == term->u.id);
1780 printk(KERN_ERR "Error evaluate term op=%d\n",
1785 bitvector_clear(tidx, vec_true);
1787 bitvector_set(tidx, vec_true);
1789 bitvector_set(tidx, vec_eval);
1791 rc = bitvector_test(tidx, vec_true);
1800 //#define PDATA_DEBUG
1803 #define MAX_PDATA 10000
1804 void *pdata_arr[MAX_PDATA];
1805 int pdata_count, pdata_next;
1806 static spinlock_t pdata_lock = SPIN_LOCK_UNLOCKED;
1808 static inline int valid_pdata(struct rbce_private_data *pdata)
1815 spin_lock(&pdata_lock);
1816 for (i = 0; i < MAX_PDATA; i++) {
1817 if (pdata_arr[i] == pdata) {
1818 spin_unlock(&pdata_lock);
1822 spin_unlock(&pdata_lock);
1823 printk(KERN_WARNING "INVALID/CORRUPT PDATA %p\n", pdata);
1827 static inline void store_pdata(struct rbce_private_data *pdata)
1832 spin_lock(&pdata_lock);
1834 while (i < MAX_PDATA) {
1835 if (pdata_arr[pdata_next] == NULL) {
1836 printk(KERN_DEBUG "storing %p at %d, count %d\n", pdata,
1837 pdata_next, pdata_count);
1838 pdata_arr[pdata_next++] = pdata;
1839 if (pdata_next == MAX_PDATA) {
1848 spin_unlock(&pdata_lock);
1850 if (i == MAX_PDATA) {
1851 printk(KERN_DEBUG "PDATA BUFFER FULL pdata_count %d pdata %p\n",
1852 pdata_count, pdata);
1856 static inline void unstore_pdata(struct rbce_private_data *pdata)
1860 spin_lock(&pdata_lock);
1861 for (i = 0; i < MAX_PDATA; i++) {
1862 if (pdata_arr[i] == pdata) {
1863 printk(KERN_DEBUG "unstoring %p at %d, count %d\n", pdata,
1865 pdata_arr[i] = NULL;
1871 spin_unlock(&pdata_lock);
1872 if (i == MAX_PDATA) {
1873 printk(KERN_DEBUG "pdata %p not found in the stored array\n",
1880 #else // PDATA_DEBUG
1882 #define valid_pdata(pdata) (1)
1883 #define store_pdata(pdata)
1884 #define unstore_pdata(pdata)
1886 #endif // PDATA_DEBUG
1889 * Allocate and initialize a rbce_private_data data structure.
1891 * Caller must hold global_rwlock atleast in read mode.
1895 copy_ext_private_data(struct rbce_private_data *src,
1896 struct rbce_private_data *dst)
1899 dst->ext_data = src->ext_data;
1901 memset(&dst->ext_data, 0, sizeof(dst->ext_data));
1904 static struct rbce_private_data *create_private_data(struct rbce_private_data
1905 *src, int copy_sample)
1907 int vsize, psize, bsize;
1908 struct rbce_private_data *pdata;
1910 if (use_persistent_state) {
1911 vsize = gl_allocated;
1912 bsize = vsize / 8 + sizeof(bitvector_t);
1913 psize = sizeof(struct rbce_private_data) + 2 * bsize;
1915 psize = sizeof(struct rbce_private_data);
1918 pdata = kmalloc(psize, GFP_ATOMIC);
1919 if (pdata != NULL) {
1920 if (use_persistent_state) {
1921 pdata->bitmap_version = gl_bitmap_version;
1922 pdata->eval = (bitvector_t *) & pdata->data[0];
1923 pdata->true = (bitvector_t *) & pdata->data[bsize];
1924 if (src && (src->bitmap_version == gl_bitmap_version)) {
1925 memcpy(pdata->data, src->data, 2 * bsize);
1927 bitvector_init(pdata->eval, vsize);
1928 bitvector_init(pdata->true, vsize);
1931 copy_ext_private_data(src, pdata);
1932 //if (src) { // inherit evaluate and app_tag
1933 // pdata->evaluate = src->evaluate;
1934 // if(src->app_tag) {
1935 // int len = strlen(src->app_tag)+1;
1936 // printk(KERN_DEBUG "CREATE_PRIVATE: apptag %s len %d\n",
1937 // src->app_tag,len);
1938 // pdata->app_tag = kmalloc(len, GFP_ATOMIC);
1939 // if (pdata->app_tag) {
1940 // strcpy(pdata->app_tag, src->app_tag);
1944 pdata->evaluate = 1;
1945 pdata->rules_version = src ? src->rules_version : 0;
1946 pdata->app_tag = NULL;
1953 static inline void free_private_data(struct rbce_private_data *pdata)
1955 if (valid_pdata(pdata)) {
1956 unstore_pdata(pdata);
1961 static void free_all_private_data(void)
1963 struct task_struct *proc, *thread;
1965 read_lock(&tasklist_lock);
1966 do_each_thread(proc, thread) {
1967 struct rbce_private_data *pdata;
1969 pdata = RBCE_DATA(thread);
1970 RBCE_DATAP(thread) = NULL;
1971 free_private_data(pdata);
1972 } while_each_thread(proc, thread);
1973 read_unlock(&tasklist_lock);
1978 * reclassify function, which is called by all the callback functions.
1980 * Takes that task to be reclassified and ruleflags that indicates the
1981 * attributes that caused this reclassification request.
1983 * On success, returns the core class pointer to which the given task should
1986 static struct ckrm_core_class *rbce_classify(struct task_struct *tsk,
1987 struct ckrm_net_struct *ns,
1988 unsigned long termflag,
1992 struct rbce_rule *rule;
1993 bitvector_t *vec_true = NULL, *vec_eval = NULL;
1994 struct rbce_class *tgt = NULL;
1995 struct ckrm_core_class *cls = NULL;
1996 char *filename = NULL;
1998 if (!valid_pdata(RBCE_DATA(tsk))) {
2001 if (classtype >= CKRM_MAX_CLASSTYPES) {
2002 // can't handle more than CKRM_MAX_CLASSTYPES
2005 // fast path to avoid locking in case CE is not enabled or if no rules
2006 // are defined or if the tasks states that no evaluation is needed.
2007 if (!rbce_enabled || !gl_num_rules ||
2008 (RBCE_DATA(tsk) && !RBCE_DATA(tsk)->evaluate)) {
2011 // FIXME: optimize_policy should be called from here if
2012 // gl_action is non-zero. Also, it has to be called with the
2013 // global_rwlock held in write mode.
2015 read_lock(&global_rwlock);
2017 vec_eval = vec_true = NULL;
2018 if (use_persistent_state) {
2019 struct rbce_private_data *pdata = RBCE_DATA(tsk);
2023 && (gl_bitmap_version != pdata->bitmap_version))) {
2024 struct rbce_private_data *new_pdata =
2025 create_private_data(pdata, 1);
2029 new_pdata->rules_version =
2030 pdata->rules_version;
2031 new_pdata->evaluate = pdata->evaluate;
2032 new_pdata->app_tag = pdata->app_tag;
2033 free_private_data(pdata);
2035 pdata = RBCE_DATAP(tsk) = new_pdata;
2036 termflag = RBCE_TERMFLAG_ALL;
2037 // need to evaluate them all
2039 // we shouldn't free the pdata as it has more
2040 // details than the vectors. But, this
2041 // reclassification should go thru
2046 goto cls_determined;
2048 vec_eval = pdata->eval;
2049 vec_true = pdata->true;
2051 int bsize = gl_allocated;
2053 vec_eval = bitvector_alloc(bsize);
2054 vec_true = bitvector_alloc(bsize);
2056 if (vec_eval == NULL || vec_true == NULL) {
2057 goto cls_determined;
2059 termflag = RBCE_TERMFLAG_ALL;
2060 // need to evaluate all of them now
2064 * using bit ops invalidate all terms related to this termflag
2065 * context (only in per task vec)
2067 DPRINTK(DBG_CLASSIFY_DETAILS, "\nClassify: termflag=%lx\n", termflag);
2068 DPRINTK(DBG_CLASSIFY_DETAILS, " eval before: ");
2069 bitvector_print(DBG_CLASSIFY_DETAILS, vec_eval);
2070 DPRINTK(DBG_CLASSIFY_DETAILS, "\n true before: ");
2071 bitvector_print(DBG_CLASSIFY_DETAILS, vec_true);
2072 DPRINTK(DBG_CLASSIFY_DETAILS, "\n redo => ");
2074 if (termflag == RBCE_TERMFLAG_ALL) {
2075 DPRINTK(DBG_CLASSIFY_DETAILS, " redoall ");
2076 bitvector_zero(vec_eval);
2078 for (i = 0; i < NUM_TERM_MASK_VECTOR; i++) {
2079 if (test_bit(i, &termflag)) {
2080 bitvector_t *maskvec = gl_mask_vecs[i];
2082 DPRINTK(DBG_CLASSIFY_DETAILS, " mask(%d) ", i);
2083 bitvector_print(DBG_CLASSIFY_DETAILS, maskvec);
2084 bitvector_and_not(vec_eval, vec_eval, maskvec);
2088 bitvector_and(vec_true, vec_true, vec_eval);
2090 DPRINTK(DBG_CLASSIFY_DETAILS, "\n eval now: ");
2091 bitvector_print(DBG_CLASSIFY_DETAILS, vec_eval);
2092 DPRINTK(DBG_CLASSIFY_DETAILS, "\n");
2094 /* run through the rules in order and see what needs evaluation */
2095 list_for_each_entry(rule, &rules_list[classtype], obj.link) {
2096 if (rule->state == RBCE_RULE_ENABLED &&
2097 rule->target_class &&
2098 rule->target_class->classobj &&
2099 evaluate_rule(tsk, ns, rule, vec_eval, vec_true,
2101 tgt = rule->target_class;
2102 cls = rule->target_class->classobj;
2108 DPRINTK(DBG_CLASSIFY_RES,
2109 "==> |%s|; pid %d; euid %d; egid %d; ruid %d; rgid %d;"
2110 "tag |%s| ===> class |%s|\n",
2111 filename ? filename : tsk->comm,
2117 RBCE_DATA(tsk) ? RBCE_DATA(tsk)->app_tag : "",
2118 tgt ? tgt->obj.name : "");
2119 DPRINTK(DBG_CLASSIFY_DETAILS, " eval after: ");
2120 bitvector_print(DBG_CLASSIFY_DETAILS, vec_eval);
2121 DPRINTK(DBG_CLASSIFY_DETAILS, "\n true after: ");
2122 bitvector_print(DBG_CLASSIFY_DETAILS, vec_true);
2123 DPRINTK(DBG_CLASSIFY_DETAILS, "\n");
2125 if (!use_persistent_state) {
2127 bitvector_free(vec_eval);
2130 bitvector_free(vec_true);
2133 ckrm_core_grab(cls);
2134 read_unlock(&global_rwlock);
2138 if (RBCE_DATA(tsk)) {
2139 RBCE_DATA(tsk)->rules_version = gl_rules_version;
2144 /*****************************************************************************
2146 * Module specific utilization of core RBCE functionality
2148 * Includes support for the various classtypes
2149 * New classtypes will require extensions here
2151 *****************************************************************************/
2153 /* helper functions that are required in the extended version */
2155 static inline void rbce_tc_manual(struct task_struct *tsk)
2157 read_lock(&global_rwlock);
2159 if (!RBCE_DATA(tsk)) {
2161 (void *)create_private_data(RBCE_DATA(tsk->parent), 0);
2163 if (RBCE_DATA(tsk)) {
2164 RBCE_DATA(tsk)->evaluate = 0;
2166 read_unlock(&global_rwlock);
2170 /*****************************************************************************
2171 * load any extensions
2172 *****************************************************************************/
2174 #ifdef RBCE_EXTENSION
2175 #include "rbcemod_ext.c"
2178 /*****************************************************************************
2179 * VARIOUS CLASSTYPES
2180 *****************************************************************************/
2182 // to enable type coercion of the function pointers
2184 /*============================================================================
2185 * TASKCLASS CLASSTYPE
2186 *============================================================================*/
2188 int tc_classtype = -1;
2191 * fork callback to be registered with core module.
2193 inline static void *rbce_tc_forkcb(struct task_struct *tsk)
2195 int rule_version_changed = 1;
2196 struct ckrm_core_class *cls;
2197 read_lock(&global_rwlock);
2200 (void *)create_private_data(RBCE_DATA(tsk->parent), 0);
2201 read_unlock(&global_rwlock);
2203 if (RBCE_DATA(tsk->parent)) {
2204 rule_version_changed =
2205 (RBCE_DATA(tsk->parent)->rules_version != gl_rules_version);
2207 cls = rule_version_changed ?
2208 rbce_classify(tsk, NULL, RBCE_TERMFLAG_ALL, tc_classtype) : NULL;
2210 // note the fork notification to any user client will be sent through
2211 // the guaranteed fork-reclassification
2216 * exit callback to be registered with core module.
2218 static void rbce_tc_exitcb(struct task_struct *tsk)
2220 struct rbce_private_data *pdata;
2222 send_exit_notification(tsk);
2224 pdata = RBCE_DATA(tsk);
2225 RBCE_DATAP(tsk) = NULL;
2227 if (pdata->app_tag) {
2228 kfree(pdata->app_tag);
2230 free_private_data(pdata);
2235 #define AENT(x) [ CKRM_EVENT_##x] = #x
2236 static const char *event_names[CKRM_NUM_EVENTS] = {
2254 void *rbce_tc_classify(enum ckrm_event event, ...)
2258 struct task_struct *tsk;
2259 struct rbce_private_data *pdata;
2261 va_start(args, event);
2262 tsk = va_arg(args, struct task_struct *);
2265 /* we only have to deal with events between
2266 * [ CKRM_LATCHABLE_EVENTS .. CKRM_NONLATCHABLE_EVENTS )
2269 // printk(KERN_DEBUG "tc_classify %p:%d:%s '%s'\n",tsk,tsk->pid,
2270 // tsk->comm,event_names[event]);
2274 case CKRM_EVENT_FORK:
2275 cls = rbce_tc_forkcb(tsk);
2278 case CKRM_EVENT_EXIT:
2279 rbce_tc_exitcb(tsk);
2282 case CKRM_EVENT_EXEC:
2283 cls = rbce_classify(tsk, NULL, RBCE_TERMFLAG_CMD |
2284 RBCE_TERMFLAG_UID | RBCE_TERMFLAG_GID,
2288 case CKRM_EVENT_UID:
2289 cls = rbce_classify(tsk, NULL, RBCE_TERMFLAG_UID, tc_classtype);
2292 case CKRM_EVENT_GID:
2293 cls = rbce_classify(tsk, NULL, RBCE_TERMFLAG_GID, tc_classtype);
2296 case CKRM_EVENT_XID:
2297 cls = rbce_classify(tsk, NULL, RBCE_TERMFLAG_XID, tc_classtype);
2300 case CKRM_EVENT_LOGIN:
2301 case CKRM_EVENT_USERADD:
2302 case CKRM_EVENT_USERDEL:
2303 case CKRM_EVENT_LISTEN_START:
2304 case CKRM_EVENT_LISTEN_STOP:
2305 case CKRM_EVENT_APPTAG:
2306 /* no interest in this events .. */
2313 case CKRM_EVENT_RECLASSIFY:
2314 if ((pdata = (RBCE_DATA(tsk)))) {
2315 pdata->evaluate = 1;
2317 cls = rbce_classify(tsk, NULL, RBCE_TERMFLAG_ALL, tc_classtype);
2321 // printk(KERN_DEBUG "tc_classify %p:%d:%s '%s' ==> %p\n",tsk,tsk->pid,
2322 // tsk->comm,event_names[event],cls);
2327 #ifndef RBCE_EXTENSION
2328 static void rbce_tc_notify(int event, void *core, struct task_struct *tsk)
2330 printk(KERN_DEBUG "tc_manual %p:%d:%s '%s'\n", tsk, tsk->pid, tsk->comm,
2331 event_names[event]);
2332 if (event != CKRM_EVENT_MANUAL)
2334 rbce_tc_manual(tsk);
2338 static struct ckrm_eng_callback rbce_taskclass_ecbs = {
2339 .c_interest = (unsigned long)(-1), // set whole bitmap
2340 .classify = (ce_classify_fct_t) rbce_tc_classify,
2341 .class_delete = rbce_class_deletecb,
2342 #ifndef RBCE_EXTENSION
2343 .n_interest = (1 << CKRM_EVENT_MANUAL),
2344 .notify = (ce_notify_fct_t) rbce_tc_notify,
2345 .always_callback = 0,
2347 .n_interest = (unsigned long)(-1), // set whole bitmap
2348 .notify = (ce_notify_fct_t) rbce_tc_ext_notify,
2349 .class_add = rbce_class_addcb,
2350 .always_callback = 1,
2354 /*============================================================================
2356 *============================================================================*/
2358 int sc_classtype = -1;
2360 void *rbce_sc_classify(enum ckrm_event event, ...)
2362 // no special consideratation
2365 struct task_struct *tsk;
2366 struct ckrm_net_struct *ns;
2368 va_start(args, event);
2369 ns = va_arg(args, struct ckrm_net_struct *);
2370 tsk = va_arg(args, struct task_struct *);
2373 result = rbce_classify(tsk, ns, RBCE_TERMFLAG_ALL, sc_classtype);
2375 DPRINTK(DBG_CLASSIFY_RES,
2376 "==> %d.%d.%d.%d\\%d , %p:%d:%s '%s' => %p\n",
2377 NIPQUAD(ns->ns_daddrv4), ns->ns_dport,
2378 tsk, tsk ? tsk->pid : 0, tsk ? tsk->comm : "-",
2379 event_names[event], result);
2383 static struct ckrm_eng_callback rbce_acceptQclass_ecbs = {
2384 .c_interest = (unsigned long)(-1),
2385 .always_callback = 0, // enable during debugging only
2386 .classify = (ce_classify_fct_t) & rbce_sc_classify,
2387 .class_delete = rbce_class_deletecb,
2390 /*============================================================================
2391 * Module Initialization ...
2392 *============================================================================*/
2394 #define TASKCLASS_NAME "taskclass"
2395 #define SOCKCLASS_NAME "socket_class"
2397 struct ce_regtable_struct {
2399 struct ckrm_eng_callback *cbs;
2403 struct ce_regtable_struct ce_regtable[] = {
2404 {TASKCLASS_NAME, &rbce_taskclass_ecbs, &tc_classtype},
2405 {SOCKCLASS_NAME, &rbce_acceptQclass_ecbs, &sc_classtype},
2409 static void unregister_classtype_engines(void)
2412 struct ce_regtable_struct *ceptr = ce_regtable;
2414 while (ceptr->name) {
2415 if (*ceptr->clsvar >= 0) {
2416 printk(KERN_DEBUG "ce unregister with <%s>\n",ceptr->name);
2417 while ((rc = ckrm_unregister_engine(ceptr->name)) == -EAGAIN)
2419 printk(KERN_DEBUG "ce unregister with <%s> rc=%d\n",ceptr->name,rc);
2420 *ceptr->clsvar = -1;
2426 static int register_classtype_engines(void)
2429 struct ce_regtable_struct *ceptr = ce_regtable;
2431 while (ceptr->name) {
2432 rc = ckrm_register_engine(ceptr->name, ceptr->cbs);
2433 printk(KERN_DEBUG "ce register with <%s> typeId=%d\n",ceptr->name,rc);
2434 if ((rc < 0) && (rc != -ENOENT)) {
2435 unregister_classtype_engines();
2439 *ceptr->clsvar = rc;
2445 // =========== /proc/sysctl/debug/rbce debug stuff =============
2448 static struct ctl_table_header *rbce_sysctl_table_header;
2450 #define CTL_RBCE_DEBUG (201) // picked some number.. dont know algo to pick
2451 static struct ctl_table rbce_entry_table[] = {
2453 .ctl_name = CTL_RBCE_DEBUG,
2456 .maxlen = sizeof(int),
2458 .proc_handler = &proc_dointvec,
2463 static struct ctl_table rbce_root_table[] = {
2465 .ctl_name = CTL_DEBUG,
2466 .procname = "debug",
2470 .child = rbce_entry_table},
2474 static inline void start_debug(void)
2476 rbce_sysctl_table_header = register_sysctl_table(rbce_root_table, 1);
2478 static inline void stop_debug(void)
2480 if (rbce_sysctl_table_header)
2481 unregister_sysctl_table(rbce_sysctl_table_header);
2486 static inline void start_debug(void)
2489 static inline void stop_debug(void)
2495 extern int rbce_mkdir(struct inode *, struct dentry *, int);
2496 extern int rbce_rmdir(struct inode *, struct dentry *);
2497 extern int rbce_create_magic(void);
2498 extern int rbce_clear_magic(void);
2500 rbce_eng_callback_t rcfs_ecbs = {
2507 /* ======================= Module definition Functions ====================== */
2513 printk(KERN_DEBUG "<1>\nInstalling \'%s\' module\n", modname);
2515 for (i = 0; i < CKRM_MAX_CLASSTYPES; i++) {
2516 INIT_LIST_HEAD(&rules_list[i]);
2519 rc = init_rbce_ext_pre();
2524 rc = register_classtype_engines();
2527 goto out_unreg_ckrm; // need to remove anyone opened
2529 /* register any other class type engine here */
2531 rc = rcfs_register_engine(&rcfs_ecbs);
2534 goto out_unreg_ckrm;
2537 rc = rbce_create_magic();
2540 goto out_unreg_rcfs;
2545 rc = init_rbce_ext_post();
2550 return 0; // SUCCESS
2556 rcfs_unregister_engine(&rcfs_ecbs);
2558 unregister_classtype_engines();
2562 printk(KERN_DEBUG "<1>%s: error installing rc=%d line=%d\n", __FUNCTION__, rc,
2567 void exit_rbce(void)
2571 printk(KERN_DEBUG "<1>Removing \'%s\' module\n", modname);
2576 // Print warnings if lists are not empty, which is a bug
2577 if (!list_empty(&class_list)) {
2578 printk(KERN_DEBUG "exit_rbce: Class list is not empty\n");
2581 for (i = 0; i < CKRM_MAX_CLASSTYPES; i++) {
2582 if (!list_empty(&rules_list[i])) {
2583 printk(KERN_DEBUG "exit_rbce: Rules list for classtype %d"
2584 " is not empty\n", i);
2591 rcfs_unregister_engine(&rcfs_ecbs);
2592 unregister_classtype_engines();
2593 free_all_private_data();
2596 EXPORT_SYMBOL(get_rule);
2597 EXPORT_SYMBOL(rule_exists);
2598 EXPORT_SYMBOL(change_rule);
2599 EXPORT_SYMBOL(delete_rule);
2600 EXPORT_SYMBOL(rename_rule);
2601 EXPORT_SYMBOL(set_tasktag);
2603 module_init(init_rbce);
2604 module_exit(exit_rbce);