netlabel_kapi.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  1. /*
  2. * NetLabel Kernel API
  3. *
  4. * This file defines the kernel API for the NetLabel system. The NetLabel
  5. * system manages static and dynamic label mappings for network protocols such
  6. * as CIPSO and RIPSO.
  7. *
  8. * Author: Paul Moore <paul@paul-moore.com>
  9. *
  10. */
  11. /*
  12. * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  22. * the GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. #include <linux/init.h>
  29. #include <linux/types.h>
  30. #include <linux/slab.h>
  31. #include <linux/audit.h>
  32. #include <linux/in.h>
  33. #include <linux/in6.h>
  34. #include <net/ip.h>
  35. #include <net/ipv6.h>
  36. #include <net/netlabel.h>
  37. #include <net/cipso_ipv4.h>
  38. #include <asm/bug.h>
  39. #include <linux/atomic.h>
  40. #include "netlabel_domainhash.h"
  41. #include "netlabel_unlabeled.h"
  42. #include "netlabel_cipso_v4.h"
  43. #include "netlabel_user.h"
  44. #include "netlabel_mgmt.h"
  45. #include "netlabel_addrlist.h"
  46. /*
  47. * Configuration Functions
  48. */
  49. /**
  50. * netlbl_cfg_map_del - Remove a NetLabel/LSM domain mapping
  51. * @domain: the domain mapping to remove
  52. * @family: address family
  53. * @addr: IP address
  54. * @mask: IP address mask
  55. * @audit_info: NetLabel audit information
  56. *
  57. * Description:
  58. * Removes a NetLabel/LSM domain mapping. A @domain value of NULL causes the
  59. * default domain mapping to be removed. Returns zero on success, negative
  60. * values on failure.
  61. *
  62. */
  63. int netlbl_cfg_map_del(const char *domain,
  64. u16 family,
  65. const void *addr,
  66. const void *mask,
  67. struct netlbl_audit *audit_info)
  68. {
  69. if (addr == NULL && mask == NULL) {
  70. return netlbl_domhsh_remove(domain, audit_info);
  71. } else if (addr != NULL && mask != NULL) {
  72. switch (family) {
  73. case AF_INET:
  74. return netlbl_domhsh_remove_af4(domain, addr, mask,
  75. audit_info);
  76. default:
  77. return -EPFNOSUPPORT;
  78. }
  79. } else
  80. return -EINVAL;
  81. }
  82. /**
  83. * netlbl_cfg_unlbl_map_add - Add a new unlabeled mapping
  84. * @domain: the domain mapping to add
  85. * @family: address family
  86. * @addr: IP address
  87. * @mask: IP address mask
  88. * @audit_info: NetLabel audit information
  89. *
  90. * Description:
  91. * Adds a new unlabeled NetLabel/LSM domain mapping. A @domain value of NULL
  92. * causes a new default domain mapping to be added. Returns zero on success,
  93. * negative values on failure.
  94. *
  95. */
  96. int netlbl_cfg_unlbl_map_add(const char *domain,
  97. u16 family,
  98. const void *addr,
  99. const void *mask,
  100. struct netlbl_audit *audit_info)
  101. {
  102. int ret_val = -ENOMEM;
  103. struct netlbl_dom_map *entry;
  104. struct netlbl_domaddr_map *addrmap = NULL;
  105. struct netlbl_domaddr4_map *map4 = NULL;
  106. struct netlbl_domaddr6_map *map6 = NULL;
  107. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  108. if (entry == NULL)
  109. return -ENOMEM;
  110. if (domain != NULL) {
  111. entry->domain = kstrdup(domain, GFP_ATOMIC);
  112. if (entry->domain == NULL)
  113. goto cfg_unlbl_map_add_failure;
  114. }
  115. if (addr == NULL && mask == NULL)
  116. entry->def.type = NETLBL_NLTYPE_UNLABELED;
  117. else if (addr != NULL && mask != NULL) {
  118. addrmap = kzalloc(sizeof(*addrmap), GFP_ATOMIC);
  119. if (addrmap == NULL)
  120. goto cfg_unlbl_map_add_failure;
  121. INIT_LIST_HEAD(&addrmap->list4);
  122. INIT_LIST_HEAD(&addrmap->list6);
  123. switch (family) {
  124. case AF_INET: {
  125. const struct in_addr *addr4 = addr;
  126. const struct in_addr *mask4 = mask;
  127. map4 = kzalloc(sizeof(*map4), GFP_ATOMIC);
  128. if (map4 == NULL)
  129. goto cfg_unlbl_map_add_failure;
  130. map4->def.type = NETLBL_NLTYPE_UNLABELED;
  131. map4->list.addr = addr4->s_addr & mask4->s_addr;
  132. map4->list.mask = mask4->s_addr;
  133. map4->list.valid = 1;
  134. ret_val = netlbl_af4list_add(&map4->list,
  135. &addrmap->list4);
  136. if (ret_val != 0)
  137. goto cfg_unlbl_map_add_failure;
  138. break;
  139. }
  140. #if IS_ENABLED(CONFIG_IPV6)
  141. case AF_INET6: {
  142. const struct in6_addr *addr6 = addr;
  143. const struct in6_addr *mask6 = mask;
  144. map6 = kzalloc(sizeof(*map6), GFP_ATOMIC);
  145. if (map6 == NULL)
  146. goto cfg_unlbl_map_add_failure;
  147. map6->def.type = NETLBL_NLTYPE_UNLABELED;
  148. map6->list.addr = *addr6;
  149. map6->list.addr.s6_addr32[0] &= mask6->s6_addr32[0];
  150. map6->list.addr.s6_addr32[1] &= mask6->s6_addr32[1];
  151. map6->list.addr.s6_addr32[2] &= mask6->s6_addr32[2];
  152. map6->list.addr.s6_addr32[3] &= mask6->s6_addr32[3];
  153. map6->list.mask = *mask6;
  154. map6->list.valid = 1;
  155. ret_val = netlbl_af6list_add(&map6->list,
  156. &addrmap->list6);
  157. if (ret_val != 0)
  158. goto cfg_unlbl_map_add_failure;
  159. break;
  160. }
  161. #endif /* IPv6 */
  162. default:
  163. goto cfg_unlbl_map_add_failure;
  164. break;
  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. * @secid: LSM secid value for the entry
  233. * @audit_info: NetLabel audit information
  234. *
  235. * Description:
  236. * Removes an existing NetLabel static label used when protocol provided labels
  237. * are not present on incoming traffic. If @dev_name is NULL then the default
  238. * interface will be used. Returns zero on success, negative values on failure.
  239. *
  240. */
  241. int netlbl_cfg_unlbl_static_del(struct net *net,
  242. const char *dev_name,
  243. const void *addr,
  244. const void *mask,
  245. u16 family,
  246. struct netlbl_audit *audit_info)
  247. {
  248. u32 addr_len;
  249. switch (family) {
  250. case AF_INET:
  251. addr_len = sizeof(struct in_addr);
  252. break;
  253. #if IS_ENABLED(CONFIG_IPV6)
  254. case AF_INET6:
  255. addr_len = sizeof(struct in6_addr);
  256. break;
  257. #endif /* IPv6 */
  258. default:
  259. return -EPFNOSUPPORT;
  260. }
  261. return netlbl_unlhsh_remove(net,
  262. dev_name, addr, mask, addr_len,
  263. audit_info);
  264. }
  265. /**
  266. * netlbl_cfg_cipsov4_add - Add a new CIPSOv4 DOI definition
  267. * @doi_def: CIPSO DOI definition
  268. * @audit_info: NetLabel audit information
  269. *
  270. * Description:
  271. * Add a new CIPSO DOI definition as defined by @doi_def. Returns zero on
  272. * success and negative values on failure.
  273. *
  274. */
  275. int netlbl_cfg_cipsov4_add(struct cipso_v4_doi *doi_def,
  276. struct netlbl_audit *audit_info)
  277. {
  278. return cipso_v4_doi_add(doi_def, audit_info);
  279. }
  280. /**
  281. * netlbl_cfg_cipsov4_del - Remove an existing CIPSOv4 DOI definition
  282. * @doi: CIPSO DOI
  283. * @audit_info: NetLabel audit information
  284. *
  285. * Description:
  286. * Remove an existing CIPSO DOI definition matching @doi. Returns zero on
  287. * success and negative values on failure.
  288. *
  289. */
  290. void netlbl_cfg_cipsov4_del(u32 doi, struct netlbl_audit *audit_info)
  291. {
  292. cipso_v4_doi_remove(doi, audit_info);
  293. }
  294. /**
  295. * netlbl_cfg_cipsov4_map_add - Add a new CIPSOv4 DOI mapping
  296. * @doi: the CIPSO DOI
  297. * @domain: the domain mapping to add
  298. * @addr: IP address
  299. * @mask: IP address mask
  300. * @audit_info: NetLabel audit information
  301. *
  302. * Description:
  303. * Add a new NetLabel/LSM domain mapping for the given CIPSO DOI to the NetLabel
  304. * subsystem. A @domain value of NULL adds a new default domain mapping.
  305. * Returns zero on success, negative values on failure.
  306. *
  307. */
  308. int netlbl_cfg_cipsov4_map_add(u32 doi,
  309. const char *domain,
  310. const struct in_addr *addr,
  311. const struct in_addr *mask,
  312. struct netlbl_audit *audit_info)
  313. {
  314. int ret_val = -ENOMEM;
  315. struct cipso_v4_doi *doi_def;
  316. struct netlbl_dom_map *entry;
  317. struct netlbl_domaddr_map *addrmap = NULL;
  318. struct netlbl_domaddr4_map *addrinfo = NULL;
  319. doi_def = cipso_v4_doi_getdef(doi);
  320. if (doi_def == NULL)
  321. return -ENOENT;
  322. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  323. if (entry == NULL)
  324. goto out_entry;
  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_NONE, 0);
  563. if (iter == NULL) {
  564. *offset = (u32)-1;
  565. return 0;
  566. }
  567. if (off < iter->startbit) {
  568. off = iter->startbit;
  569. *offset = off;
  570. } else
  571. off -= iter->startbit;
  572. idx = off / NETLBL_CATMAP_MAPSIZE;
  573. *bitmap = iter->bitmap[idx] >> (off % NETLBL_CATMAP_SIZE);
  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. /*
  665. * LSM Functions
  666. */
  667. /**
  668. * netlbl_enabled - Determine if the NetLabel subsystem is enabled
  669. *
  670. * Description:
  671. * The LSM can use this function to determine if it should use NetLabel
  672. * security attributes in it's enforcement mechanism. Currently, NetLabel is
  673. * considered to be enabled when it's configuration contains a valid setup for
  674. * at least one labeled protocol (i.e. NetLabel can understand incoming
  675. * labeled packets of at least one type); otherwise NetLabel is considered to
  676. * be disabled.
  677. *
  678. */
  679. int netlbl_enabled(void)
  680. {
  681. /* At some point we probably want to expose this mechanism to the user
  682. * as well so that admins can toggle NetLabel regardless of the
  683. * configuration */
  684. return (atomic_read(&netlabel_mgmt_protocount) > 0);
  685. }
  686. /**
  687. * netlbl_sock_setattr - Label a socket using the correct protocol
  688. * @sk: the socket to label
  689. * @family: protocol family
  690. * @secattr: the security attributes
  691. *
  692. * Description:
  693. * Attach the correct label to the given socket using the security attributes
  694. * specified in @secattr. This function requires exclusive access to @sk,
  695. * which means it either needs to be in the process of being created or locked.
  696. * Returns zero on success, -EDESTADDRREQ if the domain is configured to use
  697. * network address selectors (can't blindly label the socket), and negative
  698. * values on all other failures.
  699. *
  700. */
  701. int netlbl_sock_setattr(struct sock *sk,
  702. u16 family,
  703. const struct netlbl_lsm_secattr *secattr)
  704. {
  705. int ret_val;
  706. struct netlbl_dom_map *dom_entry;
  707. rcu_read_lock();
  708. dom_entry = netlbl_domhsh_getentry(secattr->domain);
  709. if (dom_entry == NULL) {
  710. ret_val = -ENOENT;
  711. goto socket_setattr_return;
  712. }
  713. switch (family) {
  714. case AF_INET:
  715. switch (dom_entry->def.type) {
  716. case NETLBL_NLTYPE_ADDRSELECT:
  717. ret_val = -EDESTADDRREQ;
  718. break;
  719. case NETLBL_NLTYPE_CIPSOV4:
  720. ret_val = cipso_v4_sock_setattr(sk,
  721. dom_entry->def.cipso,
  722. secattr);
  723. break;
  724. case NETLBL_NLTYPE_UNLABELED:
  725. ret_val = 0;
  726. break;
  727. default:
  728. ret_val = -ENOENT;
  729. }
  730. break;
  731. #if IS_ENABLED(CONFIG_IPV6)
  732. case AF_INET6:
  733. /* since we don't support any IPv6 labeling protocols right
  734. * now we can optimize everything away until we do */
  735. ret_val = 0;
  736. break;
  737. #endif /* IPv6 */
  738. default:
  739. ret_val = -EPROTONOSUPPORT;
  740. }
  741. socket_setattr_return:
  742. rcu_read_unlock();
  743. return ret_val;
  744. }
  745. /**
  746. * netlbl_sock_delattr - Delete all the NetLabel labels on a socket
  747. * @sk: the socket
  748. *
  749. * Description:
  750. * Remove all the NetLabel labeling from @sk. The caller is responsible for
  751. * ensuring that @sk is locked.
  752. *
  753. */
  754. void netlbl_sock_delattr(struct sock *sk)
  755. {
  756. cipso_v4_sock_delattr(sk);
  757. }
  758. /**
  759. * netlbl_sock_getattr - Determine the security attributes of a sock
  760. * @sk: the sock
  761. * @secattr: the security attributes
  762. *
  763. * Description:
  764. * Examines the given sock to see if any NetLabel style labeling has been
  765. * applied to the sock, if so it parses the socket label and returns the
  766. * security attributes in @secattr. Returns zero on success, negative values
  767. * on failure.
  768. *
  769. */
  770. int netlbl_sock_getattr(struct sock *sk,
  771. struct netlbl_lsm_secattr *secattr)
  772. {
  773. int ret_val;
  774. switch (sk->sk_family) {
  775. case AF_INET:
  776. ret_val = cipso_v4_sock_getattr(sk, secattr);
  777. break;
  778. #if IS_ENABLED(CONFIG_IPV6)
  779. case AF_INET6:
  780. ret_val = -ENOMSG;
  781. break;
  782. #endif /* IPv6 */
  783. default:
  784. ret_val = -EPROTONOSUPPORT;
  785. }
  786. return ret_val;
  787. }
  788. /**
  789. * netlbl_conn_setattr - Label a connected socket using the correct protocol
  790. * @sk: the socket to label
  791. * @addr: the destination address
  792. * @secattr: the security attributes
  793. *
  794. * Description:
  795. * Attach the correct label to the given connected socket using the security
  796. * attributes specified in @secattr. The caller is responsible for ensuring
  797. * that @sk is locked. Returns zero on success, negative values on failure.
  798. *
  799. */
  800. int netlbl_conn_setattr(struct sock *sk,
  801. struct sockaddr *addr,
  802. const struct netlbl_lsm_secattr *secattr)
  803. {
  804. int ret_val;
  805. struct sockaddr_in *addr4;
  806. struct netlbl_dommap_def *entry;
  807. rcu_read_lock();
  808. switch (addr->sa_family) {
  809. case AF_INET:
  810. addr4 = (struct sockaddr_in *)addr;
  811. entry = netlbl_domhsh_getentry_af4(secattr->domain,
  812. addr4->sin_addr.s_addr);
  813. if (entry == NULL) {
  814. ret_val = -ENOENT;
  815. goto conn_setattr_return;
  816. }
  817. switch (entry->type) {
  818. case NETLBL_NLTYPE_CIPSOV4:
  819. ret_val = cipso_v4_sock_setattr(sk,
  820. entry->cipso, secattr);
  821. break;
  822. case NETLBL_NLTYPE_UNLABELED:
  823. /* just delete the protocols we support for right now
  824. * but we could remove other protocols if needed */
  825. cipso_v4_sock_delattr(sk);
  826. ret_val = 0;
  827. break;
  828. default:
  829. ret_val = -ENOENT;
  830. }
  831. break;
  832. #if IS_ENABLED(CONFIG_IPV6)
  833. case AF_INET6:
  834. /* since we don't support any IPv6 labeling protocols right
  835. * now we can optimize everything away until we do */
  836. ret_val = 0;
  837. break;
  838. #endif /* IPv6 */
  839. default:
  840. ret_val = -EPROTONOSUPPORT;
  841. }
  842. conn_setattr_return:
  843. rcu_read_unlock();
  844. return ret_val;
  845. }
  846. /**
  847. * netlbl_req_setattr - Label a request socket using the correct protocol
  848. * @req: the request socket to label
  849. * @secattr: the security attributes
  850. *
  851. * Description:
  852. * Attach the correct label to the given socket using the security attributes
  853. * specified in @secattr. Returns zero on success, negative values on failure.
  854. *
  855. */
  856. int netlbl_req_setattr(struct request_sock *req,
  857. const struct netlbl_lsm_secattr *secattr)
  858. {
  859. int ret_val;
  860. struct netlbl_dommap_def *entry;
  861. rcu_read_lock();
  862. switch (req->rsk_ops->family) {
  863. case AF_INET:
  864. entry = netlbl_domhsh_getentry_af4(secattr->domain,
  865. inet_rsk(req)->ir_rmt_addr);
  866. if (entry == NULL) {
  867. ret_val = -ENOENT;
  868. goto req_setattr_return;
  869. }
  870. switch (entry->type) {
  871. case NETLBL_NLTYPE_CIPSOV4:
  872. ret_val = cipso_v4_req_setattr(req,
  873. entry->cipso, secattr);
  874. break;
  875. case NETLBL_NLTYPE_UNLABELED:
  876. /* just delete the protocols we support for right now
  877. * but we could remove other protocols if needed */
  878. cipso_v4_req_delattr(req);
  879. ret_val = 0;
  880. break;
  881. default:
  882. ret_val = -ENOENT;
  883. }
  884. break;
  885. #if IS_ENABLED(CONFIG_IPV6)
  886. case AF_INET6:
  887. /* since we don't support any IPv6 labeling protocols right
  888. * now we can optimize everything away until we do */
  889. ret_val = 0;
  890. break;
  891. #endif /* IPv6 */
  892. default:
  893. ret_val = -EPROTONOSUPPORT;
  894. }
  895. req_setattr_return:
  896. rcu_read_unlock();
  897. return ret_val;
  898. }
  899. /**
  900. * netlbl_req_delattr - Delete all the NetLabel labels on a socket
  901. * @req: the socket
  902. *
  903. * Description:
  904. * Remove all the NetLabel labeling from @req.
  905. *
  906. */
  907. void netlbl_req_delattr(struct request_sock *req)
  908. {
  909. cipso_v4_req_delattr(req);
  910. }
  911. /**
  912. * netlbl_skbuff_setattr - Label a packet using the correct protocol
  913. * @skb: the packet
  914. * @family: protocol family
  915. * @secattr: the security attributes
  916. *
  917. * Description:
  918. * Attach the correct label to the given packet using the security attributes
  919. * specified in @secattr. Returns zero on success, negative values on failure.
  920. *
  921. */
  922. int netlbl_skbuff_setattr(struct sk_buff *skb,
  923. u16 family,
  924. const struct netlbl_lsm_secattr *secattr)
  925. {
  926. int ret_val;
  927. struct iphdr *hdr4;
  928. struct netlbl_dommap_def *entry;
  929. rcu_read_lock();
  930. switch (family) {
  931. case AF_INET:
  932. hdr4 = ip_hdr(skb);
  933. entry = netlbl_domhsh_getentry_af4(secattr->domain,hdr4->daddr);
  934. if (entry == NULL) {
  935. ret_val = -ENOENT;
  936. goto skbuff_setattr_return;
  937. }
  938. switch (entry->type) {
  939. case NETLBL_NLTYPE_CIPSOV4:
  940. ret_val = cipso_v4_skbuff_setattr(skb, entry->cipso,
  941. secattr);
  942. break;
  943. case NETLBL_NLTYPE_UNLABELED:
  944. /* just delete the protocols we support for right now
  945. * but we could remove other protocols if needed */
  946. ret_val = cipso_v4_skbuff_delattr(skb);
  947. break;
  948. default:
  949. ret_val = -ENOENT;
  950. }
  951. break;
  952. #if IS_ENABLED(CONFIG_IPV6)
  953. case AF_INET6:
  954. /* since we don't support any IPv6 labeling protocols right
  955. * now we can optimize everything away until we do */
  956. ret_val = 0;
  957. break;
  958. #endif /* IPv6 */
  959. default:
  960. ret_val = -EPROTONOSUPPORT;
  961. }
  962. skbuff_setattr_return:
  963. rcu_read_unlock();
  964. return ret_val;
  965. }
  966. /**
  967. * netlbl_skbuff_getattr - Determine the security attributes of a packet
  968. * @skb: the packet
  969. * @family: protocol family
  970. * @secattr: the security attributes
  971. *
  972. * Description:
  973. * Examines the given packet to see if a recognized form of packet labeling
  974. * is present, if so it parses the packet label and returns the security
  975. * attributes in @secattr. Returns zero on success, negative values on
  976. * failure.
  977. *
  978. */
  979. int netlbl_skbuff_getattr(const struct sk_buff *skb,
  980. u16 family,
  981. struct netlbl_lsm_secattr *secattr)
  982. {
  983. switch (family) {
  984. case AF_INET:
  985. if (CIPSO_V4_OPTEXIST(skb) &&
  986. cipso_v4_skbuff_getattr(skb, secattr) == 0)
  987. return 0;
  988. break;
  989. #if IS_ENABLED(CONFIG_IPV6)
  990. case AF_INET6:
  991. break;
  992. #endif /* IPv6 */
  993. }
  994. return netlbl_unlabel_getattr(skb, family, secattr);
  995. }
  996. /**
  997. * netlbl_skbuff_err - Handle a LSM error on a sk_buff
  998. * @skb: the packet
  999. * @error: the error code
  1000. * @gateway: true if host is acting as a gateway, false otherwise
  1001. *
  1002. * Description:
  1003. * Deal with a LSM problem when handling the packet in @skb, typically this is
  1004. * a permission denied problem (-EACCES). The correct action is determined
  1005. * according to the packet's labeling protocol.
  1006. *
  1007. */
  1008. void netlbl_skbuff_err(struct sk_buff *skb, int error, int gateway)
  1009. {
  1010. if (CIPSO_V4_OPTEXIST(skb))
  1011. cipso_v4_error(skb, error, gateway);
  1012. }
  1013. /**
  1014. * netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches
  1015. *
  1016. * Description:
  1017. * For all of the NetLabel protocols that support some form of label mapping
  1018. * cache, invalidate the cache. Returns zero on success, negative values on
  1019. * error.
  1020. *
  1021. */
  1022. void netlbl_cache_invalidate(void)
  1023. {
  1024. cipso_v4_cache_invalidate();
  1025. }
  1026. /**
  1027. * netlbl_cache_add - Add an entry to a NetLabel protocol cache
  1028. * @skb: the packet
  1029. * @secattr: the packet's security attributes
  1030. *
  1031. * Description:
  1032. * Add the LSM security attributes for the given packet to the underlying
  1033. * NetLabel protocol's label mapping cache. Returns zero on success, negative
  1034. * values on error.
  1035. *
  1036. */
  1037. int netlbl_cache_add(const struct sk_buff *skb,
  1038. const struct netlbl_lsm_secattr *secattr)
  1039. {
  1040. if ((secattr->flags & NETLBL_SECATTR_CACHE) == 0)
  1041. return -ENOMSG;
  1042. if (CIPSO_V4_OPTEXIST(skb))
  1043. return cipso_v4_cache_add(skb, secattr);
  1044. return -ENOMSG;
  1045. }
  1046. /*
  1047. * Protocol Engine Functions
  1048. */
  1049. /**
  1050. * netlbl_audit_start - Start an audit message
  1051. * @type: audit message type
  1052. * @audit_info: NetLabel audit information
  1053. *
  1054. * Description:
  1055. * Start an audit message using the type specified in @type and fill the audit
  1056. * message with some fields common to all NetLabel audit messages. This
  1057. * function should only be used by protocol engines, not LSMs. Returns a
  1058. * pointer to the audit buffer on success, NULL on failure.
  1059. *
  1060. */
  1061. struct audit_buffer *netlbl_audit_start(int type,
  1062. struct netlbl_audit *audit_info)
  1063. {
  1064. return netlbl_audit_start_common(type, audit_info);
  1065. }
  1066. /*
  1067. * Setup Functions
  1068. */
  1069. /**
  1070. * netlbl_init - Initialize NetLabel
  1071. *
  1072. * Description:
  1073. * Perform the required NetLabel initialization before first use.
  1074. *
  1075. */
  1076. static int __init netlbl_init(void)
  1077. {
  1078. int ret_val;
  1079. printk(KERN_INFO "NetLabel: Initializing\n");
  1080. printk(KERN_INFO "NetLabel: domain hash size = %u\n",
  1081. (1 << NETLBL_DOMHSH_BITSIZE));
  1082. printk(KERN_INFO "NetLabel: protocols ="
  1083. " UNLABELED"
  1084. " CIPSOv4"
  1085. "\n");
  1086. ret_val = netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE);
  1087. if (ret_val != 0)
  1088. goto init_failure;
  1089. ret_val = netlbl_unlabel_init(NETLBL_UNLHSH_BITSIZE);
  1090. if (ret_val != 0)
  1091. goto init_failure;
  1092. ret_val = netlbl_netlink_init();
  1093. if (ret_val != 0)
  1094. goto init_failure;
  1095. ret_val = netlbl_unlabel_defconf();
  1096. if (ret_val != 0)
  1097. goto init_failure;
  1098. printk(KERN_INFO "NetLabel: unlabeled traffic allowed by default\n");
  1099. return 0;
  1100. init_failure:
  1101. panic("NetLabel: failed to initialize properly (%d)\n", ret_val);
  1102. }
  1103. subsys_initcall(netlbl_init);