netlabel_kapi.c 28 KB

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