4 * SELinux must keep a mapping of network ports to labels/SIDs. This
5 * mapping is maintained as part of the normal policy but a fast cache is
6 * needed to reduce the lookup overhead.
8 * Author: Paul Moore <paul.moore@hp.com>
10 * This code is heavily based on the "netif" concept originally developed by
11 * James Morris <jmorris@redhat.com>
12 * (see security/selinux/netif.c for more information)
17 * (c) Copyright Hewlett-Packard Development Company, L.P., 2008
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of version 2 of the GNU General Public License as
21 * published by the Free Software Foundation.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
30 #include <linux/types.h>
31 #include <linux/rcupdate.h>
32 #include <linux/list.h>
33 #include <linux/spinlock.h>
35 #include <linux/in6.h>
37 #include <linux/ipv6.h>
45 #define SEL_NETPORT_HASH_SIZE 256
46 #define SEL_NETPORT_HASH_BKT_LIMIT 16
48 struct sel_netport_bkt
{
50 struct list_head list
;
54 struct netport_security_struct psec
;
56 struct list_head list
;
60 /* NOTE: we are using a combined hash table for both IPv4 and IPv6, the reason
61 * for this is that I suspect most users will not make heavy use of both
62 * address families at the same time so one table will usually end up wasted,
63 * if this becomes a problem we can always add a hash table for each address
66 static LIST_HEAD(sel_netport_list
);
67 static DEFINE_SPINLOCK(sel_netport_lock
);
68 static struct sel_netport_bkt sel_netport_hash
[SEL_NETPORT_HASH_SIZE
];
71 * sel_netport_free - Frees a port entry
72 * @p: the entry's RCU field
75 * This function is designed to be used as a callback to the call_rcu()
76 * function so that memory allocated to a hash table port entry can be
80 static void sel_netport_free(struct rcu_head
*p
)
82 struct sel_netport
*port
= container_of(p
, struct sel_netport
, rcu
);
87 * sel_netport_hashfn - Hashing function for the port table
91 * This is the hashing function for the port table, it returns the bucket
92 * number for the given port.
95 static unsigned int sel_netport_hashfn(u16 pnum
)
97 return (pnum
& (SEL_NETPORT_HASH_SIZE
- 1));
101 * sel_netport_find - Search for a port record
102 * @protocol: protocol
106 * Search the network port table and return the matching record. If an entry
107 * can not be found in the table return NULL.
110 static struct sel_netport
*sel_netport_find(u8 protocol
, u16 pnum
)
113 struct sel_netport
*port
;
115 idx
= sel_netport_hashfn(pnum
);
116 list_for_each_entry_rcu(port
, &sel_netport_hash
[idx
].list
, list
)
117 if (port
->psec
.port
== pnum
&&
118 port
->psec
.protocol
== protocol
)
125 * sel_netport_insert - Insert a new port into the table
126 * @port: the new port record
129 * Add a new port record to the network address hash table. Returns zero on
130 * success, negative values on failure.
133 static int sel_netport_insert(struct sel_netport
*port
)
137 /* we need to impose a limit on the growth of the hash table so check
138 * this bucket to make sure it is within the specified bounds */
139 idx
= sel_netport_hashfn(port
->psec
.port
);
140 list_add_rcu(&port
->list
, &sel_netport_hash
[idx
].list
);
141 if (sel_netport_hash
[idx
].size
== SEL_NETPORT_HASH_BKT_LIMIT
) {
142 struct sel_netport
*tail
;
143 tail
= list_entry(port
->list
.prev
, struct sel_netport
, list
);
144 list_del_rcu(port
->list
.prev
);
145 call_rcu(&tail
->rcu
, sel_netport_free
);
147 sel_netport_hash
[idx
].size
++;
153 * sel_netport_sid_slow - Lookup the SID of a network address using the policy
154 * @protocol: protocol
159 * This function determines the SID of a network port by quering the security
160 * policy. The result is added to the network port table to speedup future
161 * queries. Returns zero on success, negative values on failure.
164 static int sel_netport_sid_slow(u8 protocol
, u16 pnum
, u32
*sid
)
167 struct sel_netport
*port
;
168 struct sel_netport
*new = NULL
;
170 spin_lock_bh(&sel_netport_lock
);
171 port
= sel_netport_find(protocol
, pnum
);
173 *sid
= port
->psec
.sid
;
177 new = kzalloc(sizeof(*new), GFP_ATOMIC
);
182 ret
= security_port_sid(protocol
, pnum
, &new->psec
.sid
);
185 new->psec
.port
= pnum
;
186 new->psec
.protocol
= protocol
;
187 ret
= sel_netport_insert(new);
190 *sid
= new->psec
.sid
;
193 spin_unlock_bh(&sel_netport_lock
);
196 "SELinux: failure in sel_netport_sid_slow(),"
197 " unable to determine network port label\n");
204 * sel_netport_sid - Lookup the SID of a network port
205 * @protocol: protocol
210 * This function determines the SID of a network port using the fastest method
211 * possible. First the port table is queried, but if an entry can't be found
212 * then the policy is queried and the result is added to the table to speedup
213 * future queries. Returns zero on success, negative values on failure.
216 int sel_netport_sid(u8 protocol
, u16 pnum
, u32
*sid
)
218 struct sel_netport
*port
;
221 port
= sel_netport_find(protocol
, pnum
);
223 *sid
= port
->psec
.sid
;
229 return sel_netport_sid_slow(protocol
, pnum
, sid
);
233 * sel_netport_flush - Flush the entire network port table
236 * Remove all entries from the network address table.
239 static void sel_netport_flush(void)
242 struct sel_netport
*port
;
244 spin_lock_bh(&sel_netport_lock
);
245 for (idx
= 0; idx
< SEL_NETPORT_HASH_SIZE
; idx
++) {
246 list_for_each_entry(port
, &sel_netport_hash
[idx
].list
, list
) {
247 list_del_rcu(&port
->list
);
248 call_rcu(&port
->rcu
, sel_netport_free
);
250 sel_netport_hash
[idx
].size
= 0;
252 spin_unlock_bh(&sel_netport_lock
);
255 static int sel_netport_avc_callback(u32 event
, u32 ssid
, u32 tsid
,
256 u16
class, u32 perms
, u32
*retained
)
258 if (event
== AVC_CALLBACK_RESET
) {
265 static __init
int sel_netport_init(void)
270 if (!selinux_enabled
)
273 for (iter
= 0; iter
< SEL_NETPORT_HASH_SIZE
; iter
++) {
274 INIT_LIST_HEAD(&sel_netport_hash
[iter
].list
);
275 sel_netport_hash
[iter
].size
= 0;
278 ret
= avc_add_callback(sel_netport_avc_callback
, AVC_CALLBACK_RESET
,
279 SECSID_NULL
, SECSID_NULL
, SECCLASS_NULL
, 0);
281 panic("avc_add_callback() failed, error %d\n", ret
);
286 __initcall(sel_netport_init
);