Commit 3ed02ada2a5e695e2fbb5e4a0008cfcb0f50feaa

Authored by John Johansen
Committed by James Morris
1 parent 9f1c1d426b

AppArmor: Ensure the size of the copy is < the buffer allocated to hold it

Actually I think in this case the appropriate thing to do is to BUG as there
is currently a case (remove) where the alloc_size needs to be larger than
the copy_size, and if copy_size is ever greater than alloc_size there is
a mistake in the caller code.

Signed-off-by: John Johansen <john.johansen@canonical.com>
Acked-by: Kees Cook <kees.cook@canonical.com>
Signed-off-by: James Morris <jmorris@namei.org>

Showing 1 changed file with 3 additions and 1 deletions Inline Diff

security/apparmor/apparmorfs.c
1 /* 1 /*
2 * AppArmor security module 2 * AppArmor security module
3 * 3 *
4 * This file contains AppArmor /sys/kernel/security/apparmor interface functions 4 * This file contains AppArmor /sys/kernel/security/apparmor interface functions
5 * 5 *
6 * Copyright (C) 1998-2008 Novell/SUSE 6 * Copyright (C) 1998-2008 Novell/SUSE
7 * Copyright 2009-2010 Canonical Ltd. 7 * Copyright 2009-2010 Canonical Ltd.
8 * 8 *
9 * This program is free software; you can redistribute it and/or 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 10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation, version 2 of the 11 * published by the Free Software Foundation, version 2 of the
12 * License. 12 * License.
13 */ 13 */
14 14
15 #include <linux/security.h> 15 #include <linux/security.h>
16 #include <linux/vmalloc.h> 16 #include <linux/vmalloc.h>
17 #include <linux/module.h> 17 #include <linux/module.h>
18 #include <linux/seq_file.h> 18 #include <linux/seq_file.h>
19 #include <linux/uaccess.h> 19 #include <linux/uaccess.h>
20 #include <linux/namei.h> 20 #include <linux/namei.h>
21 21
22 #include "include/apparmor.h" 22 #include "include/apparmor.h"
23 #include "include/apparmorfs.h" 23 #include "include/apparmorfs.h"
24 #include "include/audit.h" 24 #include "include/audit.h"
25 #include "include/context.h" 25 #include "include/context.h"
26 #include "include/policy.h" 26 #include "include/policy.h"
27 27
28 /** 28 /**
29 * aa_simple_write_to_buffer - common routine for getting policy from user 29 * aa_simple_write_to_buffer - common routine for getting policy from user
30 * @op: operation doing the user buffer copy 30 * @op: operation doing the user buffer copy
31 * @userbuf: user buffer to copy data from (NOT NULL) 31 * @userbuf: user buffer to copy data from (NOT NULL)
32 * @alloc_size: size of user buffer 32 * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size)
33 * @copy_size: size of data to copy from user buffer 33 * @copy_size: size of data to copy from user buffer
34 * @pos: position write is at in the file (NOT NULL) 34 * @pos: position write is at in the file (NOT NULL)
35 * 35 *
36 * Returns: kernel buffer containing copy of user buffer data or an 36 * Returns: kernel buffer containing copy of user buffer data or an
37 * ERR_PTR on failure. 37 * ERR_PTR on failure.
38 */ 38 */
39 static char *aa_simple_write_to_buffer(int op, const char __user *userbuf, 39 static char *aa_simple_write_to_buffer(int op, const char __user *userbuf,
40 size_t alloc_size, size_t copy_size, 40 size_t alloc_size, size_t copy_size,
41 loff_t *pos) 41 loff_t *pos)
42 { 42 {
43 char *data; 43 char *data;
44
45 BUG_ON(copy_size > alloc_size);
44 46
45 if (*pos != 0) 47 if (*pos != 0)
46 /* only writes from pos 0, that is complete writes */ 48 /* only writes from pos 0, that is complete writes */
47 return ERR_PTR(-ESPIPE); 49 return ERR_PTR(-ESPIPE);
48 50
49 /* 51 /*
50 * Don't allow profile load/replace/remove from profiles that don't 52 * Don't allow profile load/replace/remove from profiles that don't
51 * have CAP_MAC_ADMIN 53 * have CAP_MAC_ADMIN
52 */ 54 */
53 if (!aa_may_manage_policy(op)) 55 if (!aa_may_manage_policy(op))
54 return ERR_PTR(-EACCES); 56 return ERR_PTR(-EACCES);
55 57
56 /* freed by caller to simple_write_to_buffer */ 58 /* freed by caller to simple_write_to_buffer */
57 data = kvmalloc(alloc_size); 59 data = kvmalloc(alloc_size);
58 if (data == NULL) 60 if (data == NULL)
59 return ERR_PTR(-ENOMEM); 61 return ERR_PTR(-ENOMEM);
60 62
61 if (copy_from_user(data, userbuf, copy_size)) { 63 if (copy_from_user(data, userbuf, copy_size)) {
62 kvfree(data); 64 kvfree(data);
63 return ERR_PTR(-EFAULT); 65 return ERR_PTR(-EFAULT);
64 } 66 }
65 67
66 return data; 68 return data;
67 } 69 }
68 70
69 71
70 /* .load file hook fn to load policy */ 72 /* .load file hook fn to load policy */
71 static ssize_t profile_load(struct file *f, const char __user *buf, size_t size, 73 static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
72 loff_t *pos) 74 loff_t *pos)
73 { 75 {
74 char *data; 76 char *data;
75 ssize_t error; 77 ssize_t error;
76 78
77 data = aa_simple_write_to_buffer(OP_PROF_LOAD, buf, size, size, pos); 79 data = aa_simple_write_to_buffer(OP_PROF_LOAD, buf, size, size, pos);
78 80
79 error = PTR_ERR(data); 81 error = PTR_ERR(data);
80 if (!IS_ERR(data)) { 82 if (!IS_ERR(data)) {
81 error = aa_replace_profiles(data, size, PROF_ADD); 83 error = aa_replace_profiles(data, size, PROF_ADD);
82 kvfree(data); 84 kvfree(data);
83 } 85 }
84 86
85 return error; 87 return error;
86 } 88 }
87 89
88 static const struct file_operations aa_fs_profile_load = { 90 static const struct file_operations aa_fs_profile_load = {
89 .write = profile_load 91 .write = profile_load
90 }; 92 };
91 93
92 /* .replace file hook fn to load and/or replace policy */ 94 /* .replace file hook fn to load and/or replace policy */
93 static ssize_t profile_replace(struct file *f, const char __user *buf, 95 static ssize_t profile_replace(struct file *f, const char __user *buf,
94 size_t size, loff_t *pos) 96 size_t size, loff_t *pos)
95 { 97 {
96 char *data; 98 char *data;
97 ssize_t error; 99 ssize_t error;
98 100
99 data = aa_simple_write_to_buffer(OP_PROF_REPL, buf, size, size, pos); 101 data = aa_simple_write_to_buffer(OP_PROF_REPL, buf, size, size, pos);
100 error = PTR_ERR(data); 102 error = PTR_ERR(data);
101 if (!IS_ERR(data)) { 103 if (!IS_ERR(data)) {
102 error = aa_replace_profiles(data, size, PROF_REPLACE); 104 error = aa_replace_profiles(data, size, PROF_REPLACE);
103 kvfree(data); 105 kvfree(data);
104 } 106 }
105 107
106 return error; 108 return error;
107 } 109 }
108 110
109 static const struct file_operations aa_fs_profile_replace = { 111 static const struct file_operations aa_fs_profile_replace = {
110 .write = profile_replace 112 .write = profile_replace
111 }; 113 };
112 114
113 /* .remove file hook fn to remove loaded policy */ 115 /* .remove file hook fn to remove loaded policy */
114 static ssize_t profile_remove(struct file *f, const char __user *buf, 116 static ssize_t profile_remove(struct file *f, const char __user *buf,
115 size_t size, loff_t *pos) 117 size_t size, loff_t *pos)
116 { 118 {
117 char *data; 119 char *data;
118 ssize_t error; 120 ssize_t error;
119 121
120 /* 122 /*
121 * aa_remove_profile needs a null terminated string so 1 extra 123 * aa_remove_profile needs a null terminated string so 1 extra
122 * byte is allocated and the copied data is null terminated. 124 * byte is allocated and the copied data is null terminated.
123 */ 125 */
124 data = aa_simple_write_to_buffer(OP_PROF_RM, buf, size + 1, size, pos); 126 data = aa_simple_write_to_buffer(OP_PROF_RM, buf, size + 1, size, pos);
125 127
126 error = PTR_ERR(data); 128 error = PTR_ERR(data);
127 if (!IS_ERR(data)) { 129 if (!IS_ERR(data)) {
128 data[size] = 0; 130 data[size] = 0;
129 error = aa_remove_profiles(data, size); 131 error = aa_remove_profiles(data, size);
130 kvfree(data); 132 kvfree(data);
131 } 133 }
132 134
133 return error; 135 return error;
134 } 136 }
135 137
136 static const struct file_operations aa_fs_profile_remove = { 138 static const struct file_operations aa_fs_profile_remove = {
137 .write = profile_remove 139 .write = profile_remove
138 }; 140 };
139 141
140 /** Base file system setup **/ 142 /** Base file system setup **/
141 143
142 static struct dentry *aa_fs_dentry __initdata; 144 static struct dentry *aa_fs_dentry __initdata;
143 145
144 static void __init aafs_remove(const char *name) 146 static void __init aafs_remove(const char *name)
145 { 147 {
146 struct dentry *dentry; 148 struct dentry *dentry;
147 149
148 dentry = lookup_one_len(name, aa_fs_dentry, strlen(name)); 150 dentry = lookup_one_len(name, aa_fs_dentry, strlen(name));
149 if (!IS_ERR(dentry)) { 151 if (!IS_ERR(dentry)) {
150 securityfs_remove(dentry); 152 securityfs_remove(dentry);
151 dput(dentry); 153 dput(dentry);
152 } 154 }
153 } 155 }
154 156
155 /** 157 /**
156 * aafs_create - create an entry in the apparmor filesystem 158 * aafs_create - create an entry in the apparmor filesystem
157 * @name: name of the entry (NOT NULL) 159 * @name: name of the entry (NOT NULL)
158 * @mask: file permission mask of the file 160 * @mask: file permission mask of the file
159 * @fops: file operations for the file (NOT NULL) 161 * @fops: file operations for the file (NOT NULL)
160 * 162 *
161 * Used aafs_remove to remove entries created with this fn. 163 * Used aafs_remove to remove entries created with this fn.
162 */ 164 */
163 static int __init aafs_create(const char *name, int mask, 165 static int __init aafs_create(const char *name, int mask,
164 const struct file_operations *fops) 166 const struct file_operations *fops)
165 { 167 {
166 struct dentry *dentry; 168 struct dentry *dentry;
167 169
168 dentry = securityfs_create_file(name, S_IFREG | mask, aa_fs_dentry, 170 dentry = securityfs_create_file(name, S_IFREG | mask, aa_fs_dentry,
169 NULL, fops); 171 NULL, fops);
170 172
171 return IS_ERR(dentry) ? PTR_ERR(dentry) : 0; 173 return IS_ERR(dentry) ? PTR_ERR(dentry) : 0;
172 } 174 }
173 175
174 /** 176 /**
175 * aa_destroy_aafs - cleanup and free aafs 177 * aa_destroy_aafs - cleanup and free aafs
176 * 178 *
177 * releases dentries allocated by aa_create_aafs 179 * releases dentries allocated by aa_create_aafs
178 */ 180 */
179 void __init aa_destroy_aafs(void) 181 void __init aa_destroy_aafs(void)
180 { 182 {
181 if (aa_fs_dentry) { 183 if (aa_fs_dentry) {
182 aafs_remove(".remove"); 184 aafs_remove(".remove");
183 aafs_remove(".replace"); 185 aafs_remove(".replace");
184 aafs_remove(".load"); 186 aafs_remove(".load");
185 187
186 securityfs_remove(aa_fs_dentry); 188 securityfs_remove(aa_fs_dentry);
187 aa_fs_dentry = NULL; 189 aa_fs_dentry = NULL;
188 } 190 }
189 } 191 }
190 192
191 /** 193 /**
192 * aa_create_aafs - create the apparmor security filesystem 194 * aa_create_aafs - create the apparmor security filesystem
193 * 195 *
194 * dentries created here are released by aa_destroy_aafs 196 * dentries created here are released by aa_destroy_aafs
195 * 197 *
196 * Returns: error on failure 198 * Returns: error on failure
197 */ 199 */
198 int __init aa_create_aafs(void) 200 int __init aa_create_aafs(void)
199 { 201 {
200 int error; 202 int error;
201 203
202 if (!apparmor_initialized) 204 if (!apparmor_initialized)
203 return 0; 205 return 0;
204 206
205 if (aa_fs_dentry) { 207 if (aa_fs_dentry) {
206 AA_ERROR("%s: AppArmor securityfs already exists\n", __func__); 208 AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
207 return -EEXIST; 209 return -EEXIST;
208 } 210 }
209 211
210 aa_fs_dentry = securityfs_create_dir("apparmor", NULL); 212 aa_fs_dentry = securityfs_create_dir("apparmor", NULL);
211 if (IS_ERR(aa_fs_dentry)) { 213 if (IS_ERR(aa_fs_dentry)) {
212 error = PTR_ERR(aa_fs_dentry); 214 error = PTR_ERR(aa_fs_dentry);
213 aa_fs_dentry = NULL; 215 aa_fs_dentry = NULL;
214 goto error; 216 goto error;
215 } 217 }
216 218
217 error = aafs_create(".load", 0640, &aa_fs_profile_load); 219 error = aafs_create(".load", 0640, &aa_fs_profile_load);
218 if (error) 220 if (error)
219 goto error; 221 goto error;
220 error = aafs_create(".replace", 0640, &aa_fs_profile_replace); 222 error = aafs_create(".replace", 0640, &aa_fs_profile_replace);
221 if (error) 223 if (error)
222 goto error; 224 goto error;
223 error = aafs_create(".remove", 0640, &aa_fs_profile_remove); 225 error = aafs_create(".remove", 0640, &aa_fs_profile_remove);
224 if (error) 226 if (error)
225 goto error; 227 goto error;
226 228
227 /* TODO: add support for apparmorfs_null and apparmorfs_mnt */ 229 /* TODO: add support for apparmorfs_null and apparmorfs_mnt */
228 230
229 /* Report that AppArmor fs is enabled */ 231 /* Report that AppArmor fs is enabled */
230 aa_info_message("AppArmor Filesystem Enabled"); 232 aa_info_message("AppArmor Filesystem Enabled");
231 return 0; 233 return 0;
232 234
233 error: 235 error:
234 aa_destroy_aafs(); 236 aa_destroy_aafs();
235 AA_ERROR("Error creating AppArmor securityfs\n"); 237 AA_ERROR("Error creating AppArmor securityfs\n");
236 return error; 238 return error;
237 } 239 }
238 240
239 fs_initcall(aa_create_aafs); 241 fs_initcall(aa_create_aafs);
240 242