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