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> 33#include <net/ip.h> 34#include <net/netlabel.h> 35#include <net/cipso_ipv4.h> 36#include <asm/bug.h> 37 38#include "netlabel_domainhash.h" 39#include "netlabel_unlabeled.h" 40#include "netlabel_user.h" 41 42/* 43 * Security Attribute Functions 44 */ 45 46/** 47 * netlbl_secattr_catmap_walk - Walk a LSM secattr catmap looking for a bit 48 * @catmap: the category bitmap 49 * @offset: the offset to start searching at, in bits 50 * 51 * Description: 52 * This function walks a LSM secattr category bitmap starting at @offset and 53 * returns the spot of the first set bit or -ENOENT if no bits are set. 54 * 55 */ 56int netlbl_secattr_catmap_walk(struct netlbl_lsm_secattr_catmap *catmap, 57 u32 offset) 58{ 59 struct netlbl_lsm_secattr_catmap *iter = catmap; 60 u32 node_idx; 61 u32 node_bit; 62 NETLBL_CATMAP_MAPTYPE bitmap; 63 64 if (offset > iter->startbit) { 65 while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) { 66 iter = iter->next; 67 if (iter == NULL) 68 return -ENOENT; 69 } 70 node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE; 71 node_bit = offset - iter->startbit - 72 (NETLBL_CATMAP_MAPSIZE * node_idx); 73 } else { 74 node_idx = 0; 75 node_bit = 0; 76 } 77 bitmap = iter->bitmap[node_idx] >> node_bit; 78 79 for (;;) { 80 if (bitmap != 0) { 81 while ((bitmap & NETLBL_CATMAP_BIT) == 0) { 82 bitmap >>= 1; 83 node_bit++; 84 } 85 return iter->startbit + 86 (NETLBL_CATMAP_MAPSIZE * node_idx) + node_bit; 87 } 88 if (++node_idx >= NETLBL_CATMAP_MAPCNT) { 89 if (iter->next != NULL) { 90 iter = iter->next; 91 node_idx = 0; 92 } else 93 return -ENOENT; 94 } 95 bitmap = iter->bitmap[node_idx]; 96 node_bit = 0; 97 } 98 99 return -ENOENT; 100} 101 102/** 103 * netlbl_secattr_catmap_walk_rng - Find the end of a string of set bits 104 * @catmap: the category bitmap 105 * @offset: the offset to start searching at, in bits 106 * 107 * Description: 108 * This function walks a LSM secattr category bitmap starting at @offset and 109 * returns the spot of the first cleared bit or -ENOENT if the offset is past 110 * the end of the bitmap. 111 * 112 */ 113int netlbl_secattr_catmap_walk_rng(struct netlbl_lsm_secattr_catmap *catmap, 114 u32 offset) 115{ 116 struct netlbl_lsm_secattr_catmap *iter = catmap; 117 u32 node_idx; 118 u32 node_bit; 119 NETLBL_CATMAP_MAPTYPE bitmask; 120 NETLBL_CATMAP_MAPTYPE bitmap; 121 122 if (offset > iter->startbit) { 123 while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) { 124 iter = iter->next; 125 if (iter == NULL) 126 return -ENOENT; 127 } 128 node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE; 129 node_bit = offset - iter->startbit - 130 (NETLBL_CATMAP_MAPSIZE * node_idx); 131 } else { 132 node_idx = 0; 133 node_bit = 0; 134 } 135 bitmask = NETLBL_CATMAP_BIT << node_bit; 136 137 for (;;) { 138 bitmap = iter->bitmap[node_idx]; 139 while (bitmask != 0 && (bitmap & bitmask) != 0) { 140 bitmask <<= 1; 141 node_bit++; 142 } 143 144 if (bitmask != 0) 145 return iter->startbit + 146 (NETLBL_CATMAP_MAPSIZE * node_idx) + 147 node_bit - 1; 148 else if (++node_idx >= NETLBL_CATMAP_MAPCNT) { 149 if (iter->next == NULL) 150 return iter->startbit + NETLBL_CATMAP_SIZE - 1; 151 iter = iter->next; 152 node_idx = 0; 153 } 154 bitmask = NETLBL_CATMAP_BIT; 155 node_bit = 0; 156 } 157 158 return -ENOENT; 159} 160 161/** 162 * netlbl_secattr_catmap_setbit - Set a bit in a LSM secattr catmap 163 * @catmap: the category bitmap 164 * @bit: the bit to set 165 * @flags: memory allocation flags 166 * 167 * Description: 168 * Set the bit specified by @bit in @catmap. Returns zero on success, 169 * negative values on failure. 170 * 171 */ 172int netlbl_secattr_catmap_setbit(struct netlbl_lsm_secattr_catmap *catmap, 173 u32 bit, 174 gfp_t flags) 175{ 176 struct netlbl_lsm_secattr_catmap *iter = catmap; 177 u32 node_bit; 178 u32 node_idx; 179 180 while (iter->next != NULL && 181 bit >= (iter->startbit + NETLBL_CATMAP_SIZE)) 182 iter = iter->next; 183 if (bit >= (iter->startbit + NETLBL_CATMAP_SIZE)) { 184 iter->next = netlbl_secattr_catmap_alloc(flags); 185 if (iter->next == NULL) 186 return -ENOMEM; 187 iter = iter->next; 188 iter->startbit = bit & ~(NETLBL_CATMAP_SIZE - 1); 189 } 190 191 /* gcc always rounds to zero when doing integer division */ 192 node_idx = (bit - iter->startbit) / NETLBL_CATMAP_MAPSIZE; 193 node_bit = bit - iter->startbit - (NETLBL_CATMAP_MAPSIZE * node_idx); 194 iter->bitmap[node_idx] |= NETLBL_CATMAP_BIT << node_bit; 195 196 return 0; 197} 198 199/** 200 * netlbl_secattr_catmap_setrng - Set a range of bits in a LSM secattr catmap 201 * @catmap: the category bitmap 202 * @start: the starting bit 203 * @end: the last bit in the string 204 * @flags: memory allocation flags 205 * 206 * Description: 207 * Set a range of bits, starting at @start and ending with @end. Returns zero 208 * on success, negative values on failure. 209 * 210 */ 211int netlbl_secattr_catmap_setrng(struct netlbl_lsm_secattr_catmap *catmap, 212 u32 start, 213 u32 end, 214 gfp_t flags) 215{ 216 int ret_val = 0; 217 struct netlbl_lsm_secattr_catmap *iter = catmap; 218 u32 iter_max_spot; 219 u32 spot; 220 221 222 while (iter->next != NULL && 223 start >= (iter->startbit + NETLBL_CATMAP_SIZE)) 224 iter = iter->next; 225 iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE; 226 227 for (spot = start; spot <= end && ret_val == 0; spot++) { 228 if (spot >= iter_max_spot && iter->next != NULL) { 229 iter = iter->next; 230 iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE; 231 } 232 ret_val = netlbl_secattr_catmap_setbit(iter, spot, GFP_ATOMIC); 233 } 234 235 return ret_val; 236} 237 238/* 239 * LSM Functions 240 */ 241 242/** 243 * netlbl_socket_setattr - Label a socket using the correct protocol 244 * @sk: the socket to label 245 * @secattr: the security attributes 246 * 247 * Description: 248 * Attach the correct label to the given socket using the security attributes 249 * specified in @secattr. This function requires exclusive access to @sk, 250 * which means it either needs to be in the process of being created or locked. 251 * Returns zero on success, negative values on failure. 252 * 253 */ 254int netlbl_sock_setattr(struct sock *sk, 255 const struct netlbl_lsm_secattr *secattr) 256{ 257 int ret_val = -ENOENT; 258 struct netlbl_dom_map *dom_entry; 259 260 rcu_read_lock(); 261 dom_entry = netlbl_domhsh_getentry(secattr->domain); 262 if (dom_entry == NULL) 263 goto socket_setattr_return; 264 switch (dom_entry->type) { 265 case NETLBL_NLTYPE_CIPSOV4: 266 ret_val = cipso_v4_sock_setattr(sk, 267 dom_entry->type_def.cipsov4, 268 secattr); 269 break; 270 case NETLBL_NLTYPE_UNLABELED: 271 ret_val = 0; 272 break; 273 default: 274 ret_val = -ENOENT; 275 } 276 277socket_setattr_return: 278 rcu_read_unlock(); 279 return ret_val; 280} 281 282/** 283 * netlbl_sock_getattr - Determine the security attributes of a sock 284 * @sk: the sock 285 * @secattr: the security attributes 286 * 287 * Description: 288 * Examines the given sock to see any NetLabel style labeling has been 289 * applied to the sock, if so it parses the socket label and returns the 290 * security attributes in @secattr. Returns zero on success, negative values 291 * on failure. 292 * 293 */ 294int netlbl_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr) 295{ 296 int ret_val; 297 298 ret_val = cipso_v4_sock_getattr(sk, secattr); 299 if (ret_val == 0) 300 return 0; 301 302 return netlbl_unlabel_getattr(secattr); 303} 304 305/** 306 * netlbl_skbuff_getattr - Determine the security attributes of a packet 307 * @skb: the packet 308 * @secattr: the security attributes 309 * 310 * Description: 311 * Examines the given packet to see if a recognized form of packet labeling 312 * is present, if so it parses the packet label and returns the security 313 * attributes in @secattr. Returns zero on success, negative values on 314 * failure. 315 * 316 */ 317int netlbl_skbuff_getattr(const struct sk_buff *skb, 318 struct netlbl_lsm_secattr *secattr) 319{ 320 if (CIPSO_V4_OPTEXIST(skb) && 321 cipso_v4_skbuff_getattr(skb, secattr) == 0) 322 return 0; 323 324 return netlbl_unlabel_getattr(secattr); 325} 326 327/** 328 * netlbl_skbuff_err - Handle a LSM error on a sk_buff 329 * @skb: the packet 330 * @error: the error code 331 * 332 * Description: 333 * Deal with a LSM problem when handling the packet in @skb, typically this is 334 * a permission denied problem (-EACCES). The correct action is determined 335 * according to the packet's labeling protocol. 336 * 337 */ 338void netlbl_skbuff_err(struct sk_buff *skb, int error) 339{ 340 if (CIPSO_V4_OPTEXIST(skb)) 341 cipso_v4_error(skb, error, 0); 342} 343 344/** 345 * netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches 346 * 347 * Description: 348 * For all of the NetLabel protocols that support some form of label mapping 349 * cache, invalidate the cache. Returns zero on success, negative values on 350 * error. 351 * 352 */ 353void netlbl_cache_invalidate(void) 354{ 355 cipso_v4_cache_invalidate(); 356} 357 358/** 359 * netlbl_cache_add - Add an entry to a NetLabel protocol cache 360 * @skb: the packet 361 * @secattr: the packet's security attributes 362 * 363 * Description: 364 * Add the LSM security attributes for the given packet to the underlying 365 * NetLabel protocol's label mapping cache. Returns zero on success, negative 366 * values on error. 367 * 368 */ 369int netlbl_cache_add(const struct sk_buff *skb, 370 const struct netlbl_lsm_secattr *secattr) 371{ 372 if ((secattr->flags & NETLBL_SECATTR_CACHE) == 0) 373 return -ENOMSG; 374 375 if (CIPSO_V4_OPTEXIST(skb)) 376 return cipso_v4_cache_add(skb, secattr); 377 378 return -ENOMSG; 379} 380 381/* 382 * Setup Functions 383 */ 384 385/** 386 * netlbl_init - Initialize NetLabel 387 * 388 * Description: 389 * Perform the required NetLabel initialization before first use. 390 * 391 */ 392static int __init netlbl_init(void) 393{ 394 int ret_val; 395 396 printk(KERN_INFO "NetLabel: Initializing\n"); 397 printk(KERN_INFO "NetLabel: domain hash size = %u\n", 398 (1 << NETLBL_DOMHSH_BITSIZE)); 399 printk(KERN_INFO "NetLabel: protocols =" 400 " UNLABELED" 401 " CIPSOv4" 402 "\n"); 403 404 ret_val = netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE); 405 if (ret_val != 0) 406 goto init_failure; 407 408 ret_val = netlbl_netlink_init(); 409 if (ret_val != 0) 410 goto init_failure; 411 412 ret_val = netlbl_unlabel_defconf(); 413 if (ret_val != 0) 414 goto init_failure; 415 printk(KERN_INFO "NetLabel: unlabeled traffic allowed by default\n"); 416 417 return 0; 418 419init_failure: 420 panic("NetLabel: failed to initialize properly (%d)\n", ret_val); 421} 422 423subsys_initcall(netlbl_init); 424