netlabel_kapi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. if ((secattr->flags & NETLBL_SECATTR_DOMAIN) == 0)
  242. return -ENOENT;
  243. rcu_read_lock();
  244. dom_entry = netlbl_domhsh_getentry(secattr->domain);
  245. if (dom_entry == NULL)
  246. goto socket_setattr_return;
  247. switch (dom_entry->type) {
  248. case NETLBL_NLTYPE_CIPSOV4:
  249. ret_val = cipso_v4_socket_setattr(sock,
  250. dom_entry->type_def.cipsov4,
  251. secattr);
  252. break;
  253. case NETLBL_NLTYPE_UNLABELED:
  254. ret_val = 0;
  255. break;
  256. default:
  257. ret_val = -ENOENT;
  258. }
  259. socket_setattr_return:
  260. rcu_read_unlock();
  261. return ret_val;
  262. }
  263. /**
  264. * netlbl_sock_getattr - Determine the security attributes of a sock
  265. * @sk: the sock
  266. * @secattr: the security attributes
  267. *
  268. * Description:
  269. * Examines the given sock to see any NetLabel style labeling has been
  270. * applied to the sock, if so it parses the socket label and returns the
  271. * security attributes in @secattr. Returns zero on success, negative values
  272. * on failure.
  273. *
  274. */
  275. int netlbl_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
  276. {
  277. int ret_val;
  278. ret_val = cipso_v4_sock_getattr(sk, secattr);
  279. if (ret_val == 0)
  280. return 0;
  281. return netlbl_unlabel_getattr(secattr);
  282. }
  283. /**
  284. * netlbl_socket_getattr - Determine the security attributes of a socket
  285. * @sock: the socket
  286. * @secattr: the security attributes
  287. *
  288. * Description:
  289. * Examines the given socket to see any NetLabel style labeling has been
  290. * applied to the socket, if so it parses the socket label and returns the
  291. * security attributes in @secattr. Returns zero on success, negative values
  292. * on failure.
  293. *
  294. */
  295. int netlbl_socket_getattr(const struct socket *sock,
  296. struct netlbl_lsm_secattr *secattr)
  297. {
  298. int ret_val;
  299. ret_val = cipso_v4_socket_getattr(sock, secattr);
  300. if (ret_val == 0)
  301. return 0;
  302. return netlbl_unlabel_getattr(secattr);
  303. }
  304. /**
  305. * netlbl_skbuff_getattr - Determine the security attributes of a packet
  306. * @skb: the packet
  307. * @secattr: the security attributes
  308. *
  309. * Description:
  310. * Examines the given packet to see if a recognized form of packet labeling
  311. * is present, if so it parses the packet label and returns the security
  312. * attributes in @secattr. Returns zero on success, negative values on
  313. * failure.
  314. *
  315. */
  316. int netlbl_skbuff_getattr(const struct sk_buff *skb,
  317. struct netlbl_lsm_secattr *secattr)
  318. {
  319. if (CIPSO_V4_OPTEXIST(skb) &&
  320. cipso_v4_skbuff_getattr(skb, secattr) == 0)
  321. return 0;
  322. return netlbl_unlabel_getattr(secattr);
  323. }
  324. /**
  325. * netlbl_skbuff_err - Handle a LSM error on a sk_buff
  326. * @skb: the packet
  327. * @error: the error code
  328. *
  329. * Description:
  330. * Deal with a LSM problem when handling the packet in @skb, typically this is
  331. * a permission denied problem (-EACCES). The correct action is determined
  332. * according to the packet's labeling protocol.
  333. *
  334. */
  335. void netlbl_skbuff_err(struct sk_buff *skb, int error)
  336. {
  337. if (CIPSO_V4_OPTEXIST(skb))
  338. cipso_v4_error(skb, error, 0);
  339. }
  340. /**
  341. * netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches
  342. *
  343. * Description:
  344. * For all of the NetLabel protocols that support some form of label mapping
  345. * cache, invalidate the cache. Returns zero on success, negative values on
  346. * error.
  347. *
  348. */
  349. void netlbl_cache_invalidate(void)
  350. {
  351. cipso_v4_cache_invalidate();
  352. }
  353. /**
  354. * netlbl_cache_add - Add an entry to a NetLabel protocol cache
  355. * @skb: the packet
  356. * @secattr: the packet's security attributes
  357. *
  358. * Description:
  359. * Add the LSM security attributes for the given packet to the underlying
  360. * NetLabel protocol's label mapping cache. Returns zero on success, negative
  361. * values on error.
  362. *
  363. */
  364. int netlbl_cache_add(const struct sk_buff *skb,
  365. const struct netlbl_lsm_secattr *secattr)
  366. {
  367. if ((secattr->flags & NETLBL_SECATTR_CACHE) == 0)
  368. return -ENOMSG;
  369. if (CIPSO_V4_OPTEXIST(skb))
  370. return cipso_v4_cache_add(skb, secattr);
  371. return -ENOMSG;
  372. }
  373. /*
  374. * Setup Functions
  375. */
  376. /**
  377. * netlbl_init - Initialize NetLabel
  378. *
  379. * Description:
  380. * Perform the required NetLabel initialization before first use.
  381. *
  382. */
  383. static int __init netlbl_init(void)
  384. {
  385. int ret_val;
  386. printk(KERN_INFO "NetLabel: Initializing\n");
  387. printk(KERN_INFO "NetLabel: domain hash size = %u\n",
  388. (1 << NETLBL_DOMHSH_BITSIZE));
  389. printk(KERN_INFO "NetLabel: protocols ="
  390. " UNLABELED"
  391. " CIPSOv4"
  392. "\n");
  393. ret_val = netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE);
  394. if (ret_val != 0)
  395. goto init_failure;
  396. ret_val = netlbl_netlink_init();
  397. if (ret_val != 0)
  398. goto init_failure;
  399. ret_val = netlbl_unlabel_defconf();
  400. if (ret_val != 0)
  401. goto init_failure;
  402. printk(KERN_INFO "NetLabel: unlabeled traffic allowed by default\n");
  403. return 0;
  404. init_failure:
  405. panic("NetLabel: failed to initialize properly (%d)\n", ret_val);
  406. }
  407. subsys_initcall(netlbl_init);