netlabel_kapi.c 12 KB

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