netlabel_kapi.c 11 KB

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