netlabel_unlabeled.c 42 KB

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