security/selinux/netif.c has a serious problem
PATCH 1/1 security/selinux/netif.c has a serious problem
Hello
I noticed security/selinux/netif.c has a serious problem. It causes kernel-panic when loading the security-policy and networking related programs are executed concurrently. The attached patch (against to 2.6.9-rc3-mm2) fixes the problem.
[Detail]
Pay attention to sel_netif_lookup() and sel_netif_put() in security/selinux/netif.c.sel_netif_lookup() searches on the sel_netif_hash table, and grabs the target 'netif' object. Then, the reference counter of the grabbed object is incremented. sel_netif_put() decrements the reference counter of 'netif' object, then destroys 'netif' object if the reference counter equals zero.
But we can sometimes destroy the same object twice or more, because grabbing the object and incrementing the reference counter are not atomic.
For example:
---- CPU-A ---- ---- CPU-B ----
sel_netif_lookup(){
:
netif = sel_netif_find()
(netif->users=1) sel_netif_flush(){
: :
: sel_netif_put(netif)
: atomic_dec_and_test(&netif->users)
: (netif->users=0)
: +--> sel_netif_destroy(netif)
: (release the netif)atomic_inc(&netif->users);
(netif->users=1)
return netif
}
:
sel_netif_put(netif)
atomic_dec_and_test(&netif->users)
(netif->users=0)
+--> sel_netif_destroy(netif)
(release the same netif twice!)[The attached patch]
This patch fixes the problem.
- It does not depend on the reference counter. RCU-mechanism determines the timing of release.
- If sel_netif_lookup() return success, the context is under rcu_read_lock(). sel_netif_put() is defined as an alias of rcu_read_unlock(). The presence of 'netif' object looked up is guaranteed between those period only.
- Updating the sel_netif_hash table is protected by spinlock_irqsave(). This method is same as my RCU-patch.
Thanks
# And, I wondered if 'sel_netif_total' is really necessary.
--------
Kai Gai <[email protected]>
security/selinux/netif.c +59 -78
@@ -36,11 +36,10 @@ #define DEBUGP(format, args...)#endif static u32 sel_netif_total; -static LIST_HEAD (sel_netif_list); -static spinlock_t sel_netif_lock =SPIN_LOCK_UNLOCKED ; -static struct sel_netif sel_netif_hash[SEL_NETIF_HASH_SIZE ]; +static spinlock_t sel_netif_lock[SEL_NETIF_HASH_SIZE ]; +static struct list_head sel_netif_hash[SEL_NETIF_HASH_SIZE ];static inline u32 sel_netif_hasfn (struct net_device *dev) {return (dev->ifindex & (SEL_NETIF_HASH_SIZE -1 ));
@@ -48,53 +47,39 @@/* * All of the devices should normally fit in the hash, so we optimize * for that case. +* This function must be called under rcu_read_lock(). */ static struct sel_netif *sel_netif_find (struct net_device *dev) { -struct list_head *pos; +struct sel_netif *netif;int idx =sel_netif_hasfn (dev); -__list_for_each_rcu (pos, &sel_netif_hash[idx].list) { -struct sel_netif *netif =list_entry (pos, - struct sel_netif, list); +list_for_each_entry_rcu (netif, &sel_netif_hash[idx], list) {if (likely (netif->nsec.dev == dev))return netif; } -return NULL ; -} -static int sel_netif_insert (struct sel_netif *netif) -{ -int idx, ret =0 ; - -if (sel_netif_total >=SEL_NETIF_HASH_MAX ) { - ret = -ENOSPC ; - goto out; - } - - idx =sel_netif_hasfn (netif->nsec.dev); -list_add_rcu (&netif->list, &sel_netif_hash[idx].list); -atomic_set (&netif->users,1 ); - sel_netif_total++; -out: -return ret; +return NULL ; } +/* +* If sel_netif_lookup() succeed, current is under the rcu_read_lock(). +* rcu_read_unlock() is called by sel_netif_put(). +*/ struct sel_netif *sel_netif_lookup (struct net_device *dev) { -int ret; +int ret, idx; +unsigned long flags;struct sel_netif *netif, *new;struct netif_security_struct *nsec;rcu_read_lock (); netif =sel_netif_find (dev); -rcu_read_unlock (); -if (likely (netif !=NULL )) - goto out_hold; - +return netif; + new =kmalloc (sizeof (*new),GFP_ATOMIC );if (!new) { netif =ERR_PTR (-ENOMEM ); goto out;
@@ -110,28 +95,35 @@ goto out; } nsec->dev = dev; - -spin_lock_bh (&sel_netif_lock); - - netif =sel_netif_find (dev); -if (netif) { -spin_unlock_bh (&sel_netif_lock); + +/* insert new entry */ + idx =sel_netif_hasfn (nsec->dev); +spin_lock_irqsave (&sel_netif_lock[idx], flags); +list_for_each_entry_rcu (netif, &sel_netif_hash[idx], list) { +if (likely (netif->nsec.dev == dev)) { +kfree (new); + goto out_unlock; + } + } + +if (sel_netif_total >=SEL_NETIF_HASH_MAX ) {kfree (new); - goto out_hold; + netif =ERR_PTR (-ENOSPC ); + goto out_unlock; } - -sel_netif_insert (new); -spin_unlock_bh (&sel_netif_lock); - netif = new; - +list_add_rcu (&netif->list, &sel_netif_hash[idx]); + sel_netif_total++; + +out_unlock: +spin_unlock_irqrestore (&sel_netif_lock[idx], flags); +DEBUGP ("new: ifindex=%u name=%s if_sid=%u msg_sid=%u\n" , dev->ifindex, dev->name, nsec->if_sid, nsec->msg_sid); -out_hold: -atomic_inc (&netif->users); out: +rcu_read_unlock ();return netif; }static void sel_netif_free (struct rcu_head *p)
@@ -141,53 +133,39 @@DEBUGP ("%s: %s\n" , __FUNCTION__, netif->nsec.dev->name);kfree (netif); } -static void sel_netif_destroy (struct sel_netif *netif) -{ -DEBUGP ("%s: %s\n" , __FUNCTION__, netif->nsec.dev->name); - -spin_lock_bh (&sel_netif_lock); -list_del_rcu (&netif->list); - sel_netif_total--; -spin_unlock_bh (&sel_netif_lock); - -call_rcu (&netif->rcu_head, sel_netif_free); -} - -void sel_netif_put (struct sel_netif *netif) -{ -if (atomic_dec_and_test (&netif->users)) -sel_netif_destroy (netif); -} -static void sel_netif_kill (struct net_device *dev) {struct sel_netif *netif; - -rcu_read_lock (); - netif =sel_netif_find (dev); -rcu_read_unlock (); +unsigned long flags; +int idx =sel_netif_hasfn (dev); -/* Drop internal reference */ -if (netif) -sel_netif_put (netif); +spin_lock_irqsave (&sel_netif_lock[idx], flags); + netif =sel_netif_find (dev); +if (netif) { +list_del_rcu (&netif->list); +call_rcu (&netif->rcu_head, sel_netif_free); + sel_netif_total--; + } +spin_unlock_irqrestore (&sel_netif_lock[idx], flags); }static void sel_netif_flush (void ) { +unsigned long flags;int idx;for (idx =0 ; idx <SEL_NETIF_HASH_SIZE ; idx++) { -struct list_head *pos; - -list_for_each_rcu (pos, &sel_netif_hash[idx].list) { -struct sel_netif *netif; - - netif = list_entry(pos, struct sel_netif, list); -if (netif) -sel_netif_put (netif); +struct sel_netif *netif; + +spin_lock_irqsave (&sel_netif_lock[idx], flags); +list_for_each_entry_rcu (netif, &sel_netif_hash[idx], list) { +list_del_rcu (&netif->list); +call_rcu (&netif->rcu_head, sel_netif_free); + sel_netif_total--; } +spin_unlock_irqrestore (&sel_netif_lock[idx], flags); } } static int sel_netif_avc_callback(u32 event, u32 ssid, u32 tsid,
@@ -221,10 +199,13 @@if (!selinux_enabled) goto out; -for (i =0 ; i <SEL_NETIF_HASH_SIZE ; i++) -INIT_LIST_HEAD (&sel_netif_hash[i].list); + sel_netif_total =0 ; +for (i =0 ; i <SEL_NETIF_HASH_SIZE ; i++){ +INIT_LIST_HEAD (&sel_netif_hash[i]); +spin_lock_init (&sel_netif_lock[i]); + }register_netdevice_notifier (&sel_netif_netdev_notifier); err = avc_add_callback(sel_netif_avc_callback, AVC_CALLBACK_RESET,
security/selinux/include/netif.h +1 -2
@@ -17,14 +17,13 @@struct sel_netif {struct list_head list; -atomic_t users;struct netif_security_struct nsec;struct rcu_head rcu_head; };struct sel_netif *sel_netif_lookup (struct net_device *dev); -void sel_netif_put (struct sel_netif *netif); +#define sel_netif_put (x) rcu_read_unlock()#endif /* _SELINUX_NETIF_H_ */
Quoted 2 lines · Kaigai Kohei
- Updating the
sel_netif_hashtable is protected byspinlock_irqsave(). This method is same as my RCU-patch.
Why use irqsave? This code is never contended from irq context.
Quoted 1 line
# And, I wondered if '
sel_netif_total' is really necessary.
This prevents the cache from growing without bounds.
- James
Signature · 9 lines
James Morris <[email protected]> -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 1 line · Kaigai Kohei
[The attached patch]
In sel_netif_lookup():
rcu_read_lock();
netif = sel_netif_find(dev);
if (likely(netif != NULL))
return netif;return without unlock. Please use goto.
- James
Signature · 9 lines
James Morris <[email protected]> -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 1 line · James Morris
return without unlock. Please use goto.
Hold on, you want to hold the lock while the user has the netif?
- James
Signature · 9 lines
James Morris <[email protected]> -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 5 lines · James Morris
Quoted 1 line · James Morris
return without unlock. Please use goto.
Hold on, you want to hold the lock while the user has the netif?
Yes, that is necessary to avoid having it freed while it is still being used. Lock is released by the put method; looks sane to me.
Signature · 8 lines
Stephen Smalley <[email protected]> National Security Agency -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 4 lines · Stephen Smalley
Quoted 1 line
Hold on, you want to hold the lock while the user has the netif?
Yes, that is necessary to avoid having it freed while it is still being used. Lock is released by the put method; looks sane to me.
It's bad form for a function to maybe take a lock and keep it (in this case disable preemption, although that could possibly change).
Below is a simpler patch which only allows the netif to be destroyed once.
Please review.
- James
Signature · 56 lines
James Morris <[email protected]> diff -purN -X dontdiff linux-2.6.9-rc3-mm2.o/security/selinux/include/netif.h linux-2.6.9-rc3-mm2.w/security/selinux/include/netif.h --- linux-2.6.9-rc3-mm2.o/security/selinux/include/netif.h 2004-09-29 23:04:46.000000000 -0400 +++ linux-2.6.9-rc3-mm2.w/security/selinux/include/netif.h 2004-10-06 12:22:51.000000000 -0400 @@ -19,6 +19,7 @@ struct sel_netif { struct list_head list; atomic_t users; + atomic_t alive; struct netif_security_struct nsec; struct rcu_head rcu_head; }; diff -purN -X dontdiff linux-2.6.9-rc3-mm2.o/security/selinux/netif.c linux-2.6.9-rc3-mm2.w/security/selinux/netif.c --- linux-2.6.9-rc3-mm2.o/security/selinux/netif.c 2004-09-29 23:04:47.000000000 -0400 +++ linux-2.6.9-rc3-mm2.w/security/selinux/netif.c 2004-10-06 14:48:40.081338584 -0400 @@ -76,6 +76,7 @@ static int sel_netif_insert(struct sel_n idx = sel_netif_hasfn(netif->nsec.dev); list_add_rcu(&netif->list, &sel_netif_hash[idx].list); atomic_set(&netif->users, 1); + atomic_set(&netif->alive, 1); sel_netif_total++; out: return ret; @@ -144,14 +145,16 @@ static void sel_netif_free(struct rcu_he static void sel_netif_destroy(struct sel_netif *netif) { - DEBUGP("%s: %s\n", __FUNCTION__, netif->nsec.dev->name); - - spin_lock_bh(&sel_netif_lock); - list_del_rcu(&netif->list); - sel_netif_total--; - spin_unlock_bh(&sel_netif_lock); + DEBUGP("%s: %s alive=%d\n", __FUNCTION__, netif->nsec.dev->name, + atomic_read(&netif->alive)); - call_rcu(&netif->rcu_head, sel_netif_free); + if (atomic_dec_and_test(&netif->alive)) { + spin_lock_bh(&sel_netif_lock); + list_del_rcu(&netif->list); + sel_netif_total--; + spin_unlock_bh(&sel_netif_lock); + call_rcu(&netif->rcu_head, sel_netif_free); + } } void sel_netif_put(struct sel_netif *netif) -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 6 lines · James Morris
It's bad form for a function to maybe take a lock and keep it (in this case disable preemption, although that could possibly change).
Below is a simpler patch which only allows the netif to be destroyed once.
Please review.
Doesn't this still allow the netif to be destroyed (e.g. upon preemption) while it is still in use by a hook function? Perhaps we need to move the rcu_read_lock/unlock to the hook functions that call sel_netif_lookup?
Signature · 8 lines
Stephen Smalley <[email protected]> National Security Agency -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 2 lines · Stephen Smalley
Doesn't this still allow the netif to be destroyed (e.g. upon preemption) while it is still in use by a hook function?
Yes, if the hook function gets preempted.
Quoted 2 lines
Perhaps we need to move the rcu_read_lock/unlock to the hook functions that call
sel_netif_lookup?
We shouldn't push locking to the caller, there must be another way.
- James
Signature · 9 lines
James Morris <[email protected]> -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 4 lines · James Morris
Quoted 2 lines
Perhaps we need to move the rcu_read_lock/unlock to the hook functions that call
sel_netif_lookup?We shouldn't push locking to the caller, there must be another way.
Actually, that's what some of the core network code is doing. I guess it works.
- James
Signature · 9 lines
James Morris <[email protected]> -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 4 lines · James Morris
Quoted 2 lines
Perhaps we need to move the rcu_read_lock/unlock to the hook functions that call
sel_netif_lookup?We shouldn't push locking to the caller, there must be another way.
Comparing with the AVC, we have avc_has_perm_noaudit() perform the rcu_read_lock/unlock, but it holds the lock until it has copied the result, and merely returns a copy of the decision to the caller, not a reference to a node itself. You could do likewise for sel_netif_lookup and only have it return copies of the SIDs to the caller. The netif AVC entry reference is obsolete anyway with the introduction of RCU for the AVC. No need for any atomic operations on the read path, which is one of the stated purposes of RCU (Documentation/RCU/rcu.txt) - readers need not acquire any locks, perform any atomic instructions, ...
Signature · 8 lines
Stephen Smalley <[email protected]> National Security Agency -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Good morning.
Quoted 4 lines · James Morris
Quoted 2 lines
- Updating the
sel_netif_hashtable is protected byspinlock_irqsave(). This method is same as my RCU-patch.Why use irqsave? This code is never contended from irq context.
Hmm, I worried about using spinlock() without _irqsave(), since I faced to the another problem as you know. Your notice is right.
Quoted 3 lines
Quoted 1 line
# And, I wondered if '
sel_netif_total' is really necessary.This prevents the cache from growing without bounds.
But a result of sel_netif_insert() was not checked in the original code. It's assumed the number of 'netif' is less than SEL_NETIF_HASH_MAX. And, Its upper limitation is the number of the real network interfaces. Why do you think the cache may grow without bounds?
Thanks.
--------
Kai Gai <[email protected]>
--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Good morning.
Thu, Oct 07, 2004 5:12 AM
Quoted 5 lines · Stephen Smalley
Quoted 4 lines
Quoted 2 lines
Perhaps we need to move the rcu_read_lock/unlock to the hook functions that call
sel_netif_lookup?We shouldn't push locking to the caller, there must be another way.
Quoted 9 lines
Comparing with the AVC, we have
avc_has_perm_noaudit()perform the rcu_read_lock/unlock, but it holds the lock until it has copied the result, and merely returns a copy of the decision to the caller, not a reference to a node itself. You could do likewise forsel_netif_lookupand only have it return copies of the SIDs to the caller. The netif AVC entry reference is obsolete anyway with the introduction of RCU for the AVC. No need for any atomic operations on the read path, which is one of the stated purposes of RCU (Documentation/RCU/rcu.txt) - readers need not acquire any locks, perform any atomic instructions, ...
Indeed, I agree it doesn't need to return the reference of 'netif' object. For example, should the function prototype be replaced as follows?
- struct *sel_netif *sel_netif_lookup(struct net_device *dev)
-> int sel_netif_lookup(struct net_device *dev, u32 *if_sid, u32 *msg_sid)
- sel_netif_put() is discontinued.
If it's so, we can hide rcu_read_lock()/unlock() in netif.c.
In my previous patch, caller is under the rcu_read_lock() when sel_netif_lookup() returns success. I don't think it's a significant issue, because it's like XXXX_trylock(). But Stephen's suggestion is better.
Thanks.
--------
Kai Gai <[email protected]>
--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 8 lines · Kaigai Kohei
Indeed, I agree it doesn't need to return the reference of 'netif' object. For example, should the function prototype be replaced as follows?
- struct
*sel_netif*sel_netif_lookup(structnet_device*dev)
-> intsel_netif_lookup(struct net_device *dev, u32 *if_sid, u32 *msg_sid)
-sel_netif_put()is discontinued.If it's so, we can hide rcu_read_lock()/unlock() in
netif.c.
Yes, we can get rid of the refcounting completely, I'm currently testing a new patch.
- James
Signature · 9 lines
James Morris <[email protected]> -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 5 lines · Kaigai Kohei
Quoted 3 lines
Quoted 1 line
# And, I wondered if '
sel_netif_total' is really necessary.This prevents the cache from growing without bounds.
But a result of
sel_netif_insert()was not checked in the original code.
Looks like a bug, thanks for pointing it out.
Quoted 3 lines
It's assumed the number of 'netif' is less than
SEL_NETIF_HASH_MAX. And, Its upper limitation is the number of the real network interfaces. Why do you think the cache may grow without bounds?
It will keep growing until the maximum number of interfaces, which we have no control over in this part of the code.
- James
Signature · 9 lines
James Morris <[email protected]> -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.