1224dfbd8SPaul Moore /* 2224dfbd8SPaul Moore * Network node table 3224dfbd8SPaul Moore * 4224dfbd8SPaul Moore * SELinux must keep a mapping of network nodes to labels/SIDs. This 5224dfbd8SPaul Moore * mapping is maintained as part of the normal policy but a fast cache is 6224dfbd8SPaul Moore * needed to reduce the lookup overhead since most of these queries happen on 7224dfbd8SPaul Moore * a per-packet basis. 8224dfbd8SPaul Moore * 9224dfbd8SPaul Moore * Author: Paul Moore <paul.moore@hp.com> 10224dfbd8SPaul Moore * 11224dfbd8SPaul Moore * This code is heavily based on the "netif" concept originally developed by 12224dfbd8SPaul Moore * James Morris <jmorris@redhat.com> 13224dfbd8SPaul Moore * (see security/selinux/netif.c for more information) 14224dfbd8SPaul Moore * 15224dfbd8SPaul Moore */ 16224dfbd8SPaul Moore 17224dfbd8SPaul Moore /* 18224dfbd8SPaul Moore * (c) Copyright Hewlett-Packard Development Company, L.P., 2007 19224dfbd8SPaul Moore * 20224dfbd8SPaul Moore * This program is free software: you can redistribute it and/or modify 21224dfbd8SPaul Moore * it under the terms of version 2 of the GNU General Public License as 22224dfbd8SPaul Moore * published by the Free Software Foundation. 23224dfbd8SPaul Moore * 24224dfbd8SPaul Moore * This program is distributed in the hope that it will be useful, 25224dfbd8SPaul Moore * but WITHOUT ANY WARRANTY; without even the implied warranty of 26224dfbd8SPaul Moore * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 27224dfbd8SPaul Moore * GNU General Public License for more details. 28224dfbd8SPaul Moore * 29224dfbd8SPaul Moore */ 30224dfbd8SPaul Moore 31224dfbd8SPaul Moore #include <linux/types.h> 32224dfbd8SPaul Moore #include <linux/rcupdate.h> 33224dfbd8SPaul Moore #include <linux/list.h> 345a0e3ad6STejun Heo #include <linux/slab.h> 35224dfbd8SPaul Moore #include <linux/spinlock.h> 36224dfbd8SPaul Moore #include <linux/in.h> 37224dfbd8SPaul Moore #include <linux/in6.h> 38224dfbd8SPaul Moore #include <linux/ip.h> 39224dfbd8SPaul Moore #include <linux/ipv6.h> 40224dfbd8SPaul Moore #include <net/ip.h> 41224dfbd8SPaul Moore #include <net/ipv6.h> 42224dfbd8SPaul Moore 43a639e7caSPaul Moore #include "netnode.h" 44224dfbd8SPaul Moore #include "objsec.h" 45224dfbd8SPaul Moore 46224dfbd8SPaul Moore #define SEL_NETNODE_HASH_SIZE 256 47224dfbd8SPaul Moore #define SEL_NETNODE_HASH_BKT_LIMIT 16 48224dfbd8SPaul Moore 49a639e7caSPaul Moore struct sel_netnode_bkt { 50a639e7caSPaul Moore unsigned int size; 51a639e7caSPaul Moore struct list_head list; 52a639e7caSPaul Moore }; 53a639e7caSPaul Moore 54224dfbd8SPaul Moore struct sel_netnode { 55224dfbd8SPaul Moore struct netnode_security_struct nsec; 56224dfbd8SPaul Moore 57224dfbd8SPaul Moore struct list_head list; 58224dfbd8SPaul Moore struct rcu_head rcu; 59224dfbd8SPaul Moore }; 60224dfbd8SPaul Moore 61224dfbd8SPaul Moore /* NOTE: we are using a combined hash table for both IPv4 and IPv6, the reason 62224dfbd8SPaul Moore * for this is that I suspect most users will not make heavy use of both 63224dfbd8SPaul Moore * address families at the same time so one table will usually end up wasted, 64224dfbd8SPaul Moore * if this becomes a problem we can always add a hash table for each address 65224dfbd8SPaul Moore * family later */ 66224dfbd8SPaul Moore 67224dfbd8SPaul Moore static LIST_HEAD(sel_netnode_list); 68224dfbd8SPaul Moore static DEFINE_SPINLOCK(sel_netnode_lock); 69a639e7caSPaul Moore static struct sel_netnode_bkt sel_netnode_hash[SEL_NETNODE_HASH_SIZE]; 70224dfbd8SPaul Moore 71224dfbd8SPaul Moore /** 72224dfbd8SPaul Moore * sel_netnode_hashfn_ipv4 - IPv4 hashing function for the node table 73224dfbd8SPaul Moore * @addr: IPv4 address 74224dfbd8SPaul Moore * 75224dfbd8SPaul Moore * Description: 76224dfbd8SPaul Moore * This is the IPv4 hashing function for the node interface table, it returns 77224dfbd8SPaul Moore * the bucket number for the given IP address. 78224dfbd8SPaul Moore * 79224dfbd8SPaul Moore */ 80a639e7caSPaul Moore static unsigned int sel_netnode_hashfn_ipv4(__be32 addr) 81224dfbd8SPaul Moore { 82224dfbd8SPaul Moore /* at some point we should determine if the mismatch in byte order 83224dfbd8SPaul Moore * affects the hash function dramatically */ 84224dfbd8SPaul Moore return (addr & (SEL_NETNODE_HASH_SIZE - 1)); 85224dfbd8SPaul Moore } 86224dfbd8SPaul Moore 87224dfbd8SPaul Moore /** 88224dfbd8SPaul Moore * sel_netnode_hashfn_ipv6 - IPv6 hashing function for the node table 89224dfbd8SPaul Moore * @addr: IPv6 address 90224dfbd8SPaul Moore * 91224dfbd8SPaul Moore * Description: 92224dfbd8SPaul Moore * This is the IPv6 hashing function for the node interface table, it returns 93224dfbd8SPaul Moore * the bucket number for the given IP address. 94224dfbd8SPaul Moore * 95224dfbd8SPaul Moore */ 96a639e7caSPaul Moore static unsigned int sel_netnode_hashfn_ipv6(const struct in6_addr *addr) 97224dfbd8SPaul Moore { 98224dfbd8SPaul Moore /* just hash the least significant 32 bits to keep things fast (they 99224dfbd8SPaul Moore * are the most likely to be different anyway), we can revisit this 100224dfbd8SPaul Moore * later if needed */ 101224dfbd8SPaul Moore return (addr->s6_addr32[3] & (SEL_NETNODE_HASH_SIZE - 1)); 102224dfbd8SPaul Moore } 103224dfbd8SPaul Moore 104224dfbd8SPaul Moore /** 105224dfbd8SPaul Moore * sel_netnode_find - Search for a node record 106224dfbd8SPaul Moore * @addr: IP address 107224dfbd8SPaul Moore * @family: address family 108224dfbd8SPaul Moore * 109224dfbd8SPaul Moore * Description: 110224dfbd8SPaul Moore * Search the network node table and return the record matching @addr. If an 111224dfbd8SPaul Moore * entry can not be found in the table return NULL. 112224dfbd8SPaul Moore * 113224dfbd8SPaul Moore */ 114224dfbd8SPaul Moore static struct sel_netnode *sel_netnode_find(const void *addr, u16 family) 115224dfbd8SPaul Moore { 116a639e7caSPaul Moore unsigned int idx; 117224dfbd8SPaul Moore struct sel_netnode *node; 118224dfbd8SPaul Moore 119224dfbd8SPaul Moore switch (family) { 120224dfbd8SPaul Moore case PF_INET: 121224dfbd8SPaul Moore idx = sel_netnode_hashfn_ipv4(*(__be32 *)addr); 122224dfbd8SPaul Moore break; 123224dfbd8SPaul Moore case PF_INET6: 124224dfbd8SPaul Moore idx = sel_netnode_hashfn_ipv6(addr); 125224dfbd8SPaul Moore break; 126224dfbd8SPaul Moore default: 127224dfbd8SPaul Moore BUG(); 128a35c6c83SEric Paris return NULL; 129224dfbd8SPaul Moore } 130224dfbd8SPaul Moore 131a639e7caSPaul Moore list_for_each_entry_rcu(node, &sel_netnode_hash[idx].list, list) 132224dfbd8SPaul Moore if (node->nsec.family == family) 133224dfbd8SPaul Moore switch (family) { 134224dfbd8SPaul Moore case PF_INET: 135224dfbd8SPaul Moore if (node->nsec.addr.ipv4 == *(__be32 *)addr) 136224dfbd8SPaul Moore return node; 137224dfbd8SPaul Moore break; 138224dfbd8SPaul Moore case PF_INET6: 139224dfbd8SPaul Moore if (ipv6_addr_equal(&node->nsec.addr.ipv6, 140224dfbd8SPaul Moore addr)) 141224dfbd8SPaul Moore return node; 142224dfbd8SPaul Moore break; 143224dfbd8SPaul Moore } 144224dfbd8SPaul Moore 145224dfbd8SPaul Moore return NULL; 146224dfbd8SPaul Moore } 147224dfbd8SPaul Moore 148224dfbd8SPaul Moore /** 149224dfbd8SPaul Moore * sel_netnode_insert - Insert a new node into the table 150224dfbd8SPaul Moore * @node: the new node record 151224dfbd8SPaul Moore * 152224dfbd8SPaul Moore * Description: 153a639e7caSPaul Moore * Add a new node record to the network address hash table. 154224dfbd8SPaul Moore * 155224dfbd8SPaul Moore */ 156a639e7caSPaul Moore static void sel_netnode_insert(struct sel_netnode *node) 157224dfbd8SPaul Moore { 158a639e7caSPaul Moore unsigned int idx; 159224dfbd8SPaul Moore 160224dfbd8SPaul Moore switch (node->nsec.family) { 161224dfbd8SPaul Moore case PF_INET: 162224dfbd8SPaul Moore idx = sel_netnode_hashfn_ipv4(node->nsec.addr.ipv4); 163224dfbd8SPaul Moore break; 164224dfbd8SPaul Moore case PF_INET6: 165224dfbd8SPaul Moore idx = sel_netnode_hashfn_ipv6(&node->nsec.addr.ipv6); 166224dfbd8SPaul Moore break; 167224dfbd8SPaul Moore default: 168224dfbd8SPaul Moore BUG(); 169224dfbd8SPaul Moore } 170a639e7caSPaul Moore 171224dfbd8SPaul Moore /* we need to impose a limit on the growth of the hash table so check 172224dfbd8SPaul Moore * this bucket to make sure it is within the specified bounds */ 173a639e7caSPaul Moore list_add_rcu(&node->list, &sel_netnode_hash[idx].list); 174a639e7caSPaul Moore if (sel_netnode_hash[idx].size == SEL_NETNODE_HASH_BKT_LIMIT) { 175a639e7caSPaul Moore struct sel_netnode *tail; 176a639e7caSPaul Moore tail = list_entry( 177a639e7caSPaul Moore rcu_dereference(sel_netnode_hash[idx].list.prev), 178a639e7caSPaul Moore struct sel_netnode, list); 179a639e7caSPaul Moore list_del_rcu(&tail->list); 180*9801c60eSLai Jiangshan kfree_rcu(tail, rcu); 181a639e7caSPaul Moore } else 182a639e7caSPaul Moore sel_netnode_hash[idx].size++; 183224dfbd8SPaul Moore } 184224dfbd8SPaul Moore 185224dfbd8SPaul Moore /** 186224dfbd8SPaul Moore * sel_netnode_sid_slow - Lookup the SID of a network address using the policy 187224dfbd8SPaul Moore * @addr: the IP address 188224dfbd8SPaul Moore * @family: the address family 189224dfbd8SPaul Moore * @sid: node SID 190224dfbd8SPaul Moore * 191224dfbd8SPaul Moore * Description: 192224dfbd8SPaul Moore * This function determines the SID of a network address by quering the 193224dfbd8SPaul Moore * security policy. The result is added to the network address table to 194224dfbd8SPaul Moore * speedup future queries. Returns zero on success, negative values on 195224dfbd8SPaul Moore * failure. 196224dfbd8SPaul Moore * 197224dfbd8SPaul Moore */ 198224dfbd8SPaul Moore static int sel_netnode_sid_slow(void *addr, u16 family, u32 *sid) 199224dfbd8SPaul Moore { 200a639e7caSPaul Moore int ret = -ENOMEM; 201224dfbd8SPaul Moore struct sel_netnode *node; 202224dfbd8SPaul Moore struct sel_netnode *new = NULL; 203224dfbd8SPaul Moore 204224dfbd8SPaul Moore spin_lock_bh(&sel_netnode_lock); 205224dfbd8SPaul Moore node = sel_netnode_find(addr, family); 206224dfbd8SPaul Moore if (node != NULL) { 207224dfbd8SPaul Moore *sid = node->nsec.sid; 208a639e7caSPaul Moore spin_unlock_bh(&sel_netnode_lock); 209a639e7caSPaul Moore return 0; 210224dfbd8SPaul Moore } 211224dfbd8SPaul Moore new = kzalloc(sizeof(*new), GFP_ATOMIC); 212a639e7caSPaul Moore if (new == NULL) 213224dfbd8SPaul Moore goto out; 214224dfbd8SPaul Moore switch (family) { 215224dfbd8SPaul Moore case PF_INET: 216224dfbd8SPaul Moore ret = security_node_sid(PF_INET, 217a639e7caSPaul Moore addr, sizeof(struct in_addr), sid); 218224dfbd8SPaul Moore new->nsec.addr.ipv4 = *(__be32 *)addr; 219224dfbd8SPaul Moore break; 220224dfbd8SPaul Moore case PF_INET6: 221224dfbd8SPaul Moore ret = security_node_sid(PF_INET6, 222a639e7caSPaul Moore addr, sizeof(struct in6_addr), sid); 223224dfbd8SPaul Moore ipv6_addr_copy(&new->nsec.addr.ipv6, addr); 224224dfbd8SPaul Moore break; 225224dfbd8SPaul Moore default: 226224dfbd8SPaul Moore BUG(); 227224dfbd8SPaul Moore } 228224dfbd8SPaul Moore if (ret != 0) 229224dfbd8SPaul Moore goto out; 230a639e7caSPaul Moore 231224dfbd8SPaul Moore new->nsec.family = family; 232a639e7caSPaul Moore new->nsec.sid = *sid; 233a639e7caSPaul Moore sel_netnode_insert(new); 234224dfbd8SPaul Moore 235224dfbd8SPaul Moore out: 236224dfbd8SPaul Moore spin_unlock_bh(&sel_netnode_lock); 23771f1cb05SPaul Moore if (unlikely(ret)) { 23871f1cb05SPaul Moore printk(KERN_WARNING 23971f1cb05SPaul Moore "SELinux: failure in sel_netnode_sid_slow()," 24071f1cb05SPaul Moore " unable to determine network node label\n"); 241224dfbd8SPaul Moore kfree(new); 24271f1cb05SPaul Moore } 243224dfbd8SPaul Moore return ret; 244224dfbd8SPaul Moore } 245224dfbd8SPaul Moore 246224dfbd8SPaul Moore /** 247224dfbd8SPaul Moore * sel_netnode_sid - Lookup the SID of a network address 248224dfbd8SPaul Moore * @addr: the IP address 249224dfbd8SPaul Moore * @family: the address family 250224dfbd8SPaul Moore * @sid: node SID 251224dfbd8SPaul Moore * 252224dfbd8SPaul Moore * Description: 253224dfbd8SPaul Moore * This function determines the SID of a network address using the fastest 254224dfbd8SPaul Moore * method possible. First the address table is queried, but if an entry 255224dfbd8SPaul Moore * can't be found then the policy is queried and the result is added to the 256224dfbd8SPaul Moore * table to speedup future queries. Returns zero on success, negative values 257224dfbd8SPaul Moore * on failure. 258224dfbd8SPaul Moore * 259224dfbd8SPaul Moore */ 260224dfbd8SPaul Moore int sel_netnode_sid(void *addr, u16 family, u32 *sid) 261224dfbd8SPaul Moore { 262224dfbd8SPaul Moore struct sel_netnode *node; 263224dfbd8SPaul Moore 264224dfbd8SPaul Moore rcu_read_lock(); 265224dfbd8SPaul Moore node = sel_netnode_find(addr, family); 266224dfbd8SPaul Moore if (node != NULL) { 267224dfbd8SPaul Moore *sid = node->nsec.sid; 268224dfbd8SPaul Moore rcu_read_unlock(); 269224dfbd8SPaul Moore return 0; 270224dfbd8SPaul Moore } 271224dfbd8SPaul Moore rcu_read_unlock(); 272224dfbd8SPaul Moore 273224dfbd8SPaul Moore return sel_netnode_sid_slow(addr, family, sid); 274224dfbd8SPaul Moore } 275224dfbd8SPaul Moore 276224dfbd8SPaul Moore /** 277224dfbd8SPaul Moore * sel_netnode_flush - Flush the entire network address table 278224dfbd8SPaul Moore * 279224dfbd8SPaul Moore * Description: 280224dfbd8SPaul Moore * Remove all entries from the network address table. 281224dfbd8SPaul Moore * 282224dfbd8SPaul Moore */ 283224dfbd8SPaul Moore static void sel_netnode_flush(void) 284224dfbd8SPaul Moore { 285a639e7caSPaul Moore unsigned int idx; 286a639e7caSPaul Moore struct sel_netnode *node, *node_tmp; 287224dfbd8SPaul Moore 288224dfbd8SPaul Moore spin_lock_bh(&sel_netnode_lock); 289a639e7caSPaul Moore for (idx = 0; idx < SEL_NETNODE_HASH_SIZE; idx++) { 290a639e7caSPaul Moore list_for_each_entry_safe(node, node_tmp, 291a639e7caSPaul Moore &sel_netnode_hash[idx].list, list) { 292a639e7caSPaul Moore list_del_rcu(&node->list); 293*9801c60eSLai Jiangshan kfree_rcu(node, rcu); 294a639e7caSPaul Moore } 295a639e7caSPaul Moore sel_netnode_hash[idx].size = 0; 296a639e7caSPaul Moore } 297224dfbd8SPaul Moore spin_unlock_bh(&sel_netnode_lock); 298224dfbd8SPaul Moore } 299224dfbd8SPaul Moore 300224dfbd8SPaul Moore static int sel_netnode_avc_callback(u32 event, u32 ssid, u32 tsid, 301224dfbd8SPaul Moore u16 class, u32 perms, u32 *retained) 302224dfbd8SPaul Moore { 303224dfbd8SPaul Moore if (event == AVC_CALLBACK_RESET) { 304224dfbd8SPaul Moore sel_netnode_flush(); 305224dfbd8SPaul Moore synchronize_net(); 306224dfbd8SPaul Moore } 307224dfbd8SPaul Moore return 0; 308224dfbd8SPaul Moore } 309224dfbd8SPaul Moore 310224dfbd8SPaul Moore static __init int sel_netnode_init(void) 311224dfbd8SPaul Moore { 312224dfbd8SPaul Moore int iter; 313224dfbd8SPaul Moore int ret; 314224dfbd8SPaul Moore 315224dfbd8SPaul Moore if (!selinux_enabled) 316224dfbd8SPaul Moore return 0; 317224dfbd8SPaul Moore 318a639e7caSPaul Moore for (iter = 0; iter < SEL_NETNODE_HASH_SIZE; iter++) { 319a639e7caSPaul Moore INIT_LIST_HEAD(&sel_netnode_hash[iter].list); 320a639e7caSPaul Moore sel_netnode_hash[iter].size = 0; 321a639e7caSPaul Moore } 322224dfbd8SPaul Moore 323224dfbd8SPaul Moore ret = avc_add_callback(sel_netnode_avc_callback, AVC_CALLBACK_RESET, 324224dfbd8SPaul Moore SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0); 325224dfbd8SPaul Moore if (ret != 0) 326224dfbd8SPaul Moore panic("avc_add_callback() failed, error %d\n", ret); 327224dfbd8SPaul Moore 328224dfbd8SPaul Moore return ret; 329224dfbd8SPaul Moore } 330224dfbd8SPaul Moore 331224dfbd8SPaul Moore __initcall(sel_netnode_init); 332