~ubuntu-branches/ubuntu/feisty/apparmor/feisty

« back to all changes in this revision

Viewing changes to kernel-patches/for-mainline/cleanup-aa_register-2.diff

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2007-03-23 16:42:01 UTC
  • Revision ID: james.westby@ubuntu.com-20070323164201-jkax6f0oku087b7l
Tags: upstream-2.0.1+510.dfsg
ImportĀ upstreamĀ versionĀ 2.0.1+510.dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Index: b/security/apparmor/main.c
 
2
===================================================================
 
3
--- a/security/apparmor/main.c
 
4
+++ b/security/apparmor/main.c
 
5
@@ -772,6 +772,45 @@ int aa_fork(struct task_struct *task)
 
6
        return 0;
 
7
 }
 
8
 
 
9
+static struct aa_profile *
 
10
+aa_register_find(const char *name, int mandatory, int complain)
 
11
+{
 
12
+       struct aa_profile *profile;
 
13
+
 
14
+       /* Locate new profile */
 
15
+       profile = aa_profilelist_find(name);
 
16
+       if (profile) {
 
17
+               AA_DEBUG("%s: setting profile %s\n",
 
18
+                        __FUNCTION__, profile->name);
 
19
+       } else if (mandatory) {
 
20
+               if (complain) {
 
21
+                       LOG_HINT(profile, GFP_KERNEL, HINT_MANDPROF,
 
22
+                               "image=%s pid=%d profile=%s active=%s\n",
 
23
+                               name,
 
24
+                               current->pid,
 
25
+                               BASE_PROFILE(profile)->name, profile->name);
 
26
+
 
27
+                       profile = aa_dup_profile(null_complain_profile);
 
28
+               } else {
 
29
+                       AA_WARN("REJECTING exec(2) of image '%s'. "
 
30
+                               "Profile mandatory and not found "
 
31
+                               "(%s(%d) profile %s active %s)\n",
 
32
+                               name,
 
33
+                               current->comm, current->pid,
 
34
+                               BASE_PROFILE(profile)->name, profile->name);
 
35
+                       return ERR_PTR(-EPERM);
 
36
+               }
 
37
+       } else {
 
38
+               /* Only way we can get into this code is if task
 
39
+                * is unconstrained.
 
40
+                */
 
41
+               AA_DEBUG("%s: No profile found for exec image %s\n",
 
42
+                        __FUNCTION__,
 
43
+                        name);
 
44
+       }
 
45
+       return profile;
 
46
+}
 
47
+
 
48
 /**
 
49
  * aa_register - register a new program
 
50
  * @bprm: binprm of program being registered
 
51
@@ -783,156 +822,96 @@ int aa_register(struct linux_binprm *bpr
 
52
 {
 
53
        char *filename, *buffer = NULL;
 
54
        struct file *filp = bprm->file;
 
55
-       struct aa_profile *profile;
 
56
-       struct aa_profile *newprofile = NULL, unconstrained_flag;
 
57
-       int     error = -ENOMEM,
 
58
-               exec_mode = 0,
 
59
-               find_profile = 0,
 
60
-               find_profile_mandatory = 0,
 
61
-               complain = 0,
 
62
-               unsafe_exec = 0;
 
63
+       struct aa_profile *profile, *newprofile = NULL;
 
64
+       int exec_mode = AA_EXEC_UNSAFE, complain = 0;
 
65
 
 
66
        AA_DEBUG("%s\n", __FUNCTION__);
 
67
 
 
68
        filename = aa_get_name(filp->f_dentry, filp->f_vfsmnt, &buffer);
 
69
        if (IS_ERR(filename)) {
 
70
                AA_WARN("%s: Failed to get filename\n", __FUNCTION__);
 
71
-               goto out;
 
72
+               return -ENOENT;
 
73
        }
 
74
 
 
75
-       error = 0;
 
76
-
 
77
        profile = aa_get_profile(current);
 
78
+repeat:
 
79
+       if (profile) {
 
80
+               complain = PROFILE_COMPLAIN(profile);
 
81
 
 
82
-       if (!profile) {
 
83
-               /* Unconfined task, load profile if it exists */
 
84
-               find_profile = 1;
 
85
-               goto find_profile;
 
86
-       }
 
87
-
 
88
-       complain = PROFILE_COMPLAIN(profile);
 
89
-
 
90
-       /* Confined task, determine what mode inherit, unconstrained or
 
91
-        * mandatory to load new profile
 
92
-        */
 
93
-       exec_mode = aa_match(profile->file_rules, filename);
 
94
-       unsafe_exec = exec_mode & AA_EXEC_UNSAFE;
 
95
-       exec_mode &= AA_EXEC_MODIFIERS;
 
96
-
 
97
-       if (exec_mode) {
 
98
-               switch (exec_mode) {
 
99
-               case AA_EXEC_INHERIT:
 
100
-                       /* do nothing - setting of profile
 
101
-                        * already handed in aa_fork
 
102
-                        */
 
103
-                       AA_DEBUG("%s: INHERIT %s\n",
 
104
-                                __FUNCTION__,
 
105
-                                filename);
 
106
-                       break;
 
107
-
 
108
-               case AA_EXEC_UNCONSTRAINED:
 
109
-                       AA_DEBUG("%s: UNCONSTRAINED %s\n",
 
110
-                                __FUNCTION__,
 
111
-                                filename);
 
112
-
 
113
-                       /* unload profile */
 
114
-                       newprofile = &unconstrained_flag;
 
115
-                       break;
 
116
-
 
117
-               case AA_EXEC_PROFILE:
 
118
-                       AA_DEBUG("%s: PROFILE %s\n",
 
119
-                                __FUNCTION__,
 
120
-                                filename);
 
121
-
 
122
-                       find_profile = 1;
 
123
-                       find_profile_mandatory = 1;
 
124
-                       break;
 
125
-
 
126
-               default:
 
127
-                       AA_ERROR("%s: Rejecting exec(2) of image '%s'. "
 
128
-                                "Unknown exec qualifier %x "
 
129
-                                "(%s (pid %d) profile %s active %s)\n",
 
130
-                                __FUNCTION__,
 
131
-                                filename,
 
132
-                                exec_mode,
 
133
-                                current->comm, current->pid,
 
134
-                                BASE_PROFILE(profile)->name, profile->name);
 
135
-                       error = -EPERM;
 
136
-                       break;
 
137
-               }
 
138
-
 
139
-       } else if (complain) {
 
140
-               /* There was no entry in calling profile
 
141
-                * describing mode to execute image in.
 
142
-                * Drop into null-profile (disabling secure exec).
 
143
+               /* Confined task, determine what mode inherit, unconstrained or
 
144
+                * mandatory to load new profile
 
145
                 */
 
146
-               newprofile = aa_dup_profile(null_complain_profile);
 
147
-               unsafe_exec = 1;
 
148
-       } else {
 
149
-               AA_WARN("%s: Rejecting exec(2) of image '%s'. "
 
150
-                       "Unable to determine exec qualifier "
 
151
-                       "(%s (pid %d) profile %s active %s)\n",
 
152
-                       __FUNCTION__,
 
153
-                       filename,
 
154
-                       current->comm, current->pid,
 
155
-                       BASE_PROFILE(profile)->name, profile->name);
 
156
-               error = -EPERM;
 
157
-       }
 
158
-
 
159
+               exec_mode = aa_match(profile->file_rules, filename);
 
160
 
 
161
-find_profile:
 
162
-       if (!find_profile)
 
163
-               goto apply_profile;
 
164
-
 
165
-       /* Locate new profile */
 
166
-       newprofile = aa_profilelist_find(filename);
 
167
-       if (newprofile) {
 
168
-               AA_DEBUG("%s: setting profile %s\n",
 
169
-                        __FUNCTION__, newprofile->name);
 
170
-       } else if (find_profile_mandatory) {
 
171
-               /* Profile (mandatory) could not be found */
 
172
+               if (exec_mode & AA_EXEC_MODIFIERS) {
 
173
+                       switch (exec_mode & AA_EXEC_MODIFIERS) {
 
174
+                       case AA_EXEC_INHERIT:
 
175
+                               AA_DEBUG("%s: INHERIT %s\n",
 
176
+                                        __FUNCTION__,
 
177
+                                        filename);
 
178
+                               /* nothing to be done here */
 
179
+                               goto cleanup;
 
180
 
 
181
-               if (complain) {
 
182
-                       LOG_HINT(profile, GFP_KERNEL, HINT_MANDPROF,
 
183
-                               "image=%s pid=%d profile=%s active=%s\n",
 
184
-                               filename,
 
185
-                               current->pid,
 
186
-                               BASE_PROFILE(profile)->name, profile->name);
 
187
+                       case AA_EXEC_UNCONSTRAINED:
 
188
+                               AA_DEBUG("%s: UNCONSTRAINED %s\n",
 
189
+                                        __FUNCTION__,
 
190
+                                        filename);
 
191
+
 
192
+                               /* unload profile */
 
193
+                               newprofile = NULL;
 
194
+                               break;
 
195
+
 
196
+                       case AA_EXEC_PROFILE:
 
197
+                               AA_DEBUG("%s: PROFILE %s\n",
 
198
+                                        __FUNCTION__,
 
199
+                                        filename);
 
200
+                               newprofile = aa_register_find(filename, 1,
 
201
+                                                             complain);
 
202
+                               break;
 
203
+
 
204
+                       default:
 
205
+                               AA_ERROR("%s: Rejecting exec(2) of image '%s'. "
 
206
+                                        "Unknown exec qualifier %x "
 
207
+                                        "(%s (pid %d) profile %s active %s)\n",
 
208
+                                        __FUNCTION__,
 
209
+                                        filename,
 
210
+                                        exec_mode & AA_EXEC_MODIFIERS,
 
211
+                                        current->comm, current->pid,
 
212
+                                        BASE_PROFILE(profile)->name,
 
213
+                                        profile->name);
 
214
+                               newprofile = ERR_PTR(-EPERM);
 
215
+                               break;
 
216
+                       }
 
217
 
 
218
+               } else if (complain) {
 
219
+                       /* There was no entry in calling profile
 
220
+                        * describing mode to execute image in.
 
221
+                        * Drop into null-profile (disabling secure exec).
 
222
+                        */
 
223
                        newprofile = aa_dup_profile(null_complain_profile);
 
224
+                       exec_mode |= AA_EXEC_UNSAFE;
 
225
                } else {
 
226
-                       AA_WARN("REJECTING exec(2) of image '%s'. "
 
227
-                               "Profile mandatory and not found "
 
228
-                               "(%s(%d) profile %s active %s)\n",
 
229
+                       AA_WARN("%s: Rejecting exec(2) of image '%s'. "
 
230
+                               "Unable to determine exec qualifier "
 
231
+                               "(%s (pid %d) profile %s active %s)\n",
 
232
+                               __FUNCTION__,
 
233
                                filename,
 
234
                                current->comm, current->pid,
 
235
                                BASE_PROFILE(profile)->name, profile->name);
 
236
-                       error = -EPERM;
 
237
+                       newprofile = ERR_PTR(-EPERM);
 
238
                }
 
239
        } else {
 
240
-               /* Profile (non-mandatory) could not be found */
 
241
-
 
242
-               /* Only way we can get into this code is if task
 
243
-                * is unconstrained.
 
244
-                */
 
245
-
 
246
-               WARN_ON(profile);
 
247
-
 
248
-               AA_DEBUG("%s: No profile found for exec image %s\n",
 
249
-                        __FUNCTION__,
 
250
-                        filename);
 
251
-       } /* newprofile */
 
252
-
 
253
+               /* Unconfined task, load profile if it exists */
 
254
+               newprofile = aa_register_find(filename, 0, 0);
 
255
+               if (newprofile == NULL)
 
256
+                       goto cleanup;
 
257
+       }
 
258
 
 
259
-apply_profile:
 
260
-       /* Apply profile if necessary */
 
261
-       if (newprofile) {
 
262
+       /* Apply the new profile, or switch to unconfined if NULL. */
 
263
+       if (!IS_ERR(newprofile)) {
 
264
                struct aa_task_context *cxt, *lazy_cxt = NULL;
 
265
                unsigned long flags;
 
266
 
 
267
-               if (newprofile == &unconstrained_flag)
 
268
-                       newprofile = NULL;
 
269
-
 
270
                /* grab a lock - this is to guarentee consistency against
 
271
                 * other writers of aa_task_context (replacement/removal)
 
272
                 *
 
273
@@ -961,7 +940,7 @@ apply_profile:
 
274
                        if (!lazy_cxt) {
 
275
                                AA_ERROR("%s: Failed to allocate aa_task_context\n",
 
276
                                         __FUNCTION__);
 
277
-                               error = -ENOMEM;
 
278
+                               newprofile = ERR_PTR(-ENOMEM);
 
279
                                goto cleanup;
 
280
                        }
 
281
                }
 
282
@@ -999,7 +978,7 @@ apply_profile:
 
283
                                 * Redo with error checking
 
284
                                 */
 
285
                                spin_unlock_irqrestore(&cxt_lock, flags);
 
286
-                               goto find_profile;
 
287
+                               goto repeat;
 
288
                        }
 
289
                }
 
290
 
 
291
@@ -1012,7 +991,7 @@ apply_profile:
 
292
                 * Cases 2 and 3 are marked as requiring secure exec
 
293
                 * (unless policy specified "unsafe exec")
 
294
                 */
 
295
-               if (cxt && cxt->profile && !unsafe_exec) {
 
296
+               if (newprofile && !(exec_mode & AA_EXEC_UNSAFE)) {
 
297
                        unsigned long bprm_flags;
 
298
 
 
299
                        bprm_flags = AA_SECURE_EXEC_NEEDED;
 
300
@@ -1021,7 +1000,6 @@ apply_profile:
 
301
                }
 
302
 
 
303
                aa_switch_to_profile(cxt, newprofile, 0);
 
304
-               aa_put_profile(newprofile);
 
305
 
 
306
                if (complain && newprofile == null_complain_profile)
 
307
                        LOG_HINT(newprofile, GFP_ATOMIC, HINT_CHGPROF,
 
308
@@ -1033,11 +1011,11 @@ apply_profile:
 
309
 
 
310
 cleanup:
 
311
        aa_put_name_buffer(buffer);
 
312
-
 
313
        aa_put_profile(profile);
 
314
-
 
315
-out:
 
316
-       return error;
 
317
+       if (IS_ERR(newprofile))
 
318
+               return PTR_ERR(newprofile);
 
319
+       aa_put_profile(newprofile);
 
320
+       return 0;
 
321
 }
 
322
 
 
323
 /**