1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#include <linux/types.h>
31#include <linux/rcupdate.h>
32#include <linux/list.h>
33#include <linux/slab.h>
34#include <linux/spinlock.h>
35#include <linux/in.h>
36#include <linux/in6.h>
37#include <linux/ip.h>
38#include <linux/ipv6.h>
39#include <net/ip.h>
40#include <net/ipv6.h>
41
42#include "netport.h"
43#include "objsec.h"
44
45#define SEL_NETPORT_HASH_SIZE 256
46#define SEL_NETPORT_HASH_BKT_LIMIT 16
47
48struct sel_netport_bkt {
49 int size;
50 struct list_head list;
51};
52
53struct sel_netport {
54 struct netport_security_struct psec;
55
56 struct list_head list;
57 struct rcu_head rcu;
58};
59
60
61
62
63
64
65
66static LIST_HEAD(sel_netport_list);
67static DEFINE_SPINLOCK(sel_netport_lock);
68static struct sel_netport_bkt sel_netport_hash[SEL_NETPORT_HASH_SIZE];
69
70
71
72
73
74
75
76
77
78
79static unsigned int sel_netport_hashfn(u16 pnum)
80{
81 return (pnum & (SEL_NETPORT_HASH_SIZE - 1));
82}
83
84
85
86
87
88
89
90
91
92
93
94static struct sel_netport *sel_netport_find(u8 protocol, u16 pnum)
95{
96 unsigned int idx;
97 struct sel_netport *port;
98
99 idx = sel_netport_hashfn(pnum);
100 list_for_each_entry_rcu(port, &sel_netport_hash[idx].list, list)
101 if (port->psec.port == pnum && port->psec.protocol == protocol)
102 return port;
103
104 return NULL;
105}
106
107
108
109
110
111
112
113
114
115static void sel_netport_insert(struct sel_netport *port)
116{
117 unsigned int idx;
118
119
120
121 idx = sel_netport_hashfn(port->psec.port);
122 list_add_rcu(&port->list, &sel_netport_hash[idx].list);
123 if (sel_netport_hash[idx].size == SEL_NETPORT_HASH_BKT_LIMIT) {
124 struct sel_netport *tail;
125 tail = list_entry(
126 rcu_dereference_protected(
127 sel_netport_hash[idx].list.prev,
128 lockdep_is_held(&sel_netport_lock)),
129 struct sel_netport, list);
130 list_del_rcu(&tail->list);
131 kfree_rcu(tail, rcu);
132 } else
133 sel_netport_hash[idx].size++;
134}
135
136
137
138
139
140
141
142
143
144
145
146
147
148static int sel_netport_sid_slow(u8 protocol, u16 pnum, u32 *sid)
149{
150 int ret = -ENOMEM;
151 struct sel_netport *port;
152 struct sel_netport *new = NULL;
153
154 spin_lock_bh(&sel_netport_lock);
155 port = sel_netport_find(protocol, pnum);
156 if (port != NULL) {
157 *sid = port->psec.sid;
158 spin_unlock_bh(&sel_netport_lock);
159 return 0;
160 }
161 new = kzalloc(sizeof(*new), GFP_ATOMIC);
162 if (new == NULL)
163 goto out;
164 ret = security_port_sid(protocol, pnum, sid);
165 if (ret != 0)
166 goto out;
167
168 new->psec.port = pnum;
169 new->psec.protocol = protocol;
170 new->psec.sid = *sid;
171 sel_netport_insert(new);
172
173out:
174 spin_unlock_bh(&sel_netport_lock);
175 if (unlikely(ret)) {
176 printk(KERN_WARNING
177 "SELinux: failure in sel_netport_sid_slow(),"
178 " unable to determine network port label\n");
179 kfree(new);
180 }
181 return ret;
182}
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197int sel_netport_sid(u8 protocol, u16 pnum, u32 *sid)
198{
199 struct sel_netport *port;
200
201 rcu_read_lock();
202 port = sel_netport_find(protocol, pnum);
203 if (port != NULL) {
204 *sid = port->psec.sid;
205 rcu_read_unlock();
206 return 0;
207 }
208 rcu_read_unlock();
209
210 return sel_netport_sid_slow(protocol, pnum, sid);
211}
212
213
214
215
216
217
218
219
220static void sel_netport_flush(void)
221{
222 unsigned int idx;
223 struct sel_netport *port, *port_tmp;
224
225 spin_lock_bh(&sel_netport_lock);
226 for (idx = 0; idx < SEL_NETPORT_HASH_SIZE; idx++) {
227 list_for_each_entry_safe(port, port_tmp,
228 &sel_netport_hash[idx].list, list) {
229 list_del_rcu(&port->list);
230 kfree_rcu(port, rcu);
231 }
232 sel_netport_hash[idx].size = 0;
233 }
234 spin_unlock_bh(&sel_netport_lock);
235}
236
237static int sel_netport_avc_callback(u32 event)
238{
239 if (event == AVC_CALLBACK_RESET) {
240 sel_netport_flush();
241 synchronize_net();
242 }
243 return 0;
244}
245
246static __init int sel_netport_init(void)
247{
248 int iter;
249 int ret;
250
251 if (!selinux_enabled)
252 return 0;
253
254 for (iter = 0; iter < SEL_NETPORT_HASH_SIZE; iter++) {
255 INIT_LIST_HEAD(&sel_netport_hash[iter].list);
256 sel_netport_hash[iter].size = 0;
257 }
258
259 ret = avc_add_callback(sel_netport_avc_callback, AVC_CALLBACK_RESET);
260 if (ret != 0)
261 panic("avc_add_callback() failed, error %d\n", ret);
262
263 return ret;
264}
265
266__initcall(sel_netport_init);
267