···11+/*22+ * Network port table33+ *44+ * SELinux must keep a mapping of network ports to labels/SIDs. This55+ * mapping is maintained as part of the normal policy but a fast cache is66+ * needed to reduce the lookup overhead.77+ *88+ * Author: Paul Moore <paul.moore@hp.com>99+ *1010+ */1111+1212+/*1313+ * (c) Copyright Hewlett-Packard Development Company, L.P., 20081414+ *1515+ * This program is free software: you can redistribute it and/or modify1616+ * it under the terms of version 2 of the GNU General Public License as1717+ * published by the Free Software Foundation.1818+ *1919+ * This program is distributed in the hope that it will be useful,2020+ * but WITHOUT ANY WARRANTY; without even the implied warranty of2121+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the2222+ * GNU General Public License for more details.2323+ *2424+ */2525+2626+#ifndef _SELINUX_NETPORT_H2727+#define _SELINUX_NETPORT_H2828+2929+int sel_netport_sid(u8 protocol, u16 pnum, u32 *sid);3030+3131+#endif
+286
security/selinux/netport.c
···11+/*22+ * Network port table33+ *44+ * SELinux must keep a mapping of network ports to labels/SIDs. This55+ * mapping is maintained as part of the normal policy but a fast cache is66+ * needed to reduce the lookup overhead.77+ *88+ * Author: Paul Moore <paul.moore@hp.com>99+ *1010+ * This code is heavily based on the "netif" concept originally developed by1111+ * James Morris <jmorris@redhat.com>1212+ * (see security/selinux/netif.c for more information)1313+ *1414+ */1515+1616+/*1717+ * (c) Copyright Hewlett-Packard Development Company, L.P., 20081818+ *1919+ * This program is free software: you can redistribute it and/or modify2020+ * it under the terms of version 2 of the GNU General Public License as2121+ * published by the Free Software Foundation.2222+ *2323+ * This program is distributed in the hope that it will be useful,2424+ * but WITHOUT ANY WARRANTY; without even the implied warranty of2525+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the2626+ * GNU General Public License for more details.2727+ *2828+ */2929+3030+#include <linux/types.h>3131+#include <linux/rcupdate.h>3232+#include <linux/list.h>3333+#include <linux/spinlock.h>3434+#include <linux/in.h>3535+#include <linux/in6.h>3636+#include <linux/ip.h>3737+#include <linux/ipv6.h>3838+#include <net/ip.h>3939+#include <net/ipv6.h>4040+#include <asm/bug.h>4141+4242+#include "netport.h"4343+#include "objsec.h"4444+4545+#define SEL_NETPORT_HASH_SIZE 2564646+#define SEL_NETPORT_HASH_BKT_LIMIT 164747+4848+struct sel_netport_bkt {4949+ int size;5050+ struct list_head list;5151+};5252+5353+struct sel_netport {5454+ struct netport_security_struct psec;5555+5656+ struct list_head list;5757+ struct rcu_head rcu;5858+};5959+6060+/* NOTE: we are using a combined hash table for both IPv4 and IPv6, the reason6161+ * for this is that I suspect most users will not make heavy use of both6262+ * address families at the same time so one table will usually end up wasted,6363+ * if this becomes a problem we can always add a hash table for each address6464+ * family later */6565+6666+static LIST_HEAD(sel_netport_list);6767+static DEFINE_SPINLOCK(sel_netport_lock);6868+static struct sel_netport_bkt sel_netport_hash[SEL_NETPORT_HASH_SIZE];6969+7070+/**7171+ * sel_netport_free - Frees a port entry7272+ * @p: the entry's RCU field7373+ *7474+ * Description:7575+ * This function is designed to be used as a callback to the call_rcu()7676+ * function so that memory allocated to a hash table port entry can be7777+ * released safely.7878+ *7979+ */8080+static void sel_netport_free(struct rcu_head *p)8181+{8282+ struct sel_netport *port = container_of(p, struct sel_netport, rcu);8383+ kfree(port);8484+}8585+8686+/**8787+ * sel_netport_hashfn - Hashing function for the port table8888+ * @pnum: port number8989+ *9090+ * Description:9191+ * This is the hashing function for the port table, it returns the bucket9292+ * number for the given port.9393+ *9494+ */9595+static unsigned int sel_netport_hashfn(u16 pnum)9696+{9797+ return (pnum & (SEL_NETPORT_HASH_SIZE - 1));9898+}9999+100100+/**101101+ * sel_netport_find - Search for a port record102102+ * @protocol: protocol103103+ * @port: pnum104104+ *105105+ * Description:106106+ * Search the network port table and return the matching record. If an entry107107+ * can not be found in the table return NULL.108108+ *109109+ */110110+static struct sel_netport *sel_netport_find(u8 protocol, u16 pnum)111111+{112112+ unsigned int idx;113113+ struct sel_netport *port;114114+115115+ idx = sel_netport_hashfn(pnum);116116+ list_for_each_entry_rcu(port, &sel_netport_hash[idx].list, list)117117+ if (port->psec.port == pnum &&118118+ port->psec.protocol == protocol)119119+ return port;120120+121121+ return NULL;122122+}123123+124124+/**125125+ * sel_netport_insert - Insert a new port into the table126126+ * @port: the new port record127127+ *128128+ * Description:129129+ * Add a new port record to the network address hash table. Returns zero on130130+ * success, negative values on failure.131131+ *132132+ */133133+static int sel_netport_insert(struct sel_netport *port)134134+{135135+ unsigned int idx;136136+137137+ /* we need to impose a limit on the growth of the hash table so check138138+ * this bucket to make sure it is within the specified bounds */139139+ idx = sel_netport_hashfn(port->psec.port);140140+ list_add_rcu(&port->list, &sel_netport_hash[idx].list);141141+ if (sel_netport_hash[idx].size == SEL_NETPORT_HASH_BKT_LIMIT) {142142+ struct sel_netport *tail;143143+ tail = list_entry(port->list.prev, struct sel_netport, list);144144+ list_del_rcu(port->list.prev);145145+ call_rcu(&tail->rcu, sel_netport_free);146146+ } else147147+ sel_netport_hash[idx].size++;148148+149149+ return 0;150150+}151151+152152+/**153153+ * sel_netport_sid_slow - Lookup the SID of a network address using the policy154154+ * @protocol: protocol155155+ * @pnum: port156156+ * @sid: port SID157157+ *158158+ * Description:159159+ * This function determines the SID of a network port by quering the security160160+ * policy. The result is added to the network port table to speedup future161161+ * queries. Returns zero on success, negative values on failure.162162+ *163163+ */164164+static int sel_netport_sid_slow(u8 protocol, u16 pnum, u32 *sid)165165+{166166+ int ret;167167+ struct sel_netport *port;168168+ struct sel_netport *new = NULL;169169+170170+ spin_lock_bh(&sel_netport_lock);171171+ port = sel_netport_find(protocol, pnum);172172+ if (port != NULL) {173173+ *sid = port->psec.sid;174174+ ret = 0;175175+ goto out;176176+ }177177+ new = kzalloc(sizeof(*new), GFP_ATOMIC);178178+ if (new == NULL) {179179+ ret = -ENOMEM;180180+ goto out;181181+ }182182+ ret = security_port_sid(protocol, pnum, &new->psec.sid);183183+ if (ret != 0)184184+ goto out;185185+ new->psec.port = pnum;186186+ new->psec.protocol = protocol;187187+ ret = sel_netport_insert(new);188188+ if (ret != 0)189189+ goto out;190190+ *sid = new->psec.sid;191191+192192+out:193193+ spin_unlock_bh(&sel_netport_lock);194194+ if (unlikely(ret)) {195195+ printk(KERN_WARNING196196+ "SELinux: failure in sel_netport_sid_slow(),"197197+ " unable to determine network port label\n");198198+ kfree(new);199199+ }200200+ return ret;201201+}202202+203203+/**204204+ * sel_netport_sid - Lookup the SID of a network port205205+ * @protocol: protocol206206+ * @pnum: port207207+ * @sid: port SID208208+ *209209+ * Description:210210+ * This function determines the SID of a network port using the fastest method211211+ * possible. First the port table is queried, but if an entry can't be found212212+ * then the policy is queried and the result is added to the table to speedup213213+ * future queries. Returns zero on success, negative values on failure.214214+ *215215+ */216216+int sel_netport_sid(u8 protocol, u16 pnum, u32 *sid)217217+{218218+ struct sel_netport *port;219219+220220+ rcu_read_lock();221221+ port = sel_netport_find(protocol, pnum);222222+ if (port != NULL) {223223+ *sid = port->psec.sid;224224+ rcu_read_unlock();225225+ return 0;226226+ }227227+ rcu_read_unlock();228228+229229+ return sel_netport_sid_slow(protocol, pnum, sid);230230+}231231+232232+/**233233+ * sel_netport_flush - Flush the entire network port table234234+ *235235+ * Description:236236+ * Remove all entries from the network address table.237237+ *238238+ */239239+static void sel_netport_flush(void)240240+{241241+ unsigned int idx;242242+ struct sel_netport *port;243243+244244+ spin_lock_bh(&sel_netport_lock);245245+ for (idx = 0; idx < SEL_NETPORT_HASH_SIZE; idx++) {246246+ list_for_each_entry(port, &sel_netport_hash[idx].list, list) {247247+ list_del_rcu(&port->list);248248+ call_rcu(&port->rcu, sel_netport_free);249249+ }250250+ sel_netport_hash[idx].size = 0;251251+ }252252+ spin_unlock_bh(&sel_netport_lock);253253+}254254+255255+static int sel_netport_avc_callback(u32 event, u32 ssid, u32 tsid,256256+ u16 class, u32 perms, u32 *retained)257257+{258258+ if (event == AVC_CALLBACK_RESET) {259259+ sel_netport_flush();260260+ synchronize_net();261261+ }262262+ return 0;263263+}264264+265265+static __init int sel_netport_init(void)266266+{267267+ int iter;268268+ int ret;269269+270270+ if (!selinux_enabled)271271+ return 0;272272+273273+ for (iter = 0; iter < SEL_NETPORT_HASH_SIZE; iter++) {274274+ INIT_LIST_HEAD(&sel_netport_hash[iter].list);275275+ sel_netport_hash[iter].size = 0;276276+ }277277+278278+ ret = avc_add_callback(sel_netport_avc_callback, AVC_CALLBACK_RESET,279279+ SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);280280+ if (ret != 0)281281+ panic("avc_add_callback() failed, error %d\n", ret);282282+283283+ return ret;284284+}285285+286286+__initcall(sel_netport_init);