netlabel_unlabeled.c 42 KB

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