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, 2008
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_delattr - Delete all the NetLabel labels on a socket
463 * Remove all the NetLabel labeling from @sk. The caller is responsible for
464 * ensuring that @sk is locked.
467 void netlbl_sock_delattr(struct sock *sk)
469 cipso_v4_sock_delattr(sk);
473 * netlbl_sock_getattr - Determine the security attributes of a sock
475 * @secattr: the security attributes
478 * Examines the given sock to see if any NetLabel style labeling has been
479 * applied to the sock, if so it parses the socket label and returns the
480 * security attributes in @secattr. Returns zero on success, negative values
484 int netlbl_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
486 return cipso_v4_sock_getattr(sk, secattr);
490 * netlbl_conn_setattr - Label a connected socket using the correct protocol
491 * @sk: the socket to label
492 * @addr: the destination address
493 * @secattr: the security attributes
496 * Attach the correct label to the given connected socket using the security
497 * attributes specified in @secattr. The caller is responsible for ensuring
498 * that @sk is locked. Returns zero on success, negative values on failure.
501 int netlbl_conn_setattr(struct sock *sk,
502 struct sockaddr *addr,
503 const struct netlbl_lsm_secattr *secattr)
506 struct sockaddr_in *addr4;
507 struct netlbl_domaddr4_map *af4_entry;
510 switch (addr->sa_family) {
512 addr4 = (struct sockaddr_in *)addr;
513 af4_entry = netlbl_domhsh_getentry_af4(secattr->domain,
514 addr4->sin_addr.s_addr);
515 if (af4_entry == NULL) {
517 goto conn_setattr_return;
519 switch (af4_entry->type) {
520 case NETLBL_NLTYPE_CIPSOV4:
521 ret_val = cipso_v4_sock_setattr(sk,
522 af4_entry->type_def.cipsov4,
525 case NETLBL_NLTYPE_UNLABELED:
526 /* just delete the protocols we support for right now
527 * but we could remove other protocols if needed */
528 cipso_v4_sock_delattr(sk);
535 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
537 /* since we don't support any IPv6 labeling protocols right
538 * now we can optimize everything away until we do */
552 * netlbl_skbuff_setattr - Label a packet using the correct protocol
554 * @family: protocol family
555 * @secattr: the security attributes
558 * Attach the correct label to the given packet using the security attributes
559 * specified in @secattr. Returns zero on success, negative values on failure.
562 int netlbl_skbuff_setattr(struct sk_buff *skb,
564 const struct netlbl_lsm_secattr *secattr)
568 struct netlbl_domaddr4_map *af4_entry;
574 af4_entry = netlbl_domhsh_getentry_af4(secattr->domain,
576 if (af4_entry == NULL) {
578 goto skbuff_setattr_return;
580 switch (af4_entry->type) {
581 case NETLBL_NLTYPE_CIPSOV4:
582 ret_val = cipso_v4_skbuff_setattr(skb,
583 af4_entry->type_def.cipsov4,
586 case NETLBL_NLTYPE_UNLABELED:
587 /* just delete the protocols we support for right now
588 * but we could remove other protocols if needed */
589 ret_val = cipso_v4_skbuff_delattr(skb);
595 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
597 /* since we don't support any IPv6 labeling protocols right
598 * now we can optimize everything away until we do */
606 skbuff_setattr_return:
612 * netlbl_skbuff_getattr - Determine the security attributes of a packet
614 * @family: protocol family
615 * @secattr: the security attributes
618 * Examines the given packet to see if a recognized form of packet labeling
619 * is present, if so it parses the packet label and returns the security
620 * attributes in @secattr. Returns zero on success, negative values on
624 int netlbl_skbuff_getattr(const struct sk_buff *skb,
626 struct netlbl_lsm_secattr *secattr)
628 if (CIPSO_V4_OPTEXIST(skb) &&
629 cipso_v4_skbuff_getattr(skb, secattr) == 0)
632 return netlbl_unlabel_getattr(skb, family, secattr);
636 * netlbl_skbuff_err - Handle a LSM error on a sk_buff
638 * @error: the error code
639 * @gateway: true if host is acting as a gateway, false otherwise
642 * Deal with a LSM problem when handling the packet in @skb, typically this is
643 * a permission denied problem (-EACCES). The correct action is determined
644 * according to the packet's labeling protocol.
647 void netlbl_skbuff_err(struct sk_buff *skb, int error, int gateway)
649 if (CIPSO_V4_OPTEXIST(skb))
650 cipso_v4_error(skb, error, gateway);
654 * netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches
657 * For all of the NetLabel protocols that support some form of label mapping
658 * cache, invalidate the cache. Returns zero on success, negative values on
662 void netlbl_cache_invalidate(void)
664 cipso_v4_cache_invalidate();
668 * netlbl_cache_add - Add an entry to a NetLabel protocol cache
670 * @secattr: the packet's security attributes
673 * Add the LSM security attributes for the given packet to the underlying
674 * NetLabel protocol's label mapping cache. Returns zero on success, negative
678 int netlbl_cache_add(const struct sk_buff *skb,
679 const struct netlbl_lsm_secattr *secattr)
681 if ((secattr->flags & NETLBL_SECATTR_CACHE) == 0)
684 if (CIPSO_V4_OPTEXIST(skb))
685 return cipso_v4_cache_add(skb, secattr);
695 * netlbl_init - Initialize NetLabel
698 * Perform the required NetLabel initialization before first use.
701 static int __init netlbl_init(void)
705 printk(KERN_INFO "NetLabel: Initializing\n");
706 printk(KERN_INFO "NetLabel: domain hash size = %u\n",
707 (1 << NETLBL_DOMHSH_BITSIZE));
708 printk(KERN_INFO "NetLabel: protocols ="
713 ret_val = netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE);
717 ret_val = netlbl_unlabel_init(NETLBL_UNLHSH_BITSIZE);
721 ret_val = netlbl_netlink_init();
725 ret_val = netlbl_unlabel_defconf();
728 printk(KERN_INFO "NetLabel: unlabeled traffic allowed by default\n");
733 panic("NetLabel: failed to initialize properly (%d)\n", ret_val);
736 subsys_initcall(netlbl_init);