netlabel_kapi.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  22. * the GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. *
  28. */
  29. #include <linux/init.h>
  30. #include <linux/types.h>
  31. #include <linux/audit.h>
  32. #include <net/ip.h>
  33. #include <net/netlabel.h>
  34. #include <net/cipso_ipv4.h>
  35. #include <asm/bug.h>
  36. #include <asm/atomic.h>
  37. #include "netlabel_domainhash.h"
  38. #include "netlabel_unlabeled.h"
  39. #include "netlabel_cipso_v4.h"
  40. #include "netlabel_user.h"
  41. #include "netlabel_mgmt.h"
  42. /*
  43. * Configuration Functions
  44. */
  45. /**
  46. * netlbl_cfg_map_del - Remove a NetLabel/LSM domain mapping
  47. * @domain: the domain mapping to remove
  48. * @audit_info: NetLabel audit information
  49. *
  50. * Description:
  51. * Removes a NetLabel/LSM domain mapping. A @domain value of NULL causes the
  52. * default domain mapping to be removed. Returns zero on success, negative
  53. * values on failure.
  54. *
  55. */
  56. int netlbl_cfg_map_del(const char *domain, struct netlbl_audit *audit_info)
  57. {
  58. return netlbl_domhsh_remove(domain, audit_info);
  59. }
  60. /**
  61. * netlbl_cfg_unlbl_add_map - Add an unlabeled NetLabel/LSM domain mapping
  62. * @domain: the domain mapping to add
  63. * @audit_info: NetLabel audit information
  64. *
  65. * Description:
  66. * Adds a new unlabeled NetLabel/LSM domain mapping. A @domain value of NULL
  67. * causes a new default domain mapping to be added. Returns zero on success,
  68. * negative values on failure.
  69. *
  70. */
  71. int netlbl_cfg_unlbl_add_map(const char *domain,
  72. struct netlbl_audit *audit_info)
  73. {
  74. int ret_val = -ENOMEM;
  75. struct netlbl_dom_map *entry;
  76. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  77. if (entry == NULL)
  78. goto cfg_unlbl_add_map_failure;
  79. if (domain != NULL) {
  80. entry->domain = kstrdup(domain, GFP_ATOMIC);
  81. if (entry->domain == NULL)
  82. goto cfg_unlbl_add_map_failure;
  83. }
  84. entry->type = NETLBL_NLTYPE_UNLABELED;
  85. ret_val = netlbl_domhsh_add(entry, audit_info);
  86. if (ret_val != 0)
  87. goto cfg_unlbl_add_map_failure;
  88. return 0;
  89. cfg_unlbl_add_map_failure:
  90. if (entry != NULL)
  91. kfree(entry->domain);
  92. kfree(entry);
  93. return ret_val;
  94. }
  95. /**
  96. * netlbl_cfg_cipsov4_add - Add a new CIPSOv4 DOI definition
  97. * @doi_def: the DOI definition
  98. * @audit_info: NetLabel audit information
  99. *
  100. * Description:
  101. * Add a new CIPSOv4 DOI definition to the NetLabel subsystem. Returns zero on
  102. * success, negative values on failure.
  103. *
  104. */
  105. int netlbl_cfg_cipsov4_add(struct cipso_v4_doi *doi_def,
  106. struct netlbl_audit *audit_info)
  107. {
  108. int ret_val;
  109. const char *type_str;
  110. struct audit_buffer *audit_buf;
  111. ret_val = cipso_v4_doi_add(doi_def);
  112. audit_buf = netlbl_audit_start_common(AUDIT_MAC_CIPSOV4_ADD,
  113. audit_info);
  114. if (audit_buf != NULL) {
  115. switch (doi_def->type) {
  116. case CIPSO_V4_MAP_STD:
  117. type_str = "std";
  118. break;
  119. case CIPSO_V4_MAP_PASS:
  120. type_str = "pass";
  121. break;
  122. default:
  123. type_str = "(unknown)";
  124. }
  125. audit_log_format(audit_buf,
  126. " cipso_doi=%u cipso_type=%s res=%u",
  127. doi_def->doi,
  128. type_str,
  129. ret_val == 0 ? 1 : 0);
  130. audit_log_end(audit_buf);
  131. }
  132. return ret_val;
  133. }
  134. /**
  135. * netlbl_cfg_cipsov4_add_map - Add a new CIPSOv4 DOI definition and mapping
  136. * @doi_def: the DOI definition
  137. * @domain: the domain mapping to add
  138. * @audit_info: NetLabel audit information
  139. *
  140. * Description:
  141. * Add a new CIPSOv4 DOI definition and NetLabel/LSM domain mapping for this
  142. * new DOI definition to the NetLabel subsystem. A @domain value of NULL adds
  143. * a new default domain mapping. Returns zero on success, negative values on
  144. * failure.
  145. *
  146. */
  147. int netlbl_cfg_cipsov4_add_map(struct cipso_v4_doi *doi_def,
  148. const char *domain,
  149. struct netlbl_audit *audit_info)
  150. {
  151. int ret_val = -ENOMEM;
  152. struct netlbl_dom_map *entry;
  153. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  154. if (entry == NULL)
  155. goto cfg_cipsov4_add_map_failure;
  156. if (domain != NULL) {
  157. entry->domain = kstrdup(domain, GFP_ATOMIC);
  158. if (entry->domain == NULL)
  159. goto cfg_cipsov4_add_map_failure;
  160. }
  161. entry->type = NETLBL_NLTYPE_CIPSOV4;
  162. entry->type_def.cipsov4 = doi_def;
  163. /* Grab a RCU read lock here so nothing happens to the doi_def variable
  164. * between adding it to the CIPSOv4 protocol engine and adding a
  165. * domain mapping for it. */
  166. rcu_read_lock();
  167. ret_val = netlbl_cfg_cipsov4_add(doi_def, audit_info);
  168. if (ret_val != 0)
  169. goto cfg_cipsov4_add_map_failure_unlock;
  170. ret_val = netlbl_domhsh_add(entry, audit_info);
  171. if (ret_val != 0)
  172. goto cfg_cipsov4_add_map_failure_remove_doi;
  173. rcu_read_unlock();
  174. return 0;
  175. cfg_cipsov4_add_map_failure_remove_doi:
  176. cipso_v4_doi_remove(doi_def->doi, audit_info, netlbl_cipsov4_doi_free);
  177. cfg_cipsov4_add_map_failure_unlock:
  178. rcu_read_unlock();
  179. cfg_cipsov4_add_map_failure:
  180. if (entry != NULL)
  181. kfree(entry->domain);
  182. kfree(entry);
  183. return ret_val;
  184. }
  185. /**
  186. * netlbl_cfg_cipsov4_del - Removean existing CIPSOv4 DOI definition
  187. * @doi: the CIPSO DOI value
  188. * @audit_info: NetLabel audit information
  189. *
  190. * Description:
  191. * Removes an existing CIPSOv4 DOI definition from the NetLabel subsystem.
  192. * Returns zero on success, negative values on failure.
  193. *
  194. */
  195. int netlbl_cfg_cipsov4_del(u32 doi, struct netlbl_audit *audit_info)
  196. {
  197. return cipso_v4_doi_remove(doi, audit_info, netlbl_cipsov4_doi_free);
  198. }
  199. /*
  200. * Security Attribute Functions
  201. */
  202. /**
  203. * netlbl_secattr_catmap_walk - Walk a LSM secattr catmap looking for a bit
  204. * @catmap: the category bitmap
  205. * @offset: the offset to start searching at, in bits
  206. *
  207. * Description:
  208. * This function walks a LSM secattr category bitmap starting at @offset and
  209. * returns the spot of the first set bit or -ENOENT if no bits are set.
  210. *
  211. */
  212. int netlbl_secattr_catmap_walk(struct netlbl_lsm_secattr_catmap *catmap,
  213. u32 offset)
  214. {
  215. struct netlbl_lsm_secattr_catmap *iter = catmap;
  216. u32 node_idx;
  217. u32 node_bit;
  218. NETLBL_CATMAP_MAPTYPE bitmap;
  219. if (offset > iter->startbit) {
  220. while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
  221. iter = iter->next;
  222. if (iter == NULL)
  223. return -ENOENT;
  224. }
  225. node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
  226. node_bit = offset - iter->startbit -
  227. (NETLBL_CATMAP_MAPSIZE * node_idx);
  228. } else {
  229. node_idx = 0;
  230. node_bit = 0;
  231. }
  232. bitmap = iter->bitmap[node_idx] >> node_bit;
  233. for (;;) {
  234. if (bitmap != 0) {
  235. while ((bitmap & NETLBL_CATMAP_BIT) == 0) {
  236. bitmap >>= 1;
  237. node_bit++;
  238. }
  239. return iter->startbit +
  240. (NETLBL_CATMAP_MAPSIZE * node_idx) + node_bit;
  241. }
  242. if (++node_idx >= NETLBL_CATMAP_MAPCNT) {
  243. if (iter->next != NULL) {
  244. iter = iter->next;
  245. node_idx = 0;
  246. } else
  247. return -ENOENT;
  248. }
  249. bitmap = iter->bitmap[node_idx];
  250. node_bit = 0;
  251. }
  252. return -ENOENT;
  253. }
  254. /**
  255. * netlbl_secattr_catmap_walk_rng - Find the end of a string of set bits
  256. * @catmap: the category bitmap
  257. * @offset: the offset to start searching at, in bits
  258. *
  259. * Description:
  260. * This function walks a LSM secattr category bitmap starting at @offset and
  261. * returns the spot of the first cleared bit or -ENOENT if the offset is past
  262. * the end of the bitmap.
  263. *
  264. */
  265. int netlbl_secattr_catmap_walk_rng(struct netlbl_lsm_secattr_catmap *catmap,
  266. u32 offset)
  267. {
  268. struct netlbl_lsm_secattr_catmap *iter = catmap;
  269. u32 node_idx;
  270. u32 node_bit;
  271. NETLBL_CATMAP_MAPTYPE bitmask;
  272. NETLBL_CATMAP_MAPTYPE bitmap;
  273. if (offset > iter->startbit) {
  274. while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
  275. iter = iter->next;
  276. if (iter == NULL)
  277. return -ENOENT;
  278. }
  279. node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
  280. node_bit = offset - iter->startbit -
  281. (NETLBL_CATMAP_MAPSIZE * node_idx);
  282. } else {
  283. node_idx = 0;
  284. node_bit = 0;
  285. }
  286. bitmask = NETLBL_CATMAP_BIT << node_bit;
  287. for (;;) {
  288. bitmap = iter->bitmap[node_idx];
  289. while (bitmask != 0 && (bitmap & bitmask) != 0) {
  290. bitmask <<= 1;
  291. node_bit++;
  292. }
  293. if (bitmask != 0)
  294. return iter->startbit +
  295. (NETLBL_CATMAP_MAPSIZE * node_idx) +
  296. node_bit - 1;
  297. else if (++node_idx >= NETLBL_CATMAP_MAPCNT) {
  298. if (iter->next == NULL)
  299. return iter->startbit + NETLBL_CATMAP_SIZE - 1;
  300. iter = iter->next;
  301. node_idx = 0;
  302. }
  303. bitmask = NETLBL_CATMAP_BIT;
  304. node_bit = 0;
  305. }
  306. return -ENOENT;
  307. }
  308. /**
  309. * netlbl_secattr_catmap_setbit - Set a bit in a LSM secattr catmap
  310. * @catmap: the category bitmap
  311. * @bit: the bit to set
  312. * @flags: memory allocation flags
  313. *
  314. * Description:
  315. * Set the bit specified by @bit in @catmap. Returns zero on success,
  316. * negative values on failure.
  317. *
  318. */
  319. int netlbl_secattr_catmap_setbit(struct netlbl_lsm_secattr_catmap *catmap,
  320. u32 bit,
  321. gfp_t flags)
  322. {
  323. struct netlbl_lsm_secattr_catmap *iter = catmap;
  324. u32 node_bit;
  325. u32 node_idx;
  326. while (iter->next != NULL &&
  327. bit >= (iter->startbit + NETLBL_CATMAP_SIZE))
  328. iter = iter->next;
  329. if (bit >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
  330. iter->next = netlbl_secattr_catmap_alloc(flags);
  331. if (iter->next == NULL)
  332. return -ENOMEM;
  333. iter = iter->next;
  334. iter->startbit = bit & ~(NETLBL_CATMAP_SIZE - 1);
  335. }
  336. /* gcc always rounds to zero when doing integer division */
  337. node_idx = (bit - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
  338. node_bit = bit - iter->startbit - (NETLBL_CATMAP_MAPSIZE * node_idx);
  339. iter->bitmap[node_idx] |= NETLBL_CATMAP_BIT << node_bit;
  340. return 0;
  341. }
  342. /**
  343. * netlbl_secattr_catmap_setrng - Set a range of bits in a LSM secattr catmap
  344. * @catmap: the category bitmap
  345. * @start: the starting bit
  346. * @end: the last bit in the string
  347. * @flags: memory allocation flags
  348. *
  349. * Description:
  350. * Set a range of bits, starting at @start and ending with @end. Returns zero
  351. * on success, negative values on failure.
  352. *
  353. */
  354. int netlbl_secattr_catmap_setrng(struct netlbl_lsm_secattr_catmap *catmap,
  355. u32 start,
  356. u32 end,
  357. gfp_t flags)
  358. {
  359. int ret_val = 0;
  360. struct netlbl_lsm_secattr_catmap *iter = catmap;
  361. u32 iter_max_spot;
  362. u32 spot;
  363. /* XXX - This could probably be made a bit faster by combining writes
  364. * to the catmap instead of setting a single bit each time, but for
  365. * right now skipping to the start of the range in the catmap should
  366. * be a nice improvement over calling the individual setbit function
  367. * repeatedly from a loop. */
  368. while (iter->next != NULL &&
  369. start >= (iter->startbit + NETLBL_CATMAP_SIZE))
  370. iter = iter->next;
  371. iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE;
  372. for (spot = start; spot <= end && ret_val == 0; spot++) {
  373. if (spot >= iter_max_spot && iter->next != NULL) {
  374. iter = iter->next;
  375. iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE;
  376. }
  377. ret_val = netlbl_secattr_catmap_setbit(iter, spot, GFP_ATOMIC);
  378. }
  379. return ret_val;
  380. }
  381. /*
  382. * LSM Functions
  383. */
  384. /**
  385. * netlbl_enabled - Determine if the NetLabel subsystem is enabled
  386. *
  387. * Description:
  388. * The LSM can use this function to determine if it should use NetLabel
  389. * security attributes in it's enforcement mechanism. Currently, NetLabel is
  390. * considered to be enabled when it's configuration contains a valid setup for
  391. * at least one labeled protocol (i.e. NetLabel can understand incoming
  392. * labeled packets of at least one type); otherwise NetLabel is considered to
  393. * be disabled.
  394. *
  395. */
  396. int netlbl_enabled(void)
  397. {
  398. /* At some point we probably want to expose this mechanism to the user
  399. * as well so that admins can toggle NetLabel regardless of the
  400. * configuration */
  401. return (atomic_read(&netlabel_mgmt_protocount) > 0);
  402. }
  403. /**
  404. * netlbl_socket_setattr - Label a socket using the correct protocol
  405. * @sk: the socket to label
  406. * @secattr: the security attributes
  407. *
  408. * Description:
  409. * Attach the correct label to the given socket using the security attributes
  410. * specified in @secattr. This function requires exclusive access to @sk,
  411. * which means it either needs to be in the process of being created or locked.
  412. * Returns zero on success, negative values on failure.
  413. *
  414. */
  415. int netlbl_sock_setattr(struct sock *sk,
  416. const struct netlbl_lsm_secattr *secattr)
  417. {
  418. int ret_val = -ENOENT;
  419. struct netlbl_dom_map *dom_entry;
  420. rcu_read_lock();
  421. dom_entry = netlbl_domhsh_getentry(secattr->domain);
  422. if (dom_entry == NULL)
  423. goto socket_setattr_return;
  424. switch (dom_entry->type) {
  425. case NETLBL_NLTYPE_CIPSOV4:
  426. ret_val = cipso_v4_sock_setattr(sk,
  427. dom_entry->type_def.cipsov4,
  428. secattr);
  429. break;
  430. case NETLBL_NLTYPE_UNLABELED:
  431. ret_val = 0;
  432. break;
  433. default:
  434. ret_val = -ENOENT;
  435. }
  436. socket_setattr_return:
  437. rcu_read_unlock();
  438. return ret_val;
  439. }
  440. /**
  441. * netlbl_sock_getattr - Determine the security attributes of a sock
  442. * @sk: the sock
  443. * @secattr: the security attributes
  444. *
  445. * Description:
  446. * Examines the given sock to see if any NetLabel style labeling has been
  447. * applied to the sock, if so it parses the socket label and returns the
  448. * security attributes in @secattr. Returns zero on success, negative values
  449. * on failure.
  450. *
  451. */
  452. int netlbl_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
  453. {
  454. return cipso_v4_sock_getattr(sk, secattr);
  455. }
  456. /**
  457. * netlbl_skbuff_getattr - Determine the security attributes of a packet
  458. * @skb: the packet
  459. * @family: protocol family
  460. * @secattr: the security attributes
  461. *
  462. * Description:
  463. * Examines the given packet to see if a recognized form of packet labeling
  464. * is present, if so it parses the packet label and returns the security
  465. * attributes in @secattr. Returns zero on success, negative values on
  466. * failure.
  467. *
  468. */
  469. int netlbl_skbuff_getattr(const struct sk_buff *skb,
  470. u16 family,
  471. struct netlbl_lsm_secattr *secattr)
  472. {
  473. if (CIPSO_V4_OPTEXIST(skb) &&
  474. cipso_v4_skbuff_getattr(skb, secattr) == 0)
  475. return 0;
  476. return netlbl_unlabel_getattr(skb, family, secattr);
  477. }
  478. /**
  479. * netlbl_skbuff_err - Handle a LSM error on a sk_buff
  480. * @skb: the packet
  481. * @error: the error code
  482. *
  483. * Description:
  484. * Deal with a LSM problem when handling the packet in @skb, typically this is
  485. * a permission denied problem (-EACCES). The correct action is determined
  486. * according to the packet's labeling protocol.
  487. *
  488. */
  489. void netlbl_skbuff_err(struct sk_buff *skb, int error)
  490. {
  491. if (CIPSO_V4_OPTEXIST(skb))
  492. cipso_v4_error(skb, error, 0);
  493. }
  494. /**
  495. * netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches
  496. *
  497. * Description:
  498. * For all of the NetLabel protocols that support some form of label mapping
  499. * cache, invalidate the cache. Returns zero on success, negative values on
  500. * error.
  501. *
  502. */
  503. void netlbl_cache_invalidate(void)
  504. {
  505. cipso_v4_cache_invalidate();
  506. }
  507. /**
  508. * netlbl_cache_add - Add an entry to a NetLabel protocol cache
  509. * @skb: the packet
  510. * @secattr: the packet's security attributes
  511. *
  512. * Description:
  513. * Add the LSM security attributes for the given packet to the underlying
  514. * NetLabel protocol's label mapping cache. Returns zero on success, negative
  515. * values on error.
  516. *
  517. */
  518. int netlbl_cache_add(const struct sk_buff *skb,
  519. const struct netlbl_lsm_secattr *secattr)
  520. {
  521. if ((secattr->flags & NETLBL_SECATTR_CACHE) == 0)
  522. return -ENOMSG;
  523. if (CIPSO_V4_OPTEXIST(skb))
  524. return cipso_v4_cache_add(skb, secattr);
  525. return -ENOMSG;
  526. }
  527. /*
  528. * Setup Functions
  529. */
  530. /**
  531. * netlbl_init - Initialize NetLabel
  532. *
  533. * Description:
  534. * Perform the required NetLabel initialization before first use.
  535. *
  536. */
  537. static int __init netlbl_init(void)
  538. {
  539. int ret_val;
  540. printk(KERN_INFO "NetLabel: Initializing\n");
  541. printk(KERN_INFO "NetLabel: domain hash size = %u\n",
  542. (1 << NETLBL_DOMHSH_BITSIZE));
  543. printk(KERN_INFO "NetLabel: protocols ="
  544. " UNLABELED"
  545. " CIPSOv4"
  546. "\n");
  547. ret_val = netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE);
  548. if (ret_val != 0)
  549. goto init_failure;
  550. ret_val = netlbl_unlabel_init(NETLBL_UNLHSH_BITSIZE);
  551. if (ret_val != 0)
  552. goto init_failure;
  553. ret_val = netlbl_netlink_init();
  554. if (ret_val != 0)
  555. goto init_failure;
  556. ret_val = netlbl_unlabel_defconf();
  557. if (ret_val != 0)
  558. goto init_failure;
  559. printk(KERN_INFO "NetLabel: unlabeled traffic allowed by default\n");
  560. return 0;
  561. init_failure:
  562. panic("NetLabel: failed to initialize properly (%d)\n", ret_val);
  563. }
  564. subsys_initcall(netlbl_init);