netlabel_unlabeled.c 42 KB

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