netlabel_unlabeled.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  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. info->attrs[NLBL_UNLABEL_A_IPV4MASK]) {
  714. addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
  715. if (addr_len != sizeof(struct in_addr) &&
  716. addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV4MASK]))
  717. return -EINVAL;
  718. *len = addr_len;
  719. *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
  720. *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4MASK]);
  721. return 0;
  722. } else if (info->attrs[NLBL_UNLABEL_A_IPV6ADDR]) {
  723. addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
  724. if (addr_len != sizeof(struct in6_addr) &&
  725. addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV6MASK]))
  726. return -EINVAL;
  727. *len = addr_len;
  728. *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
  729. *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6MASK]);
  730. return 0;
  731. }
  732. return -EINVAL;
  733. }
  734. /*
  735. * NetLabel Command Handlers
  736. */
  737. /**
  738. * netlbl_unlabel_accept - Handle an ACCEPT message
  739. * @skb: the NETLINK buffer
  740. * @info: the Generic NETLINK info block
  741. *
  742. * Description:
  743. * Process a user generated ACCEPT message and set the accept flag accordingly.
  744. * Returns zero on success, negative values on failure.
  745. *
  746. */
  747. static int netlbl_unlabel_accept(struct sk_buff *skb, struct genl_info *info)
  748. {
  749. u8 value;
  750. struct netlbl_audit audit_info;
  751. if (info->attrs[NLBL_UNLABEL_A_ACPTFLG]) {
  752. value = nla_get_u8(info->attrs[NLBL_UNLABEL_A_ACPTFLG]);
  753. if (value == 1 || value == 0) {
  754. netlbl_netlink_auditinfo(skb, &audit_info);
  755. netlbl_unlabel_acceptflg_set(value, &audit_info);
  756. return 0;
  757. }
  758. }
  759. return -EINVAL;
  760. }
  761. /**
  762. * netlbl_unlabel_list - Handle a LIST message
  763. * @skb: the NETLINK buffer
  764. * @info: the Generic NETLINK info block
  765. *
  766. * Description:
  767. * Process a user generated LIST message and respond with the current status.
  768. * Returns zero on success, negative values on failure.
  769. *
  770. */
  771. static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info)
  772. {
  773. int ret_val = -EINVAL;
  774. struct sk_buff *ans_skb;
  775. void *data;
  776. ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  777. if (ans_skb == NULL)
  778. goto list_failure;
  779. data = genlmsg_put_reply(ans_skb, info, &netlbl_unlabel_gnl_family,
  780. 0, NLBL_UNLABEL_C_LIST);
  781. if (data == NULL) {
  782. ret_val = -ENOMEM;
  783. goto list_failure;
  784. }
  785. ret_val = nla_put_u8(ans_skb,
  786. NLBL_UNLABEL_A_ACPTFLG,
  787. netlabel_unlabel_acceptflg);
  788. if (ret_val != 0)
  789. goto list_failure;
  790. genlmsg_end(ans_skb, data);
  791. return genlmsg_reply(ans_skb, info);
  792. list_failure:
  793. kfree_skb(ans_skb);
  794. return ret_val;
  795. }
  796. /**
  797. * netlbl_unlabel_staticadd - Handle a STATICADD message
  798. * @skb: the NETLINK buffer
  799. * @info: the Generic NETLINK info block
  800. *
  801. * Description:
  802. * Process a user generated STATICADD message and add a new unlabeled
  803. * connection entry to the hash table. Returns zero on success, negative
  804. * values on failure.
  805. *
  806. */
  807. static int netlbl_unlabel_staticadd(struct sk_buff *skb,
  808. struct genl_info *info)
  809. {
  810. int ret_val;
  811. char *dev_name;
  812. void *addr;
  813. void *mask;
  814. u32 addr_len;
  815. u32 secid;
  816. struct netlbl_audit audit_info;
  817. /* Don't allow users to add both IPv4 and IPv6 addresses for a
  818. * single entry. However, allow users to create two entries, one each
  819. * for IPv4 and IPv4, with the same LSM security context which should
  820. * achieve the same result. */
  821. if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
  822. !info->attrs[NLBL_UNLABEL_A_IFACE] ||
  823. !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
  824. !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
  825. (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
  826. !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
  827. return -EINVAL;
  828. netlbl_netlink_auditinfo(skb, &audit_info);
  829. ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
  830. if (ret_val != 0)
  831. return ret_val;
  832. dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
  833. ret_val = security_secctx_to_secid(
  834. nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
  835. nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
  836. &secid);
  837. if (ret_val != 0)
  838. return ret_val;
  839. return netlbl_unlhsh_add(&init_net,
  840. dev_name, addr, mask, addr_len, secid,
  841. &audit_info);
  842. }
  843. /**
  844. * netlbl_unlabel_staticadddef - Handle a STATICADDDEF message
  845. * @skb: the NETLINK buffer
  846. * @info: the Generic NETLINK info block
  847. *
  848. * Description:
  849. * Process a user generated STATICADDDEF message and add a new default
  850. * unlabeled connection entry. Returns zero on success, negative values on
  851. * failure.
  852. *
  853. */
  854. static int netlbl_unlabel_staticadddef(struct sk_buff *skb,
  855. struct genl_info *info)
  856. {
  857. int ret_val;
  858. void *addr;
  859. void *mask;
  860. u32 addr_len;
  861. u32 secid;
  862. struct netlbl_audit audit_info;
  863. /* Don't allow users to add both IPv4 and IPv6 addresses for a
  864. * single entry. However, allow users to create two entries, one each
  865. * for IPv4 and IPv6, with the same LSM security context which should
  866. * achieve the same result. */
  867. if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
  868. !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
  869. !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
  870. (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
  871. !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
  872. return -EINVAL;
  873. netlbl_netlink_auditinfo(skb, &audit_info);
  874. ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
  875. if (ret_val != 0)
  876. return ret_val;
  877. ret_val = security_secctx_to_secid(
  878. nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
  879. nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
  880. &secid);
  881. if (ret_val != 0)
  882. return ret_val;
  883. return netlbl_unlhsh_add(&init_net,
  884. NULL, addr, mask, addr_len, secid,
  885. &audit_info);
  886. }
  887. /**
  888. * netlbl_unlabel_staticremove - Handle a STATICREMOVE message
  889. * @skb: the NETLINK buffer
  890. * @info: the Generic NETLINK info block
  891. *
  892. * Description:
  893. * Process a user generated STATICREMOVE message and remove the specified
  894. * unlabeled connection entry. Returns zero on success, negative values on
  895. * failure.
  896. *
  897. */
  898. static int netlbl_unlabel_staticremove(struct sk_buff *skb,
  899. struct genl_info *info)
  900. {
  901. int ret_val;
  902. char *dev_name;
  903. void *addr;
  904. void *mask;
  905. u32 addr_len;
  906. struct netlbl_audit audit_info;
  907. /* See the note in netlbl_unlabel_staticadd() about not allowing both
  908. * IPv4 and IPv6 in the same entry. */
  909. if (!info->attrs[NLBL_UNLABEL_A_IFACE] ||
  910. !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
  911. !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
  912. (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
  913. !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
  914. return -EINVAL;
  915. netlbl_netlink_auditinfo(skb, &audit_info);
  916. ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
  917. if (ret_val != 0)
  918. return ret_val;
  919. dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
  920. return netlbl_unlhsh_remove(&init_net,
  921. dev_name, addr, mask, addr_len,
  922. &audit_info);
  923. }
  924. /**
  925. * netlbl_unlabel_staticremovedef - Handle a STATICREMOVEDEF message
  926. * @skb: the NETLINK buffer
  927. * @info: the Generic NETLINK info block
  928. *
  929. * Description:
  930. * Process a user generated STATICREMOVEDEF message and remove the default
  931. * unlabeled connection entry. Returns zero on success, negative values on
  932. * failure.
  933. *
  934. */
  935. static int netlbl_unlabel_staticremovedef(struct sk_buff *skb,
  936. struct genl_info *info)
  937. {
  938. int ret_val;
  939. void *addr;
  940. void *mask;
  941. u32 addr_len;
  942. struct netlbl_audit audit_info;
  943. /* See the note in netlbl_unlabel_staticadd() about not allowing both
  944. * IPv4 and IPv6 in the same entry. */
  945. if (!((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
  946. !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
  947. (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
  948. !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
  949. return -EINVAL;
  950. netlbl_netlink_auditinfo(skb, &audit_info);
  951. ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
  952. if (ret_val != 0)
  953. return ret_val;
  954. return netlbl_unlhsh_remove(&init_net,
  955. NULL, addr, mask, addr_len,
  956. &audit_info);
  957. }
  958. /**
  959. * netlbl_unlabel_staticlist_gen - Generate messages for STATICLIST[DEF]
  960. * @cmd: command/message
  961. * @iface: the interface entry
  962. * @addr4: the IPv4 address entry
  963. * @addr6: the IPv6 address entry
  964. * @arg: the netlbl_unlhsh_walk_arg structure
  965. *
  966. * Description:
  967. * This function is designed to be used to generate a response for a
  968. * STATICLIST or STATICLISTDEF message. When called either @addr4 or @addr6
  969. * can be specified, not both, the other unspecified entry should be set to
  970. * NULL by the caller. Returns the size of the message on success, negative
  971. * values on failure.
  972. *
  973. */
  974. static int netlbl_unlabel_staticlist_gen(u32 cmd,
  975. const struct netlbl_unlhsh_iface *iface,
  976. const struct netlbl_unlhsh_addr4 *addr4,
  977. const struct netlbl_unlhsh_addr6 *addr6,
  978. void *arg)
  979. {
  980. int ret_val = -ENOMEM;
  981. struct netlbl_unlhsh_walk_arg *cb_arg = arg;
  982. struct net_device *dev;
  983. void *data;
  984. u32 secid;
  985. char *secctx;
  986. u32 secctx_len;
  987. data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).portid,
  988. cb_arg->seq, &netlbl_unlabel_gnl_family,
  989. NLM_F_MULTI, cmd);
  990. if (data == NULL)
  991. goto list_cb_failure;
  992. if (iface->ifindex > 0) {
  993. dev = dev_get_by_index(&init_net, iface->ifindex);
  994. if (!dev) {
  995. ret_val = -ENODEV;
  996. goto list_cb_failure;
  997. }
  998. ret_val = nla_put_string(cb_arg->skb,
  999. NLBL_UNLABEL_A_IFACE, dev->name);
  1000. dev_put(dev);
  1001. if (ret_val != 0)
  1002. goto list_cb_failure;
  1003. }
  1004. if (addr4) {
  1005. struct in_addr addr_struct;
  1006. addr_struct.s_addr = addr4->list.addr;
  1007. ret_val = nla_put_in_addr(cb_arg->skb,
  1008. NLBL_UNLABEL_A_IPV4ADDR,
  1009. addr_struct.s_addr);
  1010. if (ret_val != 0)
  1011. goto list_cb_failure;
  1012. addr_struct.s_addr = addr4->list.mask;
  1013. ret_val = nla_put_in_addr(cb_arg->skb,
  1014. NLBL_UNLABEL_A_IPV4MASK,
  1015. addr_struct.s_addr);
  1016. if (ret_val != 0)
  1017. goto list_cb_failure;
  1018. secid = addr4->secid;
  1019. } else {
  1020. ret_val = nla_put_in6_addr(cb_arg->skb,
  1021. NLBL_UNLABEL_A_IPV6ADDR,
  1022. &addr6->list.addr);
  1023. if (ret_val != 0)
  1024. goto list_cb_failure;
  1025. ret_val = nla_put_in6_addr(cb_arg->skb,
  1026. NLBL_UNLABEL_A_IPV6MASK,
  1027. &addr6->list.mask);
  1028. if (ret_val != 0)
  1029. goto list_cb_failure;
  1030. secid = addr6->secid;
  1031. }
  1032. ret_val = security_secid_to_secctx(secid, &secctx, &secctx_len);
  1033. if (ret_val != 0)
  1034. goto list_cb_failure;
  1035. ret_val = nla_put(cb_arg->skb,
  1036. NLBL_UNLABEL_A_SECCTX,
  1037. secctx_len,
  1038. secctx);
  1039. security_release_secctx(secctx, secctx_len);
  1040. if (ret_val != 0)
  1041. goto list_cb_failure;
  1042. cb_arg->seq++;
  1043. genlmsg_end(cb_arg->skb, data);
  1044. return 0;
  1045. list_cb_failure:
  1046. genlmsg_cancel(cb_arg->skb, data);
  1047. return ret_val;
  1048. }
  1049. /**
  1050. * netlbl_unlabel_staticlist - Handle a STATICLIST message
  1051. * @skb: the NETLINK buffer
  1052. * @cb: the NETLINK callback
  1053. *
  1054. * Description:
  1055. * Process a user generated STATICLIST message and dump the unlabeled
  1056. * connection hash table in a form suitable for use in a kernel generated
  1057. * STATICLIST message. Returns the length of @skb.
  1058. *
  1059. */
  1060. static int netlbl_unlabel_staticlist(struct sk_buff *skb,
  1061. struct netlink_callback *cb)
  1062. {
  1063. struct netlbl_unlhsh_walk_arg cb_arg;
  1064. u32 skip_bkt = cb->args[0];
  1065. u32 skip_chain = cb->args[1];
  1066. u32 iter_bkt;
  1067. u32 iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0;
  1068. struct netlbl_unlhsh_iface *iface;
  1069. struct list_head *iter_list;
  1070. struct netlbl_af4list *addr4;
  1071. #if IS_ENABLED(CONFIG_IPV6)
  1072. struct netlbl_af6list *addr6;
  1073. #endif
  1074. cb_arg.nl_cb = cb;
  1075. cb_arg.skb = skb;
  1076. cb_arg.seq = cb->nlh->nlmsg_seq;
  1077. rcu_read_lock();
  1078. for (iter_bkt = skip_bkt;
  1079. iter_bkt < rcu_dereference(netlbl_unlhsh)->size;
  1080. iter_bkt++, iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0) {
  1081. iter_list = &rcu_dereference(netlbl_unlhsh)->tbl[iter_bkt];
  1082. list_for_each_entry_rcu(iface, iter_list, list) {
  1083. if (!iface->valid ||
  1084. iter_chain++ < skip_chain)
  1085. continue;
  1086. netlbl_af4list_foreach_rcu(addr4,
  1087. &iface->addr4_list) {
  1088. if (iter_addr4++ < cb->args[2])
  1089. continue;
  1090. if (netlbl_unlabel_staticlist_gen(
  1091. NLBL_UNLABEL_C_STATICLIST,
  1092. iface,
  1093. netlbl_unlhsh_addr4_entry(addr4),
  1094. NULL,
  1095. &cb_arg) < 0) {
  1096. iter_addr4--;
  1097. iter_chain--;
  1098. goto unlabel_staticlist_return;
  1099. }
  1100. }
  1101. #if IS_ENABLED(CONFIG_IPV6)
  1102. netlbl_af6list_foreach_rcu(addr6,
  1103. &iface->addr6_list) {
  1104. if (iter_addr6++ < cb->args[3])
  1105. continue;
  1106. if (netlbl_unlabel_staticlist_gen(
  1107. NLBL_UNLABEL_C_STATICLIST,
  1108. iface,
  1109. NULL,
  1110. netlbl_unlhsh_addr6_entry(addr6),
  1111. &cb_arg) < 0) {
  1112. iter_addr6--;
  1113. iter_chain--;
  1114. goto unlabel_staticlist_return;
  1115. }
  1116. }
  1117. #endif /* IPv6 */
  1118. }
  1119. }
  1120. unlabel_staticlist_return:
  1121. rcu_read_unlock();
  1122. cb->args[0] = iter_bkt;
  1123. cb->args[1] = iter_chain;
  1124. cb->args[2] = iter_addr4;
  1125. cb->args[3] = iter_addr6;
  1126. return skb->len;
  1127. }
  1128. /**
  1129. * netlbl_unlabel_staticlistdef - Handle a STATICLISTDEF message
  1130. * @skb: the NETLINK buffer
  1131. * @cb: the NETLINK callback
  1132. *
  1133. * Description:
  1134. * Process a user generated STATICLISTDEF message and dump the default
  1135. * unlabeled connection entry in a form suitable for use in a kernel generated
  1136. * STATICLISTDEF message. Returns the length of @skb.
  1137. *
  1138. */
  1139. static int netlbl_unlabel_staticlistdef(struct sk_buff *skb,
  1140. struct netlink_callback *cb)
  1141. {
  1142. struct netlbl_unlhsh_walk_arg cb_arg;
  1143. struct netlbl_unlhsh_iface *iface;
  1144. u32 iter_addr4 = 0, iter_addr6 = 0;
  1145. struct netlbl_af4list *addr4;
  1146. #if IS_ENABLED(CONFIG_IPV6)
  1147. struct netlbl_af6list *addr6;
  1148. #endif
  1149. cb_arg.nl_cb = cb;
  1150. cb_arg.skb = skb;
  1151. cb_arg.seq = cb->nlh->nlmsg_seq;
  1152. rcu_read_lock();
  1153. iface = rcu_dereference(netlbl_unlhsh_def);
  1154. if (iface == NULL || !iface->valid)
  1155. goto unlabel_staticlistdef_return;
  1156. netlbl_af4list_foreach_rcu(addr4, &iface->addr4_list) {
  1157. if (iter_addr4++ < cb->args[0])
  1158. continue;
  1159. if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
  1160. iface,
  1161. netlbl_unlhsh_addr4_entry(addr4),
  1162. NULL,
  1163. &cb_arg) < 0) {
  1164. iter_addr4--;
  1165. goto unlabel_staticlistdef_return;
  1166. }
  1167. }
  1168. #if IS_ENABLED(CONFIG_IPV6)
  1169. netlbl_af6list_foreach_rcu(addr6, &iface->addr6_list) {
  1170. if (iter_addr6++ < cb->args[1])
  1171. continue;
  1172. if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
  1173. iface,
  1174. NULL,
  1175. netlbl_unlhsh_addr6_entry(addr6),
  1176. &cb_arg) < 0) {
  1177. iter_addr6--;
  1178. goto unlabel_staticlistdef_return;
  1179. }
  1180. }
  1181. #endif /* IPv6 */
  1182. unlabel_staticlistdef_return:
  1183. rcu_read_unlock();
  1184. cb->args[0] = iter_addr4;
  1185. cb->args[1] = iter_addr6;
  1186. return skb->len;
  1187. }
  1188. /*
  1189. * NetLabel Generic NETLINK Command Definitions
  1190. */
  1191. static const struct genl_ops netlbl_unlabel_genl_ops[] = {
  1192. {
  1193. .cmd = NLBL_UNLABEL_C_STATICADD,
  1194. .flags = GENL_ADMIN_PERM,
  1195. .policy = netlbl_unlabel_genl_policy,
  1196. .doit = netlbl_unlabel_staticadd,
  1197. .dumpit = NULL,
  1198. },
  1199. {
  1200. .cmd = NLBL_UNLABEL_C_STATICREMOVE,
  1201. .flags = GENL_ADMIN_PERM,
  1202. .policy = netlbl_unlabel_genl_policy,
  1203. .doit = netlbl_unlabel_staticremove,
  1204. .dumpit = NULL,
  1205. },
  1206. {
  1207. .cmd = NLBL_UNLABEL_C_STATICLIST,
  1208. .flags = 0,
  1209. .policy = netlbl_unlabel_genl_policy,
  1210. .doit = NULL,
  1211. .dumpit = netlbl_unlabel_staticlist,
  1212. },
  1213. {
  1214. .cmd = NLBL_UNLABEL_C_STATICADDDEF,
  1215. .flags = GENL_ADMIN_PERM,
  1216. .policy = netlbl_unlabel_genl_policy,
  1217. .doit = netlbl_unlabel_staticadddef,
  1218. .dumpit = NULL,
  1219. },
  1220. {
  1221. .cmd = NLBL_UNLABEL_C_STATICREMOVEDEF,
  1222. .flags = GENL_ADMIN_PERM,
  1223. .policy = netlbl_unlabel_genl_policy,
  1224. .doit = netlbl_unlabel_staticremovedef,
  1225. .dumpit = NULL,
  1226. },
  1227. {
  1228. .cmd = NLBL_UNLABEL_C_STATICLISTDEF,
  1229. .flags = 0,
  1230. .policy = netlbl_unlabel_genl_policy,
  1231. .doit = NULL,
  1232. .dumpit = netlbl_unlabel_staticlistdef,
  1233. },
  1234. {
  1235. .cmd = NLBL_UNLABEL_C_ACCEPT,
  1236. .flags = GENL_ADMIN_PERM,
  1237. .policy = netlbl_unlabel_genl_policy,
  1238. .doit = netlbl_unlabel_accept,
  1239. .dumpit = NULL,
  1240. },
  1241. {
  1242. .cmd = NLBL_UNLABEL_C_LIST,
  1243. .flags = 0,
  1244. .policy = netlbl_unlabel_genl_policy,
  1245. .doit = netlbl_unlabel_list,
  1246. .dumpit = NULL,
  1247. },
  1248. };
  1249. static struct genl_family netlbl_unlabel_gnl_family __ro_after_init = {
  1250. .hdrsize = 0,
  1251. .name = NETLBL_NLTYPE_UNLABELED_NAME,
  1252. .version = NETLBL_PROTO_VERSION,
  1253. .maxattr = NLBL_UNLABEL_A_MAX,
  1254. .module = THIS_MODULE,
  1255. .ops = netlbl_unlabel_genl_ops,
  1256. .n_ops = ARRAY_SIZE(netlbl_unlabel_genl_ops),
  1257. };
  1258. /*
  1259. * NetLabel Generic NETLINK Protocol Functions
  1260. */
  1261. /**
  1262. * netlbl_unlabel_genl_init - Register the Unlabeled NetLabel component
  1263. *
  1264. * Description:
  1265. * Register the unlabeled packet NetLabel component with the Generic NETLINK
  1266. * mechanism. Returns zero on success, negative values on failure.
  1267. *
  1268. */
  1269. int __init netlbl_unlabel_genl_init(void)
  1270. {
  1271. return genl_register_family(&netlbl_unlabel_gnl_family);
  1272. }
  1273. /*
  1274. * NetLabel KAPI Hooks
  1275. */
  1276. static struct notifier_block netlbl_unlhsh_netdev_notifier = {
  1277. .notifier_call = netlbl_unlhsh_netdev_handler,
  1278. };
  1279. /**
  1280. * netlbl_unlabel_init - Initialize the unlabeled connection hash table
  1281. * @size: the number of bits to use for the hash buckets
  1282. *
  1283. * Description:
  1284. * Initializes the unlabeled connection hash table and registers a network
  1285. * device notification handler. This function should only be called by the
  1286. * NetLabel subsystem itself during initialization. Returns zero on success,
  1287. * non-zero values on error.
  1288. *
  1289. */
  1290. int __init netlbl_unlabel_init(u32 size)
  1291. {
  1292. u32 iter;
  1293. struct netlbl_unlhsh_tbl *hsh_tbl;
  1294. if (size == 0)
  1295. return -EINVAL;
  1296. hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
  1297. if (hsh_tbl == NULL)
  1298. return -ENOMEM;
  1299. hsh_tbl->size = 1 << size;
  1300. hsh_tbl->tbl = kcalloc(hsh_tbl->size,
  1301. sizeof(struct list_head),
  1302. GFP_KERNEL);
  1303. if (hsh_tbl->tbl == NULL) {
  1304. kfree(hsh_tbl);
  1305. return -ENOMEM;
  1306. }
  1307. for (iter = 0; iter < hsh_tbl->size; iter++)
  1308. INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
  1309. spin_lock(&netlbl_unlhsh_lock);
  1310. rcu_assign_pointer(netlbl_unlhsh, hsh_tbl);
  1311. spin_unlock(&netlbl_unlhsh_lock);
  1312. register_netdevice_notifier(&netlbl_unlhsh_netdev_notifier);
  1313. return 0;
  1314. }
  1315. /**
  1316. * netlbl_unlabel_getattr - Get the security attributes for an unlabled packet
  1317. * @skb: the packet
  1318. * @family: protocol family
  1319. * @secattr: the security attributes
  1320. *
  1321. * Description:
  1322. * Determine the security attributes, if any, for an unlabled packet and return
  1323. * them in @secattr. Returns zero on success and negative values on failure.
  1324. *
  1325. */
  1326. int netlbl_unlabel_getattr(const struct sk_buff *skb,
  1327. u16 family,
  1328. struct netlbl_lsm_secattr *secattr)
  1329. {
  1330. struct netlbl_unlhsh_iface *iface;
  1331. rcu_read_lock();
  1332. iface = netlbl_unlhsh_search_iface(skb->skb_iif);
  1333. if (iface == NULL)
  1334. iface = rcu_dereference(netlbl_unlhsh_def);
  1335. if (iface == NULL || !iface->valid)
  1336. goto unlabel_getattr_nolabel;
  1337. #if IS_ENABLED(CONFIG_IPV6)
  1338. /* When resolving a fallback label, check the sk_buff version as
  1339. * it is possible (e.g. SCTP) to have family = PF_INET6 while
  1340. * receiving ip_hdr(skb)->version = 4.
  1341. */
  1342. if (family == PF_INET6 && ip_hdr(skb)->version == 4)
  1343. family = PF_INET;
  1344. #endif /* IPv6 */
  1345. switch (family) {
  1346. case PF_INET: {
  1347. struct iphdr *hdr4;
  1348. struct netlbl_af4list *addr4;
  1349. hdr4 = ip_hdr(skb);
  1350. addr4 = netlbl_af4list_search(hdr4->saddr,
  1351. &iface->addr4_list);
  1352. if (addr4 == NULL)
  1353. goto unlabel_getattr_nolabel;
  1354. secattr->attr.secid = netlbl_unlhsh_addr4_entry(addr4)->secid;
  1355. break;
  1356. }
  1357. #if IS_ENABLED(CONFIG_IPV6)
  1358. case PF_INET6: {
  1359. struct ipv6hdr *hdr6;
  1360. struct netlbl_af6list *addr6;
  1361. hdr6 = ipv6_hdr(skb);
  1362. addr6 = netlbl_af6list_search(&hdr6->saddr,
  1363. &iface->addr6_list);
  1364. if (addr6 == NULL)
  1365. goto unlabel_getattr_nolabel;
  1366. secattr->attr.secid = netlbl_unlhsh_addr6_entry(addr6)->secid;
  1367. break;
  1368. }
  1369. #endif /* IPv6 */
  1370. default:
  1371. goto unlabel_getattr_nolabel;
  1372. }
  1373. rcu_read_unlock();
  1374. secattr->flags |= NETLBL_SECATTR_SECID;
  1375. secattr->type = NETLBL_NLTYPE_UNLABELED;
  1376. return 0;
  1377. unlabel_getattr_nolabel:
  1378. rcu_read_unlock();
  1379. if (netlabel_unlabel_acceptflg == 0)
  1380. return -ENOMSG;
  1381. secattr->type = NETLBL_NLTYPE_UNLABELED;
  1382. return 0;
  1383. }
  1384. /**
  1385. * netlbl_unlabel_defconf - Set the default config to allow unlabeled packets
  1386. *
  1387. * Description:
  1388. * Set the default NetLabel configuration to allow incoming unlabeled packets
  1389. * and to send unlabeled network traffic by default.
  1390. *
  1391. */
  1392. int __init netlbl_unlabel_defconf(void)
  1393. {
  1394. int ret_val;
  1395. struct netlbl_dom_map *entry;
  1396. struct netlbl_audit audit_info;
  1397. /* Only the kernel is allowed to call this function and the only time
  1398. * it is called is at bootup before the audit subsystem is reporting
  1399. * messages so don't worry to much about these values. */
  1400. security_task_getsecid(current, &audit_info.secid);
  1401. audit_info.loginuid = GLOBAL_ROOT_UID;
  1402. audit_info.sessionid = 0;
  1403. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  1404. if (entry == NULL)
  1405. return -ENOMEM;
  1406. entry->family = AF_UNSPEC;
  1407. entry->def.type = NETLBL_NLTYPE_UNLABELED;
  1408. ret_val = netlbl_domhsh_add_default(entry, &audit_info);
  1409. if (ret_val != 0)
  1410. return ret_val;
  1411. netlbl_unlabel_acceptflg_set(1, &audit_info);
  1412. return 0;
  1413. }