at commit c03c1a14
| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * CIPSO - Commercial IP Security Option |
| 4 | * |
| 5 | * This is an implementation of the CIPSO 2.2 protocol as specified in |
| 6 | * draft-ietf-cipso-ipsecurity-01.txt with additional tag types as found in |
| 7 | * FIPS-188. While CIPSO never became a full IETF RFC standard many vendors |
| 8 | * have chosen to adopt the protocol and over the years it has become a |
| 9 | * de-facto standard for labeled networking. |
| 10 | * |
| 11 | * The CIPSO draft specification can be found in the kernel's Documentation |
| 12 | * directory as well as the following URL: |
| 13 | * https://tools.ietf.org/id/draft-ietf-cipso-ipsecurity-01.txt |
| 14 | * The FIPS-188 specification can be found at the following URL: |
| 15 | * https://www.itl.nist.gov/fipspubs/fip188.htm |
| 16 | * |
| 17 | * Author: Paul Moore <paul.moore@hp.com> |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008 |
| 22 | */ |
| 23 | |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/types.h> |
| 26 | #include <linux/rcupdate.h> |
| 27 | #include <linux/list.h> |
| 28 | #include <linux/spinlock.h> |
| 29 | #include <linux/string.h> |
| 30 | #include <linux/jhash.h> |
| 31 | #include <linux/audit.h> |
| 32 | #include <linux/slab.h> |
| 33 | #include <net/ip.h> |
| 34 | #include <net/icmp.h> |
| 35 | #include <net/tcp.h> |
| 36 | #include <net/netlabel.h> |
| 37 | #include <net/cipso_ipv4.h> |
| 38 | #include <linux/atomic.h> |
| 39 | #include <linux/bug.h> |
| 40 | #include <linux/unaligned.h> |
| 41 | |
| 42 | /* List of available DOI definitions */ |
| 43 | /* XXX - This currently assumes a minimal number of different DOIs in use, |
| 44 | * if in practice there are a lot of different DOIs this list should |
| 45 | * probably be turned into a hash table or something similar so we |
| 46 | * can do quick lookups. */ |
| 47 | static DEFINE_SPINLOCK(cipso_v4_doi_list_lock); |
| 48 | static LIST_HEAD(cipso_v4_doi_list); |
| 49 | |
| 50 | /* Label mapping cache */ |
| 51 | int cipso_v4_cache_enabled = 1; |
| 52 | int cipso_v4_cache_bucketsize = 10; |
| 53 | #define CIPSO_V4_CACHE_BUCKETBITS 7 |
| 54 | #define CIPSO_V4_CACHE_BUCKETS (1 << CIPSO_V4_CACHE_BUCKETBITS) |
| 55 | #define CIPSO_V4_CACHE_REORDERLIMIT 10 |
| 56 | struct cipso_v4_map_cache_bkt { |
| 57 | spinlock_t lock; |
| 58 | u32 size; |
| 59 | struct list_head list; |
| 60 | }; |
| 61 | |
| 62 | struct cipso_v4_map_cache_entry { |
| 63 | u32 hash; |
| 64 | unsigned char *key; |
| 65 | size_t key_len; |
| 66 | |
| 67 | struct netlbl_lsm_cache *lsm_data; |
| 68 | |
| 69 | u32 activity; |
| 70 | struct list_head list; |
| 71 | }; |
| 72 | |
| 73 | static struct cipso_v4_map_cache_bkt *cipso_v4_cache; |
| 74 | |
| 75 | /* Restricted bitmap (tag #1) flags */ |
| 76 | int cipso_v4_rbm_optfmt; |
| 77 | int cipso_v4_rbm_strictvalid = 1; |
| 78 | |
| 79 | /* |
| 80 | * Protocol Constants |
| 81 | */ |
| 82 | |
| 83 | /* Maximum size of the CIPSO IP option, derived from the fact that the maximum |
| 84 | * IPv4 header size is 60 bytes and the base IPv4 header is 20 bytes long. */ |
| 85 | #define CIPSO_V4_OPT_LEN_MAX 40 |
| 86 | |
| 87 | /* Length of the base CIPSO option, this includes the option type (1 byte), the |
| 88 | * option length (1 byte), and the DOI (4 bytes). */ |
| 89 | #define CIPSO_V4_HDR_LEN 6 |
| 90 | |
| 91 | /* Base length of the restrictive category bitmap tag (tag #1). */ |
| 92 | #define CIPSO_V4_TAG_RBM_BLEN 4 |
| 93 | |
| 94 | /* Base length of the enumerated category tag (tag #2). */ |
| 95 | #define CIPSO_V4_TAG_ENUM_BLEN 4 |
| 96 | |
| 97 | /* Base length of the ranged categories bitmap tag (tag #5). */ |
| 98 | #define CIPSO_V4_TAG_RNG_BLEN 4 |
| 99 | /* The maximum number of category ranges permitted in the ranged category tag |
| 100 | * (tag #5). You may note that the IETF draft states that the maximum number |
| 101 | * of category ranges is 7, but if the low end of the last category range is |
| 102 | * zero then it is possible to fit 8 category ranges because the zero should |
| 103 | * be omitted. */ |
| 104 | #define CIPSO_V4_TAG_RNG_CAT_MAX 8 |
| 105 | |
| 106 | /* Base length of the local tag (non-standard tag). |
| 107 | * Tag definition (may change between kernel versions) |
| 108 | * |
| 109 | * 0 8 16 24 32 |
| 110 | * +----------+----------+----------+----------+ |
| 111 | * | 10000000 | 00000110 | 32-bit secid value | |
| 112 | * +----------+----------+----------+----------+ |
| 113 | * | in (host byte order)| |
| 114 | * +----------+----------+ |
| 115 | * |
| 116 | */ |
| 117 | #define CIPSO_V4_TAG_LOC_BLEN 6 |
| 118 | |
| 119 | /* |
| 120 | * Helper Functions |
| 121 | */ |
| 122 | |
| 123 | /** |
| 124 | * cipso_v4_cache_entry_free - Frees a cache entry |
| 125 | * @entry: the entry to free |
| 126 | * |
| 127 | * Description: |
| 128 | * This function frees the memory associated with a cache entry including the |
| 129 | * LSM cache data if there are no longer any users, i.e. reference count == 0. |
| 130 | * |
| 131 | */ |
| 132 | static void cipso_v4_cache_entry_free(struct cipso_v4_map_cache_entry *entry) |
| 133 | { |
| 134 | if (entry->lsm_data) |
| 135 | netlbl_secattr_cache_free(entry->lsm_data); |
| 136 | kfree(entry->key); |
| 137 | kfree(entry); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * cipso_v4_map_cache_hash - Hashing function for the CIPSO cache |
| 142 | * @key: the hash key |
| 143 | * @key_len: the length of the key in bytes |
| 144 | * |
| 145 | * Description: |
| 146 | * The CIPSO tag hashing function. Returns a 32-bit hash value. |
| 147 | * |
| 148 | */ |
| 149 | static u32 cipso_v4_map_cache_hash(const unsigned char *key, u32 key_len) |
| 150 | { |
| 151 | return jhash(key, key_len, 0); |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Label Mapping Cache Functions |
| 156 | */ |
| 157 | |
| 158 | /** |
| 159 | * cipso_v4_cache_init - Initialize the CIPSO cache |
| 160 | * |
| 161 | * Description: |
| 162 | * Initializes the CIPSO label mapping cache, this function should be called |
| 163 | * before any of the other functions defined in this file. Returns zero on |
| 164 | * success, negative values on error. |
| 165 | * |
| 166 | */ |
| 167 | static int __init cipso_v4_cache_init(void) |
| 168 | { |
| 169 | u32 iter; |
| 170 | |
| 171 | cipso_v4_cache = kzalloc_objs(struct cipso_v4_map_cache_bkt, |
| 172 | CIPSO_V4_CACHE_BUCKETS); |
| 173 | if (!cipso_v4_cache) |
| 174 | return -ENOMEM; |
| 175 | |
| 176 | for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) { |
| 177 | spin_lock_init(&cipso_v4_cache[iter].lock); |
| 178 | cipso_v4_cache[iter].size = 0; |
| 179 | INIT_LIST_HEAD(&cipso_v4_cache[iter].list); |
| 180 | } |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * cipso_v4_cache_invalidate - Invalidates the current CIPSO cache |
| 187 | * |
| 188 | * Description: |
| 189 | * Invalidates and frees any entries in the CIPSO cache. |
| 190 | * |
| 191 | */ |
| 192 | void cipso_v4_cache_invalidate(void) |
| 193 | { |
| 194 | struct cipso_v4_map_cache_entry *entry, *tmp_entry; |
| 195 | u32 iter; |
| 196 | |
| 197 | for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) { |
| 198 | spin_lock_bh(&cipso_v4_cache[iter].lock); |
| 199 | list_for_each_entry_safe(entry, |
| 200 | tmp_entry, |
| 201 | &cipso_v4_cache[iter].list, list) { |
| 202 | list_del(&entry->list); |
| 203 | cipso_v4_cache_entry_free(entry); |
| 204 | } |
| 205 | cipso_v4_cache[iter].size = 0; |
| 206 | spin_unlock_bh(&cipso_v4_cache[iter].lock); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * cipso_v4_cache_check - Check the CIPSO cache for a label mapping |
| 212 | * @key: the buffer to check |
| 213 | * @key_len: buffer length in bytes |
| 214 | * @secattr: the security attribute struct to use |
| 215 | * |
| 216 | * Description: |
| 217 | * This function checks the cache to see if a label mapping already exists for |
| 218 | * the given key. If there is a match then the cache is adjusted and the |
| 219 | * @secattr struct is populated with the correct LSM security attributes. The |
| 220 | * cache is adjusted in the following manner if the entry is not already the |
| 221 | * first in the cache bucket: |
| 222 | * |
| 223 | * 1. The cache entry's activity counter is incremented |
| 224 | * 2. The previous (higher ranking) entry's activity counter is decremented |
| 225 | * 3. If the difference between the two activity counters is geater than |
| 226 | * CIPSO_V4_CACHE_REORDERLIMIT the two entries are swapped |
| 227 | * |
| 228 | * Returns zero on success, -ENOENT for a cache miss, and other negative values |
| 229 | * on error. |
| 230 | * |
| 231 | */ |
| 232 | static int cipso_v4_cache_check(const unsigned char *key, |
| 233 | u32 key_len, |
| 234 | struct netlbl_lsm_secattr *secattr) |
| 235 | { |
| 236 | u32 bkt; |
| 237 | struct cipso_v4_map_cache_entry *entry; |
| 238 | struct cipso_v4_map_cache_entry *prev_entry = NULL; |
| 239 | u32 hash; |
| 240 | |
| 241 | if (!READ_ONCE(cipso_v4_cache_enabled)) |
| 242 | return -ENOENT; |
| 243 | |
| 244 | hash = cipso_v4_map_cache_hash(key, key_len); |
| 245 | bkt = hash & (CIPSO_V4_CACHE_BUCKETS - 1); |
| 246 | spin_lock_bh(&cipso_v4_cache[bkt].lock); |
| 247 | list_for_each_entry(entry, &cipso_v4_cache[bkt].list, list) { |
| 248 | if (entry->hash == hash && |
| 249 | entry->key_len == key_len && |
| 250 | memcmp(entry->key, key, key_len) == 0) { |
| 251 | entry->activity += 1; |
| 252 | refcount_inc(&entry->lsm_data->refcount); |
| 253 | secattr->cache = entry->lsm_data; |
| 254 | secattr->flags |= NETLBL_SECATTR_CACHE; |
| 255 | secattr->type = NETLBL_NLTYPE_CIPSOV4; |
| 256 | if (!prev_entry) { |
| 257 | spin_unlock_bh(&cipso_v4_cache[bkt].lock); |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | if (prev_entry->activity > 0) |
| 262 | prev_entry->activity -= 1; |
| 263 | if (entry->activity > prev_entry->activity && |
| 264 | entry->activity - prev_entry->activity > |
| 265 | CIPSO_V4_CACHE_REORDERLIMIT) { |
| 266 | __list_del(entry->list.prev, entry->list.next); |
| 267 | __list_add(&entry->list, |
| 268 | prev_entry->list.prev, |
| 269 | &prev_entry->list); |
| 270 | } |
| 271 | |
| 272 | spin_unlock_bh(&cipso_v4_cache[bkt].lock); |
| 273 | return 0; |
| 274 | } |
| 275 | prev_entry = entry; |
| 276 | } |
| 277 | spin_unlock_bh(&cipso_v4_cache[bkt].lock); |
| 278 | |
| 279 | return -ENOENT; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * cipso_v4_cache_add - Add an entry to the CIPSO cache |
| 284 | * @cipso_ptr: pointer to CIPSO IP option |
| 285 | * @secattr: the packet's security attributes |
| 286 | * |
| 287 | * Description: |
| 288 | * Add a new entry into the CIPSO label mapping cache. Add the new entry to |
| 289 | * head of the cache bucket's list, if the cache bucket is out of room remove |
| 290 | * the last entry in the list first. It is important to note that there is |
| 291 | * currently no checking for duplicate keys. Returns zero on success, |
| 292 | * negative values on failure. |
| 293 | * |
| 294 | */ |
| 295 | int cipso_v4_cache_add(const unsigned char *cipso_ptr, |
| 296 | const struct netlbl_lsm_secattr *secattr) |
| 297 | { |
| 298 | int bkt_size = READ_ONCE(cipso_v4_cache_bucketsize); |
| 299 | int ret_val = -EPERM; |
| 300 | u32 bkt; |
| 301 | struct cipso_v4_map_cache_entry *entry = NULL; |
| 302 | struct cipso_v4_map_cache_entry *old_entry = NULL; |
| 303 | u32 cipso_ptr_len; |
| 304 | |
| 305 | if (!READ_ONCE(cipso_v4_cache_enabled) || bkt_size <= 0) |
| 306 | return 0; |
| 307 | |
| 308 | cipso_ptr_len = cipso_ptr[1]; |
| 309 | |
| 310 | entry = kzalloc_obj(*entry, GFP_ATOMIC); |
| 311 | if (!entry) |
| 312 | return -ENOMEM; |
| 313 | entry->key = kmemdup(cipso_ptr, cipso_ptr_len, GFP_ATOMIC); |
| 314 | if (!entry->key) { |
| 315 | ret_val = -ENOMEM; |
| 316 | goto cache_add_failure; |
| 317 | } |
| 318 | entry->key_len = cipso_ptr_len; |
| 319 | entry->hash = cipso_v4_map_cache_hash(cipso_ptr, cipso_ptr_len); |
| 320 | refcount_inc(&secattr->cache->refcount); |
| 321 | entry->lsm_data = secattr->cache; |
| 322 | |
| 323 | bkt = entry->hash & (CIPSO_V4_CACHE_BUCKETS - 1); |
| 324 | spin_lock_bh(&cipso_v4_cache[bkt].lock); |
| 325 | if (cipso_v4_cache[bkt].size < bkt_size) { |
| 326 | list_add(&entry->list, &cipso_v4_cache[bkt].list); |
| 327 | cipso_v4_cache[bkt].size += 1; |
| 328 | } else { |
| 329 | old_entry = list_entry(cipso_v4_cache[bkt].list.prev, |
| 330 | struct cipso_v4_map_cache_entry, list); |
| 331 | list_del(&old_entry->list); |
| 332 | list_add(&entry->list, &cipso_v4_cache[bkt].list); |
| 333 | cipso_v4_cache_entry_free(old_entry); |
| 334 | } |
| 335 | spin_unlock_bh(&cipso_v4_cache[bkt].lock); |
| 336 | |
| 337 | return 0; |
| 338 | |
| 339 | cache_add_failure: |
| 340 | if (entry) |
| 341 | cipso_v4_cache_entry_free(entry); |
| 342 | return ret_val; |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | * DOI List Functions |
| 347 | */ |
| 348 | |
| 349 | /** |
| 350 | * cipso_v4_doi_search - Searches for a DOI definition |
| 351 | * @doi: the DOI to search for |
| 352 | * |
| 353 | * Description: |
| 354 | * Search the DOI definition list for a DOI definition with a DOI value that |
| 355 | * matches @doi. The caller is responsible for calling rcu_read_[un]lock(). |
| 356 | * Returns a pointer to the DOI definition on success and NULL on failure. |
| 357 | */ |
| 358 | static struct cipso_v4_doi *cipso_v4_doi_search(u32 doi) |
| 359 | { |
| 360 | struct cipso_v4_doi *iter; |
| 361 | |
| 362 | list_for_each_entry_rcu(iter, &cipso_v4_doi_list, list) |
| 363 | if (iter->doi == doi && refcount_read(&iter->refcount)) |
| 364 | return iter; |
| 365 | return NULL; |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * cipso_v4_doi_add - Add a new DOI to the CIPSO protocol engine |
| 370 | * @doi_def: the DOI structure |
| 371 | * @audit_info: NetLabel audit information |
| 372 | * |
| 373 | * Description: |
| 374 | * The caller defines a new DOI for use by the CIPSO engine and calls this |
| 375 | * function to add it to the list of acceptable domains. The caller must |
| 376 | * ensure that the mapping table specified in @doi_def->map meets all of the |
| 377 | * requirements of the mapping type (see cipso_ipv4.h for details). Returns |
| 378 | * zero on success and non-zero on failure. |
| 379 | * |
| 380 | */ |
| 381 | int cipso_v4_doi_add(struct cipso_v4_doi *doi_def, |
| 382 | struct netlbl_audit *audit_info) |
| 383 | { |
| 384 | int ret_val = -EINVAL; |
| 385 | u32 iter; |
| 386 | u32 doi; |
| 387 | u32 doi_type; |
| 388 | struct audit_buffer *audit_buf; |
| 389 | |
| 390 | doi = doi_def->doi; |
| 391 | doi_type = doi_def->type; |
| 392 | |
| 393 | if (doi_def->doi == CIPSO_V4_DOI_UNKNOWN) |
| 394 | goto doi_add_return; |
| 395 | for (iter = 0; iter < CIPSO_V4_TAG_MAXCNT; iter++) { |
| 396 | switch (doi_def->tags[iter]) { |
| 397 | case CIPSO_V4_TAG_RBITMAP: |
| 398 | break; |
| 399 | case CIPSO_V4_TAG_RANGE: |
| 400 | case CIPSO_V4_TAG_ENUM: |
| 401 | if (doi_def->type != CIPSO_V4_MAP_PASS) |
| 402 | goto doi_add_return; |
| 403 | break; |
| 404 | case CIPSO_V4_TAG_LOCAL: |
| 405 | if (doi_def->type != CIPSO_V4_MAP_LOCAL) |
| 406 | goto doi_add_return; |
| 407 | break; |
| 408 | case CIPSO_V4_TAG_INVALID: |
| 409 | if (iter == 0) |
| 410 | goto doi_add_return; |
| 411 | break; |
| 412 | default: |
| 413 | goto doi_add_return; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | refcount_set(&doi_def->refcount, 1); |
| 418 | |
| 419 | spin_lock(&cipso_v4_doi_list_lock); |
| 420 | if (cipso_v4_doi_search(doi_def->doi)) { |
| 421 | spin_unlock(&cipso_v4_doi_list_lock); |
| 422 | ret_val = -EEXIST; |
| 423 | goto doi_add_return; |
| 424 | } |
| 425 | list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list); |
| 426 | spin_unlock(&cipso_v4_doi_list_lock); |
| 427 | ret_val = 0; |
| 428 | |
| 429 | doi_add_return: |
| 430 | audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_ADD, audit_info); |
| 431 | if (audit_buf) { |
| 432 | const char *type_str; |
| 433 | switch (doi_type) { |
| 434 | case CIPSO_V4_MAP_TRANS: |
| 435 | type_str = "trans"; |
| 436 | break; |
| 437 | case CIPSO_V4_MAP_PASS: |
| 438 | type_str = "pass"; |
| 439 | break; |
| 440 | case CIPSO_V4_MAP_LOCAL: |
| 441 | type_str = "local"; |
| 442 | break; |
| 443 | default: |
| 444 | type_str = "(unknown)"; |
| 445 | } |
| 446 | audit_log_format(audit_buf, |
| 447 | " cipso_doi=%u cipso_type=%s res=%u", |
| 448 | doi, type_str, ret_val == 0 ? 1 : 0); |
| 449 | audit_log_end(audit_buf); |
| 450 | } |
| 451 | |
| 452 | return ret_val; |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * cipso_v4_doi_free - Frees a DOI definition |
| 457 | * @doi_def: the DOI definition |
| 458 | * |
| 459 | * Description: |
| 460 | * This function frees all of the memory associated with a DOI definition. |
| 461 | * |
| 462 | */ |
| 463 | void cipso_v4_doi_free(struct cipso_v4_doi *doi_def) |
| 464 | { |
| 465 | if (!doi_def) |
| 466 | return; |
| 467 | |
| 468 | switch (doi_def->type) { |
| 469 | case CIPSO_V4_MAP_TRANS: |
| 470 | kfree(doi_def->map.std->lvl.cipso); |
| 471 | kfree(doi_def->map.std->lvl.local); |
| 472 | kfree(doi_def->map.std->cat.cipso); |
| 473 | kfree(doi_def->map.std->cat.local); |
| 474 | kfree(doi_def->map.std); |
| 475 | break; |
| 476 | } |
| 477 | kfree(doi_def); |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * cipso_v4_doi_free_rcu - Frees a DOI definition via the RCU pointer |
| 482 | * @entry: the entry's RCU field |
| 483 | * |
| 484 | * Description: |
| 485 | * This function is designed to be used as a callback to the call_rcu() |
| 486 | * function so that the memory allocated to the DOI definition can be released |
| 487 | * safely. |
| 488 | * |
| 489 | */ |
| 490 | static void cipso_v4_doi_free_rcu(struct rcu_head *entry) |
| 491 | { |
| 492 | struct cipso_v4_doi *doi_def; |
| 493 | |
| 494 | doi_def = container_of(entry, struct cipso_v4_doi, rcu); |
| 495 | cipso_v4_doi_free(doi_def); |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine |
| 500 | * @doi: the DOI value |
| 501 | * @audit_info: NetLabel audit information |
| 502 | * |
| 503 | * Description: |
| 504 | * Removes a DOI definition from the CIPSO engine. The NetLabel routines will |
| 505 | * be called to release their own LSM domain mappings as well as our own |
| 506 | * domain list. Returns zero on success and negative values on failure. |
| 507 | * |
| 508 | */ |
| 509 | int cipso_v4_doi_remove(u32 doi, struct netlbl_audit *audit_info) |
| 510 | { |
| 511 | int ret_val; |
| 512 | struct cipso_v4_doi *doi_def; |
| 513 | struct audit_buffer *audit_buf; |
| 514 | |
| 515 | spin_lock(&cipso_v4_doi_list_lock); |
| 516 | doi_def = cipso_v4_doi_search(doi); |
| 517 | if (!doi_def) { |
| 518 | spin_unlock(&cipso_v4_doi_list_lock); |
| 519 | ret_val = -ENOENT; |
| 520 | goto doi_remove_return; |
| 521 | } |
| 522 | list_del_rcu(&doi_def->list); |
| 523 | spin_unlock(&cipso_v4_doi_list_lock); |
| 524 | |
| 525 | cipso_v4_doi_putdef(doi_def); |
| 526 | ret_val = 0; |
| 527 | |
| 528 | doi_remove_return: |
| 529 | audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_DEL, audit_info); |
| 530 | if (audit_buf) { |
| 531 | audit_log_format(audit_buf, |
| 532 | " cipso_doi=%u res=%u", |
| 533 | doi, ret_val == 0 ? 1 : 0); |
| 534 | audit_log_end(audit_buf); |
| 535 | } |
| 536 | |
| 537 | return ret_val; |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * cipso_v4_doi_getdef - Returns a reference to a valid DOI definition |
| 542 | * @doi: the DOI value |
| 543 | * |
| 544 | * Description: |
| 545 | * Searches for a valid DOI definition and if one is found it is returned to |
| 546 | * the caller. Otherwise NULL is returned. The caller must ensure that |
| 547 | * rcu_read_lock() is held while accessing the returned definition and the DOI |
| 548 | * definition reference count is decremented when the caller is done. |
| 549 | * |
| 550 | */ |
| 551 | struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi) |
| 552 | { |
| 553 | struct cipso_v4_doi *doi_def; |
| 554 | |
| 555 | rcu_read_lock(); |
| 556 | doi_def = cipso_v4_doi_search(doi); |
| 557 | if (!doi_def) |
| 558 | goto doi_getdef_return; |
| 559 | if (!refcount_inc_not_zero(&doi_def->refcount)) |
| 560 | doi_def = NULL; |
| 561 | |
| 562 | doi_getdef_return: |
| 563 | rcu_read_unlock(); |
| 564 | return doi_def; |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * cipso_v4_doi_putdef - Releases a reference for the given DOI definition |
| 569 | * @doi_def: the DOI definition |
| 570 | * |
| 571 | * Description: |
| 572 | * Releases a DOI definition reference obtained from cipso_v4_doi_getdef(). |
| 573 | * |
| 574 | */ |
| 575 | void cipso_v4_doi_putdef(struct cipso_v4_doi *doi_def) |
| 576 | { |
| 577 | if (!doi_def) |
| 578 | return; |
| 579 | |
| 580 | if (!refcount_dec_and_test(&doi_def->refcount)) |
| 581 | return; |
| 582 | |
| 583 | cipso_v4_cache_invalidate(); |
| 584 | call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu); |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * cipso_v4_doi_walk - Iterate through the DOI definitions |
| 589 | * @skip_cnt: skip past this number of DOI definitions, updated |
| 590 | * @callback: callback for each DOI definition |
| 591 | * @cb_arg: argument for the callback function |
| 592 | * |
| 593 | * Description: |
| 594 | * Iterate over the DOI definition list, skipping the first @skip_cnt entries. |
| 595 | * For each entry call @callback, if @callback returns a negative value stop |
| 596 | * 'walking' through the list and return. Updates the value in @skip_cnt upon |
| 597 | * return. Returns zero on success, negative values on failure. |
| 598 | * |
| 599 | */ |
| 600 | int cipso_v4_doi_walk(u32 *skip_cnt, |
| 601 | int (*callback) (struct cipso_v4_doi *doi_def, void *arg), |
| 602 | void *cb_arg) |
| 603 | { |
| 604 | int ret_val = -ENOENT; |
| 605 | u32 doi_cnt = 0; |
| 606 | struct cipso_v4_doi *iter_doi; |
| 607 | |
| 608 | rcu_read_lock(); |
| 609 | list_for_each_entry_rcu(iter_doi, &cipso_v4_doi_list, list) |
| 610 | if (refcount_read(&iter_doi->refcount) > 0) { |
| 611 | if (doi_cnt++ < *skip_cnt) |
| 612 | continue; |
| 613 | ret_val = callback(iter_doi, cb_arg); |
| 614 | if (ret_val < 0) { |
| 615 | doi_cnt--; |
| 616 | goto doi_walk_return; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | doi_walk_return: |
| 621 | rcu_read_unlock(); |
| 622 | *skip_cnt = doi_cnt; |
| 623 | return ret_val; |
| 624 | } |
| 625 | |
| 626 | /* |
| 627 | * Label Mapping Functions |
| 628 | */ |
| 629 | |
| 630 | /** |
| 631 | * cipso_v4_map_lvl_valid - Checks to see if the given level is understood |
| 632 | * @doi_def: the DOI definition |
| 633 | * @level: the level to check |
| 634 | * |
| 635 | * Description: |
| 636 | * Checks the given level against the given DOI definition and returns a |
| 637 | * negative value if the level does not have a valid mapping and a zero value |
| 638 | * if the level is defined by the DOI. |
| 639 | * |
| 640 | */ |
| 641 | static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi *doi_def, u8 level) |
| 642 | { |
| 643 | switch (doi_def->type) { |
| 644 | case CIPSO_V4_MAP_PASS: |
| 645 | return 0; |
| 646 | case CIPSO_V4_MAP_TRANS: |
| 647 | if ((level < doi_def->map.std->lvl.cipso_size) && |
| 648 | (doi_def->map.std->lvl.cipso[level] < CIPSO_V4_INV_LVL)) |
| 649 | return 0; |
| 650 | break; |
| 651 | } |
| 652 | |
| 653 | return -EFAULT; |
| 654 | } |
| 655 | |
| 656 | /** |
| 657 | * cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network |
| 658 | * @doi_def: the DOI definition |
| 659 | * @host_lvl: the host MLS level |
| 660 | * @net_lvl: the network/CIPSO MLS level |
| 661 | * |
| 662 | * Description: |
| 663 | * Perform a label mapping to translate a local MLS level to the correct |
| 664 | * CIPSO level using the given DOI definition. Returns zero on success, |
| 665 | * negative values otherwise. |
| 666 | * |
| 667 | */ |
| 668 | static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi *doi_def, |
| 669 | u32 host_lvl, |
| 670 | u32 *net_lvl) |
| 671 | { |
| 672 | switch (doi_def->type) { |
| 673 | case CIPSO_V4_MAP_PASS: |
| 674 | *net_lvl = host_lvl; |
| 675 | return 0; |
| 676 | case CIPSO_V4_MAP_TRANS: |
| 677 | if (host_lvl < doi_def->map.std->lvl.local_size && |
| 678 | doi_def->map.std->lvl.local[host_lvl] < CIPSO_V4_INV_LVL) { |
| 679 | *net_lvl = doi_def->map.std->lvl.local[host_lvl]; |
| 680 | return 0; |
| 681 | } |
| 682 | return -EPERM; |
| 683 | } |
| 684 | |
| 685 | return -EINVAL; |
| 686 | } |
| 687 | |
| 688 | /** |
| 689 | * cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host |
| 690 | * @doi_def: the DOI definition |
| 691 | * @net_lvl: the network/CIPSO MLS level |
| 692 | * @host_lvl: the host MLS level |
| 693 | * |
| 694 | * Description: |
| 695 | * Perform a label mapping to translate a CIPSO level to the correct local MLS |
| 696 | * level using the given DOI definition. Returns zero on success, negative |
| 697 | * values otherwise. |
| 698 | * |
| 699 | */ |
| 700 | static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi *doi_def, |
| 701 | u32 net_lvl, |
| 702 | u32 *host_lvl) |
| 703 | { |
| 704 | struct cipso_v4_std_map_tbl *map_tbl; |
| 705 | |
| 706 | switch (doi_def->type) { |
| 707 | case CIPSO_V4_MAP_PASS: |
| 708 | *host_lvl = net_lvl; |
| 709 | return 0; |
| 710 | case CIPSO_V4_MAP_TRANS: |
| 711 | map_tbl = doi_def->map.std; |
| 712 | if (net_lvl < map_tbl->lvl.cipso_size && |
| 713 | map_tbl->lvl.cipso[net_lvl] < CIPSO_V4_INV_LVL) { |
| 714 | *host_lvl = doi_def->map.std->lvl.cipso[net_lvl]; |
| 715 | return 0; |
| 716 | } |
| 717 | return -EPERM; |
| 718 | } |
| 719 | |
| 720 | return -EINVAL; |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid |
| 725 | * @doi_def: the DOI definition |
| 726 | * @bitmap: category bitmap |
| 727 | * @bitmap_len: bitmap length in bytes |
| 728 | * |
| 729 | * Description: |
| 730 | * Checks the given category bitmap against the given DOI definition and |
| 731 | * returns a negative value if any of the categories in the bitmap do not have |
| 732 | * a valid mapping and a zero value if all of the categories are valid. |
| 733 | * |
| 734 | */ |
| 735 | static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi *doi_def, |
| 736 | const unsigned char *bitmap, |
| 737 | u32 bitmap_len) |
| 738 | { |
| 739 | int cat = -1; |
| 740 | u32 bitmap_len_bits = bitmap_len * 8; |
| 741 | u32 cipso_cat_size; |
| 742 | u32 *cipso_array; |
| 743 | |
| 744 | switch (doi_def->type) { |
| 745 | case CIPSO_V4_MAP_PASS: |
| 746 | return 0; |
| 747 | case CIPSO_V4_MAP_TRANS: |
| 748 | cipso_cat_size = doi_def->map.std->cat.cipso_size; |
| 749 | cipso_array = doi_def->map.std->cat.cipso; |
| 750 | for (;;) { |
| 751 | cat = netlbl_bitmap_walk(bitmap, |
| 752 | bitmap_len_bits, |
| 753 | cat + 1, |
| 754 | 1); |
| 755 | if (cat < 0) |
| 756 | break; |
| 757 | if (cat >= cipso_cat_size || |
| 758 | cipso_array[cat] >= CIPSO_V4_INV_CAT) |
| 759 | return -EFAULT; |
| 760 | } |
| 761 | |
| 762 | if (cat == -1) |
| 763 | return 0; |
| 764 | break; |
| 765 | } |
| 766 | |
| 767 | return -EFAULT; |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network |
| 772 | * @doi_def: the DOI definition |
| 773 | * @secattr: the security attributes |
| 774 | * @net_cat: the zero'd out category bitmap in network/CIPSO format |
| 775 | * @net_cat_len: the length of the CIPSO bitmap in bytes |
| 776 | * |
| 777 | * Description: |
| 778 | * Perform a label mapping to translate a local MLS category bitmap to the |
| 779 | * correct CIPSO bitmap using the given DOI definition. Returns the minimum |
| 780 | * size in bytes of the network bitmap on success, negative values otherwise. |
| 781 | * |
| 782 | */ |
| 783 | static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi *doi_def, |
| 784 | const struct netlbl_lsm_secattr *secattr, |
| 785 | unsigned char *net_cat, |
| 786 | u32 net_cat_len) |
| 787 | { |
| 788 | int host_spot = -1; |
| 789 | u32 net_spot = CIPSO_V4_INV_CAT; |
| 790 | u32 net_spot_max = 0; |
| 791 | u32 net_clen_bits = net_cat_len * 8; |
| 792 | u32 host_cat_size = 0; |
| 793 | u32 *host_cat_array = NULL; |
| 794 | |
| 795 | if (doi_def->type == CIPSO_V4_MAP_TRANS) { |
| 796 | host_cat_size = doi_def->map.std->cat.local_size; |
| 797 | host_cat_array = doi_def->map.std->cat.local; |
| 798 | } |
| 799 | |
| 800 | for (;;) { |
| 801 | host_spot = netlbl_catmap_walk(secattr->attr.mls.cat, |
| 802 | host_spot + 1); |
| 803 | if (host_spot < 0) |
| 804 | break; |
| 805 | |
| 806 | switch (doi_def->type) { |
| 807 | case CIPSO_V4_MAP_PASS: |
| 808 | net_spot = host_spot; |
| 809 | break; |
| 810 | case CIPSO_V4_MAP_TRANS: |
| 811 | if (host_spot >= host_cat_size) |
| 812 | return -EPERM; |
| 813 | net_spot = host_cat_array[host_spot]; |
| 814 | if (net_spot >= CIPSO_V4_INV_CAT) |
| 815 | return -EPERM; |
| 816 | break; |
| 817 | } |
| 818 | if (net_spot >= net_clen_bits) |
| 819 | return -ENOSPC; |
| 820 | netlbl_bitmap_setbit(net_cat, net_spot, 1); |
| 821 | |
| 822 | if (net_spot > net_spot_max) |
| 823 | net_spot_max = net_spot; |
| 824 | } |
| 825 | |
| 826 | if (++net_spot_max % 8) |
| 827 | return net_spot_max / 8 + 1; |
| 828 | return net_spot_max / 8; |
| 829 | } |
| 830 | |
| 831 | /** |
| 832 | * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host |
| 833 | * @doi_def: the DOI definition |
| 834 | * @net_cat: the category bitmap in network/CIPSO format |
| 835 | * @net_cat_len: the length of the CIPSO bitmap in bytes |
| 836 | * @secattr: the security attributes |
| 837 | * |
| 838 | * Description: |
| 839 | * Perform a label mapping to translate a CIPSO bitmap to the correct local |
| 840 | * MLS category bitmap using the given DOI definition. Returns zero on |
| 841 | * success, negative values on failure. |
| 842 | * |
| 843 | */ |
| 844 | static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi *doi_def, |
| 845 | const unsigned char *net_cat, |
| 846 | u32 net_cat_len, |
| 847 | struct netlbl_lsm_secattr *secattr) |
| 848 | { |
| 849 | int ret_val; |
| 850 | int net_spot = -1; |
| 851 | u32 host_spot = CIPSO_V4_INV_CAT; |
| 852 | u32 net_clen_bits = net_cat_len * 8; |
| 853 | u32 net_cat_size = 0; |
| 854 | u32 *net_cat_array = NULL; |
| 855 | |
| 856 | if (doi_def->type == CIPSO_V4_MAP_TRANS) { |
| 857 | net_cat_size = doi_def->map.std->cat.cipso_size; |
| 858 | net_cat_array = doi_def->map.std->cat.cipso; |
| 859 | } |
| 860 | |
| 861 | for (;;) { |
| 862 | net_spot = netlbl_bitmap_walk(net_cat, |
| 863 | net_clen_bits, |
| 864 | net_spot + 1, |
| 865 | 1); |
| 866 | if (net_spot < 0) |
| 867 | return 0; |
| 868 | |
| 869 | switch (doi_def->type) { |
| 870 | case CIPSO_V4_MAP_PASS: |
| 871 | host_spot = net_spot; |
| 872 | break; |
| 873 | case CIPSO_V4_MAP_TRANS: |
| 874 | if (net_spot >= net_cat_size) |
| 875 | return -EPERM; |
| 876 | host_spot = net_cat_array[net_spot]; |
| 877 | if (host_spot >= CIPSO_V4_INV_CAT) |
| 878 | return -EPERM; |
| 879 | break; |
| 880 | } |
| 881 | ret_val = netlbl_catmap_setbit(&secattr->attr.mls.cat, |
| 882 | host_spot, |
| 883 | GFP_ATOMIC); |
| 884 | if (ret_val != 0) |
| 885 | return ret_val; |
| 886 | } |
| 887 | |
| 888 | return -EINVAL; |
| 889 | } |
| 890 | |
| 891 | /** |
| 892 | * cipso_v4_map_cat_enum_valid - Checks to see if the categories are valid |
| 893 | * @doi_def: the DOI definition |
| 894 | * @enumcat: category list |
| 895 | * @enumcat_len: length of the category list in bytes |
| 896 | * |
| 897 | * Description: |
| 898 | * Checks the given categories against the given DOI definition and returns a |
| 899 | * negative value if any of the categories do not have a valid mapping and a |
| 900 | * zero value if all of the categories are valid. |
| 901 | * |
| 902 | */ |
| 903 | static int cipso_v4_map_cat_enum_valid(const struct cipso_v4_doi *doi_def, |
| 904 | const unsigned char *enumcat, |
| 905 | u32 enumcat_len) |
| 906 | { |
| 907 | u16 cat; |
| 908 | int cat_prev = -1; |
| 909 | u32 iter; |
| 910 | |
| 911 | if (doi_def->type != CIPSO_V4_MAP_PASS || enumcat_len & 0x01) |
| 912 | return -EFAULT; |
| 913 | |
| 914 | for (iter = 0; iter < enumcat_len; iter += 2) { |
| 915 | cat = get_unaligned_be16(&enumcat[iter]); |
| 916 | if (cat <= cat_prev) |
| 917 | return -EFAULT; |
| 918 | cat_prev = cat; |
| 919 | } |
| 920 | |
| 921 | return 0; |
| 922 | } |
| 923 | |
| 924 | /** |
| 925 | * cipso_v4_map_cat_enum_hton - Perform a category mapping from host to network |
| 926 | * @doi_def: the DOI definition |
| 927 | * @secattr: the security attributes |
| 928 | * @net_cat: the zero'd out category list in network/CIPSO format |
| 929 | * @net_cat_len: the length of the CIPSO category list in bytes |
| 930 | * |
| 931 | * Description: |
| 932 | * Perform a label mapping to translate a local MLS category bitmap to the |
| 933 | * correct CIPSO category list using the given DOI definition. Returns the |
| 934 | * size in bytes of the network category bitmap on success, negative values |
| 935 | * otherwise. |
| 936 | * |
| 937 | */ |
| 938 | static int cipso_v4_map_cat_enum_hton(const struct cipso_v4_doi *doi_def, |
| 939 | const struct netlbl_lsm_secattr *secattr, |
| 940 | unsigned char *net_cat, |
| 941 | u32 net_cat_len) |
| 942 | { |
| 943 | int cat = -1; |
| 944 | u32 cat_iter = 0; |
| 945 | |
| 946 | for (;;) { |
| 947 | cat = netlbl_catmap_walk(secattr->attr.mls.cat, cat + 1); |
| 948 | if (cat < 0) |
| 949 | break; |
| 950 | if ((cat_iter + 2) > net_cat_len) |
| 951 | return -ENOSPC; |
| 952 | |
| 953 | *((__be16 *)&net_cat[cat_iter]) = htons(cat); |
| 954 | cat_iter += 2; |
| 955 | } |
| 956 | |
| 957 | return cat_iter; |
| 958 | } |
| 959 | |
| 960 | /** |
| 961 | * cipso_v4_map_cat_enum_ntoh - Perform a category mapping from network to host |
| 962 | * @doi_def: the DOI definition |
| 963 | * @net_cat: the category list in network/CIPSO format |
| 964 | * @net_cat_len: the length of the CIPSO bitmap in bytes |
| 965 | * @secattr: the security attributes |
| 966 | * |
| 967 | * Description: |
| 968 | * Perform a label mapping to translate a CIPSO category list to the correct |
| 969 | * local MLS category bitmap using the given DOI definition. Returns zero on |
| 970 | * success, negative values on failure. |
| 971 | * |
| 972 | */ |
| 973 | static int cipso_v4_map_cat_enum_ntoh(const struct cipso_v4_doi *doi_def, |
| 974 | const unsigned char *net_cat, |
| 975 | u32 net_cat_len, |
| 976 | struct netlbl_lsm_secattr *secattr) |
| 977 | { |
| 978 | int ret_val; |
| 979 | u32 iter; |
| 980 | |
| 981 | for (iter = 0; iter < net_cat_len; iter += 2) { |
| 982 | ret_val = netlbl_catmap_setbit(&secattr->attr.mls.cat, |
| 983 | get_unaligned_be16(&net_cat[iter]), |
| 984 | GFP_ATOMIC); |
| 985 | if (ret_val != 0) |
| 986 | return ret_val; |
| 987 | } |
| 988 | |
| 989 | return 0; |
| 990 | } |
| 991 | |
| 992 | /** |
| 993 | * cipso_v4_map_cat_rng_valid - Checks to see if the categories are valid |
| 994 | * @doi_def: the DOI definition |
| 995 | * @rngcat: category list |
| 996 | * @rngcat_len: length of the category list in bytes |
| 997 | * |
| 998 | * Description: |
| 999 | * Checks the given categories against the given DOI definition and returns a |
| 1000 | * negative value if any of the categories do not have a valid mapping and a |
| 1001 | * zero value if all of the categories are valid. |
| 1002 | * |
| 1003 | */ |
| 1004 | static int cipso_v4_map_cat_rng_valid(const struct cipso_v4_doi *doi_def, |
| 1005 | const unsigned char *rngcat, |
| 1006 | u32 rngcat_len) |
| 1007 | { |
| 1008 | u16 cat_high; |
| 1009 | u16 cat_low; |
| 1010 | u32 cat_prev = CIPSO_V4_MAX_REM_CATS + 1; |
| 1011 | u32 iter; |
| 1012 | |
| 1013 | if (doi_def->type != CIPSO_V4_MAP_PASS || rngcat_len & 0x01) |
| 1014 | return -EFAULT; |
| 1015 | |
| 1016 | for (iter = 0; iter < rngcat_len; iter += 4) { |
| 1017 | cat_high = get_unaligned_be16(&rngcat[iter]); |
| 1018 | if ((iter + 4) <= rngcat_len) |
| 1019 | cat_low = get_unaligned_be16(&rngcat[iter + 2]); |
| 1020 | else |
| 1021 | cat_low = 0; |
| 1022 | |
| 1023 | if (cat_high > cat_prev) |
| 1024 | return -EFAULT; |
| 1025 | |
| 1026 | cat_prev = cat_low; |
| 1027 | } |
| 1028 | |
| 1029 | return 0; |
| 1030 | } |
| 1031 | |
| 1032 | /** |
| 1033 | * cipso_v4_map_cat_rng_hton - Perform a category mapping from host to network |
| 1034 | * @doi_def: the DOI definition |
| 1035 | * @secattr: the security attributes |
| 1036 | * @net_cat: the zero'd out category list in network/CIPSO format |
| 1037 | * @net_cat_len: the length of the CIPSO category list in bytes |
| 1038 | * |
| 1039 | * Description: |
| 1040 | * Perform a label mapping to translate a local MLS category bitmap to the |
| 1041 | * correct CIPSO category list using the given DOI definition. Returns the |
| 1042 | * size in bytes of the network category bitmap on success, negative values |
| 1043 | * otherwise. |
| 1044 | * |
| 1045 | */ |
| 1046 | static int cipso_v4_map_cat_rng_hton(const struct cipso_v4_doi *doi_def, |
| 1047 | const struct netlbl_lsm_secattr *secattr, |
| 1048 | unsigned char *net_cat, |
| 1049 | u32 net_cat_len) |
| 1050 | { |
| 1051 | int iter = -1; |
| 1052 | u16 array[CIPSO_V4_TAG_RNG_CAT_MAX * 2]; |
| 1053 | u32 array_cnt = 0; |
| 1054 | u32 cat_size = 0; |
| 1055 | |
| 1056 | /* make sure we don't overflow the 'array[]' variable */ |
| 1057 | if (net_cat_len > |
| 1058 | (CIPSO_V4_OPT_LEN_MAX - CIPSO_V4_HDR_LEN - CIPSO_V4_TAG_RNG_BLEN)) |
| 1059 | return -ENOSPC; |
| 1060 | |
| 1061 | for (;;) { |
| 1062 | iter = netlbl_catmap_walk(secattr->attr.mls.cat, iter + 1); |
| 1063 | if (iter < 0) |
| 1064 | break; |
| 1065 | cat_size += (iter == 0 ? 0 : sizeof(u16)); |
| 1066 | if (cat_size > net_cat_len) |
| 1067 | return -ENOSPC; |
| 1068 | array[array_cnt++] = iter; |
| 1069 | |
| 1070 | iter = netlbl_catmap_walkrng(secattr->attr.mls.cat, iter); |
| 1071 | if (iter < 0) |
| 1072 | return -EFAULT; |
| 1073 | cat_size += sizeof(u16); |
| 1074 | if (cat_size > net_cat_len) |
| 1075 | return -ENOSPC; |
| 1076 | array[array_cnt++] = iter; |
| 1077 | } |
| 1078 | |
| 1079 | for (iter = 0; array_cnt > 0;) { |
| 1080 | *((__be16 *)&net_cat[iter]) = htons(array[--array_cnt]); |
| 1081 | iter += 2; |
| 1082 | array_cnt--; |
| 1083 | if (array[array_cnt] != 0) { |
| 1084 | *((__be16 *)&net_cat[iter]) = htons(array[array_cnt]); |
| 1085 | iter += 2; |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | return cat_size; |
| 1090 | } |
| 1091 | |
| 1092 | /** |
| 1093 | * cipso_v4_map_cat_rng_ntoh - Perform a category mapping from network to host |
| 1094 | * @doi_def: the DOI definition |
| 1095 | * @net_cat: the category list in network/CIPSO format |
| 1096 | * @net_cat_len: the length of the CIPSO bitmap in bytes |
| 1097 | * @secattr: the security attributes |
| 1098 | * |
| 1099 | * Description: |
| 1100 | * Perform a label mapping to translate a CIPSO category list to the correct |
| 1101 | * local MLS category bitmap using the given DOI definition. Returns zero on |
| 1102 | * success, negative values on failure. |
| 1103 | * |
| 1104 | */ |
| 1105 | static int cipso_v4_map_cat_rng_ntoh(const struct cipso_v4_doi *doi_def, |
| 1106 | const unsigned char *net_cat, |
| 1107 | u32 net_cat_len, |
| 1108 | struct netlbl_lsm_secattr *secattr) |
| 1109 | { |
| 1110 | int ret_val; |
| 1111 | u32 net_iter; |
| 1112 | u16 cat_low; |
| 1113 | u16 cat_high; |
| 1114 | |
| 1115 | for (net_iter = 0; net_iter < net_cat_len; net_iter += 4) { |
| 1116 | cat_high = get_unaligned_be16(&net_cat[net_iter]); |
| 1117 | if ((net_iter + 4) <= net_cat_len) |
| 1118 | cat_low = get_unaligned_be16(&net_cat[net_iter + 2]); |
| 1119 | else |
| 1120 | cat_low = 0; |
| 1121 | |
| 1122 | ret_val = netlbl_catmap_setrng(&secattr->attr.mls.cat, |
| 1123 | cat_low, |
| 1124 | cat_high, |
| 1125 | GFP_ATOMIC); |
| 1126 | if (ret_val != 0) |
| 1127 | return ret_val; |
| 1128 | } |
| 1129 | |
| 1130 | return 0; |
| 1131 | } |
| 1132 | |
| 1133 | /* |
| 1134 | * Protocol Handling Functions |
| 1135 | */ |
| 1136 | |
| 1137 | /** |
| 1138 | * cipso_v4_gentag_hdr - Generate a CIPSO option header |
| 1139 | * @doi_def: the DOI definition |
| 1140 | * @len: the total tag length in bytes, not including this header |
| 1141 | * @buf: the CIPSO option buffer |
| 1142 | * |
| 1143 | * Description: |
| 1144 | * Write a CIPSO header into the beginning of @buffer. |
| 1145 | * |
| 1146 | */ |
| 1147 | static void cipso_v4_gentag_hdr(const struct cipso_v4_doi *doi_def, |
| 1148 | unsigned char *buf, |
| 1149 | u32 len) |
| 1150 | { |
| 1151 | buf[0] = IPOPT_CIPSO; |
| 1152 | buf[1] = CIPSO_V4_HDR_LEN + len; |
| 1153 | put_unaligned_be32(doi_def->doi, &buf[2]); |
| 1154 | } |
| 1155 | |
| 1156 | /** |
| 1157 | * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1) |
| 1158 | * @doi_def: the DOI definition |
| 1159 | * @secattr: the security attributes |
| 1160 | * @buffer: the option buffer |
| 1161 | * @buffer_len: length of buffer in bytes |
| 1162 | * |
| 1163 | * Description: |
| 1164 | * Generate a CIPSO option using the restricted bitmap tag, tag type #1. The |
| 1165 | * actual buffer length may be larger than the indicated size due to |
| 1166 | * translation between host and network category bitmaps. Returns the size of |
| 1167 | * the tag on success, negative values on failure. |
| 1168 | * |
| 1169 | */ |
| 1170 | static int cipso_v4_gentag_rbm(const struct cipso_v4_doi *doi_def, |
| 1171 | const struct netlbl_lsm_secattr *secattr, |
| 1172 | unsigned char *buffer, |
| 1173 | u32 buffer_len) |
| 1174 | { |
| 1175 | int ret_val; |
| 1176 | u32 tag_len; |
| 1177 | u32 level; |
| 1178 | |
| 1179 | if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0) |
| 1180 | return -EPERM; |
| 1181 | |
| 1182 | ret_val = cipso_v4_map_lvl_hton(doi_def, |
| 1183 | secattr->attr.mls.lvl, |
| 1184 | &level); |
| 1185 | if (ret_val != 0) |
| 1186 | return ret_val; |
| 1187 | |
| 1188 | if (secattr->flags & NETLBL_SECATTR_MLS_CAT) { |
| 1189 | ret_val = cipso_v4_map_cat_rbm_hton(doi_def, |
| 1190 | secattr, |
| 1191 | &buffer[4], |
| 1192 | buffer_len - 4); |
| 1193 | if (ret_val < 0) |
| 1194 | return ret_val; |
| 1195 | |
| 1196 | /* This will send packets using the "optimized" format when |
| 1197 | * possible as specified in section 3.4.2.6 of the |
| 1198 | * CIPSO draft. */ |
| 1199 | if (READ_ONCE(cipso_v4_rbm_optfmt) && ret_val > 0 && |
| 1200 | ret_val <= 10) |
| 1201 | tag_len = 14; |
| 1202 | else |
| 1203 | tag_len = 4 + ret_val; |
| 1204 | } else |
| 1205 | tag_len = 4; |
| 1206 | |
| 1207 | buffer[0] = CIPSO_V4_TAG_RBITMAP; |
| 1208 | buffer[1] = tag_len; |
| 1209 | buffer[3] = level; |
| 1210 | |
| 1211 | return tag_len; |
| 1212 | } |
| 1213 | |
| 1214 | /** |
| 1215 | * cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag |
| 1216 | * @doi_def: the DOI definition |
| 1217 | * @tag: the CIPSO tag |
| 1218 | * @secattr: the security attributes |
| 1219 | * |
| 1220 | * Description: |
| 1221 | * Parse a CIPSO restricted bitmap tag (tag type #1) and return the security |
| 1222 | * attributes in @secattr. Return zero on success, negatives values on |
| 1223 | * failure. |
| 1224 | * |
| 1225 | */ |
| 1226 | static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi *doi_def, |
| 1227 | const unsigned char *tag, |
| 1228 | struct netlbl_lsm_secattr *secattr) |
| 1229 | { |
| 1230 | int ret_val; |
| 1231 | u8 tag_len = tag[1]; |
| 1232 | u32 level; |
| 1233 | |
| 1234 | ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level); |
| 1235 | if (ret_val != 0) |
| 1236 | return ret_val; |
| 1237 | secattr->attr.mls.lvl = level; |
| 1238 | secattr->flags |= NETLBL_SECATTR_MLS_LVL; |
| 1239 | |
| 1240 | if (tag_len > 4) { |
| 1241 | ret_val = cipso_v4_map_cat_rbm_ntoh(doi_def, |
| 1242 | &tag[4], |
| 1243 | tag_len - 4, |
| 1244 | secattr); |
| 1245 | if (ret_val != 0) { |
| 1246 | netlbl_catmap_free(secattr->attr.mls.cat); |
| 1247 | return ret_val; |
| 1248 | } |
| 1249 | |
| 1250 | if (secattr->attr.mls.cat) |
| 1251 | secattr->flags |= NETLBL_SECATTR_MLS_CAT; |
| 1252 | } |
| 1253 | |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
| 1257 | /** |
| 1258 | * cipso_v4_gentag_enum - Generate a CIPSO enumerated tag (type #2) |
| 1259 | * @doi_def: the DOI definition |
| 1260 | * @secattr: the security attributes |
| 1261 | * @buffer: the option buffer |
| 1262 | * @buffer_len: length of buffer in bytes |
| 1263 | * |
| 1264 | * Description: |
| 1265 | * Generate a CIPSO option using the enumerated tag, tag type #2. Returns the |
| 1266 | * size of the tag on success, negative values on failure. |
| 1267 | * |
| 1268 | */ |
| 1269 | static int cipso_v4_gentag_enum(const struct cipso_v4_doi *doi_def, |
| 1270 | const struct netlbl_lsm_secattr *secattr, |
| 1271 | unsigned char *buffer, |
| 1272 | u32 buffer_len) |
| 1273 | { |
| 1274 | int ret_val; |
| 1275 | u32 tag_len; |
| 1276 | u32 level; |
| 1277 | |
| 1278 | if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL)) |
| 1279 | return -EPERM; |
| 1280 | |
| 1281 | ret_val = cipso_v4_map_lvl_hton(doi_def, |
| 1282 | secattr->attr.mls.lvl, |
| 1283 | &level); |
| 1284 | if (ret_val != 0) |
| 1285 | return ret_val; |
| 1286 | |
| 1287 | if (secattr->flags & NETLBL_SECATTR_MLS_CAT) { |
| 1288 | ret_val = cipso_v4_map_cat_enum_hton(doi_def, |
| 1289 | secattr, |
| 1290 | &buffer[4], |
| 1291 | buffer_len - 4); |
| 1292 | if (ret_val < 0) |
| 1293 | return ret_val; |
| 1294 | |
| 1295 | tag_len = 4 + ret_val; |
| 1296 | } else |
| 1297 | tag_len = 4; |
| 1298 | |
| 1299 | buffer[0] = CIPSO_V4_TAG_ENUM; |
| 1300 | buffer[1] = tag_len; |
| 1301 | buffer[3] = level; |
| 1302 | |
| 1303 | return tag_len; |
| 1304 | } |
| 1305 | |
| 1306 | /** |
| 1307 | * cipso_v4_parsetag_enum - Parse a CIPSO enumerated tag |
| 1308 | * @doi_def: the DOI definition |
| 1309 | * @tag: the CIPSO tag |
| 1310 | * @secattr: the security attributes |
| 1311 | * |
| 1312 | * Description: |
| 1313 | * Parse a CIPSO enumerated tag (tag type #2) and return the security |
| 1314 | * attributes in @secattr. Return zero on success, negatives values on |
| 1315 | * failure. |
| 1316 | * |
| 1317 | */ |
| 1318 | static int cipso_v4_parsetag_enum(const struct cipso_v4_doi *doi_def, |
| 1319 | const unsigned char *tag, |
| 1320 | struct netlbl_lsm_secattr *secattr) |
| 1321 | { |
| 1322 | int ret_val; |
| 1323 | u8 tag_len = tag[1]; |
| 1324 | u32 level; |
| 1325 | |
| 1326 | ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level); |
| 1327 | if (ret_val != 0) |
| 1328 | return ret_val; |
| 1329 | secattr->attr.mls.lvl = level; |
| 1330 | secattr->flags |= NETLBL_SECATTR_MLS_LVL; |
| 1331 | |
| 1332 | if (tag_len > 4) { |
| 1333 | ret_val = cipso_v4_map_cat_enum_ntoh(doi_def, |
| 1334 | &tag[4], |
| 1335 | tag_len - 4, |
| 1336 | secattr); |
| 1337 | if (ret_val != 0) { |
| 1338 | netlbl_catmap_free(secattr->attr.mls.cat); |
| 1339 | return ret_val; |
| 1340 | } |
| 1341 | |
| 1342 | secattr->flags |= NETLBL_SECATTR_MLS_CAT; |
| 1343 | } |
| 1344 | |
| 1345 | return 0; |
| 1346 | } |
| 1347 | |
| 1348 | /** |
| 1349 | * cipso_v4_gentag_rng - Generate a CIPSO ranged tag (type #5) |
| 1350 | * @doi_def: the DOI definition |
| 1351 | * @secattr: the security attributes |
| 1352 | * @buffer: the option buffer |
| 1353 | * @buffer_len: length of buffer in bytes |
| 1354 | * |
| 1355 | * Description: |
| 1356 | * Generate a CIPSO option using the ranged tag, tag type #5. Returns the |
| 1357 | * size of the tag on success, negative values on failure. |
| 1358 | * |
| 1359 | */ |
| 1360 | static int cipso_v4_gentag_rng(const struct cipso_v4_doi *doi_def, |
| 1361 | const struct netlbl_lsm_secattr *secattr, |
| 1362 | unsigned char *buffer, |
| 1363 | u32 buffer_len) |
| 1364 | { |
| 1365 | int ret_val; |
| 1366 | u32 tag_len; |
| 1367 | u32 level; |
| 1368 | |
| 1369 | if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL)) |
| 1370 | return -EPERM; |
| 1371 | |
| 1372 | ret_val = cipso_v4_map_lvl_hton(doi_def, |
| 1373 | secattr->attr.mls.lvl, |
| 1374 | &level); |
| 1375 | if (ret_val != 0) |
| 1376 | return ret_val; |
| 1377 | |
| 1378 | if (secattr->flags & NETLBL_SECATTR_MLS_CAT) { |
| 1379 | ret_val = cipso_v4_map_cat_rng_hton(doi_def, |
| 1380 | secattr, |
| 1381 | &buffer[4], |
| 1382 | buffer_len - 4); |
| 1383 | if (ret_val < 0) |
| 1384 | return ret_val; |
| 1385 | |
| 1386 | tag_len = 4 + ret_val; |
| 1387 | } else |
| 1388 | tag_len = 4; |
| 1389 | |
| 1390 | buffer[0] = CIPSO_V4_TAG_RANGE; |
| 1391 | buffer[1] = tag_len; |
| 1392 | buffer[3] = level; |
| 1393 | |
| 1394 | return tag_len; |
| 1395 | } |
| 1396 | |
| 1397 | /** |
| 1398 | * cipso_v4_parsetag_rng - Parse a CIPSO ranged tag |
| 1399 | * @doi_def: the DOI definition |
| 1400 | * @tag: the CIPSO tag |
| 1401 | * @secattr: the security attributes |
| 1402 | * |
| 1403 | * Description: |
| 1404 | * Parse a CIPSO ranged tag (tag type #5) and return the security attributes |
| 1405 | * in @secattr. Return zero on success, negatives values on failure. |
| 1406 | * |
| 1407 | */ |
| 1408 | static int cipso_v4_parsetag_rng(const struct cipso_v4_doi *doi_def, |
| 1409 | const unsigned char *tag, |
| 1410 | struct netlbl_lsm_secattr *secattr) |
| 1411 | { |
| 1412 | int ret_val; |
| 1413 | u8 tag_len = tag[1]; |
| 1414 | u32 level; |
| 1415 | |
| 1416 | ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level); |
| 1417 | if (ret_val != 0) |
| 1418 | return ret_val; |
| 1419 | secattr->attr.mls.lvl = level; |
| 1420 | secattr->flags |= NETLBL_SECATTR_MLS_LVL; |
| 1421 | |
| 1422 | if (tag_len > 4) { |
| 1423 | ret_val = cipso_v4_map_cat_rng_ntoh(doi_def, |
| 1424 | &tag[4], |
| 1425 | tag_len - 4, |
| 1426 | secattr); |
| 1427 | if (ret_val != 0) { |
| 1428 | netlbl_catmap_free(secattr->attr.mls.cat); |
| 1429 | return ret_val; |
| 1430 | } |
| 1431 | |
| 1432 | if (secattr->attr.mls.cat) |
| 1433 | secattr->flags |= NETLBL_SECATTR_MLS_CAT; |
| 1434 | } |
| 1435 | |
| 1436 | return 0; |
| 1437 | } |
| 1438 | |
| 1439 | /** |
| 1440 | * cipso_v4_gentag_loc - Generate a CIPSO local tag (non-standard) |
| 1441 | * @doi_def: the DOI definition |
| 1442 | * @secattr: the security attributes |
| 1443 | * @buffer: the option buffer |
| 1444 | * @buffer_len: length of buffer in bytes |
| 1445 | * |
| 1446 | * Description: |
| 1447 | * Generate a CIPSO option using the local tag. Returns the size of the tag |
| 1448 | * on success, negative values on failure. |
| 1449 | * |
| 1450 | */ |
| 1451 | static int cipso_v4_gentag_loc(const struct cipso_v4_doi *doi_def, |
| 1452 | const struct netlbl_lsm_secattr *secattr, |
| 1453 | unsigned char *buffer, |
| 1454 | u32 buffer_len) |
| 1455 | { |
| 1456 | if (!(secattr->flags & NETLBL_SECATTR_SECID)) |
| 1457 | return -EPERM; |
| 1458 | |
| 1459 | buffer[0] = CIPSO_V4_TAG_LOCAL; |
| 1460 | buffer[1] = CIPSO_V4_TAG_LOC_BLEN; |
| 1461 | *(u32 *)&buffer[2] = secattr->attr.secid; |
| 1462 | |
| 1463 | return CIPSO_V4_TAG_LOC_BLEN; |
| 1464 | } |
| 1465 | |
| 1466 | /** |
| 1467 | * cipso_v4_parsetag_loc - Parse a CIPSO local tag |
| 1468 | * @doi_def: the DOI definition |
| 1469 | * @tag: the CIPSO tag |
| 1470 | * @secattr: the security attributes |
| 1471 | * |
| 1472 | * Description: |
| 1473 | * Parse a CIPSO local tag and return the security attributes in @secattr. |
| 1474 | * Return zero on success, negatives values on failure. |
| 1475 | * |
| 1476 | */ |
| 1477 | static int cipso_v4_parsetag_loc(const struct cipso_v4_doi *doi_def, |
| 1478 | const unsigned char *tag, |
| 1479 | struct netlbl_lsm_secattr *secattr) |
| 1480 | { |
| 1481 | secattr->attr.secid = *(u32 *)&tag[2]; |
| 1482 | secattr->flags |= NETLBL_SECATTR_SECID; |
| 1483 | |
| 1484 | return 0; |
| 1485 | } |
| 1486 | |
| 1487 | /** |
| 1488 | * cipso_v4_optptr - Find the CIPSO option in the packet |
| 1489 | * @skb: the packet |
| 1490 | * |
| 1491 | * Description: |
| 1492 | * Parse the packet's IP header looking for a CIPSO option. Returns a pointer |
| 1493 | * to the start of the CIPSO option on success, NULL if one is not found. |
| 1494 | * |
| 1495 | */ |
| 1496 | unsigned char *cipso_v4_optptr(const struct sk_buff *skb) |
| 1497 | { |
| 1498 | const struct iphdr *iph = ip_hdr(skb); |
| 1499 | unsigned char *optptr = (unsigned char *)&(ip_hdr(skb)[1]); |
| 1500 | int optlen; |
| 1501 | int taglen; |
| 1502 | |
| 1503 | for (optlen = iph->ihl*4 - sizeof(struct iphdr); optlen > 1; ) { |
| 1504 | switch (optptr[0]) { |
| 1505 | case IPOPT_END: |
| 1506 | return NULL; |
| 1507 | case IPOPT_NOOP: |
| 1508 | taglen = 1; |
| 1509 | break; |
| 1510 | default: |
| 1511 | taglen = optptr[1]; |
| 1512 | } |
| 1513 | if (!taglen || taglen > optlen) |
| 1514 | return NULL; |
| 1515 | if (optptr[0] == IPOPT_CIPSO) |
| 1516 | return optptr; |
| 1517 | |
| 1518 | optlen -= taglen; |
| 1519 | optptr += taglen; |
| 1520 | } |
| 1521 | |
| 1522 | return NULL; |
| 1523 | } |
| 1524 | |
| 1525 | /** |
| 1526 | * cipso_v4_validate - Validate a CIPSO option |
| 1527 | * @skb: the packet |
| 1528 | * @option: the start of the option, on error it is set to point to the error |
| 1529 | * |
| 1530 | * Description: |
| 1531 | * This routine is called to validate a CIPSO option, it checks all of the |
| 1532 | * fields to ensure that they are at least valid, see the draft snippet below |
| 1533 | * for details. If the option is valid then a zero value is returned and |
| 1534 | * the value of @option is unchanged. If the option is invalid then a |
| 1535 | * non-zero value is returned and @option is adjusted to point to the |
| 1536 | * offending portion of the option. From the IETF draft ... |
| 1537 | * |
| 1538 | * "If any field within the CIPSO options, such as the DOI identifier, is not |
| 1539 | * recognized the IP datagram is discarded and an ICMP 'parameter problem' |
| 1540 | * (type 12) is generated and returned. The ICMP code field is set to 'bad |
| 1541 | * parameter' (code 0) and the pointer is set to the start of the CIPSO field |
| 1542 | * that is unrecognized." |
| 1543 | * |
| 1544 | */ |
| 1545 | int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option) |
| 1546 | { |
| 1547 | unsigned char *opt = *option; |
| 1548 | unsigned char *tag; |
| 1549 | unsigned char opt_iter; |
| 1550 | unsigned char err_offset = 0; |
| 1551 | u8 opt_len; |
| 1552 | u8 tag_len; |
| 1553 | struct cipso_v4_doi *doi_def = NULL; |
| 1554 | u32 tag_iter; |
| 1555 | |
| 1556 | /* caller already checks for length values that are too large */ |
| 1557 | opt_len = opt[1]; |
| 1558 | if (opt_len < 8) { |
| 1559 | err_offset = 1; |
| 1560 | goto validate_return; |
| 1561 | } |
| 1562 | |
| 1563 | rcu_read_lock(); |
| 1564 | doi_def = cipso_v4_doi_search(get_unaligned_be32(&opt[2])); |
| 1565 | if (!doi_def) { |
| 1566 | err_offset = 2; |
| 1567 | goto validate_return_locked; |
| 1568 | } |
| 1569 | |
| 1570 | opt_iter = CIPSO_V4_HDR_LEN; |
| 1571 | tag = opt + opt_iter; |
| 1572 | while (opt_iter < opt_len) { |
| 1573 | for (tag_iter = 0; doi_def->tags[tag_iter] != tag[0];) |
| 1574 | if (doi_def->tags[tag_iter] == CIPSO_V4_TAG_INVALID || |
| 1575 | ++tag_iter == CIPSO_V4_TAG_MAXCNT) { |
| 1576 | err_offset = opt_iter; |
| 1577 | goto validate_return_locked; |
| 1578 | } |
| 1579 | |
| 1580 | if (opt_iter + 1 == opt_len) { |
| 1581 | err_offset = opt_iter; |
| 1582 | goto validate_return_locked; |
| 1583 | } |
| 1584 | tag_len = tag[1]; |
| 1585 | if (tag_len > (opt_len - opt_iter)) { |
| 1586 | err_offset = opt_iter + 1; |
| 1587 | goto validate_return_locked; |
| 1588 | } |
| 1589 | |
| 1590 | switch (tag[0]) { |
| 1591 | case CIPSO_V4_TAG_RBITMAP: |
| 1592 | if (tag_len < CIPSO_V4_TAG_RBM_BLEN) { |
| 1593 | err_offset = opt_iter + 1; |
| 1594 | goto validate_return_locked; |
| 1595 | } |
| 1596 | |
| 1597 | /* We are already going to do all the verification |
| 1598 | * necessary at the socket layer so from our point of |
| 1599 | * view it is safe to turn these checks off (and less |
| 1600 | * work), however, the CIPSO draft says we should do |
| 1601 | * all the CIPSO validations here but it doesn't |
| 1602 | * really specify _exactly_ what we need to validate |
| 1603 | * ... so, just make it a sysctl tunable. */ |
| 1604 | if (READ_ONCE(cipso_v4_rbm_strictvalid)) { |
| 1605 | if (cipso_v4_map_lvl_valid(doi_def, |
| 1606 | tag[3]) < 0) { |
| 1607 | err_offset = opt_iter + 3; |
| 1608 | goto validate_return_locked; |
| 1609 | } |
| 1610 | if (tag_len > CIPSO_V4_TAG_RBM_BLEN && |
| 1611 | cipso_v4_map_cat_rbm_valid(doi_def, |
| 1612 | &tag[4], |
| 1613 | tag_len - 4) < 0) { |
| 1614 | err_offset = opt_iter + 4; |
| 1615 | goto validate_return_locked; |
| 1616 | } |
| 1617 | } |
| 1618 | break; |
| 1619 | case CIPSO_V4_TAG_ENUM: |
| 1620 | if (tag_len < CIPSO_V4_TAG_ENUM_BLEN) { |
| 1621 | err_offset = opt_iter + 1; |
| 1622 | goto validate_return_locked; |
| 1623 | } |
| 1624 | |
| 1625 | if (cipso_v4_map_lvl_valid(doi_def, |
| 1626 | tag[3]) < 0) { |
| 1627 | err_offset = opt_iter + 3; |
| 1628 | goto validate_return_locked; |
| 1629 | } |
| 1630 | if (tag_len > CIPSO_V4_TAG_ENUM_BLEN && |
| 1631 | cipso_v4_map_cat_enum_valid(doi_def, |
| 1632 | &tag[4], |
| 1633 | tag_len - 4) < 0) { |
| 1634 | err_offset = opt_iter + 4; |
| 1635 | goto validate_return_locked; |
| 1636 | } |
| 1637 | break; |
| 1638 | case CIPSO_V4_TAG_RANGE: |
| 1639 | if (tag_len < CIPSO_V4_TAG_RNG_BLEN) { |
| 1640 | err_offset = opt_iter + 1; |
| 1641 | goto validate_return_locked; |
| 1642 | } |
| 1643 | |
| 1644 | if (cipso_v4_map_lvl_valid(doi_def, |
| 1645 | tag[3]) < 0) { |
| 1646 | err_offset = opt_iter + 3; |
| 1647 | goto validate_return_locked; |
| 1648 | } |
| 1649 | if (tag_len > CIPSO_V4_TAG_RNG_BLEN && |
| 1650 | cipso_v4_map_cat_rng_valid(doi_def, |
| 1651 | &tag[4], |
| 1652 | tag_len - 4) < 0) { |
| 1653 | err_offset = opt_iter + 4; |
| 1654 | goto validate_return_locked; |
| 1655 | } |
| 1656 | break; |
| 1657 | case CIPSO_V4_TAG_LOCAL: |
| 1658 | /* This is a non-standard tag that we only allow for |
| 1659 | * local connections, so if the incoming interface is |
| 1660 | * not the loopback device drop the packet. Further, |
| 1661 | * there is no legitimate reason for setting this from |
| 1662 | * userspace so reject it if skb is NULL. */ |
| 1663 | if (!skb || !(skb->dev->flags & IFF_LOOPBACK)) { |
| 1664 | err_offset = opt_iter; |
| 1665 | goto validate_return_locked; |
| 1666 | } |
| 1667 | if (tag_len != CIPSO_V4_TAG_LOC_BLEN) { |
| 1668 | err_offset = opt_iter + 1; |
| 1669 | goto validate_return_locked; |
| 1670 | } |
| 1671 | break; |
| 1672 | default: |
| 1673 | err_offset = opt_iter; |
| 1674 | goto validate_return_locked; |
| 1675 | } |
| 1676 | |
| 1677 | tag += tag_len; |
| 1678 | opt_iter += tag_len; |
| 1679 | } |
| 1680 | |
| 1681 | validate_return_locked: |
| 1682 | rcu_read_unlock(); |
| 1683 | validate_return: |
| 1684 | *option = opt + err_offset; |
| 1685 | return err_offset; |
| 1686 | } |
| 1687 | |
| 1688 | /** |
| 1689 | * cipso_v4_error - Send the correct response for a bad packet |
| 1690 | * @skb: the packet |
| 1691 | * @error: the error code |
| 1692 | * @gateway: CIPSO gateway flag |
| 1693 | * |
| 1694 | * Description: |
| 1695 | * Based on the error code given in @error, send an ICMP error message back to |
| 1696 | * the originating host. From the IETF draft ... |
| 1697 | * |
| 1698 | * "If the contents of the CIPSO [option] are valid but the security label is |
| 1699 | * outside of the configured host or port label range, the datagram is |
| 1700 | * discarded and an ICMP 'destination unreachable' (type 3) is generated and |
| 1701 | * returned. The code field of the ICMP is set to 'communication with |
| 1702 | * destination network administratively prohibited' (code 9) or to |
| 1703 | * 'communication with destination host administratively prohibited' |
| 1704 | * (code 10). The value of the code is dependent on whether the originator |
| 1705 | * of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The |
| 1706 | * recipient of the ICMP message MUST be able to handle either value. The |
| 1707 | * same procedure is performed if a CIPSO [option] can not be added to an |
| 1708 | * IP packet because it is too large to fit in the IP options area." |
| 1709 | * |
| 1710 | * "If the error is triggered by receipt of an ICMP message, the message is |
| 1711 | * discarded and no response is permitted (consistent with general ICMP |
| 1712 | * processing rules)." |
| 1713 | * |
| 1714 | */ |
| 1715 | void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway) |
| 1716 | { |
| 1717 | struct inet_skb_parm parm; |
| 1718 | int res; |
| 1719 | |
| 1720 | if (ip_hdr(skb)->protocol == IPPROTO_ICMP || error != -EACCES) |
| 1721 | return; |
| 1722 | |
| 1723 | /* |
| 1724 | * We might be called above the IP layer, |
| 1725 | * so we can not use icmp_send and IPCB here. |
| 1726 | */ |
| 1727 | |
| 1728 | memset(&parm, 0, sizeof(parm)); |
| 1729 | parm.opt.optlen = ip_hdr(skb)->ihl * 4 - sizeof(struct iphdr); |
| 1730 | rcu_read_lock(); |
| 1731 | res = __ip_options_compile(dev_net(skb->dev), &parm.opt, skb, NULL); |
| 1732 | rcu_read_unlock(); |
| 1733 | |
| 1734 | if (res) |
| 1735 | return; |
| 1736 | |
| 1737 | if (gateway) |
| 1738 | __icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_ANO, 0, &parm); |
| 1739 | else |
| 1740 | __icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_ANO, 0, &parm); |
| 1741 | } |
| 1742 | |
| 1743 | /** |
| 1744 | * cipso_v4_genopt - Generate a CIPSO option |
| 1745 | * @buf: the option buffer |
| 1746 | * @buf_len: the size of opt_buf |
| 1747 | * @doi_def: the CIPSO DOI to use |
| 1748 | * @secattr: the security attributes |
| 1749 | * |
| 1750 | * Description: |
| 1751 | * Generate a CIPSO option using the DOI definition and security attributes |
| 1752 | * passed to the function. Returns the length of the option on success and |
| 1753 | * negative values on failure. |
| 1754 | * |
| 1755 | */ |
| 1756 | static int cipso_v4_genopt(unsigned char *buf, u32 buf_len, |
| 1757 | const struct cipso_v4_doi *doi_def, |
| 1758 | const struct netlbl_lsm_secattr *secattr) |
| 1759 | { |
| 1760 | int ret_val; |
| 1761 | u32 iter; |
| 1762 | |
| 1763 | if (buf_len <= CIPSO_V4_HDR_LEN) |
| 1764 | return -ENOSPC; |
| 1765 | |
| 1766 | /* XXX - This code assumes only one tag per CIPSO option which isn't |
| 1767 | * really a good assumption to make but since we only support the MAC |
| 1768 | * tags right now it is a safe assumption. */ |
| 1769 | iter = 0; |
| 1770 | do { |
| 1771 | memset(buf, 0, buf_len); |
| 1772 | switch (doi_def->tags[iter]) { |
| 1773 | case CIPSO_V4_TAG_RBITMAP: |
| 1774 | ret_val = cipso_v4_gentag_rbm(doi_def, |
| 1775 | secattr, |
| 1776 | &buf[CIPSO_V4_HDR_LEN], |
| 1777 | buf_len - CIPSO_V4_HDR_LEN); |
| 1778 | break; |
| 1779 | case CIPSO_V4_TAG_ENUM: |
| 1780 | ret_val = cipso_v4_gentag_enum(doi_def, |
| 1781 | secattr, |
| 1782 | &buf[CIPSO_V4_HDR_LEN], |
| 1783 | buf_len - CIPSO_V4_HDR_LEN); |
| 1784 | break; |
| 1785 | case CIPSO_V4_TAG_RANGE: |
| 1786 | ret_val = cipso_v4_gentag_rng(doi_def, |
| 1787 | secattr, |
| 1788 | &buf[CIPSO_V4_HDR_LEN], |
| 1789 | buf_len - CIPSO_V4_HDR_LEN); |
| 1790 | break; |
| 1791 | case CIPSO_V4_TAG_LOCAL: |
| 1792 | ret_val = cipso_v4_gentag_loc(doi_def, |
| 1793 | secattr, |
| 1794 | &buf[CIPSO_V4_HDR_LEN], |
| 1795 | buf_len - CIPSO_V4_HDR_LEN); |
| 1796 | break; |
| 1797 | default: |
| 1798 | return -EPERM; |
| 1799 | } |
| 1800 | |
| 1801 | iter++; |
| 1802 | } while (ret_val < 0 && |
| 1803 | iter < CIPSO_V4_TAG_MAXCNT && |
| 1804 | doi_def->tags[iter] != CIPSO_V4_TAG_INVALID); |
| 1805 | if (ret_val < 0) |
| 1806 | return ret_val; |
| 1807 | cipso_v4_gentag_hdr(doi_def, buf, ret_val); |
| 1808 | return CIPSO_V4_HDR_LEN + ret_val; |
| 1809 | } |
| 1810 | |
| 1811 | static int cipso_v4_get_actual_opt_len(const unsigned char *data, int len) |
| 1812 | { |
| 1813 | int iter = 0, optlen = 0; |
| 1814 | |
| 1815 | /* determining the new total option length is tricky because of |
| 1816 | * the padding necessary, the only thing i can think to do at |
| 1817 | * this point is walk the options one-by-one, skipping the |
| 1818 | * padding at the end to determine the actual option size and |
| 1819 | * from there we can determine the new total option length |
| 1820 | */ |
| 1821 | while (iter < len) { |
| 1822 | if (data[iter] == IPOPT_END) { |
| 1823 | break; |
| 1824 | } else if (data[iter] == IPOPT_NOP) { |
| 1825 | iter++; |
| 1826 | } else { |
| 1827 | iter += data[iter + 1]; |
| 1828 | optlen = iter; |
| 1829 | } |
| 1830 | } |
| 1831 | return optlen; |
| 1832 | } |
| 1833 | |
| 1834 | /** |
| 1835 | * cipso_v4_sock_setattr - Add a CIPSO option to a socket |
| 1836 | * @sk: the socket |
| 1837 | * @doi_def: the CIPSO DOI to use |
| 1838 | * @secattr: the specific security attributes of the socket |
| 1839 | * @sk_locked: true if caller holds the socket lock |
| 1840 | * |
| 1841 | * Description: |
| 1842 | * Set the CIPSO option on the given socket using the DOI definition and |
| 1843 | * security attributes passed to the function. This function requires |
| 1844 | * exclusive access to @sk, which means it either needs to be in the |
| 1845 | * process of being created or locked. Returns zero on success and negative |
| 1846 | * values on failure. |
| 1847 | * |
| 1848 | */ |
| 1849 | int cipso_v4_sock_setattr(struct sock *sk, |
| 1850 | const struct cipso_v4_doi *doi_def, |
| 1851 | const struct netlbl_lsm_secattr *secattr, |
| 1852 | bool sk_locked) |
| 1853 | { |
| 1854 | int ret_val = -EPERM; |
| 1855 | unsigned char *buf = NULL; |
| 1856 | u32 buf_len; |
| 1857 | u32 opt_len; |
| 1858 | struct ip_options_rcu *old, *opt = NULL; |
| 1859 | struct inet_sock *sk_inet; |
| 1860 | struct inet_connection_sock *sk_conn; |
| 1861 | |
| 1862 | /* In the case of sock_create_lite(), the sock->sk field is not |
| 1863 | * defined yet but it is not a problem as the only users of these |
| 1864 | * "lite" PF_INET sockets are functions which do an accept() call |
| 1865 | * afterwards so we will label the socket as part of the accept(). */ |
| 1866 | if (!sk) |
| 1867 | return 0; |
| 1868 | |
| 1869 | /* We allocate the maximum CIPSO option size here so we are probably |
| 1870 | * being a little wasteful, but it makes our life _much_ easier later |
| 1871 | * on and after all we are only talking about 40 bytes. */ |
| 1872 | buf_len = CIPSO_V4_OPT_LEN_MAX; |
| 1873 | buf = kmalloc(buf_len, GFP_ATOMIC); |
| 1874 | if (!buf) { |
| 1875 | ret_val = -ENOMEM; |
| 1876 | goto socket_setattr_failure; |
| 1877 | } |
| 1878 | |
| 1879 | ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr); |
| 1880 | if (ret_val < 0) |
| 1881 | goto socket_setattr_failure; |
| 1882 | buf_len = ret_val; |
| 1883 | |
| 1884 | /* We can't use ip_options_get() directly because it makes a call to |
| 1885 | * ip_options_get_alloc() which allocates memory with GFP_KERNEL and |
| 1886 | * we won't always have CAP_NET_RAW even though we _always_ want to |
| 1887 | * set the IPOPT_CIPSO option. */ |
| 1888 | opt_len = (buf_len + 3) & ~3; |
| 1889 | opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC); |
| 1890 | if (!opt) { |
| 1891 | ret_val = -ENOMEM; |
| 1892 | goto socket_setattr_failure; |
| 1893 | } |
| 1894 | memcpy(opt->opt.__data, buf, buf_len); |
| 1895 | opt->opt.optlen = opt_len; |
| 1896 | opt->opt.cipso = sizeof(struct iphdr); |
| 1897 | kfree(buf); |
| 1898 | buf = NULL; |
| 1899 | |
| 1900 | sk_inet = inet_sk(sk); |
| 1901 | |
| 1902 | old = rcu_dereference_protected(sk_inet->inet_opt, sk_locked); |
| 1903 | if (inet_test_bit(IS_ICSK, sk)) { |
| 1904 | sk_conn = inet_csk(sk); |
| 1905 | if (old) |
| 1906 | sk_conn->icsk_ext_hdr_len -= old->opt.optlen; |
| 1907 | sk_conn->icsk_ext_hdr_len += opt->opt.optlen; |
| 1908 | sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie); |
| 1909 | } |
| 1910 | rcu_assign_pointer(sk_inet->inet_opt, opt); |
| 1911 | if (old) |
| 1912 | kfree_rcu(old, rcu); |
| 1913 | |
| 1914 | return 0; |
| 1915 | |
| 1916 | socket_setattr_failure: |
| 1917 | kfree(buf); |
| 1918 | kfree(opt); |
| 1919 | return ret_val; |
| 1920 | } |
| 1921 | |
| 1922 | /** |
| 1923 | * cipso_v4_req_setattr - Add a CIPSO option to a connection request socket |
| 1924 | * @req: the connection request socket |
| 1925 | * @doi_def: the CIPSO DOI to use |
| 1926 | * @secattr: the specific security attributes of the socket |
| 1927 | * |
| 1928 | * Description: |
| 1929 | * Set the CIPSO option on the given socket using the DOI definition and |
| 1930 | * security attributes passed to the function. Returns zero on success and |
| 1931 | * negative values on failure. |
| 1932 | * |
| 1933 | */ |
| 1934 | int cipso_v4_req_setattr(struct request_sock *req, |
| 1935 | const struct cipso_v4_doi *doi_def, |
| 1936 | const struct netlbl_lsm_secattr *secattr) |
| 1937 | { |
| 1938 | int ret_val = -EPERM; |
| 1939 | unsigned char *buf = NULL; |
| 1940 | u32 buf_len; |
| 1941 | u32 opt_len; |
| 1942 | struct ip_options_rcu *opt = NULL; |
| 1943 | struct inet_request_sock *req_inet; |
| 1944 | |
| 1945 | /* We allocate the maximum CIPSO option size here so we are probably |
| 1946 | * being a little wasteful, but it makes our life _much_ easier later |
| 1947 | * on and after all we are only talking about 40 bytes. */ |
| 1948 | buf_len = CIPSO_V4_OPT_LEN_MAX; |
| 1949 | buf = kmalloc(buf_len, GFP_ATOMIC); |
| 1950 | if (!buf) { |
| 1951 | ret_val = -ENOMEM; |
| 1952 | goto req_setattr_failure; |
| 1953 | } |
| 1954 | |
| 1955 | ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr); |
| 1956 | if (ret_val < 0) |
| 1957 | goto req_setattr_failure; |
| 1958 | buf_len = ret_val; |
| 1959 | |
| 1960 | /* We can't use ip_options_get() directly because it makes a call to |
| 1961 | * ip_options_get_alloc() which allocates memory with GFP_KERNEL and |
| 1962 | * we won't always have CAP_NET_RAW even though we _always_ want to |
| 1963 | * set the IPOPT_CIPSO option. */ |
| 1964 | opt_len = (buf_len + 3) & ~3; |
| 1965 | opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC); |
| 1966 | if (!opt) { |
| 1967 | ret_val = -ENOMEM; |
| 1968 | goto req_setattr_failure; |
| 1969 | } |
| 1970 | memcpy(opt->opt.__data, buf, buf_len); |
| 1971 | opt->opt.optlen = opt_len; |
| 1972 | opt->opt.cipso = sizeof(struct iphdr); |
| 1973 | kfree(buf); |
| 1974 | buf = NULL; |
| 1975 | |
| 1976 | req_inet = inet_rsk(req); |
| 1977 | opt = unrcu_pointer(xchg(&req_inet->ireq_opt, RCU_INITIALIZER(opt))); |
| 1978 | if (opt) |
| 1979 | kfree_rcu(opt, rcu); |
| 1980 | |
| 1981 | return 0; |
| 1982 | |
| 1983 | req_setattr_failure: |
| 1984 | kfree(buf); |
| 1985 | kfree(opt); |
| 1986 | return ret_val; |
| 1987 | } |
| 1988 | |
| 1989 | /** |
| 1990 | * cipso_v4_delopt - Delete the CIPSO option from a set of IP options |
| 1991 | * @opt_ptr: IP option pointer |
| 1992 | * |
| 1993 | * Description: |
| 1994 | * Deletes the CIPSO IP option from a set of IP options and makes the necessary |
| 1995 | * adjustments to the IP option structure. Returns zero on success, negative |
| 1996 | * values on failure. |
| 1997 | * |
| 1998 | */ |
| 1999 | static int cipso_v4_delopt(struct ip_options_rcu __rcu **opt_ptr) |
| 2000 | { |
| 2001 | struct ip_options_rcu *opt = rcu_dereference_protected(*opt_ptr, 1); |
| 2002 | int hdr_delta = 0; |
| 2003 | |
| 2004 | if (!opt || opt->opt.cipso == 0) |
| 2005 | return 0; |
| 2006 | if (opt->opt.srr || opt->opt.rr || opt->opt.ts || opt->opt.router_alert) { |
| 2007 | u8 cipso_len; |
| 2008 | u8 cipso_off; |
| 2009 | unsigned char *cipso_ptr; |
| 2010 | int optlen_new; |
| 2011 | |
| 2012 | cipso_off = opt->opt.cipso - sizeof(struct iphdr); |
| 2013 | cipso_ptr = &opt->opt.__data[cipso_off]; |
| 2014 | cipso_len = cipso_ptr[1]; |
| 2015 | |
| 2016 | if (opt->opt.srr > opt->opt.cipso) |
| 2017 | opt->opt.srr -= cipso_len; |
| 2018 | if (opt->opt.rr > opt->opt.cipso) |
| 2019 | opt->opt.rr -= cipso_len; |
| 2020 | if (opt->opt.ts > opt->opt.cipso) |
| 2021 | opt->opt.ts -= cipso_len; |
| 2022 | if (opt->opt.router_alert > opt->opt.cipso) |
| 2023 | opt->opt.router_alert -= cipso_len; |
| 2024 | opt->opt.cipso = 0; |
| 2025 | |
| 2026 | memmove(cipso_ptr, cipso_ptr + cipso_len, |
| 2027 | opt->opt.optlen - cipso_off - cipso_len); |
| 2028 | |
| 2029 | optlen_new = cipso_v4_get_actual_opt_len(opt->opt.__data, |
| 2030 | opt->opt.optlen); |
| 2031 | hdr_delta = opt->opt.optlen; |
| 2032 | opt->opt.optlen = (optlen_new + 3) & ~3; |
| 2033 | hdr_delta -= opt->opt.optlen; |
| 2034 | } else { |
| 2035 | /* only the cipso option was present on the socket so we can |
| 2036 | * remove the entire option struct */ |
| 2037 | *opt_ptr = NULL; |
| 2038 | hdr_delta = opt->opt.optlen; |
| 2039 | kfree_rcu(opt, rcu); |
| 2040 | } |
| 2041 | |
| 2042 | return hdr_delta; |
| 2043 | } |
| 2044 | |
| 2045 | /** |
| 2046 | * cipso_v4_sock_delattr - Delete the CIPSO option from a socket |
| 2047 | * @sk: the socket |
| 2048 | * |
| 2049 | * Description: |
| 2050 | * Removes the CIPSO option from a socket, if present. |
| 2051 | * |
| 2052 | */ |
| 2053 | void cipso_v4_sock_delattr(struct sock *sk) |
| 2054 | { |
| 2055 | struct inet_sock *sk_inet; |
| 2056 | int hdr_delta; |
| 2057 | |
| 2058 | sk_inet = inet_sk(sk); |
| 2059 | |
| 2060 | hdr_delta = cipso_v4_delopt(&sk_inet->inet_opt); |
| 2061 | if (inet_test_bit(IS_ICSK, sk) && hdr_delta > 0) { |
| 2062 | struct inet_connection_sock *sk_conn = inet_csk(sk); |
| 2063 | sk_conn->icsk_ext_hdr_len -= hdr_delta; |
| 2064 | sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie); |
| 2065 | } |
| 2066 | } |
| 2067 | |
| 2068 | /** |
| 2069 | * cipso_v4_req_delattr - Delete the CIPSO option from a request socket |
| 2070 | * @req: the request socket |
| 2071 | * |
| 2072 | * Description: |
| 2073 | * Removes the CIPSO option from a request socket, if present. |
| 2074 | * |
| 2075 | */ |
| 2076 | void cipso_v4_req_delattr(struct request_sock *req) |
| 2077 | { |
| 2078 | cipso_v4_delopt(&inet_rsk(req)->ireq_opt); |
| 2079 | } |
| 2080 | |
| 2081 | /** |
| 2082 | * cipso_v4_getattr - Helper function for the cipso_v4_*_getattr functions |
| 2083 | * @cipso: the CIPSO v4 option |
| 2084 | * @secattr: the security attributes |
| 2085 | * |
| 2086 | * Description: |
| 2087 | * Inspect @cipso and return the security attributes in @secattr. Returns zero |
| 2088 | * on success and negative values on failure. |
| 2089 | * |
| 2090 | */ |
| 2091 | int cipso_v4_getattr(const unsigned char *cipso, |
| 2092 | struct netlbl_lsm_secattr *secattr) |
| 2093 | { |
| 2094 | int ret_val = -ENOMSG; |
| 2095 | u32 doi; |
| 2096 | struct cipso_v4_doi *doi_def; |
| 2097 | |
| 2098 | if (cipso_v4_cache_check(cipso, cipso[1], secattr) == 0) |
| 2099 | return 0; |
| 2100 | |
| 2101 | doi = get_unaligned_be32(&cipso[2]); |
| 2102 | rcu_read_lock(); |
| 2103 | doi_def = cipso_v4_doi_search(doi); |
| 2104 | if (!doi_def) |
| 2105 | goto getattr_return; |
| 2106 | /* XXX - This code assumes only one tag per CIPSO option which isn't |
| 2107 | * really a good assumption to make but since we only support the MAC |
| 2108 | * tags right now it is a safe assumption. */ |
| 2109 | switch (cipso[6]) { |
| 2110 | case CIPSO_V4_TAG_RBITMAP: |
| 2111 | ret_val = cipso_v4_parsetag_rbm(doi_def, &cipso[6], secattr); |
| 2112 | break; |
| 2113 | case CIPSO_V4_TAG_ENUM: |
| 2114 | ret_val = cipso_v4_parsetag_enum(doi_def, &cipso[6], secattr); |
| 2115 | break; |
| 2116 | case CIPSO_V4_TAG_RANGE: |
| 2117 | ret_val = cipso_v4_parsetag_rng(doi_def, &cipso[6], secattr); |
| 2118 | break; |
| 2119 | case CIPSO_V4_TAG_LOCAL: |
| 2120 | ret_val = cipso_v4_parsetag_loc(doi_def, &cipso[6], secattr); |
| 2121 | break; |
| 2122 | } |
| 2123 | if (ret_val == 0) |
| 2124 | secattr->type = NETLBL_NLTYPE_CIPSOV4; |
| 2125 | |
| 2126 | getattr_return: |
| 2127 | rcu_read_unlock(); |
| 2128 | return ret_val; |
| 2129 | } |
| 2130 | |
| 2131 | /** |
| 2132 | * cipso_v4_sock_getattr - Get the security attributes from a sock |
| 2133 | * @sk: the sock |
| 2134 | * @secattr: the security attributes |
| 2135 | * |
| 2136 | * Description: |
| 2137 | * Query @sk to see if there is a CIPSO option attached to the sock and if |
| 2138 | * there is return the CIPSO security attributes in @secattr. This function |
| 2139 | * requires that @sk be locked, or privately held, but it does not do any |
| 2140 | * locking itself. Returns zero on success and negative values on failure. |
| 2141 | * |
| 2142 | */ |
| 2143 | int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr) |
| 2144 | { |
| 2145 | struct ip_options_rcu *opt; |
| 2146 | int res = -ENOMSG; |
| 2147 | |
| 2148 | rcu_read_lock(); |
| 2149 | opt = rcu_dereference(inet_sk(sk)->inet_opt); |
| 2150 | if (opt && opt->opt.cipso) |
| 2151 | res = cipso_v4_getattr(opt->opt.__data + |
| 2152 | opt->opt.cipso - |
| 2153 | sizeof(struct iphdr), |
| 2154 | secattr); |
| 2155 | rcu_read_unlock(); |
| 2156 | return res; |
| 2157 | } |
| 2158 | |
| 2159 | /** |
| 2160 | * cipso_v4_skbuff_setattr - Set the CIPSO option on a packet |
| 2161 | * @skb: the packet |
| 2162 | * @doi_def: the DOI structure |
| 2163 | * @secattr: the security attributes |
| 2164 | * |
| 2165 | * Description: |
| 2166 | * Set the CIPSO option on the given packet based on the security attributes. |
| 2167 | * Returns a pointer to the IP header on success and NULL on failure. |
| 2168 | * |
| 2169 | */ |
| 2170 | int cipso_v4_skbuff_setattr(struct sk_buff *skb, |
| 2171 | const struct cipso_v4_doi *doi_def, |
| 2172 | const struct netlbl_lsm_secattr *secattr) |
| 2173 | { |
| 2174 | int ret_val; |
| 2175 | struct iphdr *iph; |
| 2176 | struct ip_options *opt = &IPCB(skb)->opt; |
| 2177 | unsigned char buf[CIPSO_V4_OPT_LEN_MAX]; |
| 2178 | u32 buf_len = CIPSO_V4_OPT_LEN_MAX; |
| 2179 | u32 opt_len; |
| 2180 | int len_delta; |
| 2181 | |
| 2182 | ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr); |
| 2183 | if (ret_val < 0) |
| 2184 | return ret_val; |
| 2185 | buf_len = ret_val; |
| 2186 | opt_len = (buf_len + 3) & ~3; |
| 2187 | |
| 2188 | /* we overwrite any existing options to ensure that we have enough |
| 2189 | * room for the CIPSO option, the reason is that we _need_ to guarantee |
| 2190 | * that the security label is applied to the packet - we do the same |
| 2191 | * thing when using the socket options and it hasn't caused a problem, |
| 2192 | * if we need to we can always revisit this choice later */ |
| 2193 | |
| 2194 | len_delta = opt_len - opt->optlen; |
| 2195 | /* if we don't ensure enough headroom we could panic on the skb_push() |
| 2196 | * call below so make sure we have enough, we are also "mangling" the |
| 2197 | * packet so we should probably do a copy-on-write call anyway */ |
| 2198 | ret_val = skb_cow(skb, |
| 2199 | skb_headroom(skb) + (len_delta > 0 ? len_delta : 0)); |
| 2200 | if (ret_val < 0) |
| 2201 | return ret_val; |
| 2202 | |
| 2203 | if (len_delta > 0) { |
| 2204 | /* we assume that the header + opt->optlen have already been |
| 2205 | * "pushed" in ip_options_build() or similar */ |
| 2206 | iph = ip_hdr(skb); |
| 2207 | skb_push(skb, len_delta); |
| 2208 | memmove((char *)iph - len_delta, iph, iph->ihl << 2); |
| 2209 | skb_reset_network_header(skb); |
| 2210 | iph = ip_hdr(skb); |
| 2211 | } else if (len_delta < 0) { |
| 2212 | iph = ip_hdr(skb); |
| 2213 | memset(iph + 1, IPOPT_NOP, opt->optlen); |
| 2214 | } else |
| 2215 | iph = ip_hdr(skb); |
| 2216 | |
| 2217 | if (opt->optlen > 0) |
| 2218 | memset(opt, 0, sizeof(*opt)); |
| 2219 | opt->optlen = opt_len; |
| 2220 | opt->cipso = sizeof(struct iphdr); |
| 2221 | opt->is_changed = 1; |
| 2222 | |
| 2223 | /* we have to do the following because we are being called from a |
| 2224 | * netfilter hook which means the packet already has had the header |
| 2225 | * fields populated and the checksum calculated - yes this means we |
| 2226 | * are doing more work than needed but we do it to keep the core |
| 2227 | * stack clean and tidy */ |
| 2228 | memcpy(iph + 1, buf, buf_len); |
| 2229 | if (opt_len > buf_len) |
| 2230 | memset((char *)(iph + 1) + buf_len, 0, opt_len - buf_len); |
| 2231 | if (len_delta != 0) { |
| 2232 | iph->ihl = 5 + (opt_len >> 2); |
| 2233 | iph_set_totlen(iph, skb->len); |
| 2234 | } |
| 2235 | ip_send_check(iph); |
| 2236 | |
| 2237 | return 0; |
| 2238 | } |
| 2239 | |
| 2240 | /** |
| 2241 | * cipso_v4_skbuff_delattr - Delete any CIPSO options from a packet |
| 2242 | * @skb: the packet |
| 2243 | * |
| 2244 | * Description: |
| 2245 | * Removes any and all CIPSO options from the given packet. Returns zero on |
| 2246 | * success, negative values on failure. |
| 2247 | * |
| 2248 | */ |
| 2249 | int cipso_v4_skbuff_delattr(struct sk_buff *skb) |
| 2250 | { |
| 2251 | int ret_val, cipso_len, hdr_len_actual, new_hdr_len_actual, new_hdr_len, |
| 2252 | hdr_len_delta; |
| 2253 | struct iphdr *iph; |
| 2254 | struct ip_options *opt = &IPCB(skb)->opt; |
| 2255 | unsigned char *cipso_ptr; |
| 2256 | |
| 2257 | if (opt->cipso == 0) |
| 2258 | return 0; |
| 2259 | |
| 2260 | /* since we are changing the packet we should make a copy */ |
| 2261 | ret_val = skb_cow(skb, skb_headroom(skb)); |
| 2262 | if (ret_val < 0) |
| 2263 | return ret_val; |
| 2264 | |
| 2265 | iph = ip_hdr(skb); |
| 2266 | cipso_ptr = (unsigned char *)iph + opt->cipso; |
| 2267 | cipso_len = cipso_ptr[1]; |
| 2268 | |
| 2269 | hdr_len_actual = sizeof(struct iphdr) + |
| 2270 | cipso_v4_get_actual_opt_len((unsigned char *)(iph + 1), |
| 2271 | opt->optlen); |
| 2272 | new_hdr_len_actual = hdr_len_actual - cipso_len; |
| 2273 | new_hdr_len = (new_hdr_len_actual + 3) & ~3; |
| 2274 | hdr_len_delta = (iph->ihl << 2) - new_hdr_len; |
| 2275 | |
| 2276 | /* 1. shift any options after CIPSO to the left */ |
| 2277 | memmove(cipso_ptr, cipso_ptr + cipso_len, |
| 2278 | new_hdr_len_actual - opt->cipso); |
| 2279 | /* 2. move the whole IP header to its new place */ |
| 2280 | memmove((unsigned char *)iph + hdr_len_delta, iph, new_hdr_len_actual); |
| 2281 | /* 3. adjust the skb layout */ |
| 2282 | skb_pull(skb, hdr_len_delta); |
| 2283 | skb_reset_network_header(skb); |
| 2284 | iph = ip_hdr(skb); |
| 2285 | /* 4. re-fill new padding with IPOPT_END (may now be longer) */ |
| 2286 | memset((unsigned char *)iph + new_hdr_len_actual, IPOPT_END, |
| 2287 | new_hdr_len - new_hdr_len_actual); |
| 2288 | |
| 2289 | opt->optlen -= hdr_len_delta; |
| 2290 | if (opt->srr > opt->cipso) |
| 2291 | opt->srr -= cipso_len; |
| 2292 | if (opt->rr > opt->cipso) |
| 2293 | opt->rr -= cipso_len; |
| 2294 | if (opt->ts > opt->cipso) |
| 2295 | opt->ts -= cipso_len; |
| 2296 | if (opt->router_alert > opt->cipso) |
| 2297 | opt->router_alert -= cipso_len; |
| 2298 | opt->cipso = 0; |
| 2299 | opt->is_changed = 1; |
| 2300 | if (hdr_len_delta != 0) { |
| 2301 | iph->ihl = new_hdr_len >> 2; |
| 2302 | iph_set_totlen(iph, skb->len); |
| 2303 | } |
| 2304 | ip_send_check(iph); |
| 2305 | |
| 2306 | return 0; |
| 2307 | } |
| 2308 | |
| 2309 | /* |
| 2310 | * Setup Functions |
| 2311 | */ |
| 2312 | |
| 2313 | /** |
| 2314 | * cipso_v4_init - Initialize the CIPSO module |
| 2315 | * |
| 2316 | * Description: |
| 2317 | * Initialize the CIPSO module and prepare it for use. Returns zero on success |
| 2318 | * and negative values on failure. |
| 2319 | * |
| 2320 | */ |
| 2321 | static int __init cipso_v4_init(void) |
| 2322 | { |
| 2323 | int ret_val; |
| 2324 | |
| 2325 | ret_val = cipso_v4_cache_init(); |
| 2326 | if (ret_val != 0) |
| 2327 | panic("Failed to initialize the CIPSO/IPv4 cache (%d)\n", |
| 2328 | ret_val); |
| 2329 | |
| 2330 | return 0; |
| 2331 | } |
| 2332 | |
| 2333 | subsys_initcall(cipso_v4_init); |