netlabel: Add network address selectors to the NetLabel/LSM domain mapping
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / netlabel / netlabel_kapi.c
CommitLineData
d15c345f
PM
1/*
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>
eda61d32 33#include <linux/audit.h>
d15c345f
PM
34#include <net/ip.h>
35#include <net/netlabel.h>
36#include <net/cipso_ipv4.h>
37#include <asm/bug.h>
c783f1ce 38#include <asm/atomic.h>
d15c345f
PM
39
40#include "netlabel_domainhash.h"
41#include "netlabel_unlabeled.h"
eda61d32 42#include "netlabel_cipso_v4.h"
d15c345f 43#include "netlabel_user.h"
23bcdc1a 44#include "netlabel_mgmt.h"
d15c345f 45
eda61d32
PM
46/*
47 * Configuration Functions
48 */
49
50/**
51 * netlbl_cfg_map_del - Remove a NetLabel/LSM domain mapping
52 * @domain: the domain mapping to remove
53 * @audit_info: NetLabel audit information
54 *
55 * Description:
56 * Removes a NetLabel/LSM domain mapping. A @domain value of NULL causes the
57 * default domain mapping to be removed. Returns zero on success, negative
58 * values on failure.
59 *
60 */
61int netlbl_cfg_map_del(const char *domain, struct netlbl_audit *audit_info)
62{
63 return netlbl_domhsh_remove(domain, audit_info);
64}
65
66/**
67 * netlbl_cfg_unlbl_add_map - Add an unlabeled NetLabel/LSM domain mapping
68 * @domain: the domain mapping to add
69 * @audit_info: NetLabel audit information
70 *
71 * Description:
72 * Adds a new unlabeled NetLabel/LSM domain mapping. A @domain value of NULL
73 * causes a new default domain mapping to be added. Returns zero on success,
74 * negative values on failure.
75 *
76 */
77int netlbl_cfg_unlbl_add_map(const char *domain,
78 struct netlbl_audit *audit_info)
79{
80 int ret_val = -ENOMEM;
81 struct netlbl_dom_map *entry;
82
83 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
84 if (entry == NULL)
948a7243 85 return -ENOMEM;
eda61d32
PM
86 if (domain != NULL) {
87 entry->domain = kstrdup(domain, GFP_ATOMIC);
88 if (entry->domain == NULL)
89 goto cfg_unlbl_add_map_failure;
90 }
91 entry->type = NETLBL_NLTYPE_UNLABELED;
92
93 ret_val = netlbl_domhsh_add(entry, audit_info);
94 if (ret_val != 0)
95 goto cfg_unlbl_add_map_failure;
96
97 return 0;
98
99cfg_unlbl_add_map_failure:
100 if (entry != NULL)
101 kfree(entry->domain);
102 kfree(entry);
103 return ret_val;
104}
105
eda61d32
PM
106/**
107 * netlbl_cfg_cipsov4_add_map - Add a new CIPSOv4 DOI definition and mapping
108 * @doi_def: the DOI definition
109 * @domain: the domain mapping to add
110 * @audit_info: NetLabel audit information
111 *
112 * Description:
113 * Add a new CIPSOv4 DOI definition and NetLabel/LSM domain mapping for this
114 * new DOI definition to the NetLabel subsystem. A @domain value of NULL adds
115 * a new default domain mapping. Returns zero on success, negative values on
116 * failure.
117 *
118 */
119int netlbl_cfg_cipsov4_add_map(struct cipso_v4_doi *doi_def,
120 const char *domain,
121 struct netlbl_audit *audit_info)
122{
123 int ret_val = -ENOMEM;
b1edeb10
PM
124 u32 doi;
125 u32 doi_type;
eda61d32 126 struct netlbl_dom_map *entry;
948a7243
PM
127 const char *type_str;
128 struct audit_buffer *audit_buf;
eda61d32 129
b1edeb10
PM
130 doi = doi_def->doi;
131 doi_type = doi_def->type;
132
eda61d32
PM
133 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
134 if (entry == NULL)
948a7243 135 return -ENOMEM;
eda61d32
PM
136 if (domain != NULL) {
137 entry->domain = kstrdup(domain, GFP_ATOMIC);
138 if (entry->domain == NULL)
139 goto cfg_cipsov4_add_map_failure;
140 }
eda61d32 141
948a7243 142 ret_val = cipso_v4_doi_add(doi_def);
eda61d32 143 if (ret_val != 0)
b1edeb10
PM
144 goto cfg_cipsov4_add_map_failure_remove_doi;
145 entry->type = NETLBL_NLTYPE_CIPSOV4;
146 entry->type_def.cipsov4 = cipso_v4_doi_getdef(doi);
147 if (entry->type_def.cipsov4 == NULL) {
148 ret_val = -ENOENT;
149 goto cfg_cipsov4_add_map_failure_remove_doi;
150 }
eda61d32
PM
151 ret_val = netlbl_domhsh_add(entry, audit_info);
152 if (ret_val != 0)
b1edeb10 153 goto cfg_cipsov4_add_map_failure_release_doi;
eda61d32 154
b1edeb10 155cfg_cipsov4_add_map_return:
948a7243
PM
156 audit_buf = netlbl_audit_start_common(AUDIT_MAC_CIPSOV4_ADD,
157 audit_info);
158 if (audit_buf != NULL) {
b1edeb10 159 switch (doi_type) {
948a7243
PM
160 case CIPSO_V4_MAP_STD:
161 type_str = "std";
162 break;
163 case CIPSO_V4_MAP_PASS:
164 type_str = "pass";
165 break;
166 default:
167 type_str = "(unknown)";
168 }
169 audit_log_format(audit_buf,
170 " cipso_doi=%u cipso_type=%s res=%u",
b1edeb10 171 doi, type_str, ret_val == 0 ? 1 : 0);
948a7243
PM
172 audit_log_end(audit_buf);
173 }
b1edeb10
PM
174
175 return ret_val;
176
177cfg_cipsov4_add_map_failure_release_doi:
178 cipso_v4_doi_putdef(doi_def);
179cfg_cipsov4_add_map_failure_remove_doi:
180 cipso_v4_doi_remove(doi, audit_info);
eda61d32
PM
181cfg_cipsov4_add_map_failure:
182 if (entry != NULL)
183 kfree(entry->domain);
184 kfree(entry);
b1edeb10 185 goto cfg_cipsov4_add_map_return;
eda61d32
PM
186}
187
02752760
PM
188/*
189 * Security Attribute Functions
190 */
191
192/**
193 * netlbl_secattr_catmap_walk - Walk a LSM secattr catmap looking for a bit
194 * @catmap: the category bitmap
195 * @offset: the offset to start searching at, in bits
196 *
197 * Description:
198 * This function walks a LSM secattr category bitmap starting at @offset and
199 * returns the spot of the first set bit or -ENOENT if no bits are set.
200 *
201 */
202int netlbl_secattr_catmap_walk(struct netlbl_lsm_secattr_catmap *catmap,
203 u32 offset)
204{
205 struct netlbl_lsm_secattr_catmap *iter = catmap;
206 u32 node_idx;
207 u32 node_bit;
208 NETLBL_CATMAP_MAPTYPE bitmap;
209
210 if (offset > iter->startbit) {
211 while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
212 iter = iter->next;
213 if (iter == NULL)
214 return -ENOENT;
215 }
216 node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
217 node_bit = offset - iter->startbit -
218 (NETLBL_CATMAP_MAPSIZE * node_idx);
219 } else {
220 node_idx = 0;
221 node_bit = 0;
222 }
223 bitmap = iter->bitmap[node_idx] >> node_bit;
224
225 for (;;) {
226 if (bitmap != 0) {
227 while ((bitmap & NETLBL_CATMAP_BIT) == 0) {
228 bitmap >>= 1;
229 node_bit++;
230 }
231 return iter->startbit +
232 (NETLBL_CATMAP_MAPSIZE * node_idx) + node_bit;
233 }
234 if (++node_idx >= NETLBL_CATMAP_MAPCNT) {
235 if (iter->next != NULL) {
236 iter = iter->next;
237 node_idx = 0;
238 } else
239 return -ENOENT;
240 }
241 bitmap = iter->bitmap[node_idx];
242 node_bit = 0;
243 }
244
245 return -ENOENT;
246}
247
248/**
249 * netlbl_secattr_catmap_walk_rng - Find the end of a string of set bits
250 * @catmap: the category bitmap
251 * @offset: the offset to start searching at, in bits
252 *
253 * Description:
254 * This function walks a LSM secattr category bitmap starting at @offset and
255 * returns the spot of the first cleared bit or -ENOENT if the offset is past
256 * the end of the bitmap.
257 *
258 */
259int netlbl_secattr_catmap_walk_rng(struct netlbl_lsm_secattr_catmap *catmap,
260 u32 offset)
261{
262 struct netlbl_lsm_secattr_catmap *iter = catmap;
263 u32 node_idx;
264 u32 node_bit;
265 NETLBL_CATMAP_MAPTYPE bitmask;
266 NETLBL_CATMAP_MAPTYPE bitmap;
267
268 if (offset > iter->startbit) {
269 while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
270 iter = iter->next;
271 if (iter == NULL)
272 return -ENOENT;
273 }
274 node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
275 node_bit = offset - iter->startbit -
276 (NETLBL_CATMAP_MAPSIZE * node_idx);
277 } else {
278 node_idx = 0;
279 node_bit = 0;
280 }
281 bitmask = NETLBL_CATMAP_BIT << node_bit;
282
283 for (;;) {
284 bitmap = iter->bitmap[node_idx];
285 while (bitmask != 0 && (bitmap & bitmask) != 0) {
286 bitmask <<= 1;
287 node_bit++;
288 }
289
290 if (bitmask != 0)
291 return iter->startbit +
292 (NETLBL_CATMAP_MAPSIZE * node_idx) +
293 node_bit - 1;
294 else if (++node_idx >= NETLBL_CATMAP_MAPCNT) {
295 if (iter->next == NULL)
296 return iter->startbit + NETLBL_CATMAP_SIZE - 1;
297 iter = iter->next;
298 node_idx = 0;
299 }
300 bitmask = NETLBL_CATMAP_BIT;
301 node_bit = 0;
302 }
303
304 return -ENOENT;
305}
306
307/**
308 * netlbl_secattr_catmap_setbit - Set a bit in a LSM secattr catmap
309 * @catmap: the category bitmap
310 * @bit: the bit to set
311 * @flags: memory allocation flags
312 *
313 * Description:
314 * Set the bit specified by @bit in @catmap. Returns zero on success,
315 * negative values on failure.
316 *
317 */
318int netlbl_secattr_catmap_setbit(struct netlbl_lsm_secattr_catmap *catmap,
319 u32 bit,
320 gfp_t flags)
321{
322 struct netlbl_lsm_secattr_catmap *iter = catmap;
323 u32 node_bit;
324 u32 node_idx;
325
326 while (iter->next != NULL &&
327 bit >= (iter->startbit + NETLBL_CATMAP_SIZE))
328 iter = iter->next;
329 if (bit >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
330 iter->next = netlbl_secattr_catmap_alloc(flags);
331 if (iter->next == NULL)
332 return -ENOMEM;
333 iter = iter->next;
334 iter->startbit = bit & ~(NETLBL_CATMAP_SIZE - 1);
335 }
336
337 /* gcc always rounds to zero when doing integer division */
338 node_idx = (bit - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
339 node_bit = bit - iter->startbit - (NETLBL_CATMAP_MAPSIZE * node_idx);
340 iter->bitmap[node_idx] |= NETLBL_CATMAP_BIT << node_bit;
341
342 return 0;
343}
344
345/**
346 * netlbl_secattr_catmap_setrng - Set a range of bits in a LSM secattr catmap
347 * @catmap: the category bitmap
348 * @start: the starting bit
349 * @end: the last bit in the string
350 * @flags: memory allocation flags
351 *
352 * Description:
353 * Set a range of bits, starting at @start and ending with @end. Returns zero
354 * on success, negative values on failure.
355 *
356 */
357int netlbl_secattr_catmap_setrng(struct netlbl_lsm_secattr_catmap *catmap,
358 u32 start,
359 u32 end,
360 gfp_t flags)
361{
362 int ret_val = 0;
363 struct netlbl_lsm_secattr_catmap *iter = catmap;
364 u32 iter_max_spot;
365 u32 spot;
366
367 /* XXX - This could probably be made a bit faster by combining writes
368 * to the catmap instead of setting a single bit each time, but for
369 * right now skipping to the start of the range in the catmap should
370 * be a nice improvement over calling the individual setbit function
371 * repeatedly from a loop. */
372
373 while (iter->next != NULL &&
374 start >= (iter->startbit + NETLBL_CATMAP_SIZE))
375 iter = iter->next;
376 iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE;
377
378 for (spot = start; spot <= end && ret_val == 0; spot++) {
379 if (spot >= iter_max_spot && iter->next != NULL) {
380 iter = iter->next;
381 iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE;
382 }
383 ret_val = netlbl_secattr_catmap_setbit(iter, spot, GFP_ATOMIC);
384 }
385
386 return ret_val;
387}
388
d15c345f
PM
389/*
390 * LSM Functions
391 */
392
23bcdc1a
PM
393/**
394 * netlbl_enabled - Determine if the NetLabel subsystem is enabled
395 *
396 * Description:
397 * The LSM can use this function to determine if it should use NetLabel
398 * security attributes in it's enforcement mechanism. Currently, NetLabel is
399 * considered to be enabled when it's configuration contains a valid setup for
400 * at least one labeled protocol (i.e. NetLabel can understand incoming
401 * labeled packets of at least one type); otherwise NetLabel is considered to
402 * be disabled.
403 *
404 */
405int netlbl_enabled(void)
406{
407 /* At some point we probably want to expose this mechanism to the user
408 * as well so that admins can toggle NetLabel regardless of the
409 * configuration */
c783f1ce 410 return (atomic_read(&netlabel_mgmt_protocount) > 0);
23bcdc1a
PM
411}
412
d15c345f
PM
413/**
414 * netlbl_socket_setattr - Label a socket using the correct protocol
ba6ff9f2 415 * @sk: the socket to label
d15c345f
PM
416 * @secattr: the security attributes
417 *
418 * Description:
419 * Attach the correct label to the given socket using the security attributes
ba6ff9f2
PM
420 * specified in @secattr. This function requires exclusive access to @sk,
421 * which means it either needs to be in the process of being created or locked.
63c41688
PM
422 * Returns zero on success, -EDESTADDRREQ if the domain is configured to use
423 * network address selectors (can't blindly label the socket), and negative
424 * values on all other failures.
d15c345f
PM
425 *
426 */
ba6ff9f2
PM
427int netlbl_sock_setattr(struct sock *sk,
428 const struct netlbl_lsm_secattr *secattr)
d15c345f
PM
429{
430 int ret_val = -ENOENT;
431 struct netlbl_dom_map *dom_entry;
432
433 rcu_read_lock();
434 dom_entry = netlbl_domhsh_getentry(secattr->domain);
435 if (dom_entry == NULL)
436 goto socket_setattr_return;
437 switch (dom_entry->type) {
63c41688
PM
438 case NETLBL_NLTYPE_ADDRSELECT:
439 ret_val = -EDESTADDRREQ;
440 break;
d15c345f 441 case NETLBL_NLTYPE_CIPSOV4:
ba6ff9f2
PM
442 ret_val = cipso_v4_sock_setattr(sk,
443 dom_entry->type_def.cipsov4,
444 secattr);
d15c345f
PM
445 break;
446 case NETLBL_NLTYPE_UNLABELED:
447 ret_val = 0;
448 break;
449 default:
450 ret_val = -ENOENT;
451 }
452
453socket_setattr_return:
454 rcu_read_unlock();
455 return ret_val;
456}
457
14a72f53
PM
458/**
459 * netlbl_sock_getattr - Determine the security attributes of a sock
460 * @sk: the sock
461 * @secattr: the security attributes
462 *
463 * Description:
8cc44579 464 * Examines the given sock to see if any NetLabel style labeling has been
14a72f53
PM
465 * applied to the sock, if so it parses the socket label and returns the
466 * security attributes in @secattr. Returns zero on success, negative values
467 * on failure.
468 *
469 */
470int netlbl_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
471{
8cc44579 472 return cipso_v4_sock_getattr(sk, secattr);
14a72f53
PM
473}
474
d15c345f
PM
475/**
476 * netlbl_skbuff_getattr - Determine the security attributes of a packet
477 * @skb: the packet
75e22910 478 * @family: protocol family
d15c345f
PM
479 * @secattr: the security attributes
480 *
481 * Description:
482 * Examines the given packet to see if a recognized form of packet labeling
483 * is present, if so it parses the packet label and returns the security
484 * attributes in @secattr. Returns zero on success, negative values on
485 * failure.
486 *
487 */
488int netlbl_skbuff_getattr(const struct sk_buff *skb,
75e22910 489 u16 family,
d15c345f
PM
490 struct netlbl_lsm_secattr *secattr)
491{
05e00cbf
PM
492 if (CIPSO_V4_OPTEXIST(skb) &&
493 cipso_v4_skbuff_getattr(skb, secattr) == 0)
d15c345f
PM
494 return 0;
495
8cc44579 496 return netlbl_unlabel_getattr(skb, family, secattr);
d15c345f
PM
497}
498
499/**
500 * netlbl_skbuff_err - Handle a LSM error on a sk_buff
501 * @skb: the packet
502 * @error: the error code
dfaebe98 503 * @gateway: true if host is acting as a gateway, false otherwise
d15c345f
PM
504 *
505 * Description:
506 * Deal with a LSM problem when handling the packet in @skb, typically this is
507 * a permission denied problem (-EACCES). The correct action is determined
508 * according to the packet's labeling protocol.
509 *
510 */
dfaebe98 511void netlbl_skbuff_err(struct sk_buff *skb, int error, int gateway)
d15c345f
PM
512{
513 if (CIPSO_V4_OPTEXIST(skb))
dfaebe98 514 cipso_v4_error(skb, error, gateway);
d15c345f
PM
515}
516
517/**
518 * netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches
519 *
520 * Description:
521 * For all of the NetLabel protocols that support some form of label mapping
522 * cache, invalidate the cache. Returns zero on success, negative values on
523 * error.
524 *
525 */
526void netlbl_cache_invalidate(void)
527{
528 cipso_v4_cache_invalidate();
529}
530
531/**
532 * netlbl_cache_add - Add an entry to a NetLabel protocol cache
533 * @skb: the packet
534 * @secattr: the packet's security attributes
535 *
536 * Description:
537 * Add the LSM security attributes for the given packet to the underlying
538 * NetLabel protocol's label mapping cache. Returns zero on success, negative
539 * values on error.
540 *
541 */
542int netlbl_cache_add(const struct sk_buff *skb,
543 const struct netlbl_lsm_secattr *secattr)
544{
701a90ba 545 if ((secattr->flags & NETLBL_SECATTR_CACHE) == 0)
d15c345f
PM
546 return -ENOMSG;
547
548 if (CIPSO_V4_OPTEXIST(skb))
549 return cipso_v4_cache_add(skb, secattr);
550
551 return -ENOMSG;
552}
553
554/*
555 * Setup Functions
556 */
557
558/**
559 * netlbl_init - Initialize NetLabel
560 *
561 * Description:
562 * Perform the required NetLabel initialization before first use.
563 *
564 */
565static int __init netlbl_init(void)
566{
567 int ret_val;
568
569 printk(KERN_INFO "NetLabel: Initializing\n");
570 printk(KERN_INFO "NetLabel: domain hash size = %u\n",
571 (1 << NETLBL_DOMHSH_BITSIZE));
572 printk(KERN_INFO "NetLabel: protocols ="
573 " UNLABELED"
574 " CIPSOv4"
575 "\n");
576
577 ret_val = netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE);
578 if (ret_val != 0)
579 goto init_failure;
580
8cc44579
PM
581 ret_val = netlbl_unlabel_init(NETLBL_UNLHSH_BITSIZE);
582 if (ret_val != 0)
583 goto init_failure;
584
d15c345f
PM
585 ret_val = netlbl_netlink_init();
586 if (ret_val != 0)
587 goto init_failure;
588
589 ret_val = netlbl_unlabel_defconf();
590 if (ret_val != 0)
591 goto init_failure;
592 printk(KERN_INFO "NetLabel: unlabeled traffic allowed by default\n");
593
594 return 0;
595
596init_failure:
597 panic("NetLabel: failed to initialize properly (%d)\n", ret_val);
598}
599
600subsys_initcall(netlbl_init);