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;
126 struct netlbl_dom_map *entry;
127 const char *type_str;
128 struct audit_buffer *audit_buf;
131 doi_type = doi_def->type;
133 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
136 if (domain != NULL) {
137 entry->domain = kstrdup(domain, GFP_ATOMIC);
138 if (entry->domain == NULL)
139 goto cfg_cipsov4_add_map_failure;
142 ret_val = cipso_v4_doi_add(doi_def);
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) {
149 goto cfg_cipsov4_add_map_failure_remove_doi;
151 ret_val = netlbl_domhsh_add(entry, audit_info);
153 goto cfg_cipsov4_add_map_failure_release_doi;
155 cfg_cipsov4_add_map_return:
156 audit_buf = netlbl_audit_start_common(AUDIT_MAC_CIPSOV4_ADD,
158 if (audit_buf != NULL) {
160 case CIPSO_V4_MAP_STD:
163 case CIPSO_V4_MAP_PASS:
167 type_str = "(unknown)";
169 audit_log_format(audit_buf,
170 " cipso_doi=%u cipso_type=%s res=%u",
171 doi, type_str, ret_val == 0 ? 1 : 0);
172 audit_log_end(audit_buf);
177 cfg_cipsov4_add_map_failure_release_doi:
178 cipso_v4_doi_putdef(doi_def);
179 cfg_cipsov4_add_map_failure_remove_doi:
180 cipso_v4_doi_remove(doi, audit_info);
181 cfg_cipsov4_add_map_failure:
183 kfree(entry->domain);
185 goto cfg_cipsov4_add_map_return;
189 * Security Attribute Functions
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
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.
202 int netlbl_secattr_catmap_walk(struct netlbl_lsm_secattr_catmap *catmap,
205 struct netlbl_lsm_secattr_catmap *iter = catmap;
208 NETLBL_CATMAP_MAPTYPE bitmap;
210 if (offset > iter->startbit) {
211 while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
216 node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
217 node_bit = offset - iter->startbit -
218 (NETLBL_CATMAP_MAPSIZE * node_idx);
223 bitmap = iter->bitmap[node_idx] >> node_bit;
227 while ((bitmap & NETLBL_CATMAP_BIT) == 0) {
231 return iter->startbit +
232 (NETLBL_CATMAP_MAPSIZE * node_idx) + node_bit;
234 if (++node_idx >= NETLBL_CATMAP_MAPCNT) {
235 if (iter->next != NULL) {
241 bitmap = iter->bitmap[node_idx];
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
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.
259 int netlbl_secattr_catmap_walk_rng(struct netlbl_lsm_secattr_catmap *catmap,
262 struct netlbl_lsm_secattr_catmap *iter = catmap;
265 NETLBL_CATMAP_MAPTYPE bitmask;
266 NETLBL_CATMAP_MAPTYPE bitmap;
268 if (offset > iter->startbit) {
269 while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
274 node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
275 node_bit = offset - iter->startbit -
276 (NETLBL_CATMAP_MAPSIZE * node_idx);
281 bitmask = NETLBL_CATMAP_BIT << node_bit;
284 bitmap = iter->bitmap[node_idx];
285 while (bitmask != 0 && (bitmap & bitmask) != 0) {
291 return iter->startbit +
292 (NETLBL_CATMAP_MAPSIZE * node_idx) +
294 else if (++node_idx >= NETLBL_CATMAP_MAPCNT) {
295 if (iter->next == NULL)
296 return iter->startbit + NETLBL_CATMAP_SIZE - 1;
300 bitmask = NETLBL_CATMAP_BIT;
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
314 * Set the bit specified by @bit in @catmap. Returns zero on success,
315 * negative values on failure.
318 int netlbl_secattr_catmap_setbit(struct netlbl_lsm_secattr_catmap *catmap,
322 struct netlbl_lsm_secattr_catmap *iter = catmap;
326 while (iter->next != NULL &&
327 bit >= (iter->startbit + NETLBL_CATMAP_SIZE))
329 if (bit >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
330 iter->next = netlbl_secattr_catmap_alloc(flags);
331 if (iter->next == NULL)
334 iter->startbit = bit & ~(NETLBL_CATMAP_SIZE - 1);
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;
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
353 * Set a range of bits, starting at @start and ending with @end. Returns zero
354 * on success, negative values on failure.
357 int netlbl_secattr_catmap_setrng(struct netlbl_lsm_secattr_catmap *catmap,
363 struct netlbl_lsm_secattr_catmap *iter = catmap;
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. */
373 while (iter->next != NULL &&
374 start >= (iter->startbit + NETLBL_CATMAP_SIZE))
376 iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE;
378 for (spot = start; spot <= end && ret_val == 0; spot++) {
379 if (spot >= iter_max_spot && iter->next != NULL) {
381 iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE;
383 ret_val = netlbl_secattr_catmap_setbit(iter, spot, GFP_ATOMIC);
394 * netlbl_enabled - Determine if the NetLabel subsystem is enabled
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
405 int netlbl_enabled(void)
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
410 return (atomic_read(&netlabel_mgmt_protocount) > 0);
414 * netlbl_socket_setattr - Label a socket using the correct protocol
415 * @sk: the socket to label
416 * @secattr: the security attributes
419 * Attach the correct label to the given socket using the security attributes
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.
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.
427 int netlbl_sock_setattr(struct sock *sk,
428 const struct netlbl_lsm_secattr *secattr)
430 int ret_val = -ENOENT;
431 struct netlbl_dom_map *dom_entry;
434 dom_entry = netlbl_domhsh_getentry(secattr->domain);
435 if (dom_entry == NULL)
436 goto socket_setattr_return;
437 switch (dom_entry->type) {
438 case NETLBL_NLTYPE_ADDRSELECT:
439 ret_val = -EDESTADDRREQ;
441 case NETLBL_NLTYPE_CIPSOV4:
442 ret_val = cipso_v4_sock_setattr(sk,
443 dom_entry->type_def.cipsov4,
446 case NETLBL_NLTYPE_UNLABELED:
453 socket_setattr_return:
459 * netlbl_sock_getattr - Determine the security attributes of a sock
461 * @secattr: the security attributes
464 * Examines the given sock to see if any NetLabel style labeling has been
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
470 int netlbl_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
472 return cipso_v4_sock_getattr(sk, secattr);
476 * netlbl_skbuff_getattr - Determine the security attributes of a packet
478 * @family: protocol family
479 * @secattr: the security attributes
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
488 int netlbl_skbuff_getattr(const struct sk_buff *skb,
490 struct netlbl_lsm_secattr *secattr)
492 if (CIPSO_V4_OPTEXIST(skb) &&
493 cipso_v4_skbuff_getattr(skb, secattr) == 0)
496 return netlbl_unlabel_getattr(skb, family, secattr);
500 * netlbl_skbuff_err - Handle a LSM error on a sk_buff
502 * @error: the error code
503 * @gateway: true if host is acting as a gateway, false otherwise
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.
511 void netlbl_skbuff_err(struct sk_buff *skb, int error, int gateway)
513 if (CIPSO_V4_OPTEXIST(skb))
514 cipso_v4_error(skb, error, gateway);
518 * netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches
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
526 void netlbl_cache_invalidate(void)
528 cipso_v4_cache_invalidate();
532 * netlbl_cache_add - Add an entry to a NetLabel protocol cache
534 * @secattr: the packet's security attributes
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
542 int netlbl_cache_add(const struct sk_buff *skb,
543 const struct netlbl_lsm_secattr *secattr)
545 if ((secattr->flags & NETLBL_SECATTR_CACHE) == 0)
548 if (CIPSO_V4_OPTEXIST(skb))
549 return cipso_v4_cache_add(skb, secattr);
559 * netlbl_init - Initialize NetLabel
562 * Perform the required NetLabel initialization before first use.
565 static int __init netlbl_init(void)
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 ="
577 ret_val = netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE);
581 ret_val = netlbl_unlabel_init(NETLBL_UNLHSH_BITSIZE);
585 ret_val = netlbl_netlink_init();
589 ret_val = netlbl_unlabel_defconf();
592 printk(KERN_INFO "NetLabel: unlabeled traffic allowed by default\n");
597 panic("NetLabel: failed to initialize properly (%d)\n", ret_val);
600 subsys_initcall(netlbl_init);