file.c 15.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * AppArmor security module
 *
 * This file contains AppArmor mediation of files
 *
 * Copyright (C) 1998-2008 Novell/SUSE
 * Copyright 2009-2010 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, version 2 of the
 * License.
 */

15 16 17 18
#include <linux/tty.h>
#include <linux/fdtable.h>
#include <linux/file.h>

19 20
#include "include/apparmor.h"
#include "include/audit.h"
21
#include "include/context.h"
22 23 24 25
#include "include/file.h"
#include "include/match.h"
#include "include/path.h"
#include "include/policy.h"
26
#include "include/label.h"
27

28 29 30 31 32 33 34 35 36 37 38
static u32 map_mask_to_chr_mask(u32 mask)
{
	u32 m = mask & PERMS_CHRS_MASK;

	if (mask & AA_MAY_GETATTR)
		m |= MAY_READ;
	if (mask & (AA_MAY_SETATTR | AA_MAY_CHMOD | AA_MAY_CHOWN))
		m |= MAY_WRITE;

	return m;
}
39 40 41 42 43 44 45 46 47 48

/**
 * audit_file_mask - convert mask to permission string
 * @buffer: buffer to write string to (NOT NULL)
 * @mask: permission mask to convert
 */
static void audit_file_mask(struct audit_buffer *ab, u32 mask)
{
	char str[10];

49
	aa_perm_mask_to_str(str, aa_file_perm_chrs, map_mask_to_chr_mask(mask));
50 51 52 53 54 55 56 57 58 59 60
	audit_log_string(ab, str);
}

/**
 * file_audit_cb - call back for file specific audit fields
 * @ab: audit_buffer  (NOT NULL)
 * @va: audit struct to audit values of  (NOT NULL)
 */
static void file_audit_cb(struct audit_buffer *ab, void *va)
{
	struct common_audit_data *sa = va;
61
	kuid_t fsuid = current_fsuid();
62

63
	if (aad(sa)->request & AA_AUDIT_FILE_MASK) {
64
		audit_log_format(ab, " requested_mask=");
65
		audit_file_mask(ab, aad(sa)->request);
66
	}
67
	if (aad(sa)->denied & AA_AUDIT_FILE_MASK) {
68
		audit_log_format(ab, " denied_mask=");
69
		audit_file_mask(ab, aad(sa)->denied);
70
	}
71
	if (aad(sa)->request & AA_AUDIT_FILE_MASK) {
72 73 74
		audit_log_format(ab, " fsuid=%d",
				 from_kuid(&init_user_ns, fsuid));
		audit_log_format(ab, " ouid=%d",
75
				 from_kuid(&init_user_ns, aad(sa)->fs.ouid));
76 77
	}

78
	if (aad(sa)->fs.target) {
79
		audit_log_format(ab, " target=");
80
		audit_log_untrustedstring(ab, aad(sa)->fs.target);
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
	}
}

/**
 * aa_audit_file - handle the auditing of file operations
 * @profile: the profile being enforced  (NOT NULL)
 * @perms: the permissions computed for the request (NOT NULL)
 * @gfp: allocation flags
 * @op: operation being mediated
 * @request: permissions requested
 * @name: name of object being mediated (MAYBE NULL)
 * @target: name of target (MAYBE NULL)
 * @ouid: object uid
 * @info: extra information message (MAYBE NULL)
 * @error: 0 if operation allowed else failure error code
 *
 * Returns: %0 or error on failure
 */
99
int aa_audit_file(struct aa_profile *profile, struct aa_perms *perms,
100
		  const char *op, u32 request, const char *name,
101
		  const char *target, kuid_t ouid, const char *info, int error)
102 103
{
	int type = AUDIT_APPARMOR_AUTO;
104 105 106
	DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_TASK, op);

	sa.u.tsk = NULL;
107
	aad(&sa)->request = request;
108 109 110 111 112
	aad(&sa)->name = name;
	aad(&sa)->fs.target = target;
	aad(&sa)->fs.ouid = ouid;
	aad(&sa)->info = info;
	aad(&sa)->error = error;
113
	sa.u.tsk = NULL;
114 115

	if (likely(!aad(&sa)->error)) {
116 117 118 119 120 121
		u32 mask = perms->audit;

		if (unlikely(AUDIT_MODE(profile) == AUDIT_ALL))
			mask = 0xffff;

		/* mask off perms that are not being force audited */
122
		aad(&sa)->request &= mask;
123

124
		if (likely(!aad(&sa)->request))
125 126 127 128
			return 0;
		type = AUDIT_APPARMOR_AUDIT;
	} else {
		/* only report permissions that were denied */
129 130
		aad(&sa)->request = aad(&sa)->request & ~perms->allow;
		AA_BUG(!aad(&sa)->request);
131

132
		if (aad(&sa)->request & perms->kill)
133 134 135
			type = AUDIT_APPARMOR_KILL;

		/* quiet known rejects, assumes quiet and kill do not overlap */
136
		if ((aad(&sa)->request & perms->quiet) &&
137 138
		    AUDIT_MODE(profile) != AUDIT_NOQUIET &&
		    AUDIT_MODE(profile) != AUDIT_ALL)
139
			aad(&sa)->request &= ~perms->quiet;
140

141
		if (!aad(&sa)->request)
142
			return COMPLAIN_MODE(profile) ? 0 : aad(&sa)->error;
143 144
	}

145
	aad(&sa)->denied = aad(&sa)->request & ~perms->allow;
146
	return aa_audit(type, profile, &sa, file_audit_cb);
147 148 149 150 151 152 153 154 155 156 157 158
}

/**
 * map_old_perms - map old file perms layout to the new layout
 * @old: permission set in old mapping
 *
 * Returns: new permission mapping
 */
static u32 map_old_perms(u32 old)
{
	u32 new = old & 0xf;
	if (old & MAY_READ)
159
		new |= AA_MAY_GETATTR | AA_MAY_OPEN;
160
	if (old & MAY_WRITE)
161 162
		new |= AA_MAY_SETATTR | AA_MAY_CREATE | AA_MAY_DELETE |
		       AA_MAY_CHMOD | AA_MAY_CHOWN | AA_MAY_OPEN;
163 164 165 166 167 168 169 170 171 172 173 174 175 176
	if (old & 0x10)
		new |= AA_MAY_LINK;
	/* the old mapping lock and link_subset flags where overlaid
	 * and use was determined by part of a pair that they were in
	 */
	if (old & 0x20)
		new |= AA_MAY_LOCK | AA_LINK_SUBSET;
	if (old & 0x40)	/* AA_EXEC_MMAP */
		new |= AA_EXEC_MMAP;

	return new;
}

/**
177
 * aa_compute_fperms - convert dfa compressed perms to internal perms
178 179 180 181 182 183 184 185 186
 * @dfa: dfa to compute perms for   (NOT NULL)
 * @state: state in dfa
 * @cond:  conditions to consider  (NOT NULL)
 *
 * TODO: convert from dfa + state to permission entry, do computation conversion
 *       at load time.
 *
 * Returns: computed permission set
 */
187 188
struct aa_perms aa_compute_fperms(struct aa_dfa *dfa, unsigned int state,
				  struct path_cond *cond)
189
{
190
	struct aa_perms perms;
191 192 193 194 195 196

	/* FIXME: change over to new dfa format
	 * currently file perms are encoded in the dfa, new format
	 * splits the permissions from the dfa.  This mapping can be
	 * done at profile load
	 */
197 198 199 200 201
	perms.deny = 0;
	perms.kill = perms.stop = 0;
	perms.complain = perms.cond = 0;
	perms.hide = 0;
	perms.prompt = 0;
202

203
	if (uid_eq(current_fsuid(), cond->uid)) {
204 205 206 207 208 209 210 211 212 213
		perms.allow = map_old_perms(dfa_user_allow(dfa, state));
		perms.audit = map_old_perms(dfa_user_audit(dfa, state));
		perms.quiet = map_old_perms(dfa_user_quiet(dfa, state));
		perms.xindex = dfa_user_xindex(dfa, state);
	} else {
		perms.allow = map_old_perms(dfa_other_allow(dfa, state));
		perms.audit = map_old_perms(dfa_other_audit(dfa, state));
		perms.quiet = map_old_perms(dfa_other_quiet(dfa, state));
		perms.xindex = dfa_other_xindex(dfa, state);
	}
214
	perms.allow |= AA_MAY_GETATTR;
215 216 217 218

	/* change_profile wasn't determined by ownership in old mapping */
	if (ACCEPT_TABLE(dfa)[state] & 0x80000000)
		perms.allow |= AA_MAY_CHANGE_PROFILE;
219 220
	if (ACCEPT_TABLE(dfa)[state] & 0x40000000)
		perms.allow |= AA_MAY_ONEXEC;
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236

	return perms;
}

/**
 * aa_str_perms - find permission that match @name
 * @dfa: to match against  (MAYBE NULL)
 * @state: state to start matching in
 * @name: string to match against dfa  (NOT NULL)
 * @cond: conditions to consider for permission set computation  (NOT NULL)
 * @perms: Returns - the permissions found when matching @name
 *
 * Returns: the final state in @dfa when beginning @start and walking @name
 */
unsigned int aa_str_perms(struct aa_dfa *dfa, unsigned int start,
			  const char *name, struct path_cond *cond,
237
			  struct aa_perms *perms)
238 239 240
{
	unsigned int state;
	state = aa_dfa_match(dfa, start, name);
241
	*perms = aa_compute_fperms(dfa, state, cond);
242 243 244 245 246 247 248 249 250 251 252 253

	return state;
}

/**
 * is_deleted - test if a file has been completely unlinked
 * @dentry: dentry of file to test for deletion  (NOT NULL)
 *
 * Returns: %1 if deleted else %0
 */
static inline bool is_deleted(struct dentry *dentry)
{
254
	if (d_unlinked(dentry) && d_backing_inode(dentry)->i_nlink == 0)
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
		return 1;
	return 0;
}

/**
 * aa_path_perm - do permissions check & audit for @path
 * @op: operation being checked
 * @profile: profile being enforced  (NOT NULL)
 * @path: path to check permissions of  (NOT NULL)
 * @flags: any additional path flags beyond what the profile specifies
 * @request: requested permissions
 * @cond: conditional info for this request  (NOT NULL)
 *
 * Returns: %0 else error if access denied or other error
 */
270 271 272
int aa_path_perm(const char *op, struct aa_profile *profile,
		 const struct path *path, int flags, u32 request,
		 struct path_cond *cond)
273 274
{
	char *buffer = NULL;
275
	struct aa_perms perms = {};
276 277 278 279
	const char *name, *info = NULL;
	int error;

	flags |= profile->path_flags | (S_ISDIR(cond->mode) ? PATH_IS_DIR : 0);
280 281
	get_buffers(buffer);
	error = aa_path_name(path, flags, buffer, &name, &info,
282
			     profile->disconnected);
283 284 285 286 287 288
	if (error) {
		if (error == -ENOENT && is_deleted(path->dentry)) {
			/* Access to open files that are deleted are
			 * give a pass (implicit delegation)
			 */
			error = 0;
289
			info = NULL;
290
			perms.allow = request;
291
		}
292 293 294 295 296 297
	} else {
		aa_str_perms(profile->file.dfa, profile->file.start, name, cond,
			     &perms);
		if (request & ~perms.allow)
			error = -EACCES;
	}
298 299
	error = aa_audit_file(profile, &perms, op, request, name, NULL,
			      cond->uid, info, error);
300
	put_buffers(buffer);
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

	return error;
}

/**
 * xindex_is_subset - helper for aa_path_link
 * @link: link permission set
 * @target: target permission set
 *
 * test target x permissions are equal OR a subset of link x permissions
 * this is done as part of the subset test, where a hardlink must have
 * a subset of permissions that the target has.
 *
 * Returns: %1 if subset else %0
 */
static inline bool xindex_is_subset(u32 link, u32 target)
{
	if (((link & ~AA_X_UNSAFE) != (target & ~AA_X_UNSAFE)) ||
	    ((link & AA_X_UNSAFE) && !(target & AA_X_UNSAFE)))
		return 0;

	return 1;
}

/**
 * aa_path_link - Handle hard link permission check
 * @profile: the profile being enforced  (NOT NULL)
 * @old_dentry: the target dentry  (NOT NULL)
 * @new_dir: directory the new link will be created in  (NOT NULL)
 * @new_dentry: the link being created  (NOT NULL)
 *
 * Handle the permission test for a link & target pair.  Permission
 * is encoded as a pair where the link permission is determined
 * first, and if allowed, the target is tested.  The target test
 * is done from the point of the link match (not start of DFA)
 * making the target permission dependent on the link permission match.
 *
 * The subset test if required forces that permissions granted
 * on link are a subset of the permission granted to target.
 *
 * Returns: %0 if allowed else error
 */
int aa_path_link(struct aa_profile *profile, struct dentry *old_dentry,
A
Al Viro 已提交
344
		 const struct path *new_dir, struct dentry *new_dentry)
345
{
346 347
	struct path link = { .mnt = new_dir->mnt, .dentry = new_dentry };
	struct path target = { .mnt = new_dir->mnt, .dentry = old_dentry };
348
	struct path_cond cond = {
349 350
		d_backing_inode(old_dentry)->i_uid,
		d_backing_inode(old_dentry)->i_mode
351 352 353
	};
	char *buffer = NULL, *buffer2 = NULL;
	const char *lname, *tname = NULL, *info = NULL;
354
	struct aa_perms lperms, perms;
355 356 357 358
	u32 request = AA_MAY_LINK;
	unsigned int state;
	int error;

359
	get_buffers(buffer, buffer2);
360 361 362
	lperms = nullperms;

	/* buffer freed below, lname is pointer in buffer */
363
	error = aa_path_name(&link, profile->path_flags, buffer, &lname,
364
			     &info, profile->disconnected);
365 366 367 368
	if (error)
		goto audit;

	/* buffer2 freed below, tname is pointer in buffer2 */
369
	error = aa_path_name(&target, profile->path_flags, buffer2, &tname,
370
			     &info, profile->disconnected);
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
	if (error)
		goto audit;

	error = -EACCES;
	/* aa_str_perms - handles the case of the dfa being NULL */
	state = aa_str_perms(profile->file.dfa, profile->file.start, lname,
			     &cond, &lperms);

	if (!(lperms.allow & AA_MAY_LINK))
		goto audit;

	/* test to see if target can be paired with link */
	state = aa_dfa_null_transition(profile->file.dfa, state);
	aa_str_perms(profile->file.dfa, state, tname, &cond, &perms);

	/* force audit/quiet masks for link are stored in the second entry
	 * in the link pair.
	 */
	lperms.audit = perms.audit;
	lperms.quiet = perms.quiet;
	lperms.kill = perms.kill;

	if (!(perms.allow & AA_MAY_LINK)) {
		info = "target restricted";
		goto audit;
	}

	/* done if link subset test is not required */
	if (!(perms.allow & AA_LINK_SUBSET))
		goto done_tests;

	/* Do link perm subset test requiring allowed permission on link are a
	 * subset of the allowed permissions on target.
	 */
	aa_str_perms(profile->file.dfa, profile->file.start, tname, &cond,
		     &perms);

	/* AA_MAY_LINK is not considered in the subset test */
	request = lperms.allow & ~AA_MAY_LINK;
	lperms.allow &= perms.allow | AA_MAY_LINK;

	request |= AA_AUDIT_FILE_MASK & (lperms.allow & ~perms.allow);
	if (request & ~lperms.allow) {
		goto audit;
	} else if ((lperms.allow & MAY_EXEC) &&
		   !xindex_is_subset(lperms.xindex, perms.xindex)) {
		lperms.allow &= ~MAY_EXEC;
		request |= MAY_EXEC;
		info = "link not subset of target";
		goto audit;
	}

done_tests:
	error = 0;

audit:
427
	error = aa_audit_file(profile, &lperms, OP_LINK, request,
428
			      lname, tname, cond.uid, info, error);
429
	put_buffers(buffer, buffer2);
430 431 432 433 434 435 436

	return error;
}

/**
 * aa_file_perm - do permission revalidation check & audit for @file
 * @op: operation being checked
437
 * @label: label being enforced   (NOT NULL)
438 439 440 441 442
 * @file: file to revalidate access permissions on  (NOT NULL)
 * @request: requested permissions
 *
 * Returns: %0 if access allowed else error
 */
443
int aa_file_perm(const char *op, struct aa_label *label, struct file *file,
444 445 446
		 u32 request)
{
	struct path_cond cond = {
A
Al Viro 已提交
447 448
		.uid = file_inode(file)->i_uid,
		.mode = file_inode(file)->i_mode
449
	};
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
	struct aa_file_ctx *fctx;
	struct aa_label *flabel;
	u32 denied;
	int error = 0;

	AA_BUG(!label);
	AA_BUG(!file);

	fctx = file_ctx(file);

	rcu_read_lock();
	flabel  = rcu_dereference(fctx->label);
	AA_BUG(!flabel);

	/* revalidate access, if task is unconfined, or the cached cred
	 * doesn't match or if the request is for more permissions than
	 * was granted.
	 *
	 * Note: the test for !unconfined(flabel) is to handle file
	 *       delegation from unconfined tasks
	 */
	denied = request & ~fctx->allow;
	if (unconfined(label) || unconfined(flabel) ||
	    (!denied && aa_label_is_subset(flabel, label)))
		goto done;

	/* TODO: label cross check */

	if (file->f_path.mnt && path_mediated_fs(file->f_path.dentry))
		error = aa_path_perm(op, labels_profile(label), &file->f_path,
				     PATH_DELEGATE_DELETED, request, &cond);
481

482 483 484 485
done:
	rcu_read_unlock();

	return error;
486
}
487

488
static void revalidate_tty(struct aa_label *label)
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
{
	struct tty_struct *tty;
	int drop_tty = 0;

	tty = get_current_tty();
	if (!tty)
		return;

	spin_lock(&tty->files_lock);
	if (!list_empty(&tty->tty_files)) {
		struct tty_file_private *file_priv;
		struct file *file;
		/* TODO: Revalidate access to controlling tty. */
		file_priv = list_first_entry(&tty->tty_files,
					     struct tty_file_private, list);
		file = file_priv->file;

506
		if (aa_file_perm(OP_INHERIT, label, file, MAY_READ | MAY_WRITE))
507 508 509 510 511 512 513 514 515 516 517
			drop_tty = 1;
	}
	spin_unlock(&tty->files_lock);
	tty_kref_put(tty);

	if (drop_tty)
		no_tty();
}

static int match_file(const void *p, struct file *file, unsigned int fd)
{
518
	struct aa_label *label = (struct aa_label *)p;
519

520
	if (aa_file_perm(OP_INHERIT, label, file, aa_map_file_to_perms(file)))
521 522 523 524 525 526 527 528
		return fd + 1;
	return 0;
}


/* based on selinux's flush_unauthorized_files */
void aa_inherit_files(const struct cred *cred, struct files_struct *files)
{
529
	struct aa_label *label = aa_get_newest_cred_label(cred);
530 531 532
	struct file *devnull = NULL;
	unsigned int n;

533
	revalidate_tty(label);
534 535

	/* Revalidate access to inherited open files. */
536
	n = iterate_fd(files, 0, match_file, label);
537 538 539 540 541 542 543 544 545
	if (!n) /* none found? */
		goto out;

	devnull = dentry_open(&aa_null, O_RDWR, cred);
	if (IS_ERR(devnull))
		devnull = NULL;
	/* replace all the matching ones with this */
	do {
		replace_fd(n - 1, devnull, 0);
546
	} while ((n = iterate_fd(files, n, match_file, label)) != 0);
547 548 549
	if (devnull)
		fput(devnull);
out:
550
	aa_put_label(label);
551
}