netlabel_kapi.c 30 KB

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