netlabel_unlabeled.c 41.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2 3 4 5 6 7 8
/*
 * NetLabel Unlabeled Support
 *
 * This file defines functions for dealing with unlabeled packets for the
 * NetLabel system.  The NetLabel system manages static and dynamic label
 * mappings for network protocols such as CIPSO and RIPSO.
 *
9
 * Author: Paul Moore <paul@paul-moore.com>
10 11 12
 */

/*
13
 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006 - 2008
14 15 16
 */

#include <linux/types.h>
17
#include <linux/rcupdate.h>
18 19 20 21 22
#include <linux/list.h>
#include <linux/spinlock.h>
#include <linux/socket.h>
#include <linux/string.h>
#include <linux/skbuff.h>
23
#include <linux/audit.h>
24 25 26 27 28 29 30
#include <linux/in.h>
#include <linux/in6.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
#include <linux/notifier.h>
#include <linux/netdevice.h>
#include <linux/security.h>
31
#include <linux/slab.h>
32 33 34
#include <net/sock.h>
#include <net/netlink.h>
#include <net/genetlink.h>
35 36 37
#include <net/ip.h>
#include <net/ipv6.h>
#include <net/net_namespace.h>
38 39
#include <net/netlabel.h>
#include <asm/bug.h>
A
Arun Sharma 已提交
40
#include <linux/atomic.h>
41 42

#include "netlabel_user.h"
43
#include "netlabel_addrlist.h"
44 45
#include "netlabel_domainhash.h"
#include "netlabel_unlabeled.h"
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#include "netlabel_mgmt.h"

/* NOTE: at present we always use init's network namespace since we don't
 *       presently support different namespaces even though the majority of
 *       the functions in this file are "namespace safe" */

/* The unlabeled connection hash table which we use to map network interfaces
 * and addresses of unlabeled packets to a user specified secid value for the
 * LSM.  The hash table is used to lookup the network interface entry
 * (struct netlbl_unlhsh_iface) and then the interface entry is used to
 * lookup an IP address match from an ordered list.  If a network interface
 * match can not be found in the hash table then the default entry
 * (netlbl_unlhsh_def) is used.  The IP address entry list
 * (struct netlbl_unlhsh_addr) is ordered such that the entries with a
 * larger netmask come first.
 */
struct netlbl_unlhsh_tbl {
	struct list_head *tbl;
	u32 size;
};
66 67
#define netlbl_unlhsh_addr4_entry(iter) \
	container_of(iter, struct netlbl_unlhsh_addr4, list)
68 69 70
struct netlbl_unlhsh_addr4 {
	u32 secid;

71
	struct netlbl_af4list list;
72 73
	struct rcu_head rcu;
};
74 75
#define netlbl_unlhsh_addr6_entry(iter) \
	container_of(iter, struct netlbl_unlhsh_addr6, list)
76 77 78
struct netlbl_unlhsh_addr6 {
	u32 secid;

79
	struct netlbl_af6list list;
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	struct rcu_head rcu;
};
struct netlbl_unlhsh_iface {
	int ifindex;
	struct list_head addr4_list;
	struct list_head addr6_list;

	u32 valid;
	struct list_head list;
	struct rcu_head rcu;
};

/* Argument struct for netlbl_unlhsh_walk() */
struct netlbl_unlhsh_walk_arg {
	struct netlink_callback *nl_cb;
	struct sk_buff *skb;
	u32 seq;
};

/* Unlabeled connection hash table */
/* updates should be so rare that having one spinlock for the entire
 * hash table should be okay */
static DEFINE_SPINLOCK(netlbl_unlhsh_lock);
103
#define netlbl_unlhsh_rcu_deref(p) \
104
	rcu_dereference_check(p, lockdep_is_held(&netlbl_unlhsh_lock))
105 106
static struct netlbl_unlhsh_tbl __rcu *netlbl_unlhsh;
static struct netlbl_unlhsh_iface __rcu *netlbl_unlhsh_def;
107 108

/* Accept unlabeled packets flag */
109
static u8 netlabel_unlabel_acceptflg;
110

111
/* NetLabel Generic NETLINK unlabeled family */
112
static struct genl_family netlbl_unlabel_gnl_family;
113

114
/* NetLabel Netlink attribute policy */
115
static const struct nla_policy netlbl_unlabel_genl_policy[NLBL_UNLABEL_A_MAX + 1] = {
116
	[NLBL_UNLABEL_A_ACPTFLG] = { .type = NLA_U8 },
117 118 119 120 121 122 123 124 125 126 127
	[NLBL_UNLABEL_A_IPV6ADDR] = { .type = NLA_BINARY,
				      .len = sizeof(struct in6_addr) },
	[NLBL_UNLABEL_A_IPV6MASK] = { .type = NLA_BINARY,
				      .len = sizeof(struct in6_addr) },
	[NLBL_UNLABEL_A_IPV4ADDR] = { .type = NLA_BINARY,
				      .len = sizeof(struct in_addr) },
	[NLBL_UNLABEL_A_IPV4MASK] = { .type = NLA_BINARY,
				      .len = sizeof(struct in_addr) },
	[NLBL_UNLABEL_A_IFACE] = { .type = NLA_NUL_STRING,
				   .len = IFNAMSIZ - 1 },
	[NLBL_UNLABEL_A_SECCTX] = { .type = NLA_BINARY }
128
};
129

130
/*
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
 * Unlabeled Connection Hash Table Functions
 */

/**
 * netlbl_unlhsh_free_iface - Frees an interface entry from the hash table
 * @entry: the entry's RCU field
 *
 * Description:
 * This function is designed to be used as a callback to the call_rcu()
 * function so that memory allocated to a hash table interface entry can be
 * released safely.  It is important to note that this function does not free
 * the IPv4 and IPv6 address lists contained as part of an interface entry.  It
 * is up to the rest of the code to make sure an interface entry is only freed
 * once it's address lists are empty.
 *
 */
static void netlbl_unlhsh_free_iface(struct rcu_head *entry)
{
	struct netlbl_unlhsh_iface *iface;
150 151
	struct netlbl_af4list *iter4;
	struct netlbl_af4list *tmp4;
E
Eric Dumazet 已提交
152
#if IS_ENABLED(CONFIG_IPV6)
153 154 155
	struct netlbl_af6list *iter6;
	struct netlbl_af6list *tmp6;
#endif /* IPv6 */
156 157 158 159 160 161

	iface = container_of(entry, struct netlbl_unlhsh_iface, rcu);

	/* no need for locks here since we are the only one with access to this
	 * structure */

162 163 164 165
	netlbl_af4list_foreach_safe(iter4, tmp4, &iface->addr4_list) {
		netlbl_af4list_remove_entry(iter4);
		kfree(netlbl_unlhsh_addr4_entry(iter4));
	}
E
Eric Dumazet 已提交
166
#if IS_ENABLED(CONFIG_IPV6)
167 168 169 170 171
	netlbl_af6list_foreach_safe(iter6, tmp6, &iface->addr6_list) {
		netlbl_af6list_remove_entry(iter6);
		kfree(netlbl_unlhsh_addr6_entry(iter6));
	}
#endif /* IPv6 */
172 173 174 175 176 177 178 179 180 181
	kfree(iface);
}

/**
 * netlbl_unlhsh_hash - Hashing function for the hash table
 * @ifindex: the network interface/device to hash
 *
 * Description:
 * This is the hashing function for the unlabeled hash table, it returns the
 * bucket number for the given device/interface.  The caller is responsible for
182 183
 * ensuring that the hash table is protected with either a RCU read lock or
 * the hash table lock.
184 185 186 187
 *
 */
static u32 netlbl_unlhsh_hash(int ifindex)
{
188
	return ifindex & (netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->size - 1);
189 190 191 192 193 194 195 196 197
}

/**
 * netlbl_unlhsh_search_iface - Search for a matching interface entry
 * @ifindex: the network interface
 *
 * Description:
 * Searches the unlabeled connection hash table and returns a pointer to the
 * interface entry which matches @ifindex, otherwise NULL is returned.  The
198 199
 * caller is responsible for ensuring that the hash table is protected with
 * either a RCU read lock or the hash table lock.
200 201 202 203 204
 *
 */
static struct netlbl_unlhsh_iface *netlbl_unlhsh_search_iface(int ifindex)
{
	u32 bkt;
P
Paul Moore 已提交
205
	struct list_head *bkt_list;
206 207 208
	struct netlbl_unlhsh_iface *iter;

	bkt = netlbl_unlhsh_hash(ifindex);
209
	bkt_list = &netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->tbl[bkt];
210 211
	list_for_each_entry_rcu(iter, bkt_list, list,
				lockdep_is_held(&netlbl_unlhsh_lock))
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
		if (iter->valid && iter->ifindex == ifindex)
			return iter;

	return NULL;
}

/**
 * netlbl_unlhsh_add_addr4 - Add a new IPv4 address entry to the hash table
 * @iface: the associated interface entry
 * @addr: IPv4 address in network byte order
 * @mask: IPv4 address mask in network byte order
 * @secid: LSM secid value for entry
 *
 * Description:
 * Add a new address entry into the unlabeled connection hash table using the
 * interface entry specified by @iface.  On success zero is returned, otherwise
228
 * a negative value is returned.
229
 *
230
 */
231 232 233 234 235
static int netlbl_unlhsh_add_addr4(struct netlbl_unlhsh_iface *iface,
				   const struct in_addr *addr,
				   const struct in_addr *mask,
				   u32 secid)
{
236
	int ret_val;
237 238 239 240 241 242
	struct netlbl_unlhsh_addr4 *entry;

	entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
	if (entry == NULL)
		return -ENOMEM;

243 244 245 246
	entry->list.addr = addr->s_addr & mask->s_addr;
	entry->list.mask = mask->s_addr;
	entry->list.valid = 1;
	entry->secid = secid;
247 248

	spin_lock(&netlbl_unlhsh_lock);
249
	ret_val = netlbl_af4list_add(&entry->list, &iface->addr4_list);
250
	spin_unlock(&netlbl_unlhsh_lock);
251 252 253 254

	if (ret_val != 0)
		kfree(entry);
	return ret_val;
255 256
}

E
Eric Dumazet 已提交
257
#if IS_ENABLED(CONFIG_IPV6)
258 259 260 261 262 263 264 265 266 267
/**
 * netlbl_unlhsh_add_addr6 - Add a new IPv6 address entry to the hash table
 * @iface: the associated interface entry
 * @addr: IPv6 address in network byte order
 * @mask: IPv6 address mask in network byte order
 * @secid: LSM secid value for entry
 *
 * Description:
 * Add a new address entry into the unlabeled connection hash table using the
 * interface entry specified by @iface.  On success zero is returned, otherwise
268
 * a negative value is returned.
269 270 271 272 273 274 275
 *
 */
static int netlbl_unlhsh_add_addr6(struct netlbl_unlhsh_iface *iface,
				   const struct in6_addr *addr,
				   const struct in6_addr *mask,
				   u32 secid)
{
276
	int ret_val;
277 278 279 280 281 282
	struct netlbl_unlhsh_addr6 *entry;

	entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
	if (entry == NULL)
		return -ENOMEM;

A
Alexey Dobriyan 已提交
283
	entry->list.addr = *addr;
284 285 286 287
	entry->list.addr.s6_addr32[0] &= mask->s6_addr32[0];
	entry->list.addr.s6_addr32[1] &= mask->s6_addr32[1];
	entry->list.addr.s6_addr32[2] &= mask->s6_addr32[2];
	entry->list.addr.s6_addr32[3] &= mask->s6_addr32[3];
A
Alexey Dobriyan 已提交
288
	entry->list.mask = *mask;
289 290
	entry->list.valid = 1;
	entry->secid = secid;
291 292

	spin_lock(&netlbl_unlhsh_lock);
293
	ret_val = netlbl_af6list_add(&entry->list, &iface->addr6_list);
294
	spin_unlock(&netlbl_unlhsh_lock);
295 296 297

	if (ret_val != 0)
		kfree(entry);
298 299 300 301 302 303 304 305 306 307 308
	return 0;
}
#endif /* IPv6 */

/**
 * netlbl_unlhsh_add_iface - Adds a new interface entry to the hash table
 * @ifindex: network interface
 *
 * Description:
 * Add a new, empty, interface entry into the unlabeled connection hash table.
 * On success a pointer to the new interface entry is returned, on failure NULL
309
 * is returned.
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
 *
 */
static struct netlbl_unlhsh_iface *netlbl_unlhsh_add_iface(int ifindex)
{
	u32 bkt;
	struct netlbl_unlhsh_iface *iface;

	iface = kzalloc(sizeof(*iface), GFP_ATOMIC);
	if (iface == NULL)
		return NULL;

	iface->ifindex = ifindex;
	INIT_LIST_HEAD(&iface->addr4_list);
	INIT_LIST_HEAD(&iface->addr6_list);
	iface->valid = 1;

	spin_lock(&netlbl_unlhsh_lock);
	if (ifindex > 0) {
		bkt = netlbl_unlhsh_hash(ifindex);
		if (netlbl_unlhsh_search_iface(ifindex) != NULL)
			goto add_iface_failure;
		list_add_tail_rcu(&iface->list,
332
			     &netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->tbl[bkt]);
333 334
	} else {
		INIT_LIST_HEAD(&iface->list);
335
		if (netlbl_unlhsh_rcu_deref(netlbl_unlhsh_def) != NULL)
336
			goto add_iface_failure;
337
		rcu_assign_pointer(netlbl_unlhsh_def, iface);
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
	}
	spin_unlock(&netlbl_unlhsh_lock);

	return iface;

add_iface_failure:
	spin_unlock(&netlbl_unlhsh_lock);
	kfree(iface);
	return NULL;
}

/**
 * netlbl_unlhsh_add - Adds a new entry to the unlabeled connection hash table
 * @net: network namespace
 * @dev_name: interface name
 * @addr: IP address in network byte order
 * @mask: address mask in network byte order
 * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
 * @secid: LSM secid value for the entry
357
 * @audit_info: NetLabel audit information
358 359 360 361 362 363
 *
 * Description:
 * Adds a new entry to the unlabeled connection hash table.  Returns zero on
 * success, negative values on failure.
 *
 */
364 365 366 367 368 369 370
int netlbl_unlhsh_add(struct net *net,
		      const char *dev_name,
		      const void *addr,
		      const void *mask,
		      u32 addr_len,
		      u32 secid,
		      struct netlbl_audit *audit_info)
371 372 373 374 375
{
	int ret_val;
	int ifindex;
	struct net_device *dev;
	struct netlbl_unlhsh_iface *iface;
376 377 378
	struct audit_buffer *audit_buf = NULL;
	char *secctx = NULL;
	u32 secctx_len;
379 380 381 382 383 384 385

	if (addr_len != sizeof(struct in_addr) &&
	    addr_len != sizeof(struct in6_addr))
		return -EINVAL;

	rcu_read_lock();
	if (dev_name != NULL) {
E
Eric Dumazet 已提交
386
		dev = dev_get_by_name_rcu(net, dev_name);
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
		if (dev == NULL) {
			ret_val = -ENODEV;
			goto unlhsh_add_return;
		}
		ifindex = dev->ifindex;
		iface = netlbl_unlhsh_search_iface(ifindex);
	} else {
		ifindex = 0;
		iface = rcu_dereference(netlbl_unlhsh_def);
	}
	if (iface == NULL) {
		iface = netlbl_unlhsh_add_iface(ifindex);
		if (iface == NULL) {
			ret_val = -ENOMEM;
			goto unlhsh_add_return;
		}
	}
404 405
	audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCADD,
					      audit_info);
406
	switch (addr_len) {
407
	case sizeof(struct in_addr): {
J
Joe Perches 已提交
408 409
		const struct in_addr *addr4 = addr;
		const struct in_addr *mask4 = mask;
410

411 412
		ret_val = netlbl_unlhsh_add_addr4(iface, addr4, mask4, secid);
		if (audit_buf != NULL)
413 414 415 416
			netlbl_af4list_audit_addr(audit_buf, 1,
						  dev_name,
						  addr4->s_addr,
						  mask4->s_addr);
417
		break;
418
	}
E
Eric Dumazet 已提交
419
#if IS_ENABLED(CONFIG_IPV6)
420
	case sizeof(struct in6_addr): {
J
Joe Perches 已提交
421 422
		const struct in6_addr *addr6 = addr;
		const struct in6_addr *mask6 = mask;
423

424 425
		ret_val = netlbl_unlhsh_add_addr6(iface, addr6, mask6, secid);
		if (audit_buf != NULL)
426 427 428
			netlbl_af6list_audit_addr(audit_buf, 1,
						  dev_name,
						  addr6, mask6);
429
		break;
430
	}
431 432 433 434 435 436 437 438 439
#endif /* IPv6 */
	default:
		ret_val = -EINVAL;
	}
	if (ret_val == 0)
		atomic_inc(&netlabel_mgmt_protocount);

unlhsh_add_return:
	rcu_read_unlock();
440 441 442 443 444 445 446 447 448 449
	if (audit_buf != NULL) {
		if (security_secid_to_secctx(secid,
					     &secctx,
					     &secctx_len) == 0) {
			audit_log_format(audit_buf, " sec_obj=%s", secctx);
			security_release_secctx(secctx, secctx_len);
		}
		audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
		audit_log_end(audit_buf);
	}
450 451 452 453 454
	return ret_val;
}

/**
 * netlbl_unlhsh_remove_addr4 - Remove an IPv4 address entry
455
 * @net: network namespace
456 457 458
 * @iface: interface entry
 * @addr: IP address
 * @mask: IP address mask
459
 * @audit_info: NetLabel audit information
460 461 462
 *
 * Description:
 * Remove an IP address entry from the unlabeled connection hash table.
463
 * Returns zero on success, negative values on failure.
464 465
 *
 */
466 467
static int netlbl_unlhsh_remove_addr4(struct net *net,
				      struct netlbl_unlhsh_iface *iface,
468
				      const struct in_addr *addr,
469 470
				      const struct in_addr *mask,
				      struct netlbl_audit *audit_info)
471
{
472
	struct netlbl_af4list *list_entry;
473
	struct netlbl_unlhsh_addr4 *entry;
474
	struct audit_buffer *audit_buf;
475
	struct net_device *dev;
476
	char *secctx;
477
	u32 secctx_len;
478 479

	spin_lock(&netlbl_unlhsh_lock);
480 481
	list_entry = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
					   &iface->addr4_list);
482
	spin_unlock(&netlbl_unlhsh_lock);
483 484 485
	if (list_entry != NULL)
		entry = netlbl_unlhsh_addr4_entry(list_entry);
	else
486
		entry = NULL;
487

488 489 490 491
	audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
					      audit_info);
	if (audit_buf != NULL) {
		dev = dev_get_by_index(net, iface->ifindex);
492 493 494
		netlbl_af4list_audit_addr(audit_buf, 1,
					  (dev != NULL ? dev->name : NULL),
					  addr->s_addr, mask->s_addr);
Y
Yajun Deng 已提交
495
		dev_put(dev);
496 497 498
		if (entry != NULL &&
		    security_secid_to_secctx(entry->secid,
					     &secctx, &secctx_len) == 0) {
499 500 501
			audit_log_format(audit_buf, " sec_obj=%s", secctx);
			security_release_secctx(secctx, secctx_len);
		}
502
		audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
503 504 505
		audit_log_end(audit_buf);
	}

506 507 508
	if (entry == NULL)
		return -ENOENT;

509
	kfree_rcu(entry, rcu);
510
	return 0;
511 512
}

E
Eric Dumazet 已提交
513
#if IS_ENABLED(CONFIG_IPV6)
514 515
/**
 * netlbl_unlhsh_remove_addr6 - Remove an IPv6 address entry
516
 * @net: network namespace
517 518 519
 * @iface: interface entry
 * @addr: IP address
 * @mask: IP address mask
520
 * @audit_info: NetLabel audit information
521 522 523
 *
 * Description:
 * Remove an IP address entry from the unlabeled connection hash table.
524
 * Returns zero on success, negative values on failure.
525 526
 *
 */
527 528
static int netlbl_unlhsh_remove_addr6(struct net *net,
				      struct netlbl_unlhsh_iface *iface,
529
				      const struct in6_addr *addr,
530 531
				      const struct in6_addr *mask,
				      struct netlbl_audit *audit_info)
532
{
533
	struct netlbl_af6list *list_entry;
534
	struct netlbl_unlhsh_addr6 *entry;
535
	struct audit_buffer *audit_buf;
536
	struct net_device *dev;
537
	char *secctx;
538
	u32 secctx_len;
539 540

	spin_lock(&netlbl_unlhsh_lock);
541
	list_entry = netlbl_af6list_remove(addr, mask, &iface->addr6_list);
542
	spin_unlock(&netlbl_unlhsh_lock);
543 544 545
	if (list_entry != NULL)
		entry = netlbl_unlhsh_addr6_entry(list_entry);
	else
546
		entry = NULL;
547

548 549 550 551
	audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
					      audit_info);
	if (audit_buf != NULL) {
		dev = dev_get_by_index(net, iface->ifindex);
552 553 554
		netlbl_af6list_audit_addr(audit_buf, 1,
					  (dev != NULL ? dev->name : NULL),
					  addr, mask);
Y
Yajun Deng 已提交
555
		dev_put(dev);
556 557 558
		if (entry != NULL &&
		    security_secid_to_secctx(entry->secid,
					     &secctx, &secctx_len) == 0) {
559 560 561
			audit_log_format(audit_buf, " sec_obj=%s", secctx);
			security_release_secctx(secctx, secctx_len);
		}
562
		audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
563 564 565
		audit_log_end(audit_buf);
	}

566 567 568
	if (entry == NULL)
		return -ENOENT;

569
	kfree_rcu(entry, rcu);
570
	return 0;
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
}
#endif /* IPv6 */

/**
 * netlbl_unlhsh_condremove_iface - Remove an interface entry
 * @iface: the interface entry
 *
 * Description:
 * Remove an interface entry from the unlabeled connection hash table if it is
 * empty.  An interface entry is considered to be empty if there are no
 * address entries assigned to it.
 *
 */
static void netlbl_unlhsh_condremove_iface(struct netlbl_unlhsh_iface *iface)
{
586
	struct netlbl_af4list *iter4;
E
Eric Dumazet 已提交
587
#if IS_ENABLED(CONFIG_IPV6)
588 589
	struct netlbl_af6list *iter6;
#endif /* IPv6 */
590 591

	spin_lock(&netlbl_unlhsh_lock);
592 593
	netlbl_af4list_foreach_rcu(iter4, &iface->addr4_list)
		goto unlhsh_condremove_failure;
E
Eric Dumazet 已提交
594
#if IS_ENABLED(CONFIG_IPV6)
595 596 597
	netlbl_af6list_foreach_rcu(iter6, &iface->addr6_list)
		goto unlhsh_condremove_failure;
#endif /* IPv6 */
598 599 600 601
	iface->valid = 0;
	if (iface->ifindex > 0)
		list_del_rcu(&iface->list);
	else
602
		RCU_INIT_POINTER(netlbl_unlhsh_def, NULL);
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
	spin_unlock(&netlbl_unlhsh_lock);

	call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
	return;

unlhsh_condremove_failure:
	spin_unlock(&netlbl_unlhsh_lock);
}

/**
 * netlbl_unlhsh_remove - Remove an entry from the unlabeled hash table
 * @net: network namespace
 * @dev_name: interface name
 * @addr: IP address in network byte order
 * @mask: address mask in network byte order
 * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
619
 * @audit_info: NetLabel audit information
620 621 622 623 624 625
 *
 * Description:
 * Removes and existing entry from the unlabeled connection hash table.
 * Returns zero on success, negative values on failure.
 *
 */
626 627 628 629 630 631
int netlbl_unlhsh_remove(struct net *net,
			 const char *dev_name,
			 const void *addr,
			 const void *mask,
			 u32 addr_len,
			 struct netlbl_audit *audit_info)
632 633 634 635 636 637 638 639 640 641 642
{
	int ret_val;
	struct net_device *dev;
	struct netlbl_unlhsh_iface *iface;

	if (addr_len != sizeof(struct in_addr) &&
	    addr_len != sizeof(struct in6_addr))
		return -EINVAL;

	rcu_read_lock();
	if (dev_name != NULL) {
E
Eric Dumazet 已提交
643
		dev = dev_get_by_name_rcu(net, dev_name);
644 645 646 647 648 649 650 651 652 653 654 655 656
		if (dev == NULL) {
			ret_val = -ENODEV;
			goto unlhsh_remove_return;
		}
		iface = netlbl_unlhsh_search_iface(dev->ifindex);
	} else
		iface = rcu_dereference(netlbl_unlhsh_def);
	if (iface == NULL) {
		ret_val = -ENOENT;
		goto unlhsh_remove_return;
	}
	switch (addr_len) {
	case sizeof(struct in_addr):
657 658 659
		ret_val = netlbl_unlhsh_remove_addr4(net,
						     iface, addr, mask,
						     audit_info);
660
		break;
E
Eric Dumazet 已提交
661
#if IS_ENABLED(CONFIG_IPV6)
662
	case sizeof(struct in6_addr):
663 664 665
		ret_val = netlbl_unlhsh_remove_addr6(net,
						     iface, addr, mask,
						     audit_info);
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
		break;
#endif /* IPv6 */
	default:
		ret_val = -EINVAL;
	}
	if (ret_val == 0) {
		netlbl_unlhsh_condremove_iface(iface);
		atomic_dec(&netlabel_mgmt_protocount);
	}

unlhsh_remove_return:
	rcu_read_unlock();
	return ret_val;
}

/*
 * General Helper Functions
 */

/**
 * netlbl_unlhsh_netdev_handler - Network device notification handler
 * @this: notifier block
 * @event: the event
689
 * @ptr: the netdevice notifier info (cast to void)
690 691 692 693 694 695 696 697
 *
 * Description:
 * Handle network device events, although at present all we care about is a
 * network device going away.  In the case of a device going away we clear any
 * related entries from the unlabeled connection hash table.
 *
 */
static int netlbl_unlhsh_netdev_handler(struct notifier_block *this,
698
					unsigned long event, void *ptr)
699
{
700
	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
701 702
	struct netlbl_unlhsh_iface *iface = NULL;

703
	if (!net_eq(dev_net(dev), &init_net))
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
		return NOTIFY_DONE;

	/* XXX - should this be a check for NETDEV_DOWN or _UNREGISTER? */
	if (event == NETDEV_DOWN) {
		spin_lock(&netlbl_unlhsh_lock);
		iface = netlbl_unlhsh_search_iface(dev->ifindex);
		if (iface != NULL && iface->valid) {
			iface->valid = 0;
			list_del_rcu(&iface->list);
		} else
			iface = NULL;
		spin_unlock(&netlbl_unlhsh_lock);
	}

	if (iface != NULL)
		call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);

	return NOTIFY_DONE;
}
723 724 725 726

/**
 * netlbl_unlabel_acceptflg_set - Set the unlabeled accept flag
 * @value: desired value
727
 * @audit_info: NetLabel audit information
728 729 730 731 732
 *
 * Description:
 * Set the value of the unlabeled accept flag to @value.
 *
 */
733 734
static void netlbl_unlabel_acceptflg_set(u8 value,
					 struct netlbl_audit *audit_info)
735
{
736 737 738
	struct audit_buffer *audit_buf;
	u8 old_val;

739
	old_val = netlabel_unlabel_acceptflg;
740
	netlabel_unlabel_acceptflg = value;
741 742
	audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_ALLOW,
					      audit_info);
743 744 745 746 747
	if (audit_buf != NULL) {
		audit_log_format(audit_buf,
				 " unlbl_accept=%u old=%u", value, old_val);
		audit_log_end(audit_buf);
	}
748 749
}

750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
/**
 * netlbl_unlabel_addrinfo_get - Get the IPv4/6 address information
 * @info: the Generic NETLINK info block
 * @addr: the IP address
 * @mask: the IP address mask
 * @len: the address length
 *
 * Description:
 * Examine the Generic NETLINK message and extract the IP address information.
 * Returns zero on success, negative values on failure.
 *
 */
static int netlbl_unlabel_addrinfo_get(struct genl_info *info,
				       void **addr,
				       void **mask,
				       u32 *len)
{
	u32 addr_len;

769 770
	if (info->attrs[NLBL_UNLABEL_A_IPV4ADDR] &&
	    info->attrs[NLBL_UNLABEL_A_IPV4MASK]) {
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
		addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
		if (addr_len != sizeof(struct in_addr) &&
		    addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV4MASK]))
			return -EINVAL;
		*len = addr_len;
		*addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
		*mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4MASK]);
		return 0;
	} else if (info->attrs[NLBL_UNLABEL_A_IPV6ADDR]) {
		addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
		if (addr_len != sizeof(struct in6_addr) &&
		    addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV6MASK]))
			return -EINVAL;
		*len = addr_len;
		*addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
		*mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6MASK]);
		return 0;
	}

	return -EINVAL;
}

793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
/*
 * NetLabel Command Handlers
 */

/**
 * netlbl_unlabel_accept - Handle an ACCEPT message
 * @skb: the NETLINK buffer
 * @info: the Generic NETLINK info block
 *
 * Description:
 * Process a user generated ACCEPT message and set the accept flag accordingly.
 * Returns zero on success, negative values on failure.
 *
 */
static int netlbl_unlabel_accept(struct sk_buff *skb, struct genl_info *info)
{
809
	u8 value;
810
	struct netlbl_audit audit_info;
811

812 813
	if (info->attrs[NLBL_UNLABEL_A_ACPTFLG]) {
		value = nla_get_u8(info->attrs[NLBL_UNLABEL_A_ACPTFLG]);
814
		if (value == 1 || value == 0) {
815
			netlbl_netlink_auditinfo(&audit_info);
816
			netlbl_unlabel_acceptflg_set(value, &audit_info);
817
			return 0;
818 819 820
		}
	}

821
	return -EINVAL;
822 823 824 825 826 827 828 829 830 831 832 833 834 835
}

/**
 * netlbl_unlabel_list - Handle a LIST message
 * @skb: the NETLINK buffer
 * @info: the Generic NETLINK info block
 *
 * Description:
 * Process a user generated LIST message and respond with the current status.
 * Returns zero on success, negative values on failure.
 *
 */
static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info)
{
836
	int ret_val = -EINVAL;
837
	struct sk_buff *ans_skb;
838
	void *data;
839

840
	ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
841 842
	if (ans_skb == NULL)
		goto list_failure;
843 844
	data = genlmsg_put_reply(ans_skb, info, &netlbl_unlabel_gnl_family,
				 0, NLBL_UNLABEL_C_LIST);
845 846
	if (data == NULL) {
		ret_val = -ENOMEM;
847
		goto list_failure;
848
	}
849

850 851
	ret_val = nla_put_u8(ans_skb,
			     NLBL_UNLABEL_A_ACPTFLG,
852
			     netlabel_unlabel_acceptflg);
853 854 855
	if (ret_val != 0)
		goto list_failure;

856
	genlmsg_end(ans_skb, data);
857
	return genlmsg_reply(ans_skb, info);
858 859

list_failure:
P
Patrick McHardy 已提交
860
	kfree_skb(ans_skb);
861 862 863
	return ret_val;
}

864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
/**
 * netlbl_unlabel_staticadd - Handle a STATICADD message
 * @skb: the NETLINK buffer
 * @info: the Generic NETLINK info block
 *
 * Description:
 * Process a user generated STATICADD message and add a new unlabeled
 * connection entry to the hash table.  Returns zero on success, negative
 * values on failure.
 *
 */
static int netlbl_unlabel_staticadd(struct sk_buff *skb,
				    struct genl_info *info)
{
	int ret_val;
	char *dev_name;
	void *addr;
	void *mask;
	u32 addr_len;
	u32 secid;
884
	struct netlbl_audit audit_info;
885 886 887

	/* Don't allow users to add both IPv4 and IPv6 addresses for a
	 * single entry.  However, allow users to create two entries, one each
T
Topi Miettinen 已提交
888
	 * for IPv4 and IPv6, with the same LSM security context which should
889 890 891 892 893 894 895 896 897
	 * achieve the same result. */
	if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
	    !info->attrs[NLBL_UNLABEL_A_IFACE] ||
	    !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
	       !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
	      (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
	       !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
		return -EINVAL;

898
	netlbl_netlink_auditinfo(&audit_info);
899

900 901 902 903 904 905 906 907 908 909 910 911
	ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
	if (ret_val != 0)
		return ret_val;
	dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
	ret_val = security_secctx_to_secid(
		                  nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
				  nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
				  &secid);
	if (ret_val != 0)
		return ret_val;

	return netlbl_unlhsh_add(&init_net,
912 913
				 dev_name, addr, mask, addr_len, secid,
				 &audit_info);
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
}

/**
 * netlbl_unlabel_staticadddef - Handle a STATICADDDEF message
 * @skb: the NETLINK buffer
 * @info: the Generic NETLINK info block
 *
 * Description:
 * Process a user generated STATICADDDEF message and add a new default
 * unlabeled connection entry.  Returns zero on success, negative values on
 * failure.
 *
 */
static int netlbl_unlabel_staticadddef(struct sk_buff *skb,
				       struct genl_info *info)
{
	int ret_val;
	void *addr;
	void *mask;
	u32 addr_len;
	u32 secid;
935
	struct netlbl_audit audit_info;
936 937 938 939 940 941 942 943 944 945 946 947

	/* Don't allow users to add both IPv4 and IPv6 addresses for a
	 * single entry.  However, allow users to create two entries, one each
	 * for IPv4 and IPv6, with the same LSM security context which should
	 * achieve the same result. */
	if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
	    !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
	       !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
	      (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
	       !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
		return -EINVAL;

948
	netlbl_netlink_auditinfo(&audit_info);
949

950 951 952 953 954 955 956 957 958 959
	ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
	if (ret_val != 0)
		return ret_val;
	ret_val = security_secctx_to_secid(
		                  nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
				  nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
				  &secid);
	if (ret_val != 0)
		return ret_val;

960 961 962
	return netlbl_unlhsh_add(&init_net,
				 NULL, addr, mask, addr_len, secid,
				 &audit_info);
963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
}

/**
 * netlbl_unlabel_staticremove - Handle a STATICREMOVE message
 * @skb: the NETLINK buffer
 * @info: the Generic NETLINK info block
 *
 * Description:
 * Process a user generated STATICREMOVE message and remove the specified
 * unlabeled connection entry.  Returns zero on success, negative values on
 * failure.
 *
 */
static int netlbl_unlabel_staticremove(struct sk_buff *skb,
				       struct genl_info *info)
{
	int ret_val;
	char *dev_name;
	void *addr;
	void *mask;
	u32 addr_len;
984
	struct netlbl_audit audit_info;
985 986 987 988 989 990 991 992 993 994

	/* See the note in netlbl_unlabel_staticadd() about not allowing both
	 * IPv4 and IPv6 in the same entry. */
	if (!info->attrs[NLBL_UNLABEL_A_IFACE] ||
	    !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
	       !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
	      (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
	       !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
		return -EINVAL;

995
	netlbl_netlink_auditinfo(&audit_info);
996

997 998 999 1000 1001
	ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
	if (ret_val != 0)
		return ret_val;
	dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);

1002 1003 1004
	return netlbl_unlhsh_remove(&init_net,
				    dev_name, addr, mask, addr_len,
				    &audit_info);
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
}

/**
 * netlbl_unlabel_staticremovedef - Handle a STATICREMOVEDEF message
 * @skb: the NETLINK buffer
 * @info: the Generic NETLINK info block
 *
 * Description:
 * Process a user generated STATICREMOVEDEF message and remove the default
 * unlabeled connection entry.  Returns zero on success, negative values on
 * failure.
 *
 */
static int netlbl_unlabel_staticremovedef(struct sk_buff *skb,
					  struct genl_info *info)
{
	int ret_val;
	void *addr;
	void *mask;
	u32 addr_len;
1025
	struct netlbl_audit audit_info;
1026 1027 1028 1029 1030 1031 1032 1033 1034

	/* See the note in netlbl_unlabel_staticadd() about not allowing both
	 * IPv4 and IPv6 in the same entry. */
	if (!((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
	       !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
	      (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
	       !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
		return -EINVAL;

1035
	netlbl_netlink_auditinfo(&audit_info);
1036

1037 1038 1039 1040
	ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
	if (ret_val != 0)
		return ret_val;

1041 1042 1043
	return netlbl_unlhsh_remove(&init_net,
				    NULL, addr, mask, addr_len,
				    &audit_info);
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
}


/**
 * netlbl_unlabel_staticlist_gen - Generate messages for STATICLIST[DEF]
 * @cmd: command/message
 * @iface: the interface entry
 * @addr4: the IPv4 address entry
 * @addr6: the IPv6 address entry
 * @arg: the netlbl_unlhsh_walk_arg structure
 *
 * Description:
 * This function is designed to be used to generate a response for a
 * STATICLIST or STATICLISTDEF message.  When called either @addr4 or @addr6
 * can be specified, not both, the other unspecified entry should be set to
 * NULL by the caller.  Returns the size of the message on success, negative
 * values on failure.
 *
 */
static int netlbl_unlabel_staticlist_gen(u32 cmd,
				       const struct netlbl_unlhsh_iface *iface,
				       const struct netlbl_unlhsh_addr4 *addr4,
				       const struct netlbl_unlhsh_addr6 *addr6,
				       void *arg)
{
	int ret_val = -ENOMEM;
	struct netlbl_unlhsh_walk_arg *cb_arg = arg;
	struct net_device *dev;
	void *data;
	u32 secid;
	char *secctx;
	u32 secctx_len;

1077
	data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).portid,
1078 1079 1080 1081 1082 1083 1084
			   cb_arg->seq, &netlbl_unlabel_gnl_family,
			   NLM_F_MULTI, cmd);
	if (data == NULL)
		goto list_cb_failure;

	if (iface->ifindex > 0) {
		dev = dev_get_by_index(&init_net, iface->ifindex);
1085 1086 1087 1088
		if (!dev) {
			ret_val = -ENODEV;
			goto list_cb_failure;
		}
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
		ret_val = nla_put_string(cb_arg->skb,
					 NLBL_UNLABEL_A_IFACE, dev->name);
		dev_put(dev);
		if (ret_val != 0)
			goto list_cb_failure;
	}

	if (addr4) {
		struct in_addr addr_struct;

1099
		addr_struct.s_addr = addr4->list.addr;
1100 1101 1102
		ret_val = nla_put_in_addr(cb_arg->skb,
					  NLBL_UNLABEL_A_IPV4ADDR,
					  addr_struct.s_addr);
1103 1104 1105
		if (ret_val != 0)
			goto list_cb_failure;

1106
		addr_struct.s_addr = addr4->list.mask;
1107 1108 1109
		ret_val = nla_put_in_addr(cb_arg->skb,
					  NLBL_UNLABEL_A_IPV4MASK,
					  addr_struct.s_addr);
1110 1111 1112 1113 1114
		if (ret_val != 0)
			goto list_cb_failure;

		secid = addr4->secid;
	} else {
1115 1116 1117
		ret_val = nla_put_in6_addr(cb_arg->skb,
					   NLBL_UNLABEL_A_IPV6ADDR,
					   &addr6->list.addr);
1118 1119 1120
		if (ret_val != 0)
			goto list_cb_failure;

1121 1122 1123
		ret_val = nla_put_in6_addr(cb_arg->skb,
					   NLBL_UNLABEL_A_IPV6MASK,
					   &addr6->list.mask);
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
		if (ret_val != 0)
			goto list_cb_failure;

		secid = addr6->secid;
	}

	ret_val = security_secid_to_secctx(secid, &secctx, &secctx_len);
	if (ret_val != 0)
		goto list_cb_failure;
	ret_val = nla_put(cb_arg->skb,
			  NLBL_UNLABEL_A_SECCTX,
			  secctx_len,
			  secctx);
	security_release_secctx(secctx, secctx_len);
	if (ret_val != 0)
		goto list_cb_failure;

	cb_arg->seq++;
1142 1143
	genlmsg_end(cb_arg->skb, data);
	return 0;
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166

list_cb_failure:
	genlmsg_cancel(cb_arg->skb, data);
	return ret_val;
}

/**
 * netlbl_unlabel_staticlist - Handle a STATICLIST message
 * @skb: the NETLINK buffer
 * @cb: the NETLINK callback
 *
 * Description:
 * Process a user generated STATICLIST message and dump the unlabeled
 * connection hash table in a form suitable for use in a kernel generated
 * STATICLIST message.  Returns the length of @skb.
 *
 */
static int netlbl_unlabel_staticlist(struct sk_buff *skb,
				     struct netlink_callback *cb)
{
	struct netlbl_unlhsh_walk_arg cb_arg;
	u32 skip_bkt = cb->args[0];
	u32 skip_chain = cb->args[1];
1167
	u32 skip_addr4 = cb->args[2];
1168
	u32 iter_bkt, iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0;
1169
	struct netlbl_unlhsh_iface *iface;
P
Paul Moore 已提交
1170
	struct list_head *iter_list;
1171
	struct netlbl_af4list *addr4;
E
Eric Dumazet 已提交
1172
#if IS_ENABLED(CONFIG_IPV6)
1173
	u32 skip_addr6 = cb->args[3];
1174 1175
	struct netlbl_af6list *addr6;
#endif
1176 1177 1178 1179 1180 1181 1182 1183

	cb_arg.nl_cb = cb;
	cb_arg.skb = skb;
	cb_arg.seq = cb->nlh->nlmsg_seq;

	rcu_read_lock();
	for (iter_bkt = skip_bkt;
	     iter_bkt < rcu_dereference(netlbl_unlhsh)->size;
1184
	     iter_bkt++) {
P
Paul Moore 已提交
1185 1186
		iter_list = &rcu_dereference(netlbl_unlhsh)->tbl[iter_bkt];
		list_for_each_entry_rcu(iface, iter_list, list) {
1187 1188 1189
			if (!iface->valid ||
			    iter_chain++ < skip_chain)
				continue;
1190 1191
			netlbl_af4list_foreach_rcu(addr4,
						   &iface->addr4_list) {
1192
				if (iter_addr4++ < skip_addr4)
1193 1194
					continue;
				if (netlbl_unlabel_staticlist_gen(
1195 1196 1197 1198 1199
					      NLBL_UNLABEL_C_STATICLIST,
					      iface,
					      netlbl_unlhsh_addr4_entry(addr4),
					      NULL,
					      &cb_arg) < 0) {
1200 1201 1202 1203 1204
					iter_addr4--;
					iter_chain--;
					goto unlabel_staticlist_return;
				}
			}
1205 1206
			iter_addr4 = 0;
			skip_addr4 = 0;
E
Eric Dumazet 已提交
1207
#if IS_ENABLED(CONFIG_IPV6)
1208 1209
			netlbl_af6list_foreach_rcu(addr6,
						   &iface->addr6_list) {
1210
				if (iter_addr6++ < skip_addr6)
1211 1212
					continue;
				if (netlbl_unlabel_staticlist_gen(
1213 1214 1215 1216 1217
					      NLBL_UNLABEL_C_STATICLIST,
					      iface,
					      NULL,
					      netlbl_unlhsh_addr6_entry(addr6),
					      &cb_arg) < 0) {
1218 1219 1220 1221 1222
					iter_addr6--;
					iter_chain--;
					goto unlabel_staticlist_return;
				}
			}
1223 1224
			iter_addr6 = 0;
			skip_addr6 = 0;
1225
#endif /* IPv6 */
1226
		}
1227 1228
		iter_chain = 0;
		skip_chain = 0;
1229 1230 1231 1232
	}

unlabel_staticlist_return:
	rcu_read_unlock();
1233 1234 1235 1236
	cb->args[0] = iter_bkt;
	cb->args[1] = iter_chain;
	cb->args[2] = iter_addr4;
	cb->args[3] = iter_addr6;
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
	return skb->len;
}

/**
 * netlbl_unlabel_staticlistdef - Handle a STATICLISTDEF message
 * @skb: the NETLINK buffer
 * @cb: the NETLINK callback
 *
 * Description:
 * Process a user generated STATICLISTDEF message and dump the default
 * unlabeled connection entry in a form suitable for use in a kernel generated
 * STATICLISTDEF message.  Returns the length of @skb.
 *
 */
static int netlbl_unlabel_staticlistdef(struct sk_buff *skb,
					struct netlink_callback *cb)
{
	struct netlbl_unlhsh_walk_arg cb_arg;
	struct netlbl_unlhsh_iface *iface;
1256
	u32 iter_addr4 = 0, iter_addr6 = 0;
1257
	struct netlbl_af4list *addr4;
E
Eric Dumazet 已提交
1258
#if IS_ENABLED(CONFIG_IPV6)
1259 1260
	struct netlbl_af6list *addr6;
#endif
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270

	cb_arg.nl_cb = cb;
	cb_arg.skb = skb;
	cb_arg.seq = cb->nlh->nlmsg_seq;

	rcu_read_lock();
	iface = rcu_dereference(netlbl_unlhsh_def);
	if (iface == NULL || !iface->valid)
		goto unlabel_staticlistdef_return;

1271
	netlbl_af4list_foreach_rcu(addr4, &iface->addr4_list) {
1272
		if (iter_addr4++ < cb->args[0])
1273 1274
			continue;
		if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
1275 1276 1277 1278
					      iface,
					      netlbl_unlhsh_addr4_entry(addr4),
					      NULL,
					      &cb_arg) < 0) {
1279 1280 1281 1282
			iter_addr4--;
			goto unlabel_staticlistdef_return;
		}
	}
E
Eric Dumazet 已提交
1283
#if IS_ENABLED(CONFIG_IPV6)
1284
	netlbl_af6list_foreach_rcu(addr6, &iface->addr6_list) {
1285
		if (iter_addr6++ < cb->args[1])
1286 1287
			continue;
		if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
1288 1289 1290 1291
					      iface,
					      NULL,
					      netlbl_unlhsh_addr6_entry(addr6),
					      &cb_arg) < 0) {
1292 1293 1294 1295
			iter_addr6--;
			goto unlabel_staticlistdef_return;
		}
	}
1296
#endif /* IPv6 */
1297 1298 1299

unlabel_staticlistdef_return:
	rcu_read_unlock();
1300 1301
	cb->args[0] = iter_addr4;
	cb->args[1] = iter_addr6;
1302 1303
	return skb->len;
}
1304 1305 1306 1307 1308

/*
 * NetLabel Generic NETLINK Command Definitions
 */

1309
static const struct genl_small_ops netlbl_unlabel_genl_ops[] = {
1310
	{
1311
	.cmd = NLBL_UNLABEL_C_STATICADD,
1312
	.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1313 1314 1315
	.flags = GENL_ADMIN_PERM,
	.doit = netlbl_unlabel_staticadd,
	.dumpit = NULL,
1316 1317
	},
	{
1318
	.cmd = NLBL_UNLABEL_C_STATICREMOVE,
1319
	.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1320 1321 1322
	.flags = GENL_ADMIN_PERM,
	.doit = netlbl_unlabel_staticremove,
	.dumpit = NULL,
1323 1324
	},
	{
1325
	.cmd = NLBL_UNLABEL_C_STATICLIST,
1326
	.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1327 1328 1329
	.flags = 0,
	.doit = NULL,
	.dumpit = netlbl_unlabel_staticlist,
1330 1331
	},
	{
1332
	.cmd = NLBL_UNLABEL_C_STATICADDDEF,
1333
	.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1334 1335 1336
	.flags = GENL_ADMIN_PERM,
	.doit = netlbl_unlabel_staticadddef,
	.dumpit = NULL,
1337 1338
	},
	{
1339
	.cmd = NLBL_UNLABEL_C_STATICREMOVEDEF,
1340
	.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1341 1342 1343
	.flags = GENL_ADMIN_PERM,
	.doit = netlbl_unlabel_staticremovedef,
	.dumpit = NULL,
1344 1345
	},
	{
1346
	.cmd = NLBL_UNLABEL_C_STATICLISTDEF,
1347
	.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1348 1349 1350
	.flags = 0,
	.doit = NULL,
	.dumpit = netlbl_unlabel_staticlistdef,
1351 1352
	},
	{
1353
	.cmd = NLBL_UNLABEL_C_ACCEPT,
1354
	.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1355
	.flags = GENL_ADMIN_PERM,
1356 1357
	.doit = netlbl_unlabel_accept,
	.dumpit = NULL,
1358 1359
	},
	{
1360
	.cmd = NLBL_UNLABEL_C_LIST,
1361
	.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1362 1363 1364
	.flags = 0,
	.doit = netlbl_unlabel_list,
	.dumpit = NULL,
1365
	},
1366 1367
};

1368
static struct genl_family netlbl_unlabel_gnl_family __ro_after_init = {
1369 1370 1371 1372
	.hdrsize = 0,
	.name = NETLBL_NLTYPE_UNLABELED_NAME,
	.version = NETLBL_PROTO_VERSION,
	.maxattr = NLBL_UNLABEL_A_MAX,
1373
	.policy = netlbl_unlabel_genl_policy,
1374
	.module = THIS_MODULE,
1375 1376
	.small_ops = netlbl_unlabel_genl_ops,
	.n_small_ops = ARRAY_SIZE(netlbl_unlabel_genl_ops),
1377
	.resv_start_op = NLBL_UNLABEL_C_STATICLISTDEF + 1,
1378 1379
};

1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
/*
 * NetLabel Generic NETLINK Protocol Functions
 */

/**
 * netlbl_unlabel_genl_init - Register the Unlabeled NetLabel component
 *
 * Description:
 * Register the unlabeled packet NetLabel component with the Generic NETLINK
 * mechanism.  Returns zero on success, negative values on failure.
 *
 */
1392
int __init netlbl_unlabel_genl_init(void)
1393
{
1394
	return genl_register_family(&netlbl_unlabel_gnl_family);
1395 1396 1397 1398 1399 1400
}

/*
 * NetLabel KAPI Hooks
 */

1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
static struct notifier_block netlbl_unlhsh_netdev_notifier = {
	.notifier_call = netlbl_unlhsh_netdev_handler,
};

/**
 * netlbl_unlabel_init - Initialize the unlabeled connection hash table
 * @size: the number of bits to use for the hash buckets
 *
 * Description:
 * Initializes the unlabeled connection hash table and registers a network
 * device notification handler.  This function should only be called by the
 * NetLabel subsystem itself during initialization.  Returns zero on success,
 * non-zero values on error.
 *
 */
1416
int __init netlbl_unlabel_init(u32 size)
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
{
	u32 iter;
	struct netlbl_unlhsh_tbl *hsh_tbl;

	if (size == 0)
		return -EINVAL;

	hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
	if (hsh_tbl == NULL)
		return -ENOMEM;
	hsh_tbl->size = 1 << size;
	hsh_tbl->tbl = kcalloc(hsh_tbl->size,
			       sizeof(struct list_head),
			       GFP_KERNEL);
	if (hsh_tbl->tbl == NULL) {
		kfree(hsh_tbl);
		return -ENOMEM;
	}
	for (iter = 0; iter < hsh_tbl->size; iter++)
		INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);

	spin_lock(&netlbl_unlhsh_lock);
1439
	rcu_assign_pointer(netlbl_unlhsh, hsh_tbl);
1440 1441 1442 1443 1444 1445 1446
	spin_unlock(&netlbl_unlhsh_lock);

	register_netdevice_notifier(&netlbl_unlhsh_netdev_notifier);

	return 0;
}

1447 1448
/**
 * netlbl_unlabel_getattr - Get the security attributes for an unlabled packet
1449 1450
 * @skb: the packet
 * @family: protocol family
1451 1452 1453 1454 1455 1456 1457
 * @secattr: the security attributes
 *
 * Description:
 * Determine the security attributes, if any, for an unlabled packet and return
 * them in @secattr.  Returns zero on success and negative values on failure.
 *
 */
1458 1459 1460
int netlbl_unlabel_getattr(const struct sk_buff *skb,
			   u16 family,
			   struct netlbl_lsm_secattr *secattr)
1461
{
1462 1463 1464
	struct netlbl_unlhsh_iface *iface;

	rcu_read_lock();
1465
	iface = netlbl_unlhsh_search_iface(skb->skb_iif);
1466
	if (iface == NULL)
1467 1468
		iface = rcu_dereference(netlbl_unlhsh_def);
	if (iface == NULL || !iface->valid)
1469
		goto unlabel_getattr_nolabel;
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479

#if IS_ENABLED(CONFIG_IPV6)
	/* When resolving a fallback label, check the sk_buff version as
	 * it is possible (e.g. SCTP) to have family = PF_INET6 while
	 * receiving ip_hdr(skb)->version = 4.
	 */
	if (family == PF_INET6 && ip_hdr(skb)->version == 4)
		family = PF_INET;
#endif /* IPv6 */

1480
	switch (family) {
1481 1482
	case PF_INET: {
		struct iphdr *hdr4;
1483
		struct netlbl_af4list *addr4;
1484

1485
		hdr4 = ip_hdr(skb);
1486 1487
		addr4 = netlbl_af4list_search(hdr4->saddr,
					      &iface->addr4_list);
1488 1489
		if (addr4 == NULL)
			goto unlabel_getattr_nolabel;
1490
		secattr->attr.secid = netlbl_unlhsh_addr4_entry(addr4)->secid;
1491
		break;
1492
	}
E
Eric Dumazet 已提交
1493
#if IS_ENABLED(CONFIG_IPV6)
1494 1495
	case PF_INET6: {
		struct ipv6hdr *hdr6;
1496
		struct netlbl_af6list *addr6;
1497

1498
		hdr6 = ipv6_hdr(skb);
1499 1500
		addr6 = netlbl_af6list_search(&hdr6->saddr,
					      &iface->addr6_list);
1501 1502
		if (addr6 == NULL)
			goto unlabel_getattr_nolabel;
1503
		secattr->attr.secid = netlbl_unlhsh_addr6_entry(addr6)->secid;
1504
		break;
1505
	}
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
#endif /* IPv6 */
	default:
		goto unlabel_getattr_nolabel;
	}
	rcu_read_unlock();

	secattr->flags |= NETLBL_SECATTR_SECID;
	secattr->type = NETLBL_NLTYPE_UNLABELED;
	return 0;

unlabel_getattr_nolabel:
	rcu_read_unlock();
1518 1519
	if (netlabel_unlabel_acceptflg == 0)
		return -ENOMSG;
1520
	secattr->type = NETLBL_NLTYPE_UNLABELED;
1521
	return 0;
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531
}

/**
 * netlbl_unlabel_defconf - Set the default config to allow unlabeled packets
 *
 * Description:
 * Set the default NetLabel configuration to allow incoming unlabeled packets
 * and to send unlabeled network traffic by default.
 *
 */
1532
int __init netlbl_unlabel_defconf(void)
1533 1534 1535
{
	int ret_val;
	struct netlbl_dom_map *entry;
1536
	struct netlbl_audit audit_info;
1537

1538 1539 1540
	/* Only the kernel is allowed to call this function and the only time
	 * it is called is at bootup before the audit subsystem is reporting
	 * messages so don't worry to much about these values. */
1541
	security_current_getsecid_subj(&audit_info.secid);
1542
	audit_info.loginuid = GLOBAL_ROOT_UID;
1543
	audit_info.sessionid = 0;
1544 1545 1546 1547

	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
	if (entry == NULL)
		return -ENOMEM;
1548
	entry->family = AF_UNSPEC;
1549
	entry->def.type = NETLBL_NLTYPE_UNLABELED;
1550
	ret_val = netlbl_domhsh_add_default(entry, &audit_info);
1551 1552 1553
	if (ret_val != 0)
		return ret_val;

1554
	netlbl_unlabel_acceptflg_set(1, &audit_info);
1555 1556 1557

	return 0;
}