smack_lsm.c 94.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>
Nick Black's avatar
Nick Black committed
38
#include <linux/magic.h>
39
#include <linux/dcache.h>
Jarkko Sakkinen's avatar
Jarkko Sakkinen committed
40
#include <linux/personality.h>
Al Viro's avatar
Al Viro committed
41 42 43
#include <linux/msg.h>
#include <linux/shm.h>
#include <linux/binfmts.h>
44 45
#include "smack.h"

46 47
#define task_security(task)	(task_cred_xxx((task), security))

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

51 52 53 54 55 56
#define SMK_CONNECTING	0
#define SMK_RECEIVING	1
#define SMK_SENDING	2

LIST_HEAD(smk_ipv6_port_list);

57 58 59 60 61 62 63 64
/**
 * smk_fetch - Fetch the smack label from a file.
 * @ip: a pointer to the inode
 * @dp: a pointer to the dentry
 *
 * Returns a pointer to the master list entry for the Smack label
 * or NULL if there was no label to fetch.
 */
65 66
static struct smack_known *smk_fetch(const char *name, struct inode *ip,
					struct dentry *dp)
67 68
{
	int rc;
69
	char *buffer;
70
	struct smack_known *skp = NULL;
71 72 73 74

	if (ip->i_op->getxattr == NULL)
		return NULL;

75 76
	buffer = kzalloc(SMK_LONGLABEL, GFP_KERNEL);
	if (buffer == NULL)
77 78
		return NULL;

79 80
	rc = ip->i_op->getxattr(dp, name, buffer, SMK_LONGLABEL);
	if (rc > 0)
81
		skp = smk_import_entry(buffer, rc);
82 83 84

	kfree(buffer);

85
	return skp;
86 87 88 89 90 91 92 93 94 95 96 97
}

/**
 * new_inode_smack - allocate an inode security blob
 * @smack: a pointer to the Smack label to use in the blob
 *
 * Returns the new blob or NULL if there's no memory available
 */
struct inode_smack *new_inode_smack(char *smack)
{
	struct inode_smack *isp;

98
	isp = kzalloc(sizeof(struct inode_smack), GFP_NOFS);
99 100 101 102 103 104 105 106 107 108
	if (isp == NULL)
		return NULL;

	isp->smk_inode = smack;
	isp->smk_flags = 0;
	mutex_init(&isp->smk_lock);

	return isp;
}

109 110 111 112 113 114
/**
 * new_task_smack - allocate a task security blob
 * @smack: a pointer to the Smack label to use in the blob
 *
 * Returns the new blob or NULL if there's no memory available
 */
115 116
static struct task_smack *new_task_smack(struct smack_known *task,
					struct smack_known *forked, gfp_t gfp)
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
{
	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);
	mutex_init(&tsp->smk_rules_lock);

	return tsp;
}

/**
 * smk_copy_rules - copy a rule set
 * @nhead - new rules header pointer
 * @ohead - old rules header pointer
 *
 * 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;

	INIT_LIST_HEAD(nhead);

	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;
}

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
/**
 * 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)
{
	switch (mode) {
	case PTRACE_MODE_READ:
		return MAY_READ;
	case PTRACE_MODE_ATTACH:
		return MAY_READWRITE;
	}

	return 0;
}

/**
 * smk_ptrace_rule_check - helper for ptrace access
 * @tracer: tracer process
181 182
 * @tracee_label: label of the process that's about to be traced,
 *                the pointer must originate from smack structures
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
 * @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
 */
static int smk_ptrace_rule_check(struct task_struct *tracer, char *tracee_label,
				 unsigned int mode, const char *func)
{
	int rc;
	struct smk_audit_info ad, *saip = NULL;
	struct task_smack *tsp;
	struct smack_known *skp;

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

	tsp = task_security(tracer);
	skp = smk_of_task(tsp);

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
	if ((mode & PTRACE_MODE_ATTACH) &&
	    (smack_ptrace_rule == SMACK_PTRACE_EXACT ||
	     smack_ptrace_rule == SMACK_PTRACE_DRACONIAN)) {
		if (skp->smk_known == tracee_label)
			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)
			smack_log(skp->smk_known, tracee_label, 0, rc, saip);

		return rc;
	}

	/* In case of rule==SMACK_PTRACE_DEFAULT or mode==PTRACE_MODE_READ */
224 225 226 227
	rc = smk_tskacc(tsp, tracee_label, smk_ptrace_mode(mode), saip);
	return rc;
}

228 229 230 231 232 233
/*
 * LSM hooks.
 * We he, that is fun!
 */

/**
234
 * smack_ptrace_access_check - Smack approval on PTRACE_ATTACH
235
 * @ctp: child task pointer
236
 * @mode: ptrace attachment mode (PTRACE_MODE_*)
237 238 239
 *
 * Returns 0 if access is OK, an error code otherwise
 *
240
 * Do the capability checks.
241
 */
242
static int smack_ptrace_access_check(struct task_struct *ctp, unsigned int mode)
243 244
{
	int rc;
245
	struct smack_known *skp;
246

247
	rc = cap_ptrace_access_check(ctp, mode);
248 249 250
	if (rc != 0)
		return rc;

251
	skp = smk_of_task(task_security(ctp));
Etienne Basset's avatar
Etienne Basset committed
252

253
	rc = smk_ptrace_rule_check(current, skp->smk_known, mode, __func__);
254 255 256 257 258 259 260 261 262
	return rc;
}

/**
 * smack_ptrace_traceme - Smack approval on PTRACE_TRACEME
 * @ptp: parent task pointer
 *
 * Returns 0 if access is OK, an error code otherwise
 *
263
 * Do the capability checks, and require PTRACE_MODE_ATTACH.
264 265 266 267
 */
static int smack_ptrace_traceme(struct task_struct *ptp)
{
	int rc;
268
	struct smack_known *skp;
269 270 271 272

	rc = cap_ptrace_traceme(ptp);
	if (rc != 0)
		return rc;
273

274
	skp = smk_of_task(current_security());
Etienne Basset's avatar
Etienne Basset committed
275

276 277
	rc = smk_ptrace_rule_check(ptp, skp->smk_known,
				   PTRACE_MODE_ATTACH, __func__);
278 279 280 281 282 283 284 285 286
	return rc;
}

/**
 * smack_syslog - Smack approval on syslog
 * @type: message type
 *
 * Returns 0 on success, error code otherwise.
 */
287
static int smack_syslog(int typefrom_file)
288
{
289
	int rc = 0;
290
	struct smack_known *skp = smk_of_current();
291

292
	if (smack_privileged(CAP_MAC_OVERRIDE))
293 294
		return 0;

295
	if (smack_syslog_label != NULL && smack_syslog_label != skp)
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
		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;

	sbsp->smk_root = smack_known_floor.smk_known;
	sbsp->smk_default = smack_known_floor.smk_known;
	sbsp->smk_floor = smack_known_floor.smk_known;
	sbsp->smk_hat = smack_known_hat.smk_known;
325 326 327
	/*
	 * smk_initialized will be zero from kzalloc.
	 */
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
	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
347
 * @smackopts: mount options string
348 349 350 351 352 353
 *
 * Returns 0 on success or -ENOMEM on error.
 *
 * Copy the Smack specific mount options out of the mount
 * options list.
 */
354
static int smack_sb_copy_data(char *orig, char *smackopts)
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
{
	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;
371 372
		else if (strstr(cp, SMK_FSTRANS) == cp)
			dp = smackopts;
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
		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;
}

/**
 * smack_sb_kern_mount - Smack specific mount processing
 * @sb: the file system superblock
394
 * @flags: the mount flags
395 396 397 398
 * @data: the smack mount options
 *
 * Returns 0 on success, an error code on failure
 */
399
static int smack_sb_kern_mount(struct super_block *sb, int flags, void *data)
400 401 402 403 404
{
	struct dentry *root = sb->s_root;
	struct inode *inode = root->d_inode;
	struct superblock_smack *sp = sb->s_security;
	struct inode_smack *isp;
405
	struct smack_known *skp;
406 407 408
	char *op;
	char *commap;
	char *nsp;
409
	int transmute = 0;
410
	int specified = 0;
411

412
	if (sp->smk_initialized)
413
		return 0;
414

415 416 417 418 419 420 421 422 423 424
	sp->smk_initialized = 1;

	for (op = data; op != NULL; op = commap) {
		commap = strchr(op, ',');
		if (commap != NULL)
			*commap++ = '\0';

		if (strncmp(op, SMK_FSHAT, strlen(SMK_FSHAT)) == 0) {
			op += strlen(SMK_FSHAT);
			nsp = smk_import(op, 0);
425
			if (nsp != NULL) {
426
				sp->smk_hat = nsp;
427 428
				specified = 1;
			}
429 430 431
		} else if (strncmp(op, SMK_FSFLOOR, strlen(SMK_FSFLOOR)) == 0) {
			op += strlen(SMK_FSFLOOR);
			nsp = smk_import(op, 0);
432
			if (nsp != NULL) {
433
				sp->smk_floor = nsp;
434 435
				specified = 1;
			}
436 437 438 439
		} else if (strncmp(op, SMK_FSDEFAULT,
				   strlen(SMK_FSDEFAULT)) == 0) {
			op += strlen(SMK_FSDEFAULT);
			nsp = smk_import(op, 0);
440
			if (nsp != NULL) {
441
				sp->smk_default = nsp;
442 443
				specified = 1;
			}
444 445 446
		} else if (strncmp(op, SMK_FSROOT, strlen(SMK_FSROOT)) == 0) {
			op += strlen(SMK_FSROOT);
			nsp = smk_import(op, 0);
447
			if (nsp != NULL) {
448
				sp->smk_root = nsp;
449 450
				specified = 1;
			}
451 452 453 454 455 456
		} else if (strncmp(op, SMK_FSTRANS, strlen(SMK_FSTRANS)) == 0) {
			op += strlen(SMK_FSTRANS);
			nsp = smk_import(op, 0);
			if (nsp != NULL) {
				sp->smk_root = nsp;
				transmute = 1;
457
				specified = 1;
458
			}
459 460 461
		}
	}

462 463 464 465 466 467 468 469 470 471 472 473 474
	if (!smack_privileged(CAP_MAC_ADMIN)) {
		/*
		 * Unprivileged mounts don't get to specify Smack values.
		 */
		if (specified)
			return -EPERM;
		/*
		 * Unprivileged mounts get root and default from the caller.
		 */
		skp = smk_of_current();
		sp->smk_root = skp->smk_known;
		sp->smk_default = skp->smk_known;
	}
475 476 477 478
	/*
	 * Initialize the root inode.
	 */
	isp = inode->i_security;
479 480 481 482 483
	if (isp == NULL) {
		isp = new_inode_smack(sp->smk_root);
		if (isp == NULL)
			return -ENOMEM;
		inode->i_security = isp;
484
	} else
485 486
		isp->smk_inode = sp->smk_root;

487 488 489
	if (transmute)
		isp->smk_flags |= SMK_INODE_TRANSMUTE;

490 491 492 493 494 495 496 497 498 499 500 501 502
	return 0;
}

/**
 * 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;
Etienne Basset's avatar
Etienne Basset committed
503 504 505
	int rc;
	struct smk_audit_info ad;

506
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Basset's avatar
Etienne Basset committed
507
	smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
508

Etienne Basset's avatar
Etienne Basset committed
509 510
	rc = smk_curacc(sbp->smk_floor, MAY_READ, &ad);
	return rc;
511 512
}

513 514 515 516
/*
 * BPRM hooks
 */

Casey Schaufler's avatar
Casey Schaufler committed
517 518 519 520
/**
 * smack_bprm_set_creds - set creds for exec
 * @bprm: the exec information
 *
521
 * Returns 0 if it gets a blob, -EPERM if exec forbidden and -ENOMEM otherwise
Casey Schaufler's avatar
Casey Schaufler committed
522
 */
523 524
static int smack_bprm_set_creds(struct linux_binprm *bprm)
{
Al Viro's avatar
Al Viro committed
525
	struct inode *inode = file_inode(bprm->file);
526
	struct task_smack *bsp = bprm->cred->security;
527 528 529 530 531 532 533 534 535 536
	struct inode_smack *isp;
	int rc;

	rc = cap_bprm_set_creds(bprm);
	if (rc != 0)
		return rc;

	if (bprm->cred_prepared)
		return 0;

537 538
	isp = inode->i_security;
	if (isp->smk_task == NULL || isp->smk_task == bsp->smk_task)
539 540
		return 0;

541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
	if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
		struct task_struct *tracer;
		rc = 0;

		rcu_read_lock();
		tracer = ptrace_parent(current);
		if (likely(tracer != NULL))
			rc = smk_ptrace_rule_check(tracer,
						   isp->smk_task->smk_known,
						   PTRACE_MODE_ATTACH,
						   __func__);
		rcu_read_unlock();

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

559 560
	bsp->smk_task = isp->smk_task;
	bprm->per_clear |= PER_CLEAR_ON_SETID;
561

562 563
	return 0;
}
564

565 566 567 568 569 570 571 572 573
/**
 * smack_bprm_committing_creds - Prepare to install the new credentials
 * from bprm.
 *
 * @bprm: binprm for exec
 */
static void smack_bprm_committing_creds(struct linux_binprm *bprm)
{
	struct task_smack *bsp = bprm->cred->security;
574

575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
	if (bsp->smk_task != bsp->smk_forked)
		current->pdeath_signal = 0;
}

/**
 * smack_bprm_secureexec - Return the decision to use secureexec.
 * @bprm: binprm for exec
 *
 * Returns 0 on success.
 */
static int smack_bprm_secureexec(struct linux_binprm *bprm)
{
	struct task_smack *tsp = current_security();
	int ret = cap_bprm_secureexec(bprm);

	if (!ret && (tsp->smk_task != tsp->smk_forked))
		ret = 1;

	return ret;
594 595
}

596 597 598 599 600 601
/*
 * Inode hooks
 */

/**
 * smack_inode_alloc_security - allocate an inode blob
602
 * @inode: the inode in need of a blob
603 604 605 606 607
 *
 * Returns 0 if it gets a blob, -ENOMEM otherwise
 */
static int smack_inode_alloc_security(struct inode *inode)
{
608 609 610
	struct smack_known *skp = smk_of_current();

	inode->i_security = new_inode_smack(skp->smk_known);
611 612 613 614 615 616 617
	if (inode->i_security == NULL)
		return -ENOMEM;
	return 0;
}

/**
 * smack_inode_free_security - free an inode blob
618
 * @inode: the inode with a blob
619 620 621 622 623 624 625 626 627 628 629 630 631
 *
 * Clears the blob pointer in inode
 */
static void smack_inode_free_security(struct inode *inode)
{
	kfree(inode->i_security);
	inode->i_security = NULL;
}

/**
 * smack_inode_init_security - copy out the smack from an inode
 * @inode: the inode
 * @dir: unused
632
 * @qstr: unused
633 634 635 636 637 638 639
 * @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,
640
				     const struct qstr *qstr, const char **name,
641
				     void **value, size_t *len)
642
{
Casey Schaufler's avatar
Casey Schaufler committed
643
	struct inode_smack *issp = inode->i_security;
644
	struct smack_known *skp = smk_of_current();
645
	char *isp = smk_of_inode(inode);
646
	char *dsp = smk_of_inode(dir);
647
	int may;
648

649 650
	if (name)
		*name = XATTR_SMACK_SUFFIX;
651 652

	if (value) {
653
		rcu_read_lock();
654
		may = smk_access_entry(skp->smk_known, dsp, &skp->smk_rules);
655
		rcu_read_unlock();
656 657 658 659 660

		/*
		 * If the access rule allows transmutation and
		 * the directory requests transmutation then
		 * by all means transmute.
Casey Schaufler's avatar
Casey Schaufler committed
661
		 * Mark the inode as changed.
662
		 */
663
		if (may > 0 && ((may & MAY_TRANSMUTE) != 0) &&
Casey Schaufler's avatar
Casey Schaufler committed
664
		    smk_inode_transmutable(dir)) {
665
			isp = dsp;
Casey Schaufler's avatar
Casey Schaufler committed
666 667
			issp->smk_flags |= SMK_INODE_CHANGED;
		}
668

669
		*value = kstrdup(isp, GFP_NOFS);
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
		if (*value == NULL)
			return -ENOMEM;
	}

	if (len)
		*len = strlen(isp) + 1;

	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)
{
	char *isp;
Etienne Basset's avatar
Etienne Basset committed
692 693 694
	struct smk_audit_info ad;
	int rc;

695
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Basset's avatar
Etienne Basset committed
696
	smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
697 698

	isp = smk_of_inode(old_dentry->d_inode);
Etienne Basset's avatar
Etienne Basset committed
699
	rc = smk_curacc(isp, MAY_WRITE, &ad);
700 701 702

	if (rc == 0 && new_dentry->d_inode != NULL) {
		isp = smk_of_inode(new_dentry->d_inode);
Etienne Basset's avatar
Etienne Basset committed
703 704
		smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
		rc = smk_curacc(isp, MAY_WRITE, &ad);
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
	}

	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)
{
	struct inode *ip = dentry->d_inode;
Etienne Basset's avatar
Etienne Basset committed
721
	struct smk_audit_info ad;
722 723
	int rc;

724
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Basset's avatar
Etienne Basset committed
725 726
	smk_ad_setfield_u_fs_path_dentry(&ad, dentry);

727 728 729
	/*
	 * You need write access to the thing you're unlinking
	 */
Etienne Basset's avatar
Etienne Basset committed
730 731
	rc = smk_curacc(smk_of_inode(ip), MAY_WRITE, &ad);
	if (rc == 0) {
732 733 734
		/*
		 * You also need write access to the containing directory
		 */
735
		smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Basset's avatar
Etienne Basset committed
736 737 738
		smk_ad_setfield_u_fs_inode(&ad, dir);
		rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
	}
739 740 741 742 743 744 745 746 747 748 749 750 751
	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)
{
Etienne Basset's avatar
Etienne Basset committed
752
	struct smk_audit_info ad;
753 754
	int rc;

755
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Basset's avatar
Etienne Basset committed
756 757
	smk_ad_setfield_u_fs_path_dentry(&ad, dentry);

758 759 760
	/*
	 * You need write access to the thing you're removing
	 */
Etienne Basset's avatar
Etienne Basset committed
761 762
	rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
	if (rc == 0) {
763 764 765
		/*
		 * You also need write access to the containing directory
		 */
766
		smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Basset's avatar
Etienne Basset committed
767 768 769
		smk_ad_setfield_u_fs_inode(&ad, dir);
		rc = smk_curacc(smk_of_inode(dir), MAY_WRITE, &ad);
	}
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792

	return rc;
}

/**
 * smack_inode_rename - Smack check on rename
 * @old_inode: the old directory
 * @old_dentry: unused
 * @new_inode: the new directory
 * @new_dentry: unused
 *
 * 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;
	char *isp;
Etienne Basset's avatar
Etienne Basset committed
793 794
	struct smk_audit_info ad;

795
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Basset's avatar
Etienne Basset committed
796
	smk_ad_setfield_u_fs_path_dentry(&ad, old_dentry);
797 798

	isp = smk_of_inode(old_dentry->d_inode);
Etienne Basset's avatar
Etienne Basset committed
799
	rc = smk_curacc(isp, MAY_READWRITE, &ad);
800 801 802

	if (rc == 0 && new_dentry->d_inode != NULL) {
		isp = smk_of_inode(new_dentry->d_inode);
Etienne Basset's avatar
Etienne Basset committed
803 804
		smk_ad_setfield_u_fs_path_dentry(&ad, new_dentry);
		rc = smk_curacc(isp, MAY_READWRITE, &ad);
805 806 807 808 809 810 811 812 813 814 815 816 817
	}
	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
 */
818
static int smack_inode_permission(struct inode *inode, int mask)
819
{
Etienne Basset's avatar
Etienne Basset committed
820
	struct smk_audit_info ad;
821
	int no_block = mask & MAY_NOT_BLOCK;
822 823

	mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
824 825 826 827 828
	/*
	 * No permission to check. Existence test. Yup, it's there.
	 */
	if (mask == 0)
		return 0;
829 830

	/* May be droppable after audit */
831
	if (no_block)
832
		return -ECHILD;
833
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_INODE);
Etienne Basset's avatar
Etienne Basset committed
834 835
	smk_ad_setfield_u_fs_inode(&ad, inode);
	return smk_curacc(smk_of_inode(inode), mask, &ad);
836 837 838 839 840 841 842 843 844 845 846
}

/**
 * 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)
{
Etienne Basset's avatar
Etienne Basset committed
847
	struct smk_audit_info ad;
848 849 850 851 852
	/*
	 * Need to allow for clearing the setuid bit.
	 */
	if (iattr->ia_valid & ATTR_FORCE)
		return 0;
853
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Basset's avatar
Etienne Basset committed
854
	smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
855

Etienne Basset's avatar
Etienne Basset committed
856
	return smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
857 858 859 860 861 862 863 864 865 866 867
}

/**
 * smack_inode_getattr - Smack check for getting attributes
 * @mnt: unused
 * @dentry: the object
 *
 * Returns 0 if access is permitted, an error code otherwise
 */
static int smack_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
{
Etienne Basset's avatar
Etienne Basset committed
868
	struct smk_audit_info ad;
869
	struct path path;
Etienne Basset's avatar
Etienne Basset committed
870

871 872
	path.dentry = dentry;
	path.mnt = mnt;
Etienne Basset's avatar
Etienne Basset committed
873

874
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
875
	smk_ad_setfield_u_fs_path(&ad, path);
Etienne Basset's avatar
Etienne Basset committed
876
	return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
877 878 879 880 881 882 883 884 885 886 887 888 889 890
}

/**
 * smack_inode_setxattr - Smack check for setting xattrs
 * @dentry: the object
 * @name: name of the attribute
 * @value: unused
 * @size: unused
 * @flags: unused
 *
 * This protects the Smack attribute explicitly.
 *
 * Returns 0 if access is permitted, an error code otherwise
 */
891 892
static int smack_inode_setxattr(struct dentry *dentry, const char *name,
				const void *value, size_t size, int flags)
893
{
Etienne Basset's avatar
Etienne Basset committed
894
	struct smk_audit_info ad;
895 896 897 898
	struct smack_known *skp;
	int check_priv = 0;
	int check_import = 0;
	int check_star = 0;
899
	int rc = 0;
900

901 902 903
	/*
	 * Check label validity here so import won't fail in post_setxattr
	 */
904 905
	if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
	    strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
906 907 908 909 910 911 912 913
	    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;
914
	} else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
915
		check_priv = 1;
916 917 918
		if (size != TRANS_TRUE_SIZE ||
		    strncmp(value, TRANS_TRUE, TRANS_TRUE_SIZE) != 0)
			rc = -EINVAL;
919 920 921
	} else
		rc = cap_inode_setxattr(dentry, name, value, size, flags);

922 923 924 925 926 927 928 929 930 931
	if (check_priv && !smack_privileged(CAP_MAC_ADMIN))
		rc = -EPERM;

	if (rc == 0 && check_import) {
		skp = smk_import_entry(value, size);
		if (skp == NULL || (check_star &&
		    (skp == &smack_known_star || skp == &smack_known_web)))
			rc = -EINVAL;
	}

932
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Basset's avatar
Etienne Basset committed
933 934
	smk_ad_setfield_u_fs_path_dentry(&ad, dentry);

935
	if (rc == 0)
Etienne Basset's avatar
Etienne Basset committed
936
		rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
937 938

	return rc;
939 940 941 942 943 944 945 946 947 948 949 950 951
}

/**
 * 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.
 */
952 953
static void smack_inode_post_setxattr(struct dentry *dentry, const char *name,
				      const void *value, size_t size, int flags)
954
{
955
	struct smack_known *skp;
956
	struct inode_smack *isp = dentry->d_inode->i_security;
957

958 959 960 961 962
	if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0) {
		isp->smk_flags |= SMK_INODE_TRANSMUTE;
		return;
	}

963
	if (strcmp(name, XATTR_NAME_SMACK) == 0) {
964
		skp = smk_import_entry(value, size);
965 966
		if (skp != NULL)
			isp->smk_inode = skp->smk_known;
967 968
		else
			isp->smk_inode = smack_known_invalid.smk_known;
969
	} else if (strcmp(name, XATTR_NAME_SMACKEXEC) == 0) {
970
		skp = smk_import_entry(value, size);
971 972
		if (skp != NULL)
			isp->smk_task = skp;
973
		else
974
			isp->smk_task = &smack_known_invalid;
975
	} else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
976
		skp = smk_import_entry(value, size);
977 978
		if (skp != NULL)
			isp->smk_mmap = skp;
979
		else
980 981
			isp->smk_mmap = &smack_known_invalid;
	}
982 983 984 985

	return;
}

Casey Schaufler's avatar
Casey Schaufler committed
986
/**
987 988 989 990 991 992
 * smack_inode_getxattr - Smack check on getxattr
 * @dentry: the object
 * @name: unused
 *
 * Returns 0 if access is permitted, an error code otherwise
 */
993
static int smack_inode_getxattr(struct dentry *dentry, const char *name)
994
{
Etienne Basset's avatar
Etienne Basset committed
995 996
	struct smk_audit_info ad;

997
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Basset's avatar
Etienne Basset committed
998 999 1000
	smk_ad_setfield_u_fs_path_dentry(&ad, dentry);

	return smk_curacc(smk_of_inode(dentry->d_inode), MAY_READ, &ad);
1001 1002
}

Casey Schaufler's avatar
Casey Schaufler committed
1003
/**
1004 1005 1006 1007 1008 1009 1010 1011
 * 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
 */
1012
static int smack_inode_removexattr(struct dentry *dentry, const char *name)
1013
{
1014
	struct inode_smack *isp;
Etienne Basset's avatar
Etienne Basset committed
1015
	struct smk_audit_info ad;
1016
	int rc = 0;
1017

1018 1019
	if (strcmp(name, XATTR_NAME_SMACK) == 0 ||
	    strcmp(name, XATTR_NAME_SMACKIPIN) == 0 ||
1020
	    strcmp(name, XATTR_NAME_SMACKIPOUT) == 0 ||
1021
	    strcmp(name, XATTR_NAME_SMACKEXEC) == 0 ||
1022
	    strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0 ||
Pankaj Kumar's avatar
Pankaj Kumar committed
1023
	    strcmp(name, XATTR_NAME_SMACKMMAP) == 0) {
1024
		if (!smack_privileged(CAP_MAC_ADMIN))
1025 1026 1027 1028
			rc = -EPERM;
	} else
		rc = cap_inode_removexattr(dentry, name);

1029 1030 1031
	if (rc != 0)
		return rc;

1032
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_DENTRY);
Etienne Basset's avatar
Etienne Basset committed
1033
	smk_ad_setfield_u_fs_path_dentry(&ad, dentry);
1034

1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
	rc = smk_curacc(smk_of_inode(dentry->d_inode), MAY_WRITE, &ad);
	if (rc != 0)
		return rc;

	isp = dentry->d_inode->i_security;
	/*
	 * Don't do anything special for these.
	 *	XATTR_NAME_SMACKIPIN
	 *	XATTR_NAME_SMACKIPOUT
	 *	XATTR_NAME_SMACKEXEC
	 */
	if (strcmp(name, XATTR_NAME_SMACK) == 0)
1047
		isp->smk_task = NULL;
1048
	else if (strcmp(name, XATTR_NAME_SMACKMMAP) == 0)
1049
		isp->smk_mmap = NULL;
1050 1051
	else if (strcmp(name, XATTR_NAME_SMACKTRANSMUTE) == 0)
		isp->smk_flags &= ~SMK_INODE_TRANSMUTE;
1052

1053
	return 0;
1054 1055 1056 1057 1058 1059 1060
}

/**
 * smack_inode_getsecurity - get smack xattrs
 * @inode: the object
 * @name: attribute name
 * @buffer: where to put the result
1061
 * @alloc: unused
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
 *
 * Returns the size of the attribute or an error code
 */
static int smack_inode_getsecurity(const struct inode *inode,
				   const char *name, void **buffer,
				   bool alloc)
{
	struct socket_smack *ssp;
	struct socket *sock;
	struct super_block *sbp;
	struct inode *ip = (struct inode *)inode;
	char *isp;
	int ilen;
	int rc = 0;

	if (strcmp(name, XATTR_SMACK_SUFFIX) == 0) {
		isp = smk_of_inode(inode);
		ilen = strlen(isp) + 1;
		*buffer = isp;
		return ilen;
	}

	/*
	 * The rest of the Smack xattrs are only on sockets.
	 */
	sbp = ip->i_sb;
	if (sbp->s_magic != SOCKFS_MAGIC)
		return -EOPNOTSUPP;

	sock = SOCKET_I(ip);
1092
	if (sock == NULL || sock->sk == NULL)
1093 1094 1095 1096 1097
		return -EOPNOTSUPP;

	ssp = sock->sk->sk_security;

	if (strcmp(name, XATTR_SMACK_IPIN) == 0)
1098
		isp = ssp->smk_in->smk_known;
1099
	else if (strcmp(name, XATTR_SMACK_IPOUT) == 0)
1100
		isp = ssp->smk_out->smk_known;
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
	else
		return -EOPNOTSUPP;

	ilen = strlen(isp) + 1;
	if (rc == 0) {
		*buffer = isp;
		rc = ilen;
	}

	return rc;
}


/**
 * smack_inode_listsecurity - list the Smack attributes
 * @inode: the object
 * @buffer: where they go
 * @buffer_size: size of buffer
 *
 * Returns 0 on success, -EINVAL otherwise
 */
static int smack_inode_listsecurity(struct inode *inode, char *buffer,
				    size_t buffer_size)
{
	int len = strlen(XATTR_NAME_SMACK);

	if (buffer != NULL && len <= buffer_size) {
		memcpy(buffer, XATTR_NAME_SMACK, len);
		return len;
	}
	return -EINVAL;
}

1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
/**
 * smack_inode_getsecid - Extract inode's security id
 * @inode: inode to extract the info from
 * @secid: where result will be saved
 */
static void smack_inode_getsecid(const struct inode *inode, u32 *secid)
{
	struct inode_smack *isp = inode->i_security;

	*secid = smack_to_secid(isp->smk_inode);
}

1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
/*
 * File Hooks
 */

/**
 * smack_file_permission - Smack check on file operations
 * @file: unused
 * @mask: unused
 *
 * Returns 0
 *
 * 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.
 */
static int smack_file_permission(struct file *file, int mask)
{
	return 0;
}

/**
 * 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.
 *
 * Returns 0
 */
static int smack_file_alloc_security(struct file *file)
{
1180 1181 1182
	struct smack_known *skp = smk_of_current();

	file->f_security = skp->smk_known;
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
	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;
Etienne Basset's avatar
Etienne Basset committed
1212 1213
	struct smk_audit_info ad;

1214
	smk_ad_init(&ad, __func__, LSM_AUDIT_DATA_PATH);
Etienne Basset's avatar
Etienne Basset committed
1215
	smk_ad_setfield_u_fs_path(&ad, file->f_path);
1216 1217

	if (_IOC_DIR(cmd) & _IOC_WRITE)
Etienne Basset's avatar
Etienne Basset committed
1218
		rc = smk_curacc(file->f_security, MAY_WRITE, &ad);
1219 1220

	if (rc == 0 && (_IOC_DIR(cmd) & _IOC_READ))
Etienne Basset's avatar
Etienne Basset committed
1221
		rc = smk_curacc(file->f_security, MAY_READ, &ad);
1222 1223 1224 1225 1226 1227 1228

	return rc;
}

/**
 * smack_file_lock - Smack check on file locking
 * @file: the object
1229
 * @cmd: unused
1230
 *
1231
 * Returns 0 if current has lock access, error code otherwise