netlabel_kapi.c 28 KB

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