netlabel_unlabeled.c 42 KB

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