netlabel_unlabeled.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559
  1. /*
  2. * NetLabel Unlabeled Support
  3. *
  4. * This file defines functions for dealing with unlabeled packets for the
  5. * NetLabel system. The NetLabel system manages static and dynamic label
  6. * mappings for network protocols such 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/types.h>
  29. #include <linux/rcupdate.h>
  30. #include <linux/list.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/socket.h>
  33. #include <linux/string.h>
  34. #include <linux/skbuff.h>
  35. #include <linux/audit.h>
  36. #include <linux/in.h>
  37. #include <linux/in6.h>
  38. #include <linux/ip.h>
  39. #include <linux/ipv6.h>
  40. #include <linux/notifier.h>
  41. #include <linux/netdevice.h>
  42. #include <linux/security.h>
  43. #include <linux/slab.h>
  44. #include <net/sock.h>
  45. #include <net/netlink.h>
  46. #include <net/genetlink.h>
  47. #include <net/ip.h>
  48. #include <net/ipv6.h>
  49. #include <net/net_namespace.h>
  50. #include <net/netlabel.h>
  51. #include <asm/bug.h>
  52. #include <linux/atomic.h>
  53. #include "netlabel_user.h"
  54. #include "netlabel_addrlist.h"
  55. #include "netlabel_domainhash.h"
  56. #include "netlabel_unlabeled.h"
  57. #include "netlabel_mgmt.h"
  58. /* NOTE: at present we always use init's network namespace since we don't
  59. * presently support different namespaces even though the majority of
  60. * the functions in this file are "namespace safe" */
  61. /* The unlabeled connection hash table which we use to map network interfaces
  62. * and addresses of unlabeled packets to a user specified secid value for the
  63. * LSM. The hash table is used to lookup the network interface entry
  64. * (struct netlbl_unlhsh_iface) and then the interface entry is used to
  65. * lookup an IP address match from an ordered list. If a network interface
  66. * match can not be found in the hash table then the default entry
  67. * (netlbl_unlhsh_def) is used. The IP address entry list
  68. * (struct netlbl_unlhsh_addr) is ordered such that the entries with a
  69. * larger netmask come first.
  70. */
  71. struct netlbl_unlhsh_tbl {
  72. struct list_head *tbl;
  73. u32 size;
  74. };
  75. #define netlbl_unlhsh_addr4_entry(iter) \
  76. container_of(iter, struct netlbl_unlhsh_addr4, list)
  77. struct netlbl_unlhsh_addr4 {
  78. u32 secid;
  79. struct netlbl_af4list list;
  80. struct rcu_head rcu;
  81. };
  82. #define netlbl_unlhsh_addr6_entry(iter) \
  83. container_of(iter, struct netlbl_unlhsh_addr6, list)
  84. struct netlbl_unlhsh_addr6 {
  85. u32 secid;
  86. struct netlbl_af6list list;
  87. struct rcu_head rcu;
  88. };
  89. struct netlbl_unlhsh_iface {
  90. int ifindex;
  91. struct list_head addr4_list;
  92. struct list_head addr6_list;
  93. u32 valid;
  94. struct list_head list;
  95. struct rcu_head rcu;
  96. };
  97. /* Argument struct for netlbl_unlhsh_walk() */
  98. struct netlbl_unlhsh_walk_arg {
  99. struct netlink_callback *nl_cb;
  100. struct sk_buff *skb;
  101. u32 seq;
  102. };
  103. /* Unlabeled connection hash table */
  104. /* updates should be so rare that having one spinlock for the entire
  105. * hash table should be okay */
  106. static DEFINE_SPINLOCK(netlbl_unlhsh_lock);
  107. #define netlbl_unlhsh_rcu_deref(p) \
  108. rcu_dereference_check(p, lockdep_is_held(&netlbl_unlhsh_lock))
  109. static struct netlbl_unlhsh_tbl *netlbl_unlhsh = NULL;
  110. static struct netlbl_unlhsh_iface *netlbl_unlhsh_def = NULL;
  111. /* Accept unlabeled packets flag */
  112. static u8 netlabel_unlabel_acceptflg = 0;
  113. /* NetLabel Generic NETLINK unlabeled family */
  114. static struct genl_family netlbl_unlabel_gnl_family = {
  115. .id = GENL_ID_GENERATE,
  116. .hdrsize = 0,
  117. .name = NETLBL_NLTYPE_UNLABELED_NAME,
  118. .version = NETLBL_PROTO_VERSION,
  119. .maxattr = NLBL_UNLABEL_A_MAX,
  120. };
  121. /* NetLabel Netlink attribute policy */
  122. static const struct nla_policy netlbl_unlabel_genl_policy[NLBL_UNLABEL_A_MAX + 1] = {
  123. [NLBL_UNLABEL_A_ACPTFLG] = { .type = NLA_U8 },
  124. [NLBL_UNLABEL_A_IPV6ADDR] = { .type = NLA_BINARY,
  125. .len = sizeof(struct in6_addr) },
  126. [NLBL_UNLABEL_A_IPV6MASK] = { .type = NLA_BINARY,
  127. .len = sizeof(struct in6_addr) },
  128. [NLBL_UNLABEL_A_IPV4ADDR] = { .type = NLA_BINARY,
  129. .len = sizeof(struct in_addr) },
  130. [NLBL_UNLABEL_A_IPV4MASK] = { .type = NLA_BINARY,
  131. .len = sizeof(struct in_addr) },
  132. [NLBL_UNLABEL_A_IFACE] = { .type = NLA_NUL_STRING,
  133. .len = IFNAMSIZ - 1 },
  134. [NLBL_UNLABEL_A_SECCTX] = { .type = NLA_BINARY }
  135. };
  136. /*
  137. * Unlabeled Connection Hash Table Functions
  138. */
  139. /**
  140. * netlbl_unlhsh_free_iface - Frees an interface entry from the hash table
  141. * @entry: the entry's RCU field
  142. *
  143. * Description:
  144. * This function is designed to be used as a callback to the call_rcu()
  145. * function so that memory allocated to a hash table interface entry can be
  146. * released safely. It is important to note that this function does not free
  147. * the IPv4 and IPv6 address lists contained as part of an interface entry. It
  148. * is up to the rest of the code to make sure an interface entry is only freed
  149. * once it's address lists are empty.
  150. *
  151. */
  152. static void netlbl_unlhsh_free_iface(struct rcu_head *entry)
  153. {
  154. struct netlbl_unlhsh_iface *iface;
  155. struct netlbl_af4list *iter4;
  156. struct netlbl_af4list *tmp4;
  157. #if IS_ENABLED(CONFIG_IPV6)
  158. struct netlbl_af6list *iter6;
  159. struct netlbl_af6list *tmp6;
  160. #endif /* IPv6 */
  161. iface = container_of(entry, struct netlbl_unlhsh_iface, rcu);
  162. /* no need for locks here since we are the only one with access to this
  163. * structure */
  164. netlbl_af4list_foreach_safe(iter4, tmp4, &iface->addr4_list) {
  165. netlbl_af4list_remove_entry(iter4);
  166. kfree(netlbl_unlhsh_addr4_entry(iter4));
  167. }
  168. #if IS_ENABLED(CONFIG_IPV6)
  169. netlbl_af6list_foreach_safe(iter6, tmp6, &iface->addr6_list) {
  170. netlbl_af6list_remove_entry(iter6);
  171. kfree(netlbl_unlhsh_addr6_entry(iter6));
  172. }
  173. #endif /* IPv6 */
  174. kfree(iface);
  175. }
  176. /**
  177. * netlbl_unlhsh_hash - Hashing function for the hash table
  178. * @ifindex: the network interface/device to hash
  179. *
  180. * Description:
  181. * This is the hashing function for the unlabeled hash table, it returns the
  182. * bucket number for the given device/interface. The caller is responsible for
  183. * ensuring that the hash table is protected with either a RCU read lock or
  184. * the hash table lock.
  185. *
  186. */
  187. static u32 netlbl_unlhsh_hash(int ifindex)
  188. {
  189. return ifindex & (netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->size - 1);
  190. }
  191. /**
  192. * netlbl_unlhsh_search_iface - Search for a matching interface entry
  193. * @ifindex: the network interface
  194. *
  195. * Description:
  196. * Searches the unlabeled connection hash table and returns a pointer to the
  197. * interface entry which matches @ifindex, otherwise NULL is returned. The
  198. * caller is responsible for ensuring that the hash table is protected with
  199. * either a RCU read lock or the hash table lock.
  200. *
  201. */
  202. static struct netlbl_unlhsh_iface *netlbl_unlhsh_search_iface(int ifindex)
  203. {
  204. u32 bkt;
  205. struct list_head *bkt_list;
  206. struct netlbl_unlhsh_iface *iter;
  207. bkt = netlbl_unlhsh_hash(ifindex);
  208. bkt_list = &netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->tbl[bkt];
  209. list_for_each_entry_rcu(iter, bkt_list, list)
  210. if (iter->valid && iter->ifindex == ifindex)
  211. return iter;
  212. return NULL;
  213. }
  214. /**
  215. * netlbl_unlhsh_add_addr4 - Add a new IPv4 address entry to the hash table
  216. * @iface: the associated interface entry
  217. * @addr: IPv4 address in network byte order
  218. * @mask: IPv4 address mask in network byte order
  219. * @secid: LSM secid value for entry
  220. *
  221. * Description:
  222. * Add a new address entry into the unlabeled connection hash table using the
  223. * interface entry specified by @iface. On success zero is returned, otherwise
  224. * a negative value is returned.
  225. *
  226. */
  227. static int netlbl_unlhsh_add_addr4(struct netlbl_unlhsh_iface *iface,
  228. const struct in_addr *addr,
  229. const struct in_addr *mask,
  230. u32 secid)
  231. {
  232. int ret_val;
  233. struct netlbl_unlhsh_addr4 *entry;
  234. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  235. if (entry == NULL)
  236. return -ENOMEM;
  237. entry->list.addr = addr->s_addr & mask->s_addr;
  238. entry->list.mask = mask->s_addr;
  239. entry->list.valid = 1;
  240. entry->secid = secid;
  241. spin_lock(&netlbl_unlhsh_lock);
  242. ret_val = netlbl_af4list_add(&entry->list, &iface->addr4_list);
  243. spin_unlock(&netlbl_unlhsh_lock);
  244. if (ret_val != 0)
  245. kfree(entry);
  246. return ret_val;
  247. }
  248. #if IS_ENABLED(CONFIG_IPV6)
  249. /**
  250. * netlbl_unlhsh_add_addr6 - Add a new IPv6 address entry to the hash table
  251. * @iface: the associated interface entry
  252. * @addr: IPv6 address in network byte order
  253. * @mask: IPv6 address mask in network byte order
  254. * @secid: LSM secid value for entry
  255. *
  256. * Description:
  257. * Add a new address entry into the unlabeled connection hash table using the
  258. * interface entry specified by @iface. On success zero is returned, otherwise
  259. * a negative value is returned.
  260. *
  261. */
  262. static int netlbl_unlhsh_add_addr6(struct netlbl_unlhsh_iface *iface,
  263. const struct in6_addr *addr,
  264. const struct in6_addr *mask,
  265. u32 secid)
  266. {
  267. int ret_val;
  268. struct netlbl_unlhsh_addr6 *entry;
  269. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  270. if (entry == NULL)
  271. return -ENOMEM;
  272. entry->list.addr = *addr;
  273. entry->list.addr.s6_addr32[0] &= mask->s6_addr32[0];
  274. entry->list.addr.s6_addr32[1] &= mask->s6_addr32[1];
  275. entry->list.addr.s6_addr32[2] &= mask->s6_addr32[2];
  276. entry->list.addr.s6_addr32[3] &= mask->s6_addr32[3];
  277. entry->list.mask = *mask;
  278. entry->list.valid = 1;
  279. entry->secid = secid;
  280. spin_lock(&netlbl_unlhsh_lock);
  281. ret_val = netlbl_af6list_add(&entry->list, &iface->addr6_list);
  282. spin_unlock(&netlbl_unlhsh_lock);
  283. if (ret_val != 0)
  284. kfree(entry);
  285. return 0;
  286. }
  287. #endif /* IPv6 */
  288. /**
  289. * netlbl_unlhsh_add_iface - Adds a new interface entry to the hash table
  290. * @ifindex: network interface
  291. *
  292. * Description:
  293. * Add a new, empty, interface entry into the unlabeled connection hash table.
  294. * On success a pointer to the new interface entry is returned, on failure NULL
  295. * is returned.
  296. *
  297. */
  298. static struct netlbl_unlhsh_iface *netlbl_unlhsh_add_iface(int ifindex)
  299. {
  300. u32 bkt;
  301. struct netlbl_unlhsh_iface *iface;
  302. iface = kzalloc(sizeof(*iface), GFP_ATOMIC);
  303. if (iface == NULL)
  304. return NULL;
  305. iface->ifindex = ifindex;
  306. INIT_LIST_HEAD(&iface->addr4_list);
  307. INIT_LIST_HEAD(&iface->addr6_list);
  308. iface->valid = 1;
  309. spin_lock(&netlbl_unlhsh_lock);
  310. if (ifindex > 0) {
  311. bkt = netlbl_unlhsh_hash(ifindex);
  312. if (netlbl_unlhsh_search_iface(ifindex) != NULL)
  313. goto add_iface_failure;
  314. list_add_tail_rcu(&iface->list,
  315. &netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->tbl[bkt]);
  316. } else {
  317. INIT_LIST_HEAD(&iface->list);
  318. if (netlbl_unlhsh_rcu_deref(netlbl_unlhsh_def) != NULL)
  319. goto add_iface_failure;
  320. rcu_assign_pointer(netlbl_unlhsh_def, iface);
  321. }
  322. spin_unlock(&netlbl_unlhsh_lock);
  323. return iface;
  324. add_iface_failure:
  325. spin_unlock(&netlbl_unlhsh_lock);
  326. kfree(iface);
  327. return NULL;
  328. }
  329. /**
  330. * netlbl_unlhsh_add - Adds a new entry to the unlabeled connection hash table
  331. * @net: network namespace
  332. * @dev_name: interface name
  333. * @addr: IP address in network byte order
  334. * @mask: address mask in network byte order
  335. * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
  336. * @secid: LSM secid value for the entry
  337. * @audit_info: NetLabel audit information
  338. *
  339. * Description:
  340. * Adds a new entry to the unlabeled connection hash table. Returns zero on
  341. * success, negative values on failure.
  342. *
  343. */
  344. int netlbl_unlhsh_add(struct net *net,
  345. const char *dev_name,
  346. const void *addr,
  347. const void *mask,
  348. u32 addr_len,
  349. u32 secid,
  350. struct netlbl_audit *audit_info)
  351. {
  352. int ret_val;
  353. int ifindex;
  354. struct net_device *dev;
  355. struct netlbl_unlhsh_iface *iface;
  356. struct audit_buffer *audit_buf = NULL;
  357. char *secctx = NULL;
  358. u32 secctx_len;
  359. if (addr_len != sizeof(struct in_addr) &&
  360. addr_len != sizeof(struct in6_addr))
  361. return -EINVAL;
  362. rcu_read_lock();
  363. if (dev_name != NULL) {
  364. dev = dev_get_by_name_rcu(net, dev_name);
  365. if (dev == NULL) {
  366. ret_val = -ENODEV;
  367. goto unlhsh_add_return;
  368. }
  369. ifindex = dev->ifindex;
  370. iface = netlbl_unlhsh_search_iface(ifindex);
  371. } else {
  372. ifindex = 0;
  373. iface = rcu_dereference(netlbl_unlhsh_def);
  374. }
  375. if (iface == NULL) {
  376. iface = netlbl_unlhsh_add_iface(ifindex);
  377. if (iface == NULL) {
  378. ret_val = -ENOMEM;
  379. goto unlhsh_add_return;
  380. }
  381. }
  382. audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCADD,
  383. audit_info);
  384. switch (addr_len) {
  385. case sizeof(struct in_addr): {
  386. const struct in_addr *addr4 = addr;
  387. const struct in_addr *mask4 = mask;
  388. ret_val = netlbl_unlhsh_add_addr4(iface, addr4, mask4, secid);
  389. if (audit_buf != NULL)
  390. netlbl_af4list_audit_addr(audit_buf, 1,
  391. dev_name,
  392. addr4->s_addr,
  393. mask4->s_addr);
  394. break;
  395. }
  396. #if IS_ENABLED(CONFIG_IPV6)
  397. case sizeof(struct in6_addr): {
  398. const struct in6_addr *addr6 = addr;
  399. const struct in6_addr *mask6 = mask;
  400. ret_val = netlbl_unlhsh_add_addr6(iface, addr6, mask6, secid);
  401. if (audit_buf != NULL)
  402. netlbl_af6list_audit_addr(audit_buf, 1,
  403. dev_name,
  404. addr6, mask6);
  405. break;
  406. }
  407. #endif /* IPv6 */
  408. default:
  409. ret_val = -EINVAL;
  410. }
  411. if (ret_val == 0)
  412. atomic_inc(&netlabel_mgmt_protocount);
  413. unlhsh_add_return:
  414. rcu_read_unlock();
  415. if (audit_buf != NULL) {
  416. if (security_secid_to_secctx(secid,
  417. &secctx,
  418. &secctx_len) == 0) {
  419. audit_log_format(audit_buf, " sec_obj=%s", secctx);
  420. security_release_secctx(secctx, secctx_len);
  421. }
  422. audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
  423. audit_log_end(audit_buf);
  424. }
  425. return ret_val;
  426. }
  427. /**
  428. * netlbl_unlhsh_remove_addr4 - Remove an IPv4 address entry
  429. * @net: network namespace
  430. * @iface: interface entry
  431. * @addr: IP address
  432. * @mask: IP address mask
  433. * @audit_info: NetLabel audit information
  434. *
  435. * Description:
  436. * Remove an IP address entry from the unlabeled connection hash table.
  437. * Returns zero on success, negative values on failure.
  438. *
  439. */
  440. static int netlbl_unlhsh_remove_addr4(struct net *net,
  441. struct netlbl_unlhsh_iface *iface,
  442. const struct in_addr *addr,
  443. const struct in_addr *mask,
  444. struct netlbl_audit *audit_info)
  445. {
  446. struct netlbl_af4list *list_entry;
  447. struct netlbl_unlhsh_addr4 *entry;
  448. struct audit_buffer *audit_buf;
  449. struct net_device *dev;
  450. char *secctx;
  451. u32 secctx_len;
  452. spin_lock(&netlbl_unlhsh_lock);
  453. list_entry = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
  454. &iface->addr4_list);
  455. spin_unlock(&netlbl_unlhsh_lock);
  456. if (list_entry != NULL)
  457. entry = netlbl_unlhsh_addr4_entry(list_entry);
  458. else
  459. entry = NULL;
  460. audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
  461. audit_info);
  462. if (audit_buf != NULL) {
  463. dev = dev_get_by_index(net, iface->ifindex);
  464. netlbl_af4list_audit_addr(audit_buf, 1,
  465. (dev != NULL ? dev->name : NULL),
  466. addr->s_addr, mask->s_addr);
  467. if (dev != NULL)
  468. dev_put(dev);
  469. if (entry != NULL &&
  470. security_secid_to_secctx(entry->secid,
  471. &secctx, &secctx_len) == 0) {
  472. audit_log_format(audit_buf, " sec_obj=%s", secctx);
  473. security_release_secctx(secctx, secctx_len);
  474. }
  475. audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
  476. audit_log_end(audit_buf);
  477. }
  478. if (entry == NULL)
  479. return -ENOENT;
  480. kfree_rcu(entry, rcu);
  481. return 0;
  482. }
  483. #if IS_ENABLED(CONFIG_IPV6)
  484. /**
  485. * netlbl_unlhsh_remove_addr6 - Remove an IPv6 address entry
  486. * @net: network namespace
  487. * @iface: interface entry
  488. * @addr: IP address
  489. * @mask: IP address mask
  490. * @audit_info: NetLabel audit information
  491. *
  492. * Description:
  493. * Remove an IP address entry from the unlabeled connection hash table.
  494. * Returns zero on success, negative values on failure.
  495. *
  496. */
  497. static int netlbl_unlhsh_remove_addr6(struct net *net,
  498. struct netlbl_unlhsh_iface *iface,
  499. const struct in6_addr *addr,
  500. const struct in6_addr *mask,
  501. struct netlbl_audit *audit_info)
  502. {
  503. struct netlbl_af6list *list_entry;
  504. struct netlbl_unlhsh_addr6 *entry;
  505. struct audit_buffer *audit_buf;
  506. struct net_device *dev;
  507. char *secctx;
  508. u32 secctx_len;
  509. spin_lock(&netlbl_unlhsh_lock);
  510. list_entry = netlbl_af6list_remove(addr, mask, &iface->addr6_list);
  511. spin_unlock(&netlbl_unlhsh_lock);
  512. if (list_entry != NULL)
  513. entry = netlbl_unlhsh_addr6_entry(list_entry);
  514. else
  515. entry = NULL;
  516. audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
  517. audit_info);
  518. if (audit_buf != NULL) {
  519. dev = dev_get_by_index(net, iface->ifindex);
  520. netlbl_af6list_audit_addr(audit_buf, 1,
  521. (dev != NULL ? dev->name : NULL),
  522. addr, mask);
  523. if (dev != NULL)
  524. dev_put(dev);
  525. if (entry != NULL &&
  526. security_secid_to_secctx(entry->secid,
  527. &secctx, &secctx_len) == 0) {
  528. audit_log_format(audit_buf, " sec_obj=%s", secctx);
  529. security_release_secctx(secctx, secctx_len);
  530. }
  531. audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
  532. audit_log_end(audit_buf);
  533. }
  534. if (entry == NULL)
  535. return -ENOENT;
  536. kfree_rcu(entry, rcu);
  537. return 0;
  538. }
  539. #endif /* IPv6 */
  540. /**
  541. * netlbl_unlhsh_condremove_iface - Remove an interface entry
  542. * @iface: the interface entry
  543. *
  544. * Description:
  545. * Remove an interface entry from the unlabeled connection hash table if it is
  546. * empty. An interface entry is considered to be empty if there are no
  547. * address entries assigned to it.
  548. *
  549. */
  550. static void netlbl_unlhsh_condremove_iface(struct netlbl_unlhsh_iface *iface)
  551. {
  552. struct netlbl_af4list *iter4;
  553. #if IS_ENABLED(CONFIG_IPV6)
  554. struct netlbl_af6list *iter6;
  555. #endif /* IPv6 */
  556. spin_lock(&netlbl_unlhsh_lock);
  557. netlbl_af4list_foreach_rcu(iter4, &iface->addr4_list)
  558. goto unlhsh_condremove_failure;
  559. #if IS_ENABLED(CONFIG_IPV6)
  560. netlbl_af6list_foreach_rcu(iter6, &iface->addr6_list)
  561. goto unlhsh_condremove_failure;
  562. #endif /* IPv6 */
  563. iface->valid = 0;
  564. if (iface->ifindex > 0)
  565. list_del_rcu(&iface->list);
  566. else
  567. RCU_INIT_POINTER(netlbl_unlhsh_def, NULL);
  568. spin_unlock(&netlbl_unlhsh_lock);
  569. call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
  570. return;
  571. unlhsh_condremove_failure:
  572. spin_unlock(&netlbl_unlhsh_lock);
  573. }
  574. /**
  575. * netlbl_unlhsh_remove - Remove an entry from the unlabeled hash table
  576. * @net: network namespace
  577. * @dev_name: interface name
  578. * @addr: IP address in network byte order
  579. * @mask: address mask in network byte order
  580. * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
  581. * @audit_info: NetLabel audit information
  582. *
  583. * Description:
  584. * Removes and existing entry from the unlabeled connection hash table.
  585. * Returns zero on success, negative values on failure.
  586. *
  587. */
  588. int netlbl_unlhsh_remove(struct net *net,
  589. const char *dev_name,
  590. const void *addr,
  591. const void *mask,
  592. u32 addr_len,
  593. struct netlbl_audit *audit_info)
  594. {
  595. int ret_val;
  596. struct net_device *dev;
  597. struct netlbl_unlhsh_iface *iface;
  598. if (addr_len != sizeof(struct in_addr) &&
  599. addr_len != sizeof(struct in6_addr))
  600. return -EINVAL;
  601. rcu_read_lock();
  602. if (dev_name != NULL) {
  603. dev = dev_get_by_name_rcu(net, dev_name);
  604. if (dev == NULL) {
  605. ret_val = -ENODEV;
  606. goto unlhsh_remove_return;
  607. }
  608. iface = netlbl_unlhsh_search_iface(dev->ifindex);
  609. } else
  610. iface = rcu_dereference(netlbl_unlhsh_def);
  611. if (iface == NULL) {
  612. ret_val = -ENOENT;
  613. goto unlhsh_remove_return;
  614. }
  615. switch (addr_len) {
  616. case sizeof(struct in_addr):
  617. ret_val = netlbl_unlhsh_remove_addr4(net,
  618. iface, addr, mask,
  619. audit_info);
  620. break;
  621. #if IS_ENABLED(CONFIG_IPV6)
  622. case sizeof(struct in6_addr):
  623. ret_val = netlbl_unlhsh_remove_addr6(net,
  624. iface, addr, mask,
  625. audit_info);
  626. break;
  627. #endif /* IPv6 */
  628. default:
  629. ret_val = -EINVAL;
  630. }
  631. if (ret_val == 0) {
  632. netlbl_unlhsh_condremove_iface(iface);
  633. atomic_dec(&netlabel_mgmt_protocount);
  634. }
  635. unlhsh_remove_return:
  636. rcu_read_unlock();
  637. return ret_val;
  638. }
  639. /*
  640. * General Helper Functions
  641. */
  642. /**
  643. * netlbl_unlhsh_netdev_handler - Network device notification handler
  644. * @this: notifier block
  645. * @event: the event
  646. * @ptr: the netdevice notifier info (cast to void)
  647. *
  648. * Description:
  649. * Handle network device events, although at present all we care about is a
  650. * network device going away. In the case of a device going away we clear any
  651. * related entries from the unlabeled connection hash table.
  652. *
  653. */
  654. static int netlbl_unlhsh_netdev_handler(struct notifier_block *this,
  655. unsigned long event, void *ptr)
  656. {
  657. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  658. struct netlbl_unlhsh_iface *iface = NULL;
  659. if (!net_eq(dev_net(dev), &init_net))
  660. return NOTIFY_DONE;
  661. /* XXX - should this be a check for NETDEV_DOWN or _UNREGISTER? */
  662. if (event == NETDEV_DOWN) {
  663. spin_lock(&netlbl_unlhsh_lock);
  664. iface = netlbl_unlhsh_search_iface(dev->ifindex);
  665. if (iface != NULL && iface->valid) {
  666. iface->valid = 0;
  667. list_del_rcu(&iface->list);
  668. } else
  669. iface = NULL;
  670. spin_unlock(&netlbl_unlhsh_lock);
  671. }
  672. if (iface != NULL)
  673. call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
  674. return NOTIFY_DONE;
  675. }
  676. /**
  677. * netlbl_unlabel_acceptflg_set - Set the unlabeled accept flag
  678. * @value: desired value
  679. * @audit_info: NetLabel audit information
  680. *
  681. * Description:
  682. * Set the value of the unlabeled accept flag to @value.
  683. *
  684. */
  685. static void netlbl_unlabel_acceptflg_set(u8 value,
  686. struct netlbl_audit *audit_info)
  687. {
  688. struct audit_buffer *audit_buf;
  689. u8 old_val;
  690. old_val = netlabel_unlabel_acceptflg;
  691. netlabel_unlabel_acceptflg = value;
  692. audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_ALLOW,
  693. audit_info);
  694. if (audit_buf != NULL) {
  695. audit_log_format(audit_buf,
  696. " unlbl_accept=%u old=%u", value, old_val);
  697. audit_log_end(audit_buf);
  698. }
  699. }
  700. /**
  701. * netlbl_unlabel_addrinfo_get - Get the IPv4/6 address information
  702. * @info: the Generic NETLINK info block
  703. * @addr: the IP address
  704. * @mask: the IP address mask
  705. * @len: the address length
  706. *
  707. * Description:
  708. * Examine the Generic NETLINK message and extract the IP address information.
  709. * Returns zero on success, negative values on failure.
  710. *
  711. */
  712. static int netlbl_unlabel_addrinfo_get(struct genl_info *info,
  713. void **addr,
  714. void **mask,
  715. u32 *len)
  716. {
  717. u32 addr_len;
  718. if (info->attrs[NLBL_UNLABEL_A_IPV4ADDR] &&
  719. info->attrs[NLBL_UNLABEL_A_IPV4MASK]) {
  720. addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
  721. if (addr_len != sizeof(struct in_addr) &&
  722. addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV4MASK]))
  723. return -EINVAL;
  724. *len = addr_len;
  725. *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
  726. *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4MASK]);
  727. return 0;
  728. } else if (info->attrs[NLBL_UNLABEL_A_IPV6ADDR]) {
  729. addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
  730. if (addr_len != sizeof(struct in6_addr) &&
  731. addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV6MASK]))
  732. return -EINVAL;
  733. *len = addr_len;
  734. *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
  735. *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6MASK]);
  736. return 0;
  737. }
  738. return -EINVAL;
  739. }
  740. /*
  741. * NetLabel Command Handlers
  742. */
  743. /**
  744. * netlbl_unlabel_accept - Handle an ACCEPT message
  745. * @skb: the NETLINK buffer
  746. * @info: the Generic NETLINK info block
  747. *
  748. * Description:
  749. * Process a user generated ACCEPT message and set the accept flag accordingly.
  750. * Returns zero on success, negative values on failure.
  751. *
  752. */
  753. static int netlbl_unlabel_accept(struct sk_buff *skb, struct genl_info *info)
  754. {
  755. u8 value;
  756. struct netlbl_audit audit_info;
  757. if (info->attrs[NLBL_UNLABEL_A_ACPTFLG]) {
  758. value = nla_get_u8(info->attrs[NLBL_UNLABEL_A_ACPTFLG]);
  759. if (value == 1 || value == 0) {
  760. netlbl_netlink_auditinfo(skb, &audit_info);
  761. netlbl_unlabel_acceptflg_set(value, &audit_info);
  762. return 0;
  763. }
  764. }
  765. return -EINVAL;
  766. }
  767. /**
  768. * netlbl_unlabel_list - Handle a LIST message
  769. * @skb: the NETLINK buffer
  770. * @info: the Generic NETLINK info block
  771. *
  772. * Description:
  773. * Process a user generated LIST message and respond with the current status.
  774. * Returns zero on success, negative values on failure.
  775. *
  776. */
  777. static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info)
  778. {
  779. int ret_val = -EINVAL;
  780. struct sk_buff *ans_skb;
  781. void *data;
  782. ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  783. if (ans_skb == NULL)
  784. goto list_failure;
  785. data = genlmsg_put_reply(ans_skb, info, &netlbl_unlabel_gnl_family,
  786. 0, NLBL_UNLABEL_C_LIST);
  787. if (data == NULL) {
  788. ret_val = -ENOMEM;
  789. goto list_failure;
  790. }
  791. ret_val = nla_put_u8(ans_skb,
  792. NLBL_UNLABEL_A_ACPTFLG,
  793. netlabel_unlabel_acceptflg);
  794. if (ret_val != 0)
  795. goto list_failure;
  796. genlmsg_end(ans_skb, data);
  797. return genlmsg_reply(ans_skb, info);
  798. list_failure:
  799. kfree_skb(ans_skb);
  800. return ret_val;
  801. }
  802. /**
  803. * netlbl_unlabel_staticadd - Handle a STATICADD message
  804. * @skb: the NETLINK buffer
  805. * @info: the Generic NETLINK info block
  806. *
  807. * Description:
  808. * Process a user generated STATICADD message and add a new unlabeled
  809. * connection entry to the hash table. Returns zero on success, negative
  810. * values on failure.
  811. *
  812. */
  813. static int netlbl_unlabel_staticadd(struct sk_buff *skb,
  814. struct genl_info *info)
  815. {
  816. int ret_val;
  817. char *dev_name;
  818. void *addr;
  819. void *mask;
  820. u32 addr_len;
  821. u32 secid;
  822. struct netlbl_audit audit_info;
  823. /* Don't allow users to add both IPv4 and IPv6 addresses for a
  824. * single entry. However, allow users to create two entries, one each
  825. * for IPv4 and IPv4, with the same LSM security context which should
  826. * achieve the same result. */
  827. if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
  828. !info->attrs[NLBL_UNLABEL_A_IFACE] ||
  829. !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
  830. !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
  831. (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
  832. !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
  833. return -EINVAL;
  834. netlbl_netlink_auditinfo(skb, &audit_info);
  835. ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
  836. if (ret_val != 0)
  837. return ret_val;
  838. dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
  839. ret_val = security_secctx_to_secid(
  840. nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
  841. nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
  842. &secid);
  843. if (ret_val != 0)
  844. return ret_val;
  845. return netlbl_unlhsh_add(&init_net,
  846. dev_name, addr, mask, addr_len, secid,
  847. &audit_info);
  848. }
  849. /**
  850. * netlbl_unlabel_staticadddef - Handle a STATICADDDEF message
  851. * @skb: the NETLINK buffer
  852. * @info: the Generic NETLINK info block
  853. *
  854. * Description:
  855. * Process a user generated STATICADDDEF message and add a new default
  856. * unlabeled connection entry. Returns zero on success, negative values on
  857. * failure.
  858. *
  859. */
  860. static int netlbl_unlabel_staticadddef(struct sk_buff *skb,
  861. struct genl_info *info)
  862. {
  863. int ret_val;
  864. void *addr;
  865. void *mask;
  866. u32 addr_len;
  867. u32 secid;
  868. struct netlbl_audit audit_info;
  869. /* Don't allow users to add both IPv4 and IPv6 addresses for a
  870. * single entry. However, allow users to create two entries, one each
  871. * for IPv4 and IPv6, with the same LSM security context which should
  872. * achieve the same result. */
  873. if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
  874. !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
  875. !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
  876. (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
  877. !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
  878. return -EINVAL;
  879. netlbl_netlink_auditinfo(skb, &audit_info);
  880. ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
  881. if (ret_val != 0)
  882. return ret_val;
  883. ret_val = security_secctx_to_secid(
  884. nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
  885. nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
  886. &secid);
  887. if (ret_val != 0)
  888. return ret_val;
  889. return netlbl_unlhsh_add(&init_net,
  890. NULL, addr, mask, addr_len, secid,
  891. &audit_info);
  892. }
  893. /**
  894. * netlbl_unlabel_staticremove - Handle a STATICREMOVE message
  895. * @skb: the NETLINK buffer
  896. * @info: the Generic NETLINK info block
  897. *
  898. * Description:
  899. * Process a user generated STATICREMOVE message and remove the specified
  900. * unlabeled connection entry. Returns zero on success, negative values on
  901. * failure.
  902. *
  903. */
  904. static int netlbl_unlabel_staticremove(struct sk_buff *skb,
  905. struct genl_info *info)
  906. {
  907. int ret_val;
  908. char *dev_name;
  909. void *addr;
  910. void *mask;
  911. u32 addr_len;
  912. struct netlbl_audit audit_info;
  913. /* See the note in netlbl_unlabel_staticadd() about not allowing both
  914. * IPv4 and IPv6 in the same entry. */
  915. if (!info->attrs[NLBL_UNLABEL_A_IFACE] ||
  916. !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
  917. !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
  918. (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
  919. !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
  920. return -EINVAL;
  921. netlbl_netlink_auditinfo(skb, &audit_info);
  922. ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
  923. if (ret_val != 0)
  924. return ret_val;
  925. dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
  926. return netlbl_unlhsh_remove(&init_net,
  927. dev_name, addr, mask, addr_len,
  928. &audit_info);
  929. }
  930. /**
  931. * netlbl_unlabel_staticremovedef - Handle a STATICREMOVEDEF message
  932. * @skb: the NETLINK buffer
  933. * @info: the Generic NETLINK info block
  934. *
  935. * Description:
  936. * Process a user generated STATICREMOVEDEF message and remove the default
  937. * unlabeled connection entry. Returns zero on success, negative values on
  938. * failure.
  939. *
  940. */
  941. static int netlbl_unlabel_staticremovedef(struct sk_buff *skb,
  942. struct genl_info *info)
  943. {
  944. int ret_val;
  945. void *addr;
  946. void *mask;
  947. u32 addr_len;
  948. struct netlbl_audit audit_info;
  949. /* See the note in netlbl_unlabel_staticadd() about not allowing both
  950. * IPv4 and IPv6 in the same entry. */
  951. if (!((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
  952. !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
  953. (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
  954. !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
  955. return -EINVAL;
  956. netlbl_netlink_auditinfo(skb, &audit_info);
  957. ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
  958. if (ret_val != 0)
  959. return ret_val;
  960. return netlbl_unlhsh_remove(&init_net,
  961. NULL, addr, mask, addr_len,
  962. &audit_info);
  963. }
  964. /**
  965. * netlbl_unlabel_staticlist_gen - Generate messages for STATICLIST[DEF]
  966. * @cmd: command/message
  967. * @iface: the interface entry
  968. * @addr4: the IPv4 address entry
  969. * @addr6: the IPv6 address entry
  970. * @arg: the netlbl_unlhsh_walk_arg structure
  971. *
  972. * Description:
  973. * This function is designed to be used to generate a response for a
  974. * STATICLIST or STATICLISTDEF message. When called either @addr4 or @addr6
  975. * can be specified, not both, the other unspecified entry should be set to
  976. * NULL by the caller. Returns the size of the message on success, negative
  977. * values on failure.
  978. *
  979. */
  980. static int netlbl_unlabel_staticlist_gen(u32 cmd,
  981. const struct netlbl_unlhsh_iface *iface,
  982. const struct netlbl_unlhsh_addr4 *addr4,
  983. const struct netlbl_unlhsh_addr6 *addr6,
  984. void *arg)
  985. {
  986. int ret_val = -ENOMEM;
  987. struct netlbl_unlhsh_walk_arg *cb_arg = arg;
  988. struct net_device *dev;
  989. void *data;
  990. u32 secid;
  991. char *secctx;
  992. u32 secctx_len;
  993. data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).portid,
  994. cb_arg->seq, &netlbl_unlabel_gnl_family,
  995. NLM_F_MULTI, cmd);
  996. if (data == NULL)
  997. goto list_cb_failure;
  998. if (iface->ifindex > 0) {
  999. dev = dev_get_by_index(&init_net, iface->ifindex);
  1000. if (!dev) {
  1001. ret_val = -ENODEV;
  1002. goto list_cb_failure;
  1003. }
  1004. ret_val = nla_put_string(cb_arg->skb,
  1005. NLBL_UNLABEL_A_IFACE, dev->name);
  1006. dev_put(dev);
  1007. if (ret_val != 0)
  1008. goto list_cb_failure;
  1009. }
  1010. if (addr4) {
  1011. struct in_addr addr_struct;
  1012. addr_struct.s_addr = addr4->list.addr;
  1013. ret_val = nla_put_in_addr(cb_arg->skb,
  1014. NLBL_UNLABEL_A_IPV4ADDR,
  1015. addr_struct.s_addr);
  1016. if (ret_val != 0)
  1017. goto list_cb_failure;
  1018. addr_struct.s_addr = addr4->list.mask;
  1019. ret_val = nla_put_in_addr(cb_arg->skb,
  1020. NLBL_UNLABEL_A_IPV4MASK,
  1021. addr_struct.s_addr);
  1022. if (ret_val != 0)
  1023. goto list_cb_failure;
  1024. secid = addr4->secid;
  1025. } else {
  1026. ret_val = nla_put_in6_addr(cb_arg->skb,
  1027. NLBL_UNLABEL_A_IPV6ADDR,
  1028. &addr6->list.addr);
  1029. if (ret_val != 0)
  1030. goto list_cb_failure;
  1031. ret_val = nla_put_in6_addr(cb_arg->skb,
  1032. NLBL_UNLABEL_A_IPV6MASK,
  1033. &addr6->list.mask);
  1034. if (ret_val != 0)
  1035. goto list_cb_failure;
  1036. secid = addr6->secid;
  1037. }
  1038. ret_val = security_secid_to_secctx(secid, &secctx, &secctx_len);
  1039. if (ret_val != 0)
  1040. goto list_cb_failure;
  1041. ret_val = nla_put(cb_arg->skb,
  1042. NLBL_UNLABEL_A_SECCTX,
  1043. secctx_len,
  1044. secctx);
  1045. security_release_secctx(secctx, secctx_len);
  1046. if (ret_val != 0)
  1047. goto list_cb_failure;
  1048. cb_arg->seq++;
  1049. genlmsg_end(cb_arg->skb, data);
  1050. return 0;
  1051. list_cb_failure:
  1052. genlmsg_cancel(cb_arg->skb, data);
  1053. return ret_val;
  1054. }
  1055. /**
  1056. * netlbl_unlabel_staticlist - Handle a STATICLIST message
  1057. * @skb: the NETLINK buffer
  1058. * @cb: the NETLINK callback
  1059. *
  1060. * Description:
  1061. * Process a user generated STATICLIST message and dump the unlabeled
  1062. * connection hash table in a form suitable for use in a kernel generated
  1063. * STATICLIST message. Returns the length of @skb.
  1064. *
  1065. */
  1066. static int netlbl_unlabel_staticlist(struct sk_buff *skb,
  1067. struct netlink_callback *cb)
  1068. {
  1069. struct netlbl_unlhsh_walk_arg cb_arg;
  1070. u32 skip_bkt = cb->args[0];
  1071. u32 skip_chain = cb->args[1];
  1072. u32 iter_bkt;
  1073. u32 iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0;
  1074. struct netlbl_unlhsh_iface *iface;
  1075. struct list_head *iter_list;
  1076. struct netlbl_af4list *addr4;
  1077. #if IS_ENABLED(CONFIG_IPV6)
  1078. struct netlbl_af6list *addr6;
  1079. #endif
  1080. cb_arg.nl_cb = cb;
  1081. cb_arg.skb = skb;
  1082. cb_arg.seq = cb->nlh->nlmsg_seq;
  1083. rcu_read_lock();
  1084. for (iter_bkt = skip_bkt;
  1085. iter_bkt < rcu_dereference(netlbl_unlhsh)->size;
  1086. iter_bkt++, iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0) {
  1087. iter_list = &rcu_dereference(netlbl_unlhsh)->tbl[iter_bkt];
  1088. list_for_each_entry_rcu(iface, iter_list, list) {
  1089. if (!iface->valid ||
  1090. iter_chain++ < skip_chain)
  1091. continue;
  1092. netlbl_af4list_foreach_rcu(addr4,
  1093. &iface->addr4_list) {
  1094. if (iter_addr4++ < cb->args[2])
  1095. continue;
  1096. if (netlbl_unlabel_staticlist_gen(
  1097. NLBL_UNLABEL_C_STATICLIST,
  1098. iface,
  1099. netlbl_unlhsh_addr4_entry(addr4),
  1100. NULL,
  1101. &cb_arg) < 0) {
  1102. iter_addr4--;
  1103. iter_chain--;
  1104. goto unlabel_staticlist_return;
  1105. }
  1106. }
  1107. #if IS_ENABLED(CONFIG_IPV6)
  1108. netlbl_af6list_foreach_rcu(addr6,
  1109. &iface->addr6_list) {
  1110. if (iter_addr6++ < cb->args[3])
  1111. continue;
  1112. if (netlbl_unlabel_staticlist_gen(
  1113. NLBL_UNLABEL_C_STATICLIST,
  1114. iface,
  1115. NULL,
  1116. netlbl_unlhsh_addr6_entry(addr6),
  1117. &cb_arg) < 0) {
  1118. iter_addr6--;
  1119. iter_chain--;
  1120. goto unlabel_staticlist_return;
  1121. }
  1122. }
  1123. #endif /* IPv6 */
  1124. }
  1125. }
  1126. unlabel_staticlist_return:
  1127. rcu_read_unlock();
  1128. cb->args[0] = iter_bkt;
  1129. cb->args[1] = iter_chain;
  1130. cb->args[2] = iter_addr4;
  1131. cb->args[3] = iter_addr6;
  1132. return skb->len;
  1133. }
  1134. /**
  1135. * netlbl_unlabel_staticlistdef - Handle a STATICLISTDEF message
  1136. * @skb: the NETLINK buffer
  1137. * @cb: the NETLINK callback
  1138. *
  1139. * Description:
  1140. * Process a user generated STATICLISTDEF message and dump the default
  1141. * unlabeled connection entry in a form suitable for use in a kernel generated
  1142. * STATICLISTDEF message. Returns the length of @skb.
  1143. *
  1144. */
  1145. static int netlbl_unlabel_staticlistdef(struct sk_buff *skb,
  1146. struct netlink_callback *cb)
  1147. {
  1148. struct netlbl_unlhsh_walk_arg cb_arg;
  1149. struct netlbl_unlhsh_iface *iface;
  1150. u32 iter_addr4 = 0, iter_addr6 = 0;
  1151. struct netlbl_af4list *addr4;
  1152. #if IS_ENABLED(CONFIG_IPV6)
  1153. struct netlbl_af6list *addr6;
  1154. #endif
  1155. cb_arg.nl_cb = cb;
  1156. cb_arg.skb = skb;
  1157. cb_arg.seq = cb->nlh->nlmsg_seq;
  1158. rcu_read_lock();
  1159. iface = rcu_dereference(netlbl_unlhsh_def);
  1160. if (iface == NULL || !iface->valid)
  1161. goto unlabel_staticlistdef_return;
  1162. netlbl_af4list_foreach_rcu(addr4, &iface->addr4_list) {
  1163. if (iter_addr4++ < cb->args[0])
  1164. continue;
  1165. if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
  1166. iface,
  1167. netlbl_unlhsh_addr4_entry(addr4),
  1168. NULL,
  1169. &cb_arg) < 0) {
  1170. iter_addr4--;
  1171. goto unlabel_staticlistdef_return;
  1172. }
  1173. }
  1174. #if IS_ENABLED(CONFIG_IPV6)
  1175. netlbl_af6list_foreach_rcu(addr6, &iface->addr6_list) {
  1176. if (iter_addr6++ < cb->args[1])
  1177. continue;
  1178. if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
  1179. iface,
  1180. NULL,
  1181. netlbl_unlhsh_addr6_entry(addr6),
  1182. &cb_arg) < 0) {
  1183. iter_addr6--;
  1184. goto unlabel_staticlistdef_return;
  1185. }
  1186. }
  1187. #endif /* IPv6 */
  1188. unlabel_staticlistdef_return:
  1189. rcu_read_unlock();
  1190. cb->args[0] = iter_addr4;
  1191. cb->args[1] = iter_addr6;
  1192. return skb->len;
  1193. }
  1194. /*
  1195. * NetLabel Generic NETLINK Command Definitions
  1196. */
  1197. static const struct genl_ops netlbl_unlabel_genl_ops[] = {
  1198. {
  1199. .cmd = NLBL_UNLABEL_C_STATICADD,
  1200. .flags = GENL_ADMIN_PERM,
  1201. .policy = netlbl_unlabel_genl_policy,
  1202. .doit = netlbl_unlabel_staticadd,
  1203. .dumpit = NULL,
  1204. },
  1205. {
  1206. .cmd = NLBL_UNLABEL_C_STATICREMOVE,
  1207. .flags = GENL_ADMIN_PERM,
  1208. .policy = netlbl_unlabel_genl_policy,
  1209. .doit = netlbl_unlabel_staticremove,
  1210. .dumpit = NULL,
  1211. },
  1212. {
  1213. .cmd = NLBL_UNLABEL_C_STATICLIST,
  1214. .flags = 0,
  1215. .policy = netlbl_unlabel_genl_policy,
  1216. .doit = NULL,
  1217. .dumpit = netlbl_unlabel_staticlist,
  1218. },
  1219. {
  1220. .cmd = NLBL_UNLABEL_C_STATICADDDEF,
  1221. .flags = GENL_ADMIN_PERM,
  1222. .policy = netlbl_unlabel_genl_policy,
  1223. .doit = netlbl_unlabel_staticadddef,
  1224. .dumpit = NULL,
  1225. },
  1226. {
  1227. .cmd = NLBL_UNLABEL_C_STATICREMOVEDEF,
  1228. .flags = GENL_ADMIN_PERM,
  1229. .policy = netlbl_unlabel_genl_policy,
  1230. .doit = netlbl_unlabel_staticremovedef,
  1231. .dumpit = NULL,
  1232. },
  1233. {
  1234. .cmd = NLBL_UNLABEL_C_STATICLISTDEF,
  1235. .flags = 0,
  1236. .policy = netlbl_unlabel_genl_policy,
  1237. .doit = NULL,
  1238. .dumpit = netlbl_unlabel_staticlistdef,
  1239. },
  1240. {
  1241. .cmd = NLBL_UNLABEL_C_ACCEPT,
  1242. .flags = GENL_ADMIN_PERM,
  1243. .policy = netlbl_unlabel_genl_policy,
  1244. .doit = netlbl_unlabel_accept,
  1245. .dumpit = NULL,
  1246. },
  1247. {
  1248. .cmd = NLBL_UNLABEL_C_LIST,
  1249. .flags = 0,
  1250. .policy = netlbl_unlabel_genl_policy,
  1251. .doit = netlbl_unlabel_list,
  1252. .dumpit = NULL,
  1253. },
  1254. };
  1255. /*
  1256. * NetLabel Generic NETLINK Protocol Functions
  1257. */
  1258. /**
  1259. * netlbl_unlabel_genl_init - Register the Unlabeled NetLabel component
  1260. *
  1261. * Description:
  1262. * Register the unlabeled packet NetLabel component with the Generic NETLINK
  1263. * mechanism. Returns zero on success, negative values on failure.
  1264. *
  1265. */
  1266. int __init netlbl_unlabel_genl_init(void)
  1267. {
  1268. return genl_register_family_with_ops(&netlbl_unlabel_gnl_family,
  1269. netlbl_unlabel_genl_ops);
  1270. }
  1271. /*
  1272. * NetLabel KAPI Hooks
  1273. */
  1274. static struct notifier_block netlbl_unlhsh_netdev_notifier = {
  1275. .notifier_call = netlbl_unlhsh_netdev_handler,
  1276. };
  1277. /**
  1278. * netlbl_unlabel_init - Initialize the unlabeled connection hash table
  1279. * @size: the number of bits to use for the hash buckets
  1280. *
  1281. * Description:
  1282. * Initializes the unlabeled connection hash table and registers a network
  1283. * device notification handler. This function should only be called by the
  1284. * NetLabel subsystem itself during initialization. Returns zero on success,
  1285. * non-zero values on error.
  1286. *
  1287. */
  1288. int __init netlbl_unlabel_init(u32 size)
  1289. {
  1290. u32 iter;
  1291. struct netlbl_unlhsh_tbl *hsh_tbl;
  1292. if (size == 0)
  1293. return -EINVAL;
  1294. hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
  1295. if (hsh_tbl == NULL)
  1296. return -ENOMEM;
  1297. hsh_tbl->size = 1 << size;
  1298. hsh_tbl->tbl = kcalloc(hsh_tbl->size,
  1299. sizeof(struct list_head),
  1300. GFP_KERNEL);
  1301. if (hsh_tbl->tbl == NULL) {
  1302. kfree(hsh_tbl);
  1303. return -ENOMEM;
  1304. }
  1305. for (iter = 0; iter < hsh_tbl->size; iter++)
  1306. INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
  1307. spin_lock(&netlbl_unlhsh_lock);
  1308. rcu_assign_pointer(netlbl_unlhsh, hsh_tbl);
  1309. spin_unlock(&netlbl_unlhsh_lock);
  1310. register_netdevice_notifier(&netlbl_unlhsh_netdev_notifier);
  1311. return 0;
  1312. }
  1313. /**
  1314. * netlbl_unlabel_getattr - Get the security attributes for an unlabled packet
  1315. * @skb: the packet
  1316. * @family: protocol family
  1317. * @secattr: the security attributes
  1318. *
  1319. * Description:
  1320. * Determine the security attributes, if any, for an unlabled packet and return
  1321. * them in @secattr. Returns zero on success and negative values on failure.
  1322. *
  1323. */
  1324. int netlbl_unlabel_getattr(const struct sk_buff *skb,
  1325. u16 family,
  1326. struct netlbl_lsm_secattr *secattr)
  1327. {
  1328. struct netlbl_unlhsh_iface *iface;
  1329. rcu_read_lock();
  1330. iface = netlbl_unlhsh_search_iface(skb->skb_iif);
  1331. if (iface == NULL)
  1332. iface = rcu_dereference(netlbl_unlhsh_def);
  1333. if (iface == NULL || !iface->valid)
  1334. goto unlabel_getattr_nolabel;
  1335. #if IS_ENABLED(CONFIG_IPV6)
  1336. /* When resolving a fallback label, check the sk_buff version as
  1337. * it is possible (e.g. SCTP) to have family = PF_INET6 while
  1338. * receiving ip_hdr(skb)->version = 4.
  1339. */
  1340. if (family == PF_INET6 && ip_hdr(skb)->version == 4)
  1341. family = PF_INET;
  1342. #endif /* IPv6 */
  1343. switch (family) {
  1344. case PF_INET: {
  1345. struct iphdr *hdr4;
  1346. struct netlbl_af4list *addr4;
  1347. hdr4 = ip_hdr(skb);
  1348. addr4 = netlbl_af4list_search(hdr4->saddr,
  1349. &iface->addr4_list);
  1350. if (addr4 == NULL)
  1351. goto unlabel_getattr_nolabel;
  1352. secattr->attr.secid = netlbl_unlhsh_addr4_entry(addr4)->secid;
  1353. break;
  1354. }
  1355. #if IS_ENABLED(CONFIG_IPV6)
  1356. case PF_INET6: {
  1357. struct ipv6hdr *hdr6;
  1358. struct netlbl_af6list *addr6;
  1359. hdr6 = ipv6_hdr(skb);
  1360. addr6 = netlbl_af6list_search(&hdr6->saddr,
  1361. &iface->addr6_list);
  1362. if (addr6 == NULL)
  1363. goto unlabel_getattr_nolabel;
  1364. secattr->attr.secid = netlbl_unlhsh_addr6_entry(addr6)->secid;
  1365. break;
  1366. }
  1367. #endif /* IPv6 */
  1368. default:
  1369. goto unlabel_getattr_nolabel;
  1370. }
  1371. rcu_read_unlock();
  1372. secattr->flags |= NETLBL_SECATTR_SECID;
  1373. secattr->type = NETLBL_NLTYPE_UNLABELED;
  1374. return 0;
  1375. unlabel_getattr_nolabel:
  1376. rcu_read_unlock();
  1377. if (netlabel_unlabel_acceptflg == 0)
  1378. return -ENOMSG;
  1379. secattr->type = NETLBL_NLTYPE_UNLABELED;
  1380. return 0;
  1381. }
  1382. /**
  1383. * netlbl_unlabel_defconf - Set the default config to allow unlabeled packets
  1384. *
  1385. * Description:
  1386. * Set the default NetLabel configuration to allow incoming unlabeled packets
  1387. * and to send unlabeled network traffic by default.
  1388. *
  1389. */
  1390. int __init netlbl_unlabel_defconf(void)
  1391. {
  1392. int ret_val;
  1393. struct netlbl_dom_map *entry;
  1394. struct netlbl_audit audit_info;
  1395. /* Only the kernel is allowed to call this function and the only time
  1396. * it is called is at bootup before the audit subsystem is reporting
  1397. * messages so don't worry to much about these values. */
  1398. security_task_getsecid(current, &audit_info.secid);
  1399. audit_info.loginuid = GLOBAL_ROOT_UID;
  1400. audit_info.sessionid = 0;
  1401. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  1402. if (entry == NULL)
  1403. return -ENOMEM;
  1404. entry->def.type = NETLBL_NLTYPE_UNLABELED;
  1405. ret_val = netlbl_domhsh_add_default(entry, &audit_info);
  1406. if (ret_val != 0)
  1407. return ret_val;
  1408. netlbl_unlabel_acceptflg_set(1, &audit_info);
  1409. return 0;
  1410. }