netlabel_unlabeled.c 42 KB

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