netlabel_kapi.c 27 KB

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