netlabel_kapi.c 28 KB

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