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
8 * Author: Paul Moore <paul.moore@hp.com>
13 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
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.
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.
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
31 #include <linux/init.h>
32 #include <linux/types.h>
33 #include <linux/audit.h>
35 #include <net/netlabel.h>
36 #include <net/cipso_ipv4.h>
38 #include <asm/atomic.h>
40 #include "netlabel_domainhash.h"
41 #include "netlabel_unlabeled.h"
42 #include "netlabel_cipso_v4.h"
43 #include "netlabel_user.h"
44 #include "netlabel_mgmt.h"
47 * Configuration Functions
51 * netlbl_cfg_map_del - Remove a NetLabel/LSM domain mapping
52 * @domain: the domain mapping to remove
53 * @audit_info: NetLabel audit information
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
61 int netlbl_cfg_map_del(const char *domain, struct netlbl_audit *audit_info)
63 return netlbl_domhsh_remove(domain, audit_info);
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
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.
77 int netlbl_cfg_unlbl_add_map(const char *domain,
78 struct netlbl_audit *audit_info)
80 int ret_val = -ENOMEM;
81 struct netlbl_dom_map *entry;
83 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
87 entry->domain = kstrdup(domain, GFP_ATOMIC);
88 if (entry->domain == NULL)
89 goto cfg_unlbl_add_map_failure;
91 entry->type = NETLBL_NLTYPE_UNLABELED;
93 ret_val = netlbl_domhsh_add(entry, audit_info);
95 goto cfg_unlbl_add_map_failure;
99 cfg_unlbl_add_map_failure:
101 kfree(entry->domain);
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
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
119 int netlbl_cfg_cipsov4_add_map(struct cipso_v4_doi *doi_def,
121 struct netlbl_audit *audit_info)
123 int ret_val = -ENOMEM;
124 struct netlbl_dom_map *entry;
125 const char *type_str;
126 struct audit_buffer *audit_buf;
128 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
131 if (domain != NULL) {
132 entry->domain = kstrdup(domain, GFP_ATOMIC);
133 if (entry->domain == NULL)
134 goto cfg_cipsov4_add_map_failure;
136 entry->type = NETLBL_NLTYPE_CIPSOV4;
137 entry->type_def.cipsov4 = doi_def;
139 /* Grab a RCU read lock here so nothing happens to the doi_def variable
140 * between adding it to the CIPSOv4 protocol engine and adding a
141 * domain mapping for it. */
144 ret_val = cipso_v4_doi_add(doi_def);
146 goto cfg_cipsov4_add_map_failure_unlock;
147 ret_val = netlbl_domhsh_add(entry, audit_info);
149 goto cfg_cipsov4_add_map_failure_remove_doi;
154 cfg_cipsov4_add_map_failure_remove_doi:
155 cipso_v4_doi_remove(doi_def->doi, audit_info, netlbl_cipsov4_doi_free);
156 cfg_cipsov4_add_map_failure_unlock:
158 audit_buf = netlbl_audit_start_common(AUDIT_MAC_CIPSOV4_ADD,
160 if (audit_buf != NULL) {
161 switch (doi_def->type) {
162 case CIPSO_V4_MAP_STD:
165 case CIPSO_V4_MAP_PASS:
169 type_str = "(unknown)";
171 audit_log_format(audit_buf,
172 " cipso_doi=%u cipso_type=%s res=%u",
173 doi_def->doi, type_str, ret_val == 0 ? 1 : 0);
174 audit_log_end(audit_buf);
176 cfg_cipsov4_add_map_failure:
178 kfree(entry->domain);
184 * Security Attribute Functions
188 * netlbl_secattr_catmap_walk - Walk a LSM secattr catmap looking for a bit
189 * @catmap: the category bitmap
190 * @offset: the offset to start searching at, in bits
193 * This function walks a LSM secattr category bitmap starting at @offset and
194 * returns the spot of the first set bit or -ENOENT if no bits are set.
197 int netlbl_secattr_catmap_walk(struct netlbl_lsm_secattr_catmap *catmap,
200 struct netlbl_lsm_secattr_catmap *iter = catmap;
203 NETLBL_CATMAP_MAPTYPE bitmap;
205 if (offset > iter->startbit) {
206 while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
211 node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
212 node_bit = offset - iter->startbit -
213 (NETLBL_CATMAP_MAPSIZE * node_idx);
218 bitmap = iter->bitmap[node_idx] >> node_bit;
222 while ((bitmap & NETLBL_CATMAP_BIT) == 0) {
226 return iter->startbit +
227 (NETLBL_CATMAP_MAPSIZE * node_idx) + node_bit;
229 if (++node_idx >= NETLBL_CATMAP_MAPCNT) {
230 if (iter->next != NULL) {
236 bitmap = iter->bitmap[node_idx];
244 * netlbl_secattr_catmap_walk_rng - Find the end of a string of set bits
245 * @catmap: the category bitmap
246 * @offset: the offset to start searching at, in bits
249 * This function walks a LSM secattr category bitmap starting at @offset and
250 * returns the spot of the first cleared bit or -ENOENT if the offset is past
251 * the end of the bitmap.
254 int netlbl_secattr_catmap_walk_rng(struct netlbl_lsm_secattr_catmap *catmap,
257 struct netlbl_lsm_secattr_catmap *iter = catmap;
260 NETLBL_CATMAP_MAPTYPE bitmask;
261 NETLBL_CATMAP_MAPTYPE bitmap;
263 if (offset > iter->startbit) {
264 while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
269 node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
270 node_bit = offset - iter->startbit -
271 (NETLBL_CATMAP_MAPSIZE * node_idx);
276 bitmask = NETLBL_CATMAP_BIT << node_bit;
279 bitmap = iter->bitmap[node_idx];
280 while (bitmask != 0 && (bitmap & bitmask) != 0) {
286 return iter->startbit +
287 (NETLBL_CATMAP_MAPSIZE * node_idx) +
289 else if (++node_idx >= NETLBL_CATMAP_MAPCNT) {
290 if (iter->next == NULL)
291 return iter->startbit + NETLBL_CATMAP_SIZE - 1;
295 bitmask = NETLBL_CATMAP_BIT;
303 * netlbl_secattr_catmap_setbit - Set a bit in a LSM secattr catmap
304 * @catmap: the category bitmap
305 * @bit: the bit to set
306 * @flags: memory allocation flags
309 * Set the bit specified by @bit in @catmap. Returns zero on success,
310 * negative values on failure.
313 int netlbl_secattr_catmap_setbit(struct netlbl_lsm_secattr_catmap *catmap,
317 struct netlbl_lsm_secattr_catmap *iter = catmap;
321 while (iter->next != NULL &&
322 bit >= (iter->startbit + NETLBL_CATMAP_SIZE))
324 if (bit >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
325 iter->next = netlbl_secattr_catmap_alloc(flags);
326 if (iter->next == NULL)
329 iter->startbit = bit & ~(NETLBL_CATMAP_SIZE - 1);
332 /* gcc always rounds to zero when doing integer division */
333 node_idx = (bit - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
334 node_bit = bit - iter->startbit - (NETLBL_CATMAP_MAPSIZE * node_idx);
335 iter->bitmap[node_idx] |= NETLBL_CATMAP_BIT << node_bit;
341 * netlbl_secattr_catmap_setrng - Set a range of bits in a LSM secattr catmap
342 * @catmap: the category bitmap
343 * @start: the starting bit
344 * @end: the last bit in the string
345 * @flags: memory allocation flags
348 * Set a range of bits, starting at @start and ending with @end. Returns zero
349 * on success, negative values on failure.
352 int netlbl_secattr_catmap_setrng(struct netlbl_lsm_secattr_catmap *catmap,
358 struct netlbl_lsm_secattr_catmap *iter = catmap;
362 /* XXX - This could probably be made a bit faster by combining writes
363 * to the catmap instead of setting a single bit each time, but for
364 * right now skipping to the start of the range in the catmap should
365 * be a nice improvement over calling the individual setbit function
366 * repeatedly from a loop. */
368 while (iter->next != NULL &&
369 start >= (iter->startbit + NETLBL_CATMAP_SIZE))
371 iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE;
373 for (spot = start; spot <= end && ret_val == 0; spot++) {
374 if (spot >= iter_max_spot && iter->next != NULL) {
376 iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE;
378 ret_val = netlbl_secattr_catmap_setbit(iter, spot, GFP_ATOMIC);
389 * netlbl_enabled - Determine if the NetLabel subsystem is enabled
392 * The LSM can use this function to determine if it should use NetLabel
393 * security attributes in it's enforcement mechanism. Currently, NetLabel is
394 * considered to be enabled when it's configuration contains a valid setup for
395 * at least one labeled protocol (i.e. NetLabel can understand incoming
396 * labeled packets of at least one type); otherwise NetLabel is considered to
400 int netlbl_enabled(void)
402 /* At some point we probably want to expose this mechanism to the user
403 * as well so that admins can toggle NetLabel regardless of the
405 return (atomic_read(&netlabel_mgmt_protocount) > 0);
409 * netlbl_socket_setattr - Label a socket using the correct protocol
410 * @sk: the socket to label
411 * @secattr: the security attributes
414 * Attach the correct label to the given socket using the security attributes
415 * specified in @secattr. This function requires exclusive access to @sk,
416 * which means it either needs to be in the process of being created or locked.
417 * Returns zero on success, negative values on failure.
420 int netlbl_sock_setattr(struct sock *sk,
421 const struct netlbl_lsm_secattr *secattr)
423 int ret_val = -ENOENT;
424 struct netlbl_dom_map *dom_entry;
427 dom_entry = netlbl_domhsh_getentry(secattr->domain);
428 if (dom_entry == NULL)
429 goto socket_setattr_return;
430 switch (dom_entry->type) {
431 case NETLBL_NLTYPE_CIPSOV4:
432 ret_val = cipso_v4_sock_setattr(sk,
433 dom_entry->type_def.cipsov4,
436 case NETLBL_NLTYPE_UNLABELED:
443 socket_setattr_return:
449 * netlbl_sock_getattr - Determine the security attributes of a sock
451 * @secattr: the security attributes
454 * Examines the given sock to see if any NetLabel style labeling has been
455 * applied to the sock, if so it parses the socket label and returns the
456 * security attributes in @secattr. Returns zero on success, negative values
460 int netlbl_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
462 return cipso_v4_sock_getattr(sk, secattr);
466 * netlbl_skbuff_getattr - Determine the security attributes of a packet
468 * @family: protocol family
469 * @secattr: the security attributes
472 * Examines the given packet to see if a recognized form of packet labeling
473 * is present, if so it parses the packet label and returns the security
474 * attributes in @secattr. Returns zero on success, negative values on
478 int netlbl_skbuff_getattr(const struct sk_buff *skb,
480 struct netlbl_lsm_secattr *secattr)
482 if (CIPSO_V4_OPTEXIST(skb) &&
483 cipso_v4_skbuff_getattr(skb, secattr) == 0)
486 return netlbl_unlabel_getattr(skb, family, secattr);
490 * netlbl_skbuff_err - Handle a LSM error on a sk_buff
492 * @error: the error code
493 * @gateway: true if host is acting as a gateway, false otherwise
496 * Deal with a LSM problem when handling the packet in @skb, typically this is
497 * a permission denied problem (-EACCES). The correct action is determined
498 * according to the packet's labeling protocol.
501 void netlbl_skbuff_err(struct sk_buff *skb, int error, int gateway)
503 if (CIPSO_V4_OPTEXIST(skb))
504 cipso_v4_error(skb, error, gateway);
508 * netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches
511 * For all of the NetLabel protocols that support some form of label mapping
512 * cache, invalidate the cache. Returns zero on success, negative values on
516 void netlbl_cache_invalidate(void)
518 cipso_v4_cache_invalidate();
522 * netlbl_cache_add - Add an entry to a NetLabel protocol cache
524 * @secattr: the packet's security attributes
527 * Add the LSM security attributes for the given packet to the underlying
528 * NetLabel protocol's label mapping cache. Returns zero on success, negative
532 int netlbl_cache_add(const struct sk_buff *skb,
533 const struct netlbl_lsm_secattr *secattr)
535 if ((secattr->flags & NETLBL_SECATTR_CACHE) == 0)
538 if (CIPSO_V4_OPTEXIST(skb))
539 return cipso_v4_cache_add(skb, secattr);
549 * netlbl_init - Initialize NetLabel
552 * Perform the required NetLabel initialization before first use.
555 static int __init netlbl_init(void)
559 printk(KERN_INFO "NetLabel: Initializing\n");
560 printk(KERN_INFO "NetLabel: domain hash size = %u\n",
561 (1 << NETLBL_DOMHSH_BITSIZE));
562 printk(KERN_INFO "NetLabel: protocols ="
567 ret_val = netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE);
571 ret_val = netlbl_unlabel_init(NETLBL_UNLHSH_BITSIZE);
575 ret_val = netlbl_netlink_init();
579 ret_val = netlbl_unlabel_defconf();
582 printk(KERN_INFO "NetLabel: unlabeled traffic allowed by default\n");
587 panic("NetLabel: failed to initialize properly (%d)\n", ret_val);
590 subsys_initcall(netlbl_init);