netlabel_unlabeled.c 42 KB

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