netlabel_kapi.c 12 KB

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