netlabel_kapi.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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/slab.h>
  32. #include <linux/audit.h>
  33. #include <linux/in.h>
  34. #include <linux/in6.h>
  35. #include <net/ip.h>
  36. #include <net/ipv6.h>
  37. #include <net/netlabel.h>
  38. #include <net/cipso_ipv4.h>
  39. #include <asm/bug.h>
  40. #include <asm/atomic.h>
  41. #include "netlabel_domainhash.h"
  42. #include "netlabel_unlabeled.h"
  43. #include "netlabel_cipso_v4.h"
  44. #include "netlabel_user.h"
  45. #include "netlabel_mgmt.h"
  46. #include "netlabel_addrlist.h"
  47. /*
  48. * Configuration Functions
  49. */
  50. /**
  51. * netlbl_cfg_map_del - Remove a NetLabel/LSM domain mapping
  52. * @domain: the domain mapping to remove
  53. * @family: address family
  54. * @addr: IP address
  55. * @mask: IP address mask
  56. * @audit_info: NetLabel audit information
  57. *
  58. * Description:
  59. * Removes a NetLabel/LSM domain mapping. A @domain value of NULL causes the
  60. * default domain mapping to be removed. Returns zero on success, negative
  61. * values on failure.
  62. *
  63. */
  64. int netlbl_cfg_map_del(const char *domain,
  65. u16 family,
  66. const void *addr,
  67. const void *mask,
  68. struct netlbl_audit *audit_info)
  69. {
  70. if (addr == NULL && mask == NULL) {
  71. return netlbl_domhsh_remove(domain, audit_info);
  72. } else if (addr != NULL && mask != NULL) {
  73. switch (family) {
  74. case AF_INET:
  75. return netlbl_domhsh_remove_af4(domain, addr, mask,
  76. audit_info);
  77. default:
  78. return -EPFNOSUPPORT;
  79. }
  80. } else
  81. return -EINVAL;
  82. }
  83. /**
  84. * netlbl_cfg_unlbl_map_add - Add a new unlabeled mapping
  85. * @domain: the domain mapping to add
  86. * @family: address family
  87. * @addr: IP address
  88. * @mask: IP address mask
  89. * @audit_info: NetLabel audit information
  90. *
  91. * Description:
  92. * Adds a new unlabeled NetLabel/LSM domain mapping. A @domain value of NULL
  93. * causes a new default domain mapping to be added. Returns zero on success,
  94. * negative values on failure.
  95. *
  96. */
  97. int netlbl_cfg_unlbl_map_add(const char *domain,
  98. u16 family,
  99. const void *addr,
  100. const void *mask,
  101. struct netlbl_audit *audit_info)
  102. {
  103. int ret_val = -ENOMEM;
  104. struct netlbl_dom_map *entry;
  105. struct netlbl_domaddr_map *addrmap = NULL;
  106. struct netlbl_domaddr4_map *map4 = NULL;
  107. struct netlbl_domaddr6_map *map6 = NULL;
  108. const struct in_addr *addr4, *mask4;
  109. const struct in6_addr *addr6, *mask6;
  110. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  111. if (entry == NULL)
  112. return -ENOMEM;
  113. if (domain != NULL) {
  114. entry->domain = kstrdup(domain, GFP_ATOMIC);
  115. if (entry->domain == NULL)
  116. goto cfg_unlbl_map_add_failure;
  117. }
  118. if (addr == NULL && mask == NULL)
  119. entry->type = NETLBL_NLTYPE_UNLABELED;
  120. else if (addr != NULL && mask != NULL) {
  121. addrmap = kzalloc(sizeof(*addrmap), GFP_ATOMIC);
  122. if (addrmap == NULL)
  123. goto cfg_unlbl_map_add_failure;
  124. INIT_LIST_HEAD(&addrmap->list4);
  125. INIT_LIST_HEAD(&addrmap->list6);
  126. switch (family) {
  127. case AF_INET:
  128. addr4 = addr;
  129. mask4 = mask;
  130. map4 = kzalloc(sizeof(*map4), GFP_ATOMIC);
  131. if (map4 == NULL)
  132. goto cfg_unlbl_map_add_failure;
  133. map4->type = NETLBL_NLTYPE_UNLABELED;
  134. map4->list.addr = addr4->s_addr & mask4->s_addr;
  135. map4->list.mask = mask4->s_addr;
  136. map4->list.valid = 1;
  137. ret_val = netlbl_af4list_add(&map4->list,
  138. &addrmap->list4);
  139. if (ret_val != 0)
  140. goto cfg_unlbl_map_add_failure;
  141. break;
  142. case AF_INET6:
  143. addr6 = addr;
  144. mask6 = mask;
  145. map6 = kzalloc(sizeof(*map6), GFP_ATOMIC);
  146. if (map6 == NULL)
  147. goto cfg_unlbl_map_add_failure;
  148. map6->type = NETLBL_NLTYPE_UNLABELED;
  149. ipv6_addr_copy(&map6->list.addr, addr6);
  150. map6->list.addr.s6_addr32[0] &= mask6->s6_addr32[0];
  151. map6->list.addr.s6_addr32[1] &= mask6->s6_addr32[1];
  152. map6->list.addr.s6_addr32[2] &= mask6->s6_addr32[2];
  153. map6->list.addr.s6_addr32[3] &= mask6->s6_addr32[3];
  154. ipv6_addr_copy(&map6->list.mask, mask6);
  155. map6->list.valid = 1;
  156. ret_val = netlbl_af4list_add(&map4->list,
  157. &addrmap->list4);
  158. if (ret_val != 0)
  159. goto cfg_unlbl_map_add_failure;
  160. break;
  161. default:
  162. goto cfg_unlbl_map_add_failure;
  163. break;
  164. }
  165. entry->type_def.addrsel = addrmap;
  166. entry->type = NETLBL_NLTYPE_ADDRSELECT;
  167. } else {
  168. ret_val = -EINVAL;
  169. goto cfg_unlbl_map_add_failure;
  170. }
  171. ret_val = netlbl_domhsh_add(entry, audit_info);
  172. if (ret_val != 0)
  173. goto cfg_unlbl_map_add_failure;
  174. return 0;
  175. cfg_unlbl_map_add_failure:
  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. kfree(entry->domain);
  358. kfree(entry);
  359. kfree(addrmap);
  360. kfree(addrinfo);
  361. return ret_val;
  362. }
  363. /*
  364. * Security Attribute Functions
  365. */
  366. /**
  367. * netlbl_secattr_catmap_walk - Walk a LSM secattr catmap looking for a bit
  368. * @catmap: the category bitmap
  369. * @offset: the offset to start searching at, in bits
  370. *
  371. * Description:
  372. * This function walks a LSM secattr category bitmap starting at @offset and
  373. * returns the spot of the first set bit or -ENOENT if no bits are set.
  374. *
  375. */
  376. int netlbl_secattr_catmap_walk(struct netlbl_lsm_secattr_catmap *catmap,
  377. u32 offset)
  378. {
  379. struct netlbl_lsm_secattr_catmap *iter = catmap;
  380. u32 node_idx;
  381. u32 node_bit;
  382. NETLBL_CATMAP_MAPTYPE bitmap;
  383. if (offset > iter->startbit) {
  384. while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
  385. iter = iter->next;
  386. if (iter == NULL)
  387. return -ENOENT;
  388. }
  389. node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
  390. node_bit = offset - iter->startbit -
  391. (NETLBL_CATMAP_MAPSIZE * node_idx);
  392. } else {
  393. node_idx = 0;
  394. node_bit = 0;
  395. }
  396. bitmap = iter->bitmap[node_idx] >> node_bit;
  397. for (;;) {
  398. if (bitmap != 0) {
  399. while ((bitmap & NETLBL_CATMAP_BIT) == 0) {
  400. bitmap >>= 1;
  401. node_bit++;
  402. }
  403. return iter->startbit +
  404. (NETLBL_CATMAP_MAPSIZE * node_idx) + node_bit;
  405. }
  406. if (++node_idx >= NETLBL_CATMAP_MAPCNT) {
  407. if (iter->next != NULL) {
  408. iter = iter->next;
  409. node_idx = 0;
  410. } else
  411. return -ENOENT;
  412. }
  413. bitmap = iter->bitmap[node_idx];
  414. node_bit = 0;
  415. }
  416. return -ENOENT;
  417. }
  418. /**
  419. * netlbl_secattr_catmap_walk_rng - Find the end of a string of set bits
  420. * @catmap: the category bitmap
  421. * @offset: the offset to start searching at, in bits
  422. *
  423. * Description:
  424. * This function walks a LSM secattr category bitmap starting at @offset and
  425. * returns the spot of the first cleared bit or -ENOENT if the offset is past
  426. * the end of the bitmap.
  427. *
  428. */
  429. int netlbl_secattr_catmap_walk_rng(struct netlbl_lsm_secattr_catmap *catmap,
  430. u32 offset)
  431. {
  432. struct netlbl_lsm_secattr_catmap *iter = catmap;
  433. u32 node_idx;
  434. u32 node_bit;
  435. NETLBL_CATMAP_MAPTYPE bitmask;
  436. NETLBL_CATMAP_MAPTYPE bitmap;
  437. if (offset > iter->startbit) {
  438. while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
  439. iter = iter->next;
  440. if (iter == NULL)
  441. return -ENOENT;
  442. }
  443. node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
  444. node_bit = offset - iter->startbit -
  445. (NETLBL_CATMAP_MAPSIZE * node_idx);
  446. } else {
  447. node_idx = 0;
  448. node_bit = 0;
  449. }
  450. bitmask = NETLBL_CATMAP_BIT << node_bit;
  451. for (;;) {
  452. bitmap = iter->bitmap[node_idx];
  453. while (bitmask != 0 && (bitmap & bitmask) != 0) {
  454. bitmask <<= 1;
  455. node_bit++;
  456. }
  457. if (bitmask != 0)
  458. return iter->startbit +
  459. (NETLBL_CATMAP_MAPSIZE * node_idx) +
  460. node_bit - 1;
  461. else if (++node_idx >= NETLBL_CATMAP_MAPCNT) {
  462. if (iter->next == NULL)
  463. return iter->startbit + NETLBL_CATMAP_SIZE - 1;
  464. iter = iter->next;
  465. node_idx = 0;
  466. }
  467. bitmask = NETLBL_CATMAP_BIT;
  468. node_bit = 0;
  469. }
  470. return -ENOENT;
  471. }
  472. /**
  473. * netlbl_secattr_catmap_setbit - Set a bit in a LSM secattr catmap
  474. * @catmap: the category bitmap
  475. * @bit: the bit to set
  476. * @flags: memory allocation flags
  477. *
  478. * Description:
  479. * Set the bit specified by @bit in @catmap. Returns zero on success,
  480. * negative values on failure.
  481. *
  482. */
  483. int netlbl_secattr_catmap_setbit(struct netlbl_lsm_secattr_catmap *catmap,
  484. u32 bit,
  485. gfp_t flags)
  486. {
  487. struct netlbl_lsm_secattr_catmap *iter = catmap;
  488. u32 node_bit;
  489. u32 node_idx;
  490. while (iter->next != NULL &&
  491. bit >= (iter->startbit + NETLBL_CATMAP_SIZE))
  492. iter = iter->next;
  493. if (bit >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
  494. iter->next = netlbl_secattr_catmap_alloc(flags);
  495. if (iter->next == NULL)
  496. return -ENOMEM;
  497. iter = iter->next;
  498. iter->startbit = bit & ~(NETLBL_CATMAP_SIZE - 1);
  499. }
  500. /* gcc always rounds to zero when doing integer division */
  501. node_idx = (bit - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
  502. node_bit = bit - iter->startbit - (NETLBL_CATMAP_MAPSIZE * node_idx);
  503. iter->bitmap[node_idx] |= NETLBL_CATMAP_BIT << node_bit;
  504. return 0;
  505. }
  506. /**
  507. * netlbl_secattr_catmap_setrng - Set a range of bits in a LSM secattr catmap
  508. * @catmap: the category bitmap
  509. * @start: the starting bit
  510. * @end: the last bit in the string
  511. * @flags: memory allocation flags
  512. *
  513. * Description:
  514. * Set a range of bits, starting at @start and ending with @end. Returns zero
  515. * on success, negative values on failure.
  516. *
  517. */
  518. int netlbl_secattr_catmap_setrng(struct netlbl_lsm_secattr_catmap *catmap,
  519. u32 start,
  520. u32 end,
  521. gfp_t flags)
  522. {
  523. int ret_val = 0;
  524. struct netlbl_lsm_secattr_catmap *iter = catmap;
  525. u32 iter_max_spot;
  526. u32 spot;
  527. /* XXX - This could probably be made a bit faster by combining writes
  528. * to the catmap instead of setting a single bit each time, but for
  529. * right now skipping to the start of the range in the catmap should
  530. * be a nice improvement over calling the individual setbit function
  531. * repeatedly from a loop. */
  532. while (iter->next != NULL &&
  533. start >= (iter->startbit + NETLBL_CATMAP_SIZE))
  534. iter = iter->next;
  535. iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE;
  536. for (spot = start; spot <= end && ret_val == 0; spot++) {
  537. if (spot >= iter_max_spot && iter->next != NULL) {
  538. iter = iter->next;
  539. iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE;
  540. }
  541. ret_val = netlbl_secattr_catmap_setbit(iter, spot, GFP_ATOMIC);
  542. }
  543. return ret_val;
  544. }
  545. /*
  546. * LSM Functions
  547. */
  548. /**
  549. * netlbl_enabled - Determine if the NetLabel subsystem is enabled
  550. *
  551. * Description:
  552. * The LSM can use this function to determine if it should use NetLabel
  553. * security attributes in it's enforcement mechanism. Currently, NetLabel is
  554. * considered to be enabled when it's configuration contains a valid setup for
  555. * at least one labeled protocol (i.e. NetLabel can understand incoming
  556. * labeled packets of at least one type); otherwise NetLabel is considered to
  557. * be disabled.
  558. *
  559. */
  560. int netlbl_enabled(void)
  561. {
  562. /* At some point we probably want to expose this mechanism to the user
  563. * as well so that admins can toggle NetLabel regardless of the
  564. * configuration */
  565. return (atomic_read(&netlabel_mgmt_protocount) > 0);
  566. }
  567. /**
  568. * netlbl_sock_setattr - Label a socket using the correct protocol
  569. * @sk: the socket to label
  570. * @family: protocol family
  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. u16 family,
  584. const struct netlbl_lsm_secattr *secattr)
  585. {
  586. int ret_val;
  587. struct netlbl_dom_map *dom_entry;
  588. rcu_read_lock();
  589. dom_entry = netlbl_domhsh_getentry(secattr->domain);
  590. if (dom_entry == NULL) {
  591. ret_val = -ENOENT;
  592. goto socket_setattr_return;
  593. }
  594. switch (family) {
  595. case AF_INET:
  596. switch (dom_entry->type) {
  597. case NETLBL_NLTYPE_ADDRSELECT:
  598. ret_val = -EDESTADDRREQ;
  599. break;
  600. case NETLBL_NLTYPE_CIPSOV4:
  601. ret_val = cipso_v4_sock_setattr(sk,
  602. dom_entry->type_def.cipsov4,
  603. secattr);
  604. break;
  605. case NETLBL_NLTYPE_UNLABELED:
  606. ret_val = 0;
  607. break;
  608. default:
  609. ret_val = -ENOENT;
  610. }
  611. break;
  612. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  613. case AF_INET6:
  614. /* since we don't support any IPv6 labeling protocols right
  615. * now we can optimize everything away until we do */
  616. ret_val = 0;
  617. break;
  618. #endif /* IPv6 */
  619. default:
  620. ret_val = -EPROTONOSUPPORT;
  621. }
  622. socket_setattr_return:
  623. rcu_read_unlock();
  624. return ret_val;
  625. }
  626. /**
  627. * netlbl_sock_delattr - Delete all the NetLabel labels on a socket
  628. * @sk: the socket
  629. *
  630. * Description:
  631. * Remove all the NetLabel labeling from @sk. The caller is responsible for
  632. * ensuring that @sk is locked.
  633. *
  634. */
  635. void netlbl_sock_delattr(struct sock *sk)
  636. {
  637. cipso_v4_sock_delattr(sk);
  638. }
  639. /**
  640. * netlbl_sock_getattr - Determine the security attributes of a sock
  641. * @sk: the sock
  642. * @secattr: the security attributes
  643. *
  644. * Description:
  645. * Examines the given sock to see if any NetLabel style labeling has been
  646. * applied to the sock, if so it parses the socket label and returns the
  647. * security attributes in @secattr. Returns zero on success, negative values
  648. * on failure.
  649. *
  650. */
  651. int netlbl_sock_getattr(struct sock *sk,
  652. struct netlbl_lsm_secattr *secattr)
  653. {
  654. int ret_val;
  655. switch (sk->sk_family) {
  656. case AF_INET:
  657. ret_val = cipso_v4_sock_getattr(sk, secattr);
  658. break;
  659. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  660. case AF_INET6:
  661. ret_val = -ENOMSG;
  662. break;
  663. #endif /* IPv6 */
  664. default:
  665. ret_val = -EPROTONOSUPPORT;
  666. }
  667. return ret_val;
  668. }
  669. /**
  670. * netlbl_conn_setattr - Label a connected socket using the correct protocol
  671. * @sk: the socket to label
  672. * @addr: the destination address
  673. * @secattr: the security attributes
  674. *
  675. * Description:
  676. * Attach the correct label to the given connected socket using the security
  677. * attributes specified in @secattr. The caller is responsible for ensuring
  678. * that @sk is locked. Returns zero on success, negative values on failure.
  679. *
  680. */
  681. int netlbl_conn_setattr(struct sock *sk,
  682. struct sockaddr *addr,
  683. const struct netlbl_lsm_secattr *secattr)
  684. {
  685. int ret_val;
  686. struct sockaddr_in *addr4;
  687. struct netlbl_domaddr4_map *af4_entry;
  688. rcu_read_lock();
  689. switch (addr->sa_family) {
  690. case AF_INET:
  691. addr4 = (struct sockaddr_in *)addr;
  692. af4_entry = netlbl_domhsh_getentry_af4(secattr->domain,
  693. addr4->sin_addr.s_addr);
  694. if (af4_entry == NULL) {
  695. ret_val = -ENOENT;
  696. goto conn_setattr_return;
  697. }
  698. switch (af4_entry->type) {
  699. case NETLBL_NLTYPE_CIPSOV4:
  700. ret_val = cipso_v4_sock_setattr(sk,
  701. af4_entry->type_def.cipsov4,
  702. secattr);
  703. break;
  704. case NETLBL_NLTYPE_UNLABELED:
  705. /* just delete the protocols we support for right now
  706. * but we could remove other protocols if needed */
  707. cipso_v4_sock_delattr(sk);
  708. ret_val = 0;
  709. break;
  710. default:
  711. ret_val = -ENOENT;
  712. }
  713. break;
  714. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  715. case AF_INET6:
  716. /* since we don't support any IPv6 labeling protocols right
  717. * now we can optimize everything away until we do */
  718. ret_val = 0;
  719. break;
  720. #endif /* IPv6 */
  721. default:
  722. ret_val = -EPROTONOSUPPORT;
  723. }
  724. conn_setattr_return:
  725. rcu_read_unlock();
  726. return ret_val;
  727. }
  728. /**
  729. * netlbl_req_setattr - Label a request socket using the correct protocol
  730. * @req: the request socket to label
  731. * @secattr: the security attributes
  732. *
  733. * Description:
  734. * Attach the correct label to the given socket using the security attributes
  735. * specified in @secattr. Returns zero on success, negative values on failure.
  736. *
  737. */
  738. int netlbl_req_setattr(struct request_sock *req,
  739. const struct netlbl_lsm_secattr *secattr)
  740. {
  741. int ret_val;
  742. struct netlbl_dom_map *dom_entry;
  743. struct netlbl_domaddr4_map *af4_entry;
  744. u32 proto_type;
  745. struct cipso_v4_doi *proto_cv4;
  746. rcu_read_lock();
  747. dom_entry = netlbl_domhsh_getentry(secattr->domain);
  748. if (dom_entry == NULL) {
  749. ret_val = -ENOENT;
  750. goto req_setattr_return;
  751. }
  752. switch (req->rsk_ops->family) {
  753. case AF_INET:
  754. if (dom_entry->type == NETLBL_NLTYPE_ADDRSELECT) {
  755. struct inet_request_sock *req_inet = inet_rsk(req);
  756. af4_entry = netlbl_domhsh_getentry_af4(secattr->domain,
  757. req_inet->rmt_addr);
  758. if (af4_entry == NULL) {
  759. ret_val = -ENOENT;
  760. goto req_setattr_return;
  761. }
  762. proto_type = af4_entry->type;
  763. proto_cv4 = af4_entry->type_def.cipsov4;
  764. } else {
  765. proto_type = dom_entry->type;
  766. proto_cv4 = dom_entry->type_def.cipsov4;
  767. }
  768. switch (proto_type) {
  769. case NETLBL_NLTYPE_CIPSOV4:
  770. ret_val = cipso_v4_req_setattr(req, proto_cv4, secattr);
  771. break;
  772. case NETLBL_NLTYPE_UNLABELED:
  773. /* just delete the protocols we support for right now
  774. * but we could remove other protocols if needed */
  775. cipso_v4_req_delattr(req);
  776. ret_val = 0;
  777. break;
  778. default:
  779. ret_val = -ENOENT;
  780. }
  781. break;
  782. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  783. case AF_INET6:
  784. /* since we don't support any IPv6 labeling protocols right
  785. * now we can optimize everything away until we do */
  786. ret_val = 0;
  787. break;
  788. #endif /* IPv6 */
  789. default:
  790. ret_val = -EPROTONOSUPPORT;
  791. }
  792. req_setattr_return:
  793. rcu_read_unlock();
  794. return ret_val;
  795. }
  796. /**
  797. * netlbl_req_delattr - Delete all the NetLabel labels on a socket
  798. * @req: the socket
  799. *
  800. * Description:
  801. * Remove all the NetLabel labeling from @req.
  802. *
  803. */
  804. void netlbl_req_delattr(struct request_sock *req)
  805. {
  806. cipso_v4_req_delattr(req);
  807. }
  808. /**
  809. * netlbl_skbuff_setattr - Label a packet using the correct protocol
  810. * @skb: the packet
  811. * @family: protocol family
  812. * @secattr: the security attributes
  813. *
  814. * Description:
  815. * Attach the correct label to the given packet using the security attributes
  816. * specified in @secattr. Returns zero on success, negative values on failure.
  817. *
  818. */
  819. int netlbl_skbuff_setattr(struct sk_buff *skb,
  820. u16 family,
  821. const struct netlbl_lsm_secattr *secattr)
  822. {
  823. int ret_val;
  824. struct iphdr *hdr4;
  825. struct netlbl_domaddr4_map *af4_entry;
  826. rcu_read_lock();
  827. switch (family) {
  828. case AF_INET:
  829. hdr4 = ip_hdr(skb);
  830. af4_entry = netlbl_domhsh_getentry_af4(secattr->domain,
  831. hdr4->daddr);
  832. if (af4_entry == NULL) {
  833. ret_val = -ENOENT;
  834. goto skbuff_setattr_return;
  835. }
  836. switch (af4_entry->type) {
  837. case NETLBL_NLTYPE_CIPSOV4:
  838. ret_val = cipso_v4_skbuff_setattr(skb,
  839. af4_entry->type_def.cipsov4,
  840. secattr);
  841. break;
  842. case NETLBL_NLTYPE_UNLABELED:
  843. /* just delete the protocols we support for right now
  844. * but we could remove other protocols if needed */
  845. ret_val = cipso_v4_skbuff_delattr(skb);
  846. break;
  847. default:
  848. ret_val = -ENOENT;
  849. }
  850. break;
  851. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  852. case AF_INET6:
  853. /* since we don't support any IPv6 labeling protocols right
  854. * now we can optimize everything away until we do */
  855. ret_val = 0;
  856. break;
  857. #endif /* IPv6 */
  858. default:
  859. ret_val = -EPROTONOSUPPORT;
  860. }
  861. skbuff_setattr_return:
  862. rcu_read_unlock();
  863. return ret_val;
  864. }
  865. /**
  866. * netlbl_skbuff_getattr - Determine the security attributes of a packet
  867. * @skb: the packet
  868. * @family: protocol family
  869. * @secattr: the security attributes
  870. *
  871. * Description:
  872. * Examines the given packet to see if a recognized form of packet labeling
  873. * is present, if so it parses the packet label and returns the security
  874. * attributes in @secattr. Returns zero on success, negative values on
  875. * failure.
  876. *
  877. */
  878. int netlbl_skbuff_getattr(const struct sk_buff *skb,
  879. u16 family,
  880. struct netlbl_lsm_secattr *secattr)
  881. {
  882. switch (family) {
  883. case AF_INET:
  884. if (CIPSO_V4_OPTEXIST(skb) &&
  885. cipso_v4_skbuff_getattr(skb, secattr) == 0)
  886. return 0;
  887. break;
  888. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  889. case AF_INET6:
  890. break;
  891. #endif /* IPv6 */
  892. }
  893. return netlbl_unlabel_getattr(skb, family, secattr);
  894. }
  895. /**
  896. * netlbl_skbuff_err - Handle a LSM error on a sk_buff
  897. * @skb: the packet
  898. * @error: the error code
  899. * @gateway: true if host is acting as a gateway, false otherwise
  900. *
  901. * Description:
  902. * Deal with a LSM problem when handling the packet in @skb, typically this is
  903. * a permission denied problem (-EACCES). The correct action is determined
  904. * according to the packet's labeling protocol.
  905. *
  906. */
  907. void netlbl_skbuff_err(struct sk_buff *skb, int error, int gateway)
  908. {
  909. if (CIPSO_V4_OPTEXIST(skb))
  910. cipso_v4_error(skb, error, gateway);
  911. }
  912. /**
  913. * netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches
  914. *
  915. * Description:
  916. * For all of the NetLabel protocols that support some form of label mapping
  917. * cache, invalidate the cache. Returns zero on success, negative values on
  918. * error.
  919. *
  920. */
  921. void netlbl_cache_invalidate(void)
  922. {
  923. cipso_v4_cache_invalidate();
  924. }
  925. /**
  926. * netlbl_cache_add - Add an entry to a NetLabel protocol cache
  927. * @skb: the packet
  928. * @secattr: the packet's security attributes
  929. *
  930. * Description:
  931. * Add the LSM security attributes for the given packet to the underlying
  932. * NetLabel protocol's label mapping cache. Returns zero on success, negative
  933. * values on error.
  934. *
  935. */
  936. int netlbl_cache_add(const struct sk_buff *skb,
  937. const struct netlbl_lsm_secattr *secattr)
  938. {
  939. if ((secattr->flags & NETLBL_SECATTR_CACHE) == 0)
  940. return -ENOMSG;
  941. if (CIPSO_V4_OPTEXIST(skb))
  942. return cipso_v4_cache_add(skb, secattr);
  943. return -ENOMSG;
  944. }
  945. /*
  946. * Protocol Engine Functions
  947. */
  948. /**
  949. * netlbl_audit_start - Start an audit message
  950. * @type: audit message type
  951. * @audit_info: NetLabel audit information
  952. *
  953. * Description:
  954. * Start an audit message using the type specified in @type and fill the audit
  955. * message with some fields common to all NetLabel audit messages. This
  956. * function should only be used by protocol engines, not LSMs. Returns a
  957. * pointer to the audit buffer on success, NULL on failure.
  958. *
  959. */
  960. struct audit_buffer *netlbl_audit_start(int type,
  961. struct netlbl_audit *audit_info)
  962. {
  963. return netlbl_audit_start_common(type, audit_info);
  964. }
  965. /*
  966. * Setup Functions
  967. */
  968. /**
  969. * netlbl_init - Initialize NetLabel
  970. *
  971. * Description:
  972. * Perform the required NetLabel initialization before first use.
  973. *
  974. */
  975. static int __init netlbl_init(void)
  976. {
  977. int ret_val;
  978. printk(KERN_INFO "NetLabel: Initializing\n");
  979. printk(KERN_INFO "NetLabel: domain hash size = %u\n",
  980. (1 << NETLBL_DOMHSH_BITSIZE));
  981. printk(KERN_INFO "NetLabel: protocols ="
  982. " UNLABELED"
  983. " CIPSOv4"
  984. "\n");
  985. ret_val = netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE);
  986. if (ret_val != 0)
  987. goto init_failure;
  988. ret_val = netlbl_unlabel_init(NETLBL_UNLHSH_BITSIZE);
  989. if (ret_val != 0)
  990. goto init_failure;
  991. ret_val = netlbl_netlink_init();
  992. if (ret_val != 0)
  993. goto init_failure;
  994. ret_val = netlbl_unlabel_defconf();
  995. if (ret_val != 0)
  996. goto init_failure;
  997. printk(KERN_INFO "NetLabel: unlabeled traffic allowed by default\n");
  998. return 0;
  999. init_failure:
  1000. panic("NetLabel: failed to initialize properly (%d)\n", ret_val);
  1001. }
  1002. subsys_initcall(netlbl_init);