/*1* Network node table2*3* SELinux must keep a mapping of network nodes to labels/SIDs. This4* mapping is maintained as part of the normal policy but a fast cache is5* needed to reduce the lookup overhead since most of these queries happen on6* a per-packet basis.7*8* Author: Paul Moore <[email protected]>9*10* This code is heavily based on the "netif" concept originally developed by11* James Morris <[email protected]>12* (see security/selinux/netif.c for more information)13*14*/1516/*17* (c) Copyright Hewlett-Packard Development Company, L.P., 200718*19* This program is free software: you can redistribute it and/or modify20* it under the terms of version 2 of the GNU General Public License as21* published by the Free Software Foundation.22*23* This program is distributed in the hope that it will be useful,24* but WITHOUT ANY WARRANTY; without even the implied warranty of25* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the26* GNU General Public License for more details.27*28*/2930#include <linux/types.h>31#include <linux/rcupdate.h>32#include <linux/list.h>33#include <linux/slab.h>34#include <linux/spinlock.h>35#include <linux/in.h>36#include <linux/in6.h>37#include <linux/ip.h>38#include <linux/ipv6.h>39#include <net/ip.h>40#include <net/ipv6.h>4142#include "netnode.h"43#include "objsec.h"4445#define SEL_NETNODE_HASH_SIZE 25646#define SEL_NETNODE_HASH_BKT_LIMIT 164748struct sel_netnode_bkt {49unsigned int size;50struct list_head list;51};5253struct sel_netnode {54struct netnode_security_struct nsec;5556struct list_head list;57struct rcu_head rcu;58};5960/* NOTE: we are using a combined hash table for both IPv4 and IPv6, the reason61* for this is that I suspect most users will not make heavy use of both62* address families at the same time so one table will usually end up wasted,63* if this becomes a problem we can always add a hash table for each address64* family later */6566static LIST_HEAD(sel_netnode_list);67static DEFINE_SPINLOCK(sel_netnode_lock);68static struct sel_netnode_bkt sel_netnode_hash[SEL_NETNODE_HASH_SIZE];6970/**71* sel_netnode_free - Frees a node entry72* @p: the entry's RCU field73*74* Description:75* This function is designed to be used as a callback to the call_rcu()76* function so that memory allocated to a hash table node entry can be77* released safely.78*79*/80static void sel_netnode_free(struct rcu_head *p)81{82struct sel_netnode *node = container_of(p, struct sel_netnode, rcu);83kfree(node);84}8586/**87* sel_netnode_hashfn_ipv4 - IPv4 hashing function for the node table88* @addr: IPv4 address89*90* Description:91* This is the IPv4 hashing function for the node interface table, it returns92* the bucket number for the given IP address.93*94*/95static unsigned int sel_netnode_hashfn_ipv4(__be32 addr)96{97/* at some point we should determine if the mismatch in byte order98* affects the hash function dramatically */99return (addr & (SEL_NETNODE_HASH_SIZE - 1));100}101102/**103* sel_netnode_hashfn_ipv6 - IPv6 hashing function for the node table104* @addr: IPv6 address105*106* Description:107* This is the IPv6 hashing function for the node interface table, it returns108* the bucket number for the given IP address.109*110*/111static unsigned int sel_netnode_hashfn_ipv6(const struct in6_addr *addr)112{113/* just hash the least significant 32 bits to keep things fast (they114* are the most likely to be different anyway), we can revisit this115* later if needed */116return (addr->s6_addr32[3] & (SEL_NETNODE_HASH_SIZE - 1));117}118119/**120* sel_netnode_find - Search for a node record121* @addr: IP address122* @family: address family123*124* Description:125* Search the network node table and return the record matching @addr. If an126* entry can not be found in the table return NULL.127*128*/129static struct sel_netnode *sel_netnode_find(const void *addr, u16 family)130{131unsigned int idx;132struct sel_netnode *node;133134switch (family) {135case PF_INET:136idx = sel_netnode_hashfn_ipv4(*(__be32 *)addr);137break;138case PF_INET6:139idx = sel_netnode_hashfn_ipv6(addr);140break;141default:142BUG();143return NULL;144}145146list_for_each_entry_rcu(node, &sel_netnode_hash[idx].list, list)147if (node->nsec.family == family)148switch (family) {149case PF_INET:150if (node->nsec.addr.ipv4 == *(__be32 *)addr)151return node;152break;153case PF_INET6:154if (ipv6_addr_equal(&node->nsec.addr.ipv6,155addr))156return node;157break;158}159160return NULL;161}162163/**164* sel_netnode_insert - Insert a new node into the table165* @node: the new node record166*167* Description:168* Add a new node record to the network address hash table.169*170*/171static void sel_netnode_insert(struct sel_netnode *node)172{173unsigned int idx;174175switch (node->nsec.family) {176case PF_INET:177idx = sel_netnode_hashfn_ipv4(node->nsec.addr.ipv4);178break;179case PF_INET6:180idx = sel_netnode_hashfn_ipv6(&node->nsec.addr.ipv6);181break;182default:183BUG();184}185186/* we need to impose a limit on the growth of the hash table so check187* this bucket to make sure it is within the specified bounds */188list_add_rcu(&node->list, &sel_netnode_hash[idx].list);189if (sel_netnode_hash[idx].size == SEL_NETNODE_HASH_BKT_LIMIT) {190struct sel_netnode *tail;191tail = list_entry(192rcu_dereference(sel_netnode_hash[idx].list.prev),193struct sel_netnode, list);194list_del_rcu(&tail->list);195call_rcu(&tail->rcu, sel_netnode_free);196} else197sel_netnode_hash[idx].size++;198}199200/**201* sel_netnode_sid_slow - Lookup the SID of a network address using the policy202* @addr: the IP address203* @family: the address family204* @sid: node SID205*206* Description:207* This function determines the SID of a network address by quering the208* security policy. The result is added to the network address table to209* speedup future queries. Returns zero on success, negative values on210* failure.211*212*/213static int sel_netnode_sid_slow(void *addr, u16 family, u32 *sid)214{215int ret = -ENOMEM;216struct sel_netnode *node;217struct sel_netnode *new = NULL;218219spin_lock_bh(&sel_netnode_lock);220node = sel_netnode_find(addr, family);221if (node != NULL) {222*sid = node->nsec.sid;223spin_unlock_bh(&sel_netnode_lock);224return 0;225}226new = kzalloc(sizeof(*new), GFP_ATOMIC);227if (new == NULL)228goto out;229switch (family) {230case PF_INET:231ret = security_node_sid(PF_INET,232addr, sizeof(struct in_addr), sid);233new->nsec.addr.ipv4 = *(__be32 *)addr;234break;235case PF_INET6:236ret = security_node_sid(PF_INET6,237addr, sizeof(struct in6_addr), sid);238ipv6_addr_copy(&new->nsec.addr.ipv6, addr);239break;240default:241BUG();242}243if (ret != 0)244goto out;245246new->nsec.family = family;247new->nsec.sid = *sid;248sel_netnode_insert(new);249250out:251spin_unlock_bh(&sel_netnode_lock);252if (unlikely(ret)) {253printk(KERN_WARNING254"SELinux: failure in sel_netnode_sid_slow(),"255" unable to determine network node label\n");256kfree(new);257}258return ret;259}260261/**262* sel_netnode_sid - Lookup the SID of a network address263* @addr: the IP address264* @family: the address family265* @sid: node SID266*267* Description:268* This function determines the SID of a network address using the fastest269* method possible. First the address table is queried, but if an entry270* can't be found then the policy is queried and the result is added to the271* table to speedup future queries. Returns zero on success, negative values272* on failure.273*274*/275int sel_netnode_sid(void *addr, u16 family, u32 *sid)276{277struct sel_netnode *node;278279rcu_read_lock();280node = sel_netnode_find(addr, family);281if (node != NULL) {282*sid = node->nsec.sid;283rcu_read_unlock();284return 0;285}286rcu_read_unlock();287288return sel_netnode_sid_slow(addr, family, sid);289}290291/**292* sel_netnode_flush - Flush the entire network address table293*294* Description:295* Remove all entries from the network address table.296*297*/298static void sel_netnode_flush(void)299{300unsigned int idx;301struct sel_netnode *node, *node_tmp;302303spin_lock_bh(&sel_netnode_lock);304for (idx = 0; idx < SEL_NETNODE_HASH_SIZE; idx++) {305list_for_each_entry_safe(node, node_tmp,306&sel_netnode_hash[idx].list, list) {307list_del_rcu(&node->list);308call_rcu(&node->rcu, sel_netnode_free);309}310sel_netnode_hash[idx].size = 0;311}312spin_unlock_bh(&sel_netnode_lock);313}314315static int sel_netnode_avc_callback(u32 event, u32 ssid, u32 tsid,316u16 class, u32 perms, u32 *retained)317{318if (event == AVC_CALLBACK_RESET) {319sel_netnode_flush();320synchronize_net();321}322return 0;323}324325static __init int sel_netnode_init(void)326{327int iter;328int ret;329330if (!selinux_enabled)331return 0;332333for (iter = 0; iter < SEL_NETNODE_HASH_SIZE; iter++) {334INIT_LIST_HEAD(&sel_netnode_hash[iter].list);335sel_netnode_hash[iter].size = 0;336}337338ret = avc_add_callback(sel_netnode_avc_callback, AVC_CALLBACK_RESET,339SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);340if (ret != 0)341panic("avc_add_callback() failed, error %d\n", ret);342343return ret;344}345346__initcall(sel_netnode_init);347348349