netlabel_kapi.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  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, family, 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. entry->family = family;
  116. if (addr == NULL && mask == NULL)
  117. entry->def.type = NETLBL_NLTYPE_UNLABELED;
  118. else if (addr != NULL && mask != NULL) {
  119. addrmap = kzalloc(sizeof(*addrmap), GFP_ATOMIC);
  120. if (addrmap == NULL)
  121. goto cfg_unlbl_map_add_failure;
  122. INIT_LIST_HEAD(&addrmap->list4);
  123. INIT_LIST_HEAD(&addrmap->list6);
  124. switch (family) {
  125. case AF_INET: {
  126. const struct in_addr *addr4 = addr;
  127. const struct in_addr *mask4 = mask;
  128. map4 = kzalloc(sizeof(*map4), GFP_ATOMIC);
  129. if (map4 == NULL)
  130. goto cfg_unlbl_map_add_failure;
  131. map4->def.type = NETLBL_NLTYPE_UNLABELED;
  132. map4->list.addr = addr4->s_addr & mask4->s_addr;
  133. map4->list.mask = mask4->s_addr;
  134. map4->list.valid = 1;
  135. ret_val = netlbl_af4list_add(&map4->list,
  136. &addrmap->list4);
  137. if (ret_val != 0)
  138. goto cfg_unlbl_map_add_failure;
  139. break;
  140. }
  141. #if IS_ENABLED(CONFIG_IPV6)
  142. case AF_INET6: {
  143. const struct in6_addr *addr6 = addr;
  144. const struct in6_addr *mask6 = mask;
  145. map6 = kzalloc(sizeof(*map6), GFP_ATOMIC);
  146. if (map6 == NULL)
  147. goto cfg_unlbl_map_add_failure;
  148. map6->def.type = NETLBL_NLTYPE_UNLABELED;
  149. map6->list.addr = *addr6;
  150. map6->list.addr.s6_addr32[0] &= mask6->s6_addr32[0];
  151. map6->list.addr.s6_addr32[1] &= mask6->s6_addr32[1];
  152. map6->list.addr.s6_addr32[2] &= mask6->s6_addr32[2];
  153. map6->list.addr.s6_addr32[3] &= mask6->s6_addr32[3];
  154. map6->list.mask = *mask6;
  155. map6->list.valid = 1;
  156. ret_val = netlbl_af6list_add(&map6->list,
  157. &addrmap->list6);
  158. if (ret_val != 0)
  159. goto cfg_unlbl_map_add_failure;
  160. break;
  161. }
  162. #endif /* IPv6 */
  163. default:
  164. goto cfg_unlbl_map_add_failure;
  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. * @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. entry->family = AF_INET;
  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. #define _CM_F_NONE 0x00000000
  376. #define _CM_F_ALLOC 0x00000001
  377. #define _CM_F_WALK 0x00000002
  378. /**
  379. * _netlbl_catmap_getnode - Get a individual node from a catmap
  380. * @catmap: pointer to the category bitmap
  381. * @offset: the requested offset
  382. * @cm_flags: catmap flags, see _CM_F_*
  383. * @gfp_flags: memory allocation flags
  384. *
  385. * Description:
  386. * Iterate through the catmap looking for the node associated with @offset.
  387. * If the _CM_F_ALLOC flag is set in @cm_flags and there is no associated node,
  388. * one will be created and inserted into the catmap. If the _CM_F_WALK flag is
  389. * set in @cm_flags and there is no associated node, the next highest node will
  390. * be returned. Returns a pointer to the node on success, NULL on failure.
  391. *
  392. */
  393. static struct netlbl_lsm_catmap *_netlbl_catmap_getnode(
  394. struct netlbl_lsm_catmap **catmap,
  395. u32 offset,
  396. unsigned int cm_flags,
  397. gfp_t gfp_flags)
  398. {
  399. struct netlbl_lsm_catmap *iter = *catmap;
  400. struct netlbl_lsm_catmap *prev = NULL;
  401. if (iter == NULL)
  402. goto catmap_getnode_alloc;
  403. if (offset < iter->startbit)
  404. goto catmap_getnode_walk;
  405. while (iter && offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
  406. prev = iter;
  407. iter = iter->next;
  408. }
  409. if (iter == NULL || offset < iter->startbit)
  410. goto catmap_getnode_walk;
  411. return iter;
  412. catmap_getnode_walk:
  413. if (cm_flags & _CM_F_WALK)
  414. return iter;
  415. catmap_getnode_alloc:
  416. if (!(cm_flags & _CM_F_ALLOC))
  417. return NULL;
  418. iter = netlbl_catmap_alloc(gfp_flags);
  419. if (iter == NULL)
  420. return NULL;
  421. iter->startbit = offset & ~(NETLBL_CATMAP_SIZE - 1);
  422. if (prev == NULL) {
  423. iter->next = *catmap;
  424. *catmap = iter;
  425. } else {
  426. iter->next = prev->next;
  427. prev->next = iter;
  428. }
  429. return iter;
  430. }
  431. /**
  432. * netlbl_catmap_walk - Walk a LSM secattr catmap looking for a bit
  433. * @catmap: the category bitmap
  434. * @offset: the offset to start searching at, in bits
  435. *
  436. * Description:
  437. * This function walks a LSM secattr category bitmap starting at @offset and
  438. * returns the spot of the first set bit or -ENOENT if no bits are set.
  439. *
  440. */
  441. int netlbl_catmap_walk(struct netlbl_lsm_catmap *catmap, u32 offset)
  442. {
  443. struct netlbl_lsm_catmap *iter = catmap;
  444. u32 idx;
  445. u32 bit;
  446. NETLBL_CATMAP_MAPTYPE bitmap;
  447. iter = _netlbl_catmap_getnode(&catmap, offset, _CM_F_WALK, 0);
  448. if (iter == NULL)
  449. return -ENOENT;
  450. if (offset > iter->startbit) {
  451. offset -= iter->startbit;
  452. idx = offset / NETLBL_CATMAP_MAPSIZE;
  453. bit = offset % NETLBL_CATMAP_MAPSIZE;
  454. } else {
  455. idx = 0;
  456. bit = 0;
  457. }
  458. bitmap = iter->bitmap[idx] >> bit;
  459. for (;;) {
  460. if (bitmap != 0) {
  461. while ((bitmap & NETLBL_CATMAP_BIT) == 0) {
  462. bitmap >>= 1;
  463. bit++;
  464. }
  465. return iter->startbit +
  466. (NETLBL_CATMAP_MAPSIZE * idx) + bit;
  467. }
  468. if (++idx >= NETLBL_CATMAP_MAPCNT) {
  469. if (iter->next != NULL) {
  470. iter = iter->next;
  471. idx = 0;
  472. } else
  473. return -ENOENT;
  474. }
  475. bitmap = iter->bitmap[idx];
  476. bit = 0;
  477. }
  478. return -ENOENT;
  479. }
  480. /**
  481. * netlbl_catmap_walkrng - Find the end of a string of set bits
  482. * @catmap: the category bitmap
  483. * @offset: the offset to start searching at, in bits
  484. *
  485. * Description:
  486. * This function walks a LSM secattr category bitmap starting at @offset and
  487. * returns the spot of the first cleared bit or -ENOENT if the offset is past
  488. * the end of the bitmap.
  489. *
  490. */
  491. int netlbl_catmap_walkrng(struct netlbl_lsm_catmap *catmap, u32 offset)
  492. {
  493. struct netlbl_lsm_catmap *iter;
  494. struct netlbl_lsm_catmap *prev = NULL;
  495. u32 idx;
  496. u32 bit;
  497. NETLBL_CATMAP_MAPTYPE bitmask;
  498. NETLBL_CATMAP_MAPTYPE bitmap;
  499. iter = _netlbl_catmap_getnode(&catmap, offset, _CM_F_WALK, 0);
  500. if (iter == NULL)
  501. return -ENOENT;
  502. if (offset > iter->startbit) {
  503. offset -= iter->startbit;
  504. idx = offset / NETLBL_CATMAP_MAPSIZE;
  505. bit = offset % NETLBL_CATMAP_MAPSIZE;
  506. } else {
  507. idx = 0;
  508. bit = 0;
  509. }
  510. bitmask = NETLBL_CATMAP_BIT << bit;
  511. for (;;) {
  512. bitmap = iter->bitmap[idx];
  513. while (bitmask != 0 && (bitmap & bitmask) != 0) {
  514. bitmask <<= 1;
  515. bit++;
  516. }
  517. if (prev && idx == 0 && bit == 0)
  518. return prev->startbit + NETLBL_CATMAP_SIZE - 1;
  519. else if (bitmask != 0)
  520. return iter->startbit +
  521. (NETLBL_CATMAP_MAPSIZE * idx) + bit - 1;
  522. else if (++idx >= NETLBL_CATMAP_MAPCNT) {
  523. if (iter->next == NULL)
  524. return iter->startbit + NETLBL_CATMAP_SIZE - 1;
  525. prev = iter;
  526. iter = iter->next;
  527. idx = 0;
  528. }
  529. bitmask = NETLBL_CATMAP_BIT;
  530. bit = 0;
  531. }
  532. return -ENOENT;
  533. }
  534. /**
  535. * netlbl_catmap_getlong - Export an unsigned long bitmap
  536. * @catmap: pointer to the category bitmap
  537. * @offset: pointer to the requested offset
  538. * @bitmap: the exported bitmap
  539. *
  540. * Description:
  541. * Export a bitmap with an offset greater than or equal to @offset and return
  542. * it in @bitmap. The @offset must be aligned to an unsigned long and will be
  543. * updated on return if different from what was requested; if the catmap is
  544. * empty at the requested offset and beyond, the @offset is set to (u32)-1.
  545. * Returns zero on sucess, negative values on failure.
  546. *
  547. */
  548. int netlbl_catmap_getlong(struct netlbl_lsm_catmap *catmap,
  549. u32 *offset,
  550. unsigned long *bitmap)
  551. {
  552. struct netlbl_lsm_catmap *iter;
  553. u32 off = *offset;
  554. u32 idx;
  555. /* only allow aligned offsets */
  556. if ((off & (BITS_PER_LONG - 1)) != 0)
  557. return -EINVAL;
  558. if (off < catmap->startbit) {
  559. off = catmap->startbit;
  560. *offset = off;
  561. }
  562. iter = _netlbl_catmap_getnode(&catmap, off, _CM_F_WALK, 0);
  563. if (iter == NULL) {
  564. *offset = (u32)-1;
  565. return 0;
  566. }
  567. if (off < iter->startbit) {
  568. *offset = iter->startbit;
  569. off = 0;
  570. } else
  571. off -= iter->startbit;
  572. idx = off / NETLBL_CATMAP_MAPSIZE;
  573. *bitmap = iter->bitmap[idx] >> (off % NETLBL_CATMAP_MAPSIZE);
  574. return 0;
  575. }
  576. /**
  577. * netlbl_catmap_setbit - Set a bit in a LSM secattr catmap
  578. * @catmap: pointer to the category bitmap
  579. * @bit: the bit to set
  580. * @flags: memory allocation flags
  581. *
  582. * Description:
  583. * Set the bit specified by @bit in @catmap. Returns zero on success,
  584. * negative values on failure.
  585. *
  586. */
  587. int netlbl_catmap_setbit(struct netlbl_lsm_catmap **catmap,
  588. u32 bit,
  589. gfp_t flags)
  590. {
  591. struct netlbl_lsm_catmap *iter;
  592. u32 idx;
  593. iter = _netlbl_catmap_getnode(catmap, bit, _CM_F_ALLOC, flags);
  594. if (iter == NULL)
  595. return -ENOMEM;
  596. bit -= iter->startbit;
  597. idx = bit / NETLBL_CATMAP_MAPSIZE;
  598. iter->bitmap[idx] |= NETLBL_CATMAP_BIT << (bit % NETLBL_CATMAP_MAPSIZE);
  599. return 0;
  600. }
  601. /**
  602. * netlbl_catmap_setrng - Set a range of bits in a LSM secattr catmap
  603. * @catmap: pointer to the category bitmap
  604. * @start: the starting bit
  605. * @end: the last bit in the string
  606. * @flags: memory allocation flags
  607. *
  608. * Description:
  609. * Set a range of bits, starting at @start and ending with @end. Returns zero
  610. * on success, negative values on failure.
  611. *
  612. */
  613. int netlbl_catmap_setrng(struct netlbl_lsm_catmap **catmap,
  614. u32 start,
  615. u32 end,
  616. gfp_t flags)
  617. {
  618. int rc = 0;
  619. u32 spot = start;
  620. while (rc == 0 && spot <= end) {
  621. if (((spot & (BITS_PER_LONG - 1)) == 0) &&
  622. ((end - spot) > BITS_PER_LONG)) {
  623. rc = netlbl_catmap_setlong(catmap,
  624. spot,
  625. (unsigned long)-1,
  626. flags);
  627. spot += BITS_PER_LONG;
  628. } else
  629. rc = netlbl_catmap_setbit(catmap, spot++, flags);
  630. }
  631. return rc;
  632. }
  633. /**
  634. * netlbl_catmap_setlong - Import an unsigned long bitmap
  635. * @catmap: pointer to the category bitmap
  636. * @offset: offset to the start of the imported bitmap
  637. * @bitmap: the bitmap to import
  638. * @flags: memory allocation flags
  639. *
  640. * Description:
  641. * Import the bitmap specified in @bitmap into @catmap, using the offset
  642. * in @offset. The offset must be aligned to an unsigned long. Returns zero
  643. * on success, negative values on failure.
  644. *
  645. */
  646. int netlbl_catmap_setlong(struct netlbl_lsm_catmap **catmap,
  647. u32 offset,
  648. unsigned long bitmap,
  649. gfp_t flags)
  650. {
  651. struct netlbl_lsm_catmap *iter;
  652. u32 idx;
  653. /* only allow aligned offsets */
  654. if ((offset & (BITS_PER_LONG - 1)) != 0)
  655. return -EINVAL;
  656. iter = _netlbl_catmap_getnode(catmap, offset, _CM_F_ALLOC, flags);
  657. if (iter == NULL)
  658. return -ENOMEM;
  659. offset -= iter->startbit;
  660. idx = offset / NETLBL_CATMAP_MAPSIZE;
  661. iter->bitmap[idx] |= bitmap << (offset % NETLBL_CATMAP_MAPSIZE);
  662. return 0;
  663. }
  664. /* Bitmap functions
  665. */
  666. /**
  667. * netlbl_bitmap_walk - Walk a bitmap looking for a bit
  668. * @bitmap: the bitmap
  669. * @bitmap_len: length in bits
  670. * @offset: starting offset
  671. * @state: if non-zero, look for a set (1) bit else look for a cleared (0) bit
  672. *
  673. * Description:
  674. * Starting at @offset, walk the bitmap from left to right until either the
  675. * desired bit is found or we reach the end. Return the bit offset, -1 if
  676. * not found, or -2 if error.
  677. */
  678. int netlbl_bitmap_walk(const unsigned char *bitmap, u32 bitmap_len,
  679. u32 offset, u8 state)
  680. {
  681. u32 bit_spot;
  682. u32 byte_offset;
  683. unsigned char bitmask;
  684. unsigned char byte;
  685. byte_offset = offset / 8;
  686. byte = bitmap[byte_offset];
  687. bit_spot = offset;
  688. bitmask = 0x80 >> (offset % 8);
  689. while (bit_spot < bitmap_len) {
  690. if ((state && (byte & bitmask) == bitmask) ||
  691. (state == 0 && (byte & bitmask) == 0))
  692. return bit_spot;
  693. bit_spot++;
  694. bitmask >>= 1;
  695. if (bitmask == 0) {
  696. byte = bitmap[++byte_offset];
  697. bitmask = 0x80;
  698. }
  699. }
  700. return -1;
  701. }
  702. EXPORT_SYMBOL(netlbl_bitmap_walk);
  703. /**
  704. * netlbl_bitmap_setbit - Sets a single bit in a bitmap
  705. * @bitmap: the bitmap
  706. * @bit: the bit
  707. * @state: if non-zero, set the bit (1) else clear the bit (0)
  708. *
  709. * Description:
  710. * Set a single bit in the bitmask. Returns zero on success, negative values
  711. * on error.
  712. */
  713. void netlbl_bitmap_setbit(unsigned char *bitmap, u32 bit, u8 state)
  714. {
  715. u32 byte_spot;
  716. u8 bitmask;
  717. /* gcc always rounds to zero when doing integer division */
  718. byte_spot = bit / 8;
  719. bitmask = 0x80 >> (bit % 8);
  720. if (state)
  721. bitmap[byte_spot] |= bitmask;
  722. else
  723. bitmap[byte_spot] &= ~bitmask;
  724. }
  725. EXPORT_SYMBOL(netlbl_bitmap_setbit);
  726. /*
  727. * LSM Functions
  728. */
  729. /**
  730. * netlbl_enabled - Determine if the NetLabel subsystem is enabled
  731. *
  732. * Description:
  733. * The LSM can use this function to determine if it should use NetLabel
  734. * security attributes in it's enforcement mechanism. Currently, NetLabel is
  735. * considered to be enabled when it's configuration contains a valid setup for
  736. * at least one labeled protocol (i.e. NetLabel can understand incoming
  737. * labeled packets of at least one type); otherwise NetLabel is considered to
  738. * be disabled.
  739. *
  740. */
  741. int netlbl_enabled(void)
  742. {
  743. /* At some point we probably want to expose this mechanism to the user
  744. * as well so that admins can toggle NetLabel regardless of the
  745. * configuration */
  746. return (atomic_read(&netlabel_mgmt_protocount) > 0);
  747. }
  748. /**
  749. * netlbl_sock_setattr - Label a socket using the correct protocol
  750. * @sk: the socket to label
  751. * @family: protocol family
  752. * @secattr: the security attributes
  753. *
  754. * Description:
  755. * Attach the correct label to the given socket using the security attributes
  756. * specified in @secattr. This function requires exclusive access to @sk,
  757. * which means it either needs to be in the process of being created or locked.
  758. * Returns zero on success, -EDESTADDRREQ if the domain is configured to use
  759. * network address selectors (can't blindly label the socket), and negative
  760. * values on all other failures.
  761. *
  762. */
  763. int netlbl_sock_setattr(struct sock *sk,
  764. u16 family,
  765. const struct netlbl_lsm_secattr *secattr)
  766. {
  767. int ret_val;
  768. struct netlbl_dom_map *dom_entry;
  769. rcu_read_lock();
  770. dom_entry = netlbl_domhsh_getentry(secattr->domain, family);
  771. if (dom_entry == NULL) {
  772. ret_val = -ENOENT;
  773. goto socket_setattr_return;
  774. }
  775. switch (family) {
  776. case AF_INET:
  777. switch (dom_entry->def.type) {
  778. case NETLBL_NLTYPE_ADDRSELECT:
  779. ret_val = -EDESTADDRREQ;
  780. break;
  781. case NETLBL_NLTYPE_CIPSOV4:
  782. ret_val = cipso_v4_sock_setattr(sk,
  783. dom_entry->def.cipso,
  784. secattr);
  785. break;
  786. case NETLBL_NLTYPE_UNLABELED:
  787. ret_val = 0;
  788. break;
  789. default:
  790. ret_val = -ENOENT;
  791. }
  792. break;
  793. #if IS_ENABLED(CONFIG_IPV6)
  794. case AF_INET6:
  795. /* since we don't support any IPv6 labeling protocols right
  796. * now we can optimize everything away until we do */
  797. ret_val = 0;
  798. break;
  799. #endif /* IPv6 */
  800. default:
  801. ret_val = -EPROTONOSUPPORT;
  802. }
  803. socket_setattr_return:
  804. rcu_read_unlock();
  805. return ret_val;
  806. }
  807. /**
  808. * netlbl_sock_delattr - Delete all the NetLabel labels on a socket
  809. * @sk: the socket
  810. *
  811. * Description:
  812. * Remove all the NetLabel labeling from @sk. The caller is responsible for
  813. * ensuring that @sk is locked.
  814. *
  815. */
  816. void netlbl_sock_delattr(struct sock *sk)
  817. {
  818. switch (sk->sk_family) {
  819. case AF_INET:
  820. cipso_v4_sock_delattr(sk);
  821. break;
  822. }
  823. }
  824. /**
  825. * netlbl_sock_getattr - Determine the security attributes of a sock
  826. * @sk: the sock
  827. * @secattr: the security attributes
  828. *
  829. * Description:
  830. * Examines the given sock to see if any NetLabel style labeling has been
  831. * applied to the sock, if so it parses the socket label and returns the
  832. * security attributes in @secattr. Returns zero on success, negative values
  833. * on failure.
  834. *
  835. */
  836. int netlbl_sock_getattr(struct sock *sk,
  837. struct netlbl_lsm_secattr *secattr)
  838. {
  839. int ret_val;
  840. switch (sk->sk_family) {
  841. case AF_INET:
  842. ret_val = cipso_v4_sock_getattr(sk, secattr);
  843. break;
  844. #if IS_ENABLED(CONFIG_IPV6)
  845. case AF_INET6:
  846. ret_val = -ENOMSG;
  847. break;
  848. #endif /* IPv6 */
  849. default:
  850. ret_val = -EPROTONOSUPPORT;
  851. }
  852. return ret_val;
  853. }
  854. /**
  855. * netlbl_conn_setattr - Label a connected socket using the correct protocol
  856. * @sk: the socket to label
  857. * @addr: the destination address
  858. * @secattr: the security attributes
  859. *
  860. * Description:
  861. * Attach the correct label to the given connected socket using the security
  862. * attributes specified in @secattr. The caller is responsible for ensuring
  863. * that @sk is locked. Returns zero on success, negative values on failure.
  864. *
  865. */
  866. int netlbl_conn_setattr(struct sock *sk,
  867. struct sockaddr *addr,
  868. const struct netlbl_lsm_secattr *secattr)
  869. {
  870. int ret_val;
  871. struct sockaddr_in *addr4;
  872. struct netlbl_dommap_def *entry;
  873. rcu_read_lock();
  874. switch (addr->sa_family) {
  875. case AF_INET:
  876. addr4 = (struct sockaddr_in *)addr;
  877. entry = netlbl_domhsh_getentry_af4(secattr->domain,
  878. addr4->sin_addr.s_addr);
  879. if (entry == NULL) {
  880. ret_val = -ENOENT;
  881. goto conn_setattr_return;
  882. }
  883. switch (entry->type) {
  884. case NETLBL_NLTYPE_CIPSOV4:
  885. ret_val = cipso_v4_sock_setattr(sk,
  886. entry->cipso, secattr);
  887. break;
  888. case NETLBL_NLTYPE_UNLABELED:
  889. /* just delete the protocols we support for right now
  890. * but we could remove other protocols if needed */
  891. cipso_v4_sock_delattr(sk);
  892. ret_val = 0;
  893. break;
  894. default:
  895. ret_val = -ENOENT;
  896. }
  897. break;
  898. #if IS_ENABLED(CONFIG_IPV6)
  899. case AF_INET6:
  900. /* since we don't support any IPv6 labeling protocols right
  901. * now we can optimize everything away until we do */
  902. ret_val = 0;
  903. break;
  904. #endif /* IPv6 */
  905. default:
  906. ret_val = -EPROTONOSUPPORT;
  907. }
  908. conn_setattr_return:
  909. rcu_read_unlock();
  910. return ret_val;
  911. }
  912. /**
  913. * netlbl_req_setattr - Label a request socket using the correct protocol
  914. * @req: the request socket to label
  915. * @secattr: the security attributes
  916. *
  917. * Description:
  918. * Attach the correct label to the given socket using the security attributes
  919. * specified in @secattr. Returns zero on success, negative values on failure.
  920. *
  921. */
  922. int netlbl_req_setattr(struct request_sock *req,
  923. const struct netlbl_lsm_secattr *secattr)
  924. {
  925. int ret_val;
  926. struct netlbl_dommap_def *entry;
  927. rcu_read_lock();
  928. switch (req->rsk_ops->family) {
  929. case AF_INET:
  930. entry = netlbl_domhsh_getentry_af4(secattr->domain,
  931. inet_rsk(req)->ir_rmt_addr);
  932. if (entry == NULL) {
  933. ret_val = -ENOENT;
  934. goto req_setattr_return;
  935. }
  936. switch (entry->type) {
  937. case NETLBL_NLTYPE_CIPSOV4:
  938. ret_val = cipso_v4_req_setattr(req,
  939. entry->cipso, secattr);
  940. break;
  941. case NETLBL_NLTYPE_UNLABELED:
  942. /* just delete the protocols we support for right now
  943. * but we could remove other protocols if needed */
  944. cipso_v4_req_delattr(req);
  945. ret_val = 0;
  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. req_setattr_return:
  962. rcu_read_unlock();
  963. return ret_val;
  964. }
  965. /**
  966. * netlbl_req_delattr - Delete all the NetLabel labels on a socket
  967. * @req: the socket
  968. *
  969. * Description:
  970. * Remove all the NetLabel labeling from @req.
  971. *
  972. */
  973. void netlbl_req_delattr(struct request_sock *req)
  974. {
  975. switch (req->rsk_ops->family) {
  976. case AF_INET:
  977. cipso_v4_req_delattr(req);
  978. break;
  979. }
  980. }
  981. /**
  982. * netlbl_skbuff_setattr - Label a packet using the correct protocol
  983. * @skb: the packet
  984. * @family: protocol family
  985. * @secattr: the security attributes
  986. *
  987. * Description:
  988. * Attach the correct label to the given packet using the security attributes
  989. * specified in @secattr. Returns zero on success, negative values on failure.
  990. *
  991. */
  992. int netlbl_skbuff_setattr(struct sk_buff *skb,
  993. u16 family,
  994. const struct netlbl_lsm_secattr *secattr)
  995. {
  996. int ret_val;
  997. struct iphdr *hdr4;
  998. struct netlbl_dommap_def *entry;
  999. rcu_read_lock();
  1000. switch (family) {
  1001. case AF_INET:
  1002. hdr4 = ip_hdr(skb);
  1003. entry = netlbl_domhsh_getentry_af4(secattr->domain,hdr4->daddr);
  1004. if (entry == NULL) {
  1005. ret_val = -ENOENT;
  1006. goto skbuff_setattr_return;
  1007. }
  1008. switch (entry->type) {
  1009. case NETLBL_NLTYPE_CIPSOV4:
  1010. ret_val = cipso_v4_skbuff_setattr(skb, entry->cipso,
  1011. secattr);
  1012. break;
  1013. case NETLBL_NLTYPE_UNLABELED:
  1014. /* just delete the protocols we support for right now
  1015. * but we could remove other protocols if needed */
  1016. ret_val = cipso_v4_skbuff_delattr(skb);
  1017. break;
  1018. default:
  1019. ret_val = -ENOENT;
  1020. }
  1021. break;
  1022. #if IS_ENABLED(CONFIG_IPV6)
  1023. case AF_INET6:
  1024. /* since we don't support any IPv6 labeling protocols right
  1025. * now we can optimize everything away until we do */
  1026. ret_val = 0;
  1027. break;
  1028. #endif /* IPv6 */
  1029. default:
  1030. ret_val = -EPROTONOSUPPORT;
  1031. }
  1032. skbuff_setattr_return:
  1033. rcu_read_unlock();
  1034. return ret_val;
  1035. }
  1036. /**
  1037. * netlbl_skbuff_getattr - Determine the security attributes of a packet
  1038. * @skb: the packet
  1039. * @family: protocol family
  1040. * @secattr: the security attributes
  1041. *
  1042. * Description:
  1043. * Examines the given packet to see if a recognized form of packet labeling
  1044. * is present, if so it parses the packet label and returns the security
  1045. * attributes in @secattr. Returns zero on success, negative values on
  1046. * failure.
  1047. *
  1048. */
  1049. int netlbl_skbuff_getattr(const struct sk_buff *skb,
  1050. u16 family,
  1051. struct netlbl_lsm_secattr *secattr)
  1052. {
  1053. unsigned char *ptr;
  1054. switch (family) {
  1055. case AF_INET:
  1056. ptr = cipso_v4_optptr(skb);
  1057. if (ptr && cipso_v4_getattr(ptr, secattr) == 0)
  1058. return 0;
  1059. break;
  1060. #if IS_ENABLED(CONFIG_IPV6)
  1061. case AF_INET6:
  1062. break;
  1063. #endif /* IPv6 */
  1064. }
  1065. return netlbl_unlabel_getattr(skb, family, secattr);
  1066. }
  1067. /**
  1068. * netlbl_skbuff_err - Handle a LSM error on a sk_buff
  1069. * @skb: the packet
  1070. * @error: the error code
  1071. * @gateway: true if host is acting as a gateway, false otherwise
  1072. *
  1073. * Description:
  1074. * Deal with a LSM problem when handling the packet in @skb, typically this is
  1075. * a permission denied problem (-EACCES). The correct action is determined
  1076. * according to the packet's labeling protocol.
  1077. *
  1078. */
  1079. void netlbl_skbuff_err(struct sk_buff *skb, int error, int gateway)
  1080. {
  1081. if (cipso_v4_optptr(skb))
  1082. cipso_v4_error(skb, error, gateway);
  1083. }
  1084. /**
  1085. * netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches
  1086. *
  1087. * Description:
  1088. * For all of the NetLabel protocols that support some form of label mapping
  1089. * cache, invalidate the cache. Returns zero on success, negative values on
  1090. * error.
  1091. *
  1092. */
  1093. void netlbl_cache_invalidate(void)
  1094. {
  1095. cipso_v4_cache_invalidate();
  1096. }
  1097. /**
  1098. * netlbl_cache_add - Add an entry to a NetLabel protocol cache
  1099. * @skb: the packet
  1100. * @secattr: the packet's security attributes
  1101. *
  1102. * Description:
  1103. * Add the LSM security attributes for the given packet to the underlying
  1104. * NetLabel protocol's label mapping cache. Returns zero on success, negative
  1105. * values on error.
  1106. *
  1107. */
  1108. int netlbl_cache_add(const struct sk_buff *skb,
  1109. const struct netlbl_lsm_secattr *secattr)
  1110. {
  1111. unsigned char *ptr;
  1112. if ((secattr->flags & NETLBL_SECATTR_CACHE) == 0)
  1113. return -ENOMSG;
  1114. ptr = cipso_v4_optptr(skb);
  1115. if (ptr)
  1116. return cipso_v4_cache_add(ptr, secattr);
  1117. return -ENOMSG;
  1118. }
  1119. /*
  1120. * Protocol Engine Functions
  1121. */
  1122. /**
  1123. * netlbl_audit_start - Start an audit message
  1124. * @type: audit message type
  1125. * @audit_info: NetLabel audit information
  1126. *
  1127. * Description:
  1128. * Start an audit message using the type specified in @type and fill the audit
  1129. * message with some fields common to all NetLabel audit messages. This
  1130. * function should only be used by protocol engines, not LSMs. Returns a
  1131. * pointer to the audit buffer on success, NULL on failure.
  1132. *
  1133. */
  1134. struct audit_buffer *netlbl_audit_start(int type,
  1135. struct netlbl_audit *audit_info)
  1136. {
  1137. return netlbl_audit_start_common(type, audit_info);
  1138. }
  1139. EXPORT_SYMBOL(netlbl_audit_start);
  1140. /*
  1141. * Setup Functions
  1142. */
  1143. /**
  1144. * netlbl_init - Initialize NetLabel
  1145. *
  1146. * Description:
  1147. * Perform the required NetLabel initialization before first use.
  1148. *
  1149. */
  1150. static int __init netlbl_init(void)
  1151. {
  1152. int ret_val;
  1153. printk(KERN_INFO "NetLabel: Initializing\n");
  1154. printk(KERN_INFO "NetLabel: domain hash size = %u\n",
  1155. (1 << NETLBL_DOMHSH_BITSIZE));
  1156. printk(KERN_INFO "NetLabel: protocols ="
  1157. " UNLABELED"
  1158. " CIPSOv4"
  1159. "\n");
  1160. ret_val = netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE);
  1161. if (ret_val != 0)
  1162. goto init_failure;
  1163. ret_val = netlbl_unlabel_init(NETLBL_UNLHSH_BITSIZE);
  1164. if (ret_val != 0)
  1165. goto init_failure;
  1166. ret_val = netlbl_netlink_init();
  1167. if (ret_val != 0)
  1168. goto init_failure;
  1169. ret_val = netlbl_unlabel_defconf();
  1170. if (ret_val != 0)
  1171. goto init_failure;
  1172. printk(KERN_INFO "NetLabel: unlabeled traffic allowed by default\n");
  1173. return 0;
  1174. init_failure:
  1175. panic("NetLabel: failed to initialize properly (%d)\n", ret_val);
  1176. }
  1177. subsys_initcall(netlbl_init);