netlabel_kapi.c 30 KB

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