netlabel_kapi.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  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 <paul@paul-moore.com>
  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. u64 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. u64 bitmask;
  614. u64 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] |= (u64)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.
  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. * @sk_locked: true if caller holds the socket lock
  880. *
  881. * Description:
  882. * Attach the correct label to the given socket using the security attributes
  883. * specified in @secattr. This function requires exclusive access to @sk,
  884. * which means it either needs to be in the process of being created or locked.
  885. * Returns zero on success, -EDESTADDRREQ if the domain is configured to use
  886. * network address selectors (can't blindly label the socket), and negative
  887. * values on all other failures.
  888. *
  889. */
  890. int netlbl_sock_setattr(struct sock *sk,
  891. u16 family,
  892. const struct netlbl_lsm_secattr *secattr,
  893. bool sk_locked)
  894. {
  895. int ret_val;
  896. struct netlbl_dom_map *dom_entry;
  897. rcu_read_lock();
  898. dom_entry = netlbl_domhsh_getentry(secattr->domain, family);
  899. if (dom_entry == NULL) {
  900. ret_val = -ENOENT;
  901. goto socket_setattr_return;
  902. }
  903. switch (family) {
  904. case AF_INET:
  905. switch (dom_entry->def.type) {
  906. case NETLBL_NLTYPE_ADDRSELECT:
  907. ret_val = -EDESTADDRREQ;
  908. break;
  909. case NETLBL_NLTYPE_CIPSOV4:
  910. ret_val = cipso_v4_sock_setattr(sk,
  911. dom_entry->def.cipso,
  912. secattr, sk_locked);
  913. break;
  914. case NETLBL_NLTYPE_UNLABELED:
  915. ret_val = 0;
  916. break;
  917. default:
  918. ret_val = -ENOENT;
  919. }
  920. break;
  921. #if IS_ENABLED(CONFIG_IPV6)
  922. case AF_INET6:
  923. switch (dom_entry->def.type) {
  924. case NETLBL_NLTYPE_ADDRSELECT:
  925. ret_val = -EDESTADDRREQ;
  926. break;
  927. case NETLBL_NLTYPE_CALIPSO:
  928. ret_val = calipso_sock_setattr(sk,
  929. dom_entry->def.calipso,
  930. secattr);
  931. break;
  932. case NETLBL_NLTYPE_UNLABELED:
  933. ret_val = 0;
  934. break;
  935. default:
  936. ret_val = -ENOENT;
  937. }
  938. break;
  939. #endif /* IPv6 */
  940. default:
  941. ret_val = -EPROTONOSUPPORT;
  942. }
  943. socket_setattr_return:
  944. rcu_read_unlock();
  945. return ret_val;
  946. }
  947. /**
  948. * netlbl_sock_delattr - Delete all the NetLabel labels on a socket
  949. * @sk: the socket
  950. *
  951. * Description:
  952. * Remove all the NetLabel labeling from @sk. The caller is responsible for
  953. * ensuring that @sk is locked.
  954. *
  955. */
  956. void netlbl_sock_delattr(struct sock *sk)
  957. {
  958. switch (sk->sk_family) {
  959. case AF_INET:
  960. cipso_v4_sock_delattr(sk);
  961. break;
  962. #if IS_ENABLED(CONFIG_IPV6)
  963. case AF_INET6:
  964. calipso_sock_delattr(sk);
  965. break;
  966. #endif /* IPv6 */
  967. }
  968. }
  969. /**
  970. * netlbl_sock_getattr - Determine the security attributes of a sock
  971. * @sk: the sock
  972. * @secattr: the security attributes
  973. *
  974. * Description:
  975. * Examines the given sock to see if any NetLabel style labeling has been
  976. * applied to the sock, if so it parses the socket label and returns the
  977. * security attributes in @secattr. Returns zero on success, negative values
  978. * on failure.
  979. *
  980. */
  981. int netlbl_sock_getattr(struct sock *sk,
  982. struct netlbl_lsm_secattr *secattr)
  983. {
  984. int ret_val;
  985. switch (sk->sk_family) {
  986. case AF_INET:
  987. ret_val = cipso_v4_sock_getattr(sk, secattr);
  988. break;
  989. #if IS_ENABLED(CONFIG_IPV6)
  990. case AF_INET6:
  991. ret_val = calipso_sock_getattr(sk, secattr);
  992. break;
  993. #endif /* IPv6 */
  994. default:
  995. ret_val = -EPROTONOSUPPORT;
  996. }
  997. return ret_val;
  998. }
  999. /**
  1000. * netlbl_sk_lock_check - Check if the socket lock has been acquired.
  1001. * @sk: the socket to be checked
  1002. *
  1003. * Return: true if socket @sk is locked or if lock debugging is disabled at
  1004. * runtime or compile-time; false otherwise
  1005. *
  1006. */
  1007. #ifdef CONFIG_LOCKDEP
  1008. bool netlbl_sk_lock_check(struct sock *sk)
  1009. {
  1010. if (debug_locks)
  1011. return lockdep_sock_is_held(sk);
  1012. return true;
  1013. }
  1014. #else
  1015. bool netlbl_sk_lock_check(struct sock *sk)
  1016. {
  1017. return true;
  1018. }
  1019. #endif
  1020. /**
  1021. * netlbl_conn_setattr - Label a connected socket using the correct protocol
  1022. * @sk: the socket to label
  1023. * @addr: the destination address
  1024. * @secattr: the security attributes
  1025. *
  1026. * Description:
  1027. * Attach the correct label to the given connected socket using the security
  1028. * attributes specified in @secattr. The caller is responsible for ensuring
  1029. * that @sk is locked. Returns zero on success, negative values on failure.
  1030. *
  1031. */
  1032. int netlbl_conn_setattr(struct sock *sk,
  1033. struct sockaddr *addr,
  1034. const struct netlbl_lsm_secattr *secattr)
  1035. {
  1036. int ret_val;
  1037. struct sockaddr_in *addr4;
  1038. #if IS_ENABLED(CONFIG_IPV6)
  1039. struct sockaddr_in6 *addr6;
  1040. #endif
  1041. struct netlbl_dommap_def *entry;
  1042. rcu_read_lock();
  1043. switch (addr->sa_family) {
  1044. case AF_INET:
  1045. addr4 = (struct sockaddr_in *)addr;
  1046. entry = netlbl_domhsh_getentry_af4(secattr->domain,
  1047. addr4->sin_addr.s_addr);
  1048. if (entry == NULL) {
  1049. ret_val = -ENOENT;
  1050. goto conn_setattr_return;
  1051. }
  1052. switch (entry->type) {
  1053. case NETLBL_NLTYPE_CIPSOV4:
  1054. ret_val = cipso_v4_sock_setattr(sk,
  1055. entry->cipso, secattr,
  1056. netlbl_sk_lock_check(sk));
  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. #if IS_ENABLED(CONFIG_IPV6)
  1069. case AF_INET6:
  1070. addr6 = (struct sockaddr_in6 *)addr;
  1071. entry = netlbl_domhsh_getentry_af6(secattr->domain,
  1072. &addr6->sin6_addr);
  1073. if (entry == NULL) {
  1074. ret_val = -ENOENT;
  1075. goto conn_setattr_return;
  1076. }
  1077. switch (entry->type) {
  1078. case NETLBL_NLTYPE_CALIPSO:
  1079. ret_val = calipso_sock_setattr(sk,
  1080. entry->calipso, secattr);
  1081. break;
  1082. case NETLBL_NLTYPE_UNLABELED:
  1083. /* just delete the protocols we support for right now
  1084. * but we could remove other protocols if needed */
  1085. netlbl_sock_delattr(sk);
  1086. ret_val = 0;
  1087. break;
  1088. default:
  1089. ret_val = -ENOENT;
  1090. }
  1091. break;
  1092. #endif /* IPv6 */
  1093. default:
  1094. ret_val = -EPROTONOSUPPORT;
  1095. }
  1096. conn_setattr_return:
  1097. rcu_read_unlock();
  1098. return ret_val;
  1099. }
  1100. /**
  1101. * netlbl_req_setattr - Label a request socket using the correct protocol
  1102. * @req: the request socket to label
  1103. * @secattr: the security attributes
  1104. *
  1105. * Description:
  1106. * Attach the correct label to the given socket using the security attributes
  1107. * specified in @secattr. Returns zero on success, negative values on failure.
  1108. *
  1109. */
  1110. int netlbl_req_setattr(struct request_sock *req,
  1111. const struct netlbl_lsm_secattr *secattr)
  1112. {
  1113. int ret_val;
  1114. struct netlbl_dommap_def *entry;
  1115. struct inet_request_sock *ireq = inet_rsk(req);
  1116. rcu_read_lock();
  1117. switch (req->rsk_ops->family) {
  1118. case AF_INET:
  1119. entry = netlbl_domhsh_getentry_af4(secattr->domain,
  1120. ireq->ir_rmt_addr);
  1121. if (entry == NULL) {
  1122. ret_val = -ENOENT;
  1123. goto req_setattr_return;
  1124. }
  1125. switch (entry->type) {
  1126. case NETLBL_NLTYPE_CIPSOV4:
  1127. ret_val = cipso_v4_req_setattr(req,
  1128. entry->cipso, secattr);
  1129. break;
  1130. case NETLBL_NLTYPE_UNLABELED:
  1131. netlbl_req_delattr(req);
  1132. ret_val = 0;
  1133. break;
  1134. default:
  1135. ret_val = -ENOENT;
  1136. }
  1137. break;
  1138. #if IS_ENABLED(CONFIG_IPV6)
  1139. case AF_INET6:
  1140. entry = netlbl_domhsh_getentry_af6(secattr->domain,
  1141. &ireq->ir_v6_rmt_addr);
  1142. if (entry == NULL) {
  1143. ret_val = -ENOENT;
  1144. goto req_setattr_return;
  1145. }
  1146. switch (entry->type) {
  1147. case NETLBL_NLTYPE_CALIPSO:
  1148. ret_val = calipso_req_setattr(req,
  1149. entry->calipso, secattr);
  1150. break;
  1151. case NETLBL_NLTYPE_UNLABELED:
  1152. netlbl_req_delattr(req);
  1153. ret_val = 0;
  1154. break;
  1155. default:
  1156. ret_val = -ENOENT;
  1157. }
  1158. break;
  1159. #endif /* IPv6 */
  1160. default:
  1161. ret_val = -EPROTONOSUPPORT;
  1162. }
  1163. req_setattr_return:
  1164. rcu_read_unlock();
  1165. return ret_val;
  1166. }
  1167. /**
  1168. * netlbl_req_delattr - Delete all the NetLabel labels on a socket
  1169. * @req: the socket
  1170. *
  1171. * Description:
  1172. * Remove all the NetLabel labeling from @req.
  1173. *
  1174. */
  1175. void netlbl_req_delattr(struct request_sock *req)
  1176. {
  1177. switch (req->rsk_ops->family) {
  1178. case AF_INET:
  1179. cipso_v4_req_delattr(req);
  1180. break;
  1181. #if IS_ENABLED(CONFIG_IPV6)
  1182. case AF_INET6:
  1183. calipso_req_delattr(req);
  1184. break;
  1185. #endif /* IPv6 */
  1186. }
  1187. }
  1188. /**
  1189. * netlbl_skbuff_setattr - Label a packet using the correct protocol
  1190. * @skb: the packet
  1191. * @family: protocol family
  1192. * @secattr: the security attributes
  1193. *
  1194. * Description:
  1195. * Attach the correct label to the given packet using the security attributes
  1196. * specified in @secattr. Returns zero on success, negative values on failure.
  1197. *
  1198. */
  1199. int netlbl_skbuff_setattr(struct sk_buff *skb,
  1200. u16 family,
  1201. const struct netlbl_lsm_secattr *secattr)
  1202. {
  1203. int ret_val;
  1204. struct iphdr *hdr4;
  1205. #if IS_ENABLED(CONFIG_IPV6)
  1206. struct ipv6hdr *hdr6;
  1207. #endif
  1208. struct netlbl_dommap_def *entry;
  1209. rcu_read_lock();
  1210. switch (family) {
  1211. case AF_INET:
  1212. hdr4 = ip_hdr(skb);
  1213. entry = netlbl_domhsh_getentry_af4(secattr->domain,
  1214. hdr4->daddr);
  1215. if (entry == NULL) {
  1216. ret_val = -ENOENT;
  1217. goto skbuff_setattr_return;
  1218. }
  1219. switch (entry->type) {
  1220. case NETLBL_NLTYPE_CIPSOV4:
  1221. ret_val = cipso_v4_skbuff_setattr(skb, entry->cipso,
  1222. secattr);
  1223. break;
  1224. case NETLBL_NLTYPE_UNLABELED:
  1225. /* just delete the protocols we support for right now
  1226. * but we could remove other protocols if needed */
  1227. ret_val = cipso_v4_skbuff_delattr(skb);
  1228. break;
  1229. default:
  1230. ret_val = -ENOENT;
  1231. }
  1232. break;
  1233. #if IS_ENABLED(CONFIG_IPV6)
  1234. case AF_INET6:
  1235. hdr6 = ipv6_hdr(skb);
  1236. entry = netlbl_domhsh_getentry_af6(secattr->domain,
  1237. &hdr6->daddr);
  1238. if (entry == NULL) {
  1239. ret_val = -ENOENT;
  1240. goto skbuff_setattr_return;
  1241. }
  1242. switch (entry->type) {
  1243. case NETLBL_NLTYPE_CALIPSO:
  1244. ret_val = calipso_skbuff_setattr(skb, entry->calipso,
  1245. secattr);
  1246. break;
  1247. case NETLBL_NLTYPE_UNLABELED:
  1248. /* just delete the protocols we support for right now
  1249. * but we could remove other protocols if needed */
  1250. ret_val = calipso_skbuff_delattr(skb);
  1251. break;
  1252. default:
  1253. ret_val = -ENOENT;
  1254. }
  1255. break;
  1256. #endif /* IPv6 */
  1257. default:
  1258. ret_val = -EPROTONOSUPPORT;
  1259. }
  1260. skbuff_setattr_return:
  1261. rcu_read_unlock();
  1262. return ret_val;
  1263. }
  1264. /**
  1265. * netlbl_skbuff_getattr - Determine the security attributes of a packet
  1266. * @skb: the packet
  1267. * @family: protocol family
  1268. * @secattr: the security attributes
  1269. *
  1270. * Description:
  1271. * Examines the given packet to see if a recognized form of packet labeling
  1272. * is present, if so it parses the packet label and returns the security
  1273. * attributes in @secattr. Returns zero on success, negative values on
  1274. * failure.
  1275. *
  1276. */
  1277. int netlbl_skbuff_getattr(const struct sk_buff *skb,
  1278. u16 family,
  1279. struct netlbl_lsm_secattr *secattr)
  1280. {
  1281. unsigned char *ptr;
  1282. switch (family) {
  1283. case AF_INET:
  1284. ptr = cipso_v4_optptr(skb);
  1285. if (ptr && cipso_v4_getattr(ptr, secattr) == 0)
  1286. return 0;
  1287. break;
  1288. #if IS_ENABLED(CONFIG_IPV6)
  1289. case AF_INET6:
  1290. ptr = calipso_optptr(skb);
  1291. if (ptr && calipso_getattr(ptr, secattr) == 0)
  1292. return 0;
  1293. break;
  1294. #endif /* IPv6 */
  1295. }
  1296. return netlbl_unlabel_getattr(skb, family, secattr);
  1297. }
  1298. /**
  1299. * netlbl_skbuff_err - Handle a LSM error on a sk_buff
  1300. * @skb: the packet
  1301. * @family: the family
  1302. * @error: the error code
  1303. * @gateway: true if host is acting as a gateway, false otherwise
  1304. *
  1305. * Description:
  1306. * Deal with a LSM problem when handling the packet in @skb, typically this is
  1307. * a permission denied problem (-EACCES). The correct action is determined
  1308. * according to the packet's labeling protocol.
  1309. *
  1310. */
  1311. void netlbl_skbuff_err(struct sk_buff *skb, u16 family, int error, int gateway)
  1312. {
  1313. switch (family) {
  1314. case AF_INET:
  1315. if (cipso_v4_optptr(skb))
  1316. cipso_v4_error(skb, error, gateway);
  1317. break;
  1318. }
  1319. }
  1320. /**
  1321. * netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches
  1322. *
  1323. * Description:
  1324. * For all of the NetLabel protocols that support some form of label mapping
  1325. * cache, invalidate the cache. Returns zero on success, negative values on
  1326. * error.
  1327. *
  1328. */
  1329. void netlbl_cache_invalidate(void)
  1330. {
  1331. cipso_v4_cache_invalidate();
  1332. #if IS_ENABLED(CONFIG_IPV6)
  1333. calipso_cache_invalidate();
  1334. #endif /* IPv6 */
  1335. }
  1336. /**
  1337. * netlbl_cache_add - Add an entry to a NetLabel protocol cache
  1338. * @skb: the packet
  1339. * @family: the family
  1340. * @secattr: the packet's security attributes
  1341. *
  1342. * Description:
  1343. * Add the LSM security attributes for the given packet to the underlying
  1344. * NetLabel protocol's label mapping cache. Returns zero on success, negative
  1345. * values on error.
  1346. *
  1347. */
  1348. int netlbl_cache_add(const struct sk_buff *skb, u16 family,
  1349. const struct netlbl_lsm_secattr *secattr)
  1350. {
  1351. unsigned char *ptr;
  1352. if ((secattr->flags & NETLBL_SECATTR_CACHE) == 0)
  1353. return -ENOMSG;
  1354. switch (family) {
  1355. case AF_INET:
  1356. ptr = cipso_v4_optptr(skb);
  1357. if (ptr)
  1358. return cipso_v4_cache_add(ptr, secattr);
  1359. break;
  1360. #if IS_ENABLED(CONFIG_IPV6)
  1361. case AF_INET6:
  1362. ptr = calipso_optptr(skb);
  1363. if (ptr)
  1364. return calipso_cache_add(ptr, secattr);
  1365. break;
  1366. #endif /* IPv6 */
  1367. }
  1368. return -ENOMSG;
  1369. }
  1370. /*
  1371. * Protocol Engine Functions
  1372. */
  1373. /**
  1374. * netlbl_audit_start - Start an audit message
  1375. * @type: audit message type
  1376. * @audit_info: NetLabel audit information
  1377. *
  1378. * Description:
  1379. * Start an audit message using the type specified in @type and fill the audit
  1380. * message with some fields common to all NetLabel audit messages. This
  1381. * function should only be used by protocol engines, not LSMs. Returns a
  1382. * pointer to the audit buffer on success, NULL on failure.
  1383. *
  1384. */
  1385. struct audit_buffer *netlbl_audit_start(int type,
  1386. struct netlbl_audit *audit_info)
  1387. {
  1388. return netlbl_audit_start_common(type, audit_info);
  1389. }
  1390. EXPORT_SYMBOL(netlbl_audit_start);
  1391. /*
  1392. * Setup Functions
  1393. */
  1394. /**
  1395. * netlbl_init - Initialize NetLabel
  1396. *
  1397. * Description:
  1398. * Perform the required NetLabel initialization before first use.
  1399. *
  1400. */
  1401. static int __init netlbl_init(void)
  1402. {
  1403. int ret_val;
  1404. printk(KERN_INFO "NetLabel: Initializing\n");
  1405. printk(KERN_INFO "NetLabel: domain hash size = %u\n",
  1406. (1 << NETLBL_DOMHSH_BITSIZE));
  1407. printk(KERN_INFO "NetLabel: protocols = UNLABELED CIPSOv4 CALIPSO\n");
  1408. ret_val = netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE);
  1409. if (ret_val != 0)
  1410. goto init_failure;
  1411. ret_val = netlbl_unlabel_init(NETLBL_UNLHSH_BITSIZE);
  1412. if (ret_val != 0)
  1413. goto init_failure;
  1414. ret_val = netlbl_netlink_init();
  1415. if (ret_val != 0)
  1416. goto init_failure;
  1417. ret_val = netlbl_unlabel_defconf();
  1418. if (ret_val != 0)
  1419. goto init_failure;
  1420. printk(KERN_INFO "NetLabel: unlabeled traffic allowed by default\n");
  1421. return 0;
  1422. init_failure:
  1423. panic("NetLabel: failed to initialize properly (%d)\n", ret_val);
  1424. }
  1425. subsys_initcall(netlbl_init);