~mathiaz/apparmor/ubuntu-mathiaz

« back to all changes in this revision

Viewing changes to module-deprecated/apparmorfs.c

  • Committer: Mathias Gug
  • Date: 2008-02-04 18:57:00 UTC
  • mfrom: (885.1.5 apparmor)
  • Revision ID: mathiaz@ubuntu.com-20080204185700-wwlyq0ksssxclv8w
Merge  ubuntu branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *      Copyright (C) 2005 Novell/SUSE
3
 
 *
4
 
 *      This program is free software; you can redistribute it and/or
5
 
 *      modify it under the terms of the GNU General Public License as
6
 
 *      published by the Free Software Foundation, version 2 of the
7
 
 *      License.
8
 
 *
9
 
 *      AppArmor filesystem (part of securityfs)
10
 
 */
11
 
 
12
 
#include <linux/security.h>
13
 
#include <linux/vmalloc.h>
14
 
#include <linux/module.h>
15
 
#include <linux/seq_file.h>
16
 
#include <asm/uaccess.h>
17
 
 
18
 
#include "apparmor.h"
19
 
#include "inline.h"
20
 
#include "aamatch/match.h"
21
 
 
22
 
#define SECFS_SD "apparmor"
23
 
static struct dentry *sdfs_dentry = NULL;
24
 
 
25
 
/* profile */
26
 
extern struct seq_operations subdomainfs_profiles_op;
27
 
static int sd_prof_open(struct inode *inode, struct file *file);
28
 
static int sd_prof_release(struct inode *inode, struct file *file);
29
 
 
30
 
static struct file_operations subdomainfs_profiles_fops = {
31
 
        .open =         sd_prof_open,
32
 
        .read =         seq_read,
33
 
        .llseek =       seq_lseek,
34
 
        .release =      sd_prof_release,
35
 
};
36
 
 
37
 
/* version */
38
 
static ssize_t sd_version_read(struct file *file, char __user *buf,
39
 
                               size_t size, loff_t *ppos);
40
 
 
41
 
static struct file_operations subdomainfs_version_fops = {
42
 
        .read =         sd_version_read,
43
 
};
44
 
 
45
 
/* matching */
46
 
static ssize_t sd_matching_read(struct file *file, char __user *buf,
47
 
                               size_t size, loff_t *ppos);
48
 
 
49
 
static struct file_operations subdomainfs_matching_fops = {
50
 
        .read =         sd_matching_read,
51
 
};
52
 
 
53
 
 
54
 
/* interface */
55
 
extern ssize_t sd_file_prof_add(void *, size_t);
56
 
extern ssize_t sd_file_prof_repl(void *, size_t);
57
 
extern ssize_t sd_file_prof_remove(const char *, int);
58
 
 
59
 
static ssize_t sd_profile_load(struct file *f, const char __user *buf,
60
 
                               size_t size, loff_t *pos);
61
 
static ssize_t sd_profile_replace(struct file *f, const char __user *buf,
62
 
                                  size_t size, loff_t *pos);
63
 
static ssize_t sd_profile_remove(struct file *f, const char __user *buf,
64
 
                                 size_t size, loff_t *pos);
65
 
 
66
 
static struct file_operations subdomainfs_profile_load = {
67
 
        .write = sd_profile_load
68
 
};
69
 
 
70
 
static struct file_operations subdomainfs_profile_replace = {
71
 
        .write = sd_profile_replace
72
 
};
73
 
 
74
 
static struct file_operations subdomainfs_profile_remove = {
75
 
        .write = sd_profile_remove
76
 
};
77
 
 
78
 
 
79
 
/* control */
80
 
static u64 sd_control_get(void *data);
81
 
static void sd_control_set(void *data, u64 val);
82
 
 
83
 
DEFINE_SIMPLE_ATTRIBUTE(subdomainfs_control_fops, sd_control_get,
84
 
                        sd_control_set, "%lld\n");
85
 
 
86
 
 
87
 
 
88
 
/* table of static entries */
89
 
 
90
 
static struct root_entry {
91
 
        const char *name;
92
 
        int mode;
93
 
        int access;
94
 
        struct file_operations *fops;
95
 
        void *data;
96
 
 
97
 
        /* internal fields */
98
 
        struct dentry *dentry;
99
 
        int parent_index;
100
 
} root_entries[] = {
101
 
        /* our root, normally /sys/kernel/security/subdomain */
102
 
        {SECFS_SD,      S_IFDIR, 0550}, /* DO NOT EDIT/MOVE */
103
 
 
104
 
        /* interface for obtaining list of profiles currently loaded */
105
 
        {"profiles",    S_IFREG, 0440, &subdomainfs_profiles_fops,
106
 
                                       NULL},
107
 
 
108
 
        /* interface for obtaining version# of subdomain */
109
 
        {"version",     S_IFREG, 0440, &subdomainfs_version_fops,
110
 
                                       NULL},
111
 
 
112
 
        /* interface for obtaining matching features supported */
113
 
        {"matching",    S_IFREG, 0440, &subdomainfs_matching_fops,
114
 
                                       NULL},
115
 
 
116
 
        /* interface for loading/removing/replacing profiles */
117
 
        {".load",       S_IFREG, 0640, &subdomainfs_profile_load,
118
 
                                       NULL},
119
 
        {".replace",    S_IFREG, 0640, &subdomainfs_profile_replace,
120
 
                                       NULL},
121
 
        {".remove",     S_IFREG, 0640, &subdomainfs_profile_remove,
122
 
                                       NULL},
123
 
 
124
 
        /* interface for setting binary config values */
125
 
        {"control",     S_IFDIR, 0550},
126
 
        {"complain",    S_IFREG, 0640, &subdomainfs_control_fops,
127
 
                                       &subdomain_complain},
128
 
        {"audit",       S_IFREG, 0640, &subdomainfs_control_fops,
129
 
                                       &subdomain_audit},
130
 
        {"debug",       S_IFREG, 0640, &subdomainfs_control_fops,
131
 
                                       &subdomain_debug},
132
 
        {"logsyscall",  S_IFREG, 0640, &subdomainfs_control_fops,
133
 
                                       &subdomain_logsyscall},
134
 
        {NULL,          S_IFDIR, 0},
135
 
 
136
 
        /* root end */
137
 
        {NULL,          S_IFDIR, 0}
138
 
};
139
 
 
140
 
#define SDFS_DENTRY root_entries[0].dentry
141
 
 
142
 
static const unsigned int num_entries =
143
 
        sizeof(root_entries) / sizeof(struct root_entry);
144
 
 
145
 
 
146
 
 
147
 
static int sd_prof_open(struct inode *inode, struct file *file)
148
 
{
149
 
        return seq_open(file, &subdomainfs_profiles_op);
150
 
}
151
 
 
152
 
 
153
 
static int sd_prof_release(struct inode *inode, struct file *file)
154
 
{
155
 
        return seq_release(inode, file);
156
 
}
157
 
 
158
 
static ssize_t sd_version_read(struct file *file, char __user *buf,
159
 
                               size_t size, loff_t *ppos)
160
 
{
161
 
        const char *version = apparmor_version_nl();
162
 
 
163
 
        return simple_read_from_buffer(buf, size, ppos, version,
164
 
                                       strlen(version));
165
 
}
166
 
 
167
 
static ssize_t sd_matching_read(struct file *file, char __user *buf,
168
 
                               size_t size, loff_t *ppos)
169
 
{
170
 
        const char *matching = sdmatch_features();
171
 
 
172
 
        return simple_read_from_buffer(buf, size, ppos, matching,
173
 
                                       strlen(matching));
174
 
}
175
 
 
176
 
static char *sd_simple_write_to_buffer(const char __user *userbuf,
177
 
                                       size_t alloc_size, size_t copy_size,
178
 
                                       loff_t *pos, const char *msg)
179
 
{
180
 
        char *data;
181
 
 
182
 
        if (*pos != 0) {
183
 
                /* only writes from pos 0, that is complete writes */
184
 
                data = ERR_PTR(-ESPIPE);
185
 
                goto out;
186
 
        }
187
 
 
188
 
        /* Don't allow confined processes to load/replace/remove profiles.
189
 
         * No sane person would add rules allowing this to a profile
190
 
         * but we enforce the restriction anyways.
191
 
         */
192
 
        if (sd_is_confined()) {
193
 
                struct subdomain *sd = SD_SUBDOMAIN(current->security);
194
 
 
195
 
                SD_WARN("REJECTING access to profile %s (%s(%d) "
196
 
                        "profile %s active %s)\n",
197
 
                        msg, current->comm, current->pid,
198
 
                        sd->profile->name, sd->active->name);
199
 
 
200
 
                data = ERR_PTR(-EPERM);
201
 
                goto out;
202
 
        }
203
 
 
204
 
        data = vmalloc(alloc_size);
205
 
        if (data == NULL) {
206
 
                data = ERR_PTR(-ENOMEM);
207
 
                goto out;
208
 
        }
209
 
 
210
 
        if (copy_from_user(data, userbuf, copy_size)) {
211
 
                vfree(data);
212
 
                data = ERR_PTR(-EFAULT);
213
 
                goto out;
214
 
        }
215
 
 
216
 
out:
217
 
        return data;
218
 
}
219
 
 
220
 
static ssize_t sd_profile_load(struct file *f, const char __user *buf,
221
 
                               size_t size, loff_t *pos)
222
 
{
223
 
        char *data;
224
 
        ssize_t error;
225
 
 
226
 
        data = sd_simple_write_to_buffer(buf, size, size, pos, "load");
227
 
 
228
 
        if (!IS_ERR(data)) {
229
 
                error = sd_file_prof_add(data, size);
230
 
                vfree(data);
231
 
        } else {
232
 
                error = PTR_ERR(data);
233
 
        }
234
 
 
235
 
        return error;
236
 
}
237
 
 
238
 
static ssize_t sd_profile_replace(struct file *f, const char __user *buf,
239
 
                                  size_t size, loff_t *pos)
240
 
{
241
 
        char *data;
242
 
        ssize_t error;
243
 
 
244
 
        data = sd_simple_write_to_buffer(buf, size, size, pos, "replacement");
245
 
 
246
 
        if (!IS_ERR(data)) {
247
 
                error = sd_file_prof_repl(data, size);
248
 
                vfree(data);
249
 
        } else {
250
 
                error = PTR_ERR(data);
251
 
        }
252
 
 
253
 
        return error;
254
 
}
255
 
 
256
 
static ssize_t sd_profile_remove(struct file *f, const char __user *buf,
257
 
                                  size_t size, loff_t *pos)
258
 
{
259
 
        char *data;
260
 
        ssize_t error;
261
 
 
262
 
        /* sd_file_prof_remove needs a null terminated string so 1 extra
263
 
         * byte is allocated and null the copied data is then null terminated
264
 
         */
265
 
        data = sd_simple_write_to_buffer(buf, size+1, size, pos, "removal");
266
 
 
267
 
        if (!IS_ERR(data)) {
268
 
                data[size] = 0;
269
 
                error = sd_file_prof_remove(data, size);
270
 
                vfree(data);
271
 
        } else {
272
 
                error = PTR_ERR(data);
273
 
        }
274
 
 
275
 
        return error;
276
 
}
277
 
 
278
 
static u64 sd_control_get(void *data)
279
 
{
280
 
        return *(int *)data;
281
 
}
282
 
 
283
 
static void sd_control_set(void *data, u64 val)
284
 
{
285
 
        if (val > 1)
286
 
                val = 1;
287
 
 
288
 
        *(int*)data = (int)val;
289
 
}
290
 
 
291
 
static void clear_subdomainfs(void)
292
 
{
293
 
        unsigned int i;
294
 
 
295
 
        for (i=0; i < num_entries;i++) {
296
 
                unsigned int index;
297
 
 
298
 
                if (root_entries[i].mode == S_IFDIR) {
299
 
                        if (root_entries[i].name)
300
 
                                /* defer dir free till all sub-entries freed */
301
 
                                continue;
302
 
                        else
303
 
                                /* cleanup parent */
304
 
                                index = root_entries[i].parent_index;
305
 
                } else {
306
 
                        index = i;
307
 
                }
308
 
 
309
 
                if (root_entries[index].dentry) {
310
 
                        securityfs_remove(root_entries[index].dentry);
311
 
 
312
 
                        SD_DEBUG("%s: deleted subdomainfs entry name=%s "
313
 
                                 "dentry=%p\n",
314
 
                                __FUNCTION__,
315
 
                                root_entries[index].name,
316
 
                                root_entries[index].dentry);
317
 
 
318
 
                        root_entries[index].dentry = NULL;
319
 
                        root_entries[index].parent_index = 0;
320
 
                }
321
 
        }
322
 
}
323
 
 
324
 
static int populate_subdomainfs(struct dentry *root)
325
 
{
326
 
        unsigned int i, parent_index, depth;
327
 
 
328
 
#define ENT root_entries[i]
329
 
 
330
 
        for (i = 0; i < num_entries; i++) {
331
 
                root_entries[i].dentry = NULL;
332
 
                root_entries[i].parent_index = 0;
333
 
        }
334
 
 
335
 
        /* 1. Verify entry 0 is valid [sanity check] */
336
 
        if (num_entries == 0 ||
337
 
            !root_entries[0].name ||
338
 
            strcmp(root_entries[0].name, SECFS_SD) != 0 ||
339
 
            root_entries[0].mode != S_IFDIR) {
340
 
                SD_ERROR("%s: root entry 0 is not SECFS_SD/dir\n",
341
 
                        __FUNCTION__);
342
 
                goto error;
343
 
        }
344
 
 
345
 
        /* 2. Verify table structure */
346
 
        parent_index = 0;
347
 
        depth = 1;
348
 
 
349
 
        for (i = 1; i < num_entries; i++) {
350
 
                ENT.parent_index = parent_index;
351
 
 
352
 
                if (ENT.name && ENT.mode == S_IFDIR) {
353
 
                        depth++;
354
 
                        parent_index = i;
355
 
                } else if (!ENT.name) {
356
 
                        if (ENT.mode != S_IFDIR || depth == 0) {
357
 
                                SD_ERROR("%s: root_entry %d invalid (%u %d)",
358
 
                                         __FUNCTION__, i,
359
 
                                         ENT.mode, ENT.parent_index);
360
 
                                goto error;
361
 
                        }
362
 
 
363
 
                        depth--;
364
 
                        parent_index = root_entries[parent_index].parent_index;
365
 
                }
366
 
        }
367
 
 
368
 
        if (depth != 0) {
369
 
                SD_ERROR("%s: root_entry table not correctly terminated\n",
370
 
                        __FUNCTION__);
371
 
                goto error;
372
 
        }
373
 
 
374
 
        /* 3. Create root (parent=NULL) */
375
 
        i=0;
376
 
 
377
 
        ENT.dentry = securityfs_create_file(ENT.name,
378
 
                                        ENT.mode | ENT.access,
379
 
                                        NULL, NULL, NULL);
380
 
 
381
 
        if (ENT.dentry)
382
 
                SD_DEBUG("%s: created securityfs/subdomain [dentry=%p]\n",
383
 
                        __FUNCTION__, ENT.dentry);
384
 
        else
385
 
                goto error;
386
 
 
387
 
 
388
 
        /* 4. create remaining nodes */
389
 
        for (i = 1; i < num_entries; i++) {
390
 
                struct dentry *parent;
391
 
 
392
 
                /* end of directory ? */
393
 
                if (!ENT.name)
394
 
                        continue;
395
 
 
396
 
                parent = root_entries[ENT.parent_index].dentry;
397
 
 
398
 
                ENT.dentry = securityfs_create_file(ENT.name,
399
 
                                        ENT.mode | ENT.access,
400
 
                                        parent,
401
 
                                        ENT.mode != S_IFDIR ? ENT.data : NULL,
402
 
                                        ENT.mode != S_IFDIR ? ENT.fops : NULL);
403
 
 
404
 
                if (!ENT.dentry)
405
 
                        goto cleanup_error;
406
 
 
407
 
                SD_DEBUG("%s: added subdomainfs entry "
408
 
                         "name=%s mode=%x dentry=%p [parent %p]\n",
409
 
                        __FUNCTION__, ENT.name, ENT.mode|ENT.access,
410
 
                        ENT.dentry, parent);
411
 
        }
412
 
 
413
 
        return 1;
414
 
 
415
 
cleanup_error:
416
 
        clear_subdomainfs();
417
 
 
418
 
error:
419
 
        return 0;
420
 
}
421
 
 
422
 
int create_subdomainfs(void)
423
 
{
424
 
        if (SDFS_DENTRY)
425
 
                SD_ERROR("%s: Subdomain securityfs already exists\n",
426
 
                        __FUNCTION__);
427
 
        else if (!populate_subdomainfs(sdfs_dentry))
428
 
                SD_ERROR("%s: Error populating Subdomain securityfs\n",
429
 
                        __FUNCTION__);
430
 
 
431
 
        return (SDFS_DENTRY != NULL);
432
 
}
433
 
 
434
 
int destroy_subdomainfs(void)
435
 
{
436
 
        if (SDFS_DENTRY)
437
 
                clear_subdomainfs();
438
 
 
439
 
        return 1;
440
 
}