123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- #include <linux/types.h>
- #include <linux/rcupdate.h>
- #include <linux/list.h>
- #include <linux/slab.h>
- #include <linux/spinlock.h>
- #include <linux/in.h>
- #include <linux/in6.h>
- #include <linux/ip.h>
- #include <linux/ipv6.h>
- #include <net/ip.h>
- #include <net/ipv6.h>
- #include "netnode.h"
- #include "objsec.h"
- #define SEL_NETNODE_HASH_SIZE 256
- #define SEL_NETNODE_HASH_BKT_LIMIT 16
- struct sel_netnode_bkt {
- unsigned int size;
- struct list_head list;
- };
- struct sel_netnode {
- struct netnode_security_struct nsec;
- struct list_head list;
- struct rcu_head rcu;
- };
- static LIST_HEAD(sel_netnode_list);
- static DEFINE_SPINLOCK(sel_netnode_lock);
- static struct sel_netnode_bkt sel_netnode_hash[SEL_NETNODE_HASH_SIZE];
- static unsigned int sel_netnode_hashfn_ipv4(__be32 addr)
- {
-
- return (addr & (SEL_NETNODE_HASH_SIZE - 1));
- }
- static unsigned int sel_netnode_hashfn_ipv6(const struct in6_addr *addr)
- {
-
- return (addr->s6_addr32[3] & (SEL_NETNODE_HASH_SIZE - 1));
- }
- static struct sel_netnode *sel_netnode_find(const void *addr, u16 family)
- {
- unsigned int idx;
- struct sel_netnode *node;
- switch (family) {
- case PF_INET:
- idx = sel_netnode_hashfn_ipv4(*(__be32 *)addr);
- break;
- case PF_INET6:
- idx = sel_netnode_hashfn_ipv6(addr);
- break;
- default:
- BUG();
- return NULL;
- }
- list_for_each_entry_rcu(node, &sel_netnode_hash[idx].list, list)
- if (node->nsec.family == family)
- switch (family) {
- case PF_INET:
- if (node->nsec.addr.ipv4 == *(__be32 *)addr)
- return node;
- break;
- case PF_INET6:
- if (ipv6_addr_equal(&node->nsec.addr.ipv6,
- addr))
- return node;
- break;
- }
- return NULL;
- }
- static void sel_netnode_insert(struct sel_netnode *node)
- {
- unsigned int idx;
- switch (node->nsec.family) {
- case PF_INET:
- idx = sel_netnode_hashfn_ipv4(node->nsec.addr.ipv4);
- break;
- case PF_INET6:
- idx = sel_netnode_hashfn_ipv6(&node->nsec.addr.ipv6);
- break;
- default:
- BUG();
- return;
- }
-
- list_add_rcu(&node->list, &sel_netnode_hash[idx].list);
- if (sel_netnode_hash[idx].size == SEL_NETNODE_HASH_BKT_LIMIT) {
- struct sel_netnode *tail;
- tail = list_entry(
- rcu_dereference_protected(sel_netnode_hash[idx].list.prev,
- lockdep_is_held(&sel_netnode_lock)),
- struct sel_netnode, list);
- list_del_rcu(&tail->list);
- kfree_rcu(tail, rcu);
- } else
- sel_netnode_hash[idx].size++;
- }
- static int sel_netnode_sid_slow(void *addr, u16 family, u32 *sid)
- {
- int ret = -ENOMEM;
- struct sel_netnode *node;
- struct sel_netnode *new = NULL;
- spin_lock_bh(&sel_netnode_lock);
- node = sel_netnode_find(addr, family);
- if (node != NULL) {
- *sid = node->nsec.sid;
- spin_unlock_bh(&sel_netnode_lock);
- return 0;
- }
- new = kzalloc(sizeof(*new), GFP_ATOMIC);
- if (new == NULL)
- goto out;
- switch (family) {
- case PF_INET:
- ret = security_node_sid(PF_INET,
- addr, sizeof(struct in_addr), sid);
- new->nsec.addr.ipv4 = *(__be32 *)addr;
- break;
- case PF_INET6:
- ret = security_node_sid(PF_INET6,
- addr, sizeof(struct in6_addr), sid);
- new->nsec.addr.ipv6 = *(struct in6_addr *)addr;
- break;
- default:
- BUG();
- ret = -EINVAL;
- }
- if (ret != 0)
- goto out;
- new->nsec.family = family;
- new->nsec.sid = *sid;
- sel_netnode_insert(new);
- out:
- spin_unlock_bh(&sel_netnode_lock);
- if (unlikely(ret)) {
- printk(KERN_WARNING
- "SELinux: failure in sel_netnode_sid_slow(),"
- " unable to determine network node label\n");
- kfree(new);
- }
- return ret;
- }
- int sel_netnode_sid(void *addr, u16 family, u32 *sid)
- {
- struct sel_netnode *node;
- rcu_read_lock();
- node = sel_netnode_find(addr, family);
- if (node != NULL) {
- *sid = node->nsec.sid;
- rcu_read_unlock();
- return 0;
- }
- rcu_read_unlock();
- return sel_netnode_sid_slow(addr, family, sid);
- }
- void sel_netnode_flush(void)
- {
- unsigned int idx;
- struct sel_netnode *node, *node_tmp;
- spin_lock_bh(&sel_netnode_lock);
- for (idx = 0; idx < SEL_NETNODE_HASH_SIZE; idx++) {
- list_for_each_entry_safe(node, node_tmp,
- &sel_netnode_hash[idx].list, list) {
- list_del_rcu(&node->list);
- kfree_rcu(node, rcu);
- }
- sel_netnode_hash[idx].size = 0;
- }
- spin_unlock_bh(&sel_netnode_lock);
- }
- static __init int sel_netnode_init(void)
- {
- int iter;
- if (!selinux_enabled)
- return 0;
- for (iter = 0; iter < SEL_NETNODE_HASH_SIZE; iter++) {
- INIT_LIST_HEAD(&sel_netnode_hash[iter].list);
- sel_netnode_hash[iter].size = 0;
- }
- return 0;
- }
- __initcall(sel_netnode_init);
|