xref: /linux/security/apparmor/procattr.c (revision fea23bf73f0cae8ccb1d0684e4a3003874771f41)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * AppArmor security module
4  *
5  * This file contains AppArmor /proc/<pid>/attr/ interface functions
6  *
7  * Copyright (C) 1998-2008 Novell/SUSE
8  * Copyright 2009-2010 Canonical Ltd.
9  */
10 
11 #include "include/apparmor.h"
12 #include "include/cred.h"
13 #include "include/policy.h"
14 #include "include/policy_ns.h"
15 #include "include/domain.h"
16 #include "include/procattr.h"
17 
18 
19 /**
20  * aa_getprocattr - Return the label information for @label
21  * @label: the label to print label info about  (NOT NULL)
22  * @string: Returns - string containing the label info (NOT NULL)
23  * @newline: indicates that a newline should be added
24  *
25  * Requires: label != NULL && string != NULL
26  *
27  * Creates a string containing the label information for @label.
28  *
29  * Returns: size of string placed in @string else error code on failure
30  */
31 int aa_getprocattr(struct aa_label *label, char **string, bool newline)
32 {
33 	struct aa_ns *ns = labels_ns(label);
34 	struct aa_ns *current_ns = aa_get_current_ns();
35 	int len;
36 
37 	if (!aa_ns_visible(current_ns, ns, true)) {
38 		aa_put_ns(current_ns);
39 		return -EACCES;
40 	}
41 
42 	len = aa_label_snxprint(NULL, 0, current_ns, label,
43 				FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
44 				FLAG_HIDDEN_UNCONFINED);
45 	AA_BUG(len < 0);
46 
47 	*string = kmalloc(len + 2, GFP_KERNEL);
48 	if (!*string) {
49 		aa_put_ns(current_ns);
50 		return -ENOMEM;
51 	}
52 
53 	len = aa_label_snxprint(*string, len + 2, current_ns, label,
54 				FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
55 				FLAG_HIDDEN_UNCONFINED);
56 	if (len < 0) {
57 		kfree(*string);
58 		*string = NULL;
59 		aa_put_ns(current_ns);
60 		return len;
61 	}
62 
63 	if (newline)
64 		(*string)[len++] = '\n';
65 	(*string)[len] = 0;
66 
67 	aa_put_ns(current_ns);
68 	return len;
69 }
70 
71 /**
72  * split_token_from_name - separate a string of form  <token>^<name>
73  * @op: operation being checked
74  * @args: string to parse  (NOT NULL)
75  * @token: stores returned parsed token value  (NOT NULL)
76  *
77  * Returns: start position of name after token else NULL on failure
78  */
79 static char *split_token_from_name(const char *op, char *args, u64 *token)
80 {
81 	char *name;
82 
83 	*token = simple_strtoull(args, &name, 16);
84 	if ((name == args) || *name != '^') {
85 		AA_ERROR("%s: Invalid input '%s'", op, args);
86 		return ERR_PTR(-EINVAL);
87 	}
88 
89 	name++;			/* skip ^ */
90 	if (!*name)
91 		name = NULL;
92 	return name;
93 }
94 
95 /**
96  * aa_setprocattr_changehat - handle procattr interface to change_hat
97  * @args: args received from writing to /proc/<pid>/attr/current (NOT NULL)
98  * @size: size of the args
99  * @flags: set of flags governing behavior
100  *
101  * Returns: %0 or error code if change_hat fails
102  */
103 int aa_setprocattr_changehat(char *args, size_t size, int flags)
104 {
105 	char *hat;
106 	u64 token;
107 	const char *hats[16];		/* current hard limit on # of names */
108 	int count = 0;
109 
110 	hat = split_token_from_name(OP_CHANGE_HAT, args, &token);
111 	if (IS_ERR(hat))
112 		return PTR_ERR(hat);
113 
114 	if (!hat && !token) {
115 		AA_ERROR("change_hat: Invalid input, NULL hat and NULL magic");
116 		return -EINVAL;
117 	}
118 
119 	if (hat) {
120 		/* set up hat name vector, args guaranteed null terminated
121 		 * at args[size] by setprocattr.
122 		 *
123 		 * If there are multiple hat names in the buffer each is
124 		 * separated by a \0.  Ie. userspace writes them pre tokenized
125 		 */
126 		char *end = args + size;
127 		for (count = 0; (hat < end) && count < 16; ++count) {
128 			char *next = hat + strlen(hat) + 1;
129 			hats[count] = hat;
130 			AA_DEBUG(DEBUG_DOMAIN,
131 				 "%s: (pid %d) Magic 0x%llx count %d hat '%s'\n"
132 				 , __func__, current->pid, token, count, hat);
133 			hat = next;
134 		}
135 	} else
136 		AA_DEBUG(DEBUG_DOMAIN,
137 			 "%s: (pid %d) Magic 0x%llx count %d Hat '%s'\n",
138 			 __func__, current->pid, token, count, "<NULL>");
139 
140 	return aa_change_hat(hats, count, token, flags);
141 }
142