netlabel_kapi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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, 2008
  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 <linux/audit.h>
  32. #include <linux/in.h>
  33. #include <linux/in6.h>
  34. #include <net/ip.h>
  35. #include <net/ipv6.h>
  36. #include <net/netlabel.h>
  37. #include <net/cipso_ipv4.h>
  38. #include <asm/bug.h>
  39. #include <asm/atomic.h>
  40. #include "netlabel_domainhash.h"
  41. #include "netlabel_unlabeled.h"
  42. #include "netlabel_cipso_v4.h"
  43. #include "netlabel_user.h"
  44. #include "netlabel_mgmt.h"
  45. #include "netlabel_addrlist.h"
  46. /*
  47. * Configuration Functions
  48. */
  49. /**
  50. * netlbl_cfg_map_del - Remove a NetLabel/LSM domain mapping
  51. * @domain: the domain mapping to remove
  52. * @family: address family
  53. * @addr: IP address
  54. * @mask: IP address mask
  55. * @audit_info: NetLabel audit information
  56. *
  57. * Description:
  58. * Removes a NetLabel/LSM domain mapping. A @domain value of NULL causes the
  59. * default domain mapping to be removed. Returns zero on success, negative
  60. * values on failure.
  61. *
  62. */
  63. int netlbl_cfg_map_del(const char *domain,
  64. u16 family,
  65. const void *addr,
  66. const void *mask,
  67. struct netlbl_audit *audit_info)
  68. {
  69. if (addr == NULL && mask == NULL) {
  70. return netlbl_domhsh_remove(domain, audit_info);
  71. } else if (addr != NULL && mask != NULL) {
  72. switch (family) {
  73. case AF_INET:
  74. return netlbl_domhsh_remove_af4(domain, addr, mask,
  75. audit_info);
  76. default:
  77. return -EPFNOSUPPORT;
  78. }
  79. } else
  80. return -EINVAL;
  81. }
  82. /**
  83. * netlbl_cfg_unlbl_map_add - Add a new unlabeled mapping
  84. * @domain: the domain mapping to add
  85. * @family: address family
  86. * @addr: IP address
  87. * @mask: IP address mask
  88. * @audit_info: NetLabel audit information
  89. *
  90. * Description:
  91. * Adds a new unlabeled NetLabel/LSM domain mapping. A @domain value of NULL
  92. * causes a new default domain mapping to be added. Returns zero on success,
  93. * negative values on failure.
  94. *
  95. */
  96. int netlbl_cfg_unlbl_map_add(const char *domain,
  97. u16 family,
  98. const void *addr,
  99. const void *mask,
  100. struct netlbl_audit *audit_info)
  101. {
  102. int ret_val = -ENOMEM;
  103. struct netlbl_dom_map *entry;
  104. struct netlbl_domaddr_map *addrmap = NULL;
  105. struct netlbl_domaddr4_map *map4 = NULL;
  106. struct netlbl_domaddr6_map *map6 = NULL;
  107. const struct in_addr *addr4, *mask4;
  108. const struct in6_addr *addr6, *mask6;
  109. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  110. if (entry == NULL)
  111. return -ENOMEM;
  112. if (domain != NULL) {
  113. entry->domain = kstrdup(domain, GFP_ATOMIC);
  114. if (entry->domain == NULL)
  115. goto cfg_unlbl_map_add_failure;
  116. }
  117. if (addr == NULL && mask == NULL)
  118. entry->type = NETLBL_NLTYPE_UNLABELED;
  119. else if (addr != NULL && mask != NULL) {
  120. addrmap = kzalloc(sizeof(*addrmap), GFP_ATOMIC);
  121. if (addrmap == NULL)
  122. goto cfg_unlbl_map_add_failure;
  123. INIT_LIST_HEAD(&addrmap->list4);
  124. INIT_LIST_HEAD(&addrmap->list6);
  125. switch (family) {
  126. case AF_INET:
  127. addr4 = addr;
  128. mask4 = mask;
  129. map4 = kzalloc(sizeof(*map4), GFP_ATOMIC);
  130. if (map4 == NULL)
  131. goto cfg_unlbl_map_add_failure;
  132. map4->type = NETLBL_NLTYPE_UNLABELED;
  133. map4->list.addr = addr4->s_addr & mask4->s_addr;
  134. map4->list.mask = mask4->s_addr;
  135. map4->list.valid = 1;
  136. ret_val = netlbl_af4list_add(&map4->list,
  137. &addrmap->list4);
  138. if (ret_val != 0)
  139. goto cfg_unlbl_map_add_failure;
  140. break;
  141. case AF_INET6:
  142. addr6 = addr;
  143. mask6 = mask;
  144. map6 = kzalloc(sizeof(*map6), GFP_ATOMIC);
  145. if (map4 == NULL)
  146. goto cfg_unlbl_map_add_failure;
  147. map6->type = NETLBL_NLTYPE_UNLABELED;
  148. ipv6_addr_copy(&map6->list.addr, addr6);
  149. map6->list.addr.s6_addr32[0] &= mask6->s6_addr32[0];
  150. map6->list.addr.s6_addr32[1] &= mask6->s6_addr32[1];
  151. map6->list.addr.s6_addr32[2] &= mask6->s6_addr32[2];
  152. map6->list.addr.s6_addr32[3] &= mask6->s6_addr32[3];
  153. ipv6_addr_copy(&map6->list.mask, mask6);
  154. map6->list.valid = 1;
  155. ret_val = netlbl_af4list_add(&map4->list,
  156. &addrmap->list4);
  157. if (ret_val != 0)
  158. goto cfg_unlbl_map_add_failure;
  159. break;
  160. default:
  161. goto cfg_unlbl_map_add_failure;
  162. break;
  163. }
  164. entry->type_def.addrsel = addrmap;
  165. entry->type = NETLBL_NLTYPE_ADDRSELECT;
  166. } else {
  167. ret_val = -EINVAL;
  168. goto cfg_unlbl_map_add_failure;
  169. }
  170. ret_val = netlbl_domhsh_add(entry, audit_info);
  171. if (ret_val != 0)
  172. goto cfg_unlbl_map_add_failure;
  173. return 0;
  174. cfg_unlbl_map_add_failure:
  175. if (entry != NULL)
  176. kfree(entry->domain);
  177. kfree(entry);
  178. kfree(addrmap);
  179. kfree(map4);
  180. kfree(map6);
  181. return ret_val;
  182. }
  183. /**
  184. * netlbl_cfg_unlbl_static_add - Adds a new static label
  185. * @net: network namespace
  186. * @dev_name: interface name
  187. * @addr: IP address in network byte order (struct in[6]_addr)
  188. * @mask: address mask in network byte order (struct in[6]_addr)
  189. * @family: address family
  190. * @secid: LSM secid value for the entry
  191. * @audit_info: NetLabel audit information
  192. *
  193. * Description:
  194. * Adds a new NetLabel static label to be used when protocol provided labels
  195. * are not present on incoming traffic. If @dev_name is NULL then the default
  196. * interface will be used. Returns zero on success, negative values on failure.
  197. *
  198. */
  199. int netlbl_cfg_unlbl_static_add(struct net *net,
  200. const char *dev_name,
  201. const void *addr,
  202. const void *mask,
  203. u16 family,
  204. u32 secid,
  205. struct netlbl_audit *audit_info)
  206. {
  207. u32 addr_len;
  208. switch (family) {
  209. case AF_INET:
  210. addr_len = sizeof(struct in_addr);
  211. break;
  212. case AF_INET6:
  213. addr_len = sizeof(struct in6_addr);
  214. break;
  215. default:
  216. return -EPFNOSUPPORT;
  217. }
  218. return netlbl_unlhsh_add(net,
  219. dev_name, addr, mask, addr_len,
  220. secid, audit_info);
  221. }
  222. /**
  223. * netlbl_cfg_unlbl_static_del - Removes an existing static label
  224. * @net: network namespace
  225. * @dev_name: interface name
  226. * @addr: IP address in network byte order (struct in[6]_addr)
  227. * @mask: address mask in network byte order (struct in[6]_addr)
  228. * @family: address family
  229. * @secid: LSM secid value for the entry
  230. * @audit_info: NetLabel audit information
  231. *
  232. * Description:
  233. * Removes an existing NetLabel static label used when protocol provided labels
  234. * are not present on incoming traffic. If @dev_name is NULL then the default
  235. * interface will be used. Returns zero on success, negative values on failure.
  236. *
  237. */
  238. int netlbl_cfg_unlbl_static_del(struct net *net,
  239. const char *dev_name,
  240. const void *addr,
  241. const void *mask,
  242. u16 family,
  243. struct netlbl_audit *audit_info)
  244. {
  245. u32 addr_len;
  246. switch (family) {
  247. case AF_INET:
  248. addr_len = sizeof(struct in_addr);
  249. break;
  250. case AF_INET6:
  251. addr_len = sizeof(struct in6_addr);
  252. break;
  253. default:
  254. return -EPFNOSUPPORT;
  255. }
  256. return netlbl_unlhsh_remove(net,
  257. dev_name, addr, mask, addr_len,
  258. audit_info);
  259. }
  260. /**
  261. * netlbl_cfg_cipsov4_add - Add a new CIPSOv4 DOI definition
  262. * @doi_def: CIPSO DOI definition
  263. * @audit_info: NetLabel audit information
  264. *
  265. * Description:
  266. * Add a new CIPSO DOI definition as defined by @doi_def. Returns zero on
  267. * success and negative values on failure.
  268. *
  269. */
  270. int netlbl_cfg_cipsov4_add(struct cipso_v4_doi *doi_def,
  271. struct netlbl_audit *audit_info)
  272. {
  273. return cipso_v4_doi_add(doi_def, audit_info);
  274. }
  275. /**
  276. * netlbl_cfg_cipsov4_del - Remove an existing CIPSOv4 DOI definition
  277. * @doi: CIPSO DOI
  278. * @audit_info: NetLabel audit information
  279. *
  280. * Description:
  281. * Remove an existing CIPSO DOI definition matching @doi. Returns zero on
  282. * success and negative values on failure.
  283. *
  284. */
  285. void netlbl_cfg_cipsov4_del(u32 doi, struct netlbl_audit *audit_info)
  286. {
  287. cipso_v4_doi_remove(doi, audit_info);
  288. }
  289. /**
  290. * netlbl_cfg_cipsov4_map_add - Add a new CIPSOv4 DOI mapping
  291. * @doi: the CIPSO DOI
  292. * @domain: the domain mapping to add
  293. * @addr: IP address
  294. * @mask: IP address mask
  295. * @audit_info: NetLabel audit information
  296. *
  297. * Description:
  298. * Add a new NetLabel/LSM domain mapping for the given CIPSO DOI to the NetLabel
  299. * subsystem. A @domain value of NULL adds a new default domain mapping.
  300. * Returns zero on success, negative values on failure.
  301. *
  302. */
  303. int netlbl_cfg_cipsov4_map_add(u32 doi,
  304. const char *domain,
  305. const struct in_addr *addr,
  306. const struct in_addr *mask,
  307. struct netlbl_audit *audit_info)
  308. {
  309. int ret_val = -ENOMEM;
  310. struct cipso_v4_doi *doi_def;
  311. struct netlbl_dom_map *entry;
  312. struct netlbl_domaddr_map *addrmap = NULL;
  313. struct netlbl_domaddr4_map *addrinfo = NULL;
  314. doi_def = cipso_v4_doi_getdef(doi);
  315. if (doi_def == NULL)
  316. return -ENOENT;
  317. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  318. if (entry == NULL)
  319. return -ENOMEM;
  320. if (domain != NULL) {
  321. entry->domain = kstrdup(domain, GFP_ATOMIC);
  322. if (entry->domain == NULL)
  323. goto cfg_cipsov4_map_add_failure;
  324. }
  325. if (addr == NULL && mask == NULL) {
  326. entry->type_def.cipsov4 = doi_def;
  327. entry->type = NETLBL_NLTYPE_CIPSOV4;
  328. } else if (addr != NULL && mask != NULL) {
  329. addrmap = kzalloc(sizeof(*addrmap), GFP_ATOMIC);
  330. if (addrmap == NULL)
  331. goto cfg_cipsov4_map_add_failure;
  332. INIT_LIST_HEAD(&addrmap->list4);
  333. INIT_LIST_HEAD(&addrmap->list6);
  334. addrinfo = kzalloc(sizeof(*addrinfo), GFP_ATOMIC);
  335. if (addrinfo == NULL)
  336. goto cfg_cipsov4_map_add_failure;
  337. addrinfo->type_def.cipsov4 = doi_def;
  338. addrinfo->type = NETLBL_NLTYPE_CIPSOV4;
  339. addrinfo->list.addr = addr->s_addr & mask->s_addr;
  340. addrinfo->list.mask = mask->s_addr;
  341. addrinfo->list.valid = 1;
  342. ret_val = netlbl_af4list_add(&addrinfo->list, &addrmap->list4);
  343. if (ret_val != 0)
  344. goto cfg_cipsov4_map_add_failure;
  345. entry->type_def.addrsel = addrmap;
  346. entry->type = NETLBL_NLTYPE_ADDRSELECT;
  347. } else {
  348. ret_val = -EINVAL;
  349. goto cfg_cipsov4_map_add_failure;
  350. }
  351. ret_val = netlbl_domhsh_add(entry, audit_info);
  352. if (ret_val != 0)
  353. goto cfg_cipsov4_map_add_failure;
  354. return 0;
  355. cfg_cipsov4_map_add_failure:
  356. cipso_v4_doi_putdef(doi_def);
  357. if (entry != NULL)
  358. kfree(entry->domain);
  359. kfree(entry);
  360. kfree(addrmap);
  361. kfree(addrinfo);
  362. return ret_val;
  363. }
  364. /*
  365. * Security Attribute Functions
  366. */
  367. /**
  368. * netlbl_secattr_catmap_walk - Walk a LSM secattr catmap looking for a bit
  369. * @catmap: the category bitmap
  370. * @offset: the offset to start searching at, in bits
  371. *
  372. * Description:
  373. * This function walks a LSM secattr category bitmap starting at @offset and
  374. * returns the spot of the first set bit or -ENOENT if no bits are set.
  375. *
  376. */
  377. int netlbl_secattr_catmap_walk(struct netlbl_lsm_secattr_catmap *catmap,
  378. u32 offset)
  379. {
  380. struct netlbl_lsm_secattr_catmap *iter = catmap;
  381. u32 node_idx;
  382. u32 node_bit;
  383. NETLBL_CATMAP_MAPTYPE bitmap;
  384. if (offset > iter->startbit) {
  385. while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
  386. iter = iter->next;
  387. if (iter == NULL)
  388. return -ENOENT;
  389. }
  390. node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
  391. node_bit = offset - iter->startbit -
  392. (NETLBL_CATMAP_MAPSIZE * node_idx);
  393. } else {
  394. node_idx = 0;
  395. node_bit = 0;
  396. }
  397. bitmap = iter->bitmap[node_idx] >> node_bit;
  398. for (;;) {
  399. if (bitmap != 0) {
  400. while ((bitmap & NETLBL_CATMAP_BIT) == 0) {
  401. bitmap >>= 1;
  402. node_bit++;
  403. }
  404. return iter->startbit +
  405. (NETLBL_CATMAP_MAPSIZE * node_idx) + node_bit;
  406. }
  407. if (++node_idx >= NETLBL_CATMAP_MAPCNT) {
  408. if (iter->next != NULL) {
  409. iter = iter->next;
  410. node_idx = 0;
  411. } else
  412. return -ENOENT;
  413. }
  414. bitmap = iter->bitmap[node_idx];
  415. node_bit = 0;
  416. }
  417. return -ENOENT;
  418. }
  419. /**
  420. * netlbl_secattr_catmap_walk_rng - Find the end of a string of set bits
  421. * @catmap: the category bitmap
  422. * @offset: the offset to start searching at, in bits
  423. *
  424. * Description:
  425. * This function walks a LSM secattr category bitmap starting at @offset and
  426. * returns the spot of the first cleared bit or -ENOENT if the offset is past
  427. * the end of the bitmap.
  428. *
  429. */
  430. int netlbl_secattr_catmap_walk_rng(struct netlbl_lsm_secattr_catmap *catmap,
  431. u32 offset)
  432. {
  433. struct netlbl_lsm_secattr_catmap *iter = catmap;
  434. u32 node_idx;
  435. u32 node_bit;
  436. NETLBL_CATMAP_MAPTYPE bitmask;
  437. NETLBL_CATMAP_MAPTYPE bitmap;
  438. if (offset > iter->startbit) {
  439. while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
  440. iter = iter->next;
  441. if (iter == NULL)
  442. return -ENOENT;
  443. }
  444. node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
  445. node_bit = offset - iter->startbit -
  446. (NETLBL_CATMAP_MAPSIZE * node_idx);
  447. } else {
  448. node_idx = 0;
  449. node_bit = 0;
  450. }
  451. bitmask = NETLBL_CATMAP_BIT << node_bit;
  452. for (;;) {
  453. bitmap = iter->bitmap[node_idx];
  454. while (bitmask != 0 && (bitmap & bitmask) != 0) {
  455. bitmask <<= 1;
  456. node_bit++;
  457. }
  458. if (bitmask != 0)
  459. return iter->startbit +
  460. (NETLBL_CATMAP_MAPSIZE * node_idx) +
  461. node_bit - 1;
  462. else if (++node_idx >= NETLBL_CATMAP_MAPCNT) {
  463. if (iter->next == NULL)
  464. return iter->startbit + NETLBL_CATMAP_SIZE - 1;
  465. iter = iter->next;
  466. node_idx = 0;
  467. }
  468. bitmask = NETLBL_CATMAP_BIT;
  469. node_bit = 0;
  470. }
  471. return -ENOENT;
  472. }
  473. /**
  474. * netlbl_secattr_catmap_setbit - Set a bit in a LSM secattr catmap
  475. * @catmap: the category bitmap
  476. * @bit: the bit to set
  477. * @flags: memory allocation flags
  478. *
  479. * Description:
  480. * Set the bit specified by @bit in @catmap. Returns zero on success,
  481. * negative values on failure.
  482. *
  483. */
  484. int netlbl_secattr_catmap_setbit(struct netlbl_lsm_secattr_catmap *catmap,
  485. u32 bit,
  486. gfp_t flags)
  487. {
  488. struct netlbl_lsm_secattr_catmap *iter = catmap;
  489. u32 node_bit;
  490. u32 node_idx;
  491. while (iter->next != NULL &&
  492. bit >= (iter->startbit + NETLBL_CATMAP_SIZE))
  493. iter = iter->next;
  494. if (bit >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
  495. iter->next = netlbl_secattr_catmap_alloc(flags);
  496. if (iter->next == NULL)
  497. return -ENOMEM;
  498. iter = iter->next;
  499. iter->startbit = bit & ~(NETLBL_CATMAP_SIZE - 1);
  500. }
  501. /* gcc always rounds to zero when doing integer division */
  502. node_idx = (bit - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
  503. node_bit = bit - iter->startbit - (NETLBL_CATMAP_MAPSIZE * node_idx);
  504. iter->bitmap[node_idx] |= NETLBL_CATMAP_BIT << node_bit;
  505. return 0;
  506. }
  507. /**
  508. * netlbl_secattr_catmap_setrng - Set a range of bits in a LSM secattr catmap
  509. * @catmap: the category bitmap
  510. * @start: the starting bit
  511. * @end: the last bit in the string
  512. * @flags: memory allocation flags
  513. *
  514. * Description:
  515. * Set a range of bits, starting at @start and ending with @end. Returns zero
  516. * on success, negative values on failure.
  517. *
  518. */
  519. int netlbl_secattr_catmap_setrng(struct netlbl_lsm_secattr_catmap *catmap,
  520. u32 start,
  521. u32 end,
  522. gfp_t flags)
  523. {
  524. int ret_val = 0;
  525. struct netlbl_lsm_secattr_catmap *iter = catmap;
  526. u32 iter_max_spot;
  527. u32 spot;
  528. /* XXX - This could probably be made a bit faster by combining writes
  529. * to the catmap instead of setting a single bit each time, but for
  530. * right now skipping to the start of the range in the catmap should
  531. * be a nice improvement over calling the individual setbit function
  532. * repeatedly from a loop. */
  533. while (iter->next != NULL &&
  534. start >= (iter->startbit + NETLBL_CATMAP_SIZE))
  535. iter = iter->next;
  536. iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE;
  537. for (spot = start; spot <= end && ret_val == 0; spot++) {
  538. if (spot >= iter_max_spot && iter->next != NULL) {
  539. iter = iter->next;
  540. iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE;
  541. }
  542. ret_val = netlbl_secattr_catmap_setbit(iter, spot, GFP_ATOMIC);
  543. }
  544. return ret_val;
  545. }
  546. /*
  547. * LSM Functions
  548. */
  549. /**
  550. * netlbl_enabled - Determine if the NetLabel subsystem is enabled
  551. *
  552. * Description:
  553. * The LSM can use this function to determine if it should use NetLabel
  554. * security attributes in it's enforcement mechanism. Currently, NetLabel is
  555. * considered to be enabled when it's configuration contains a valid setup for
  556. * at least one labeled protocol (i.e. NetLabel can understand incoming
  557. * labeled packets of at least one type); otherwise NetLabel is considered to
  558. * be disabled.
  559. *
  560. */
  561. int netlbl_enabled(void)
  562. {
  563. /* At some point we probably want to expose this mechanism to the user
  564. * as well so that admins can toggle NetLabel regardless of the
  565. * configuration */
  566. return (atomic_read(&netlabel_mgmt_protocount) > 0);
  567. }
  568. /**
  569. * netlbl_socket_setattr - Label a socket using the correct protocol
  570. * @sk: the socket to label
  571. * @secattr: the security attributes
  572. *
  573. * Description:
  574. * Attach the correct label to the given socket using the security attributes
  575. * specified in @secattr. This function requires exclusive access to @sk,
  576. * which means it either needs to be in the process of being created or locked.
  577. * Returns zero on success, -EDESTADDRREQ if the domain is configured to use
  578. * network address selectors (can't blindly label the socket), and negative
  579. * values on all other failures.
  580. *
  581. */
  582. int netlbl_sock_setattr(struct sock *sk,
  583. const struct netlbl_lsm_secattr *secattr)
  584. {
  585. int ret_val = -ENOENT;
  586. struct netlbl_dom_map *dom_entry;
  587. rcu_read_lock();
  588. dom_entry = netlbl_domhsh_getentry(secattr->domain);
  589. if (dom_entry == NULL)
  590. goto socket_setattr_return;
  591. switch (dom_entry->type) {
  592. case NETLBL_NLTYPE_ADDRSELECT:
  593. ret_val = -EDESTADDRREQ;
  594. break;
  595. case NETLBL_NLTYPE_CIPSOV4:
  596. ret_val = cipso_v4_sock_setattr(sk,
  597. dom_entry->type_def.cipsov4,
  598. secattr);
  599. break;
  600. case NETLBL_NLTYPE_UNLABELED:
  601. ret_val = 0;
  602. break;
  603. default:
  604. ret_val = -ENOENT;
  605. }
  606. socket_setattr_return:
  607. rcu_read_unlock();
  608. return ret_val;
  609. }
  610. /**
  611. * netlbl_sock_delattr - Delete all the NetLabel labels on a socket
  612. * @sk: the socket
  613. *
  614. * Description:
  615. * Remove all the NetLabel labeling from @sk. The caller is responsible for
  616. * ensuring that @sk is locked.
  617. *
  618. */
  619. void netlbl_sock_delattr(struct sock *sk)
  620. {
  621. cipso_v4_sock_delattr(sk);
  622. }
  623. /**
  624. * netlbl_sock_getattr - Determine the security attributes of a sock
  625. * @sk: the sock
  626. * @secattr: the security attributes
  627. *
  628. * Description:
  629. * Examines the given sock to see if any NetLabel style labeling has been
  630. * applied to the sock, if so it parses the socket label and returns the
  631. * security attributes in @secattr. Returns zero on success, negative values
  632. * on failure.
  633. *
  634. */
  635. int netlbl_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
  636. {
  637. return cipso_v4_sock_getattr(sk, secattr);
  638. }
  639. /**
  640. * netlbl_conn_setattr - Label a connected socket using the correct protocol
  641. * @sk: the socket to label
  642. * @addr: the destination address
  643. * @secattr: the security attributes
  644. *
  645. * Description:
  646. * Attach the correct label to the given connected socket using the security
  647. * attributes specified in @secattr. The caller is responsible for ensuring
  648. * that @sk is locked. Returns zero on success, negative values on failure.
  649. *
  650. */
  651. int netlbl_conn_setattr(struct sock *sk,
  652. struct sockaddr *addr,
  653. const struct netlbl_lsm_secattr *secattr)
  654. {
  655. int ret_val;
  656. struct sockaddr_in *addr4;
  657. struct netlbl_domaddr4_map *af4_entry;
  658. rcu_read_lock();
  659. switch (addr->sa_family) {
  660. case AF_INET:
  661. addr4 = (struct sockaddr_in *)addr;
  662. af4_entry = netlbl_domhsh_getentry_af4(secattr->domain,
  663. addr4->sin_addr.s_addr);
  664. if (af4_entry == NULL) {
  665. ret_val = -ENOENT;
  666. goto conn_setattr_return;
  667. }
  668. switch (af4_entry->type) {
  669. case NETLBL_NLTYPE_CIPSOV4:
  670. ret_val = cipso_v4_sock_setattr(sk,
  671. af4_entry->type_def.cipsov4,
  672. secattr);
  673. break;
  674. case NETLBL_NLTYPE_UNLABELED:
  675. /* just delete the protocols we support for right now
  676. * but we could remove other protocols if needed */
  677. cipso_v4_sock_delattr(sk);
  678. ret_val = 0;
  679. break;
  680. default:
  681. ret_val = -ENOENT;
  682. }
  683. break;
  684. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  685. case AF_INET6:
  686. /* since we don't support any IPv6 labeling protocols right
  687. * now we can optimize everything away until we do */
  688. ret_val = 0;
  689. break;
  690. #endif /* IPv6 */
  691. default:
  692. ret_val = 0;
  693. }
  694. conn_setattr_return:
  695. rcu_read_unlock();
  696. return ret_val;
  697. }
  698. /**
  699. * netlbl_skbuff_setattr - Label a packet using the correct protocol
  700. * @skb: the packet
  701. * @family: protocol family
  702. * @secattr: the security attributes
  703. *
  704. * Description:
  705. * Attach the correct label to the given packet using the security attributes
  706. * specified in @secattr. Returns zero on success, negative values on failure.
  707. *
  708. */
  709. int netlbl_skbuff_setattr(struct sk_buff *skb,
  710. u16 family,
  711. const struct netlbl_lsm_secattr *secattr)
  712. {
  713. int ret_val;
  714. struct iphdr *hdr4;
  715. struct netlbl_domaddr4_map *af4_entry;
  716. rcu_read_lock();
  717. switch (family) {
  718. case AF_INET:
  719. hdr4 = ip_hdr(skb);
  720. af4_entry = netlbl_domhsh_getentry_af4(secattr->domain,
  721. hdr4->daddr);
  722. if (af4_entry == NULL) {
  723. ret_val = -ENOENT;
  724. goto skbuff_setattr_return;
  725. }
  726. switch (af4_entry->type) {
  727. case NETLBL_NLTYPE_CIPSOV4:
  728. ret_val = cipso_v4_skbuff_setattr(skb,
  729. af4_entry->type_def.cipsov4,
  730. secattr);
  731. break;
  732. case NETLBL_NLTYPE_UNLABELED:
  733. /* just delete the protocols we support for right now
  734. * but we could remove other protocols if needed */
  735. ret_val = cipso_v4_skbuff_delattr(skb);
  736. break;
  737. default:
  738. ret_val = -ENOENT;
  739. }
  740. break;
  741. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  742. case AF_INET6:
  743. /* since we don't support any IPv6 labeling protocols right
  744. * now we can optimize everything away until we do */
  745. ret_val = 0;
  746. break;
  747. #endif /* IPv6 */
  748. default:
  749. ret_val = 0;
  750. }
  751. skbuff_setattr_return:
  752. rcu_read_unlock();
  753. return ret_val;
  754. }
  755. /**
  756. * netlbl_skbuff_getattr - Determine the security attributes of a packet
  757. * @skb: the packet
  758. * @family: protocol family
  759. * @secattr: the security attributes
  760. *
  761. * Description:
  762. * Examines the given packet to see if a recognized form of packet labeling
  763. * is present, if so it parses the packet label and returns the security
  764. * attributes in @secattr. Returns zero on success, negative values on
  765. * failure.
  766. *
  767. */
  768. int netlbl_skbuff_getattr(const struct sk_buff *skb,
  769. u16 family,
  770. struct netlbl_lsm_secattr *secattr)
  771. {
  772. if (CIPSO_V4_OPTEXIST(skb) &&
  773. cipso_v4_skbuff_getattr(skb, secattr) == 0)
  774. return 0;
  775. return netlbl_unlabel_getattr(skb, family, secattr);
  776. }
  777. /**
  778. * netlbl_skbuff_err - Handle a LSM error on a sk_buff
  779. * @skb: the packet
  780. * @error: the error code
  781. * @gateway: true if host is acting as a gateway, false otherwise
  782. *
  783. * Description:
  784. * Deal with a LSM problem when handling the packet in @skb, typically this is
  785. * a permission denied problem (-EACCES). The correct action is determined
  786. * according to the packet's labeling protocol.
  787. *
  788. */
  789. void netlbl_skbuff_err(struct sk_buff *skb, int error, int gateway)
  790. {
  791. if (CIPSO_V4_OPTEXIST(skb))
  792. cipso_v4_error(skb, error, gateway);
  793. }
  794. /**
  795. * netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches
  796. *
  797. * Description:
  798. * For all of the NetLabel protocols that support some form of label mapping
  799. * cache, invalidate the cache. Returns zero on success, negative values on
  800. * error.
  801. *
  802. */
  803. void netlbl_cache_invalidate(void)
  804. {
  805. cipso_v4_cache_invalidate();
  806. }
  807. /**
  808. * netlbl_cache_add - Add an entry to a NetLabel protocol cache
  809. * @skb: the packet
  810. * @secattr: the packet's security attributes
  811. *
  812. * Description:
  813. * Add the LSM security attributes for the given packet to the underlying
  814. * NetLabel protocol's label mapping cache. Returns zero on success, negative
  815. * values on error.
  816. *
  817. */
  818. int netlbl_cache_add(const struct sk_buff *skb,
  819. const struct netlbl_lsm_secattr *secattr)
  820. {
  821. if ((secattr->flags & NETLBL_SECATTR_CACHE) == 0)
  822. return -ENOMSG;
  823. if (CIPSO_V4_OPTEXIST(skb))
  824. return cipso_v4_cache_add(skb, secattr);
  825. return -ENOMSG;
  826. }
  827. /*
  828. * Protocol Engine Functions
  829. */
  830. /**
  831. * netlbl_audit_start - Start an audit message
  832. * @type: audit message type
  833. * @audit_info: NetLabel audit information
  834. *
  835. * Description:
  836. * Start an audit message using the type specified in @type and fill the audit
  837. * message with some fields common to all NetLabel audit messages. This
  838. * function should only be used by protocol engines, not LSMs. Returns a
  839. * pointer to the audit buffer on success, NULL on failure.
  840. *
  841. */
  842. struct audit_buffer *netlbl_audit_start(int type,
  843. struct netlbl_audit *audit_info)
  844. {
  845. return netlbl_audit_start_common(type, audit_info);
  846. }
  847. /*
  848. * Setup Functions
  849. */
  850. /**
  851. * netlbl_init - Initialize NetLabel
  852. *
  853. * Description:
  854. * Perform the required NetLabel initialization before first use.
  855. *
  856. */
  857. static int __init netlbl_init(void)
  858. {
  859. int ret_val;
  860. printk(KERN_INFO "NetLabel: Initializing\n");
  861. printk(KERN_INFO "NetLabel: domain hash size = %u\n",
  862. (1 << NETLBL_DOMHSH_BITSIZE));
  863. printk(KERN_INFO "NetLabel: protocols ="
  864. " UNLABELED"
  865. " CIPSOv4"
  866. "\n");
  867. ret_val = netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE);
  868. if (ret_val != 0)
  869. goto init_failure;
  870. ret_val = netlbl_unlabel_init(NETLBL_UNLHSH_BITSIZE);
  871. if (ret_val != 0)
  872. goto init_failure;
  873. ret_val = netlbl_netlink_init();
  874. if (ret_val != 0)
  875. goto init_failure;
  876. ret_val = netlbl_unlabel_defconf();
  877. if (ret_val != 0)
  878. goto init_failure;
  879. printk(KERN_INFO "NetLabel: unlabeled traffic allowed by default\n");
  880. return 0;
  881. init_failure:
  882. panic("NetLabel: failed to initialize properly (%d)\n", ret_val);
  883. }
  884. subsys_initcall(netlbl_init);