netlabel_kapi.c 28 KB

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