[PATCH] TPE updates

From: Chris Wright (chrisat_private)
Date: Wed Jun 11 2003 - 17:21:40 PDT

  • Next message: sales: "Companies Inc."

    Hi Niki,
    
    Here are some more TPE updates.  Removing the fs code helped get rid of
    some memory leaks, as well as the ability for users to specify how much
    kernel memory they want allocated (IIRC), so cleanups are going well ;-)
    
    This is largely CodingStyle type changes, plus a couple spots where
    you'll return with the spin_lock held.  Could you review these changes?
    
    - fix returns without spin_unlock
    - change -EFAULT -> -EINVAL
    - use common return
    - move some large inline code to .c
    - fix unload order
    - fix __get_free_pages memleak
    
    thanks,
    -chris
    -- 
    Linux Security Modules     http://lsm.immunix.org     http://lsm.bkbits.net
    
    ===== security/tpe.c 1.5 vs edited =====
    --- 1.5/security/tpe.c	Wed Jun 11 12:04:12 2003
    +++ edited/security/tpe.c	Wed Jun 11 17:18:13 2003
    @@ -65,11 +65,103 @@
     
     #include "tpe.h"
     
    -/* Beginning of a sysfs subsystem for tpe */
    -
     static int secondary;
    -static spinlock_t tpe_acl_lock;
    +static spinlock_t tpe_acl_lock = SPIN_LOCK_UNLOCKED;
    +
    +/*  Insertion sort the list. */
    +static void tpe_sort (int low, int high)  /* (list low element, list high element) */
    +{
    +	int i,j,n;
    +	/* Standard insertion sort. */
    +	for (i = low + 1; i <= high; i++) {
    +		if (tpe_acl[i] < tpe_acl[low]) {
    +			tpe_acl[low] ^= tpe_acl[i];
    +			tpe_acl[i] ^= tpe_acl[low];
    +			tpe_acl[low] ^= tpe_acl[i];
    +		}	
    +	}
    +
    +	for (i = low + 2; i <= high; i++) {
    +		j = i;
    +		n = tpe_acl[i];
    +		while (n < tpe_acl[j - 1]) {
    +			tpe_acl[j] = tpe_acl[j - 1];
    +			j--;
    +		}
    +		tpe_acl[j] = n;
    +	}
    +}
    +
    +/*  Attempt to add a candidate to the list.  */
    +static int tpe_add (uid_t add_candidate)
    +{
    +	int retval = -EINVAL;
    +
    +	/* Full list. */
    +	if (tpe_acl_candidates == (TPE_ACL_SIZE - 2)) {
    +		printk(KERN_INFO "Unable to add user %d. List is full.\n",
    +				  add_candidate);
    +		goto out;
    +	}
    +	
    +	if (add_candidate == 0) {
    +		printk(KERN_INFO "tpe: Invalid userid. Cannot add.\n");
    +		goto out;
    +	}
    +
    +	/* Don't add duplicates */
    +	if ((tpe_search(add_candidate)) == NACK) {
    +		/* Add to the end of the list, then sort. */
    +		tpe_acl_candidates++;
    +		tpe_acl[tpe_acl_candidates] = add_candidate;
    +		tpe_acl[tpe_acl_candidates + 1] = '\0'; /* terminate array */
    +		tpe_sort(0, tpe_acl_candidates);
    +		printk(KERN_INFO "tpe: UID %d added to trust list\n",
    +			 	  add_candidate);
    +	} else {
    +		printk(KERN_INFO "tpe: duplicate UID %d not added\n",
    +				  add_candidate);
    +		goto out;
    +	}
    +	retval = 0;
    +out:
    +	return retval;
    +}	
    + 
    +/*  Attempt to remove a candidate from the list.  Only fails if the entry is */
    +/*  not there. */
    +static int tpe_remove (uid_t rem_candidate)
    +{
    +	int n;
    +	int retval = -EINVAL;
    +	if (tpe_acl_candidates == 0) {
    +		/* Empty list */
    +		goto out;
    +	}
    +	if (rem_candidate == 0) {
    +		printk(KERN_INFO "tpe: Invalid userid. Cannot remove.\n");
    +		goto out;
    +	}
    +		
    +	n = tpe_search(rem_candidate);
    +	if (n != NACK) {
    +		/* Remove candidate (mark slot as unused), resort the list. */
    +		tpe_acl[n] = TPE_INITIALIZER;
    +		tpe_acl_candidates--;
    +		tpe_sort(0, tpe_acl_candidates);
    +		printk(KERN_INFO "tpe: UID %d removed from trust list\n",
    +				  rem_candidate);
    +		retval = 0;
    +		goto out;
    +	}
    +	/* Not found. */
    +	printk(KERN_INFO "tpe: UID %d not found in trust list\n",
    +			  rem_candidate);
    +out:
    +	return retval;
    +}
     
    +/* Beginning of a sysfs subsystem for tpe */
     static struct subsystem tpefs_subsys;
     
     struct tpe_list {
    @@ -114,12 +206,6 @@
     
     static decl_subsys(tpefs, &tpefs_ktype, NULL);
     
    -#if defined(CONFIG_SECURITY_TPE_MODULE)
    -#define MY_NAME THIS_MODULE->name
    -#else
    -#define MY_NAME "tpe"
    -#endif
    -
     static ssize_t trustedlistadd_read_file (struct tpe_list *list, char *buf)
     {
     	int i;
    @@ -145,10 +231,11 @@
     		strcat(buffer, user);
     	}
     	printk(KERN_INFO "\n");
    -	spin_unlock (&tpe_acl_lock);
    +	spin_unlock(&tpe_acl_lock);
     	
     	retval = snprintf(buf, 4096, "%s\n", buffer);
     
    +	free_page((unsigned long)user);
     	return retval;
     }
     
    @@ -163,10 +250,10 @@
     	printk(KERN_INFO "value of add_candidate is %d.\n", (int)add_candidate);
     	spin_lock(&tpe_acl_lock);
     	retval = tpe_add(add_candidate);
    +	spin_unlock(&tpe_acl_lock);
     	if (retval) {
     		return retval;
     	} 
    -	spin_unlock (&tpe_acl_lock);
     	return count;
     
     }
    @@ -188,10 +275,10 @@
     	printk(KERN_INFO "value of rem_candidate is %d.\n", (int)rem_candidate);
     	spin_lock(&tpe_acl_lock);
     	retval = tpe_remove(rem_candidate);
    +	spin_unlock(&tpe_acl_lock);
     	if (retval) {
     		return retval;
     	} 
    -	spin_unlock (&tpe_acl_lock);
     	return count;
     
     }
    @@ -220,6 +307,12 @@
             bprm_set_security:              tpe_bprm_set_security,
     };
     
    +#if defined(CONFIG_SECURITY_TPE_MODULE)
    +#define MY_NAME THIS_MODULE->name
    +#else
    +#define MY_NAME "tpe"
    +#endif
    +
     static int __init tpe_module_init (void)
     {
     	int retval;
    @@ -236,8 +329,6 @@
     		secondary = 1;
     	}
     	
    -	spin_lock_init(&tpe_acl_lock);
    -	
     	/* register tpe subsystem */
     	printk(KERN_INFO "registering tpe subsystem.\n");
     	retval = subsystem_register(&tpefs_subsys);
    @@ -257,6 +348,10 @@
     
     static void __exit tpe_exit (void)
     {
    +	sysfs_remove_file(&tpefs_subsys.kset.kobj, &tpefs_listadd_attr.attr);
    +	sysfs_remove_file(&tpefs_subsys.kset.kobj, &tpefs_listdel_attr.attr);
    +	subsystem_unregister(&tpefs_subsys);	
    +	
     	/* remove ourselves from the security framework */
     	if (secondary) {
     		if (mod_unreg_security (MY_NAME, &tpe_security_ops))
    @@ -264,10 +359,6 @@
     				"with primary module.\n");
     			return;
     	}
    -	
    -	sysfs_remove_file(&tpefs_subsys.kset.kobj, &tpefs_listadd_attr.attr);
    -	sysfs_remove_file(&tpefs_subsys.kset.kobj, &tpefs_listdel_attr.attr);
    -	subsystem_unregister(&tpefs_subsys);	
     	
     	if (unregister_security (&tpe_security_ops)) {
     		printk (KERN_INFO
    ===== security/tpe.h 1.3 vs edited =====
    --- 1.3/security/tpe.h	Wed Jun 11 12:04:12 2003
    +++ edited/security/tpe.h	Wed Jun 11 17:15:59 2003
    @@ -63,9 +63,6 @@
     #define NACK            -1      /* negative acknowledgement */
     #define DUP		3	/* duplicate id return for tpe_add */
     
    -static inline void tpe_sort(int, int);
    -static inline int tpe_search(uid_t);
    -
     /*
      *  Verify the path.  
      */
    @@ -92,105 +89,6 @@
     	tpe_acl[0] = 0;
     }
     
    -/*  Attempt to add a candidate to the list.  */
    -static inline int tpe_add (uid_t add_candidate)
    -{
    -	int retval = 0;
    -
    -	/* Full list. */
    -	if (tpe_acl_candidates == (TPE_ACL_SIZE - 2)) {
    -		printk(KERN_INFO "Unable to add user %d. List is full.\n",
    -				  add_candidate);
    -		return -EFAULT; 
    -	}
    -	
    -	if (add_candidate == 0) {
    -		printk(KERN_INFO "tpe: Invalid userid. Cannot add.\n");
    -		return -EFAULT;
    -	}
    -
    -	/* Don't add duplicates */
    -	if ((tpe_search(add_candidate)) == NACK) {
    -		/* Add to the end of the list, then sort. */
    -		tpe_acl_candidates++;
    -		tpe_acl[tpe_acl_candidates] = add_candidate;
    -		tpe_acl[tpe_acl_candidates + 1] = '\0'; /* terminate array */
    -		tpe_sort(0, tpe_acl_candidates);
    -		printk(KERN_INFO "tpe: UID %d added to trust list\n",
    -			 	  add_candidate);
    -	} else {
    -		printk(KERN_INFO "tpe: duplicate UID %d not added\n",
    -				  add_candidate);
    -		return -EFAULT;
    -	}
    -	return retval;
    -}	
    - 
    -/*  Attempt to remove a candidate from the list.  Only fails if the entry is */
    -/*  not there. */
    -static inline int tpe_remove (uid_t rem_candidate)
    -{
    -	int n;
    -	int retval = 0;
    -	if (tpe_acl_candidates == 0) {
    -		/* Empty list */
    -		return -EFAULT;
    -	}
    -	if (rem_candidate == 0) {
    -		printk(KERN_INFO "tpe: Invalid userid. Cannot remove.\n");
    -		return -EFAULT;
    -	}
    -		
    -	if ((n= tpe_search(rem_candidate)) != NACK) {
    -		/* Remove candidate (mark slot as unused), resort the list. */
    -		tpe_acl[n] = TPE_INITIALIZER;
    -		tpe_acl_candidates--;
    -		tpe_sort(0, tpe_acl_candidates);
    -		printk(KERN_INFO "tpe: UID %d removed from trust list\n",
    -				  rem_candidate);
    -		return retval;
    -	}
    -	/* Not found. */
    -	printk(KERN_INFO "tpe: UID %d not found in trust list\n",
    -			  rem_candidate);
    -	return -EFAULT;
    -}
    -
    -/*  Verify a candidate user. */
    -
    -static inline int tpe_verify (uid_t candidate)
    -{
    -	if ((tpe_search(candidate)) != NACK) {	
    -		return (ACK);
    -	}
    -	return (NACK);
    -}
    -
    -
    -/*  Insertion sort the list. */
    -static inline void tpe_sort (int low, int high)  /* (list low element, list high element) */
    -{
    -	int i,j,n;
    -	/* Standard insertion sort. */
    -	for (i = low + 1; i <= high; i++) {
    -		if (tpe_acl[i] < tpe_acl[low]) {
    -			tpe_acl[low] ^= tpe_acl[i];
    -			tpe_acl[i] ^= tpe_acl[low];
    -			tpe_acl[low] ^= tpe_acl[i];
    -		}	
    -	}
    -
    -	for (i = low + 2; i <= high; i++) {
    -		j = i;
    -		n = tpe_acl[i];
    -		while (n < tpe_acl[j - 1]) {
    -			tpe_acl[j] = tpe_acl[j - 1];
    -			j--;
    -		}
    -		tpe_acl[j] = n;
    -	}
    -}
    -
     /*  Locate a uid in the list */
     static inline int tpe_search (uid_t candidate)
     { 
    @@ -203,4 +101,12 @@
     	return NACK;
     }
     
    +/*  Verify a candidate user. */
    +static inline int tpe_verify (uid_t candidate)
    +{
    +	if ((tpe_search(candidate)) != NACK) {	
    +		return (ACK);
    +	}
    +	return (NACK);
    +}
     #endif  /* __TPE_H */
    _______________________________________________
    linux-security-module mailing list
    linux-security-moduleat_private
    http://mail.wirex.com/mailman/listinfo/linux-security-module
    



    This archive was generated by hypermail 2b30 : Wed Jun 11 2003 - 17:22:35 PDT