^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Network node table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * SELinux must keep a mapping of network nodes to labels/SIDs. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * mapping is maintained as part of the normal policy but a fast cache is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * needed to reduce the lookup overhead since most of these queries happen on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * a per-packet basis.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Author: Paul Moore <paul@paul-moore.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * This code is heavily based on the "netif" concept originally developed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * James Morris <jmorris@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * (see security/selinux/netif.c for more information)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * (c) Copyright Hewlett-Packard Development Company, L.P., 2007
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/rcupdate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/in.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/in6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/ipv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <net/ipv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include "netnode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include "objsec.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define SEL_NETNODE_HASH_SIZE 256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define SEL_NETNODE_HASH_BKT_LIMIT 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct sel_netnode_bkt {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) unsigned int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct sel_netnode {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct netnode_security_struct nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct rcu_head rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /* NOTE: we are using a combined hash table for both IPv4 and IPv6, the reason
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * for this is that I suspect most users will not make heavy use of both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * address families at the same time so one table will usually end up wasted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * if this becomes a problem we can always add a hash table for each address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * family later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static LIST_HEAD(sel_netnode_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static DEFINE_SPINLOCK(sel_netnode_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static struct sel_netnode_bkt sel_netnode_hash[SEL_NETNODE_HASH_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * sel_netnode_hashfn_ipv4 - IPv4 hashing function for the node table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * @addr: IPv4 address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * This is the IPv4 hashing function for the node interface table, it returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * the bucket number for the given IP address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static unsigned int sel_netnode_hashfn_ipv4(__be32 addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) /* at some point we should determine if the mismatch in byte order
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * affects the hash function dramatically */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return (addr & (SEL_NETNODE_HASH_SIZE - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * sel_netnode_hashfn_ipv6 - IPv6 hashing function for the node table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * @addr: IPv6 address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * This is the IPv6 hashing function for the node interface table, it returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * the bucket number for the given IP address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static unsigned int sel_netnode_hashfn_ipv6(const struct in6_addr *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /* just hash the least significant 32 bits to keep things fast (they
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * are the most likely to be different anyway), we can revisit this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * later if needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return (addr->s6_addr32[3] & (SEL_NETNODE_HASH_SIZE - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * sel_netnode_find - Search for a node record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * @addr: IP address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * @family: address family
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * Search the network node table and return the record matching @addr. If an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * entry can not be found in the table return NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static struct sel_netnode *sel_netnode_find(const void *addr, u16 family)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) unsigned int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct sel_netnode *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) switch (family) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) case PF_INET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) idx = sel_netnode_hashfn_ipv4(*(__be32 *)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) case PF_INET6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) idx = sel_netnode_hashfn_ipv6(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) list_for_each_entry_rcu(node, &sel_netnode_hash[idx].list, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (node->nsec.family == family)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) switch (family) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) case PF_INET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (node->nsec.addr.ipv4 == *(__be32 *)addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) case PF_INET6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (ipv6_addr_equal(&node->nsec.addr.ipv6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * sel_netnode_insert - Insert a new node into the table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * @node: the new node record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * Add a new node record to the network address hash table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static void sel_netnode_insert(struct sel_netnode *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) unsigned int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) switch (node->nsec.family) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) case PF_INET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) idx = sel_netnode_hashfn_ipv4(node->nsec.addr.ipv4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) case PF_INET6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) idx = sel_netnode_hashfn_ipv6(&node->nsec.addr.ipv6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /* we need to impose a limit on the growth of the hash table so check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * this bucket to make sure it is within the specified bounds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) list_add_rcu(&node->list, &sel_netnode_hash[idx].list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (sel_netnode_hash[idx].size == SEL_NETNODE_HASH_BKT_LIMIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct sel_netnode *tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) tail = list_entry(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) rcu_dereference_protected(sel_netnode_hash[idx].list.prev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) lockdep_is_held(&sel_netnode_lock)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct sel_netnode, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) list_del_rcu(&tail->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) kfree_rcu(tail, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) sel_netnode_hash[idx].size++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * sel_netnode_sid_slow - Lookup the SID of a network address using the policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * @addr: the IP address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * @family: the address family
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * @sid: node SID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * This function determines the SID of a network address by querying the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * security policy. The result is added to the network address table to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * speedup future queries. Returns zero on success, negative values on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static int sel_netnode_sid_slow(void *addr, u16 family, u32 *sid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) struct sel_netnode *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) struct sel_netnode *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) spin_lock_bh(&sel_netnode_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) node = sel_netnode_find(addr, family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (node != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) *sid = node->nsec.sid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) spin_unlock_bh(&sel_netnode_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) new = kzalloc(sizeof(*new), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) switch (family) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) case PF_INET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) ret = security_node_sid(&selinux_state, PF_INET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) addr, sizeof(struct in_addr), sid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) new->nsec.addr.ipv4 = *(__be32 *)addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) case PF_INET6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) ret = security_node_sid(&selinux_state, PF_INET6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) addr, sizeof(struct in6_addr), sid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) new->nsec.addr.ipv6 = *(struct in6_addr *)addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (ret == 0 && new) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) new->nsec.family = family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) new->nsec.sid = *sid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) sel_netnode_insert(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) kfree(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) spin_unlock_bh(&sel_netnode_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (unlikely(ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) pr_warn("SELinux: failure in %s(), unable to determine network node label\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) * sel_netnode_sid - Lookup the SID of a network address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * @addr: the IP address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * @family: the address family
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * @sid: node SID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * This function determines the SID of a network address using the fastest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * method possible. First the address table is queried, but if an entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * can't be found then the policy is queried and the result is added to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * table to speedup future queries. Returns zero on success, negative values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) int sel_netnode_sid(void *addr, u16 family, u32 *sid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) struct sel_netnode *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) node = sel_netnode_find(addr, family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (node != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) *sid = node->nsec.sid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return sel_netnode_sid_slow(addr, family, sid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) * sel_netnode_flush - Flush the entire network address table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * Remove all entries from the network address table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) void sel_netnode_flush(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) unsigned int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) struct sel_netnode *node, *node_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) spin_lock_bh(&sel_netnode_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) for (idx = 0; idx < SEL_NETNODE_HASH_SIZE; idx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) list_for_each_entry_safe(node, node_tmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) &sel_netnode_hash[idx].list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) list_del_rcu(&node->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) kfree_rcu(node, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) sel_netnode_hash[idx].size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) spin_unlock_bh(&sel_netnode_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) static __init int sel_netnode_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) int iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (!selinux_enabled_boot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) for (iter = 0; iter < SEL_NETNODE_HASH_SIZE; iter++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) INIT_LIST_HEAD(&sel_netnode_hash[iter].list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) sel_netnode_hash[iter].size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) __initcall(sel_netnode_init);