netlabel_kapi.c 12 KB

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