// SPDX-License-Identifier: GPL-2.0-only1/*2* Network interface table.3*4* Network interfaces (devices) do not have a security field, so we5* maintain a table associating each interface with a SID.6*7* Author: James Morris <[email protected]>8*9* Copyright (C) 2003 Red Hat, Inc., James Morris <[email protected]>10* Copyright (C) 2007 Hewlett-Packard Development Company, L.P.11* Paul Moore <[email protected]>12*/13#include <linux/init.h>14#include <linux/types.h>15#include <linux/slab.h>16#include <linux/stddef.h>17#include <linux/kernel.h>18#include <linux/list.h>19#include <linux/notifier.h>20#include <linux/netdevice.h>21#include <linux/rcupdate.h>22#include <net/net_namespace.h>2324#include "initcalls.h"25#include "security.h"26#include "objsec.h"27#include "netif.h"2829#define SEL_NETIF_HASH_SIZE 6430#define SEL_NETIF_HASH_MAX 10243132struct sel_netif {33struct list_head list;34struct netif_security_struct nsec;35struct rcu_head rcu_head;36};3738static u32 sel_netif_total;39static DEFINE_SPINLOCK(sel_netif_lock);40static struct list_head sel_netif_hash[SEL_NETIF_HASH_SIZE];4142/**43* sel_netif_hashfn - Hashing function for the interface table44* @ns: the network namespace45* @ifindex: the network interface46*47* Description:48* This is the hashing function for the network interface table, it returns the49* bucket number for the given interface.50*51*/52static inline u32 sel_netif_hashfn(const struct net *ns, int ifindex)53{54return (((uintptr_t)ns + ifindex) & (SEL_NETIF_HASH_SIZE - 1));55}5657/**58* sel_netif_find - Search for an interface record59* @ns: the network namespace60* @ifindex: the network interface61*62* Description:63* Search the network interface table and return the record matching @ifindex.64* If an entry can not be found in the table return NULL.65*66*/67static inline struct sel_netif *sel_netif_find(const struct net *ns,68int ifindex)69{70u32 idx = sel_netif_hashfn(ns, ifindex);71struct sel_netif *netif;7273list_for_each_entry_rcu(netif, &sel_netif_hash[idx], list)74if (net_eq(netif->nsec.ns, ns) &&75netif->nsec.ifindex == ifindex)76return netif;7778return NULL;79}8081/**82* sel_netif_insert - Insert a new interface into the table83* @netif: the new interface record84*85* Description:86* Add a new interface record to the network interface hash table. Returns87* zero on success, negative values on failure.88*89*/90static int sel_netif_insert(struct sel_netif *netif)91{92u32 idx;9394if (sel_netif_total >= SEL_NETIF_HASH_MAX)95return -ENOSPC;9697idx = sel_netif_hashfn(netif->nsec.ns, netif->nsec.ifindex);98list_add_rcu(&netif->list, &sel_netif_hash[idx]);99sel_netif_total++;100101return 0;102}103104/**105* sel_netif_destroy - Remove an interface record from the table106* @netif: the existing interface record107*108* Description:109* Remove an existing interface record from the network interface table.110*111*/112static void sel_netif_destroy(struct sel_netif *netif)113{114list_del_rcu(&netif->list);115sel_netif_total--;116kfree_rcu(netif, rcu_head);117}118119/**120* sel_netif_sid_slow - Lookup the SID of a network interface using the policy121* @ns: the network namespace122* @ifindex: the network interface123* @sid: interface SID124*125* Description:126* This function determines the SID of a network interface by querying the127* security policy. The result is added to the network interface table to128* speedup future queries. Returns zero on success, negative values on129* failure.130*131*/132static int sel_netif_sid_slow(struct net *ns, int ifindex, u32 *sid)133{134int ret = 0;135struct sel_netif *netif;136struct sel_netif *new;137struct net_device *dev;138139/* NOTE: we always use init's network namespace since we don't140* currently support containers */141142dev = dev_get_by_index(ns, ifindex);143if (unlikely(dev == NULL)) {144pr_warn("SELinux: failure in %s(), invalid network interface (%d)\n",145__func__, ifindex);146return -ENOENT;147}148149spin_lock_bh(&sel_netif_lock);150netif = sel_netif_find(ns, ifindex);151if (netif != NULL) {152*sid = netif->nsec.sid;153goto out;154}155156ret = security_netif_sid(dev->name, sid);157if (ret != 0)158goto out;159160/* If this memory allocation fails still return 0. The SID161* is valid, it just won't be added to the cache.162*/163new = kmalloc(sizeof(*new), GFP_ATOMIC);164if (new) {165new->nsec.ns = ns;166new->nsec.ifindex = ifindex;167new->nsec.sid = *sid;168if (sel_netif_insert(new))169kfree(new);170}171172out:173spin_unlock_bh(&sel_netif_lock);174dev_put(dev);175if (unlikely(ret))176pr_warn("SELinux: failure in %s(), unable to determine network interface label (%d)\n",177__func__, ifindex);178return ret;179}180181/**182* sel_netif_sid - Lookup the SID of a network interface183* @ns: the network namespace184* @ifindex: the network interface185* @sid: interface SID186*187* Description:188* This function determines the SID of a network interface using the fastest189* method possible. First the interface table is queried, but if an entry190* can't be found then the policy is queried and the result is added to the191* table to speedup future queries. Returns zero on success, negative values192* on failure.193*194*/195int sel_netif_sid(struct net *ns, int ifindex, u32 *sid)196{197struct sel_netif *netif;198199rcu_read_lock();200netif = sel_netif_find(ns, ifindex);201if (likely(netif != NULL)) {202*sid = netif->nsec.sid;203rcu_read_unlock();204return 0;205}206rcu_read_unlock();207208return sel_netif_sid_slow(ns, ifindex, sid);209}210211/**212* sel_netif_kill - Remove an entry from the network interface table213* @ns: the network namespace214* @ifindex: the network interface215*216* Description:217* This function removes the entry matching @ifindex from the network interface218* table if it exists.219*220*/221static void sel_netif_kill(const struct net *ns, int ifindex)222{223struct sel_netif *netif;224225rcu_read_lock();226spin_lock_bh(&sel_netif_lock);227netif = sel_netif_find(ns, ifindex);228if (netif)229sel_netif_destroy(netif);230spin_unlock_bh(&sel_netif_lock);231rcu_read_unlock();232}233234/**235* sel_netif_flush - Flush the entire network interface table236*237* Description:238* Remove all entries from the network interface table.239*240*/241void sel_netif_flush(void)242{243int idx;244struct sel_netif *netif;245246spin_lock_bh(&sel_netif_lock);247for (idx = 0; idx < SEL_NETIF_HASH_SIZE; idx++)248list_for_each_entry(netif, &sel_netif_hash[idx], list)249sel_netif_destroy(netif);250spin_unlock_bh(&sel_netif_lock);251}252253static int sel_netif_netdev_notifier_handler(struct notifier_block *this,254unsigned long event, void *ptr)255{256struct net_device *dev = netdev_notifier_info_to_dev(ptr);257258if (event == NETDEV_DOWN)259sel_netif_kill(dev_net(dev), dev->ifindex);260261return NOTIFY_DONE;262}263264static struct notifier_block sel_netif_netdev_notifier = {265.notifier_call = sel_netif_netdev_notifier_handler,266};267268int __init sel_netif_init(void)269{270int i;271272if (!selinux_enabled_boot)273return 0;274275for (i = 0; i < SEL_NETIF_HASH_SIZE; i++)276INIT_LIST_HEAD(&sel_netif_hash[i]);277278register_netdevice_notifier(&sel_netif_netdev_notifier);279280return 0;281}282283284285