netlabel_kapi.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * NetLabel Kernel API
  4. *
  5. * This file defines the kernel API for the NetLabel system. The NetLabel
  6. * system manages static and dynamic label mappings for network protocols such
  7. * as CIPSO and RIPSO.
  8. *
  9. * Author: Paul Moore <[email protected]>
  10. */
  11. /*
  12. * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
  13. */
  14. #include <linux/init.h>
  15. #include <linux/types.h>
  16. #include <linux/slab.h>
  17. #include <linux/audit.h>
  18. #include <linux/in.h>
  19. #include <linux/in6.h>
  20. #include <net/ip.h>
  21. #include <net/ipv6.h>
  22. #include <net/netlabel.h>
  23. #include <net/cipso_ipv4.h>
  24. #include <net/calipso.h>
  25. #include <asm/bug.h>
  26. #include <linux/atomic.h>
  27. #include "netlabel_domainhash.h"
  28. #include "netlabel_unlabeled.h"
  29. #include "netlabel_cipso_v4.h"
  30. #include "netlabel_calipso.h"
  31. #include "netlabel_user.h"
  32. #include "netlabel_mgmt.h"
  33. #include "netlabel_addrlist.h"
  34. /*
  35. * Configuration Functions
  36. */
  37. /**
  38. * netlbl_cfg_map_del - Remove a NetLabel/LSM domain mapping
  39. * @domain: the domain mapping to remove
  40. * @family: address family
  41. * @addr: IP address
  42. * @mask: IP address mask
  43. * @audit_info: NetLabel audit information
  44. *
  45. * Description:
  46. * Removes a NetLabel/LSM domain mapping. A @domain value of NULL causes the
  47. * default domain mapping to be removed. Returns zero on success, negative
  48. * values on failure.
  49. *
  50. */
  51. int netlbl_cfg_map_del(const char *domain,
  52. u16 family,
  53. const void *addr,
  54. const void *mask,
  55. struct netlbl_audit *audit_info)
  56. {
  57. if (addr == NULL && mask == NULL) {
  58. return netlbl_domhsh_remove(domain, family, audit_info);
  59. } else if (addr != NULL && mask != NULL) {
  60. switch (family) {
  61. case AF_INET:
  62. return netlbl_domhsh_remove_af4(domain, addr, mask,
  63. audit_info);
  64. #if IS_ENABLED(CONFIG_IPV6)
  65. case AF_INET6:
  66. return netlbl_domhsh_remove_af6(domain, addr, mask,
  67. audit_info);
  68. #endif /* IPv6 */
  69. default:
  70. return -EPFNOSUPPORT;
  71. }
  72. } else
  73. return -EINVAL;
  74. }
  75. /**
  76. * netlbl_cfg_unlbl_map_add - Add a new unlabeled mapping
  77. * @domain: the domain mapping to add
  78. * @family: address family
  79. * @addr: IP address
  80. * @mask: IP address mask
  81. * @audit_info: NetLabel audit information
  82. *
  83. * Description:
  84. * Adds a new unlabeled NetLabel/LSM domain mapping. A @domain value of NULL
  85. * causes a new default domain mapping to be added. Returns zero on success,
  86. * negative values on failure.
  87. *
  88. */
  89. int netlbl_cfg_unlbl_map_add(const char *domain,
  90. u16 family,
  91. const void *addr,
  92. const void *mask,
  93. struct netlbl_audit *audit_info)
  94. {
  95. int ret_val = -ENOMEM;
  96. struct netlbl_dom_map *entry;
  97. struct netlbl_domaddr_map *addrmap = NULL;
  98. struct netlbl_domaddr4_map *map4 = NULL;
  99. struct netlbl_domaddr6_map *map6 = NULL;
  100. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  101. if (entry == NULL)
  102. return -ENOMEM;
  103. if (domain != NULL) {
  104. entry->domain = kstrdup(domain, GFP_ATOMIC);
  105. if (entry->domain == NULL)
  106. goto cfg_unlbl_map_add_failure;
  107. }
  108. entry->family = family;
  109. if (addr == NULL && mask == NULL)
  110. entry->def.type = NETLBL_NLTYPE_UNLABELED;
  111. else if (addr != NULL && mask != NULL) {
  112. addrmap = kzalloc(sizeof(*addrmap), GFP_ATOMIC);
  113. if (addrmap == NULL)
  114. goto cfg_unlbl_map_add_failure;
  115. INIT_LIST_HEAD(&addrmap->list4);
  116. INIT_LIST_HEAD(&addrmap->list6);
  117. switch (family) {
  118. case AF_INET: {
  119. const struct in_addr *addr4 = addr;
  120. const struct in_addr *mask4 = mask;
  121. map4 = kzalloc(sizeof(*map4), GFP_ATOMIC);
  122. if (map4 == NULL)
  123. goto cfg_unlbl_map_add_failure;
  124. map4->def.type = NETLBL_NLTYPE_UNLABELED;
  125. map4->list.addr = addr4->s_addr & mask4->s_addr;
  126. map4->list.mask = mask4->s_addr;
  127. map4->list.valid = 1;
  128. ret_val = netlbl_af4list_add(&map4->list,
  129. &addrmap->list4);
  130. if (ret_val != 0)
  131. goto cfg_unlbl_map_add_failure;
  132. break;
  133. }
  134. #if IS_ENABLED(CONFIG_IPV6)
  135. case AF_INET6: {
  136. const struct in6_addr *addr6 = addr;
  137. const struct in6_addr *mask6 = mask;
  138. map6 = kzalloc(sizeof(*map6), GFP_ATOMIC);
  139. if (map6 == NULL)
  140. goto cfg_unlbl_map_add_failure;
  141. map6->def.type = NETLBL_NLTYPE_UNLABELED;
  142. map6->list.addr = *addr6;
  143. map6->list.addr.s6_addr32[0] &= mask6->s6_addr32[0];
  144. map6->list.addr.s6_addr32[1] &= mask6->s6_addr32[1];
  145. map6->list.addr.s6_addr32[2] &= mask6->s6_addr32[2];
  146. map6->list.addr.s6_addr32[3] &= mask6->s6_addr32[3];
  147. map6->list.mask = *mask6;
  148. map6->list.valid = 1;
  149. ret_val = netlbl_af6list_add(&map6->list,
  150. &addrmap->list6);
  151. if (ret_val != 0)
  152. goto cfg_unlbl_map_add_failure;
  153. break;
  154. }
  155. #endif /* IPv6 */
  156. default:
  157. goto cfg_unlbl_map_add_failure;
  158. }
  159. entry->def.addrsel = addrmap;
  160. entry->def.type = NETLBL_NLTYPE_ADDRSELECT;
  161. } else {
  162. ret_val = -EINVAL;
  163. goto cfg_unlbl_map_add_failure;
  164. }
  165. ret_val = netlbl_domhsh_add(entry, audit_info);
  166. if (ret_val != 0)
  167. goto cfg_unlbl_map_add_failure;
  168. return 0;
  169. cfg_unlbl_map_add_failure:
  170. kfree(entry->domain);
  171. kfree(entry);
  172. kfree(addrmap);
  173. kfree(map4);
  174. kfree(map6);
  175. return ret_val;
  176. }
  177. /**
  178. * netlbl_cfg_unlbl_static_add - Adds a new static label
  179. * @net: network namespace
  180. * @dev_name: interface name
  181. * @addr: IP address in network byte order (struct in[6]_addr)
  182. * @mask: address mask in network byte order (struct in[6]_addr)
  183. * @family: address family
  184. * @secid: LSM secid value for the entry
  185. * @audit_info: NetLabel audit information
  186. *
  187. * Description:
  188. * Adds a new NetLabel static label to be used when protocol provided labels
  189. * are not present on incoming traffic. If @dev_name is NULL then the default
  190. * interface will be used. Returns zero on success, negative values on failure.
  191. *
  192. */
  193. int netlbl_cfg_unlbl_static_add(struct net *net,
  194. const char *dev_name,
  195. const void *addr,
  196. const void *mask,
  197. u16 family,
  198. u32 secid,
  199. struct netlbl_audit *audit_info)
  200. {
  201. u32 addr_len;
  202. switch (family) {
  203. case AF_INET:
  204. addr_len = sizeof(struct in_addr);
  205. break;
  206. #if IS_ENABLED(CONFIG_IPV6)
  207. case AF_INET6:
  208. addr_len = sizeof(struct in6_addr);
  209. break;
  210. #endif /* IPv6 */
  211. default:
  212. return -EPFNOSUPPORT;
  213. }
  214. return netlbl_unlhsh_add(net,
  215. dev_name, addr, mask, addr_len,
  216. secid, audit_info);
  217. }
  218. /**
  219. * netlbl_cfg_unlbl_static_del - Removes an existing static label
  220. * @net: network namespace
  221. * @dev_name: interface name
  222. * @addr: IP address in network byte order (struct in[6]_addr)
  223. * @mask: address mask in network byte order (struct in[6]_addr)
  224. * @family: address family
  225. * @audit_info: NetLabel audit information
  226. *
  227. * Description:
  228. * Removes an existing NetLabel static label used when protocol provided labels
  229. * are not present on incoming traffic. If @dev_name is NULL then the default
  230. * interface will be used. Returns zero on success, negative values on failure.
  231. *
  232. */
  233. int netlbl_cfg_unlbl_static_del(struct net *net,
  234. const char *dev_name,
  235. const void *addr,
  236. const void *mask,
  237. u16 family,
  238. struct netlbl_audit *audit_info)
  239. {
  240. u32 addr_len;
  241. switch (family) {
  242. case AF_INET:
  243. addr_len = sizeof(struct in_addr);
  244. break;
  245. #if IS_ENABLED(CONFIG_IPV6)
  246. case AF_INET6:
  247. addr_len = sizeof(struct in6_addr);
  248. break;
  249. #endif /* IPv6 */
  250. default:
  251. return -EPFNOSUPPORT;
  252. }
  253. return netlbl_unlhsh_remove(net,
  254. dev_name, addr, mask, addr_len,
  255. audit_info);
  256. }
  257. /**
  258. * netlbl_cfg_cipsov4_add - Add a new CIPSOv4 DOI definition
  259. * @doi_def: CIPSO DOI definition
  260. * @audit_info: NetLabel audit information
  261. *
  262. * Description:
  263. * Add a new CIPSO DOI definition as defined by @doi_def. Returns zero on
  264. * success and negative values on failure.
  265. *
  266. */
  267. int netlbl_cfg_cipsov4_add(struct cipso_v4_doi *doi_def,
  268. struct netlbl_audit *audit_info)
  269. {
  270. return cipso_v4_doi_add(doi_def, audit_info);
  271. }
  272. /**
  273. * netlbl_cfg_cipsov4_del - Remove an existing CIPSOv4 DOI definition
  274. * @doi: CIPSO DOI
  275. * @audit_info: NetLabel audit information
  276. *
  277. * Description:
  278. * Remove an existing CIPSO DOI definition matching @doi. Returns zero on
  279. * success and negative values on failure.
  280. *
  281. */
  282. void netlbl_cfg_cipsov4_del(u32 doi, struct netlbl_audit *audit_info)
  283. {
  284. cipso_v4_doi_remove(doi, audit_info);
  285. }
  286. /**
  287. * netlbl_cfg_cipsov4_map_add - Add a new CIPSOv4 DOI mapping
  288. * @doi: the CIPSO DOI
  289. * @domain: the domain mapping to add
  290. * @addr: IP address
  291. * @mask: IP address mask
  292. * @audit_info: NetLabel audit information
  293. *
  294. * Description:
  295. * Add a new NetLabel/LSM domain mapping for the given CIPSO DOI to the NetLabel
  296. * subsystem. A @domain value of NULL adds a new default domain mapping.
  297. * Returns zero on success, negative values on failure.
  298. *
  299. */
  300. int netlbl_cfg_cipsov4_map_add(u32 doi,
  301. const char *domain,
  302. const struct in_addr *addr,
  303. const struct in_addr *mask,
  304. struct netlbl_audit *audit_info)
  305. {
  306. int ret_val = -ENOMEM;
  307. struct cipso_v4_doi *doi_def;
  308. struct netlbl_dom_map *entry;
  309. struct netlbl_domaddr_map *addrmap = NULL;
  310. struct netlbl_domaddr4_map *addrinfo = NULL;
  311. doi_def = cipso_v4_doi_getdef(doi);
  312. if (doi_def == NULL)
  313. return -ENOENT;
  314. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  315. if (entry == NULL)
  316. goto out_entry;
  317. entry->family = AF_INET;
  318. if (domain != NULL) {
  319. entry->domain = kstrdup(domain, GFP_ATOMIC);
  320. if (entry->domain == NULL)
  321. goto out_domain;
  322. }
  323. if (addr == NULL && mask == NULL) {
  324. entry->def.cipso = doi_def;
  325. entry->def.type = NETLBL_NLTYPE_CIPSOV4;
  326. } else if (addr != NULL && mask != NULL) {
  327. addrmap = kzalloc(sizeof(*addrmap), GFP_ATOMIC);
  328. if (addrmap == NULL)
  329. goto out_addrmap;
  330. INIT_LIST_HEAD(&addrmap->list4);
  331. INIT_LIST_HEAD(&addrmap->list6);
  332. addrinfo = kzalloc(sizeof(*addrinfo), GFP_ATOMIC);
  333. if (addrinfo == NULL)
  334. goto out_addrinfo;
  335. addrinfo->def.cipso = doi_def;
  336. addrinfo->def.type = NETLBL_NLTYPE_CIPSOV4;
  337. addrinfo->list.addr = addr->s_addr & mask->s_addr;
  338. addrinfo->list.mask = mask->s_addr;
  339. addrinfo->list.valid = 1;
  340. ret_val = netlbl_af4list_add(&addrinfo->list, &addrmap->list4);
  341. if (ret_val != 0)
  342. goto cfg_cipsov4_map_add_failure;
  343. entry->def.addrsel = addrmap;
  344. entry->def.type = NETLBL_NLTYPE_ADDRSELECT;
  345. } else {
  346. ret_val = -EINVAL;
  347. goto out_addrmap;
  348. }
  349. ret_val = netlbl_domhsh_add(entry, audit_info);
  350. if (ret_val != 0)
  351. goto cfg_cipsov4_map_add_failure;
  352. return 0;
  353. cfg_cipsov4_map_add_failure:
  354. kfree(addrinfo);
  355. out_addrinfo:
  356. kfree(addrmap);
  357. out_addrmap:
  358. kfree(entry->domain);
  359. out_domain:
  360. kfree(entry);
  361. out_entry:
  362. cipso_v4_doi_putdef(doi_def);
  363. return ret_val;
  364. }
  365. /**
  366. * netlbl_cfg_calipso_add - Add a new CALIPSO DOI definition
  367. * @doi_def: CALIPSO DOI definition
  368. * @audit_info: NetLabel audit information
  369. *
  370. * Description:
  371. * Add a new CALIPSO DOI definition as defined by @doi_def. Returns zero on
  372. * success and negative values on failure.
  373. *
  374. */
  375. int netlbl_cfg_calipso_add(struct calipso_doi *doi_def,
  376. struct netlbl_audit *audit_info)
  377. {
  378. #if IS_ENABLED(CONFIG_IPV6)
  379. return calipso_doi_add(doi_def, audit_info);
  380. #else /* IPv6 */
  381. return -ENOSYS;
  382. #endif /* IPv6 */
  383. }
  384. /**
  385. * netlbl_cfg_calipso_del - Remove an existing CALIPSO DOI definition
  386. * @doi: CALIPSO DOI
  387. * @audit_info: NetLabel audit information
  388. *
  389. * Description:
  390. * Remove an existing CALIPSO DOI definition matching @doi. Returns zero on
  391. * success and negative values on failure.
  392. *
  393. */
  394. void netlbl_cfg_calipso_del(u32 doi, struct netlbl_audit *audit_info)
  395. {
  396. #if IS_ENABLED(CONFIG_IPV6)
  397. calipso_doi_remove(doi, audit_info);
  398. #endif /* IPv6 */
  399. }
  400. /**
  401. * netlbl_cfg_calipso_map_add - Add a new CALIPSO DOI mapping
  402. * @doi: the CALIPSO DOI
  403. * @domain: the domain mapping to add
  404. * @addr: IP address
  405. * @mask: IP address mask
  406. * @audit_info: NetLabel audit information
  407. *
  408. * Description:
  409. * Add a new NetLabel/LSM domain mapping for the given CALIPSO DOI to the
  410. * NetLabel subsystem. A @domain value of NULL adds a new default domain
  411. * mapping. Returns zero on success, negative values on failure.
  412. *
  413. */
  414. int netlbl_cfg_calipso_map_add(u32 doi,
  415. const char *domain,
  416. const struct in6_addr *addr,
  417. const struct in6_addr *mask,
  418. struct netlbl_audit *audit_info)
  419. {
  420. #if IS_ENABLED(CONFIG_IPV6)
  421. int ret_val = -ENOMEM;
  422. struct calipso_doi *doi_def;
  423. struct netlbl_dom_map *entry;
  424. struct netlbl_domaddr_map *addrmap = NULL;
  425. struct netlbl_domaddr6_map *addrinfo = NULL;
  426. doi_def = calipso_doi_getdef(doi);
  427. if (doi_def == NULL)
  428. return -ENOENT;
  429. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  430. if (entry == NULL)
  431. goto out_entry;
  432. entry->family = AF_INET6;
  433. if (domain != NULL) {
  434. entry->domain = kstrdup(domain, GFP_ATOMIC);
  435. if (entry->domain == NULL)
  436. goto out_domain;
  437. }
  438. if (addr == NULL && mask == NULL) {
  439. entry->def.calipso = doi_def;
  440. entry->def.type = NETLBL_NLTYPE_CALIPSO;
  441. } else if (addr != NULL && mask != NULL) {
  442. addrmap = kzalloc(sizeof(*addrmap), GFP_ATOMIC);
  443. if (addrmap == NULL)
  444. goto out_addrmap;
  445. INIT_LIST_HEAD(&addrmap->list4);
  446. INIT_LIST_HEAD(&addrmap->list6);
  447. addrinfo = kzalloc(sizeof(*addrinfo), GFP_ATOMIC);
  448. if (addrinfo == NULL)
  449. goto out_addrinfo;
  450. addrinfo->def.calipso = doi_def;
  451. addrinfo->def.type = NETLBL_NLTYPE_CALIPSO;
  452. addrinfo->list.addr = *addr;
  453. addrinfo->list.addr.s6_addr32[0] &= mask->s6_addr32[0];
  454. addrinfo->list.addr.s6_addr32[1] &= mask->s6_addr32[1];
  455. addrinfo->list.addr.s6_addr32[2] &= mask->s6_addr32[2];
  456. addrinfo->list.addr.s6_addr32[3] &= mask->s6_addr32[3];
  457. addrinfo->list.mask = *mask;
  458. addrinfo->list.valid = 1;
  459. ret_val = netlbl_af6list_add(&addrinfo->list, &addrmap->list6);
  460. if (ret_val != 0)
  461. goto cfg_calipso_map_add_failure;
  462. entry->def.addrsel = addrmap;
  463. entry->def.type = NETLBL_NLTYPE_ADDRSELECT;
  464. } else {
  465. ret_val = -EINVAL;
  466. goto out_addrmap;
  467. }
  468. ret_val = netlbl_domhsh_add(entry, audit_info);
  469. if (ret_val != 0)
  470. goto cfg_calipso_map_add_failure;
  471. return 0;
  472. cfg_calipso_map_add_failure:
  473. kfree(addrinfo);
  474. out_addrinfo:
  475. kfree(addrmap);
  476. out_addrmap:
  477. kfree(entry->domain);
  478. out_domain:
  479. kfree(entry);
  480. out_entry:
  481. calipso_doi_putdef(doi_def);
  482. return ret_val;
  483. #else /* IPv6 */
  484. return -ENOSYS;
  485. #endif /* IPv6 */
  486. }
  487. /*
  488. * Security Attribute Functions
  489. */
  490. #define _CM_F_NONE 0x00000000
  491. #define _CM_F_ALLOC 0x00000001
  492. #define _CM_F_WALK 0x00000002
  493. /**
  494. * _netlbl_catmap_getnode - Get a individual node from a catmap
  495. * @catmap: pointer to the category bitmap
  496. * @offset: the requested offset
  497. * @cm_flags: catmap flags, see _CM_F_*
  498. * @gfp_flags: memory allocation flags
  499. *
  500. * Description:
  501. * Iterate through the catmap looking for the node associated with @offset.
  502. * If the _CM_F_ALLOC flag is set in @cm_flags and there is no associated node,
  503. * one will be created and inserted into the catmap. If the _CM_F_WALK flag is
  504. * set in @cm_flags and there is no associated node, the next highest node will
  505. * be returned. Returns a pointer to the node on success, NULL on failure.
  506. *
  507. */
  508. static struct netlbl_lsm_catmap *_netlbl_catmap_getnode(
  509. struct netlbl_lsm_catmap **catmap,
  510. u32 offset,
  511. unsigned int cm_flags,
  512. gfp_t gfp_flags)
  513. {
  514. struct netlbl_lsm_catmap *iter = *catmap;
  515. struct netlbl_lsm_catmap *prev = NULL;
  516. if (iter == NULL)
  517. goto catmap_getnode_alloc;
  518. if (offset < iter->startbit)
  519. goto catmap_getnode_walk;
  520. while (iter && offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
  521. prev = iter;
  522. iter = iter->next;
  523. }
  524. if (iter == NULL || offset < iter->startbit)
  525. goto catmap_getnode_walk;
  526. return iter;
  527. catmap_getnode_walk:
  528. if (cm_flags & _CM_F_WALK)
  529. return iter;
  530. catmap_getnode_alloc:
  531. if (!(cm_flags & _CM_F_ALLOC))
  532. return NULL;
  533. iter = netlbl_catmap_alloc(gfp_flags);
  534. if (iter == NULL)
  535. return NULL;
  536. iter->startbit = offset & ~(NETLBL_CATMAP_SIZE - 1);
  537. if (prev == NULL) {
  538. iter->next = *catmap;
  539. *catmap = iter;
  540. } else {
  541. iter->next = prev->next;
  542. prev->next = iter;
  543. }
  544. return iter;
  545. }
  546. /**
  547. * netlbl_catmap_walk - Walk a LSM secattr catmap looking for a bit
  548. * @catmap: the category bitmap
  549. * @offset: the offset to start searching at, in bits
  550. *
  551. * Description:
  552. * This function walks a LSM secattr category bitmap starting at @offset and
  553. * returns the spot of the first set bit or -ENOENT if no bits are set.
  554. *
  555. */
  556. int netlbl_catmap_walk(struct netlbl_lsm_catmap *catmap, u32 offset)
  557. {
  558. struct netlbl_lsm_catmap *iter;
  559. u32 idx;
  560. u32 bit;
  561. NETLBL_CATMAP_MAPTYPE bitmap;
  562. iter = _netlbl_catmap_getnode(&catmap, offset, _CM_F_WALK, 0);
  563. if (iter == NULL)
  564. return -ENOENT;
  565. if (offset > iter->startbit) {
  566. offset -= iter->startbit;
  567. idx = offset / NETLBL_CATMAP_MAPSIZE;
  568. bit = offset % NETLBL_CATMAP_MAPSIZE;
  569. } else {
  570. idx = 0;
  571. bit = 0;
  572. }
  573. bitmap = iter->bitmap[idx] >> bit;
  574. for (;;) {
  575. if (bitmap != 0) {
  576. while ((bitmap & NETLBL_CATMAP_BIT) == 0) {
  577. bitmap >>= 1;
  578. bit++;
  579. }
  580. return iter->startbit +
  581. (NETLBL_CATMAP_MAPSIZE * idx) + bit;
  582. }
  583. if (++idx >= NETLBL_CATMAP_MAPCNT) {
  584. if (iter->next != NULL) {
  585. iter = iter->next;
  586. idx = 0;
  587. } else
  588. return -ENOENT;
  589. }
  590. bitmap = iter->bitmap[idx];
  591. bit = 0;
  592. }
  593. return -ENOENT;
  594. }
  595. EXPORT_SYMBOL(netlbl_catmap_walk);
  596. /**
  597. * netlbl_catmap_walkrng - Find the end of a string of set bits
  598. * @catmap: the category bitmap
  599. * @offset: the offset to start searching at, in bits
  600. *
  601. * Description:
  602. * This function walks a LSM secattr category bitmap starting at @offset and
  603. * returns the spot of the first cleared bit or -ENOENT if the offset is past
  604. * the end of the bitmap.
  605. *
  606. */
  607. int netlbl_catmap_walkrng(struct netlbl_lsm_catmap *catmap, u32 offset)
  608. {
  609. struct netlbl_lsm_catmap *iter;
  610. struct netlbl_lsm_catmap *prev = NULL;
  611. u32 idx;
  612. u32 bit;
  613. NETLBL_CATMAP_MAPTYPE bitmask;
  614. NETLBL_CATMAP_MAPTYPE bitmap;
  615. iter = _netlbl_catmap_getnode(&catmap, offset, _CM_F_WALK, 0);
  616. if (iter == NULL)
  617. return -ENOENT;
  618. if (offset > iter->startbit) {
  619. offset -= iter->startbit;
  620. idx = offset / NETLBL_CATMAP_MAPSIZE;
  621. bit = offset % NETLBL_CATMAP_MAPSIZE;
  622. } else {
  623. idx = 0;
  624. bit = 0;
  625. }
  626. bitmask = NETLBL_CATMAP_BIT << bit;
  627. for (;;) {
  628. bitmap = iter->bitmap[idx];
  629. while (bitmask != 0 && (bitmap & bitmask) != 0) {
  630. bitmask <<= 1;
  631. bit++;
  632. }
  633. if (prev && idx == 0 && bit == 0)
  634. return prev->startbit + NETLBL_CATMAP_SIZE - 1;
  635. else if (bitmask != 0)
  636. return iter->startbit +
  637. (NETLBL_CATMAP_MAPSIZE * idx) + bit - 1;
  638. else if (++idx >= NETLBL_CATMAP_MAPCNT) {
  639. if (iter->next == NULL)
  640. return iter->startbit + NETLBL_CATMAP_SIZE - 1;
  641. prev = iter;
  642. iter = iter->next;
  643. idx = 0;
  644. }
  645. bitmask = NETLBL_CATMAP_BIT;
  646. bit = 0;
  647. }
  648. return -ENOENT;
  649. }
  650. /**
  651. * netlbl_catmap_getlong - Export an unsigned long bitmap
  652. * @catmap: pointer to the category bitmap
  653. * @offset: pointer to the requested offset
  654. * @bitmap: the exported bitmap
  655. *
  656. * Description:
  657. * Export a bitmap with an offset greater than or equal to @offset and return
  658. * it in @bitmap. The @offset must be aligned to an unsigned long and will be
  659. * updated on return if different from what was requested; if the catmap is
  660. * empty at the requested offset and beyond, the @offset is set to (u32)-1.
  661. * Returns zero on success, negative values on failure.
  662. *
  663. */
  664. int netlbl_catmap_getlong(struct netlbl_lsm_catmap *catmap,
  665. u32 *offset,
  666. unsigned long *bitmap)
  667. {
  668. struct netlbl_lsm_catmap *iter;
  669. u32 off = *offset;
  670. u32 idx;
  671. /* only allow aligned offsets */
  672. if ((off & (BITS_PER_LONG - 1)) != 0)
  673. return -EINVAL;
  674. /* a null catmap is equivalent to an empty one */
  675. if (!catmap) {
  676. *offset = (u32)-1;
  677. return 0;
  678. }
  679. if (off < catmap->startbit) {
  680. off = catmap->startbit;
  681. *offset = off;
  682. }
  683. iter = _netlbl_catmap_getnode(&catmap, off, _CM_F_WALK, 0);
  684. if (iter == NULL) {
  685. *offset = (u32)-1;
  686. return 0;
  687. }
  688. if (off < iter->startbit) {
  689. *offset = iter->startbit;
  690. off = 0;
  691. } else
  692. off -= iter->startbit;
  693. idx = off / NETLBL_CATMAP_MAPSIZE;
  694. *bitmap = iter->bitmap[idx] >> (off % NETLBL_CATMAP_MAPSIZE);
  695. return 0;
  696. }
  697. /**
  698. * netlbl_catmap_setbit - Set a bit in a LSM secattr catmap
  699. * @catmap: pointer to the category bitmap
  700. * @bit: the bit to set
  701. * @flags: memory allocation flags
  702. *
  703. * Description:
  704. * Set the bit specified by @bit in @catmap. Returns zero on success,
  705. * negative values on failure.
  706. *
  707. */
  708. int netlbl_catmap_setbit(struct netlbl_lsm_catmap **catmap,
  709. u32 bit,
  710. gfp_t flags)
  711. {
  712. struct netlbl_lsm_catmap *iter;
  713. u32 idx;
  714. iter = _netlbl_catmap_getnode(catmap, bit, _CM_F_ALLOC, flags);
  715. if (iter == NULL)
  716. return -ENOMEM;
  717. bit -= iter->startbit;
  718. idx = bit / NETLBL_CATMAP_MAPSIZE;
  719. iter->bitmap[idx] |= NETLBL_CATMAP_BIT << (bit % NETLBL_CATMAP_MAPSIZE);
  720. return 0;
  721. }
  722. EXPORT_SYMBOL(netlbl_catmap_setbit);
  723. /**
  724. * netlbl_catmap_setrng - Set a range of bits in a LSM secattr catmap
  725. * @catmap: pointer to the category bitmap
  726. * @start: the starting bit
  727. * @end: the last bit in the string
  728. * @flags: memory allocation flags
  729. *
  730. * Description:
  731. * Set a range of bits, starting at @start and ending with @end. Returns zero
  732. * on success, negative values on failure.
  733. *
  734. */
  735. int netlbl_catmap_setrng(struct netlbl_lsm_catmap **catmap,
  736. u32 start,
  737. u32 end,
  738. gfp_t flags)
  739. {
  740. int rc = 0;
  741. u32 spot = start;
  742. while (rc == 0 && spot <= end) {
  743. if (((spot & (BITS_PER_LONG - 1)) == 0) &&
  744. ((end - spot) > BITS_PER_LONG)) {
  745. rc = netlbl_catmap_setlong(catmap,
  746. spot,
  747. (unsigned long)-1,
  748. flags);
  749. spot += BITS_PER_LONG;
  750. } else
  751. rc = netlbl_catmap_setbit(catmap, spot++, flags);
  752. }
  753. return rc;
  754. }
  755. /**
  756. * netlbl_catmap_setlong - Import an unsigned long bitmap
  757. * @catmap: pointer to the category bitmap
  758. * @offset: offset to the start of the imported bitmap
  759. * @bitmap: the bitmap to import
  760. * @flags: memory allocation flags
  761. *
  762. * Description:
  763. * Import the bitmap specified in @bitmap into @catmap, using the offset
  764. * in @offset. The offset must be aligned to an unsigned long. Returns zero
  765. * on success, negative values on failure.
  766. *
  767. */
  768. int netlbl_catmap_setlong(struct netlbl_lsm_catmap **catmap,
  769. u32 offset,
  770. unsigned long bitmap,
  771. gfp_t flags)
  772. {
  773. struct netlbl_lsm_catmap *iter;
  774. u32 idx;
  775. /* only allow aligned offsets */
  776. if ((offset & (BITS_PER_LONG - 1)) != 0)
  777. return -EINVAL;
  778. iter = _netlbl_catmap_getnode(catmap, offset, _CM_F_ALLOC, flags);
  779. if (iter == NULL)
  780. return -ENOMEM;
  781. offset -= iter->startbit;
  782. idx = offset / NETLBL_CATMAP_MAPSIZE;
  783. iter->bitmap[idx] |= (NETLBL_CATMAP_MAPTYPE)bitmap
  784. << (offset % NETLBL_CATMAP_MAPSIZE);
  785. return 0;
  786. }
  787. /* Bitmap functions
  788. */
  789. /**
  790. * netlbl_bitmap_walk - Walk a bitmap looking for a bit
  791. * @bitmap: the bitmap
  792. * @bitmap_len: length in bits
  793. * @offset: starting offset
  794. * @state: if non-zero, look for a set (1) bit else look for a cleared (0) bit
  795. *
  796. * Description:
  797. * Starting at @offset, walk the bitmap from left to right until either the
  798. * desired bit is found or we reach the end. Return the bit offset, -1 if
  799. * not found, or -2 if error.
  800. */
  801. int netlbl_bitmap_walk(const unsigned char *bitmap, u32 bitmap_len,
  802. u32 offset, u8 state)
  803. {
  804. u32 bit_spot;
  805. u32 byte_offset;
  806. unsigned char bitmask;
  807. unsigned char byte;
  808. if (offset >= bitmap_len)
  809. return -1;
  810. byte_offset = offset / 8;
  811. byte = bitmap[byte_offset];
  812. bit_spot = offset;
  813. bitmask = 0x80 >> (offset % 8);
  814. while (bit_spot < bitmap_len) {
  815. if ((state && (byte & bitmask) == bitmask) ||
  816. (state == 0 && (byte & bitmask) == 0))
  817. return bit_spot;
  818. if (++bit_spot >= bitmap_len)
  819. return -1;
  820. bitmask >>= 1;
  821. if (bitmask == 0) {
  822. byte = bitmap[++byte_offset];
  823. bitmask = 0x80;
  824. }
  825. }
  826. return -1;
  827. }
  828. EXPORT_SYMBOL(netlbl_bitmap_walk);
  829. /**
  830. * netlbl_bitmap_setbit - Sets a single bit in a bitmap
  831. * @bitmap: the bitmap
  832. * @bit: the bit
  833. * @state: if non-zero, set the bit (1) else clear the bit (0)
  834. *
  835. * Description:
  836. * Set a single bit in the bitmask. Returns zero on success, negative values
  837. * on error.
  838. */
  839. void netlbl_bitmap_setbit(unsigned char *bitmap, u32 bit, u8 state)
  840. {
  841. u32 byte_spot;
  842. u8 bitmask;
  843. /* gcc always rounds to zero when doing integer division */
  844. byte_spot = bit / 8;
  845. bitmask = 0x80 >> (bit % 8);
  846. if (state)
  847. bitmap[byte_spot] |= bitmask;
  848. else
  849. bitmap[byte_spot] &= ~bitmask;
  850. }
  851. EXPORT_SYMBOL(netlbl_bitmap_setbit);
  852. /*
  853. * LSM Functions
  854. */
  855. /**
  856. * netlbl_enabled - Determine if the NetLabel subsystem is enabled
  857. *
  858. * Description:
  859. * The LSM can use this function to determine if it should use NetLabel
  860. * security attributes in it's enforcement mechanism. Currently, NetLabel is
  861. * considered to be enabled when it's configuration contains a valid setup for
  862. * at least one labeled protocol (i.e. NetLabel can understand incoming
  863. * labeled packets of at least one type); otherwise NetLabel is considered to
  864. * be disabled.
  865. *
  866. */
  867. int netlbl_enabled(void)
  868. {
  869. /* At some point we probably want to expose this mechanism to the user
  870. * as well so that admins can toggle NetLabel regardless of the
  871. * configuration */
  872. return (atomic_read(&netlabel_mgmt_protocount) > 0);
  873. }
  874. /**
  875. * netlbl_sock_setattr - Label a socket using the correct protocol
  876. * @sk: the socket to label
  877. * @family: protocol family
  878. * @secattr: the security attributes
  879. *
  880. * Description:
  881. * Attach the correct label to the given socket using the security attributes
  882. * specified in @secattr. This function requires exclusive access to @sk,
  883. * which means it either needs to be in the process of being created or locked.
  884. * Returns zero on success, -EDESTADDRREQ if the domain is configured to use
  885. * network address selectors (can't blindly label the socket), and negative
  886. * values on all other failures.
  887. *
  888. */
  889. int netlbl_sock_setattr(struct sock *sk,
  890. u16 family,
  891. const struct netlbl_lsm_secattr *secattr)
  892. {
  893. int ret_val;
  894. struct netlbl_dom_map *dom_entry;
  895. rcu_read_lock();
  896. dom_entry = netlbl_domhsh_getentry(secattr->domain, family);
  897. if (dom_entry == NULL) {
  898. ret_val = -ENOENT;
  899. goto socket_setattr_return;
  900. }
  901. switch (family) {
  902. case AF_INET:
  903. switch (dom_entry->def.type) {
  904. case NETLBL_NLTYPE_ADDRSELECT:
  905. ret_val = -EDESTADDRREQ;
  906. break;
  907. case NETLBL_NLTYPE_CIPSOV4:
  908. ret_val = cipso_v4_sock_setattr(sk,
  909. dom_entry->def.cipso,
  910. secattr);
  911. break;
  912. case NETLBL_NLTYPE_UNLABELED:
  913. ret_val = 0;
  914. break;
  915. default:
  916. ret_val = -ENOENT;
  917. }
  918. break;
  919. #if IS_ENABLED(CONFIG_IPV6)
  920. case AF_INET6:
  921. switch (dom_entry->def.type) {
  922. case NETLBL_NLTYPE_ADDRSELECT:
  923. ret_val = -EDESTADDRREQ;
  924. break;
  925. case NETLBL_NLTYPE_CALIPSO:
  926. ret_val = calipso_sock_setattr(sk,
  927. dom_entry->def.calipso,
  928. secattr);
  929. break;
  930. case NETLBL_NLTYPE_UNLABELED:
  931. ret_val = 0;
  932. break;
  933. default:
  934. ret_val = -ENOENT;
  935. }
  936. break;
  937. #endif /* IPv6 */
  938. default:
  939. ret_val = -EPROTONOSUPPORT;
  940. }
  941. socket_setattr_return:
  942. rcu_read_unlock();
  943. return ret_val;
  944. }
  945. /**
  946. * netlbl_sock_delattr - Delete all the NetLabel labels on a socket
  947. * @sk: the socket
  948. *
  949. * Description:
  950. * Remove all the NetLabel labeling from @sk. The caller is responsible for
  951. * ensuring that @sk is locked.
  952. *
  953. */
  954. void netlbl_sock_delattr(struct sock *sk)
  955. {
  956. switch (sk->sk_family) {
  957. case AF_INET:
  958. cipso_v4_sock_delattr(sk);
  959. break;
  960. #if IS_ENABLED(CONFIG_IPV6)
  961. case AF_INET6:
  962. calipso_sock_delattr(sk);
  963. break;
  964. #endif /* IPv6 */
  965. }
  966. }
  967. /**
  968. * netlbl_sock_getattr - Determine the security attributes of a sock
  969. * @sk: the sock
  970. * @secattr: the security attributes
  971. *
  972. * Description:
  973. * Examines the given sock to see if any NetLabel style labeling has been
  974. * applied to the sock, if so it parses the socket label and returns the
  975. * security attributes in @secattr. Returns zero on success, negative values
  976. * on failure.
  977. *
  978. */
  979. int netlbl_sock_getattr(struct sock *sk,
  980. struct netlbl_lsm_secattr *secattr)
  981. {
  982. int ret_val;
  983. switch (sk->sk_family) {
  984. case AF_INET:
  985. ret_val = cipso_v4_sock_getattr(sk, secattr);
  986. break;
  987. #if IS_ENABLED(CONFIG_IPV6)
  988. case AF_INET6:
  989. ret_val = calipso_sock_getattr(sk, secattr);
  990. break;
  991. #endif /* IPv6 */
  992. default:
  993. ret_val = -EPROTONOSUPPORT;
  994. }
  995. return ret_val;
  996. }
  997. /**
  998. * netlbl_conn_setattr - Label a connected socket using the correct protocol
  999. * @sk: the socket to label
  1000. * @addr: the destination address
  1001. * @secattr: the security attributes
  1002. *
  1003. * Description:
  1004. * Attach the correct label to the given connected socket using the security
  1005. * attributes specified in @secattr. The caller is responsible for ensuring
  1006. * that @sk is locked. Returns zero on success, negative values on failure.
  1007. *
  1008. */
  1009. int netlbl_conn_setattr(struct sock *sk,
  1010. struct sockaddr *addr,
  1011. const struct netlbl_lsm_secattr *secattr)
  1012. {
  1013. int ret_val;
  1014. struct sockaddr_in *addr4;
  1015. #if IS_ENABLED(CONFIG_IPV6)
  1016. struct sockaddr_in6 *addr6;
  1017. #endif
  1018. struct netlbl_dommap_def *entry;
  1019. rcu_read_lock();
  1020. switch (addr->sa_family) {
  1021. case AF_INET:
  1022. addr4 = (struct sockaddr_in *)addr;
  1023. entry = netlbl_domhsh_getentry_af4(secattr->domain,
  1024. addr4->sin_addr.s_addr);
  1025. if (entry == NULL) {
  1026. ret_val = -ENOENT;
  1027. goto conn_setattr_return;
  1028. }
  1029. switch (entry->type) {
  1030. case NETLBL_NLTYPE_CIPSOV4:
  1031. ret_val = cipso_v4_sock_setattr(sk,
  1032. entry->cipso, secattr);
  1033. break;
  1034. case NETLBL_NLTYPE_UNLABELED:
  1035. /* just delete the protocols we support for right now
  1036. * but we could remove other protocols if needed */
  1037. netlbl_sock_delattr(sk);
  1038. ret_val = 0;
  1039. break;
  1040. default:
  1041. ret_val = -ENOENT;
  1042. }
  1043. break;
  1044. #if IS_ENABLED(CONFIG_IPV6)
  1045. case AF_INET6:
  1046. addr6 = (struct sockaddr_in6 *)addr;
  1047. entry = netlbl_domhsh_getentry_af6(secattr->domain,
  1048. &addr6->sin6_addr);
  1049. if (entry == NULL) {
  1050. ret_val = -ENOENT;
  1051. goto conn_setattr_return;
  1052. }
  1053. switch (entry->type) {
  1054. case NETLBL_NLTYPE_CALIPSO:
  1055. ret_val = calipso_sock_setattr(sk,
  1056. entry->calipso, secattr);
  1057. break;
  1058. case NETLBL_NLTYPE_UNLABELED:
  1059. /* just delete the protocols we support for right now
  1060. * but we could remove other protocols if needed */
  1061. netlbl_sock_delattr(sk);
  1062. ret_val = 0;
  1063. break;
  1064. default:
  1065. ret_val = -ENOENT;
  1066. }
  1067. break;
  1068. #endif /* IPv6 */
  1069. default:
  1070. ret_val = -EPROTONOSUPPORT;
  1071. }
  1072. conn_setattr_return:
  1073. rcu_read_unlock();
  1074. return ret_val;
  1075. }
  1076. /**
  1077. * netlbl_req_setattr - Label a request socket using the correct protocol
  1078. * @req: the request socket to label
  1079. * @secattr: the security attributes
  1080. *
  1081. * Description:
  1082. * Attach the correct label to the given socket using the security attributes
  1083. * specified in @secattr. Returns zero on success, negative values on failure.
  1084. *
  1085. */
  1086. int netlbl_req_setattr(struct request_sock *req,
  1087. const struct netlbl_lsm_secattr *secattr)
  1088. {
  1089. int ret_val;
  1090. struct netlbl_dommap_def *entry;
  1091. struct inet_request_sock *ireq = inet_rsk(req);
  1092. rcu_read_lock();
  1093. switch (req->rsk_ops->family) {
  1094. case AF_INET:
  1095. entry = netlbl_domhsh_getentry_af4(secattr->domain,
  1096. ireq->ir_rmt_addr);
  1097. if (entry == NULL) {
  1098. ret_val = -ENOENT;
  1099. goto req_setattr_return;
  1100. }
  1101. switch (entry->type) {
  1102. case NETLBL_NLTYPE_CIPSOV4:
  1103. ret_val = cipso_v4_req_setattr(req,
  1104. entry->cipso, secattr);
  1105. break;
  1106. case NETLBL_NLTYPE_UNLABELED:
  1107. netlbl_req_delattr(req);
  1108. ret_val = 0;
  1109. break;
  1110. default:
  1111. ret_val = -ENOENT;
  1112. }
  1113. break;
  1114. #if IS_ENABLED(CONFIG_IPV6)
  1115. case AF_INET6:
  1116. entry = netlbl_domhsh_getentry_af6(secattr->domain,
  1117. &ireq->ir_v6_rmt_addr);
  1118. if (entry == NULL) {
  1119. ret_val = -ENOENT;
  1120. goto req_setattr_return;
  1121. }
  1122. switch (entry->type) {
  1123. case NETLBL_NLTYPE_CALIPSO:
  1124. ret_val = calipso_req_setattr(req,
  1125. entry->calipso, secattr);
  1126. break;
  1127. case NETLBL_NLTYPE_UNLABELED:
  1128. netlbl_req_delattr(req);
  1129. ret_val = 0;
  1130. break;
  1131. default:
  1132. ret_val = -ENOENT;
  1133. }
  1134. break;
  1135. #endif /* IPv6 */
  1136. default:
  1137. ret_val = -EPROTONOSUPPORT;
  1138. }
  1139. req_setattr_return:
  1140. rcu_read_unlock();
  1141. return ret_val;
  1142. }
  1143. /**
  1144. * netlbl_req_delattr - Delete all the NetLabel labels on a socket
  1145. * @req: the socket
  1146. *
  1147. * Description:
  1148. * Remove all the NetLabel labeling from @req.
  1149. *
  1150. */
  1151. void netlbl_req_delattr(struct request_sock *req)
  1152. {
  1153. switch (req->rsk_ops->family) {
  1154. case AF_INET:
  1155. cipso_v4_req_delattr(req);
  1156. break;
  1157. #if IS_ENABLED(CONFIG_IPV6)
  1158. case AF_INET6:
  1159. calipso_req_delattr(req);
  1160. break;
  1161. #endif /* IPv6 */
  1162. }
  1163. }
  1164. /**
  1165. * netlbl_skbuff_setattr - Label a packet using the correct protocol
  1166. * @skb: the packet
  1167. * @family: protocol family
  1168. * @secattr: the security attributes
  1169. *
  1170. * Description:
  1171. * Attach the correct label to the given packet using the security attributes
  1172. * specified in @secattr. Returns zero on success, negative values on failure.
  1173. *
  1174. */
  1175. int netlbl_skbuff_setattr(struct sk_buff *skb,
  1176. u16 family,
  1177. const struct netlbl_lsm_secattr *secattr)
  1178. {
  1179. int ret_val;
  1180. struct iphdr *hdr4;
  1181. #if IS_ENABLED(CONFIG_IPV6)
  1182. struct ipv6hdr *hdr6;
  1183. #endif
  1184. struct netlbl_dommap_def *entry;
  1185. rcu_read_lock();
  1186. switch (family) {
  1187. case AF_INET:
  1188. hdr4 = ip_hdr(skb);
  1189. entry = netlbl_domhsh_getentry_af4(secattr->domain,
  1190. hdr4->daddr);
  1191. if (entry == NULL) {
  1192. ret_val = -ENOENT;
  1193. goto skbuff_setattr_return;
  1194. }
  1195. switch (entry->type) {
  1196. case NETLBL_NLTYPE_CIPSOV4:
  1197. ret_val = cipso_v4_skbuff_setattr(skb, entry->cipso,
  1198. secattr);
  1199. break;
  1200. case NETLBL_NLTYPE_UNLABELED:
  1201. /* just delete the protocols we support for right now
  1202. * but we could remove other protocols if needed */
  1203. ret_val = cipso_v4_skbuff_delattr(skb);
  1204. break;
  1205. default:
  1206. ret_val = -ENOENT;
  1207. }
  1208. break;
  1209. #if IS_ENABLED(CONFIG_IPV6)
  1210. case AF_INET6:
  1211. hdr6 = ipv6_hdr(skb);
  1212. entry = netlbl_domhsh_getentry_af6(secattr->domain,
  1213. &hdr6->daddr);
  1214. if (entry == NULL) {
  1215. ret_val = -ENOENT;
  1216. goto skbuff_setattr_return;
  1217. }
  1218. switch (entry->type) {
  1219. case NETLBL_NLTYPE_CALIPSO:
  1220. ret_val = calipso_skbuff_setattr(skb, entry->calipso,
  1221. secattr);
  1222. break;
  1223. case NETLBL_NLTYPE_UNLABELED:
  1224. /* just delete the protocols we support for right now
  1225. * but we could remove other protocols if needed */
  1226. ret_val = calipso_skbuff_delattr(skb);
  1227. break;
  1228. default:
  1229. ret_val = -ENOENT;
  1230. }
  1231. break;
  1232. #endif /* IPv6 */
  1233. default:
  1234. ret_val = -EPROTONOSUPPORT;
  1235. }
  1236. skbuff_setattr_return:
  1237. rcu_read_unlock();
  1238. return ret_val;
  1239. }
  1240. /**
  1241. * netlbl_skbuff_getattr - Determine the security attributes of a packet
  1242. * @skb: the packet
  1243. * @family: protocol family
  1244. * @secattr: the security attributes
  1245. *
  1246. * Description:
  1247. * Examines the given packet to see if a recognized form of packet labeling
  1248. * is present, if so it parses the packet label and returns the security
  1249. * attributes in @secattr. Returns zero on success, negative values on
  1250. * failure.
  1251. *
  1252. */
  1253. int netlbl_skbuff_getattr(const struct sk_buff *skb,
  1254. u16 family,
  1255. struct netlbl_lsm_secattr *secattr)
  1256. {
  1257. unsigned char *ptr;
  1258. switch (family) {
  1259. case AF_INET:
  1260. ptr = cipso_v4_optptr(skb);
  1261. if (ptr && cipso_v4_getattr(ptr, secattr) == 0)
  1262. return 0;
  1263. break;
  1264. #if IS_ENABLED(CONFIG_IPV6)
  1265. case AF_INET6:
  1266. ptr = calipso_optptr(skb);
  1267. if (ptr && calipso_getattr(ptr, secattr) == 0)
  1268. return 0;
  1269. break;
  1270. #endif /* IPv6 */
  1271. }
  1272. return netlbl_unlabel_getattr(skb, family, secattr);
  1273. }
  1274. /**
  1275. * netlbl_skbuff_err - Handle a LSM error on a sk_buff
  1276. * @skb: the packet
  1277. * @family: the family
  1278. * @error: the error code
  1279. * @gateway: true if host is acting as a gateway, false otherwise
  1280. *
  1281. * Description:
  1282. * Deal with a LSM problem when handling the packet in @skb, typically this is
  1283. * a permission denied problem (-EACCES). The correct action is determined
  1284. * according to the packet's labeling protocol.
  1285. *
  1286. */
  1287. void netlbl_skbuff_err(struct sk_buff *skb, u16 family, int error, int gateway)
  1288. {
  1289. switch (family) {
  1290. case AF_INET:
  1291. if (cipso_v4_optptr(skb))
  1292. cipso_v4_error(skb, error, gateway);
  1293. break;
  1294. }
  1295. }
  1296. /**
  1297. * netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches
  1298. *
  1299. * Description:
  1300. * For all of the NetLabel protocols that support some form of label mapping
  1301. * cache, invalidate the cache. Returns zero on success, negative values on
  1302. * error.
  1303. *
  1304. */
  1305. void netlbl_cache_invalidate(void)
  1306. {
  1307. cipso_v4_cache_invalidate();
  1308. #if IS_ENABLED(CONFIG_IPV6)
  1309. calipso_cache_invalidate();
  1310. #endif /* IPv6 */
  1311. }
  1312. /**
  1313. * netlbl_cache_add - Add an entry to a NetLabel protocol cache
  1314. * @skb: the packet
  1315. * @family: the family
  1316. * @secattr: the packet's security attributes
  1317. *
  1318. * Description:
  1319. * Add the LSM security attributes for the given packet to the underlying
  1320. * NetLabel protocol's label mapping cache. Returns zero on success, negative
  1321. * values on error.
  1322. *
  1323. */
  1324. int netlbl_cache_add(const struct sk_buff *skb, u16 family,
  1325. const struct netlbl_lsm_secattr *secattr)
  1326. {
  1327. unsigned char *ptr;
  1328. if ((secattr->flags & NETLBL_SECATTR_CACHE) == 0)
  1329. return -ENOMSG;
  1330. switch (family) {
  1331. case AF_INET:
  1332. ptr = cipso_v4_optptr(skb);
  1333. if (ptr)
  1334. return cipso_v4_cache_add(ptr, secattr);
  1335. break;
  1336. #if IS_ENABLED(CONFIG_IPV6)
  1337. case AF_INET6:
  1338. ptr = calipso_optptr(skb);
  1339. if (ptr)
  1340. return calipso_cache_add(ptr, secattr);
  1341. break;
  1342. #endif /* IPv6 */
  1343. }
  1344. return -ENOMSG;
  1345. }
  1346. /*
  1347. * Protocol Engine Functions
  1348. */
  1349. /**
  1350. * netlbl_audit_start - Start an audit message
  1351. * @type: audit message type
  1352. * @audit_info: NetLabel audit information
  1353. *
  1354. * Description:
  1355. * Start an audit message using the type specified in @type and fill the audit
  1356. * message with some fields common to all NetLabel audit messages. This
  1357. * function should only be used by protocol engines, not LSMs. Returns a
  1358. * pointer to the audit buffer on success, NULL on failure.
  1359. *
  1360. */
  1361. struct audit_buffer *netlbl_audit_start(int type,
  1362. struct netlbl_audit *audit_info)
  1363. {
  1364. return netlbl_audit_start_common(type, audit_info);
  1365. }
  1366. EXPORT_SYMBOL(netlbl_audit_start);
  1367. /*
  1368. * Setup Functions
  1369. */
  1370. /**
  1371. * netlbl_init - Initialize NetLabel
  1372. *
  1373. * Description:
  1374. * Perform the required NetLabel initialization before first use.
  1375. *
  1376. */
  1377. static int __init netlbl_init(void)
  1378. {
  1379. int ret_val;
  1380. printk(KERN_INFO "NetLabel: Initializing\n");
  1381. printk(KERN_INFO "NetLabel: domain hash size = %u\n",
  1382. (1 << NETLBL_DOMHSH_BITSIZE));
  1383. printk(KERN_INFO "NetLabel: protocols = UNLABELED CIPSOv4 CALIPSO\n");
  1384. ret_val = netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE);
  1385. if (ret_val != 0)
  1386. goto init_failure;
  1387. ret_val = netlbl_unlabel_init(NETLBL_UNLHSH_BITSIZE);
  1388. if (ret_val != 0)
  1389. goto init_failure;
  1390. ret_val = netlbl_netlink_init();
  1391. if (ret_val != 0)
  1392. goto init_failure;
  1393. ret_val = netlbl_unlabel_defconf();
  1394. if (ret_val != 0)
  1395. goto init_failure;
  1396. printk(KERN_INFO "NetLabel: unlabeled traffic allowed by default\n");
  1397. return 0;
  1398. init_failure:
  1399. panic("NetLabel: failed to initialize properly (%d)\n", ret_val);
  1400. }
  1401. subsys_initcall(netlbl_init);