smack_lsm.c 117.9 KB
Newer Older
1 2 3 4 5
/*
 *  Simplified MAC Kernel (smack) security module
 *
 *  This file contains the smack hook function implementations.
 *
6
 *  Authors:
7
 *	Casey Schaufler <casey@schaufler-ca.com>
8
 *	Jarkko Sakkinen <jarkko.sakkinen@intel.com>
9 10
 *
 *  Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
11
 *  Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
12
 *                Paul Moore <paul@paul-moore.com>
13
 *  Copyright (C) 2010 Nokia Corporation
14
 *  Copyright (C) 2011 Intel Corporation.
15 16 17 18 19 20 21 22 23 24 25 26
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License version 2,
 *      as published by the Free Software Foundation.
 */

#include <linux/xattr.h>
#include <linux/pagemap.h>
#include <linux/mount.h>
#include <linux/stat.h>
#include <linux/kd.h>
#include <asm/ioctls.h>
27
#include <linux/ip.h>
28 29
#include <linux/tcp.h>
#include <linux/udp.h>
30
#include <linux/dccp.h>
31
#include <linux/slab.h>
32 33 34
#include <linux/mutex.h>
#include <linux/pipe_fs_i.h>
#include <net/cipso_ipv4.h>
35 36
#include <net/ip.h>
#include <net/ipv6.h>
37
#include <linux/audit.h>
N
Nick Black 已提交
38
#include <linux/magic.h>
39
#include <linux/dcache.h>
J
Jarkko Sakkinen 已提交
40
#include <linux/personality.h>
A
Al Viro 已提交
41 42 43
#include <linux/msg.h>
#include <linux/shm.h>
#include <linux/binfmts.h>
44
#include <linux/parser.h>
45 46
#include "smack.h"

47 48 49
#define TRANS_TRUE	"TRUE"
#define TRANS_TRUE_SIZE	4

50 51 52 53
#define SMK_CONNECTING	0
#define SMK_RECEIVING	1
#define SMK_SENDING	2

C
Casey Schaufler 已提交
54
#ifdef SMACK_IPV6_PORT_LABELING
55
DEFINE_MUTEX(smack_ipv6_lock);
56
static LIST_HEAD(smk_ipv6_port_list);
C
Casey Schaufler 已提交
57
#endif
58
static struct kmem_cache *smack_inode_cache;
59
int smack_enabled;
60

61
static const match_table_t smk_mount_tokens = {
62 63 64 65 66 67 68 69
	{Opt_fsdefault, SMK_FSDEFAULT "%s"},
	{Opt_fsfloor, SMK_FSFLOOR "%s"},
	{Opt_fshat, SMK_FSHAT "%s"},
	{Opt_fsroot, SMK_FSROOT "%s"},
	{Opt_fstransmute, SMK_FSTRANS "%s"},
	{Opt_error, NULL},
};

70 71 72 73 74 75 76 77
#ifdef CONFIG_SECURITY_SMACK_BRINGUP
static char *smk_bu_mess[] = {
	"Bringup Error",	/* Unused */
	"Bringup",		/* SMACK_BRINGUP_ALLOW */
	"Unconfined Subject",	/* SMACK_UNCONFINED_SUBJECT */
	"Unconfined Object",	/* SMACK_UNCONFINED_OBJECT */
};

C
Casey Schaufler 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
static void smk_bu_mode(int mode, char *s)
{
	int i = 0;

	if (mode & MAY_READ)
		s[i++] = 'r';
	if (mode & MAY_WRITE)
		s[i++] = 'w';
	if (mode & MAY_EXEC)
		s[i++] = 'x';
	if (mode & MAY_APPEND)
		s[i++] = 'a';
	if (mode & MAY_TRANSMUTE)
		s[i++] = 't';
	if (mode & MAY_LOCK)
		s[i++] = 'l';
	if (i == 0)
		s[i++] = '-';
	s[i] = '\0';
}
#endif

#ifdef CONFIG_SECURITY_SMACK_BRINGUP
101 102
static int smk_bu_note(char *note, struct smack_known *sskp,
		       struct smack_known *oskp, int mode, int rc)
C
Casey Schaufler 已提交
103 104 105 106 107
{
	char acc[SMK_NUM_ACCESS_TYPE + 1];

	if (rc <= 0)
		return rc;
108 109
	if (rc > SMACK_UNCONFINED_OBJECT)
		rc = 0;
C
Casey Schaufler 已提交
110 111

	smk_bu_mode(mode, acc);
112
	pr_info("Smack %s: (%s %s %s) %s\n", smk_bu_mess[rc],
113
		sskp->smk_known, oskp->smk_known, acc, note);
C
Casey Schaufler 已提交
114 115 116
	return 0;
}
#else
117
#define smk_bu_note(note, sskp, oskp, mode, RC) (RC)
C
Casey Schaufler 已提交
118 119 120
#endif

#ifdef CONFIG_SECURITY_SMACK_BRINGUP
121 122
static int smk_bu_current(char *note, struct smack_known *oskp,
			  int mode, int rc)
C
Casey Schaufler 已提交
123 124 125 126 127 128
{
	struct task_smack *tsp = current_security();
	char acc[SMK_NUM_ACCESS_TYPE + 1];

	if (rc <= 0)
		return rc;
129 130
	if (rc > SMACK_UNCONFINED_OBJECT)
		rc = 0;
C
Casey Schaufler 已提交
131 132

	smk_bu_mode(mode, acc);
133
	pr_info("Smack %s: (%s %s %s) %s %s\n", smk_bu_mess[rc],
134 135
		tsp->smk_task->smk_known, oskp->smk_known,
		acc, current->comm, note);
C
Casey Schaufler 已提交
136 137 138
	return 0;
}
#else
139
#define smk_bu_current(note, oskp, mode, RC) (RC)
C
Casey Schaufler 已提交
140 141 142 143 144 145
#endif

#ifdef CONFIG_SECURITY_SMACK_BRINGUP
static int smk_bu_task(struct task_struct *otp, int mode, int rc)
{
	struct task_smack *tsp = current_security();
146
	struct smack_known *smk_task = smk_of_task_struct(otp);
C
Casey Schaufler 已提交
147 148 149 150
	char acc[SMK_NUM_ACCESS_TYPE + 1];

	if (rc <= 0)
		return rc;
151 152
	if (rc > SMACK_UNCONFINED_OBJECT)
		rc = 0;
C
Casey Schaufler 已提交
153 154

	smk_bu_mode(mode, acc);
155
	pr_info("Smack %s: (%s %s %s) %s to %s\n", smk_bu_mess[rc],
156
		tsp->smk_task->smk_known, smk_task->smk_known, acc,
C
Casey Schaufler 已提交
157 158 159 160 161 162 163 164 165 166 167
		current->comm, otp->comm);
	return 0;
}
#else
#define smk_bu_task(otp, mode, RC) (RC)
#endif

#ifdef CONFIG_SECURITY_SMACK_BRINGUP
static int smk_bu_inode(struct inode *inode, int mode, int rc)
{
	struct task_smack *tsp = current_security();
168
	struct inode_smack *isp = inode->i_security;
C
Casey Schaufler 已提交
169 170
	char acc[SMK_NUM_ACCESS_TYPE + 1];

171 172 173 174
	if (isp->smk_flags & SMK_INODE_IMPURE)
		pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
			inode->i_sb->s_id, inode->i_ino, current->comm);

C
Casey Schaufler 已提交
175 176
	if (rc <= 0)
		return rc;
177 178 179 180 181
	if (rc > SMACK_UNCONFINED_OBJECT)
		rc = 0;
	if (rc == SMACK_UNCONFINED_SUBJECT &&
	    (mode & (MAY_WRITE | MAY_APPEND)))
		isp->smk_flags |= SMK_INODE_IMPURE;
C
Casey Schaufler 已提交
182 183

	smk_bu_mode(mode, acc);
184 185 186

	pr_info("Smack %s: (%s %s %s) inode=(%s %ld) %s\n", smk_bu_mess[rc],
		tsp->smk_task->smk_known, isp->smk_inode->smk_known, acc,
C
Casey Schaufler 已提交
187 188 189 190 191 192 193 194 195 196 197 198
		inode->i_sb->s_id, inode->i_ino, current->comm);
	return 0;
}
#else
#define smk_bu_inode(inode, mode, RC) (RC)
#endif

#ifdef CONFIG_SECURITY_SMACK_BRINGUP
static int smk_bu_file(struct file *file, int mode, int rc)
{
	struct task_smack *tsp = current_security();
	struct smack_known *sskp = tsp->smk_task;
C
Casey Schaufler 已提交
199
	struct inode *inode = file_inode(file);
200
	struct inode_smack *isp = inode->i_security;
C
Casey Schaufler 已提交
201 202
	char acc[SMK_NUM_ACCESS_TYPE + 1];

203 204 205 206
	if (isp->smk_flags & SMK_INODE_IMPURE)
		pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
			inode->i_sb->s_id, inode->i_ino, current->comm);

C
Casey Schaufler 已提交
207 208
	if (rc <= 0)
		return rc;
209 210
	if (rc > SMACK_UNCONFINED_OBJECT)
		rc = 0;
C
Casey Schaufler 已提交
211 212

	smk_bu_mode(mode, acc);
213
	pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
C
Casey Schaufler 已提交
214
		sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
A
Al Viro 已提交
215
		inode->i_sb->s_id, inode->i_ino, file,
C
Casey Schaufler 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228
		current->comm);
	return 0;
}
#else
#define smk_bu_file(file, mode, RC) (RC)
#endif

#ifdef CONFIG_SECURITY_SMACK_BRINGUP
static int smk_bu_credfile(const struct cred *cred, struct file *file,
				int mode, int rc)
{
	struct task_smack *tsp = cred->security;
	struct smack_known *sskp = tsp->smk_task;
A
Al Viro 已提交
229
	struct inode *inode = file_inode(file);
230
	struct inode_smack *isp = inode->i_security;
C
Casey Schaufler 已提交
231 232
	char acc[SMK_NUM_ACCESS_TYPE + 1];

233 234 235 236
	if (isp->smk_flags & SMK_INODE_IMPURE)
		pr_info("Smack Unconfined Corruption: inode=(%s %ld) %s\n",
			inode->i_sb->s_id, inode->i_ino, current->comm);

C
Casey Schaufler 已提交
237 238
	if (rc <= 0)
		return rc;
239 240
	if (rc > SMACK_UNCONFINED_OBJECT)
		rc = 0;
C
Casey Schaufler 已提交
241 242

	smk_bu_mode(mode, acc);
243
	pr_info("Smack %s: (%s %s %s) file=(%s %ld %pD) %s\n", smk_bu_mess[rc],
244
		sskp->smk_known, smk_of_inode(inode)->smk_known, acc,
A
Al Viro 已提交
245
		inode->i_sb->s_id, inode->i_ino, file,
C
Casey Schaufler 已提交
246 247 248 249 250 251 252
		current->comm);
	return 0;
}
#else
#define smk_bu_credfile(cred, file, mode, RC) (RC)
#endif

253 254
/**
 * smk_fetch - Fetch the smack label from a file.
255
 * @name: type of the label (attribute)
256 257 258
 * @ip: a pointer to the inode
 * @dp: a pointer to the dentry
 *
259 260
 * Returns a pointer to the master list entry for the Smack label,
 * NULL if there was no label to fetch, or an error code.
261
 */
262 263
static struct smack_known *smk_fetch(const char *name, struct inode *ip,
					struct dentry *dp)
264 265
{
	int rc;
266
	char *buffer;
267
	struct smack_known *skp = NULL;
268

269
	if (!(ip->i_opflags & IOP_XATTR))
270
		return ERR_PTR(-EOPNOTSUPP);
271

272 273
	buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
	if (buffer == NULL)
274
		return ERR_PTR(-ENOMEM);
275

276
	rc = __vfs_getxattr(dp, ip, name, buffer, SMK_LONGLABEL);
277 278 279 280 281
	if (rc < 0)
		skp = ERR_PTR(rc);
	else if (rc == 0)
		skp = NULL;
	else
282
		skp = smk_import_entry(buffer, rc);
283 284 285

	kfree(buffer);

286
	return skp;
287 288 289 290
}

/**
 * new_inode_smack - allocate an inode security blob
291
 * @skp: a pointer to the Smack label entry to use in the blob
292 293 294
 *
 * Returns the new blob or NULL if there's no memory available
 */
295
static struct inode_smack *new_inode_smack(struct smack_known *skp)
296 297 298
{
	struct inode_smack *isp;

299
	isp = kmem_cache_zalloc(smack_inode_cache, GFP_NOFS);
300 301 302
	if (isp == NULL)
		return NULL;

303
	isp->smk_inode = skp;
304 305 306 307 308 309
	isp->smk_flags = 0;
	mutex_init(&isp->smk_lock);

	return isp;
}

310 311
/**
 * new_task_smack - allocate a task security blob
312 313 314
 * @task: a pointer to the Smack label for the running task
 * @forked: a pointer to the Smack label for the forked task
 * @gfp: type of the memory for the allocation
315 316 317
 *
 * Returns the new blob or NULL if there's no memory available
 */
318 319
static struct task_smack *new_task_smack(struct smack_known *task,
					struct smack_known *forked, gfp_t gfp)
320 321 322 323 324 325 326 327 328 329
{
	struct task_smack *tsp;

	tsp = kzalloc(sizeof(struct task_smack), gfp);
	if (tsp == NULL)
		return NULL;

	tsp->smk_task = task;
	tsp->smk_forked = forked;
	INIT_LIST_HEAD(&tsp->smk_rules);
330
	INIT_LIST_HEAD(&tsp->smk_relabel);
331 332 333 334 335 336 337
	mutex_init(&tsp->smk_rules_lock);

	return tsp;
}

/**
 * smk_copy_rules - copy a rule set
338 339 340
 * @nhead: new rules header pointer
 * @ohead: old rules header pointer
 * @gfp: type of the memory for the allocation
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
 *
 * Returns 0 on success, -ENOMEM on error
 */
static int smk_copy_rules(struct list_head *nhead, struct list_head *ohead,
				gfp_t gfp)
{
	struct smack_rule *nrp;
	struct smack_rule *orp;
	int rc = 0;

	list_for_each_entry_rcu(orp, ohead, list) {
		nrp = kzalloc(sizeof(struct smack_rule), gfp);
		if (nrp == NULL) {
			rc = -ENOMEM;
			break;
		}
		*nrp = *orp;
		list_add_rcu(&nrp->list, nhead);
	}
	return rc;
}

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
/**
 * smk_copy_relabel - copy smk_relabel labels list
 * @nhead: new rules header pointer
 * @ohead: old rules header pointer
 * @gfp: type of the memory for the allocation
 *
 * Returns 0 on success, -ENOMEM on error
 */
static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
				gfp_t gfp)
{
	struct smack_known_list_elem *nklep;
	struct smack_known_list_elem *oklep;

	list_for_each_entry(oklep, ohead, list) {
		nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
		if (nklep == NULL) {
			smk_destroy_label_list(nhead);
			return -ENOMEM;
		}
		nklep->smk_label = oklep->smk_label;
		list_add(&nklep->list, nhead);
	}

	return 0;
}

390 391 392 393 394 395 396 397
/**
 * smk_ptrace_mode - helper function for converting PTRACE_MODE_* into MAY_*
 * @mode - input mode in form of PTRACE_MODE_*
 *
 * Returns a converted MAY_* mode usable by smack rules
 */
static inline unsigned int smk_ptrace_mode(unsigned int mode)
{
398
	if (mode & PTRACE_MODE_ATTACH)
399
		return MAY_READWRITE;
400 401
	if (mode & PTRACE_MODE_READ)
		return MAY_READ;
402 403 404 405 406 407 408

	return 0;
}

/**
 * smk_ptrace_rule_check - helper for ptrace access
 * @tracer: tracer process
409
 * @tracee_known: label entry of the process that's about to be traced
410 411 412 413 414
 * @mode: ptrace attachment mode (PTRACE_MODE_*)
 * @func: name of the function that called us, used for audit
 *
 * Returns 0 on access granted, -error on error
 */
415 416
static int smk_ptrace_rule_check(struct task_struct *tracer,
				 struct smack_known *tracee_known,
417 418 419 420 421
				 unsigned int mode, const char *func)
{
	int rc;
	struct smk_audit_info ad, *saip = NULL;
	struct task_smack *tsp;
422
	struct smack_known *tracer_known;
423 424 425 426 427 428 429

	if ((mode & PTRACE_MODE_NOAUDIT) == 0) {
		smk_ad_init(&ad, func, LSM_AUDIT_DATA_TASK);
		smk_ad_setfield_u_tsk(&ad, tracer);
		saip = &ad;
	}

430 431
	rcu_read_lock();
	tsp = __task_cred(tracer)->security;
432
	tracer_known = smk_of_task(tsp);
433

434 435 436
	if ((mode & PTRACE_MODE_ATTACH) &&
	    (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
	     smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
437
		if (tracer_known->smk_known == tracee_known->smk_known)
438 439 440 441 442 443 444 445 446
			rc = 0;
		else if (smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)
			rc = -EACCES;
		else if (capable(CAP_SYS_PTRACE))
			rc = 0;
		else
			rc = -EACCES;

		if (saip)
447 448 449
			smack_log(tracer_known->smk_known,
				  tracee_known->smk_known,
				  0, rc, saip);
450

451
		rcu_read_unlock();
452 453 454 455
		return rc;
	}

	/* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
456
	rc = smk_tskacc(tsp, tracee_known, smk_ptrace_mode(mode), saip);
457 458

	rcu_read_unlock();
459 460 461
	return rc;
}

462 463 464 465 466 467
/*
 * LSM hooks.
 * We he, that is fun!
 */

/**
468
 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
469
 * @ctp: child task pointer
470
 * @mode: ptrace attachment mode (PTRACE_MODE_*)
471 472 473
 *
 * Returns 0 if access is OK, an error code otherwise
 *
474
 * Do the capability checks.
475
 */
476
static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
477
{
478
	struct smack_known *skp;
479

480
	skp = smk_of_task_struct(ctp);
E
Etienne Basset 已提交
481

C
Casey Schaufler 已提交
482
	return smk_ptrace_rule_check(current, skp, mode, __func__);
483 484 485 486 487 488 489 490
}

/**
 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
 * @ptp: parent task pointer
 *
 * Returns 0 if access is OK, an error code otherwise
 *
491
 * Do the capability checks, and require PTRACE_MODE_ATTACH.
492 493 494 495
 */
static int smack_ptrace_traceme(struct task_struct *ptp)
{
	int rc;
496
	struct smack_known *skp;
497

498
	skp = smk_of_task(current_security());
E
Etienne Basset 已提交
499

500
	rc = smk_ptrace_rule_check(ptp, skp, PTRACE_MODE_ATTACH, __func__);
501 502 503 504 505 506 507 508 509
	return rc;
}

/**
 * smack_syslog - Smack approval on syslog
 * @type: message type
 *
 * Returns 0 on success, error code otherwise.
 */
510
static int smack_syslog(int typefrom_file)
511
{
512
	int rc = 0;
513
	struct smack_known *skp = smk_of_current();
514

515
	if (smack_privileged(CAP_MAC_OVERRIDE))
516 517
		return 0;

518
	if (smack_syslog_label != NULL && smack_syslog_label != skp)
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
		rc = -EACCES;

	return rc;
}


/*
 * Superblock Hooks.
 */

/**
 * smack_sb_alloc_security - allocate a superblock blob
 * @sb: the superblock getting the blob
 *
 * Returns 0 on success or -ENOMEM on error.
 */
static int smack_sb_alloc_security(struct super_block *sb)
{
	struct superblock_smack *sbsp;

	sbsp = kzalloc(sizeof(struct superblock_smack), GFP_KERNEL);

	if (sbsp == NULL)
		return -ENOMEM;

544 545 546 547
	sbsp->smk_root = &smack_known_floor;
	sbsp->smk_default = &smack_known_floor;
	sbsp->smk_floor = &smack_known_floor;
	sbsp->smk_hat = &smack_known_hat;
548
	/*
549
	 * SMK_SB_INITIALIZED will be zero from kzalloc.
550
	 */
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
	sb->s_security = sbsp;

	return 0;
}

/**
 * smack_sb_free_security - free a superblock blob
 * @sb: the superblock getting the blob
 *
 */
static void smack_sb_free_security(struct super_block *sb)
{
	kfree(sb->s_security);
	sb->s_security = NULL;
}

/**
 * smack_sb_copy_data - copy mount options data for processing
 * @orig: where to start
570
 * @smackopts: mount options string
571 572 573 574 575 576
 *
 * Returns 0 on success or -ENOMEM on error.
 *
 * Copy the Smack specific mount options out of the mount
 * options list.
 */
577
static int smack_sb_copy_data(char *orig, char *smackopts)
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
{
	char *cp, *commap, *otheropts, *dp;

	otheropts = (char *)get_zeroed_page(GFP_KERNEL);
	if (otheropts == NULL)
		return -ENOMEM;

	for (cp = orig, commap = orig; commap != NULL; cp = commap + 1) {
		if (strstr(cp, SMK_FSDEFAULT) == cp)
			dp = smackopts;
		else if (strstr(cp, SMK_FSFLOOR) == cp)
			dp = smackopts;
		else if (strstr(cp, SMK_FSHAT) == cp)
			dp = smackopts;
		else if (strstr(cp, SMK_FSROOT) == cp)
			dp = smackopts;
594 595
		else if (strstr(cp, SMK_FSTRANS) == cp)
			dp = smackopts;
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
		else
			dp = otheropts;

		commap = strchr(cp, ',');
		if (commap != NULL)
			*commap = '\0';

		if (*dp != '\0')
			strcat(dp, ",");
		strcat(dp, cp);
	}

	strcpy(orig, otheropts);
	free_page((unsigned long)otheropts);

	return 0;
}

/**
615 616 617 618 619 620 621 622 623 624 625 626
 * smack_parse_opts_str - parse Smack specific mount options
 * @options: mount options string
 * @opts: where to store converted mount opts
 *
 * Returns 0 on success or -ENOMEM on error.
 *
 * converts Smack specific mount options to generic security option format
 */
static int smack_parse_opts_str(char *options,
		struct security_mnt_opts *opts)
{
	char *p;
627 628 629 630 631 632 633 634
	char *fsdefault = NULL;
	char *fsfloor = NULL;
	char *fshat = NULL;
	char *fsroot = NULL;
	char *fstransmute = NULL;
	int rc = -ENOMEM;
	int num_mnt_opts = 0;
	int token;
635 636 637 638 639 640 641 642 643 644 645 646

	opts->num_mnt_opts = 0;

	if (!options)
		return 0;

	while ((p = strsep(&options, ",")) != NULL) {
		substring_t args[MAX_OPT_ARGS];

		if (!*p)
			continue;

647
		token = match_token(p, smk_mount_tokens, args);
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691

		switch (token) {
		case Opt_fsdefault:
			if (fsdefault)
				goto out_opt_err;
			fsdefault = match_strdup(&args[0]);
			if (!fsdefault)
				goto out_err;
			break;
		case Opt_fsfloor:
			if (fsfloor)
				goto out_opt_err;
			fsfloor = match_strdup(&args[0]);
			if (!fsfloor)
				goto out_err;
			break;
		case Opt_fshat:
			if (fshat)
				goto out_opt_err;
			fshat = match_strdup(&args[0]);
			if (!fshat)
				goto out_err;
			break;
		case Opt_fsroot:
			if (fsroot)
				goto out_opt_err;
			fsroot = match_strdup(&args[0]);
			if (!fsroot)
				goto out_err;
			break;
		case Opt_fstransmute:
			if (fstransmute)
				goto out_opt_err;
			fstransmute = match_strdup(&args[0]);
			if (!fstransmute)
				goto out_err;
			break;
		default:
			rc = -EINVAL;
			pr_warn("Smack:  unknown mount option\n");
			goto out_err;
		}
	}

692
	opts->mnt_opts = kcalloc(NUM_SMK_MNT_OPTS, sizeof(char *), GFP_KERNEL);
693 694 695 696
	if (!opts->mnt_opts)
		goto out_err;

	opts->mnt_opts_flags = kcalloc(NUM_SMK_MNT_OPTS, sizeof(int),
697
			GFP_KERNEL);
698
	if (!opts->mnt_opts_flags)
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
		goto out_err;

	if (fsdefault) {
		opts->mnt_opts[num_mnt_opts] = fsdefault;
		opts->mnt_opts_flags[num_mnt_opts++] = FSDEFAULT_MNT;
	}
	if (fsfloor) {
		opts->mnt_opts[num_mnt_opts] = fsfloor;
		opts->mnt_opts_flags[num_mnt_opts++] = FSFLOOR_MNT;
	}
	if (fshat) {
		opts->mnt_opts[num_mnt_opts] = fshat;
		opts->mnt_opts_flags[num_mnt_opts++] = FSHAT_MNT;
	}
	if (fsroot) {
		opts->mnt_opts[num_mnt_opts] = fsroot;
		opts->mnt_opts_flags[num_mnt_opts++] = FSROOT_MNT;
	}
	if (fstransmute) {
		opts->mnt_opts[num_mnt_opts] = fstransmute;
		opts->mnt_opts_flags[num_mnt_opts++] = FSTRANS_MNT;
	}

	opts->num_mnt_opts = num_mnt_opts;
	return 0;

out_opt_err:
	rc = -EINVAL;
	pr_warn("Smack: duplicate mount options\n");

out_err:
	kfree(fsdefault);
	kfree(fsfloor);
	kfree(fshat);
	kfree(fsroot);
	kfree(fstransmute);
	return rc;
}

/**
 * smack_set_mnt_opts - set Smack specific mount options
740
 * @sb: the file system superblock
741 742 743
 * @opts: Smack mount options
 * @kern_flags: mount option from kernel space or user space
 * @set_kern_flags: where to store converted mount opts
744 745
 *
 * Returns 0 on success, an error code on failure
746 747 748
 *
 * Allow filesystems with binary mount data to explicitly set Smack mount
 * labels.
749
 */
750 751 752 753
static int smack_set_mnt_opts(struct super_block *sb,
		struct security_mnt_opts *opts,
		unsigned long kern_flags,
		unsigned long *set_kern_flags)
754 755
{
	struct dentry *root = sb->s_root;
756
	struct inode *inode = d_backing_inode(root);
757 758
	struct superblock_smack *sp = sb->s_security;
	struct inode_smack *isp;
759
	struct smack_known *skp;
760 761
	int i;
	int num_opts = opts->num_mnt_opts;
762
	int transmute = 0;
763

764
	if (sp->smk_flags & SMK_SB_INITIALIZED)
765
		return 0;
766

767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
	if (!smack_privileged(CAP_MAC_ADMIN)) {
		/*
		 * Unprivileged mounts don't get to specify Smack values.
		 */
		if (num_opts)
			return -EPERM;
		/*
		 * Unprivileged mounts get root and default from the caller.
		 */
		skp = smk_of_current();
		sp->smk_root = skp;
		sp->smk_default = skp;
		/*
		 * For a handful of fs types with no user-controlled
		 * backing store it's okay to trust security labels
		 * in the filesystem. The rest are untrusted.
		 */
		if (sb->s_user_ns != &init_user_ns &&
		    sb->s_magic != SYSFS_MAGIC && sb->s_magic != TMPFS_MAGIC &&
		    sb->s_magic != RAMFS_MAGIC) {
			transmute = 1;
			sp->smk_flags |= SMK_SB_UNTRUSTED;
		}
	}

792
	sp->smk_flags |= SMK_SB_INITIALIZED;
793

794 795 796 797
	for (i = 0; i < num_opts; i++) {
		switch (opts->mnt_opts_flags[i]) {
		case FSDEFAULT_MNT:
			skp = smk_import_entry(opts->mnt_opts[i], 0);
798 799
			if (IS_ERR(skp))
				return PTR_ERR(skp);
800 801 802 803
			sp->smk_default = skp;
			break;
		case FSFLOOR_MNT:
			skp = smk_import_entry(opts->mnt_opts[i], 0);
804 805 806
			if (IS_ERR(skp))
				return PTR_ERR(skp);
			sp->smk_floor = skp;
807 808 809
			break;
		case FSHAT_MNT:
			skp = smk_import_entry(opts->mnt_opts[i], 0);
810 811
			if (IS_ERR(skp))
				return PTR_ERR(skp);
812 813 814 815
			sp->smk_hat = skp;
			break;
		case FSROOT_MNT:
			skp = smk_import_entry(opts->mnt_opts[i], 0);
816 817 818
			if (IS_ERR(skp))
				return PTR_ERR(skp);
			sp->smk_root = skp;
819 820 821
			break;
		case FSTRANS_MNT:
			skp = smk_import_entry(opts->mnt_opts[i], 0);
822 823 824 825
			if (IS_ERR(skp))
				return PTR_ERR(skp);
			sp->smk_root = skp;
			transmute = 1;
826 827 828
			break;
		default:
			break;
829 830 831 832 833 834 835
		}
	}

	/*
	 * Initialize the root inode.
	 */
	isp = inode->i_security;
836 837 838 839 840
	if (isp == NULL) {
		isp = new_inode_smack(sp->smk_root);
		if (isp == NULL)
			return -ENOMEM;
		inode->i_security = isp;
841
	} else
842 843
		isp->smk_inode = sp->smk_root;

844 845 846
	if (transmute)
		isp->smk_flags |= SMK_INODE_TRANSMUTE;

847 848 849
	return 0;
}

850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
/**
 * smack_sb_kern_mount - Smack specific mount processing
 * @sb: the file system superblock
 * @flags: the mount flags
 * @data: the smack mount options
 *
 * Returns 0 on success, an error code on failure
 */
static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
{
	int rc = 0;
	char *options = data;
	struct security_mnt_opts opts;

	security_init_mnt_opts(&opts);

	if (!options)
		goto out;

	rc = smack_parse_opts_str(options, &opts);
	if (rc)
		goto out_err;

out:
	rc = smack_set_mnt_opts(sb, &opts, 0, NULL);

out_err:
	security_free_mnt_opts(&opts);
	return rc;
}

881 882 883 884 885 886 887 888 889 890
/**
 * smack_sb_statfs - Smack check on statfs
 * @dentry: identifies the file system in question
 *
 * Returns 0 if current can read the floor of the filesystem,
 * and error code otherwise
 */
static int smack_sb_statfs(struct dentry *dentry)
{
	struct superblock_smack *sbp = dentry->d_sb->s_security;
E
Etienne Basset 已提交
891 892 893
	int rc;
	struct smk_audit_info ad;

894
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
E
Etienne Basset 已提交
895
	smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
896

E
Etienne Basset 已提交
897
	rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
C
Casey Schaufler 已提交
898
	rc = smk_bu_current("statfs", sbp->smk_floor, MAY_READ, rc);
E
Etienne Basset 已提交
899
	return rc;
900 901
}

902 903 904 905
/*
 * BPRM hooks
 */

C
Casey Schaufler 已提交
906 907 908 909
/**
 * smack_bprm_set_creds - set creds for exec
 * @bprm: the exec information
 *
910
 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
C
Casey Schaufler 已提交
911
 */
912 913
static int smack_bprm_set_creds(struct linux_binprm *bprm)
{
A
Al Viro 已提交
914
	struct inode *inode = file_inode(bprm->file);
915
	struct task_smack *bsp = bprm->cred->security;
916
	struct inode_smack *isp;
917
	struct superblock_smack *sbsp;
918 919
	int rc;

920
	if (bprm->called_set_creds)
921 922
		return 0;

923 924
	isp = inode->i_security;
	if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
925 926
		return 0;

927 928 929 930 931
	sbsp = inode->i_sb->s_security;
	if ((sbsp->smk_flags & SMK_SB_UNTRUSTED) &&
	    isp->smk_task != sbsp->smk_root)
		return 0;

932
	if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
933 934 935 936 937 938 939
		struct task_struct *tracer;
		rc = 0;

		rcu_read_lock();
		tracer = ptrace_parent(current);
		if (likely(tracer != NULL))
			rc = smk_ptrace_rule_check(tracer,
940
						   isp->smk_task,
941 942 943 944 945 946 947
						   PTRACE_MODE_ATTACH,
						   __func__);
		rcu_read_unlock();

		if (rc != 0)
			return rc;
	} else if (bprm->unsafe)
948
		return -EPERM;
949

950 951
	bsp->smk_task = isp->smk_task;
	bprm->per_clear |= PER_CLEAR_ON_SETID;
952

953 954 955 956
	/* Decide if this is a secure exec. */
	if (bsp->smk_task != bsp->smk_forked)
		bprm->secureexec = 1;

957 958
	return 0;
}
959

960 961 962 963 964 965
/*
 * Inode hooks
 */

/**
 * smack_inode_alloc_security - allocate an inode blob
966
 * @inode: the inode in need of a blob
967 968 969 970 971
 *
 * Returns 0 if it gets a blob, -ENOMEM otherwise
 */
static int smack_inode_alloc_security(struct inode *inode)
{
972 973
	struct smack_known *skp = smk_of_current();

974
	inode->i_security = new_inode_smack(skp);
975 976 977 978 979 980
	if (inode->i_security == NULL)
		return -ENOMEM;
	return 0;
}

/**
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
 * smack_inode_free_rcu - Free inode_smack blob from cache
 * @head: the rcu_head for getting inode_smack pointer
 *
 *  Call back function called from call_rcu() to free
 *  the i_security blob pointer in inode
 */
static void smack_inode_free_rcu(struct rcu_head *head)
{
	struct inode_smack *issp;

	issp = container_of(head, struct inode_smack, smk_rcu);
	kmem_cache_free(smack_inode_cache, issp);
}

/**
 * smack_inode_free_security - free an inode blob using call_rcu()
997
 * @inode: the inode with a blob
998
 *
999
 * Clears the blob pointer in inode using RCU
1000 1001 1002
 */
static void smack_inode_free_security(struct inode *inode)
{
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
	struct inode_smack *issp = inode->i_security;

	/*
	 * The inode may still be referenced in a path walk and
	 * a call to smack_inode_permission() can be made
	 * after smack_inode_free_security() is called.
	 * To avoid race condition free the i_security via RCU
	 * and leave the current inode->i_security pointer intact.
	 * The inode will be freed after the RCU grace period too.
	 */
	call_rcu(&issp->smk_rcu, smack_inode_free_rcu);
1014 1015 1016 1017
}

/**
 * smack_inode_init_security - copy out the smack from an inode
1018 1019
 * @inode: the newly created inode
 * @dir: containing directory object
1020
 * @qstr: unused
1021 1022 1023 1024 1025 1026 1027
 * @name: where to put the attribute name
 * @value: where to put the attribute value
 * @len: where to put the length of the attribute
 *
 * Returns 0 if it all works out, -ENOMEM if there's no memory
 */
static int smack_inode_init_security(struct inode *inode, struct inode *dir,
1028
				     const struct qstr *qstr, const char **name,
1029
				     void **value, size_t *len)
1030
{
C
Casey Schaufler 已提交
1031
	struct inode_smack *issp = inode->i_security;
1032
	struct smack_known *skp = smk_of_current();
1033 1034
	struct smack_known *isp = smk_of_inode(inode);
	struct smack_known *dsp = smk_of_inode(dir);
1035
	int may;
1036

1037 1038
	if (name)
		*name = XATTR_SMACK_SUFFIX;
1039

1040
	if (value && len) {
1041
		rcu_read_lock();
1042 1043
		may = smk_access_entry(skp->smk_known, dsp->smk_known,
				       &skp->smk_rules);
1044
		rcu_read_unlock();
1045 1046 1047 1048 1049

		/*
		 * If the access rule allows transmutation and
		 * the directory requests transmutation then
		 * by all means transmute.
C
Casey Schaufler 已提交
1050
		 * Mark the inode as changed.
1051
		 */
1052
		if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
C
Casey Schaufler 已提交
1053
		    smk_inode_transmutable(dir)) {
1054
			isp = dsp;
C
Casey Schaufler 已提交
1055 1056
			issp->smk_flags |= SMK_INODE_CHANGED;
		}
1057

1058
		*value = kstrdup(isp->smk_known, GFP_NOFS);
1059 1060 1061
		if (*value == NULL)
			return -ENOMEM;

1062
		*len = strlen(isp->smk_known);
1063
	}
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078

	return 0;
}

/**
 * smack_inode_link - Smack check on link
 * @old_dentry: the existing object
 * @dir: unused
 * @new_dentry: the new object
 *
 * Returns 0 if access is permitted, an error code otherwise
 */
static int smack_inode_link(struct dentry *old_dentry, struct inode *dir,
			    struct dentry *new_dentry)
{
1079
	struct smack_known *isp;
E
Etienne Basset 已提交
1080 1081 1082
	struct smk_audit_info ad;
	int rc;

1083
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
E
Etienne Basset 已提交
1084
	smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1085

1086
	isp = smk_of_inode(d_backing_inode(old_dentry));
E
Etienne Basset 已提交
1087
	rc = smk_curacc(isp, MAY_WRITE, &ad);
1088
	rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_WRITE, rc);
1089

1090
	if (rc == 0 && d_is_positive(new_dentry)) {
1091
		isp = smk_of_inode(d_backing_inode(new_dentry));
E
Etienne Basset 已提交
1092 1093
		smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
		rc = smk_curacc(isp, MAY_WRITE, &ad);
1094
		rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_WRITE, rc);
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
	}

	return rc;
}

/**
 * smack_inode_unlink - Smack check on inode deletion
 * @dir: containing directory object
 * @dentry: file to unlink
 *
 * Returns 0 if current can write the containing directory
 * and the object, error code otherwise
 */
static int smack_inode_unlink(struct inode *dir, struct dentry *dentry)
{
1110
	struct inode *ip = d_backing_inode(dentry);
E
Etienne Basset 已提交
1111
	struct smk_audit_info ad;
1112 1113
	int rc;

1114
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
E
Etienne Basset 已提交
1115 1116
	smk_ad_setfield_u_fs_path_dentry(&ad, dentry);

1117 1118 1119
	/*
	 * You need write access to the thing you're unlinking
	 */
E
Etienne Basset 已提交
1120
	rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
C
Casey Schaufler 已提交
1121
	rc = smk_bu_inode(ip, MAY_WRITE, rc);
E
Etienne Basset 已提交
1122
	if (rc == 0) {
1123 1124 1125
		/*
		 * You also need write access to the containing directory
		 */
1126
		smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
E
Etienne Basset 已提交
1127 1128
		smk_ad_setfield_u_fs_inode(&ad, dir);
		rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
C
Casey Schaufler 已提交
1129
		rc = smk_bu_inode(dir, MAY_WRITE, rc);
E
Etienne Basset 已提交
1130
	}
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
	return rc;
}

/**
 * smack_inode_rmdir - Smack check on directory deletion
 * @dir: containing directory object
 * @dentry: directory to unlink
 *
 * Returns 0 if current can write the containing directory
 * and the directory, error code otherwise
 */
static int smack_inode_rmdir(struct inode *dir, struct dentry *dentry)
{
E
Etienne Basset 已提交
1144
	struct smk_audit_info ad;
1145 1146
	int rc;

1147
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
E
Etienne Basset 已提交
1148 1149
	smk_ad_setfield_u_fs_path_dentry(&ad, dentry);

1150 1151 1152
	/*
	 * You need write access to the thing you're removing
	 */
1153 1154
	rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
	rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
E
Etienne Basset 已提交
1155
	if (rc == 0) {
1156 1157 1158
		/*
		 * You also need write access to the containing directory
		 */
1159
		smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
E
Etienne Basset 已提交
1160 1161
		smk_ad_setfield_u_fs_inode(&ad, dir);
		rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
C
Casey Schaufler 已提交
1162
		rc = smk_bu_inode(dir, MAY_WRITE, rc);
E
Etienne Basset 已提交
1163
	}
1164 1165 1166 1167 1168 1169

	return rc;
}

/**
 * smack_inode_rename - Smack check on rename
1170 1171 1172 1173
 * @old_inode: unused
 * @old_dentry: the old object
 * @new_inode: unused
 * @new_dentry: the new object
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
 *
 * Read and write access is required on both the old and
 * new directories.
 *
 * Returns 0 if access is permitted, an error code otherwise
 */
static int smack_inode_rename(struct inode *old_inode,
			      struct dentry *old_dentry,
			      struct inode *new_inode,
			      struct dentry *new_dentry)
{
	int rc;
1186
	struct smack_known *isp;
E
Etienne Basset 已提交
1187 1188
	struct smk_audit_info ad;

1189
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
E
Etienne Basset 已提交
1190
	smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
1191

1192
	isp = smk_of_inode(d_backing_inode(old_dentry));
E
Etienne Basset 已提交
1193
	rc = smk_curacc(isp, MAY_READWRITE, &ad);
1194
	rc = smk_bu_inode(d_backing_inode(old_dentry), MAY_READWRITE, rc);
1195

1196
	if (rc == 0 && d_is_positive(new_dentry)) {
1197
		isp = smk_of_inode(d_backing_inode(new_dentry));
E
Etienne Basset 已提交
1198 1199
		smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
		rc = smk_curacc(isp, MAY_READWRITE, &ad);
1200
		rc = smk_bu_inode(d_backing_inode(new_dentry), MAY_READWRITE, rc);
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
	}
	return rc;
}

/**
 * smack_inode_permission - Smack version of permission()
 * @inode: the inode in question
 * @mask: the access requested
 *
 * This is the important Smack hook.
 *
 * Returns 0 if access is permitted, -EACCES otherwise
 */
1214
static int smack_inode_permission(struct inode *inode, int mask)
1215
{
1216
	struct superblock_smack *sbsp = inode->i_sb->s_security;
E
Etienne Basset 已提交
1217
	struct smk_audit_info ad;
1218
	int no_block = mask & MAY_NOT_BLOCK;
C
Casey Schaufler 已提交
1219
	int rc;
1220 1221

	mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
1222 1223 1224 1225 1226
	/*
	 * No permission to check. Existence test. Yup, it's there.
	 */
	if (mask == 0)
		return 0;
1227

1228 1229 1230 1231 1232
	if (sbsp->smk_flags & SMK_SB_UNTRUSTED) {
		if (smk_of_inode(inode) != sbsp->smk_root)
			return -EACCES;
	}

1233
	/* May be droppable after audit */
1234
	if (no_block)
1235
		return -ECHILD;
1236
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
E
Etienne Basset 已提交
1237
	smk_ad_setfield_u_fs_inode(&ad, inode);
C
Casey Schaufler 已提交
1238 1239 1240
	rc = smk_curacc(smk_of_inode(inode), mask, &ad);
	rc = smk_bu_inode(inode, mask, rc);
	return rc;
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
}

/**
 * smack_inode_setattr - Smack check for setting attributes
 * @dentry: the object
 * @iattr: for the force flag
 *
 * Returns 0 if access is permitted, an error code otherwise
 */
static int smack_inode_setattr(struct dentry *dentry, struct iattr *iattr)
{
E
Etienne Basset 已提交
1252
	struct smk_audit_info ad;
C
Casey Schaufler 已提交
1253 1254
	int rc;

1255 1256 1257 1258 1259
	/*
	 * Need to allow for clearing the setuid bit.
	 */
	if (iattr->ia_valid & ATTR_FORCE)
		return 0;
1260
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
E
Etienne Basset 已提交
1261
	smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1262

1263 1264
	rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
	rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
C
Casey Schaufler 已提交
1265
	return rc;
1266 1267 1268 1269
}

/**
 * smack_inode_getattr - Smack check for getting attributes
1270
 * @mnt: vfsmount of the object
1271 1272 1273 1274
 * @dentry: the object
 *
 * Returns 0 if access is permitted, an error code otherwise
 */
1275
static int smack_inode_getattr(const struct path *path)
1276
{
E
Etienne Basset 已提交
1277
	struct smk_audit_info ad;
1278
	struct inode *inode = d_backing_inode(path->dentry);
C
Casey Schaufler 已提交
1279
	int rc;
E
Etienne Basset 已提交
1280

1281
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
1282 1283 1284
	smk_ad_setfield_u_fs_path(&ad, *path);
	rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
	rc = smk_bu_inode(inode, MAY_READ, rc);
C
Casey Schaufler 已提交
1285
	return rc;
1286 1287 1288 1289 1290 1291
}

/**
 * smack_inode_setxattr - Smack check for setting xattrs
 * @dentry: the object
 * @name: name of the attribute
1292 1293
 * @value: value of the attribute
 * @size: size of the value
1294 1295 1296 1297 1298 1299
 * @flags: unused
 *
 * This protects the Smack attribute explicitly.
 *
 * Returns 0 if access is permitted, an error code otherwise
 */
1300 1301
static int smack_inode_setxattr(struct dentry *dentry, const char *name,
				const void *value, size_t size, int flags)
1302
{
E
Etienne Basset 已提交
1303
	struct smk_audit_info ad;
1304 1305 1306 1307
	struct smack_known *skp;
	int check_priv = 0;
	int check_import = 0;
	int check_star = 0;
1308
	int rc = 0;
1309

1310 1311 1312
	/*
	 * Check label validity here so import won't fail in post_setxattr
	 */
1313 1314
	if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
	    strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1315 1316 1317 1318 1319 1320 1321 1322
	    strcmp(name, XATTR_NAME_SMACKIPOUT) == 0) {
		check_priv = 1;
		check_import = 1;
	} else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
		   strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
		check_priv = 1;
		check_import = 1;
		check_star = 1;
1323
	} else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
1324
		check_priv = 1;
1325 1326 1327
		if (size != TRANS_TRUE_SIZE ||
		    strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
			rc = -EINVAL;
1328 1329 1330
	} else
		rc = cap_inode_setxattr(dentry, name, value, size, flags);

1331 1332 1333 1334
	if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
		rc = -EPERM;

	if (rc == 0 && check_import) {
1335
		skp = size ? smk_import_entry(value, size) : NULL;
1336 1337 1338
		if (IS_ERR(skp))
			rc = PTR_ERR(skp);
		else if (skp == NULL || (check_star &&
1339 1340 1341 1342
		    (skp == &smack_known_star || skp == &smack_known_web)))
			rc = -EINVAL;
	}

1343
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
E
Etienne Basset 已提交
1344 1345
	smk_ad_setfield_u_fs_path_dentry(&ad, dentry);

C
Casey Schaufler 已提交
1346
	if (rc == 0) {
1347 1348
		rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
		rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
C
Casey Schaufler 已提交
1349
	}
1350 1351

	return rc;
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
}

/**
 * smack_inode_post_setxattr - Apply the Smack update approved above
 * @dentry: object
 * @name: attribute name
 * @value: attribute value
 * @size: attribute size
 * @flags: unused
 *
 * Set the pointer in the inode blob to the entry found
 * in the master label list.
 */
1365 1366
static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
				      const void *value, size_t size, int flags)
1367
{
1368
	struct smack_known *skp;
1369
	struct inode_smack *isp = d_backing_inode(dentry)->i_security;
1370

1371 1372 1373 1374 1375
	if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
		isp->smk_flags |= SMK_INODE_TRANSMUTE;
		return;
	}

1376
	if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1377
		skp = smk_import_entry(value, size);
1378
		if (!IS_ERR(skp))
1379
			isp->smk_inode = skp;
1380
	} else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
1381
		skp = smk_import_entry(value, size);
1382
		if (!IS_ERR(skp))
1383
			isp->smk_task = skp;
1384
	} else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1385
		skp = smk_import_entry(value, size);
1386
		if (!IS_ERR(skp))
1387 1388
			isp->smk_mmap = skp;
	}
1389 1390 1391 1392

	return;
}

C
Casey Schaufler 已提交
1393
/**
1394 1395 1396 1397 1398 1399
 * smack_inode_getxattr - Smack check on getxattr
 * @dentry: the object
 * @name: unused
 *
 * Returns 0 if access is permitted, an error code otherwise
 */
1400
static int smack_inode_getxattr(struct dentry *dentry, const char *name)
1401
{
E
Etienne Basset 已提交
1402
	struct smk_audit_info ad;
C
Casey Schaufler 已提交
1403
	int rc;
E
Etienne Basset 已提交
1404

1405
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
E
Etienne Basset 已提交
1406 1407
	smk_ad_setfield_u_fs_path_dentry(&ad, dentry);

1408 1409
	rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_READ, &ad);
	rc = smk_bu_inode(d_backing_inode(dentry), MAY_READ, rc);
C
Casey Schaufler 已提交
1410
	return rc;
1411 1412
}

C
Casey Schaufler 已提交
1413
/**
1414 1415 1416 1417 1418 1419 1420 1421
 * smack_inode_removexattr - Smack check on removexattr
 * @dentry: the object
 * @name: name of the attribute
 *
 * Removing the Smack attribute requires CAP_MAC_ADMIN
 *
 * Returns 0 if access is permitted, an error code otherwise
 */
1422
static int smack_inode_removexattr(struct dentry *dentry, const char *name)
1423
{
1424
	struct inode_smack *isp;
E
Etienne Basset 已提交
1425
	struct smk_audit_info ad;
1426
	int rc = 0;
1427

1428 1429
	if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
	    strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1430
	    strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1431
	    strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1432
	    strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
P
Pankaj Kumar 已提交
1433
	    strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1434
		if (!smack_privileged(CAP_MAC_ADMIN))
1435 1436 1437 1438
			rc = -EPERM;
	} else
		rc = cap_inode_removexattr(dentry, name);

1439 1440 1441
	if (rc != 0)
		return rc;

1442
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
E
Etienne Basset 已提交
1443
	smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1444

1445 1446
	rc = smk_curacc(smk_of_inode(d_backing_inode(dentry)), MAY_WRITE, &ad);
	rc = smk_bu_inode(d_backing_inode(dentry), MAY_WRITE, rc);
1447 1448 1449
	if (rc != 0)
		return rc;

1450
	isp = d_backing_inode(dentry)->i_security;
1451 1452 1453 1454 1455
	/*
	 * Don't do anything special for these.
	 *	XATTR_NAME_SMACKIPIN
	 *	XATTR_NAME_SMACKIPOUT
	 */
J
José Bollo 已提交
1456
	if (strcmp(name, XATTR_NAME_SMACK) == 0) {
1457
		struct super_block *sbp = dentry->d_sb;
J
José Bollo 已提交
1458 1459 1460 1461
		struct superblock_smack *sbsp = sbp->s_security;

		isp->smk_inode = sbsp->smk_default;
	} else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0)
1462
		isp->smk_task = NULL;
1463
	else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1464
		isp->smk_mmap = NULL;
1465 1466
	else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
		isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1467

1468
	return 0;
1469 1470 1471 1472 1473 1474 1475
}

/**
 * smack_inode_getsecurity - get smack xattrs
 * @inode: the object
 * @name: attribute name
 * @buffer: where to put the result
1476
 * @alloc: duplicate memory
1477 1478 1479
 *
 * Returns the size of the attribute or an error code
 */
1480
static int smack_inode_getsecurity(struct inode *inode,
1481 1482 1483 1484 1485 1486 1487
				   const char *name, void **buffer,
				   bool alloc)
{
	struct socket_smack *ssp;
	struct socket *sock;
	struct super_block *sbp;
	struct inode *ip = (struct inode *)inode;
1488
	struct smack_known *isp;
1489

1490
	if (strcmp(name, XATTR_SMACK_SUFFIX) == 0)
1491
		isp = smk_of_inode(inode);
1492 1493 1494 1495 1496 1497 1498
	else {
		/*
		 * The rest of the Smack xattrs are only on sockets.
		 */
		sbp = ip->i_sb;
		if (sbp->s_magic != SOCKFS_MAGIC)
			return -EOPNOTSUPP;
1499

1500 1501 1502
		sock = SOCKET_I(ip);
		if (sock == NULL || sock->sk == NULL)
			return -EOPNOTSUPP;
1503

1504
		ssp = sock->sk->sk_security;
1505

1506 1507 1508 1509 1510 1511 1512
		if (strcmp(name, XATTR_SMACK_IPIN) == 0)
			isp = ssp->smk_in;
		else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
			isp = ssp->smk_out;
		else
			return -EOPNOTSUPP;
	}
1513

1514 1515 1516 1517
	if (alloc) {
		*buffer = kstrdup(isp->smk_known, GFP_KERNEL);
		if (*buffer == NULL)
			return -ENOMEM;
1518 1519
	}

1520
	return strlen(isp->smk_known);
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
}


/**
 * smack_inode_listsecurity - list the Smack attributes
 * @inode: the object
 * @buffer: where they go
 * @buffer_size: size of buffer
 */
static int smack_inode_listsecurity(struct inode *inode, char *buffer,
				    size_t buffer_size)
{
1533
	int len = sizeof(XATTR_NAME_SMACK);
1534

1535
	if (buffer != NULL && len <= buffer_size)
1536
		memcpy(buffer, XATTR_NAME_SMACK, len);
1537 1538

	return len;
1539 1540
}

1541 1542 1543 1544 1545
/**
 * smack_inode_getsecid - Extract inode's security id
 * @inode: inode to extract the info from
 * @secid: where result will be saved
 */
1546
static void smack_inode_getsecid(struct inode *inode, u32 *secid)
1547 1548 1549
{
	struct inode_smack *isp = inode->i_security;

1550
	*secid = isp->smk_inode->smk_secid;
1551 1552
}

1553 1554 1555 1556
/*
 * File Hooks
 */

C
Casey Schaufler 已提交
1557 1558
/*
 * There is no smack_file_permission hook
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
 *
 * Should access checks be done on each read or write?
 * UNICOS and SELinux say yes.
 * Trusted Solaris, Trusted Irix, and just about everyone else says no.
 *
 * I'll say no for now. Smack does not do the frequent
 * label changing that SELinux does.
 */

/**
 * smack_file_alloc_security - assign a file security blob
 * @file: the object
 *
 * The security blob for a file is a pointer to the master
 * label list, so no allocation is done.
 *
C
Casey Schaufler 已提交
1575 1576 1577
 * f_security is the owner security information. It
 * isn't used on file access checks, it's for send_sigio.
 *
1578 1579 1580 1581
 * Returns 0
 */
static int smack_file_alloc_security(struct file *file)
{
1582 1583
	struct smack_known *skp = smk_of_current();

1584
	file->f_security = skp;
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
	return 0;
}

/**
 * smack_file_free_security - clear a file security blob
 * @file: the object
 *
 * The security blob for a file is a pointer to the master
 * label list, so no memory is freed.
 */
static void smack_file_free_security(struct file *file)
{
	file->f_security = NULL;
}

/**
 * smack_file_ioctl - Smack check on ioctls
 * @file: the object
 * @cmd: what to do
 * @arg: unused
 *
 * Relies heavily on the correct use of the ioctl command conventions.
 *
 * Returns 0 if allowed, error code otherwise
 */
static int smack_file_ioctl(struct file *file, unsigned int cmd,
			    unsigned long arg)
{
	int rc = 0;
E
Etienne Basset 已提交
1614
	struct smk_audit_info ad;
C
Casey Schaufler 已提交
1615
	struct inode *inode = file_inode(file);
E
Etienne Basset 已提交
1616

1617 1618 1619
	if (unlikely(IS_PRIVATE(inode)))
		return 0;

1620
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
E
Etienne Basset 已提交
1621
	smk_ad_setfield_u_fs_path(&ad, file->f_path);
1622

C
Casey Schaufler 已提交
1623
	if (_IOC_DIR(cmd) & _IOC_WRITE) {
C
Casey Schaufler 已提交
1624
		rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
C
Casey Schaufler 已提交
1625 1626
		rc = smk_bu_file(file, MAY_WRITE, rc);
	}
1627

C
Casey Schaufler 已提交
1628
	if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ)) {
C
Casey Schaufler 已提交
1629
		rc = smk_curacc(smk_of_inode(inode), MAY_READ, &ad);
C
Casey Schaufler 已提交
1630 1631
		rc = smk_bu_file(file, MAY_READ, rc);
	}
1632 1633 1634 1635 1636 1637 1638

	return rc;
}

/**
 * smack_file_lock - Smack check on file locking
 * @file: the object
1639
 * @cmd: unused
1640
 *
1641
 * Returns 0 if current has lock access, error code otherwise
1642 1643 1644
 */
static int smack_file_lock(struct file *file, unsigned int cmd)
{
E
Etienne Basset 已提交
1645
	struct smk_audit_info ad;
C
Casey Schaufler 已提交
1646
	int rc;
C
Casey Schaufler 已提交
1647
	struct inode *inode = file_inode(file);
E
Etienne Basset 已提交
1648

1649 1650 1651
	if (unlikely(IS_PRIVATE(inode)))
		return 0;

1652 1653
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
	smk_ad_setfield_u_fs_path(&ad, file->f_path);
C
Casey Schaufler 已提交
1654
	rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
C
Casey Schaufler 已提交
1655 1656
	rc = smk_bu_file(file, MAY_LOCK, rc);
	return rc;
1657 1658 1659 1660 1661 1662 1663 1664
}

/**
 * smack_file_fcntl - Smack check on fcntl
 * @file: the object
 * @cmd: what action to check
 * @arg: unused
 *
1665 1666 1667 1668
 * Generally these operations are harmless.
 * File locking operations present an obvious mechanism
 * for passing information, so they require write access.
 *
1669 1670 1671 1672 1673
 * Returns 0 if current has access, error code otherwise
 */
static int smack_file_fcntl(struct file *file, unsigned int cmd,
			    unsigned long arg)
{
E
Etienne Basset 已提交
1674
	struct smk_audit_info ad;
1675
	int rc = 0;
C
Casey Schaufler 已提交
1676
	struct inode *inode = file_inode(file);
E
Etienne Basset 已提交
1677

1678 1679 1680
	if (unlikely(IS_PRIVATE(inode)))
		return 0;

1681 1682
	switch (cmd) {
	case F_GETLK:
1683
		break;
1684 1685
	case F_SETLK:
	case F_SETLKW:
1686 1687
		smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
		smk_ad_setfield_u_fs_path(&ad, file->f_path);
C
Casey Schaufler 已提交
1688
		rc = smk_curacc(smk_of_inode(inode), MAY_LOCK, &ad);
C
Casey Schaufler 已提交
1689
		rc = smk_bu_file(file, MAY_LOCK, rc);
1690
		break;
1691 1692
	case F_SETOWN:
	case F_SETSIG:
1693 1694
		smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
		smk_ad_setfield_u_fs_path(&ad, file->f_path);
C
Casey Schaufler 已提交
1695
		rc = smk_curacc(smk_of_inode(inode), MAY_WRITE, &ad);
C
Casey Schaufler 已提交
1696
		rc = smk_bu_file(file, MAY_WRITE, rc);
1697 1698
		break;
	default:
1699
		break;
1700 1701 1702 1703 1704
	}

	return rc;
}

1705
/**
1706
 * smack_mmap_file :
1707 1708 1709 1710 1711 1712 1713 1714
 * Check permissions for a mmap operation.  The @file may be NULL, e.g.
 * if mapping anonymous memory.
 * @file contains the file structure for file to map (may be NULL).
 * @reqprot contains the protection requested by the application.
 * @prot contains the protection that will be applied by the kernel.
 * @flags contains the operational flags.
 * Return 0 if permission is granted.
 */
1715
static int smack_mmap_file(struct file *file,
1716
			   unsigned long reqprot, unsigned long prot,
1717
			   unsigned long flags)
1718
{
1719
	struct smack_known *skp;
1720
	struct smack_known *mkp;
1721 1722
	struct smack_rule *srp;
	struct task_smack *tsp;
1723
	struct smack_known *okp;
1724
	struct inode_smack *isp;
1725
	struct superblock_smack *sbsp;
1726 1727 1728
	int may;
	int mmay;
	int tmay;
1729 1730
	int rc;

A
Al Viro 已提交
1731
	if (file == NULL)
1732 1733
		return 0;

1734 1735 1736
	if (unlikely(IS_PRIVATE(file_inode(file))))
		return 0;

A
Al Viro 已提交
1737
	isp = file_inode(file)->i_security;
1738 1739
	if (isp->smk_mmap == NULL)
		return 0;
1740 1741 1742 1743
	sbsp = file_inode(file)->i_sb->s_security;
	if (sbsp->smk_flags & SMK_SB_UNTRUSTED &&
	    isp->smk_mmap != sbsp->smk_root)
		return -EACCES;
1744
	mkp = isp->smk_mmap;
1745 1746

	tsp = current_security();
1747
	skp = smk_of_current();
1748 1749 1750 1751 1752 1753 1754 1755
	rc = 0;

	rcu_read_lock();
	/*
	 * For each Smack rule associated with the subject
	 * label verify that the SMACK64MMAP also has access
	 * to that rule's object label.
	 */
1756
	list_for_each_entry_rcu(srp, &skp->smk_rules, list) {
1757
		okp = srp->smk_object;
1758 1759 1760
		/*
		 * Matching labels always allows access.
		 */
1761
		if (mkp->smk_known == okp->smk_known)
1762
			continue;
1763 1764 1765 1766
		/*
		 * If there is a matching local rule take
		 * that into account as well.
		 */
1767 1768 1769
		may = smk_access_entry(srp->smk_subject->smk_known,
				       okp->smk_known,
				       &tsp->smk_rules);
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785
		if (may == -ENOENT)
			may = srp->smk_access;
		else
			may &= srp->smk_access;
		/*
		 * If may is zero the SMACK64MMAP subject can't
		 * possibly have less access.
		 */
		if (may == 0)
			continue;

		/*
		 * Fetch the global list entry.
		 * If there isn't one a SMACK64MMAP subject
		 * can't have as much access as current.
		 */
1786 1787
		mmay = smk_access_entry(mkp->smk_known, okp->smk_known,
					&mkp->smk_rules);
1788 1789 1790 1791 1792 1793 1794 1795
		if (mmay == -ENOENT) {
			rc = -EACCES;
			break;
		}
		/*
		 * If there is a local entry it modifies the
		 * potential access, too.
		 */
1796 1797
		tmay = smk_access_entry(mkp->smk_known, okp->smk_known,
					&tsp->smk_rules);
1798 1799
		if (tmay != -ENOENT)
			mmay &= tmay;
1800

1801 1802 1803 1804 1805
		/*
		 * If there is any access available to current that is
		 * not available to a SMACK64MMAP subject
		 * deny access.
		 */
1806
		if ((may | mmay) != mmay) {
1807
			rc = -EACCES;
1808
			break;
1809
		}
1810 1811 1812 1813 1814 1815 1816
	}

	rcu_read_unlock();

	return rc;
}

1817 1818 1819 1820 1821
/**
 * smack_file_set_fowner - set the file security blob value
 * @file: object in question
 *
 */
1822
static void smack_file_set_fowner(struct file *file)
1823
{
C
Casey Schaufler 已提交
1824
	file->f_security = smk_of_current();
1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840
}

/**
 * smack_file_send_sigiotask - Smack on sigio
 * @tsk: The target task
 * @fown: the object the signal come from
 * @signum: unused
 *
 * Allow a privileged task to get signals even if it shouldn't
 *
 * Returns 0 if a subject with the object's smack could
 * write to the task, an error code otherwise.
 */
static int smack_file_send_sigiotask(struct task_struct *tsk,
				     struct fown_struct *fown, int signum)
{
1841 1842
	struct smack_known *skp;
	struct smack_known *tkp = smk_of_task(tsk->cred->security);
1843 1844
	struct file *file;
	int rc;
E
Etienne Basset 已提交
1845
	struct smk_audit_info ad;
1846 1847 1848 1849 1850

	/*
	 * struct fown_struct is never outside the context of a struct file
	 */
	file = container_of(fown, struct file, f_owner);
1851

E
Etienne Basset 已提交
1852
	/* we don't log here as rc can be overriden */
1853
	skp = file->f_security;
1854 1855
	rc = smk_access(skp, tkp, MAY_DELIVER, NULL);
	rc = smk_bu_note("sigiotask", skp, tkp, MAY_DELIVER, rc);
1856
	if (rc != 0 && has_capability(tsk, CAP_MAC_OVERRIDE))
E
Etienne Basset 已提交
1857 1858 1859 1860
		rc = 0;

	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
	smk_ad_setfield_u_tsk(&ad, tsk);
1861
	smack_log(skp->smk_known, tkp->smk_known, MAY_DELIVER, rc, &ad);
1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
	return rc;
}

/**
 * smack_file_receive - Smack file receive check
 * @file: the object
 *
 * Returns 0 if current has access, error code otherwise
 */
static int smack_file_receive(struct file *file)
{
C
Casey Schaufler 已提交
1873
	int rc;
1874
	int may = 0;
E
Etienne Basset 已提交
1875
	struct smk_audit_info ad;
C
Casey Schaufler 已提交
1876
	struct inode *inode = file_inode(file);
C
Casey Schaufler 已提交
1877 1878 1879
	struct socket *sock;
	struct task_smack *tsp;
	struct socket_smack *ssp;
1880

1881 1882 1883
	if (unlikely(IS_PRIVATE(inode)))
		return 0;

1884
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
E
Etienne Basset 已提交
1885
	smk_ad_setfield_u_fs_path(&ad, file->f_path);
C
Casey Schaufler 已提交
1886

1887
	if (inode->i_sb->s_magic == SOCKFS_MAGIC) {
C
Casey Schaufler 已提交
1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
		sock = SOCKET_I(inode);
		ssp = sock->sk->sk_security;
		tsp = current_security();
		/*
		 * If the receiving process can't write to the
		 * passed socket or if the passed socket can't
		 * write to the receiving process don't accept
		 * the passed socket.
		 */
		rc = smk_access(tsp->smk_task, ssp->smk_out, MAY_WRITE, &ad);
		rc = smk_bu_file(file, may, rc);
		if (rc < 0)
			return rc;
		rc = smk_access(ssp->smk_in, tsp->smk_task, MAY_WRITE, &ad);
		rc = smk_bu_file(file, may, rc);
		return rc;
	}
1905 1906 1907 1908 1909 1910 1911 1912
	/*
	 * This code relies on bitmasks.
	 */
	if (file->f_mode & FMODE_READ)
		may = MAY_READ;
	if (file->f_mode & FMODE_WRITE)
		may |= MAY_WRITE;

C
Casey Schaufler 已提交
1913
	rc = smk_curacc(smk_of_inode(inode), may, &ad);
C
Casey Schaufler 已提交
1914 1915
	rc = smk_bu_file(file, may, rc);
	return rc;
1916 1917
}

1918
/**
1919
 * smack_file_open - Smack dentry open processing
1920
 * @file: the object
1921
 * @cred: task credential
1922 1923
 *
 * Set the security blob in the file structure.
1924 1925 1926
 * Allow the open only if the task has read access. There are
 * many read operations (e.g. fstat) that you can do with an
 * fd even if you have the file open write-only.
1927 1928 1929
 *
 * Returns 0
 */
1930
static int smack_file_open(struct file *file, const struct cred *cred)
1931
{
1932
	struct task_smack *tsp = cred->security;
C
Casey Schaufler 已提交
1933
	struct inode *inode = file_inode(file);
1934 1935
	struct smk_audit_info ad;
	int rc;
1936

1937 1938
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
	smk_ad_setfield_u_fs_path(&ad, file->f_path);
1939
	rc = smk_tskacc(tsp, smk_of_inode(inode), MAY_READ, &ad);
C
Casey Schaufler 已提交
1940
	rc = smk_bu_credfile(cred, file, MAY_READ, rc);
1941 1942

	return rc;
1943 1944
}

1945 1946 1947 1948
/*
 * Task hooks
 */

1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959
/**
 * smack_cred_alloc_blank - "allocate" blank task-level security credentials
 * @new: the new credentials
 * @gfp: the atomicity of any memory allocations
 *
 * Prepare a blank set of credentials for modification.  This must allocate all
 * the memory the LSM module might require such that cred_transfer() can
 * complete without error.
 */
static int smack_cred_alloc_blank(struct cred *cred, gfp_t gfp)
{
1960 1961 1962 1963
	struct task_smack *tsp;

	tsp = new_task_smack(NULL, NULL, gfp);
	if (tsp == NULL)
1964
		return -ENOMEM;
1965 1966 1967

	cred->security = tsp;

1968 1969 1970 1971
	return 0;
}


1972
/**
1973 1974
 * smack_cred_free - "free" task-level security credentials
 * @cred: the credentials in question
1975 1976
 *
 */
1977
static void smack_cred_free(struct cred *cred)
1978
{
1979 1980 1981 1982 1983 1984 1985 1986 1987
	struct task_smack *tsp = cred->security;
	struct smack_rule *rp;
	struct list_head *l;
	struct list_head *n;

	if (tsp == NULL)
		return;
	cred->security = NULL;

1988 1989
	smk_destroy_label_list(&tsp->smk_relabel);

1990 1991 1992 1993 1994 1995
	list_for_each_safe(l, n, &tsp->smk_rules) {
		rp = list_entry(l, struct smack_rule, list);
		list_del(&rp->list);
		kfree(rp);
	}
	kfree(tsp);
1996 1997
}

D
David Howells 已提交
1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008
/**
 * smack_cred_prepare - prepare new set of credentials for modification
 * @new: the new credentials
 * @old: the original credentials
 * @gfp: the atomicity of any memory allocations
 *
 * Prepare a new set of credentials for modification.
 */
static int smack_cred_prepare(struct cred *new, const struct cred *old,
			      gfp_t gfp)
{
2009 2010
	struct task_smack *old_tsp = old->security;
	struct task_smack *new_tsp;
2011
	int rc;
2012

2013
	new_tsp = new_task_smack(old_tsp->smk_task, old_tsp->smk_task, gfp);
2014 2015 2016
	if (new_tsp == NULL)
		return -ENOMEM;

2017 2018
	new->security = new_tsp;

2019 2020 2021 2022
	rc = smk_copy_rules(&new_tsp->smk_rules, &old_tsp->smk_rules, gfp);
	if (rc != 0)
		return rc;

2023 2024 2025 2026 2027
	rc = smk_copy_relabel(&new_tsp->smk_relabel, &old_tsp->smk_relabel,
				gfp);
	if (rc != 0)
		return rc;

D
David Howells 已提交
2028 2029 2030
	return 0;
}

2031 2032 2033 2034 2035 2036 2037 2038 2039
/**
 * smack_cred_transfer - Transfer the old credentials to the new credentials
 * @new: the new credentials
 * @old: the original credentials
 *
 * Fill in a set of blank credentials from another set of credentials.
 */
static void smack_cred_transfer(struct cred *new, const struct cred *old)
{
2040 2041 2042 2043 2044
	struct task_smack *old_tsp = old->security;
	struct task_smack *new_tsp = new->security;

	new_tsp->smk_task = old_tsp->smk_task;
	new_tsp->smk_forked = old_tsp->smk_task;
2045 2046 2047 2048 2049
	mutex_init(&new_tsp->smk_rules_lock);
	INIT_LIST_HEAD(&new_tsp->smk_rules);


	/* cbs copy rule list */
2050 2051
}

2052 2053
/**
 * smack_kernel_act_as - Set the subjective context in a set of credentials
2054 2055
 * @new: points to the set of credentials to be modified.
 * @secid: specifies the security ID to be set
2056 2057 2058 2059 2060
 *
 * Set the security data for a kernel service.
 */
static int smack_kernel_act_as(struct cred *new, u32 secid)
{
2061
	struct task_smack *new_tsp = new->security;
2062

2063
	new_tsp->smk_task = smack_from_secid(secid);
2064 2065 2066 2067 2068
	return 0;
}

/**
 * smack_kernel_create_files_as - Set the file creation label in a set of creds
2069 2070
 * @new: points to the set of credentials to be modified
 * @inode: points to the inode to use as a reference
2071 2072 2073 2074 2075 2076 2077 2078
 *
 * Set the file creation context in a set of credentials to the same
 * as the objective context of the specified inode
 */
static int smack_kernel_create_files_as(struct cred *new,
					struct inode *inode)
{
	struct inode_smack *isp = inode->i_security;
2079
	struct task_smack *tsp = new->security;
2080

2081
	tsp->smk_forked = isp->smk_inode;
2082
	tsp->smk_task = tsp->smk_forked;
2083 2084 2085
	return 0;
}

E
Etienne Basset 已提交
2086 2087 2088
/**
 * smk_curacc_on_task - helper to log task related access
 * @p: the task object
2089 2090
 * @access: the access requested
 * @caller: name of the calling function for audit
E
Etienne Basset 已提交
2091 2092 2093
 *
 * Return 0 if access is permitted
 */
2094 2095
static int smk_curacc_on_task(struct task_struct *p, int access,
				const char *caller)
E
Etienne Basset 已提交
2096 2097
{
	struct smk_audit_info ad;
2098
	struct smack_known *skp = smk_of_task_struct(p);
C
Casey Schaufler 已提交
2099
	int rc;
E
Etienne Basset 已提交
2100

2101
	smk_ad_init(&ad, caller, LSM_AUDIT_DATA_TASK);
E
Etienne Basset 已提交
2102
	smk_ad_setfield_u_tsk(&ad, p);
2103
	rc = smk_curacc(skp, access, &ad);
C
Casey Schaufler 已提交
2104 2105
	rc = smk_bu_task(p, access, rc);
	return rc;
E
Etienne Basset 已提交
2106 2107
}

2108 2109 2110 2111 2112 2113 2114 2115 2116
/**
 * smack_task_setpgid - Smack check on setting pgid
 * @p: the task object
 * @pgid: unused
 *
 * Return 0 if write access is permitted
 */
static int smack_task_setpgid(struct task_struct *p, pid_t pgid)
{
2117
	return smk_curacc_on_task(p, MAY_WRITE, __func__);
2118 2119 2120 2121 2122 2123 2124 2125 2126 2127
}

/**
 * smack_task_getpgid - Smack access check for getpgid
 * @p: the object task
 *
 * Returns 0 if current can read the object task, error code otherwise
 */
static int smack_task_getpgid(struct task_struct *p)
{
2128
	return smk_curacc_on_task(p, MAY_READ, __func__);
2129 2130 2131 2132 2133 2134 2135 2136 2137 2138
}

/**
 * smack_task_getsid - Smack access check for getsid
 * @p: the object task
 *
 * Returns 0 if current can read the object task, error code otherwise
 */
static int smack_task_getsid(struct task_struct *p)
{
2139
	return smk_curacc_on_task(p, MAY_READ, __func__);
2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150
}

/**
 * smack_task_getsecid - get the secid of the task
 * @p: the object task
 * @secid: where to put the result
 *
 * Sets the secid to contain a u32 version of the smack label.
 */
static void smack_task_getsecid(struct task_struct *p, u32 *secid)
{
2151
	struct smack_known *skp = smk_of_task_struct(p);
2152 2153

	*secid = skp->smk_secid;
2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164
}

/**
 * smack_task_setnice - Smack check on setting nice
 * @p: the task object
 * @nice: unused
 *
 * Return 0 if write access is permitted
 */
static int smack_task_setnice(struct task_struct *p, int nice)
{
C
Casey Schaufler 已提交
2165
	return smk_curacc_on_task(p, MAY_WRITE, __func__);
2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176
}

/**
 * smack_task_setioprio - Smack check on setting ioprio
 * @p: the task object
 * @ioprio: unused
 *
 * Return 0 if write access is permitted
 */
static int smack_task_setioprio(struct task_struct *p, int ioprio)
{
C
Casey Schaufler 已提交
2177
	return smk_curacc_on_task(p, MAY_WRITE, __func__);
2178 2179 2180 2181 2182 2183 2184 2185 2186 2187
}

/**
 * smack_task_getioprio - Smack check on reading ioprio
 * @p: the task object
 *
 * Return 0 if read access is permitted
 */
static int smack_task_getioprio(struct task_struct *p)
{
2188
	return smk_curacc_on_task(p, MAY_READ, __func__);
2189 2190 2191 2192 2193 2194 2195 2196 2197 2198
}

/**
 * smack_task_setscheduler - Smack check on setting scheduler
 * @p: the task object
 * @policy: unused
 * @lp: unused
 *
 * Return 0 if read access is permitted
 */
2199
static int smack_task_setscheduler(struct task_struct *p)
2200
{
C
Casey Schaufler 已提交
2201
	return smk_curacc_on_task(p, MAY_WRITE, __func__);
2202 2203 2204 2205 2206 2207 2208 2209 2210 2211
}

/**
 * smack_task_getscheduler - Smack check on reading scheduler
 * @p: the task object
 *
 * Return 0 if read access is permitted
 */
static int smack_task_getscheduler(struct task_struct *p)
{
2212
	return smk_curacc_on_task(p, MAY_READ, __func__);
2213 2214 2215 2216 2217 2218 2219 2220 2221 2222
}

/**
 * smack_task_movememory - Smack check on moving memory
 * @p: the task object
 *
 * Return 0 if write access is permitted
 */
static int smack_task_movememory(struct task_struct *p)
{
2223
	return smk_curacc_on_task(p, MAY_WRITE, __func__);
2224 2225 2226 2227 2228 2229 2230
}

/**
 * smack_task_kill - Smack check on signal delivery
 * @p: the task object
 * @info: unused
 * @sig: unused
2231
 * @cred: identifies the cred to use in lieu of current's
2232 2233 2234 2235 2236
 *
 * Return 0 if write access is permitted
 *
 */
static int smack_task_kill(struct task_struct *p, struct siginfo *info,
2237
			   int sig, const struct cred *cred)
2238
{
E
Etienne Basset 已提交
2239
	struct smk_audit_info ad;
2240
	struct smack_known *skp;
2241
	struct smack_known *tkp = smk_of_task_struct(p);
C
Casey Schaufler 已提交
2242
	int rc;
E
Etienne Basset 已提交
2243

2244 2245 2246
	if (!sig)
		return 0; /* null signal; existence test */

E
Etienne Basset 已提交
2247 2248
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_TASK);
	smk_ad_setfield_u_tsk(&ad, p);
2249 2250 2251 2252
	/*
	 * Sending a signal requires that the sender
	 * can write the receiver.
	 */
2253
	if (cred == NULL) {
2254 2255
		rc = smk_curacc(tkp, MAY_DELIVER, &ad);
		rc = smk_bu_task(p, MAY_DELIVER, rc);
C
Casey Schaufler 已提交
2256 2257
		return rc;
	}
2258
	/*
2259
	 * If the cred isn't NULL we're dealing with some USB IO
2260 2261 2262
	 * specific behavior. This is not clean. For one thing
	 * we can't take privilege into account.
	 */
2263
	skp = smk_of_task(cred->security);
2264 2265
	rc = smk_access(skp, tkp, MAY_DELIVER, &ad);
	rc = smk_bu_note("USB signal", skp, tkp, MAY_DELIVER, rc);
C
Casey Schaufler 已提交
2266
	return rc;
2267 2268 2269 2270 2271
}

/**
 * smack_task_to_inode - copy task smack into the inode blob
 * @p: task to copy from
2272
 * @inode: inode to copy to
2273 2274 2275 2276 2277 2278
 *
 * Sets the smack pointer in the inode security blob
 */
static void smack_task_to_inode(struct task_struct *p, struct inode *inode)
{
	struct inode_smack *isp = inode->i_security;
2279
	struct smack_known *skp = smk_of_task_struct(p);
2280

2281
	isp->smk_inode = skp;
2282 2283 2284 2285 2286 2287 2288 2289 2290 2291
}

/*
 * Socket hooks.
 */

/**
 * smack_sk_alloc_security - Allocate a socket blob
 * @sk: the socket
 * @family: unused
2292
 * @gfp_flags: memory allocation flags
2293 2294 2295 2296 2297 2298 2299
 *
 * Assign Smack pointers to current
 *
 * Returns 0 on success, -ENOMEM is there's no memory
 */
static int smack_sk_alloc_security(struct sock *sk, int family, gfp_t gfp_flags)
{
2300
	struct smack_known *skp = smk_of_current();
2301 2302 2303 2304 2305 2306
	struct socket_smack *ssp;

	ssp = kzalloc(sizeof(struct socket_smack), gfp_flags);
	if (ssp == NULL)
		return -ENOMEM;

2307 2308 2309 2310 2311 2312 2313 2314 2315 2316
	/*
	 * Sockets created by kernel threads receive web label.
	 */
	if (unlikely(current->flags & PF_KTHREAD)) {
		ssp->smk_in = &smack_known_web;
		ssp->smk_out = &smack_known_web;
	} else {
		ssp->smk_in = skp;
		ssp->smk_out = skp;
	}
2317
	ssp->smk_packet = NULL;
2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331

	sk->sk_security = ssp;

	return 0;
}

/**
 * smack_sk_free_security - Free a socket blob
 * @sk: the socket
 *
 * Clears the blob pointer
 */
static void smack_sk_free_security(struct sock *sk)
{
2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345
#ifdef SMACK_IPV6_PORT_LABELING
	struct smk_port_label *spp;

	if (sk->sk_family == PF_INET6) {
		rcu_read_lock();
		list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
			if (spp->smk_sock != sk)
				continue;
			spp->smk_can_reuse = 1;
			break;
		}
		rcu_read_unlock();
	}
#endif
2346 2347 2348
	kfree(sk->sk_security);
}

2349
/**
C
Casey Schaufler 已提交
2350
* smack_ipv4host_label - check host based restrictions
2351 2352 2353 2354 2355 2356 2357 2358 2359 2360
* @sip: the object end
*
* looks for host based access restrictions
*
* This version will only be appropriate for really small sets of single label
* hosts.  The caller is responsible for ensuring that the RCU read lock is
* taken before calling this function.
*
* Returns the label of the far end or NULL if it's not special.
*/
C
Casey Schaufler 已提交
2361
static struct smack_known *smack_ipv4host_label(struct sockaddr_in *sip)
2362
{
C
Casey Schaufler 已提交
2363
	struct smk_net4addr *snp;
2364 2365 2366 2367 2368
	struct in_addr *siap = &sip->sin_addr;

	if (siap->s_addr == 0)
		return NULL;

C
Casey Schaufler 已提交
2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425
	list_for_each_entry_rcu(snp, &smk_net4addr_list, list)
		/*
		 * we break after finding the first match because
		 * the list is sorted from longest to shortest mask
		 * so we have found the most specific match
		 */
		if (snp->smk_host.s_addr ==
		    (siap->s_addr & snp->smk_mask.s_addr))
			return snp->smk_label;

	return NULL;
}

#if IS_ENABLED(CONFIG_IPV6)
/*
 * smk_ipv6_localhost - Check for local ipv6 host address
 * @sip: the address
 *
 * Returns boolean true if this is the localhost address
 */
static bool smk_ipv6_localhost(struct sockaddr_in6 *sip)
{
	__be16 *be16p = (__be16 *)&sip->sin6_addr;
	__be32 *be32p = (__be32 *)&sip->sin6_addr;

	if (be32p[0] == 0 && be32p[1] == 0 && be32p[2] == 0 && be16p[6] == 0 &&
	    ntohs(be16p[7]) == 1)
		return true;
	return false;
}

/**
* smack_ipv6host_label - check host based restrictions
* @sip: the object end
*
* looks for host based access restrictions
*
* This version will only be appropriate for really small sets of single label
* hosts.  The caller is responsible for ensuring that the RCU read lock is
* taken before calling this function.
*
* Returns the label of the far end or NULL if it's not special.
*/
static struct smack_known *smack_ipv6host_label(struct sockaddr_in6 *sip)
{
	struct smk_net6addr *snp;
	struct in6_addr *sap = &sip->sin6_addr;
	int i;
	int found = 0;

	/*
	 * It's local. Don't look for a host label.
	 */
	if (smk_ipv6_localhost(sip))
		return NULL;

	list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
C
Casey Schaufler 已提交
2426 2427 2428 2429 2430 2431
		/*
		 * If the label is NULL the entry has
		 * been renounced. Ignore it.
		 */
		if (snp->smk_label == NULL)
			continue;
2432 2433 2434 2435 2436
		/*
		* we break after finding the first match because
		* the list is sorted from longest to shortest mask
		* so we have found the most specific match
		*/
C
Casey Schaufler 已提交
2437 2438 2439 2440 2441 2442
		for (found = 1, i = 0; i < 8; i++) {
			if ((sap->s6_addr16[i] & snp->smk_mask.s6_addr16[i]) !=
			    snp->smk_host.s6_addr16[i]) {
				found = 0;
				break;
			}
2443
		}
C
Casey Schaufler 已提交
2444 2445 2446
		if (found)
			return snp->smk_label;
	}
2447 2448 2449

	return NULL;
}
C
Casey Schaufler 已提交
2450
#endif /* CONFIG_IPV6 */
2451

2452 2453 2454
/**
 * smack_netlabel - Set the secattr on a socket
 * @sk: the socket
2455
 * @labeled: socket label scheme
2456 2457 2458 2459 2460 2461
 *
 * Convert the outbound smack value (smk_out) to a
 * secattr and attach it to the socket.
 *
 * Returns 0 on success or an error code
 */
2462
static int smack_netlabel(struct sock *sk, int labeled)
2463
{
2464
	struct smack_known *skp;
2465
	struct socket_smack *ssp = sk->sk_security;
2466
	int rc = 0;
2467

2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482
	/*
	 * Usually the netlabel code will handle changing the
	 * packet labeling based on the label.
	 * The case of a single label host is different, because
	 * a single label host should never get a labeled packet
	 * even though the label is usually associated with a packet
	 * label.
	 */
	local_bh_disable();
	bh_lock_sock_nested(sk);

	if (ssp->smk_out == smack_net_ambient ||
	    labeled == SMACK_UNLABELED_SOCKET)
		netlbl_sock_delattr(sk);
	else {
2483
		skp = ssp->smk_out;
2484
		rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2485 2486 2487 2488
	}

	bh_unlock_sock(sk);
	local_bh_enable();
2489

2490 2491 2492
	return rc;
}

2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505
/**
 * smack_netlbel_send - Set the secattr on a socket and perform access checks
 * @sk: the socket
 * @sap: the destination address
 *
 * Set the correct secattr for the given socket based on the destination
 * address and perform any outbound access checks needed.
 *
 * Returns 0 on success or an error code.
 *
 */
static int smack_netlabel_send(struct sock *sk, struct sockaddr_in *sap)
{
2506
	struct smack_known *skp;
2507 2508
	int rc;
	int sk_lbl;
2509
	struct smack_known *hkp;
2510
	struct socket_smack *ssp = sk->sk_security;
E
Etienne Basset 已提交
2511
	struct smk_audit_info ad;
2512 2513

	rcu_read_lock();
C
Casey Schaufler 已提交
2514
	hkp = smack_ipv4host_label(sap);
2515
	if (hkp != NULL) {
E
Etienne Basset 已提交
2516
#ifdef CONFIG_AUDIT
2517 2518
		struct lsm_network_audit net;

2519 2520 2521 2522
		smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
		ad.a.u.net->family = sap->sin_family;
		ad.a.u.net->dport = sap->sin_port;
		ad.a.u.net->v4info.daddr = sap->sin_addr.s_addr;
E
Etienne Basset 已提交
2523
#endif
2524
		sk_lbl = SMACK_UNLABELED_SOCKET;
2525
		skp = ssp->smk_out;
2526 2527
		rc = smk_access(skp, hkp, MAY_WRITE, &ad);
		rc = smk_bu_note("IPv4 host check", skp, hkp, MAY_WRITE, rc);
2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538
	} else {
		sk_lbl = SMACK_CIPSO_SOCKET;
		rc = 0;
	}
	rcu_read_unlock();
	if (rc != 0)
		return rc;

	return smack_netlabel(sk, sk_lbl);
}

C
Casey Schaufler 已提交
2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574
#if IS_ENABLED(CONFIG_IPV6)
/**
 * smk_ipv6_check - check Smack access
 * @subject: subject Smack label
 * @object: object Smack label
 * @address: address
 * @act: the action being taken
 *
 * Check an IPv6 access
 */
static int smk_ipv6_check(struct smack_known *subject,
				struct smack_known *object,
				struct sockaddr_in6 *address, int act)
{
#ifdef CONFIG_AUDIT
	struct lsm_network_audit net;
#endif
	struct smk_audit_info ad;
	int rc;

#ifdef CONFIG_AUDIT
	smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
	ad.a.u.net->family = PF_INET6;
	ad.a.u.net->dport = ntohs(address->sin6_port);
	if (act == SMK_RECEIVING)
		ad.a.u.net->v6info.saddr = address->sin6_addr;
	else
		ad.a.u.net->v6info.daddr = address->sin6_addr;
#endif
	rc = smk_access(subject, object, MAY_WRITE, &ad);
	rc = smk_bu_note("IPv6 check", subject, object, MAY_WRITE, rc);
	return rc;
}
#endif /* CONFIG_IPV6 */

#ifdef SMACK_IPV6_PORT_LABELING
2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595
/**
 * smk_ipv6_port_label - Smack port access table management
 * @sock: socket
 * @address: address
 *
 * Create or update the port list entry
 */
static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
{
	struct sock *sk = sock->sk;
	struct sockaddr_in6 *addr6;
	struct socket_smack *ssp = sock->sk->sk_security;
	struct smk_port_label *spp;
	unsigned short port = 0;

	if (address == NULL) {
		/*
		 * This operation is changing the Smack information
		 * on the bound socket. Take the changes to the port
		 * as well.
		 */
2596 2597
		rcu_read_lock();
		list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2598 2599 2600 2601
			if (sk != spp->smk_sock)
				continue;
			spp->smk_in = ssp->smk_in;
			spp->smk_out = ssp->smk_out;
2602
			rcu_read_unlock();
2603 2604 2605 2606 2607 2608
			return;
		}
		/*
		 * A NULL address is only used for updating existing
		 * bound entries. If there isn't one, it's OK.
		 */
2609
		rcu_read_unlock();
2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624
		return;
	}

	addr6 = (struct sockaddr_in6 *)address;
	port = ntohs(addr6->sin6_port);
	/*
	 * This is a special case that is safely ignored.
	 */
	if (port == 0)
		return;

	/*
	 * Look for an existing port list entry.
	 * This is an indication that a port is getting reused.
	 */
2625 2626
	rcu_read_lock();
	list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2627
		if (spp->smk_port != port || spp->smk_sock_type != sock->type)
2628
			continue;
2629 2630 2631 2632
		if (spp->smk_can_reuse != 1) {
			rcu_read_unlock();
			return;
		}
2633 2634 2635 2636
		spp->smk_port = port;
		spp->smk_sock = sk;
		spp->smk_in = ssp->smk_in;
		spp->smk_out = ssp->smk_out;
2637
		spp->smk_can_reuse = 0;
2638
		rcu_read_unlock();
2639 2640
		return;
	}
2641
	rcu_read_unlock();
2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652
	/*
	 * A new port entry is required.
	 */
	spp = kzalloc(sizeof(*spp), GFP_KERNEL);
	if (spp == NULL)
		return;

	spp->smk_port = port;
	spp->smk_sock = sk;
	spp->smk_in = ssp->smk_in;
	spp->smk_out = ssp->smk_out;
2653
	spp->smk_sock_type = sock->type;
2654
	spp->smk_can_reuse = 0;
2655

2656 2657 2658
	mutex_lock(&smack_ipv6_lock);
	list_add_rcu(&spp->list, &smk_ipv6_port_list);
	mutex_unlock(&smack_ipv6_lock);
2659 2660 2661 2662 2663 2664 2665 2666 2667 2668
	return;
}

/**
 * smk_ipv6_port_check - check Smack port access
 * @sock: socket
 * @address: address
 *
 * Create or update the port list entry
 */
2669
static int smk_ipv6_port_check(struct sock *sk, struct sockaddr_in6 *address,
2670 2671 2672 2673
				int act)
{
	struct smk_port_label *spp;
	struct socket_smack *ssp = sk->sk_security;
C
Casey Schaufler 已提交
2674 2675
	struct smack_known *skp = NULL;
	unsigned short port;
2676
	struct smack_known *object;
2677 2678

	if (act == SMK_RECEIVING) {
C
Casey Schaufler 已提交
2679
		skp = smack_ipv6host_label(address);
2680
		object = ssp->smk_in;
2681
	} else {
2682
		skp = ssp->smk_out;
C
Casey Schaufler 已提交
2683
		object = smack_ipv6host_label(address);
2684 2685 2686
	}

	/*
C
Casey Schaufler 已提交
2687
	 * The other end is a single label host.
2688
	 */
C
Casey Schaufler 已提交
2689 2690 2691 2692 2693 2694
	if (skp != NULL && object != NULL)
		return smk_ipv6_check(skp, object, address, act);
	if (skp == NULL)
		skp = smack_net_ambient;
	if (object == NULL)
		object = smack_net_ambient;
2695 2696 2697 2698

	/*
	 * It's remote, so port lookup does no good.
	 */
C
Casey Schaufler 已提交
2699 2700
	if (!smk_ipv6_localhost(address))
		return smk_ipv6_check(skp, object, address, act);
2701 2702 2703 2704

	/*
	 * It's local so the send check has to have passed.
	 */
C
Casey Schaufler 已提交
2705 2706
	if (act == SMK_RECEIVING)
		return 0;
2707

C
Casey Schaufler 已提交
2708
	port = ntohs(address->sin6_port);
2709 2710
	rcu_read_lock();
	list_for_each_entry_rcu(spp, &smk_ipv6_port_list, list) {
2711
		if (spp->smk_port != port || spp->smk_sock_type != sk->sk_type)
2712
			continue;
2713
		object = spp->smk_in;
2714
		if (act == SMK_CONNECTING)
2715
			ssp->smk_packet = spp->smk_out;
2716 2717
		break;
	}
2718
	rcu_read_unlock();
2719

C
Casey Schaufler 已提交
2720
	return smk_ipv6_check(skp, object, address, act);
2721
}
C
Casey Schaufler 已提交
2722
#endif /* SMACK_IPV6_PORT_LABELING */
2723

2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738
/**
 * smack_inode_setsecurity - set smack xattrs
 * @inode: the object
 * @name: attribute name
 * @value: attribute value
 * @size: size of the attribute
 * @flags: unused
 *
 * Sets the named attribute in the appropriate blob
 *
 * Returns 0 on success, or an error code
 */
static int smack_inode_setsecurity(struct inode *inode, const char *name,
				   const void *value, size_t size, int flags)
{
2739
	struct smack_known *skp;
2740 2741 2742
	struct inode_smack *nsp = inode->i_security;
	struct socket_smack *ssp;
	struct socket *sock;
2743
	int rc = 0;
2744

2745
	if (value == NULL || size > SMK_LONGLABEL || size == 0)
P
Pankaj Kumar 已提交
2746
		return -EINVAL;
2747

2748
	skp = smk_import_entry(value, size);
2749 2750
	if (IS_ERR(skp))
		return PTR_ERR(skp);
2751 2752

	if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
2753
		nsp->smk_inode = skp;
2754
		nsp->smk_flags |= SMK_INODE_INSTANT;
2755 2756 2757 2758 2759 2760 2761 2762 2763
		return 0;
	}
	/*
	 * The rest of the Smack xattrs are only on sockets.
	 */
	if (inode->i_sb->s_magic != SOCKFS_MAGIC)
		return -EOPNOTSUPP;

	sock = SOCKET_I(inode);
2764
	if (sock == NULL || sock->sk == NULL)
2765 2766 2767 2768 2769
		return -EOPNOTSUPP;

	ssp = sock->sk->sk_security;

	if (strcmp(name, XATTR_SMACK_IPIN) == 0)
2770
		ssp->smk_in = skp;
2771
	else if (strcmp(name, XATTR_SMACK_IPOUT) == 0) {
2772
		ssp->smk_out = skp;
2773
		if (sock->sk->sk_family == PF_INET) {
C
Casey Schaufler 已提交
2774 2775 2776 2777 2778 2779
			rc = smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
			if (rc != 0)
				printk(KERN_WARNING
					"Smack: \"%s\" netlbl error %d.\n",
					__func__, -rc);
		}
2780 2781 2782
	} else
		return -EOPNOTSUPP;

C
Casey Schaufler 已提交
2783
#ifdef SMACK_IPV6_PORT_LABELING
2784 2785
	if (sock->sk->sk_family == PF_INET6)
		smk_ipv6_port_label(sock, NULL);
C
Casey Schaufler 已提交
2786
#endif
2787

2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805
	return 0;
}

/**
 * smack_socket_post_create - finish socket setup
 * @sock: the socket
 * @family: protocol family
 * @type: unused
 * @protocol: unused
 * @kern: unused
 *
 * Sets the netlabel information on the socket
 *
 * Returns 0 on success, and error code otherwise
 */
static int smack_socket_post_create(struct socket *sock, int family,
				    int type, int protocol, int kern)
{
2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820
	struct socket_smack *ssp;

	if (sock->sk == NULL)
		return 0;

	/*
	 * Sockets created by kernel threads receive web label.
	 */
	if (unlikely(current->flags & PF_KTHREAD)) {
		ssp = sock->sk->sk_security;
		ssp->smk_in = &smack_known_web;
		ssp->smk_out = &smack_known_web;
	}

	if (family != PF_INET)
2821 2822 2823 2824
		return 0;
	/*
	 * Set the outbound netlbl.
	 */
2825 2826 2827
	return smack_netlabel(sock->sk, SMACK_CIPSO_SOCKET);
}

C
Casey Schaufler 已提交
2828
#ifdef SMACK_IPV6_PORT_LABELING
2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845
/**
 * smack_socket_bind - record port binding information.
 * @sock: the socket
 * @address: the port address
 * @addrlen: size of the address
 *
 * Records the label bound to a port.
 *
 * Returns 0
 */
static int smack_socket_bind(struct socket *sock, struct sockaddr *address,
				int addrlen)
{
	if (sock->sk != NULL && sock->sk->sk_family == PF_INET6)
		smk_ipv6_port_label(sock, address);
	return 0;
}
C
Casey Schaufler 已提交
2846
#endif /* SMACK_IPV6_PORT_LABELING */
2847

2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860
/**
 * smack_socket_connect - connect access check
 * @sock: the socket
 * @sap: the other end
 * @addrlen: size of sap
 *
 * Verifies that a connection may be possible
 *
 * Returns 0 on success, and error code otherwise
 */
static int smack_socket_connect(struct socket *sock, struct sockaddr *sap,
				int addrlen)
{
2861
	int rc = 0;
C
Casey Schaufler 已提交
2862 2863 2864 2865 2866
#if IS_ENABLED(CONFIG_IPV6)
	struct sockaddr_in6 *sip = (struct sockaddr_in6 *)sap;
#endif
#ifdef SMACK_IPV6_SECMARK_LABELING
	struct smack_known *rsp;
2867
	struct socket_smack *ssp;
C
Casey Schaufler 已提交
2868
#endif
2869 2870

	if (sock->sk == NULL)
2871 2872
		return 0;

2873 2874 2875 2876
#ifdef SMACK_IPV6_SECMARK_LABELING
	ssp = sock->sk->sk_security;
#endif

2877 2878 2879 2880 2881 2882 2883 2884 2885
	switch (sock->sk->sk_family) {
	case PF_INET:
		if (addrlen < sizeof(struct sockaddr_in))
			return -EINVAL;
		rc = smack_netlabel_send(sock->sk, (struct sockaddr_in *)sap);
		break;
	case PF_INET6:
		if (addrlen < sizeof(struct sockaddr_in6))
			return -EINVAL;
C
Casey Schaufler 已提交
2886 2887 2888 2889
#ifdef SMACK_IPV6_SECMARK_LABELING
		rsp = smack_ipv6host_label(sip);
		if (rsp != NULL)
			rc = smk_ipv6_check(ssp->smk_out, rsp, sip,
2890
						SMK_CONNECTING);
C
Casey Schaufler 已提交
2891 2892 2893 2894
#endif
#ifdef SMACK_IPV6_PORT_LABELING
		rc = smk_ipv6_port_check(sock->sk, sip, SMK_CONNECTING);
#endif
2895 2896 2897
		break;
	}
	return rc;
2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927
}

/**
 * smack_flags_to_may - convert S_ to MAY_ values
 * @flags: the S_ value
 *
 * Returns the equivalent MAY_ value
 */
static int smack_flags_to_may(int flags)
{
	int may = 0;

	if (flags & S_IRUGO)
		may |= MAY_READ;
	if (flags & S_IWUGO)
		may |= MAY_WRITE;
	if (flags & S_IXUGO)
		may |= MAY_EXEC;

	return may;
}

/**
 * smack_msg_msg_alloc_security - Set the security blob for msg_msg
 * @msg: the object
 *
 * Returns 0
 */
static int smack_msg_msg_alloc_security(struct msg_msg *msg)
{
2928 2929
	struct smack_known *skp = smk_of_current();

2930
	msg->security = skp;
2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950
	return 0;
}

/**
 * smack_msg_msg_free_security - Clear the security blob for msg_msg
 * @msg: the object
 *
 * Clears the blob pointer
 */
static void smack_msg_msg_free_security(struct msg_msg *msg)
{
	msg->security = NULL;
}

/**
 * smack_of_shm - the smack pointer for the shm
 * @shp: the object
 *
 * Returns a pointer to the smack value
 */
2951
static struct smack_known *smack_of_shm(struct shmid_kernel *shp)
2952
{
2953
	return (struct smack_known *)shp->shm_perm.security;
2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964
}

/**
 * smack_shm_alloc_security - Set the security blob for shm
 * @shp: the object
 *
 * Returns 0
 */
static int smack_shm_alloc_security(struct shmid_kernel *shp)
{
	struct kern_ipc_perm *isp = &shp->shm_perm;
2965
	struct smack_known *skp = smk_of_current();
2966

2967
	isp->security = skp;
2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983
	return 0;
}

/**
 * smack_shm_free_security - Clear the security blob for shm
 * @shp: the object
 *
 * Clears the blob pointer
 */
static void smack_shm_free_security(struct shmid_kernel *shp)
{
	struct kern_ipc_perm *isp = &shp->shm_perm;

	isp->security = NULL;
}

E
Etienne Basset 已提交
2984 2985 2986 2987 2988 2989 2990 2991 2992
/**
 * smk_curacc_shm : check if current has access on shm
 * @shp : the object
 * @access : access requested
 *
 * Returns 0 if current has the requested access, error code otherwise
 */
static int smk_curacc_shm(struct shmid_kernel *shp, int access)
{
2993
	struct smack_known *ssp = smack_of_shm(shp);
E
Etienne Basset 已提交
2994
	struct smk_audit_info ad;
C
Casey Schaufler 已提交
2995
	int rc;
E
Etienne Basset 已提交
2996 2997 2998 2999 3000

#ifdef CONFIG_AUDIT
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
	ad.a.u.ipc_id = shp->shm_perm.id;
#endif
C
Casey Schaufler 已提交
3001 3002 3003
	rc = smk_curacc(ssp, access, &ad);
	rc = smk_bu_current("shm", ssp, access, rc);
	return rc;
E
Etienne Basset 已提交
3004 3005
}

3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017
/**
 * smack_shm_associate - Smack access check for shm
 * @shp: the object
 * @shmflg: access requested
 *
 * Returns 0 if current has the requested access, error code otherwise
 */
static int smack_shm_associate(struct shmid_kernel *shp, int shmflg)
{
	int may;

	may = smack_flags_to_may(shmflg);
E
Etienne Basset 已提交
3018
	return smk_curacc_shm(shp, may);
3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051
}

/**
 * smack_shm_shmctl - Smack access check for shm
 * @shp: the object
 * @cmd: what it wants to do
 *
 * Returns 0 if current has the requested access, error code otherwise
 */
static int smack_shm_shmctl(struct shmid_kernel *shp, int cmd)
{
	int may;

	switch (cmd) {
	case IPC_STAT:
	case SHM_STAT:
		may = MAY_READ;
		break;
	case IPC_SET:
	case SHM_LOCK:
	case SHM_UNLOCK:
	case IPC_RMID:
		may = MAY_READWRITE;
		break;
	case IPC_INFO:
	case SHM_INFO:
		/*
		 * System level information.
		 */
		return 0;
	default:
		return -EINVAL;
	}
E
Etienne Basset 已提交
3052
	return smk_curacc_shm(shp, may);
3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068
}

/**
 * smack_shm_shmat - Smack access for shmat
 * @shp: the object
 * @shmaddr: unused
 * @shmflg: access requested
 *
 * Returns 0 if current has the requested access, error code otherwise
 */
static int smack_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr,
			   int shmflg)
{
	int may;

	may = smack_flags_to_may(shmflg);
E
Etienne Basset 已提交
3069
	return smk_curacc_shm(shp, may);
3070 3071 3072 3073 3074 3075 3076 3077
}

/**
 * smack_of_sem - the smack pointer for the sem
 * @sma: the object
 *
 * Returns a pointer to the smack value
 */
3078
static struct smack_known *smack_of_sem(struct sem_array *sma)
3079
{
3080
	return (struct smack_known *)sma->sem_perm.security;
3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091
}

/**
 * smack_sem_alloc_security - Set the security blob for sem
 * @sma: the object
 *
 * Returns 0
 */
static int smack_sem_alloc_security(struct sem_array *sma)
{
	struct kern_ipc_perm *isp = &sma->sem_perm;
3092
	struct smack_known *skp = smk_of_current();
3093

3094
	isp->security = skp;
3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110
	return 0;
}

/**
 * smack_sem_free_security - Clear the security blob for sem
 * @sma: the object
 *
 * Clears the blob pointer
 */
static void smack_sem_free_security(struct sem_array *sma)
{
	struct kern_ipc_perm *isp = &sma->sem_perm;

	isp->security = NULL;
}

E
Etienne Basset 已提交
3111 3112 3113 3114 3115 3116 3117 3118 3119
/**
 * smk_curacc_sem : check if current has access on sem
 * @sma : the object
 * @access : access requested
 *
 * Returns 0 if current has the requested access, error code otherwise
 */
static int smk_curacc_sem(struct sem_array *sma, int access)
{
3120
	struct smack_known *ssp = smack_of_sem(sma);
E
Etienne Basset 已提交
3121
	struct smk_audit_info ad;
C
Casey Schaufler 已提交
3122
	int rc;
E
Etienne Basset 已提交
3123 3124 3125 3126 3127

#ifdef CONFIG_AUDIT
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
	ad.a.u.ipc_id = sma->sem_perm.id;
#endif
C
Casey Schaufler 已提交
3128 3129 3130
	rc = smk_curacc(ssp, access, &ad);
	rc = smk_bu_current("sem", ssp, access, rc);
	return rc;
E
Etienne Basset 已提交
3131 3132
}

3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144
/**
 * smack_sem_associate - Smack access check for sem
 * @sma: the object
 * @semflg: access requested
 *
 * Returns 0 if current has the requested access, error code otherwise
 */
static int smack_sem_associate(struct sem_array *sma, int semflg)
{
	int may;

	may = smack_flags_to_may(semflg);
E
Etienne Basset 已提交
3145
	return smk_curacc_sem(sma, may);
3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184
}

/**
 * smack_sem_shmctl - Smack access check for sem
 * @sma: the object
 * @cmd: what it wants to do
 *
 * Returns 0 if current has the requested access, error code otherwise
 */
static int smack_sem_semctl(struct sem_array *sma, int cmd)
{
	int may;

	switch (cmd) {
	case GETPID:
	case GETNCNT:
	case GETZCNT:
	case GETVAL:
	case GETALL:
	case IPC_STAT:
	case SEM_STAT:
		may = MAY_READ;
		break;
	case SETVAL:
	case SETALL:
	case IPC_RMID:
	case IPC_SET:
		may = MAY_READWRITE;
		break;
	case IPC_INFO:
	case SEM_INFO:
		/*
		 * System level information
		 */
		return 0;
	default:
		return -EINVAL;
	}

E
Etienne Basset 已提交
3185
	return smk_curacc_sem(sma, may);
3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201
}

/**
 * smack_sem_semop - Smack checks of semaphore operations
 * @sma: the object
 * @sops: unused
 * @nsops: unused
 * @alter: unused
 *
 * Treated as read and write in all cases.
 *
 * Returns 0 if access is allowed, error code otherwise
 */
static int smack_sem_semop(struct sem_array *sma, struct sembuf *sops,
			   unsigned nsops, int alter)
{
E
Etienne Basset 已提交
3202
	return smk_curacc_sem(sma, MAY_READWRITE);
3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213
}

/**
 * smack_msg_alloc_security - Set the security blob for msg
 * @msq: the object
 *
 * Returns 0
 */
static int smack_msg_queue_alloc_security(struct msg_queue *msq)
{
	struct kern_ipc_perm *kisp = &msq->q_perm;
3214
	struct smack_known *skp = smk_of_current();
3215

3216
	kisp->security = skp;
3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236
	return 0;
}

/**
 * smack_msg_free_security - Clear the security blob for msg
 * @msq: the object
 *
 * Clears the blob pointer
 */
static void smack_msg_queue_free_security(struct msg_queue *msq)
{
	struct kern_ipc_perm *kisp = &msq->q_perm;

	kisp->security = NULL;
}

/**
 * smack_of_msq - the smack pointer for the msq
 * @msq: the object
 *
3237
 * Returns a pointer to the smack label entry
3238
 */
3239
static struct smack_known *smack_of_msq(struct msg_queue *msq)
3240
{
3241
	return (struct smack_known *)msq->q_perm.security;
3242 3243
}

E
Etienne Basset 已提交
3244 3245 3246 3247 3248 3249 3250 3251 3252
/**
 * smk_curacc_msq : helper to check if current has access on msq
 * @msq : the msq
 * @access : access requested
 *
 * return 0 if current has access, error otherwise
 */
static int smk_curacc_msq(struct msg_queue *msq, int access)
{
3253
	struct smack_known *msp = smack_of_msq(msq);
E
Etienne Basset 已提交
3254
	struct smk_audit_info ad;
C
Casey Schaufler 已提交
3255
	int rc;
E
Etienne Basset 已提交
3256 3257 3258 3259 3260

#ifdef CONFIG_AUDIT
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
	ad.a.u.ipc_id = msq->q_perm.id;
#endif
C
Casey Schaufler 已提交
3261 3262 3263
	rc = smk_curacc(msp, access, &ad);
	rc = smk_bu_current("msq", msp, access, rc);
	return rc;
E
Etienne Basset 已提交
3264 3265
}

3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277
/**
 * smack_msg_queue_associate - Smack access check for msg_queue
 * @msq: the object
 * @msqflg: access requested
 *
 * Returns 0 if current has the requested access, error code otherwise
 */
static int smack_msg_queue_associate(struct msg_queue *msq, int msqflg)
{
	int may;

	may = smack_flags_to_may(msqflg);
E
Etienne Basset 已提交
3278
	return smk_curacc_msq(msq, may);
3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310
}

/**
 * smack_msg_queue_msgctl - Smack access check for msg_queue
 * @msq: the object
 * @cmd: what it wants to do
 *
 * Returns 0 if current has the requested access, error code otherwise
 */
static int smack_msg_queue_msgctl(struct msg_queue *msq, int cmd)
{
	int may;

	switch (cmd) {
	case IPC_STAT:
	case MSG_STAT:
		may = MAY_READ;
		break;
	case IPC_SET:
	case IPC_RMID:
		may = MAY_READWRITE;
		break;
	case IPC_INFO:
	case MSG_INFO:
		/*
		 * System level information
		 */
		return 0;
	default:
		return -EINVAL;
	}

E
Etienne Basset 已提交
3311
	return smk_curacc_msq(msq, may);
3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324
}

/**
 * smack_msg_queue_msgsnd - Smack access check for msg_queue
 * @msq: the object
 * @msg: unused
 * @msqflg: access requested
 *
 * Returns 0 if current has the requested access, error code otherwise
 */
static int smack_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg,
				  int msqflg)
{
E
Etienne Basset 已提交
3325
	int may;
3326

E
Etienne Basset 已提交
3327 3328
	may = smack_flags_to_may(msqflg);
	return smk_curacc_msq(msq, may);
3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343
}

/**
 * smack_msg_queue_msgsnd - Smack access check for msg_queue
 * @msq: the object
 * @msg: unused
 * @target: unused
 * @type: unused
 * @mode: unused
 *
 * Returns 0 if current has read and write access, error code otherwise
 */
static int smack_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
			struct task_struct *target, long type, int mode)
{
E
Etienne Basset 已提交
3344
	return smk_curacc_msq(msq, MAY_READWRITE);
3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355
}

/**
 * smack_ipc_permission - Smack access for ipc_permission()
 * @ipp: the object permissions
 * @flag: access requested
 *
 * Returns 0 if current has read and write access, error code otherwise
 */
static int smack_ipc_permission(struct kern_ipc_perm *ipp, short flag)
{
3356
	struct smack_known *iskp = ipp->security;
E
Etienne Basset 已提交
3357 3358
	int may = smack_flags_to_may(flag);
	struct smk_audit_info ad;
C
Casey Schaufler 已提交
3359
	int rc;
3360

E
Etienne Basset 已提交
3361 3362 3363 3364
#ifdef CONFIG_AUDIT
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_IPC);
	ad.a.u.ipc_id = ipp->id;
#endif
3365 3366
	rc = smk_curacc(iskp, may, &ad);
	rc = smk_bu_current("svipc", iskp, may, rc);
C
Casey Schaufler 已提交
3367
	return rc;
3368 3369
}

3370 3371
/**
 * smack_ipc_getsecid - Extract smack security id
3372
 * @ipp: the object permissions
3373 3374 3375 3376
 * @secid: where result will be saved
 */
static void smack_ipc_getsecid(struct kern_ipc_perm *ipp, u32 *secid)
{
3377
	struct smack_known *iskp = ipp->security;
3378

3379
	*secid = iskp->smk_secid;
3380 3381
}

3382 3383
/**
 * smack_d_instantiate - Make sure the blob is correct on an inode
3384
 * @opt_dentry: dentry where inode will be attached
3385 3386 3387 3388 3389 3390 3391 3392 3393
 * @inode: the object
 *
 * Set the inode's security blob if it hasn't been done already.
 */
static void smack_d_instantiate(struct dentry *opt_dentry, struct inode *inode)
{
	struct super_block *sbp;
	struct superblock_smack *sbsp;
	struct inode_smack *isp;
3394 3395
	struct smack_known *skp;
	struct smack_known *ckp = smk_of_current();
3396
	struct smack_known *final;
3397 3398
	char trattr[TRANS_TRUE_SIZE];
	int transflag = 0;
C
Casey Schaufler 已提交
3399
	int rc;
3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422
	struct dentry *dp;

	if (inode == NULL)
		return;

	isp = inode->i_security;

	mutex_lock(&isp->smk_lock);
	/*
	 * If the inode is already instantiated
	 * take the quick way out
	 */
	if (isp->smk_flags & SMK_INODE_INSTANT)
		goto unlockandout;

	sbp = inode->i_sb;
	sbsp = sbp->s_security;
	/*
	 * We're going to use the superblock default label
	 * if there's no label on the file.
	 */
	final = sbsp->smk_default;

C
Casey Schaufler 已提交
3423 3424 3425 3426 3427 3428 3429
	/*
	 * If this is the root inode the superblock
	 * may be in the process of initialization.
	 * If that is the case use the root value out
	 * of the superblock.
	 */
	if (opt_dentry->d_parent == opt_dentry) {
3430 3431
		switch (sbp->s_magic) {
		case CGROUP_SUPER_MAGIC:
3432 3433 3434 3435 3436
			/*
			 * The cgroup filesystem is never mounted,
			 * so there's no opportunity to set the mount
			 * options.
			 */
3437 3438
			sbsp->smk_root = &smack_known_star;
			sbsp->smk_default = &smack_known_star;
3439 3440 3441 3442 3443 3444 3445 3446 3447
			isp->smk_inode = sbsp->smk_root;
			break;
		case TMPFS_MAGIC:
			/*
			 * What about shmem/tmpfs anonymous files with dentry
			 * obtained from d_alloc_pseudo()?
			 */
			isp->smk_inode = smk_of_current();
			break;
3448 3449 3450
		case PIPEFS_MAGIC:
			isp->smk_inode = smk_of_current();
			break;
3451 3452 3453 3454 3455 3456 3457
		case SOCKFS_MAGIC:
			/*
			 * Socket access is controlled by the socket
			 * structures associated with the task involved.
			 */
			isp->smk_inode = &smack_known_star;
			break;
3458 3459 3460
		default:
			isp->smk_inode = sbsp->smk_root;
			break;
3461
		}
C
Casey Schaufler 已提交
3462 3463 3464 3465
		isp->smk_flags |= SMK_INODE_INSTANT;
		goto unlockandout;
	}

3466 3467 3468 3469 3470 3471 3472 3473
	/*
	 * This is pretty hackish.
	 * Casey says that we shouldn't have to do
	 * file system specific code, but it does help
	 * with keeping it simple.
	 */
	switch (sbp->s_magic) {
	case SMACK_MAGIC:
3474
	case CGROUP_SUPER_MAGIC:
3475
		/*
L
Lucas De Marchi 已提交
3476
		 * Casey says that it's a little embarrassing
3477 3478
		 * that the smack file system doesn't do
		 * extended attributes.
3479 3480
		 *
		 * Cgroupfs is special
3481
		 */
3482
		final = &smack_known_star;
3483 3484 3485 3486 3487 3488 3489
		break;
	case DEVPTS_SUPER_MAGIC:
		/*
		 * devpts seems content with the label of the task.
		 * Programs that change smack have to treat the
		 * pty with respect.
		 */
3490
		final = ckp;
3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503
		break;
	case PROC_SUPER_MAGIC:
		/*
		 * Casey says procfs appears not to care.
		 * The superblock default suffices.
		 */
		break;
	case TMPFS_MAGIC:
		/*
		 * Device labels should come from the filesystem,
		 * but watch out, because they're volitile,
		 * getting recreated on every reboot.
		 */
3504
		final = &smack_known_star;
3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516
		/*
		 * No break.
		 *
		 * If a smack value has been set we want to use it,
		 * but since tmpfs isn't giving us the opportunity
		 * to set mount options simulate setting the
		 * superblock default.
		 */
	default:
		/*
		 * This isn't an understood special case.
		 * Get the value from the xattr.
C
Casey Schaufler 已提交
3517 3518 3519 3520 3521 3522
		 */

		/*
		 * UNIX domain sockets use lower level socket data.
		 */
		if (S_ISSOCK(inode->i_mode)) {
3523
			final = &smack_known_star;
C
Casey Schaufler 已提交
3524 3525 3526
			break;
		}
		/*
3527 3528 3529 3530 3531
		 * No xattr support means, alas, no SMACK label.
		 * Use the aforeapplied default.
		 * It would be curious if the label of the task
		 * does not match that assigned.
		 */
3532 3533
		if (!(inode->i_opflags & IOP_XATTR))
		        break;
3534 3535 3536
		/*
		 * Get the dentry for xattr.
		 */
3537
		dp = dget(opt_dentry);
3538
		skp = smk_fetch(XATTR_NAME_SMACK, inode, dp);
3539
		if (!IS_ERR_OR_NULL(skp))
3540
			final = skp;
C
Casey Schaufler 已提交
3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556

		/*
		 * Transmuting directory
		 */
		if (S_ISDIR(inode->i_mode)) {
			/*
			 * If this is a new directory and the label was
			 * transmuted when the inode was initialized
			 * set the transmute attribute on the directory
			 * and mark the inode.
			 *
			 * If there is a transmute attribute on the
			 * directory mark the inode.
			 */
			if (isp->smk_flags & SMK_INODE_CHANGED) {
				isp->smk_flags &= ~SMK_INODE_CHANGED;
3557
				rc = __vfs_setxattr(dp, inode,
3558
					XATTR_NAME_SMACKTRANSMUTE,
C
Casey Schaufler 已提交
3559 3560 3561
					TRANS_TRUE, TRANS_TRUE_SIZE,
					0);
			} else {
3562
				rc = __vfs_getxattr(dp, inode,
C
Casey Schaufler 已提交
3563 3564 3565 3566 3567
					XATTR_NAME_SMACKTRANSMUTE, trattr,
					TRANS_TRUE_SIZE);
				if (rc >= 0 && strncmp(trattr, TRANS_TRUE,
						       TRANS_TRUE_SIZE) != 0)
					rc = -EINVAL;
3568
			}
C
Casey Schaufler 已提交
3569 3570
			if (rc >= 0)
				transflag = SMK_INODE_TRANSMUTE;
3571
		}
3572 3573 3574 3575 3576 3577 3578 3579
		/*
		 * Don't let the exec or mmap label be "*" or "@".
		 */
		skp = smk_fetch(XATTR_NAME_SMACKEXEC, inode, dp);
		if (IS_ERR(skp) || skp == &smack_known_star ||
		    skp == &smack_known_web)
			skp = NULL;
		isp->smk_task = skp;
3580

3581
		skp = smk_fetch(XATTR_NAME_SMACKMMAP, inode, dp);
3582 3583
		if (IS_ERR(skp) || skp == &smack_known_star ||
		    skp == &smack_known_web)
3584 3585
			skp = NULL;
		isp->smk_mmap = skp;
3586

3587 3588 3589 3590 3591
		dput(dp);
		break;
	}

	if (final == NULL)
3592
		isp->smk_inode = ckp;
3593 3594 3595
	else
		isp->smk_inode = final;

3596
	isp->smk_flags |= (SMK_INODE_INSTANT | transflag);
3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614

unlockandout:
	mutex_unlock(&isp->smk_lock);
	return;
}

/**
 * smack_getprocattr - Smack process attribute access
 * @p: the object task
 * @name: the name of the attribute in /proc/.../attr
 * @value: where to put the result
 *
 * Places a copy of the task Smack into value
 *
 * Returns the length of the smack label or an error code
 */
static int smack_getprocattr(struct task_struct *p, char *name, char **value)
{
3615
	struct smack_known *skp = smk_of_task_struct(p);
3616 3617 3618 3619 3620 3621
	char *cp;
	int slen;

	if (strcmp(name, "current") != 0)
		return -EINVAL;

3622
	cp = kstrdup(skp->smk_known, GFP_KERNEL);
3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641
	if (cp == NULL)
		return -ENOMEM;

	slen = strlen(cp);
	*value = cp;
	return slen;
}

/**
 * smack_setprocattr - Smack process attribute setting
 * @name: the name of the attribute in /proc/.../attr
 * @value: the value to set
 * @size: the size of the value
 *
 * Sets the Smack value of the task. Only setting self
 * is permitted and only with privilege
 *
 * Returns the length of the smack label or an error code
 */
3642
static int smack_setprocattr(const char *name, void *value, size_t size)
3643
{
3644
	struct task_smack *tsp = current_security();
D
David Howells 已提交
3645
	struct cred *new;
3646
	struct smack_known *skp;
3647 3648
	struct smack_known_list_elem *sklep;
	int rc;
3649

3650
	if (!smack_privileged(CAP_MAC_ADMIN) && list_empty(&tsp->smk_relabel))
3651 3652
		return -EPERM;

3653
	if (value == NULL || size == 0 || size >= SMK_LONGLABEL)
3654 3655 3656 3657 3658
		return -EINVAL;

	if (strcmp(name, "current") != 0)
		return -EINVAL;

3659
	skp = smk_import_entry(value, size);
3660 3661
	if (IS_ERR(skp))
		return PTR_ERR(skp);
3662

3663
	/*
3664 3665
	 * No process is ever allowed the web ("@") label
	 * and the star ("*") label.
3666
	 */
3667 3668
	if (skp == &smack_known_web || skp == &smack_known_star)
		return -EINVAL;
3669

3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680
	if (!smack_privileged(CAP_MAC_ADMIN)) {
		rc = -EPERM;
		list_for_each_entry(sklep, &tsp->smk_relabel, list)
			if (sklep->smk_label == skp) {
				rc = 0;
				break;
			}
		if (rc)
			return rc;
	}

D
David Howells 已提交
3681
	new = prepare_creds();
3682
	if (new == NULL)
D
David Howells 已提交
3683
		return -ENOMEM;
3684

3685
	tsp = new->security;
3686
	tsp->smk_task = skp;
3687 3688 3689 3690
	/*
	 * process can change its label only once
	 */
	smk_destroy_label_list(&tsp->smk_relabel);
3691

D
David Howells 已提交
3692
	commit_creds(new);
3693 3694 3695 3696 3697
	return size;
}

/**
 * smack_unix_stream_connect - Smack access on UDS
3698 3699
 * @sock: one sock
 * @other: the other sock
3700 3701 3702 3703 3704
 * @newsk: unused
 *
 * Return 0 if a subject with the smack of sock could access
 * an object with the smack of other, otherwise an error code
 */
3705 3706
static int smack_unix_stream_connect(struct sock *sock,
				     struct sock *other, struct sock *newsk)
3707
{
3708
	struct smack_known *skp;
3709
	struct smack_known *okp;
J
James Morris 已提交
3710 3711
	struct socket_smack *ssp = sock->sk_security;
	struct socket_smack *osp = other->sk_security;
3712
	struct socket_smack *nsp = newsk->sk_security;
E
Etienne Basset 已提交
3713
	struct smk_audit_info ad;
C
Casey Schaufler 已提交
3714
	int rc = 0;
3715 3716 3717
#ifdef CONFIG_AUDIT
	struct lsm_network_audit net;
#endif
C
Casey Schaufler 已提交
3718

3719 3720
	if (!smack_privileged(CAP_MAC_OVERRIDE)) {
		skp = ssp->smk_out;
3721
		okp = osp->smk_in;
3722 3723 3724 3725
#ifdef CONFIG_AUDIT
		smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
		smk_ad_setfield_u_net_sk(&ad, other);
#endif
3726 3727
		rc = smk_access(skp, okp, MAY_WRITE, &ad);
		rc = smk_bu_note("UDS connect", skp, okp, MAY_WRITE, rc);
C
Casey Schaufler 已提交
3728
		if (rc == 0) {
3729 3730
			okp = osp->smk_out;
			skp = ssp->smk_in;
3731
			rc = smk_access(okp, skp, MAY_WRITE, &ad);
3732
			rc = smk_bu_note("UDS connect", okp, skp,
C
Casey Schaufler 已提交
3733 3734
						MAY_WRITE, rc);
		}
3735
	}
C
Casey Schaufler 已提交
3736

3737 3738 3739 3740
	/*
	 * Cross reference the peer labels for SO_PEERSEC.
	 */
	if (rc == 0) {
3741 3742
		nsp->smk_packet = ssp->smk_out;
		ssp->smk_packet = osp->smk_out;
3743 3744
	}

C
Casey Schaufler 已提交
3745
	return rc;
3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757
}

/**
 * smack_unix_may_send - Smack access on UDS
 * @sock: one socket
 * @other: the other socket
 *
 * Return 0 if a subject with the smack of sock could access
 * an object with the smack of other, otherwise an error code
 */
static int smack_unix_may_send(struct socket *sock, struct socket *other)
{
C
Casey Schaufler 已提交
3758 3759
	struct socket_smack *ssp = sock->sk->sk_security;
	struct socket_smack *osp = other->sk->sk_security;
E
Etienne Basset 已提交
3760
	struct smk_audit_info ad;
C
Casey Schaufler 已提交
3761
	int rc;
3762

3763 3764 3765
#ifdef CONFIG_AUDIT
	struct lsm_network_audit net;

3766
	smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
E
Etienne Basset 已提交
3767
	smk_ad_setfield_u_net_sk(&ad, other->sk);
3768
#endif
C
Casey Schaufler 已提交
3769

3770 3771
	if (smack_privileged(CAP_MAC_OVERRIDE))
		return 0;
C
Casey Schaufler 已提交
3772

3773 3774
	rc = smk_access(ssp->smk_out, osp->smk_in, MAY_WRITE, &ad);
	rc = smk_bu_note("UDS send", ssp->smk_out, osp->smk_in, MAY_WRITE, rc);
C
Casey Schaufler 已提交
3775
	return rc;
3776 3777
}

3778 3779 3780
/**
 * smack_socket_sendmsg - Smack check based on destination host
 * @sock: the socket
3781
 * @msg: the message
3782 3783
 * @size: the size of the message
 *
3784 3785 3786
 * Return 0 if the current subject can write to the destination host.
 * For IPv4 this is only a question if the destination is a single label host.
 * For IPv6 this is a check against the label of the port.
3787 3788 3789 3790 3791
 */
static int smack_socket_sendmsg(struct socket *sock, struct msghdr *msg,
				int size)
{
	struct sockaddr_in *sip = (struct sockaddr_in *) msg->msg_name;
C
Casey Schaufler 已提交
3792
#if IS_ENABLED(CONFIG_IPV6)
3793
	struct sockaddr_in6 *sap = (struct sockaddr_in6 *) msg->msg_name;
C
Casey Schaufler 已提交
3794 3795 3796 3797 3798
#endif
#ifdef SMACK_IPV6_SECMARK_LABELING
	struct socket_smack *ssp = sock->sk->sk_security;
	struct smack_known *rsp;
#endif
3799
	int rc = 0;
3800 3801 3802 3803

	/*
	 * Perfectly reasonable for this to be NULL
	 */
3804
	if (sip == NULL)
3805 3806
		return 0;

3807
	switch (sock->sk->sk_family) {
3808 3809 3810 3811
	case AF_INET:
		rc = smack_netlabel_send(sock->sk, sip);
		break;
	case AF_INET6:
C
Casey Schaufler 已提交
3812 3813 3814 3815 3816 3817 3818
#ifdef SMACK_IPV6_SECMARK_LABELING
		rsp = smack_ipv6host_label(sap);
		if (rsp != NULL)
			rc = smk_ipv6_check(ssp->smk_out, rsp, sap,
						SMK_CONNECTING);
#endif
#ifdef SMACK_IPV6_PORT_LABELING
3819
		rc = smk_ipv6_port_check(sock->sk, sap, SMK_SENDING);
C
Casey Schaufler 已提交
3820
#endif
3821 3822 3823
		break;
	}
	return rc;
3824 3825
}

3826
/**
3827
 * smack_from_secattr - Convert a netlabel attr.mls.lvl/attr.mls.cat pair to smack
3828
 * @sap: netlabel secattr
3829
 * @ssp: socket security information
3830
 *
3831
 * Returns a pointer to a Smack label entry found on the label list.
3832
 */
3833 3834
static struct smack_known *smack_from_secattr(struct netlbl_lsm_secattr *sap,
						struct socket_smack *ssp)
3835
{
3836
	struct smack_known *skp;
3837
	int found = 0;
C
Casey Schaufler 已提交
3838 3839
	int acat;
	int kcat;
3840

3841
	if ((sap->flags & NETLBL_SECATTR_MLS_LVL) != 0) {
3842
		/*
3843
		 * Looks like a CIPSO packet.
3844 3845 3846
		 * If there are flags but no level netlabel isn't
		 * behaving the way we expect it to.
		 *
3847
		 * Look it up in the label table
3848 3849 3850 3851
		 * Without guidance regarding the smack value
		 * for the packet fall back on the network
		 * ambient value.
		 */
3852
		rcu_read_lock();
3853
		list_for_each_entry_rcu(skp, &smack_known_list, list) {
3854
			if (sap->attr.mls.lvl != skp->smk_netlabel.attr.mls.lvl)
3855
				continue;
C
Casey Schaufler 已提交
3856 3857 3858 3859 3860 3861 3862 3863 3864 3865
			/*
			 * Compare the catsets. Use the netlbl APIs.
			 */
			if ((sap->flags & NETLBL_SECATTR_MLS_CAT) == 0) {
				if ((skp->smk_netlabel.flags &
				     NETLBL_SECATTR_MLS_CAT) == 0)
					found = 1;
				break;
			}
			for (acat = -1, kcat = -1; acat == kcat; ) {
3866 3867 3868
				acat = netlbl_catmap_walk(sap->attr.mls.cat,
							  acat + 1);
				kcat = netlbl_catmap_walk(
C
Casey Schaufler 已提交
3869 3870 3871 3872 3873 3874 3875 3876 3877
					skp->smk_netlabel.attr.mls.cat,
					kcat + 1);
				if (acat < 0 || kcat < 0)
					break;
			}
			if (acat == kcat) {
				found = 1;
				break;
			}
3878
		}
3879 3880 3881
		rcu_read_unlock();

		if (found)
3882
			return skp;
3883

3884
		if (ssp != NULL && ssp->smk_in == &smack_known_star)
3885 3886
			return &smack_known_web;
		return &smack_known_star;
3887
	}
3888
	if ((sap->flags & NETLBL_SECATTR_SECID) != 0)
3889 3890 3891
		/*
		 * Looks like a fallback, which gives us a secid.
		 */
3892
		return smack_from_secid(sap->attr.secid);
3893
	/*
3894 3895 3896
	 * Without guidance regarding the smack value
	 * for the packet fall back on the network
	 * ambient value.
3897
	 */
3898
	return smack_net_ambient;
3899 3900
}

3901
#if IS_ENABLED(CONFIG_IPV6)
3902
static int smk_skb_to_addr_ipv6(struct sk_buff *skb, struct sockaddr_in6 *sip)
3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947
{
	u8 nexthdr;
	int offset;
	int proto = -EINVAL;
	struct ipv6hdr _ipv6h;
	struct ipv6hdr *ip6;
	__be16 frag_off;
	struct tcphdr _tcph, *th;
	struct udphdr _udph, *uh;
	struct dccp_hdr _dccph, *dh;

	sip->sin6_port = 0;

	offset = skb_network_offset(skb);
	ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
	if (ip6 == NULL)
		return -EINVAL;
	sip->sin6_addr = ip6->saddr;

	nexthdr = ip6->nexthdr;
	offset += sizeof(_ipv6h);
	offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
	if (offset < 0)
		return -EINVAL;

	proto = nexthdr;
	switch (proto) {
	case IPPROTO_TCP:
		th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
		if (th != NULL)
			sip->sin6_port = th->source;
		break;
	case IPPROTO_UDP:
		uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
		if (uh != NULL)
			sip->sin6_port = uh->source;
		break;
	case IPPROTO_DCCP:
		dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
		if (dh != NULL)
			sip->sin6_port = dh->dccph_sport;
		break;
	}
	return proto;
}
3948
#endif /* CONFIG_IPV6 */
3949

3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960
/**
 * smack_socket_sock_rcv_skb - Smack packet delivery access check
 * @sk: socket
 * @skb: packet
 *
 * Returns 0 if the packet should be delivered, an error code otherwise
 */
static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
{
	struct netlbl_lsm_secattr secattr;
	struct socket_smack *ssp = sk->sk_security;
3961
	struct smack_known *skp = NULL;
3962
	int rc = 0;
E
Etienne Basset 已提交
3963
	struct smk_audit_info ad;
3964
#ifdef CONFIG_AUDIT
3965
	struct lsm_network_audit net;
3966
#endif
3967 3968 3969 3970 3971
#if IS_ENABLED(CONFIG_IPV6)
	struct sockaddr_in6 sadd;
	int proto;
#endif /* CONFIG_IPV6 */

3972 3973
	switch (sk->sk_family) {
	case PF_INET:
3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984
#ifdef CONFIG_SECURITY_SMACK_NETFILTER
		/*
		 * If there is a secmark use it rather than the CIPSO label.
		 * If there is no secmark fall back to CIPSO.
		 * The secmark is assumed to reflect policy better.
		 */
		if (skb && skb->secmark != 0) {
			skp = smack_from_secid(skb->secmark);
			goto access_check;
		}
#endif /* CONFIG_SECURITY_SMACK_NETFILTER */
3985 3986 3987 3988
		/*
		 * Translate what netlabel gave us.
		 */
		netlbl_secattr_init(&secattr);
3989

3990 3991
		rc = netlbl_skbuff_getattr(skb, sk->sk_family, &secattr);
		if (rc == 0)
3992
			skp = smack_from_secattr(&secattr, ssp);
3993
		else
3994
			skp = smack_net_ambient;
3995

3996
		netlbl_secattr_destroy(&secattr);
3997

3998 3999 4000
#ifdef CONFIG_SECURITY_SMACK_NETFILTER
access_check:
#endif
E
Etienne Basset 已提交
4001
#ifdef CONFIG_AUDIT
4002 4003 4004 4005
		smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
		ad.a.u.net->family = sk->sk_family;
		ad.a.u.net->netif = skb->skb_iif;
		ipv4_skb_to_auditdata(skb, &ad.a, NULL);
E
Etienne Basset 已提交
4006
#endif
4007 4008 4009 4010 4011 4012
		/*
		 * Receiving a packet requires that the other end
		 * be able to write here. Read access is not required.
		 * This is the simplist possible security model
		 * for networking.
		 */
4013 4014
		rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
		rc = smk_bu_note("IPv4 delivery", skp, ssp->smk_in,
C
Casey Schaufler 已提交
4015
					MAY_WRITE, rc);
4016
		if (rc != 0)
4017
			netlbl_skbuff_err(skb, sk->sk_family, rc, 0);
4018
		break;
4019
#if IS_ENABLED(CONFIG_IPV6)
4020
	case PF_INET6:
4021 4022 4023
		proto = smk_skb_to_addr_ipv6(skb, &sadd);
		if (proto != IPPROTO_UDP && proto != IPPROTO_TCP)
			break;
C
Casey Schaufler 已提交
4024
#ifdef SMACK_IPV6_SECMARK_LABELING
4025 4026
		if (skb && skb->secmark != 0)
			skp = smack_from_secid(skb->secmark);
4027
		else
C
Casey Schaufler 已提交
4028 4029
			skp = smack_ipv6host_label(&sadd);
		if (skp == NULL)
4030 4031 4032 4033 4034 4035 4036 4037 4038 4039
			skp = smack_net_ambient;
#ifdef CONFIG_AUDIT
		smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
		ad.a.u.net->family = sk->sk_family;
		ad.a.u.net->netif = skb->skb_iif;
		ipv6_skb_to_auditdata(skb, &ad.a, NULL);
#endif /* CONFIG_AUDIT */
		rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
		rc = smk_bu_note("IPv6 delivery", skp, ssp->smk_in,
					MAY_WRITE, rc);
C
Casey Schaufler 已提交
4040 4041
#endif /* SMACK_IPV6_SECMARK_LABELING */
#ifdef SMACK_IPV6_PORT_LABELING
4042
		rc = smk_ipv6_port_check(sk, &sadd, SMK_RECEIVING);
C
Casey Schaufler 已提交
4043
#endif /* SMACK_IPV6_PORT_LABELING */
4044
		break;
4045
#endif /* CONFIG_IPV6 */
4046
	}
4047

4048
	return rc;
4049 4050 4051 4052 4053 4054 4055
}

/**
 * smack_socket_getpeersec_stream - pull in packet label
 * @sock: the socket
 * @optval: user's destination
 * @optlen: size thereof
4056
 * @len: max thereof
4057 4058 4059 4060 4061 4062 4063 4064
 *
 * returns zero on success, an error code otherwise
 */
static int smack_socket_getpeersec_stream(struct socket *sock,
					  char __user *optval,
					  int __user *optlen, unsigned len)
{
	struct socket_smack *ssp;
4065 4066
	char *rcp = "";
	int slen = 1;
4067 4068 4069
	int rc = 0;

	ssp = sock->sk->sk_security;
4070
	if (ssp->smk_packet != NULL) {
4071
		rcp = ssp->smk_packet->smk_known;
4072 4073
		slen = strlen(rcp) + 1;
	}
4074 4075 4076

	if (slen > len)
		rc = -ERANGE;
4077
	else if (copy_to_user(optval, rcp, slen) != 0)
4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088
		rc = -EFAULT;

	if (put_user(slen, optlen) != 0)
		rc = -EFAULT;

	return rc;
}


/**
 * smack_socket_getpeersec_dgram - pull in packet label
C
Casey Schaufler 已提交
4089
 * @sock: the peer socket
4090 4091 4092 4093 4094 4095 4096 4097 4098 4099
 * @skb: packet data
 * @secid: pointer to where to put the secid of the packet
 *
 * Sets the netlabel socket state on sk from parent
 */
static int smack_socket_getpeersec_dgram(struct socket *sock,
					 struct sk_buff *skb, u32 *secid)

{
	struct netlbl_lsm_secattr secattr;
4100
	struct socket_smack *ssp = NULL;
4101
	struct smack_known *skp;
C
Casey Schaufler 已提交
4102 4103
	int family = PF_UNSPEC;
	u32 s = 0;	/* 0 is the invalid secid */
4104 4105
	int rc;

C
Casey Schaufler 已提交
4106 4107 4108
	if (skb != NULL) {
		if (skb->protocol == htons(ETH_P_IP))
			family = PF_INET;
4109
#if IS_ENABLED(CONFIG_IPV6)
C
Casey Schaufler 已提交
4110 4111
		else if (skb->protocol == htons(ETH_P_IPV6))
			family = PF_INET6;
4112
#endif /* CONFIG_IPV6 */
4113
	}
C
Casey Schaufler 已提交
4114 4115
	if (family == PF_UNSPEC && sock != NULL)
		family = sock->sk->sk_family;
4116

4117 4118
	switch (family) {
	case PF_UNIX:
4119
		ssp = sock->sk->sk_security;
4120
		s = ssp->smk_out->smk_secid;
4121 4122 4123 4124 4125 4126 4127
		break;
	case PF_INET:
#ifdef CONFIG_SECURITY_SMACK_NETFILTER
		s = skb->secmark;
		if (s != 0)
			break;
#endif
C
Casey Schaufler 已提交
4128 4129 4130
		/*
		 * Translate what netlabel gave us.
		 */
4131 4132
		if (sock != NULL && sock->sk != NULL)
			ssp = sock->sk->sk_security;
C
Casey Schaufler 已提交
4133 4134 4135
		netlbl_secattr_init(&secattr);
		rc = netlbl_skbuff_getattr(skb, family, &secattr);
		if (rc == 0) {
4136 4137
			skp = smack_from_secattr(&secattr, ssp);
			s = skp->smk_secid;
C
Casey Schaufler 已提交
4138 4139
		}
		netlbl_secattr_destroy(&secattr);
4140 4141
		break;
	case PF_INET6:
C
Casey Schaufler 已提交
4142
#ifdef SMACK_IPV6_SECMARK_LABELING
4143
		s = skb->secmark;
C
Casey Schaufler 已提交
4144
#endif
4145
		break;
C
Casey Schaufler 已提交
4146 4147
	}
	*secid = s;
4148 4149 4150 4151 4152 4153
	if (s == 0)
		return -EINVAL;
	return 0;
}

/**
4154 4155 4156
 * smack_sock_graft - Initialize a newly created socket with an existing sock
 * @sk: child sock
 * @parent: parent socket
4157
 *
4158 4159
 * Set the smk_{in,out} state of an existing sock based on the process that
 * is creating the new socket.
4160 4161 4162 4163
 */
static void smack_sock_graft(struct sock *sk, struct socket *parent)
{
	struct socket_smack *ssp;
4164
	struct smack_known *skp = smk_of_current();
4165

4166 4167
	if (sk == NULL ||
	    (sk->sk_family != PF_INET && sk->sk_family != PF_INET6))
4168 4169 4170
		return;

	ssp = sk->sk_security;
4171
	ssp->smk_in = skp;
4172
	ssp->smk_out = skp;
4173
	/* cssp->smk_packet is already set in smack_inet_csk_clone() */
4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187
}

/**
 * smack_inet_conn_request - Smack access check on connect
 * @sk: socket involved
 * @skb: packet
 * @req: unused
 *
 * Returns 0 if a task with the packet label could write to
 * the socket, otherwise an error code
 */
static int smack_inet_conn_request(struct sock *sk, struct sk_buff *skb,
				   struct request_sock *req)
{
4188
	u16 family = sk->sk_family;
4189
	struct smack_known *skp;
4190
	struct socket_smack *ssp = sk->sk_security;
4191 4192 4193
	struct netlbl_lsm_secattr secattr;
	struct sockaddr_in addr;
	struct iphdr *hdr;
4194
	struct smack_known *hskp;
4195
	int rc;
E
Etienne Basset 已提交
4196
	struct smk_audit_info ad;
4197
#ifdef CONFIG_AUDIT
4198
	struct lsm_network_audit net;
4199
#endif
4200

4201
#if IS_ENABLED(CONFIG_IPV6)
4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212
	if (family == PF_INET6) {
		/*
		 * Handle mapped IPv4 packets arriving
		 * via IPv6 sockets. Don't set up netlabel
		 * processing on IPv6.
		 */
		if (skb->protocol == htons(ETH_P_IP))
			family = PF_INET;
		else
			return 0;
	}
4213
#endif /* CONFIG_IPV6 */
4214

C
Casey Schaufler 已提交
4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226
#ifdef CONFIG_SECURITY_SMACK_NETFILTER
	/*
	 * If there is a secmark use it rather than the CIPSO label.
	 * If there is no secmark fall back to CIPSO.
	 * The secmark is assumed to reflect policy better.
	 */
	if (skb && skb->secmark != 0) {
		skp = smack_from_secid(skb->secmark);
		goto access_check;
	}
#endif /* CONFIG_SECURITY_SMACK_NETFILTER */

4227 4228
	netlbl_secattr_init(&secattr);
	rc = netlbl_skbuff_getattr(skb, family, &secattr);
4229
	if (rc == 0)
4230
		skp = smack_from_secattr(&secattr, ssp);
4231
	else
4232
		skp = &smack_known_huh;
4233 4234
	netlbl_secattr_destroy(&secattr);

C
Casey Schaufler 已提交
4235 4236 4237 4238
#ifdef CONFIG_SECURITY_SMACK_NETFILTER
access_check:
#endif

E
Etienne Basset 已提交
4239
#ifdef CONFIG_AUDIT
4240 4241 4242
	smk_ad_init_net(&ad, __func__, LSM_AUDIT_DATA_NET, &net);
	ad.a.u.net->family = family;
	ad.a.u.net->netif = skb->skb_iif;
E
Etienne Basset 已提交
4243 4244
	ipv4_skb_to_auditdata(skb, &ad.a, NULL);
#endif
4245
	/*
4246 4247
	 * Receiving a packet requires that the other end be able to write
	 * here. Read access is not required.
4248
	 */
4249 4250
	rc = smk_access(skp, ssp->smk_in, MAY_WRITE, &ad);
	rc = smk_bu_note("IPv4 connect", skp, ssp->smk_in, MAY_WRITE, rc);
4251 4252 4253 4254 4255 4256 4257
	if (rc != 0)
		return rc;

	/*
	 * Save the peer's label in the request_sock so we can later setup
	 * smk_packet in the child socket so that SO_PEERCRED can report it.
	 */
4258
	req->peer_secid = skp->smk_secid;
4259 4260 4261 4262

	/*
	 * We need to decide if we want to label the incoming connection here
	 * if we do we only need to label the request_sock and the stack will
L
Lucas De Marchi 已提交
4263
	 * propagate the wire-label to the sock when it is created.
4264 4265 4266 4267
	 */
	hdr = ip_hdr(skb);
	addr.sin_addr.s_addr = hdr->saddr;
	rcu_read_lock();
C
Casey Schaufler 已提交
4268
	hskp = smack_ipv4host_label(&addr);
4269 4270
	rcu_read_unlock();

4271
	if (hskp == NULL)
4272
		rc = netlbl_req_setattr(req, &skp->smk_netlabel);
4273
	else
4274
		netlbl_req_delattr(req);
4275 4276 4277 4278

	return rc;
}

4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289
/**
 * smack_inet_csk_clone - Copy the connection information to the new socket
 * @sk: the new socket
 * @req: the connection's request_sock
 *
 * Transfer the connection's peer label to the newly created socket.
 */
static void smack_inet_csk_clone(struct sock *sk,
				 const struct request_sock *req)
{
	struct socket_smack *ssp = sk->sk_security;
4290
	struct smack_known *skp;
4291

4292 4293
	if (req->peer_secid != 0) {
		skp = smack_from_secid(req->peer_secid);
4294
		ssp->smk_packet = skp;
4295
	} else
4296
		ssp->smk_packet = NULL;
4297 4298
}

4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310
/*
 * Key management security hooks
 *
 * Casey has not tested key support very heavily.
 * The permission check is most likely too restrictive.
 * If you care about keys please have a look.
 */
#ifdef CONFIG_KEYS

/**
 * smack_key_alloc - Set the key security blob
 * @key: object
D
David Howells 已提交
4311
 * @cred: the credentials to use
4312 4313 4314 4315 4316 4317
 * @flags: unused
 *
 * No allocation required
 *
 * Returns 0
 */
D
David Howells 已提交
4318
static int smack_key_alloc(struct key *key, const struct cred *cred,
4319 4320
			   unsigned long flags)
{
4321 4322
	struct smack_known *skp = smk_of_task(cred->security);

4323
	key->security = skp;
4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337
	return 0;
}

/**
 * smack_key_free - Clear the key security blob
 * @key: the object
 *
 * Clear the blob pointer
 */
static void smack_key_free(struct key *key)
{
	key->security = NULL;
}

4338
/**
4339 4340
 * smack_key_permission - Smack access on a key
 * @key_ref: gets to the object
D
David Howells 已提交
4341
 * @cred: the credentials to use
4342
 * @perm: requested key permissions
4343 4344 4345 4346 4347
 *
 * Return 0 if the task has read and write to the object,
 * an error code otherwise
 */
static int smack_key_permission(key_ref_t key_ref,
4348
				const struct cred *cred, unsigned perm)
4349 4350
{
	struct key *keyp;
E
Etienne Basset 已提交
4351
	struct smk_audit_info ad;
4352
	struct smack_known *tkp = smk_of_task(cred->security);
4353
	int request = 0;
C
Casey Schaufler 已提交
4354
	int rc;
4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367

	keyp = key_ref_to_ptr(key_ref);
	if (keyp == NULL)
		return -EINVAL;
	/*
	 * If the key hasn't been initialized give it access so that
	 * it may do so.
	 */
	if (keyp->security == NULL)
		return 0;
	/*
	 * This should not occur
	 */
4368
	if (tkp == NULL)
4369
		return -EACCES;
4370 4371 4372 4373

	if (smack_privileged_cred(CAP_MAC_OVERRIDE, cred))
		return 0;

E
Etienne Basset 已提交
4374 4375 4376 4377 4378
#ifdef CONFIG_AUDIT
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_KEY);
	ad.a.u.key_struct.key = keyp->serial;
	ad.a.u.key_struct.key_desc = keyp->description;
#endif
4379 4380 4381 4382
	if (perm & KEY_NEED_READ)
		request = MAY_READ;
	if (perm & (KEY_NEED_WRITE | KEY_NEED_LINK | KEY_NEED_SETATTR))
		request = MAY_WRITE;
C
Casey Schaufler 已提交
4383 4384 4385
	rc = smk_access(tkp, keyp->security, request, &ad);
	rc = smk_bu_note("key access", tkp, keyp->security, request, rc);
	return rc;
4386
}
4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416

/*
 * smack_key_getsecurity - Smack label tagging the key
 * @key points to the key to be queried
 * @_buffer points to a pointer that should be set to point to the
 * resulting string (if no label or an error occurs).
 * Return the length of the string (including terminating NUL) or -ve if
 * an error.
 * May also return 0 (and a NULL buffer pointer) if there is no label.
 */
static int smack_key_getsecurity(struct key *key, char **_buffer)
{
	struct smack_known *skp = key->security;
	size_t length;
	char *copy;

	if (key->security == NULL) {
		*_buffer = NULL;
		return 0;
	}

	copy = kstrdup(skp->smk_known, GFP_KERNEL);
	if (copy == NULL)
		return -ENOMEM;
	length = strlen(copy) + 1;

	*_buffer = copy;
	return length;
}

4417 4418
#endif /* CONFIG_KEYS */

4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445
/*
 * Smack Audit hooks
 *
 * Audit requires a unique representation of each Smack specific
 * rule. This unique representation is used to distinguish the
 * object to be audited from remaining kernel objects and also
 * works as a glue between the audit hooks.
 *
 * Since repository entries are added but never deleted, we'll use
 * the smack_known label address related to the given audit rule as
 * the needed unique representation. This also better fits the smack
 * model where nearly everything is a label.
 */
#ifdef CONFIG_AUDIT

/**
 * smack_audit_rule_init - Initialize a smack audit rule
 * @field: audit rule fields given from user-space (audit.h)
 * @op: required testing operator (=, !=, >, <, ...)
 * @rulestr: smack label to be audited
 * @vrule: pointer to save our own audit rule representation
 *
 * Prepare to audit cases where (@field @op @rulestr) is true.
 * The label to be audited is created if necessay.
 */
static int smack_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
{
4446
	struct smack_known *skp;
4447 4448 4449 4450 4451 4452
	char **rule = (char **)vrule;
	*rule = NULL;

	if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
		return -EINVAL;

4453
	if (op != Audit_equal && op != Audit_not_equal)
4454 4455
		return -EINVAL;

4456
	skp = smk_import_entry(rulestr, 0);
4457 4458 4459 4460
	if (IS_ERR(skp))
		return PTR_ERR(skp);

	*rule = skp->smk_known;
4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501

	return 0;
}

/**
 * smack_audit_rule_known - Distinguish Smack audit rules
 * @krule: rule of interest, in Audit kernel representation format
 *
 * This is used to filter Smack rules from remaining Audit ones.
 * If it's proved that this rule belongs to us, the
 * audit_rule_match hook will be called to do the final judgement.
 */
static int smack_audit_rule_known(struct audit_krule *krule)
{
	struct audit_field *f;
	int i;

	for (i = 0; i < krule->field_count; i++) {
		f = &krule->fields[i];

		if (f->type == AUDIT_SUBJ_USER || f->type == AUDIT_OBJ_USER)
			return 1;
	}

	return 0;
}

/**
 * smack_audit_rule_match - Audit given object ?
 * @secid: security id for identifying the object to test
 * @field: audit rule flags given from user-space
 * @op: required testing operator
 * @vrule: smack internal rule presentation
 * @actx: audit context associated with the check
 *
 * The core Audit hook. It's used to take the decision of
 * whether to audit or not to audit a given object.
 */
static int smack_audit_rule_match(u32 secid, u32 field, u32 op, void *vrule,
				  struct audit_context *actx)
{
4502
	struct smack_known *skp;
4503 4504
	char *rule = vrule;

4505 4506
	if (unlikely(!rule)) {
		WARN_ONCE(1, "Smack: missing rule\n");
4507 4508 4509 4510 4511 4512
		return -ENOENT;
	}

	if (field != AUDIT_SUBJ_USER && field != AUDIT_OBJ_USER)
		return 0;

4513
	skp = smack_from_secid(secid);
4514 4515 4516 4517 4518 4519

	/*
	 * No need to do string comparisons. If a match occurs,
	 * both pointers will point to the same smack_known
	 * label.
	 */
4520
	if (op == Audit_equal)
4521
		return (rule == skp->smk_known);
4522
	if (op == Audit_not_equal)
4523
		return (rule != skp->smk_known);
4524 4525 4526 4527

	return 0;
}

C
Casey Schaufler 已提交
4528 4529
/*
 * There is no need for a smack_audit_rule_free hook.
4530 4531 4532 4533 4534
 * No memory was allocated.
 */

#endif /* CONFIG_AUDIT */

4535 4536 4537 4538 4539 4540 4541 4542 4543 4544
/**
 * smack_ismaclabel - check if xattr @name references a smack MAC label
 * @name: Full xattr name to check.
 */
static int smack_ismaclabel(const char *name)
{
	return (strcmp(name, XATTR_SMACK_SUFFIX) == 0);
}


4545
/**
4546 4547 4548 4549 4550 4551 4552 4553 4554
 * smack_secid_to_secctx - return the smack label for a secid
 * @secid: incoming integer
 * @secdata: destination
 * @seclen: how long it is
 *
 * Exists for networking code.
 */
static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
{
4555
	struct smack_known *skp = smack_from_secid(secid);
4556

4557
	if (secdata)
4558 4559
		*secdata = skp->smk_known;
	*seclen = strlen(skp->smk_known);
4560 4561 4562
	return 0;
}

4563
/**
4564 4565 4566 4567 4568 4569 4570
 * smack_secctx_to_secid - return the secid for a smack label
 * @secdata: smack label
 * @seclen: how long result is
 * @secid: outgoing integer
 *
 * Exists for audit and networking code.
 */
4571
static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
4572
{
4573 4574 4575 4576 4577 4578
	struct smack_known *skp = smk_find_entry(secdata);

	if (skp)
		*secid = skp->smk_secid;
	else
		*secid = 0;
4579 4580 4581
	return 0;
}

C
Casey Schaufler 已提交
4582 4583 4584 4585
/*
 * There used to be a smack_release_secctx hook
 * that did nothing back when hooks were in a vector.
 * Now that there's a list such a hook adds cost.
4586 4587
 */

4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608
static int smack_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
{
	return smack_inode_setsecurity(inode, XATTR_SMACK_SUFFIX, ctx, ctxlen, 0);
}

static int smack_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
{
	return __vfs_setxattr_noperm(dentry, XATTR_NAME_SMACK, ctx, ctxlen, 0);
}

static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
{
	int len = 0;
	len = smack_inode_getsecurity(inode, XATTR_SMACK_SUFFIX, ctx, true);

	if (len < 0)
		return len;
	*ctxlen = len;
	return 0;
}

4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684
static int smack_inode_copy_up(struct dentry *dentry, struct cred **new)
{

	struct task_smack *tsp;
	struct smack_known *skp;
	struct inode_smack *isp;
	struct cred *new_creds = *new;

	if (new_creds == NULL) {
		new_creds = prepare_creds();
		if (new_creds == NULL)
			return -ENOMEM;
	}

	tsp = new_creds->security;

	/*
	 * Get label from overlay inode and set it in create_sid
	 */
	isp = d_inode(dentry->d_parent)->i_security;
	skp = isp->smk_inode;
	tsp->smk_task = skp;
	*new = new_creds;
	return 0;
}

static int smack_inode_copy_up_xattr(const char *name)
{
	/*
	 * Return 1 if this is the smack access Smack attribute.
	 */
	if (strcmp(name, XATTR_NAME_SMACK) == 0)
		return 1;

	return -EOPNOTSUPP;
}

static int smack_dentry_create_files_as(struct dentry *dentry, int mode,
					struct qstr *name,
					const struct cred *old,
					struct cred *new)
{
	struct task_smack *otsp = old->security;
	struct task_smack *ntsp = new->security;
	struct inode_smack *isp;
	int may;

	/*
	 * Use the process credential unless all of
	 * the transmuting criteria are met
	 */
	ntsp->smk_task = otsp->smk_task;

	/*
	 * the attribute of the containing directory
	 */
	isp = d_inode(dentry->d_parent)->i_security;

	if (isp->smk_flags & SMK_INODE_TRANSMUTE) {
		rcu_read_lock();
		may = smk_access_entry(otsp->smk_task->smk_known,
				       isp->smk_inode->smk_known,
				       &otsp->smk_task->smk_rules);
		rcu_read_unlock();

		/*
		 * If the directory is transmuting and the rule
		 * providing access is transmuting use the containing
		 * directory label instead of the process label.
		 */
		if (may > 0 && (may & MAY_TRANSMUTE))
			ntsp->smk_task = isp->smk_inode;
	}
	return 0;
}

4685
static struct security_hook_list smack_hooks[] __lsm_ro_after_init = {
4686 4687 4688 4689 4690 4691 4692 4693 4694
	LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
	LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
	LSM_HOOK_INIT(syslog, smack_syslog),

	LSM_HOOK_INIT(sb_alloc_security, smack_sb_alloc_security),
	LSM_HOOK_INIT(sb_free_security, smack_sb_free_security),
	LSM_HOOK_INIT(sb_copy_data, smack_sb_copy_data),
	LSM_HOOK_INIT(sb_kern_mount, smack_sb_kern_mount),
	LSM_HOOK_INIT(sb_statfs, smack_sb_statfs),
4695 4696
	LSM_HOOK_INIT(sb_set_mnt_opts, smack_set_mnt_opts),
	LSM_HOOK_INIT(sb_parse_opts_str, smack_parse_opts_str),
4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784

	LSM_HOOK_INIT(bprm_set_creds, smack_bprm_set_creds),

	LSM_HOOK_INIT(inode_alloc_security, smack_inode_alloc_security),
	LSM_HOOK_INIT(inode_free_security, smack_inode_free_security),
	LSM_HOOK_INIT(inode_init_security, smack_inode_init_security),
	LSM_HOOK_INIT(inode_link, smack_inode_link),
	LSM_HOOK_INIT(inode_unlink, smack_inode_unlink),
	LSM_HOOK_INIT(inode_rmdir, smack_inode_rmdir),
	LSM_HOOK_INIT(inode_rename, smack_inode_rename),
	LSM_HOOK_INIT(inode_permission, smack_inode_permission),
	LSM_HOOK_INIT(inode_setattr, smack_inode_setattr),
	LSM_HOOK_INIT(inode_getattr, smack_inode_getattr),
	LSM_HOOK_INIT(inode_setxattr, smack_inode_setxattr),
	LSM_HOOK_INIT(inode_post_setxattr, smack_inode_post_setxattr),
	LSM_HOOK_INIT(inode_getxattr, smack_inode_getxattr),
	LSM_HOOK_INIT(inode_removexattr, smack_inode_removexattr),
	LSM_HOOK_INIT(inode_getsecurity, smack_inode_getsecurity),
	LSM_HOOK_INIT(inode_setsecurity, smack_inode_setsecurity),
	LSM_HOOK_INIT(inode_listsecurity, smack_inode_listsecurity),
	LSM_HOOK_INIT(inode_getsecid, smack_inode_getsecid),

	LSM_HOOK_INIT(file_alloc_security, smack_file_alloc_security),
	LSM_HOOK_INIT(file_free_security, smack_file_free_security),
	LSM_HOOK_INIT(file_ioctl, smack_file_ioctl),
	LSM_HOOK_INIT(file_lock, smack_file_lock),
	LSM_HOOK_INIT(file_fcntl, smack_file_fcntl),
	LSM_HOOK_INIT(mmap_file, smack_mmap_file),
	LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
	LSM_HOOK_INIT(file_set_fowner, smack_file_set_fowner),
	LSM_HOOK_INIT(file_send_sigiotask, smack_file_send_sigiotask),
	LSM_HOOK_INIT(file_receive, smack_file_receive),

	LSM_HOOK_INIT(file_open, smack_file_open),

	LSM_HOOK_INIT(cred_alloc_blank, smack_cred_alloc_blank),
	LSM_HOOK_INIT(cred_free, smack_cred_free),
	LSM_HOOK_INIT(cred_prepare, smack_cred_prepare),
	LSM_HOOK_INIT(cred_transfer, smack_cred_transfer),
	LSM_HOOK_INIT(kernel_act_as, smack_kernel_act_as),
	LSM_HOOK_INIT(kernel_create_files_as, smack_kernel_create_files_as),
	LSM_HOOK_INIT(task_setpgid, smack_task_setpgid),
	LSM_HOOK_INIT(task_getpgid, smack_task_getpgid),
	LSM_HOOK_INIT(task_getsid, smack_task_getsid),
	LSM_HOOK_INIT(task_getsecid, smack_task_getsecid),
	LSM_HOOK_INIT(task_setnice, smack_task_setnice),
	LSM_HOOK_INIT(task_setioprio, smack_task_setioprio),
	LSM_HOOK_INIT(task_getioprio, smack_task_getioprio),
	LSM_HOOK_INIT(task_setscheduler, smack_task_setscheduler),
	LSM_HOOK_INIT(task_getscheduler, smack_task_getscheduler),
	LSM_HOOK_INIT(task_movememory, smack_task_movememory),
	LSM_HOOK_INIT(task_kill, smack_task_kill),
	LSM_HOOK_INIT(task_to_inode, smack_task_to_inode),

	LSM_HOOK_INIT(ipc_permission, smack_ipc_permission),
	LSM_HOOK_INIT(ipc_getsecid, smack_ipc_getsecid),

	LSM_HOOK_INIT(msg_msg_alloc_security, smack_msg_msg_alloc_security),
	LSM_HOOK_INIT(msg_msg_free_security, smack_msg_msg_free_security),

	LSM_HOOK_INIT(msg_queue_alloc_security, smack_msg_queue_alloc_security),
	LSM_HOOK_INIT(msg_queue_free_security, smack_msg_queue_free_security),
	LSM_HOOK_INIT(msg_queue_associate, smack_msg_queue_associate),
	LSM_HOOK_INIT(msg_queue_msgctl, smack_msg_queue_msgctl),
	LSM_HOOK_INIT(msg_queue_msgsnd, smack_msg_queue_msgsnd),
	LSM_HOOK_INIT(msg_queue_msgrcv, smack_msg_queue_msgrcv),

	LSM_HOOK_INIT(shm_alloc_security, smack_shm_alloc_security),
	LSM_HOOK_INIT(shm_free_security, smack_shm_free_security),
	LSM_HOOK_INIT(shm_associate, smack_shm_associate),
	LSM_HOOK_INIT(shm_shmctl, smack_shm_shmctl),
	LSM_HOOK_INIT(shm_shmat, smack_shm_shmat),

	LSM_HOOK_INIT(sem_alloc_security, smack_sem_alloc_security),
	LSM_HOOK_INIT(sem_free_security, smack_sem_free_security),
	LSM_HOOK_INIT(sem_associate, smack_sem_associate),
	LSM_HOOK_INIT(sem_semctl, smack_sem_semctl),
	LSM_HOOK_INIT(sem_semop, smack_sem_semop),

	LSM_HOOK_INIT(d_instantiate, smack_d_instantiate),

	LSM_HOOK_INIT(getprocattr, smack_getprocattr),
	LSM_HOOK_INIT(setprocattr, smack_setprocattr),

	LSM_HOOK_INIT(unix_stream_connect, smack_unix_stream_connect),
	LSM_HOOK_INIT(unix_may_send, smack_unix_may_send),

	LSM_HOOK_INIT(socket_post_create, smack_socket_post_create),
C
Casey Schaufler 已提交
4785
#ifdef SMACK_IPV6_PORT_LABELING
4786
	LSM_HOOK_INIT(socket_bind, smack_socket_bind),
C
Casey Schaufler 已提交
4787
#endif
4788 4789 4790 4791 4792 4793 4794 4795 4796 4797
	LSM_HOOK_INIT(socket_connect, smack_socket_connect),
	LSM_HOOK_INIT(socket_sendmsg, smack_socket_sendmsg),
	LSM_HOOK_INIT(socket_sock_rcv_skb, smack_socket_sock_rcv_skb),
	LSM_HOOK_INIT(socket_getpeersec_stream, smack_socket_getpeersec_stream),
	LSM_HOOK_INIT(socket_getpeersec_dgram, smack_socket_getpeersec_dgram),
	LSM_HOOK_INIT(sk_alloc_security, smack_sk_alloc_security),
	LSM_HOOK_INIT(sk_free_security, smack_sk_free_security),
	LSM_HOOK_INIT(sock_graft, smack_sock_graft),
	LSM_HOOK_INIT(inet_conn_request, smack_inet_conn_request),
	LSM_HOOK_INIT(inet_csk_clone, smack_inet_csk_clone),
4798

4799 4800
 /* key management security hooks */
#ifdef CONFIG_KEYS
4801 4802 4803 4804
	LSM_HOOK_INIT(key_alloc, smack_key_alloc),
	LSM_HOOK_INIT(key_free, smack_key_free),
	LSM_HOOK_INIT(key_permission, smack_key_permission),
	LSM_HOOK_INIT(key_getsecurity, smack_key_getsecurity),
4805
#endif /* CONFIG_KEYS */
4806 4807 4808

 /* Audit hooks */
#ifdef CONFIG_AUDIT
4809 4810 4811
	LSM_HOOK_INIT(audit_rule_init, smack_audit_rule_init),
	LSM_HOOK_INIT(audit_rule_known, smack_audit_rule_known),
	LSM_HOOK_INIT(audit_rule_match, smack_audit_rule_match),
4812 4813
#endif /* CONFIG_AUDIT */

4814 4815 4816 4817 4818 4819
	LSM_HOOK_INIT(ismaclabel, smack_ismaclabel),
	LSM_HOOK_INIT(secid_to_secctx, smack_secid_to_secctx),
	LSM_HOOK_INIT(secctx_to_secid, smack_secctx_to_secid),
	LSM_HOOK_INIT(inode_notifysecctx, smack_inode_notifysecctx),
	LSM_HOOK_INIT(inode_setsecctx, smack_inode_setsecctx),
	LSM_HOOK_INIT(inode_getsecctx, smack_inode_getsecctx),
4820 4821 4822
	LSM_HOOK_INIT(inode_copy_up, smack_inode_copy_up),
	LSM_HOOK_INIT(inode_copy_up_xattr, smack_inode_copy_up_xattr),
	LSM_HOOK_INIT(dentry_create_files_as, smack_dentry_create_files_as),
4823 4824
};

4825

4826
static __init void init_smack_known_list(void)
4827
{
4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846
	/*
	 * Initialize rule list locks
	 */
	mutex_init(&smack_known_huh.smk_rules_lock);
	mutex_init(&smack_known_hat.smk_rules_lock);
	mutex_init(&smack_known_floor.smk_rules_lock);
	mutex_init(&smack_known_star.smk_rules_lock);
	mutex_init(&smack_known_web.smk_rules_lock);
	/*
	 * Initialize rule lists
	 */
	INIT_LIST_HEAD(&smack_known_huh.smk_rules);
	INIT_LIST_HEAD(&smack_known_hat.smk_rules);
	INIT_LIST_HEAD(&smack_known_star.smk_rules);
	INIT_LIST_HEAD(&smack_known_floor.smk_rules);
	INIT_LIST_HEAD(&smack_known_web.smk_rules);
	/*
	 * Create the known labels list
	 */
4847 4848 4849 4850 4851
	smk_insert_entry(&smack_known_huh);
	smk_insert_entry(&smack_known_hat);
	smk_insert_entry(&smack_known_star);
	smk_insert_entry(&smack_known_floor);
	smk_insert_entry(&smack_known_web);
4852 4853
}

4854 4855 4856 4857 4858 4859 4860
/**
 * smack_init - initialize the smack system
 *
 * Returns 0
 */
static __init int smack_init(void)
{
D
David Howells 已提交
4861
	struct cred *cred;
4862
	struct task_smack *tsp;
D
David Howells 已提交
4863

C
Casey Schaufler 已提交
4864
	if (!security_module_enable("smack"))
4865 4866
		return 0;

4867 4868 4869 4870
	smack_inode_cache = KMEM_CACHE(inode_smack, 0);
	if (!smack_inode_cache)
		return -ENOMEM;

4871 4872
	tsp = new_task_smack(&smack_known_floor, &smack_known_floor,
				GFP_KERNEL);
4873 4874
	if (tsp == NULL) {
		kmem_cache_destroy(smack_inode_cache);
4875
		return -ENOMEM;
4876
	}
4877

4878 4879
	smack_enabled = 1;

C
Casey Schaufler 已提交
4880 4881 4882 4883 4884 4885 4886 4887 4888 4889
	pr_info("Smack:  Initializing.\n");
#ifdef CONFIG_SECURITY_SMACK_NETFILTER
	pr_info("Smack:  Netfilter enabled.\n");
#endif
#ifdef SMACK_IPV6_PORT_LABELING
	pr_info("Smack:  IPv6 port labeling enabled.\n");
#endif
#ifdef SMACK_IPV6_SECMARK_LABELING
	pr_info("Smack:  IPv6 Netfilter enabled.\n");
#endif
4890 4891 4892 4893

	/*
	 * Set the security state for the initial task.
	 */
D
David Howells 已提交
4894
	cred = (struct cred *) current->cred;
4895
	cred->security = tsp;
4896

4897 4898
	/* initialize the smack_known_list */
	init_smack_known_list();
4899 4900 4901 4902

	/*
	 * Register with LSM
	 */
4903
	security_add_hooks(smack_hooks, ARRAY_SIZE(smack_hooks), "smack");
4904 4905 4906 4907 4908 4909 4910 4911 4912

	return 0;
}

/*
 * Smack requires early initialization in order to label
 * all processes and objects when they are created.
 */
security_initcall(smack_init);