Avoid beyond bounds copy while caching ACL
[zen-stable.git] / security / apparmor / procattr.c
blob1b41c542d376721646ba67dc4f17a3e21e1f241f
1 /*
2 * AppArmor security module
4 * This file contains AppArmor /proc/<pid>/attr/ interface functions
6 * Copyright (C) 1998-2008 Novell/SUSE
7 * Copyright 2009-2010 Canonical Ltd.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation, version 2 of the
12 * License.
15 #include "include/apparmor.h"
16 #include "include/context.h"
17 #include "include/policy.h"
18 #include "include/domain.h"
19 #include "include/procattr.h"
22 /**
23 * aa_getprocattr - Return the profile information for @profile
24 * @profile: the profile to print profile info about (NOT NULL)
25 * @string: Returns - string containing the profile info (NOT NULL)
27 * Returns: length of @string on success else error on failure
29 * Requires: profile != NULL
31 * Creates a string containing the namespace_name://profile_name for
32 * @profile.
34 * Returns: size of string placed in @string else error code on failure
36 int aa_getprocattr(struct aa_profile *profile, char **string)
38 char *str;
39 int len = 0, mode_len = 0, ns_len = 0, name_len;
40 const char *mode_str = profile_mode_names[profile->mode];
41 const char *ns_name = NULL;
42 struct aa_namespace *ns = profile->ns;
43 struct aa_namespace *current_ns = __aa_current_profile()->ns;
44 char *s;
46 if (!aa_ns_visible(current_ns, ns))
47 return -EACCES;
49 ns_name = aa_ns_name(current_ns, ns);
50 ns_len = strlen(ns_name);
52 /* if the visible ns_name is > 0 increase size for : :// seperator */
53 if (ns_len)
54 ns_len += 4;
56 /* unconfined profiles don't have a mode string appended */
57 if (!unconfined(profile))
58 mode_len = strlen(mode_str) + 3; /* + 3 for _() */
60 name_len = strlen(profile->base.hname);
61 len = mode_len + ns_len + name_len + 1; /* + 1 for \n */
62 s = str = kmalloc(len + 1, GFP_KERNEL); /* + 1 \0 */
63 if (!str)
64 return -ENOMEM;
66 if (ns_len) {
67 /* skip over prefix current_ns->base.hname and separating // */
68 sprintf(s, ":%s://", ns_name);
69 s += ns_len;
71 if (unconfined(profile))
72 /* mode string not being appended */
73 sprintf(s, "%s\n", profile->base.hname);
74 else
75 sprintf(s, "%s (%s)\n", profile->base.hname, mode_str);
76 *string = str;
78 /* NOTE: len does not include \0 of string, not saved as part of file */
79 return len;
82 /**
83 * split_token_from_name - separate a string of form <token>^<name>
84 * @op: operation being checked
85 * @args: string to parse (NOT NULL)
86 * @token: stores returned parsed token value (NOT NULL)
88 * Returns: start position of name after token else NULL on failure
90 static char *split_token_from_name(int op, char *args, u64 * token)
92 char *name;
94 *token = simple_strtoull(args, &name, 16);
95 if ((name == args) || *name != '^') {
96 AA_ERROR("%s: Invalid input '%s'", op_table[op], args);
97 return ERR_PTR(-EINVAL);
100 name++; /* skip ^ */
101 if (!*name)
102 name = NULL;
103 return name;
107 * aa_setprocattr_chagnehat - handle procattr interface to change_hat
108 * @args: args received from writing to /proc/<pid>/attr/current (NOT NULL)
109 * @size: size of the args
110 * @test: true if this is a test of change_hat permissions
112 * Returns: %0 or error code if change_hat fails
114 int aa_setprocattr_changehat(char *args, size_t size, int test)
116 char *hat;
117 u64 token;
118 const char *hats[16]; /* current hard limit on # of names */
119 int count = 0;
121 hat = split_token_from_name(OP_CHANGE_HAT, args, &token);
122 if (IS_ERR(hat))
123 return PTR_ERR(hat);
125 if (!hat && !token) {
126 AA_ERROR("change_hat: Invalid input, NULL hat and NULL magic");
127 return -EINVAL;
130 if (hat) {
131 /* set up hat name vector, args guaranteed null terminated
132 * at args[size] by setprocattr.
134 * If there are multiple hat names in the buffer each is
135 * separated by a \0. Ie. userspace writes them pre tokenized
137 char *end = args + size;
138 for (count = 0; (hat < end) && count < 16; ++count) {
139 char *next = hat + strlen(hat) + 1;
140 hats[count] = hat;
141 hat = next;
145 AA_DEBUG("%s: Magic 0x%llx Hat '%s'\n",
146 __func__, token, hat ? hat : NULL);
148 return aa_change_hat(hats, count, token, test);
152 * aa_setprocattr_changeprofile - handle procattr interface to changeprofile
153 * @fqname: args received from writting to /proc/<pid>/attr/current (NOT NULL)
154 * @onexec: true if change_profile should be delayed until exec
155 * @test: true if this is a test of change_profile permissions
157 * Returns: %0 or error code if change_profile fails
159 int aa_setprocattr_changeprofile(char *fqname, bool onexec, int test)
161 char *name, *ns_name;
163 name = aa_split_fqname(fqname, &ns_name);
164 return aa_change_profile(ns_name, name, onexec, test);
167 int aa_setprocattr_permipc(char *fqname)
169 /* TODO: add ipc permission querying */
170 return -ENOTSUPP;