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/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_sock_setattr - Label a socket using the correct protocol
  570. * @sk: the socket to label
  571. * @family: protocol family
  572. * @secattr: the security attributes
  573. *
  574. * Description:
  575. * Attach the correct label to the given socket using the security attributes
  576. * specified in @secattr. This function requires exclusive access to @sk,
  577. * which means it either needs to be in the process of being created or locked.
  578. * Returns zero on success, -EDESTADDRREQ if the domain is configured to use
  579. * network address selectors (can't blindly label the socket), and negative
  580. * values on all other failures.
  581. *
  582. */
  583. int netlbl_sock_setattr(struct sock *sk,
  584. u16 family,
  585. const struct netlbl_lsm_secattr *secattr)
  586. {
  587. int ret_val;
  588. struct netlbl_dom_map *dom_entry;
  589. rcu_read_lock();
  590. dom_entry = netlbl_domhsh_getentry(secattr->domain);
  591. if (dom_entry == NULL) {
  592. ret_val = -ENOENT;
  593. goto socket_setattr_return;
  594. }
  595. switch (family) {
  596. case AF_INET:
  597. switch (dom_entry->type) {
  598. case NETLBL_NLTYPE_ADDRSELECT:
  599. ret_val = -EDESTADDRREQ;
  600. break;
  601. case NETLBL_NLTYPE_CIPSOV4:
  602. ret_val = cipso_v4_sock_setattr(sk,
  603. dom_entry->type_def.cipsov4,
  604. secattr);
  605. break;
  606. case NETLBL_NLTYPE_UNLABELED:
  607. ret_val = 0;
  608. break;
  609. default:
  610. ret_val = -ENOENT;
  611. }
  612. break;
  613. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  614. case AF_INET6:
  615. /* since we don't support any IPv6 labeling protocols right
  616. * now we can optimize everything away until we do */
  617. ret_val = 0;
  618. break;
  619. #endif /* IPv6 */
  620. default:
  621. ret_val = -EPROTONOSUPPORT;
  622. }
  623. socket_setattr_return:
  624. rcu_read_unlock();
  625. return ret_val;
  626. }
  627. /**
  628. * netlbl_sock_delattr - Delete all the NetLabel labels on a socket
  629. * @sk: the socket
  630. *
  631. * Description:
  632. * Remove all the NetLabel labeling from @sk. The caller is responsible for
  633. * ensuring that @sk is locked.
  634. *
  635. */
  636. void netlbl_sock_delattr(struct sock *sk)
  637. {
  638. cipso_v4_sock_delattr(sk);
  639. }
  640. /**
  641. * netlbl_sock_getattr - Determine the security attributes of a sock
  642. * @sk: the sock
  643. * @secattr: the security attributes
  644. *
  645. * Description:
  646. * Examines the given sock to see if any NetLabel style labeling has been
  647. * applied to the sock, if so it parses the socket label and returns the
  648. * security attributes in @secattr. Returns zero on success, negative values
  649. * on failure.
  650. *
  651. */
  652. int netlbl_sock_getattr(struct sock *sk,
  653. struct netlbl_lsm_secattr *secattr)
  654. {
  655. int ret_val;
  656. switch (sk->sk_family) {
  657. case AF_INET:
  658. ret_val = cipso_v4_sock_getattr(sk, secattr);
  659. break;
  660. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  661. case AF_INET6:
  662. ret_val = -ENOMSG;
  663. break;
  664. #endif /* IPv6 */
  665. default:
  666. ret_val = -EPROTONOSUPPORT;
  667. }
  668. return ret_val;
  669. }
  670. /**
  671. * netlbl_conn_setattr - Label a connected socket using the correct protocol
  672. * @sk: the socket to label
  673. * @addr: the destination address
  674. * @secattr: the security attributes
  675. *
  676. * Description:
  677. * Attach the correct label to the given connected socket using the security
  678. * attributes specified in @secattr. The caller is responsible for ensuring
  679. * that @sk is locked. Returns zero on success, negative values on failure.
  680. *
  681. */
  682. int netlbl_conn_setattr(struct sock *sk,
  683. struct sockaddr *addr,
  684. const struct netlbl_lsm_secattr *secattr)
  685. {
  686. int ret_val;
  687. struct sockaddr_in *addr4;
  688. struct netlbl_domaddr4_map *af4_entry;
  689. rcu_read_lock();
  690. switch (addr->sa_family) {
  691. case AF_INET:
  692. addr4 = (struct sockaddr_in *)addr;
  693. af4_entry = netlbl_domhsh_getentry_af4(secattr->domain,
  694. addr4->sin_addr.s_addr);
  695. if (af4_entry == NULL) {
  696. ret_val = -ENOENT;
  697. goto conn_setattr_return;
  698. }
  699. switch (af4_entry->type) {
  700. case NETLBL_NLTYPE_CIPSOV4:
  701. ret_val = cipso_v4_sock_setattr(sk,
  702. af4_entry->type_def.cipsov4,
  703. secattr);
  704. break;
  705. case NETLBL_NLTYPE_UNLABELED:
  706. /* just delete the protocols we support for right now
  707. * but we could remove other protocols if needed */
  708. cipso_v4_sock_delattr(sk);
  709. ret_val = 0;
  710. break;
  711. default:
  712. ret_val = -ENOENT;
  713. }
  714. break;
  715. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  716. case AF_INET6:
  717. /* since we don't support any IPv6 labeling protocols right
  718. * now we can optimize everything away until we do */
  719. ret_val = 0;
  720. break;
  721. #endif /* IPv6 */
  722. default:
  723. ret_val = -EPROTONOSUPPORT;
  724. }
  725. conn_setattr_return:
  726. rcu_read_unlock();
  727. return ret_val;
  728. }
  729. /**
  730. * netlbl_req_setattr - Label a request socket using the correct protocol
  731. * @req: the request socket to label
  732. * @secattr: the security attributes
  733. *
  734. * Description:
  735. * Attach the correct label to the given socket using the security attributes
  736. * specified in @secattr. Returns zero on success, negative values on failure.
  737. *
  738. */
  739. int netlbl_req_setattr(struct request_sock *req,
  740. const struct netlbl_lsm_secattr *secattr)
  741. {
  742. int ret_val;
  743. struct netlbl_dom_map *dom_entry;
  744. struct netlbl_domaddr4_map *af4_entry;
  745. u32 proto_type;
  746. struct cipso_v4_doi *proto_cv4;
  747. rcu_read_lock();
  748. dom_entry = netlbl_domhsh_getentry(secattr->domain);
  749. if (dom_entry == NULL) {
  750. ret_val = -ENOENT;
  751. goto req_setattr_return;
  752. }
  753. switch (req->rsk_ops->family) {
  754. case AF_INET:
  755. if (dom_entry->type == NETLBL_NLTYPE_ADDRSELECT) {
  756. struct inet_request_sock *req_inet = inet_rsk(req);
  757. af4_entry = netlbl_domhsh_getentry_af4(secattr->domain,
  758. req_inet->rmt_addr);
  759. if (af4_entry == NULL) {
  760. ret_val = -ENOENT;
  761. goto req_setattr_return;
  762. }
  763. proto_type = af4_entry->type;
  764. proto_cv4 = af4_entry->type_def.cipsov4;
  765. } else {
  766. proto_type = dom_entry->type;
  767. proto_cv4 = dom_entry->type_def.cipsov4;
  768. }
  769. switch (proto_type) {
  770. case NETLBL_NLTYPE_CIPSOV4:
  771. ret_val = cipso_v4_req_setattr(req, proto_cv4, secattr);
  772. break;
  773. case NETLBL_NLTYPE_UNLABELED:
  774. /* just delete the protocols we support for right now
  775. * but we could remove other protocols if needed */
  776. cipso_v4_req_delattr(req);
  777. ret_val = 0;
  778. break;
  779. default:
  780. ret_val = -ENOENT;
  781. }
  782. break;
  783. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  784. case AF_INET6:
  785. /* since we don't support any IPv6 labeling protocols right
  786. * now we can optimize everything away until we do */
  787. ret_val = 0;
  788. break;
  789. #endif /* IPv6 */
  790. default:
  791. ret_val = -EPROTONOSUPPORT;
  792. }
  793. req_setattr_return:
  794. rcu_read_unlock();
  795. return ret_val;
  796. }
  797. /**
  798. * netlbl_req_delattr - Delete all the NetLabel labels on a socket
  799. * @req: the socket
  800. *
  801. * Description:
  802. * Remove all the NetLabel labeling from @req.
  803. *
  804. */
  805. void netlbl_req_delattr(struct request_sock *req)
  806. {
  807. cipso_v4_req_delattr(req);
  808. }
  809. /**
  810. * netlbl_skbuff_setattr - Label a packet using the correct protocol
  811. * @skb: the packet
  812. * @family: protocol family
  813. * @secattr: the security attributes
  814. *
  815. * Description:
  816. * Attach the correct label to the given packet using the security attributes
  817. * specified in @secattr. Returns zero on success, negative values on failure.
  818. *
  819. */
  820. int netlbl_skbuff_setattr(struct sk_buff *skb,
  821. u16 family,
  822. const struct netlbl_lsm_secattr *secattr)
  823. {
  824. int ret_val;
  825. struct iphdr *hdr4;
  826. struct netlbl_domaddr4_map *af4_entry;
  827. rcu_read_lock();
  828. switch (family) {
  829. case AF_INET:
  830. hdr4 = ip_hdr(skb);
  831. af4_entry = netlbl_domhsh_getentry_af4(secattr->domain,
  832. hdr4->daddr);
  833. if (af4_entry == NULL) {
  834. ret_val = -ENOENT;
  835. goto skbuff_setattr_return;
  836. }
  837. switch (af4_entry->type) {
  838. case NETLBL_NLTYPE_CIPSOV4:
  839. ret_val = cipso_v4_skbuff_setattr(skb,
  840. af4_entry->type_def.cipsov4,
  841. secattr);
  842. break;
  843. case NETLBL_NLTYPE_UNLABELED:
  844. /* just delete the protocols we support for right now
  845. * but we could remove other protocols if needed */
  846. ret_val = cipso_v4_skbuff_delattr(skb);
  847. break;
  848. default:
  849. ret_val = -ENOENT;
  850. }
  851. break;
  852. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  853. case AF_INET6:
  854. /* since we don't support any IPv6 labeling protocols right
  855. * now we can optimize everything away until we do */
  856. ret_val = 0;
  857. break;
  858. #endif /* IPv6 */
  859. default:
  860. ret_val = -EPROTONOSUPPORT;
  861. }
  862. skbuff_setattr_return:
  863. rcu_read_unlock();
  864. return ret_val;
  865. }
  866. /**
  867. * netlbl_skbuff_getattr - Determine the security attributes of a packet
  868. * @skb: the packet
  869. * @family: protocol family
  870. * @secattr: the security attributes
  871. *
  872. * Description:
  873. * Examines the given packet to see if a recognized form of packet labeling
  874. * is present, if so it parses the packet label and returns the security
  875. * attributes in @secattr. Returns zero on success, negative values on
  876. * failure.
  877. *
  878. */
  879. int netlbl_skbuff_getattr(const struct sk_buff *skb,
  880. u16 family,
  881. struct netlbl_lsm_secattr *secattr)
  882. {
  883. switch (family) {
  884. case AF_INET:
  885. if (CIPSO_V4_OPTEXIST(skb) &&
  886. cipso_v4_skbuff_getattr(skb, secattr) == 0)
  887. return 0;
  888. break;
  889. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  890. case AF_INET6:
  891. break;
  892. #endif /* IPv6 */
  893. }
  894. return netlbl_unlabel_getattr(skb, family, secattr);
  895. }
  896. /**
  897. * netlbl_skbuff_err - Handle a LSM error on a sk_buff
  898. * @skb: the packet
  899. * @error: the error code
  900. * @gateway: true if host is acting as a gateway, false otherwise
  901. *
  902. * Description:
  903. * Deal with a LSM problem when handling the packet in @skb, typically this is
  904. * a permission denied problem (-EACCES). The correct action is determined
  905. * according to the packet's labeling protocol.
  906. *
  907. */
  908. void netlbl_skbuff_err(struct sk_buff *skb, int error, int gateway)
  909. {
  910. if (CIPSO_V4_OPTEXIST(skb))
  911. cipso_v4_error(skb, error, gateway);
  912. }
  913. /**
  914. * netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches
  915. *
  916. * Description:
  917. * For all of the NetLabel protocols that support some form of label mapping
  918. * cache, invalidate the cache. Returns zero on success, negative values on
  919. * error.
  920. *
  921. */
  922. void netlbl_cache_invalidate(void)
  923. {
  924. cipso_v4_cache_invalidate();
  925. }
  926. /**
  927. * netlbl_cache_add - Add an entry to a NetLabel protocol cache
  928. * @skb: the packet
  929. * @secattr: the packet's security attributes
  930. *
  931. * Description:
  932. * Add the LSM security attributes for the given packet to the underlying
  933. * NetLabel protocol's label mapping cache. Returns zero on success, negative
  934. * values on error.
  935. *
  936. */
  937. int netlbl_cache_add(const struct sk_buff *skb,
  938. const struct netlbl_lsm_secattr *secattr)
  939. {
  940. if ((secattr->flags & NETLBL_SECATTR_CACHE) == 0)
  941. return -ENOMSG;
  942. if (CIPSO_V4_OPTEXIST(skb))
  943. return cipso_v4_cache_add(skb, secattr);
  944. return -ENOMSG;
  945. }
  946. /*
  947. * Protocol Engine Functions
  948. */
  949. /**
  950. * netlbl_audit_start - Start an audit message
  951. * @type: audit message type
  952. * @audit_info: NetLabel audit information
  953. *
  954. * Description:
  955. * Start an audit message using the type specified in @type and fill the audit
  956. * message with some fields common to all NetLabel audit messages. This
  957. * function should only be used by protocol engines, not LSMs. Returns a
  958. * pointer to the audit buffer on success, NULL on failure.
  959. *
  960. */
  961. struct audit_buffer *netlbl_audit_start(int type,
  962. struct netlbl_audit *audit_info)
  963. {
  964. return netlbl_audit_start_common(type, audit_info);
  965. }
  966. /*
  967. * Setup Functions
  968. */
  969. /**
  970. * netlbl_init - Initialize NetLabel
  971. *
  972. * Description:
  973. * Perform the required NetLabel initialization before first use.
  974. *
  975. */
  976. static int __init netlbl_init(void)
  977. {
  978. int ret_val;
  979. printk(KERN_INFO "NetLabel: Initializing\n");
  980. printk(KERN_INFO "NetLabel: domain hash size = %u\n",
  981. (1 << NETLBL_DOMHSH_BITSIZE));
  982. printk(KERN_INFO "NetLabel: protocols ="
  983. " UNLABELED"
  984. " CIPSOv4"
  985. "\n");
  986. ret_val = netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE);
  987. if (ret_val != 0)
  988. goto init_failure;
  989. ret_val = netlbl_unlabel_init(NETLBL_UNLHSH_BITSIZE);
  990. if (ret_val != 0)
  991. goto init_failure;
  992. ret_val = netlbl_netlink_init();
  993. if (ret_val != 0)
  994. goto init_failure;
  995. ret_val = netlbl_unlabel_defconf();
  996. if (ret_val != 0)
  997. goto init_failure;
  998. printk(KERN_INFO "NetLabel: unlabeled traffic allowed by default\n");
  999. return 0;
  1000. init_failure:
  1001. panic("NetLabel: failed to initialize properly (%d)\n", ret_val);
  1002. }
  1003. subsys_initcall(netlbl_init);