blob: d3762ea24957ea1157ad8c2f7828c53c74bfff4c [file] [log] [blame]
Paul Moored15c3452006-08-03 16:48:37 -07001/*
2 * NetLabel Kernel API
3 *
4 * This file defines the kernel API for the NetLabel system. The NetLabel
5 * system manages static and dynamic label mappings for network protocols such
6 * as CIPSO and RIPSO.
7 *
8 * Author: Paul Moore <paul.moore@hp.com>
9 *
10 */
11
12/*
13 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
23 * the GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 *
29 */
30
31#include <linux/init.h>
32#include <linux/types.h>
33#include <net/ip.h>
34#include <net/netlabel.h>
35#include <net/cipso_ipv4.h>
36#include <asm/bug.h>
Paul Moorec783f1c2008-01-29 08:37:52 -050037#include <asm/atomic.h>
Paul Moored15c3452006-08-03 16:48:37 -070038
39#include "netlabel_domainhash.h"
40#include "netlabel_unlabeled.h"
41#include "netlabel_user.h"
Paul Moore23bcdc12007-07-18 12:28:45 -040042#include "netlabel_mgmt.h"
Paul Moored15c3452006-08-03 16:48:37 -070043
44/*
Paul Moore02752762006-11-29 13:18:18 -050045 * Security Attribute Functions
46 */
47
48/**
49 * netlbl_secattr_catmap_walk - Walk a LSM secattr catmap looking for a bit
50 * @catmap: the category bitmap
51 * @offset: the offset to start searching at, in bits
52 *
53 * Description:
54 * This function walks a LSM secattr category bitmap starting at @offset and
55 * returns the spot of the first set bit or -ENOENT if no bits are set.
56 *
57 */
58int netlbl_secattr_catmap_walk(struct netlbl_lsm_secattr_catmap *catmap,
59 u32 offset)
60{
61 struct netlbl_lsm_secattr_catmap *iter = catmap;
62 u32 node_idx;
63 u32 node_bit;
64 NETLBL_CATMAP_MAPTYPE bitmap;
65
66 if (offset > iter->startbit) {
67 while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
68 iter = iter->next;
69 if (iter == NULL)
70 return -ENOENT;
71 }
72 node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
73 node_bit = offset - iter->startbit -
74 (NETLBL_CATMAP_MAPSIZE * node_idx);
75 } else {
76 node_idx = 0;
77 node_bit = 0;
78 }
79 bitmap = iter->bitmap[node_idx] >> node_bit;
80
81 for (;;) {
82 if (bitmap != 0) {
83 while ((bitmap & NETLBL_CATMAP_BIT) == 0) {
84 bitmap >>= 1;
85 node_bit++;
86 }
87 return iter->startbit +
88 (NETLBL_CATMAP_MAPSIZE * node_idx) + node_bit;
89 }
90 if (++node_idx >= NETLBL_CATMAP_MAPCNT) {
91 if (iter->next != NULL) {
92 iter = iter->next;
93 node_idx = 0;
94 } else
95 return -ENOENT;
96 }
97 bitmap = iter->bitmap[node_idx];
98 node_bit = 0;
99 }
100
101 return -ENOENT;
102}
103
104/**
105 * netlbl_secattr_catmap_walk_rng - Find the end of a string of set bits
106 * @catmap: the category bitmap
107 * @offset: the offset to start searching at, in bits
108 *
109 * Description:
110 * This function walks a LSM secattr category bitmap starting at @offset and
111 * returns the spot of the first cleared bit or -ENOENT if the offset is past
112 * the end of the bitmap.
113 *
114 */
115int netlbl_secattr_catmap_walk_rng(struct netlbl_lsm_secattr_catmap *catmap,
116 u32 offset)
117{
118 struct netlbl_lsm_secattr_catmap *iter = catmap;
119 u32 node_idx;
120 u32 node_bit;
121 NETLBL_CATMAP_MAPTYPE bitmask;
122 NETLBL_CATMAP_MAPTYPE bitmap;
123
124 if (offset > iter->startbit) {
125 while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
126 iter = iter->next;
127 if (iter == NULL)
128 return -ENOENT;
129 }
130 node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
131 node_bit = offset - iter->startbit -
132 (NETLBL_CATMAP_MAPSIZE * node_idx);
133 } else {
134 node_idx = 0;
135 node_bit = 0;
136 }
137 bitmask = NETLBL_CATMAP_BIT << node_bit;
138
139 for (;;) {
140 bitmap = iter->bitmap[node_idx];
141 while (bitmask != 0 && (bitmap & bitmask) != 0) {
142 bitmask <<= 1;
143 node_bit++;
144 }
145
146 if (bitmask != 0)
147 return iter->startbit +
148 (NETLBL_CATMAP_MAPSIZE * node_idx) +
149 node_bit - 1;
150 else if (++node_idx >= NETLBL_CATMAP_MAPCNT) {
151 if (iter->next == NULL)
152 return iter->startbit + NETLBL_CATMAP_SIZE - 1;
153 iter = iter->next;
154 node_idx = 0;
155 }
156 bitmask = NETLBL_CATMAP_BIT;
157 node_bit = 0;
158 }
159
160 return -ENOENT;
161}
162
163/**
164 * netlbl_secattr_catmap_setbit - Set a bit in a LSM secattr catmap
165 * @catmap: the category bitmap
166 * @bit: the bit to set
167 * @flags: memory allocation flags
168 *
169 * Description:
170 * Set the bit specified by @bit in @catmap. Returns zero on success,
171 * negative values on failure.
172 *
173 */
174int netlbl_secattr_catmap_setbit(struct netlbl_lsm_secattr_catmap *catmap,
175 u32 bit,
176 gfp_t flags)
177{
178 struct netlbl_lsm_secattr_catmap *iter = catmap;
179 u32 node_bit;
180 u32 node_idx;
181
182 while (iter->next != NULL &&
183 bit >= (iter->startbit + NETLBL_CATMAP_SIZE))
184 iter = iter->next;
185 if (bit >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
186 iter->next = netlbl_secattr_catmap_alloc(flags);
187 if (iter->next == NULL)
188 return -ENOMEM;
189 iter = iter->next;
190 iter->startbit = bit & ~(NETLBL_CATMAP_SIZE - 1);
191 }
192
193 /* gcc always rounds to zero when doing integer division */
194 node_idx = (bit - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
195 node_bit = bit - iter->startbit - (NETLBL_CATMAP_MAPSIZE * node_idx);
196 iter->bitmap[node_idx] |= NETLBL_CATMAP_BIT << node_bit;
197
198 return 0;
199}
200
201/**
202 * netlbl_secattr_catmap_setrng - Set a range of bits in a LSM secattr catmap
203 * @catmap: the category bitmap
204 * @start: the starting bit
205 * @end: the last bit in the string
206 * @flags: memory allocation flags
207 *
208 * Description:
209 * Set a range of bits, starting at @start and ending with @end. Returns zero
210 * on success, negative values on failure.
211 *
212 */
213int netlbl_secattr_catmap_setrng(struct netlbl_lsm_secattr_catmap *catmap,
214 u32 start,
215 u32 end,
216 gfp_t flags)
217{
218 int ret_val = 0;
219 struct netlbl_lsm_secattr_catmap *iter = catmap;
220 u32 iter_max_spot;
221 u32 spot;
222
223 /* XXX - This could probably be made a bit faster by combining writes
224 * to the catmap instead of setting a single bit each time, but for
225 * right now skipping to the start of the range in the catmap should
226 * be a nice improvement over calling the individual setbit function
227 * repeatedly from a loop. */
228
229 while (iter->next != NULL &&
230 start >= (iter->startbit + NETLBL_CATMAP_SIZE))
231 iter = iter->next;
232 iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE;
233
234 for (spot = start; spot <= end && ret_val == 0; spot++) {
235 if (spot >= iter_max_spot && iter->next != NULL) {
236 iter = iter->next;
237 iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE;
238 }
239 ret_val = netlbl_secattr_catmap_setbit(iter, spot, GFP_ATOMIC);
240 }
241
242 return ret_val;
243}
244
245/*
Paul Moored15c3452006-08-03 16:48:37 -0700246 * LSM Functions
247 */
248
249/**
Paul Moore23bcdc12007-07-18 12:28:45 -0400250 * netlbl_enabled - Determine if the NetLabel subsystem is enabled
251 *
252 * Description:
253 * The LSM can use this function to determine if it should use NetLabel
254 * security attributes in it's enforcement mechanism. Currently, NetLabel is
255 * considered to be enabled when it's configuration contains a valid setup for
256 * at least one labeled protocol (i.e. NetLabel can understand incoming
257 * labeled packets of at least one type); otherwise NetLabel is considered to
258 * be disabled.
259 *
260 */
261int netlbl_enabled(void)
262{
263 /* At some point we probably want to expose this mechanism to the user
264 * as well so that admins can toggle NetLabel regardless of the
265 * configuration */
Paul Moorec783f1c2008-01-29 08:37:52 -0500266 return (atomic_read(&netlabel_mgmt_protocount) > 0);
Paul Moore23bcdc12007-07-18 12:28:45 -0400267}
268
269/**
Paul Moored15c3452006-08-03 16:48:37 -0700270 * netlbl_socket_setattr - Label a socket using the correct protocol
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700271 * @sk: the socket to label
Paul Moored15c3452006-08-03 16:48:37 -0700272 * @secattr: the security attributes
273 *
274 * Description:
275 * Attach the correct label to the given socket using the security attributes
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700276 * specified in @secattr. This function requires exclusive access to @sk,
277 * which means it either needs to be in the process of being created or locked.
278 * Returns zero on success, negative values on failure.
Paul Moored15c3452006-08-03 16:48:37 -0700279 *
280 */
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700281int netlbl_sock_setattr(struct sock *sk,
282 const struct netlbl_lsm_secattr *secattr)
Paul Moored15c3452006-08-03 16:48:37 -0700283{
284 int ret_val = -ENOENT;
285 struct netlbl_dom_map *dom_entry;
286
287 rcu_read_lock();
288 dom_entry = netlbl_domhsh_getentry(secattr->domain);
289 if (dom_entry == NULL)
290 goto socket_setattr_return;
291 switch (dom_entry->type) {
292 case NETLBL_NLTYPE_CIPSOV4:
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700293 ret_val = cipso_v4_sock_setattr(sk,
294 dom_entry->type_def.cipsov4,
295 secattr);
Paul Moored15c3452006-08-03 16:48:37 -0700296 break;
297 case NETLBL_NLTYPE_UNLABELED:
298 ret_val = 0;
299 break;
300 default:
301 ret_val = -ENOENT;
302 }
303
304socket_setattr_return:
305 rcu_read_unlock();
306 return ret_val;
307}
308
309/**
Paul Moore14a72f52006-09-25 15:52:01 -0700310 * netlbl_sock_getattr - Determine the security attributes of a sock
311 * @sk: the sock
312 * @secattr: the security attributes
313 *
314 * Description:
315 * Examines the given sock to see any NetLabel style labeling has been
316 * applied to the sock, if so it parses the socket label and returns the
317 * security attributes in @secattr. Returns zero on success, negative values
318 * on failure.
319 *
320 */
321int netlbl_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
322{
323 int ret_val;
324
325 ret_val = cipso_v4_sock_getattr(sk, secattr);
326 if (ret_val == 0)
327 return 0;
328
329 return netlbl_unlabel_getattr(secattr);
330}
331
332/**
Paul Moored15c3452006-08-03 16:48:37 -0700333 * netlbl_skbuff_getattr - Determine the security attributes of a packet
334 * @skb: the packet
335 * @secattr: the security attributes
336 *
337 * Description:
338 * Examines the given packet to see if a recognized form of packet labeling
339 * is present, if so it parses the packet label and returns the security
340 * attributes in @secattr. Returns zero on success, negative values on
341 * failure.
342 *
343 */
344int netlbl_skbuff_getattr(const struct sk_buff *skb,
345 struct netlbl_lsm_secattr *secattr)
346{
Paul Moore05e00cb2006-11-17 17:38:47 -0500347 if (CIPSO_V4_OPTEXIST(skb) &&
348 cipso_v4_skbuff_getattr(skb, secattr) == 0)
Paul Moored15c3452006-08-03 16:48:37 -0700349 return 0;
350
351 return netlbl_unlabel_getattr(secattr);
352}
353
354/**
355 * netlbl_skbuff_err - Handle a LSM error on a sk_buff
356 * @skb: the packet
357 * @error: the error code
358 *
359 * Description:
360 * Deal with a LSM problem when handling the packet in @skb, typically this is
361 * a permission denied problem (-EACCES). The correct action is determined
362 * according to the packet's labeling protocol.
363 *
364 */
365void netlbl_skbuff_err(struct sk_buff *skb, int error)
366{
367 if (CIPSO_V4_OPTEXIST(skb))
368 cipso_v4_error(skb, error, 0);
369}
370
371/**
372 * netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches
373 *
374 * Description:
375 * For all of the NetLabel protocols that support some form of label mapping
376 * cache, invalidate the cache. Returns zero on success, negative values on
377 * error.
378 *
379 */
380void netlbl_cache_invalidate(void)
381{
382 cipso_v4_cache_invalidate();
383}
384
385/**
386 * netlbl_cache_add - Add an entry to a NetLabel protocol cache
387 * @skb: the packet
388 * @secattr: the packet's security attributes
389 *
390 * Description:
391 * Add the LSM security attributes for the given packet to the underlying
392 * NetLabel protocol's label mapping cache. Returns zero on success, negative
393 * values on error.
394 *
395 */
396int netlbl_cache_add(const struct sk_buff *skb,
397 const struct netlbl_lsm_secattr *secattr)
398{
Paul Moore701a90b2006-11-17 17:38:46 -0500399 if ((secattr->flags & NETLBL_SECATTR_CACHE) == 0)
Paul Moored15c3452006-08-03 16:48:37 -0700400 return -ENOMSG;
401
402 if (CIPSO_V4_OPTEXIST(skb))
403 return cipso_v4_cache_add(skb, secattr);
404
405 return -ENOMSG;
406}
407
408/*
409 * Setup Functions
410 */
411
412/**
413 * netlbl_init - Initialize NetLabel
414 *
415 * Description:
416 * Perform the required NetLabel initialization before first use.
417 *
418 */
419static int __init netlbl_init(void)
420{
421 int ret_val;
422
423 printk(KERN_INFO "NetLabel: Initializing\n");
424 printk(KERN_INFO "NetLabel: domain hash size = %u\n",
425 (1 << NETLBL_DOMHSH_BITSIZE));
426 printk(KERN_INFO "NetLabel: protocols ="
427 " UNLABELED"
428 " CIPSOv4"
429 "\n");
430
431 ret_val = netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE);
432 if (ret_val != 0)
433 goto init_failure;
434
435 ret_val = netlbl_netlink_init();
436 if (ret_val != 0)
437 goto init_failure;
438
439 ret_val = netlbl_unlabel_defconf();
440 if (ret_val != 0)
441 goto init_failure;
442 printk(KERN_INFO "NetLabel: unlabeled traffic allowed by default\n");
443
444 return 0;
445
446init_failure:
447 panic("NetLabel: failed to initialize properly (%d)\n", ret_val);
448}
449
450subsys_initcall(netlbl_init);