// SPDX-License-Identifier: GPL-2.0-only /* * Network node table * * SELinux must keep a mapping of network nodes to labels/SIDs. This * mapping is maintained as part of the normal policy but a fast cache is * needed to reduce the lookup overhead since most of these queries happen on * a per-packet basis. * * Author: Paul Moore <paul@paul-moore.com> * * This code is heavily based on the "netif" concept originally developed by * James Morris <jmorris@redhat.com> * (see security/selinux/netif.c for more information)
*/
/* * (c) Copyright Hewlett-Packard Development Company, L.P., 2007
*/
/* NOTE: we are using a combined hash table for both IPv4 and IPv6, the reason * for this is that I suspect most users will not make heavy use of both * address families at the same time so one table will usually end up wasted, * if this becomes a problem we can always add a hash table for each address
* family later */
/** * sel_netnode_hashfn_ipv4 - IPv4 hashing function for the node table * @addr: IPv4 address * * Description: * This is the IPv4 hashing function for the node interface table, it returns * the bucket number for the given IP address. *
*/ staticunsignedint sel_netnode_hashfn_ipv4(__be32 addr)
{ /* at some point we should determine if the mismatch in byte order
* affects the hash function dramatically */ return (addr & (SEL_NETNODE_HASH_SIZE - 1));
}
/** * sel_netnode_hashfn_ipv6 - IPv6 hashing function for the node table * @addr: IPv6 address * * Description: * This is the IPv6 hashing function for the node interface table, it returns * the bucket number for the given IP address. *
*/ staticunsignedint sel_netnode_hashfn_ipv6(conststruct in6_addr *addr)
{ /* just hash the least significant 32 bits to keep things fast (they * are the most likely to be different anyway), we can revisit this
* later if needed */ return (addr->s6_addr32[3] & (SEL_NETNODE_HASH_SIZE - 1));
}
/** * sel_netnode_find - Search for a node record * @addr: IP address * @family: address family * * Description: * Search the network node table and return the record matching @addr. If an * entry can not be found in the table return NULL. *
*/ staticstruct sel_netnode *sel_netnode_find(constvoid *addr, u16 family)
{ unsignedint idx; struct sel_netnode *node;
list_for_each_entry_rcu(node, &sel_netnode_hash[idx].list, list) if (node->nsec.family == family) switch (family) { case PF_INET: if (node->nsec.addr.ipv4 == *(const __be32 *)addr) return node; break; case PF_INET6: if (ipv6_addr_equal(&node->nsec.addr.ipv6,
addr)) return node; break;
}
return NULL;
}
/** * sel_netnode_insert - Insert a new node into the table * @node: the new node record * * Description: * Add a new node record to the network address hash table. *
*/ staticvoid sel_netnode_insert(struct sel_netnode *node)
{ unsignedint idx;
switch (node->nsec.family) { case PF_INET:
idx = sel_netnode_hashfn_ipv4(node->nsec.addr.ipv4); break; case PF_INET6:
idx = sel_netnode_hashfn_ipv6(&node->nsec.addr.ipv6); break; default:
BUG(); return;
}
/* we need to impose a limit on the growth of the hash table so check
* this bucket to make sure it is within the specified bounds */
list_add_rcu(&node->list, &sel_netnode_hash[idx].list); if (sel_netnode_hash[idx].size == SEL_NETNODE_HASH_BKT_LIMIT) { struct sel_netnode *tail;
tail = list_entry(
rcu_dereference_protected(
list_tail_rcu(&sel_netnode_hash[idx].list),
lockdep_is_held(&sel_netnode_lock)), struct sel_netnode, list);
list_del_rcu(&tail->list);
kfree_rcu(tail, rcu);
} else
sel_netnode_hash[idx].size++;
}
/** * sel_netnode_sid_slow - Lookup the SID of a network address using the policy * @addr: the IP address * @family: the address family * @sid: node SID * * Description: * This function determines the SID of a network address by querying the * security policy. The result is added to the network address table to * speedup future queries. Returns zero on success, negative values on * failure. *
*/ staticint sel_netnode_sid_slow(constvoid *addr, u16 family, u32 *sid)
{ int ret; struct sel_netnode *node; struct sel_netnode *new;
/* If this memory allocation fails still return 0. The SID * is valid, it just won't be added to the cache.
*/ new = kmalloc(sizeof(*new), GFP_ATOMIC); switch (family) { case PF_INET:
ret = security_node_sid(PF_INET,
addr, sizeof(struct in_addr), sid); if (new)
new->nsec.addr.ipv4 = *(const __be32 *)addr; break; case PF_INET6:
ret = security_node_sid(PF_INET6,
addr, sizeof(struct in6_addr), sid); if (new)
new->nsec.addr.ipv6 = *(conststruct in6_addr *)addr; break; default:
BUG();
ret = -EINVAL;
} if (ret == 0 && new) {
new->nsec.family = family;
new->nsec.sid = *sid;
sel_netnode_insert(new);
} else
kfree(new);
spin_unlock_bh(&sel_netnode_lock); if (unlikely(ret))
pr_warn("SELinux: failure in %s(), unable to determine network node label\n",
__func__); return ret;
}
/** * sel_netnode_sid - Lookup the SID of a network address * @addr: the IP address * @family: the address family * @sid: node SID * * Description: * This function determines the SID of a network address using the fastest * method possible. First the address table is queried, but if an entry * can't be found then the policy is queried and the result is added to the * table to speedup future queries. Returns zero on success, negative values * on failure. *
*/ int sel_netnode_sid(constvoid *addr, u16 family, u32 *sid)
{ struct sel_netnode *node;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.