Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Network interface table.
0004  *
0005  * Network interfaces (devices) do not have a security field, so we
0006  * maintain a table associating each interface with a SID.
0007  *
0008  * Author: James Morris <jmorris@redhat.com>
0009  *
0010  * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
0011  * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
0012  *            Paul Moore <paul@paul-moore.com>
0013  */
0014 #include <linux/init.h>
0015 #include <linux/types.h>
0016 #include <linux/slab.h>
0017 #include <linux/stddef.h>
0018 #include <linux/kernel.h>
0019 #include <linux/list.h>
0020 #include <linux/notifier.h>
0021 #include <linux/netdevice.h>
0022 #include <linux/rcupdate.h>
0023 #include <net/net_namespace.h>
0024 
0025 #include "security.h"
0026 #include "objsec.h"
0027 #include "netif.h"
0028 
0029 #define SEL_NETIF_HASH_SIZE 64
0030 #define SEL_NETIF_HASH_MAX  1024
0031 
0032 struct sel_netif {
0033     struct list_head list;
0034     struct netif_security_struct nsec;
0035     struct rcu_head rcu_head;
0036 };
0037 
0038 static u32 sel_netif_total;
0039 static DEFINE_SPINLOCK(sel_netif_lock);
0040 static struct list_head sel_netif_hash[SEL_NETIF_HASH_SIZE];
0041 
0042 /**
0043  * sel_netif_hashfn - Hashing function for the interface table
0044  * @ns: the network namespace
0045  * @ifindex: the network interface
0046  *
0047  * Description:
0048  * This is the hashing function for the network interface table, it returns the
0049  * bucket number for the given interface.
0050  *
0051  */
0052 static inline u32 sel_netif_hashfn(const struct net *ns, int ifindex)
0053 {
0054     return (((uintptr_t)ns + ifindex) & (SEL_NETIF_HASH_SIZE - 1));
0055 }
0056 
0057 /**
0058  * sel_netif_find - Search for an interface record
0059  * @ns: the network namespace
0060  * @ifindex: the network interface
0061  *
0062  * Description:
0063  * Search the network interface table and return the record matching @ifindex.
0064  * If an entry can not be found in the table return NULL.
0065  *
0066  */
0067 static inline struct sel_netif *sel_netif_find(const struct net *ns,
0068                            int ifindex)
0069 {
0070     int idx = sel_netif_hashfn(ns, ifindex);
0071     struct sel_netif *netif;
0072 
0073     list_for_each_entry_rcu(netif, &sel_netif_hash[idx], list)
0074         if (net_eq(netif->nsec.ns, ns) &&
0075             netif->nsec.ifindex == ifindex)
0076             return netif;
0077 
0078     return NULL;
0079 }
0080 
0081 /**
0082  * sel_netif_insert - Insert a new interface into the table
0083  * @netif: the new interface record
0084  *
0085  * Description:
0086  * Add a new interface record to the network interface hash table.  Returns
0087  * zero on success, negative values on failure.
0088  *
0089  */
0090 static int sel_netif_insert(struct sel_netif *netif)
0091 {
0092     int idx;
0093 
0094     if (sel_netif_total >= SEL_NETIF_HASH_MAX)
0095         return -ENOSPC;
0096 
0097     idx = sel_netif_hashfn(netif->nsec.ns, netif->nsec.ifindex);
0098     list_add_rcu(&netif->list, &sel_netif_hash[idx]);
0099     sel_netif_total++;
0100 
0101     return 0;
0102 }
0103 
0104 /**
0105  * sel_netif_destroy - Remove an interface record from the table
0106  * @netif: the existing interface record
0107  *
0108  * Description:
0109  * Remove an existing interface record from the network interface table.
0110  *
0111  */
0112 static void sel_netif_destroy(struct sel_netif *netif)
0113 {
0114     list_del_rcu(&netif->list);
0115     sel_netif_total--;
0116     kfree_rcu(netif, rcu_head);
0117 }
0118 
0119 /**
0120  * sel_netif_sid_slow - Lookup the SID of a network interface using the policy
0121  * @ns: the network namespace
0122  * @ifindex: the network interface
0123  * @sid: interface SID
0124  *
0125  * Description:
0126  * This function determines the SID of a network interface by querying the
0127  * security policy.  The result is added to the network interface table to
0128  * speedup future queries.  Returns zero on success, negative values on
0129  * failure.
0130  *
0131  */
0132 static int sel_netif_sid_slow(struct net *ns, int ifindex, u32 *sid)
0133 {
0134     int ret = 0;
0135     struct sel_netif *netif;
0136     struct sel_netif *new;
0137     struct net_device *dev;
0138 
0139     /* NOTE: we always use init's network namespace since we don't
0140      * currently support containers */
0141 
0142     dev = dev_get_by_index(ns, ifindex);
0143     if (unlikely(dev == NULL)) {
0144         pr_warn("SELinux: failure in %s(), invalid network interface (%d)\n",
0145             __func__, ifindex);
0146         return -ENOENT;
0147     }
0148 
0149     spin_lock_bh(&sel_netif_lock);
0150     netif = sel_netif_find(ns, ifindex);
0151     if (netif != NULL) {
0152         *sid = netif->nsec.sid;
0153         goto out;
0154     }
0155 
0156     ret = security_netif_sid(&selinux_state, dev->name, sid);
0157     if (ret != 0)
0158         goto out;
0159     new = kzalloc(sizeof(*new), GFP_ATOMIC);
0160     if (new) {
0161         new->nsec.ns = ns;
0162         new->nsec.ifindex = ifindex;
0163         new->nsec.sid = *sid;
0164         if (sel_netif_insert(new))
0165             kfree(new);
0166     }
0167 
0168 out:
0169     spin_unlock_bh(&sel_netif_lock);
0170     dev_put(dev);
0171     if (unlikely(ret))
0172         pr_warn("SELinux: failure in %s(), unable to determine network interface label (%d)\n",
0173             __func__, ifindex);
0174     return ret;
0175 }
0176 
0177 /**
0178  * sel_netif_sid - Lookup the SID of a network interface
0179  * @ns: the network namespace
0180  * @ifindex: the network interface
0181  * @sid: interface SID
0182  *
0183  * Description:
0184  * This function determines the SID of a network interface using the fastest
0185  * method possible.  First the interface table is queried, but if an entry
0186  * can't be found then the policy is queried and the result is added to the
0187  * table to speedup future queries.  Returns zero on success, negative values
0188  * on failure.
0189  *
0190  */
0191 int sel_netif_sid(struct net *ns, int ifindex, u32 *sid)
0192 {
0193     struct sel_netif *netif;
0194 
0195     rcu_read_lock();
0196     netif = sel_netif_find(ns, ifindex);
0197     if (likely(netif != NULL)) {
0198         *sid = netif->nsec.sid;
0199         rcu_read_unlock();
0200         return 0;
0201     }
0202     rcu_read_unlock();
0203 
0204     return sel_netif_sid_slow(ns, ifindex, sid);
0205 }
0206 
0207 /**
0208  * sel_netif_kill - Remove an entry from the network interface table
0209  * @ns: the network namespace
0210  * @ifindex: the network interface
0211  *
0212  * Description:
0213  * This function removes the entry matching @ifindex from the network interface
0214  * table if it exists.
0215  *
0216  */
0217 static void sel_netif_kill(const struct net *ns, int ifindex)
0218 {
0219     struct sel_netif *netif;
0220 
0221     rcu_read_lock();
0222     spin_lock_bh(&sel_netif_lock);
0223     netif = sel_netif_find(ns, ifindex);
0224     if (netif)
0225         sel_netif_destroy(netif);
0226     spin_unlock_bh(&sel_netif_lock);
0227     rcu_read_unlock();
0228 }
0229 
0230 /**
0231  * sel_netif_flush - Flush the entire network interface table
0232  *
0233  * Description:
0234  * Remove all entries from the network interface table.
0235  *
0236  */
0237 void sel_netif_flush(void)
0238 {
0239     int idx;
0240     struct sel_netif *netif;
0241 
0242     spin_lock_bh(&sel_netif_lock);
0243     for (idx = 0; idx < SEL_NETIF_HASH_SIZE; idx++)
0244         list_for_each_entry(netif, &sel_netif_hash[idx], list)
0245             sel_netif_destroy(netif);
0246     spin_unlock_bh(&sel_netif_lock);
0247 }
0248 
0249 static int sel_netif_netdev_notifier_handler(struct notifier_block *this,
0250                          unsigned long event, void *ptr)
0251 {
0252     struct net_device *dev = netdev_notifier_info_to_dev(ptr);
0253 
0254     if (event == NETDEV_DOWN)
0255         sel_netif_kill(dev_net(dev), dev->ifindex);
0256 
0257     return NOTIFY_DONE;
0258 }
0259 
0260 static struct notifier_block sel_netif_netdev_notifier = {
0261     .notifier_call = sel_netif_netdev_notifier_handler,
0262 };
0263 
0264 static __init int sel_netif_init(void)
0265 {
0266     int i;
0267 
0268     if (!selinux_enabled_boot)
0269         return 0;
0270 
0271     for (i = 0; i < SEL_NETIF_HASH_SIZE; i++)
0272         INIT_LIST_HEAD(&sel_netif_hash[i]);
0273 
0274     register_netdevice_notifier(&sel_netif_netdev_notifier);
0275 
0276     return 0;
0277 }
0278 
0279 __initcall(sel_netif_init);
0280