~ubuntu-branches/ubuntu/oneiric/apparmor/oneiric-security

« back to all changes in this revision

Viewing changes to kernel-patches/for-mainline/apparmor-aa_permission.diff

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2011-04-27 10:38:07 UTC
  • mfrom: (5.1.118 natty)
  • Revision ID: james.westby@ubuntu.com-20110427103807-ym3rhwys6o84ith0
Tags: 2.6.1-2
debian/copyright: clarify for some full organization names.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
Refactor some duplicated code patterns
2
 
 
3
 
lsm.c duplicates the pattern (get active profile; do access check; put
4
 
active profile) all over the place. This can easily be abstracted away
5
 
as in the attached patch.
6
 
 
7
 
Index: b/security/apparmor/lsm.c
8
 
===================================================================
9
 
--- a/security/apparmor/lsm.c
10
 
+++ b/security/apparmor/lsm.c
11
 
@@ -277,26 +277,27 @@ out:
12
 
        return error;
13
 
 }
14
 
 
15
 
-static int apparmor_inode_create(struct inode *dir, struct dentry *dentry,
16
 
-                                struct vfsmount *mnt, int mask)
17
 
+static int aa_permission(struct inode *inode, struct dentry *dentry,
18
 
+                        struct vfsmount *mnt, int mask)
19
 
 {
20
 
-       struct aa_profile *active;
21
 
        int error = 0;
22
 
 
23
 
-       if (!mnt || !mediated_filesystem(dir))
24
 
-               goto out;
25
 
-
26
 
-       active = get_active_aa_profile();
27
 
-
28
 
-       /* At a minimum, need write perm to create */
29
 
-       if (active)
30
 
-               error = aa_perm(active, dentry, mnt, MAY_WRITE);
31
 
+       if (mnt && mediated_filesystem(inode)) {
32
 
+               struct aa_profile *active = get_active_aa_profile();
33
 
 
34
 
-       put_aa_profile(active);
35
 
-out:
36
 
+               if (active)
37
 
+                       error = aa_perm(active, dentry, mnt, mask);
38
 
+               put_aa_profile(active);
39
 
+       }
40
 
        return error;
41
 
 }
42
 
 
43
 
+static int apparmor_inode_create(struct inode *dir, struct dentry *dentry,
44
 
+                                struct vfsmount *mnt, int mask)
45
 
+{
46
 
+       return aa_permission(dir, dentry, mnt, MAY_WRITE);
47
 
+}
48
 
+
49
 
 static int apparmor_inode_link(struct dentry *old_dentry,
50
 
                               struct vfsmount *old_mnt, struct inode *dir,
51
 
                               struct dentry *new_dentry,
52
 
@@ -324,61 +325,19 @@ static int apparmor_inode_unlink(struct 
53
 
                                 struct dentry *dentry,
54
 
                                 struct vfsmount *mnt)
55
 
 {
56
 
-       struct aa_profile *active;
57
 
-       int error = 0;
58
 
-
59
 
-       if (!mnt || !mediated_filesystem(dir))
60
 
-               goto out;
61
 
-
62
 
-       active = get_active_aa_profile();
63
 
-
64
 
-       if (active)
65
 
-               error = aa_perm(active, dentry, mnt, MAY_WRITE);
66
 
-
67
 
-       put_aa_profile(active);
68
 
-
69
 
-out:
70
 
-       return error;
71
 
+       return aa_permission(dir, dentry, mnt, MAY_WRITE);
72
 
 }
73
 
 
74
 
 static int apparmor_inode_symlink(struct inode *dir, struct dentry *dentry,
75
 
                                  struct vfsmount *mnt, const char *old_name)
76
 
 {
77
 
-       struct aa_profile *active;
78
 
-       int error = 0;
79
 
-
80
 
-       if (!mnt || !mediated_filesystem(dir))
81
 
-               goto out;
82
 
-
83
 
-       active = get_active_aa_profile();
84
 
-
85
 
-       if (active)
86
 
-               error = aa_perm(active, dentry, mnt, MAY_WRITE);
87
 
-
88
 
-       put_aa_profile(active);
89
 
-
90
 
-out:
91
 
-       return error;
92
 
+       return aa_permission(dir, dentry, mnt, MAY_WRITE);
93
 
 }
94
 
 
95
 
 static int apparmor_inode_mknod(struct inode *dir, struct dentry *dentry,
96
 
                                struct vfsmount *mnt, int mode, dev_t dev)
97
 
 {
98
 
-       struct aa_profile *active;
99
 
-       int error = 0;
100
 
-
101
 
-       if (!mnt || !mediated_filesystem(dir))
102
 
-               goto out;
103
 
-
104
 
-       active = get_active_aa_profile();
105
 
-
106
 
-       if (active)
107
 
-               error = aa_perm(active, dentry, mnt, MAY_WRITE);
108
 
-
109
 
-       put_aa_profile(active);
110
 
-
111
 
-out:
112
 
-       return error;
113
 
+       return aa_permission(dir, dentry, mnt, MAY_WRITE);
114
 
 }
115
 
 
116
 
 static int apparmor_inode_rename(struct inode *old_dir,
117
 
@@ -415,21 +374,10 @@ out:
118
 
 static int apparmor_inode_permission(struct inode *inode, int mask,
119
 
                                     struct nameidata *nd)
120
 
 {
121
 
-       int error = 0;
122
 
-
123
 
-       /* Do not perform check on pipes or sockets
124
 
-        * Same as apparmor_file_permission
125
 
-        */
126
 
-       if (nd && mediated_filesystem(inode)) {
127
 
-               struct aa_profile *active;
128
 
-
129
 
-               active = get_active_aa_profile();
130
 
-               if (active)
131
 
-                       error = aa_perm(active, nd->dentry, nd->mnt, mask);
132
 
-               put_aa_profile(active);
133
 
-       }
134
 
-
135
 
-       return error;
136
 
+       if (!nd)
137
 
+               return 0;
138
 
+       return aa_permission(inode, nd->dentry, nd->mnt,
139
 
+                            mask & (MAY_READ | MAY_WRITE | MAY_EXEC));
140
 
 }
141
 
 
142
 
 static int apparmor_inode_setattr(struct dentry *dentry, struct vfsmount *mnt,
143
 
@@ -458,91 +406,49 @@ out:
144
 
        return error;
145
 
 }
146
 
 
147
 
-static int apparmor_inode_setxattr(struct dentry *dentry, struct vfsmount *mnt,
148
 
-                                  char *name, void *value, size_t size,
149
 
-                                  int flags)
150
 
+static int aa_xattr_permission(struct dentry *dentry, struct vfsmount *mnt,
151
 
+                              const char *name, const char *operation,
152
 
+                              int mask)
153
 
 {
154
 
        int error = 0;
155
 
 
156
 
-       if (!mnt)
157
 
-               goto out;
158
 
-
159
 
-       if (mediated_filesystem(dentry->d_inode)) {
160
 
-               struct aa_profile *active;
161
 
-
162
 
-               active = get_active_aa_profile();
163
 
+       if (mnt && mediated_filesystem(dentry->d_inode)) {
164
 
+               struct aa_profile *active = get_active_aa_profile();
165
 
+
166
 
                if (active)
167
 
                        error = aa_perm_xattr(active, dentry, mnt, name,
168
 
-                                             "xattr set", AA_MAY_WRITE);
169
 
+                                             operation, mask);
170
 
                put_aa_profile(active);
171
 
        }
172
 
 
173
 
-out:
174
 
        return error;
175
 
 }
176
 
 
177
 
+static int apparmor_inode_setxattr(struct dentry *dentry, struct vfsmount *mnt,
178
 
+                                  char *name, void *value, size_t size,
179
 
+                                  int flags)
180
 
+{
181
 
+       return aa_xattr_permission(dentry, mnt, name, "xattr set",
182
 
+                                  AA_MAY_WRITE);
183
 
+}
184
 
+
185
 
 static int apparmor_inode_getxattr(struct dentry *dentry, struct vfsmount *mnt,
186
 
                                   char *name)
187
 
 {
188
 
-       int error = 0;
189
 
-
190
 
-       if (!mnt)
191
 
-               goto out;
192
 
-
193
 
-       if (mediated_filesystem(dentry->d_inode)) {
194
 
-               struct aa_profile *active;
195
 
-
196
 
-               active = get_active_aa_profile();
197
 
-               if (active)
198
 
-                       error = aa_perm_xattr(active, dentry, mnt, name,
199
 
-                                             "xattr get", AA_MAY_READ);
200
 
-               put_aa_profile(active);
201
 
-       }
202
 
-
203
 
-out:
204
 
-       return error;
205
 
+       return aa_xattr_permission(dentry, mnt, name, "xattr get", AA_MAY_READ);
206
 
 }
207
 
+
208
 
 static int apparmor_inode_listxattr(struct dentry *dentry, struct vfsmount *mnt)
209
 
 {
210
 
-       int error = 0;
211
 
-
212
 
-       if (!mnt)
213
 
-               goto out;
214
 
-
215
 
-       if (mediated_filesystem(dentry->d_inode)) {
216
 
-               struct aa_profile *active;
217
 
-
218
 
-               active = get_active_aa_profile();
219
 
-               if (active)
220
 
-                       error = aa_perm_xattr(active, dentry, mnt, NULL,
221
 
-                                             "xattr list", AA_MAY_READ);;
222
 
-               put_aa_profile(active);
223
 
-       }
224
 
-
225
 
-out:
226
 
-       return error;
227
 
+       return aa_xattr_permission(dentry, mnt, NULL, "xattr list",
228
 
+                                  AA_MAY_READ);
229
 
 }
230
 
 
231
 
 static int apparmor_inode_removexattr(struct dentry *dentry,
232
 
                                      struct vfsmount *mnt, char *name)
233
 
 {
234
 
-       int error = 0;
235
 
-
236
 
-       if (!mnt)
237
 
-               goto out;
238
 
-
239
 
-       if (mediated_filesystem(dentry->d_inode)) {
240
 
-               struct aa_profile *active;
241
 
-
242
 
-               active = get_active_aa_profile();
243
 
-               if (active)
244
 
-                       error = aa_perm_xattr(active, dentry, mnt, name,
245
 
-                                             "xattr remove", AA_MAY_WRITE);
246
 
-               put_aa_profile(active);
247
 
-       }
248
 
-
249
 
-out:
250
 
-       return error;
251
 
+       return aa_xattr_permission(dentry, mnt, name, "xattr remove",
252
 
+                                  AA_MAY_WRITE);
253
 
 }
254
 
 
255
 
 static int apparmor_file_permission(struct file *file, int mask)
256
 
@@ -551,15 +457,16 @@ static int apparmor_file_permission(stru
257
 
        struct aa_profile *file_profile = (struct aa_profile*)file->f_security;
258
 
        int error = 0;
259
 
 
260
 
-       /* bail out early if this isn't a mediated file */
261
 
-       if (!file_profile || !mediated_filesystem(file->f_dentry->d_inode))
262
 
+       /* FIXME: get rid of revalidation. */
263
 
+       if (!file_profile)
264
 
                goto out;
265
 
 
266
 
        active = get_active_aa_profile();
267
 
        if (active && file_profile != active) {
268
 
-               /* FIXME: get rid of revalidation. */
269
 
-               error = aa_perm(active, file->f_dentry, file->f_vfsmnt,
270
 
-                               mask & (MAY_EXEC | MAY_WRITE | MAY_READ));
271
 
+               struct dentry *dentry = file->f_dentry;
272
 
+
273
 
+               error = aa_permission(dentry->d_inode, dentry, file->f_vfsmnt,
274
 
+                                     mask & (MAY_READ | MAY_WRITE | MAY_EXEC));
275
 
        }
276
 
        put_aa_profile(active);
277
 
 
278
 
@@ -591,32 +498,22 @@ static void apparmor_file_free_security(
279
 
 static inline int aa_mmap(struct file *file, unsigned long prot,
280
 
                          unsigned long flags)
281
 
 {
282
 
-       int error = 0, mask = 0;
283
 
-       struct aa_profile *active;
284
 
+       int mask = 0;
285
 
 
286
 
-       active = get_active_aa_profile();
287
 
-       if (!active || !file || !mediated_filesystem(file->f_dentry->d_inode))
288
 
-               goto out;
289
 
+       if (!file)
290
 
+               return 0;
291
 
 
292
 
        if (prot & PROT_READ)
293
 
                mask |= MAY_READ;
294
 
-
295
 
        /* Private mappings don't require write perms since they don't
296
 
         * write back to the files */
297
 
-       if (prot & PROT_WRITE && !(flags & MAP_PRIVATE))
298
 
+       if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE))
299
 
                mask |= MAY_WRITE;
300
 
        if (prot & PROT_EXEC)
301
 
                mask |= AA_EXEC_MMAP;
302
 
 
303
 
-       AA_DEBUG("%s: 0x%x\n", __FUNCTION__, mask);
304
 
-
305
 
-       if (mask)
306
 
-               error = aa_perm(active, file->f_dentry, file->f_vfsmnt, mask);
307
 
-
308
 
-       put_aa_profile(active);
309
 
-
310
 
-out:
311
 
-       return error;
312
 
+       return aa_permission(file->f_dentry->d_inode, file->f_dentry,
313
 
+                            file->f_vfsmnt, mask);
314
 
 }
315
 
 
316
 
 static int apparmor_file_mmap(struct file *file, unsigned long reqprot,