procattr.c 3.7 KB
Newer Older
J
John Johansen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * AppArmor security module
 *
 * This file contains AppArmor /proc/<pid>/attr/ interface functions
 *
 * 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.
 */

#include "include/apparmor.h"
16
#include "include/cred.h"
J
John Johansen 已提交
17
#include "include/policy.h"
18
#include "include/policy_ns.h"
J
John Johansen 已提交
19
#include "include/domain.h"
20
#include "include/procattr.h"
J
John Johansen 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36


/**
 * aa_getprocattr - Return the profile information for @profile
 * @profile: the profile to print profile info about  (NOT NULL)
 * @string: Returns - string containing the profile info (NOT NULL)
 *
 * Returns: length of @string on success else error on failure
 *
 * Requires: profile != NULL
 *
 * Creates a string containing the namespace_name://profile_name for
 * @profile.
 *
 * Returns: size of string placed in @string else error code on failure
 */
37
int aa_getprocattr(struct aa_label *label, char **string)
J
John Johansen 已提交
38
{
39
	struct aa_ns *ns = labels_ns(label);
40
	struct aa_ns *current_ns = aa_get_current_ns();
41
	int len;
J
John Johansen 已提交
42

43 44
	if (!aa_ns_visible(current_ns, ns, true)) {
		aa_put_ns(current_ns);
J
John Johansen 已提交
45
		return -EACCES;
46
	}
J
John Johansen 已提交
47

48 49 50 51
	len = aa_label_snxprint(NULL, 0, current_ns, label,
				FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
				FLAG_HIDDEN_UNCONFINED);
	AA_BUG(len < 0);
J
John Johansen 已提交
52

53 54 55
	*string = kmalloc(len + 2, GFP_KERNEL);
	if (!*string) {
		aa_put_ns(current_ns);
J
John Johansen 已提交
56
		return -ENOMEM;
57
	}
J
John Johansen 已提交
58

59 60 61 62 63 64
	len = aa_label_snxprint(*string, len + 2, current_ns, label,
				FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
				FLAG_HIDDEN_UNCONFINED);
	if (len < 0) {
		aa_put_ns(current_ns);
		return len;
J
John Johansen 已提交
65 66
	}

67 68 69 70 71
	(*string)[len] = '\n';
	(*string)[len + 1] = 0;

	aa_put_ns(current_ns);
	return len + 1;
J
John Johansen 已提交
72 73 74 75 76 77 78 79 80 81
}

/**
 * split_token_from_name - separate a string of form  <token>^<name>
 * @op: operation being checked
 * @args: string to parse  (NOT NULL)
 * @token: stores returned parsed token value  (NOT NULL)
 *
 * Returns: start position of name after token else NULL on failure
 */
82
static char *split_token_from_name(const char *op, char *args, u64 *token)
J
John Johansen 已提交
83 84 85 86 87
{
	char *name;

	*token = simple_strtoull(args, &name, 16);
	if ((name == args) || *name != '^') {
88
		AA_ERROR("%s: Invalid input '%s'", op, args);
J
John Johansen 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101
		return ERR_PTR(-EINVAL);
	}

	name++;			/* skip ^ */
	if (!*name)
		name = NULL;
	return name;
}

/**
 * aa_setprocattr_chagnehat - handle procattr interface to change_hat
 * @args: args received from writing to /proc/<pid>/attr/current (NOT NULL)
 * @size: size of the args
102
 * @flags: set of flags governing behavior
J
John Johansen 已提交
103 104 105
 *
 * Returns: %0 or error code if change_hat fails
 */
106
int aa_setprocattr_changehat(char *args, size_t size, int flags)
J
John Johansen 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
{
	char *hat;
	u64 token;
	const char *hats[16];		/* current hard limit on # of names */
	int count = 0;

	hat = split_token_from_name(OP_CHANGE_HAT, args, &token);
	if (IS_ERR(hat))
		return PTR_ERR(hat);

	if (!hat && !token) {
		AA_ERROR("change_hat: Invalid input, NULL hat and NULL magic");
		return -EINVAL;
	}

	if (hat) {
		/* set up hat name vector, args guaranteed null terminated
		 * at args[size] by setprocattr.
		 *
		 * If there are multiple hat names in the buffer each is
		 * separated by a \0.  Ie. userspace writes them pre tokenized
		 */
		char *end = args + size;
		for (count = 0; (hat < end) && count < 16; ++count) {
			char *next = hat + strlen(hat) + 1;
			hats[count] = hat;
133 134
			AA_DEBUG("%s: (pid %d) Magic 0x%llx count %d hat '%s'\n"
				 , __func__, current->pid, token, count, hat);
J
John Johansen 已提交
135 136
			hat = next;
		}
137 138 139
	} else
		AA_DEBUG("%s: (pid %d) Magic 0x%llx count %d Hat '%s'\n",
			 __func__, current->pid, token, count, "<NULL>");
J
John Johansen 已提交
140

141
	return aa_change_hat(hats, count, token, flags);
J
John Johansen 已提交
142
}