~ubuntu-branches/ubuntu/wily/apparmor/wily

« back to all changes in this revision

Viewing changes to module-deprecated/main.c

  • 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
 
/*
2
 
 *      Copyright (C) 2002-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 Core
10
 
 */
11
 
 
12
 
#include <linux/security.h>
13
 
#include <linux/namei.h>
14
 
#include <linux/audit.h>
15
 
 
16
 
#include "apparmor.h"
17
 
#include "aamatch/match.h"
18
 
 
19
 
#include "inline.h"
20
 
 
21
 
/* NULL profile
22
 
 *
23
 
 * Used when an attempt is made to changehat into a non-existant
24
 
 * subhat.   In the NULL profile,  no file access is allowed
25
 
 * (currently full network access is allowed).  Using a NULL
26
 
 * profile ensures that active is always non zero.
27
 
 *
28
 
 * Leaving the NULL profile is by either successfully changehatting
29
 
 * into a sibling hat, or changehatting back to the parent (NULL hat).
30
 
 */
31
 
struct sdprofile *null_profile;
32
 
 
33
 
/* NULL complain profile
34
 
 *
35
 
 * Used when in complain mode, to emit Permitting messages for non-existant
36
 
 * profiles and hats.  This is necessary because of selective mode, in which
37
 
 * case we need a complain null_profile and enforce null_profile
38
 
 *
39
 
 * The null_complain_profile cannot be statically allocated, because it
40
 
 * can be associated to files which keep their reference even if subdomain is
41
 
 * unloaded
42
 
 */
43
 
struct sdprofile *null_complain_profile;
44
 
 
45
 
/***************************
46
 
 * PRIVATE UTILITY FUNCTIONS
47
 
 **************************/
48
 
 
49
 
/**
50
 
 * dentry_xlate_error
51
 
 * @dentry: pointer to dentry
52
 
 * @error: error number
53
 
 * @dtype: type of dentry
54
 
 *
55
 
 * Display error message when a dentry translation error occured
56
 
 */
57
 
static void dentry_xlate_error(struct dentry *dentry, int error, char *dtype)
58
 
{
59
 
        const unsigned int len = 16;
60
 
        char buf[len];
61
 
 
62
 
        if (dentry->d_inode) {
63
 
                snprintf(buf, len, "%lu", dentry->d_inode->i_ino);
64
 
        } else {
65
 
                strncpy(buf, "<negative>", len);
66
 
                buf[len-1]=0;
67
 
        }
68
 
 
69
 
        SD_ERROR("An error occured while translating %s %p "
70
 
                 "inode# %s to a pathname. Error %d\n",
71
 
                 dtype,
72
 
                 dentry,
73
 
                 buf,
74
 
                 error);
75
 
}
76
 
 
77
 
/**
78
 
 * sd_taskattr_access:
79
 
 * @name: name of file to check permission
80
 
 * @mask: permission mask requested for file
81
 
 *
82
 
 * Determine if request is for write access to /proc/self/attr/current
83
 
 */
84
 
static inline int sd_taskattr_access(const char *procrelname)
85
 
{
86
 
/*
87
 
 * assumes a 32bit pid, which requires max 10 decimal digits to represent
88
 
 * sizeof includes trailing \0
89
 
 */
90
 
        char buf[sizeof("/attr/current") + 10];
91
 
        const int maxbuflen = sizeof(buf);
92
 
 
93
 
        snprintf(buf, maxbuflen, "%d/attr/current", current->pid);
94
 
        buf[maxbuflen - 1] = 0;
95
 
 
96
 
        return strcmp(buf, procrelname) == 0;
97
 
}
98
 
 
99
 
/**
100
 
 * sd_file_mode - get full mode for file entry from profile
101
 
 * @profile: profile
102
 
 * @name: filename
103
 
 */
104
 
static inline int sd_file_mode(struct sdprofile *profile, const char *name)
105
 
{
106
 
        struct sd_entry *entry;
107
 
        int mode = 0;
108
 
 
109
 
        SD_DEBUG("%s: %s\n", __FUNCTION__, name);
110
 
        if (!name) {
111
 
                SD_DEBUG("%s: no name\n", __FUNCTION__);
112
 
                goto out;
113
 
        }
114
 
 
115
 
        if (!profile) {
116
 
                SD_DEBUG("%s: no profile\n", __FUNCTION__);
117
 
                goto out;
118
 
        }
119
 
        list_for_each_entry(entry, &profile->file_entry, list) {
120
 
                if (sdmatch_match(name, entry->filename,
121
 
                                  entry->entry_type, entry->extradata))
122
 
                        mode |= entry->mode;
123
 
        }
124
 
out:
125
 
        return mode;
126
 
}
127
 
 
128
 
/**
129
 
 * sd_get_execmode - calculate what qualifier to apply to an exec
130
 
 * @sd: subdomain to search
131
 
 * @name: name of file to exec
132
 
 * @xmod: pointer to a execution mode bit for the rule that was matched
133
 
 *         if the rule has no execuition qualifier {pui} then
134
 
 *         SD_MAY_EXEC is returned indicating a naked x
135
 
 *         if the has an exec qualifier then only the qualifier bit {pui}
136
 
 *         is returned (SD_MAY_EXEC) is not set.
137
 
 * @unsafe: true if secure_exec should be overridden
138
 
 *
139
 
 * Returns 0 (false):
140
 
 *    if unable to find profile or there are conflicting pattern matches.
141
 
 *       *xmod - is not modified
142
 
 *       *unsafe - is not modified
143
 
 *
144
 
 * Returns 1 (true):
145
 
 *    if not confined
146
 
 *       *xmod = SD_MAY_EXEC
147
 
 *       *unsafe = 0
148
 
 *    if exec rule matched
149
 
 *       if the rule has an execution mode qualifier {pui} then
150
 
 *          *xmod = the execution qualifier of the rule {pui}
151
 
 *       else
152
 
 *          *xmod = SD_MAY_EXEC
153
 
 *       unsafe = presence of unsafe flag
154
 
 */
155
 
static inline int sd_get_execmode(struct subdomain *sd, const char *name,
156
 
                                  int *xmod, int *unsafe)
157
 
{
158
 
        struct sdprofile *profile;
159
 
        struct sd_entry *entry;
160
 
        struct sd_entry *match = NULL;
161
 
 
162
 
        int pattern_match_invalid = 0, rc = 0;
163
 
 
164
 
        /* not confined */
165
 
        if (!__sd_is_confined(sd)) {
166
 
                SD_DEBUG("%s: not confined\n", __FUNCTION__);
167
 
                goto not_confined;
168
 
        }
169
 
 
170
 
        profile = sd->active;
171
 
 
172
 
        /* search list of profiles with 'x' permission
173
 
         * this will also include entries with 'p', 'u' and 'i'
174
 
         * qualifiers.
175
 
         *
176
 
         * If we find a pattern match we will keep looking for an exact match
177
 
         * If we find conflicting pattern matches we will flag (while still
178
 
         * looking for an exact match).  If all we have is a conflict, FALSE
179
 
         * is returned.
180
 
         */
181
 
 
182
 
        list_for_each_entry(entry, &profile->file_entryp[POS_SD_MAY_EXEC],
183
 
                            listp[POS_SD_MAY_EXEC]) {
184
 
                if (!pattern_match_invalid &&
185
 
                    entry->entry_type == sd_entry_pattern &&
186
 
                    sdmatch_match(name, entry->filename,
187
 
                                  entry->entry_type, entry->extradata)) {
188
 
                        if (match &&
189
 
                            SD_EXEC_UNSAFE_MASK(entry->mode) !=
190
 
                            SD_EXEC_UNSAFE_MASK(match->mode))
191
 
                                pattern_match_invalid = 1;
192
 
                        else
193
 
                                /* keep searching for an exact match */
194
 
                                match = entry;
195
 
                } else if ((entry->entry_type == sd_entry_literal ||
196
 
                            (!pattern_match_invalid &&
197
 
                             entry->entry_type == sd_entry_tailglob)) &&
198
 
                            sdmatch_match(name, entry->filename,
199
 
                                          entry->entry_type,
200
 
                                          entry->extradata)) {
201
 
                        if (entry->entry_type == sd_entry_literal) {
202
 
                                /* got an exact match -- there can be only
203
 
                                 * one, asserted at profile load time
204
 
                                 */
205
 
                                match = entry;
206
 
                                pattern_match_invalid = 0;
207
 
                                break;
208
 
                        } else {
209
 
                                if (match &&
210
 
                                    SD_EXEC_UNSAFE_MASK(entry->mode) !=
211
 
                                    SD_EXEC_UNSAFE_MASK(match->mode))
212
 
                                        pattern_match_invalid = 1;
213
 
                                else
214
 
                                        /* got a tailglob match, keep searching
215
 
                                         * for an exact match
216
 
                                         */
217
 
                                        match = entry;
218
 
                        }
219
 
                }
220
 
 
221
 
        }
222
 
 
223
 
        rc = match && !pattern_match_invalid;
224
 
 
225
 
        if (rc) {
226
 
                int mode = SD_EXEC_MASK(match->mode);
227
 
 
228
 
                /* check for qualifiers, if present
229
 
                 * we just return the qualifier
230
 
                 */
231
 
                if (mode & ~SD_MAY_EXEC)
232
 
                        mode = mode & ~SD_MAY_EXEC;
233
 
 
234
 
                *xmod = mode;
235
 
                *unsafe = (match->mode & SD_EXEC_UNSAFE);
236
 
        } else if (!match) {
237
 
                SD_DEBUG("%s: Unable to find execute entry in profile "
238
 
                         "for image '%s'\n",
239
 
                         __FUNCTION__,
240
 
                         name);
241
 
        } else if (pattern_match_invalid) {
242
 
                SD_WARN("%s: Inconsistency in profile %s. "
243
 
                        "Two (or more) patterns specify conflicting exec "
244
 
                        "qualifiers ('u', 'i' or 'p') for image %s\n",
245
 
                        __FUNCTION__,
246
 
                        sd->active->name,
247
 
                        name);
248
 
        }
249
 
 
250
 
        return rc;
251
 
 
252
 
not_confined:
253
 
        *xmod = SD_MAY_EXEC;
254
 
        *unsafe = 0;
255
 
        return 1;
256
 
}
257
 
 
258
 
/**
259
 
 * sd_filter_mask
260
 
 * @mask: requested mask
261
 
 * @inode: potential directory inode
262
 
 *
263
 
 * This fn performs pre-verification of the requested mask
264
 
 * We ignore append. Previously we required 'w' on a dir to add a file.
265
 
 * No longer. Now we require 'w' on just the file itself. Traversal 'x' is
266
 
 * also ignored for directories.
267
 
 *
268
 
 * Returned value of 0 indicates no need to perform a perm check.
269
 
 */
270
 
static inline int sd_filter_mask(int mask, struct inode *inode)
271
 
{
272
 
        if (mask) {
273
 
                int elim = MAY_APPEND;
274
 
 
275
 
                if (inode && S_ISDIR(inode->i_mode))
276
 
                        elim |= (MAY_EXEC | MAY_WRITE);
277
 
 
278
 
                mask &= ~elim;
279
 
        }
280
 
 
281
 
        return mask;
282
 
}
283
 
 
284
 
static inline void sd_permerror2result(int perm_result, struct sd_audit *sa)
285
 
{
286
 
        if (perm_result == 0) { /* success */
287
 
                sa->result = 1;
288
 
                sa->errorcode = 0;
289
 
        } else { /* -ve internal error code or +ve mask of denied perms */
290
 
                sa->result = 0;
291
 
                sa->errorcode = perm_result;
292
 
        }
293
 
}
294
 
 
295
 
/*************************
296
 
 * MAIN INTERNAL FUNCTIONS
297
 
 ************************/
298
 
 
299
 
/**
300
 
 * sd_file_perm - calculate access mode for file
301
 
 * @subdomain: current subdomain
302
 
 * @name: name of file to calculate mode for
303
 
 * @mask: permission mask requested for file
304
 
 *
305
 
 * Search the sd_entry list in @profile.
306
 
 * Search looking to verify all permissions passed in mask.
307
 
 * Perform the search by looking at the partitioned list of entries, one
308
 
 * partition per permission bit.
309
 
 *
310
 
 * Return 0 on success, else mask of non-allowed permissions
311
 
 */
312
 
static unsigned int sd_file_perm(struct subdomain *sd, const char *name,
313
 
                                 int mask)
314
 
{
315
 
        struct sdprofile *profile;
316
 
        int i, error = 0, mode;
317
 
 
318
 
#define PROCPFX "/proc/"
319
 
#define PROCLEN sizeof(PROCPFX) - 1
320
 
 
321
 
        SD_DEBUG("%s: %s 0x%x\n", __FUNCTION__, name, mask);
322
 
 
323
 
        /* should not enter with other than R/W/M/X/L */
324
 
        BUG_ON(mask &
325
 
               ~(SD_MAY_READ | SD_MAY_WRITE | SD_MAY_EXEC |
326
 
                 SD_EXEC_MMAP | SD_MAY_LINK));
327
 
 
328
 
        /* not confined */
329
 
        if (!__sd_is_confined(sd)) {
330
 
                /* exit with access allowed */
331
 
                SD_DEBUG("%s: not confined\n", __FUNCTION__);
332
 
                goto done;
333
 
        }
334
 
 
335
 
        /* Special case access to /proc/self/attr/current
336
 
         * Currently we only allow access if opened O_WRONLY
337
 
         */
338
 
        if (mask == MAY_WRITE && strncmp(PROCPFX, name, PROCLEN) == 0 &&
339
 
            sd_taskattr_access(name + PROCLEN))
340
 
                goto done;
341
 
 
342
 
        profile = sd->active;
343
 
 
344
 
        mode = 0;
345
 
 
346
 
        /* iterate over partition, one permission bit at a time */
347
 
        for (i = 0; i <= POS_SD_FILE_MAX; i++) {
348
 
                struct sd_entry *entry;
349
 
 
350
 
                /* do we have to accumulate this bit?
351
 
                 * or have we already accumulated it (shortcut below)? */
352
 
                if (!(mask & (1 << i)) || mode & (1 << i))
353
 
                        continue;
354
 
 
355
 
                list_for_each_entry(entry, &profile->file_entryp[i],
356
 
                                    listp[i]) {
357
 
                        if (sdmatch_match(name, entry->filename,
358
 
                                entry->entry_type, entry->extradata)) {
359
 
                                /* Shortcut, accumulate all bits present */
360
 
                                mode |= entry->mode;
361
 
 
362
 
                                /*
363
 
                                 * Mask bits are overloaded
364
 
                                 * MAY_{EXEC,WRITE,READ,APPEND} are used by
365
 
                                 * kernel, other values are used locally only.
366
 
                                 */
367
 
                                if ((mode & mask) == mask) {
368
 
                                        SD_DEBUG("MATCH! %s=0x%x [total mode=0x%x]\n",
369
 
                                                 name, mask, mode);
370
 
 
371
 
                                        goto done;
372
 
                                }
373
 
                        }
374
 
                }
375
 
        }
376
 
 
377
 
        /* return permissions not satisfied */
378
 
        error = mask & ~mode;
379
 
 
380
 
done:
381
 
        return error;
382
 
}
383
 
 
384
 
/**
385
 
 * sd_link_perm - test permission to link to a file
386
 
 * @sd: current subdomain
387
 
 * @link: name of link being created
388
 
 * @target: name of target to be linked to
389
 
 *
390
 
 * Look up permission mode on both @link and @target.  @link must have same
391
 
 * permission mode as @target.  At least @link must have the link bit enabled.
392
 
 * Return 0 on success, error otherwise.
393
 
 */
394
 
static int sd_link_perm(struct subdomain *sd,
395
 
                        const char *link, const char *target)
396
 
{
397
 
        int l_mode, t_mode, ret;
398
 
        struct sdprofile *profile = sd->active;
399
 
 
400
 
        l_mode = sd_file_mode(profile, link);
401
 
        if (l_mode & SD_MAY_LINK) {
402
 
                /* mask off link bit */
403
 
                l_mode &= ~SD_MAY_LINK;
404
 
 
405
 
                t_mode = sd_file_mode(profile, target);
406
 
                t_mode &= ~SD_MAY_LINK;
407
 
 
408
 
                ret = (l_mode == t_mode);
409
 
        } else {
410
 
                ret = 0;
411
 
        }
412
 
 
413
 
        return ret;
414
 
}
415
 
 
416
 
/**
417
 
 * _sd_perm_dentry
418
 
 * @sd: current subdomain
419
 
 * @dentry: requested dentry
420
 
 * @mask: mask of requested operations
421
 
 * @pname: pointer to hold matched pathname (if any)
422
 
 *
423
 
 * Helper function.  Obtain pathname for specified dentry. Verify if profile
424
 
 * authorizes mask operations on pathname (due to lack of vfsmnt it is sadly
425
 
 * necessary to search mountpoints in namespace -- when nameidata is passed
426
 
 * more fully, this code can go away).  If more than one mountpoint matches
427
 
 * but none satisfy the profile, only the first pathname (mountpoint) is
428
 
 * returned for subsequent logging.
429
 
 *
430
 
 * Return 0 (success), +ve (mask of permissions not satisfied) or -ve (system
431
 
 * error, most likely -ENOMEM).
432
 
 */
433
 
static int _sd_perm_dentry(struct subdomain *sd, struct dentry *dentry,
434
 
                           int mask, const char **pname)
435
 
{
436
 
        char *name = NULL, *failed_name = NULL;
437
 
        struct sd_path_data data;
438
 
        int error = 0, failed_error = 0, sdpath_error,
439
 
            sdcomplain = SUBDOMAIN_COMPLAIN(sd);
440
 
 
441
 
        /* search all paths to dentry */
442
 
 
443
 
        sd_path_begin(dentry, &data);
444
 
        do {
445
 
                name = sd_path_getname(&data);
446
 
                if (name) {
447
 
                        /* error here is 0 (success) or +ve (mask of perms) */
448
 
                        error = sd_file_perm(sd, name, mask);
449
 
 
450
 
                        /* access via any path is enough */
451
 
                        if (sdcomplain || error == 0)
452
 
                                break; /* Caller must free name */
453
 
 
454
 
                        /* Already have an path that failed? */
455
 
                        if (failed_name) {
456
 
                                sd_put_name(name);
457
 
                        } else {
458
 
                                failed_name = name;
459
 
                                failed_error = error;
460
 
                        }
461
 
                }
462
 
        } while (name);
463
 
 
464
 
        if ((sdpath_error = sd_path_end(&data)) != 0) {
465
 
                dentry_xlate_error(dentry, sdpath_error, "dentry");
466
 
 
467
 
                WARN_ON(name);  /* name should not be set if error */
468
 
                error = sdpath_error;
469
 
                name = NULL;
470
 
        } else if (name) {
471
 
                if (failed_name)
472
 
                        sd_put_name(failed_name);
473
 
        } else {
474
 
                name = failed_name;
475
 
                error = failed_error;
476
 
        }
477
 
 
478
 
        *pname = name;
479
 
 
480
 
        return error;
481
 
}
482
 
 
483
 
/**************************
484
 
 * GLOBAL UTILITY FUNCTIONS
485
 
 *************************/
486
 
 
487
 
/**
488
 
 * alloc_nullprofiles - Allocate null profiles
489
 
 */
490
 
int alloc_nullprofiles(void)
491
 
{
492
 
        null_profile = alloc_sdprofile();
493
 
        null_complain_profile = alloc_sdprofile();
494
 
 
495
 
        if (!null_profile || !null_complain_profile)
496
 
                goto fail;
497
 
 
498
 
        null_profile->name = kstrdup("null-profile", GFP_KERNEL);
499
 
        null_complain_profile->name =
500
 
                kstrdup("null-complain-profile", GFP_KERNEL);
501
 
 
502
 
        if (!null_profile->name ||
503
 
            !null_complain_profile->name)
504
 
                goto fail;
505
 
 
506
 
        get_sdprofile(null_profile);
507
 
        get_sdprofile(null_complain_profile);
508
 
        null_complain_profile->flags.complain = 1;
509
 
 
510
 
        return 1;
511
 
 
512
 
fail:
513
 
        /* free_sdprofile is safe for freeing partially constructed objects */
514
 
        free_sdprofile(null_profile);
515
 
        free_sdprofile(null_complain_profile);
516
 
        null_profile = null_complain_profile = NULL;
517
 
        return 0;
518
 
}
519
 
 
520
 
/**
521
 
 * free_nullprofiles - Free null profiles
522
 
 */
523
 
void free_nullprofiles(void)
524
 
{
525
 
        put_sdprofile(null_complain_profile);
526
 
        put_sdprofile(null_profile);
527
 
        null_profile = null_complain_profile = NULL;
528
 
}
529
 
 
530
 
/**
531
 
 * sd_audit_message - Log a message to the audit subsystem
532
 
 * @sd: current subdomain
533
 
 * @gfp: allocation flags
534
 
 * @flags: audit flags
535
 
 * @fmt: varargs fmt
536
 
 */
537
 
int sd_audit_message(struct subdomain *sd, unsigned int gfp, int flags,
538
 
                     const char *fmt, ...)
539
 
{
540
 
        int ret;
541
 
        struct sd_audit sa;
542
 
 
543
 
        sa.type = SD_AUDITTYPE_MSG;
544
 
        sa.name = fmt;
545
 
        va_start(sa.vaval, fmt);
546
 
        sa.flags = flags;
547
 
        sa.gfp_mask = gfp;
548
 
        sa.errorcode = 0;
549
 
        sa.result = 0;  /* fake failure: force message to be logged */
550
 
 
551
 
        ret = sd_audit(sd, &sa);
552
 
 
553
 
        va_end(sa.vaval);
554
 
 
555
 
        return ret;
556
 
}
557
 
 
558
 
/**
559
 
 * sd_audit_syscallreject - Log a syscall rejection to the audit subsystem
560
 
 * @sd: current subdomain
561
 
 * @msg: string describing syscall being rejected
562
 
 * @gfp: memory allocation flags
563
 
 */
564
 
int sd_audit_syscallreject(struct subdomain *sd, unsigned int gfp,
565
 
                           enum aasyscall call)
566
 
{
567
 
        struct sd_audit sa;
568
 
        int error = -EPERM;
569
 
 
570
 
        if (!syscall_is_cached(call)) {
571
 
                sa.type = SD_AUDITTYPE_SYSCALL;
572
 
                sa.name = syscall_to_name(call);
573
 
                sa.flags = 0;
574
 
                sa.gfp_mask = gfp;
575
 
                sa.errorcode = 0;
576
 
                sa.result = 0; /* failure */
577
 
 
578
 
                error = sd_audit(sd, &sa);
579
 
                if (error == -EPERM)
580
 
                        add_to_cached_syscalls(call);
581
 
        }
582
 
        return error;
583
 
}
584
 
 
585
 
/**
586
 
 * sd_audit - Log an audit event to the audit subsystem
587
 
 * @sd: current subdomain
588
 
 * @sa: audit event
589
 
 */
590
 
int sd_audit(struct subdomain *sd, const struct sd_audit *sa)
591
 
{
592
 
        struct audit_buffer *ab = NULL;
593
 
        struct audit_context *ctx;
594
 
 
595
 
        const char *logcls;
596
 
        unsigned int flags;
597
 
        int sdaudit = 0,
598
 
            sdcomplain = 0,
599
 
            error = -EINVAL,
600
 
            opspec_error = -EACCES;
601
 
 
602
 
        const unsigned int gfp_mask = sa->gfp_mask;
603
 
 
604
 
        WARN_ON(sa->type >= SD_AUDITTYPE__END);
605
 
 
606
 
        /*
607
 
         * sa->result:    1 success, 0 failure
608
 
         * sa->errorcode: success: 0
609
 
         *                failure: +ve mask of failed permissions or -ve
610
 
         *                system error
611
 
         */
612
 
 
613
 
        if (likely(sa->result)) {
614
 
                if (likely(!SUBDOMAIN_AUDIT(sd))) {
615
 
                        /* nothing to log */
616
 
                        error = 0;
617
 
                        goto out;
618
 
                } else {
619
 
                        sdaudit = 1;
620
 
                        logcls = "AUDITING";
621
 
                }
622
 
        } else if (sa->errorcode < 0) {
623
 
                audit_log(current->audit_context, gfp_mask, AUDIT_SD,
624
 
                        "Internal error auditing event type %d (error %d)",
625
 
                        sa->type, sa->errorcode);
626
 
                SD_ERROR("Internal error auditing event type %d (error %d)\n",
627
 
                        sa->type, sa->errorcode);
628
 
                error = sa->errorcode;
629
 
                goto out;
630
 
        } else if (sa->type == SD_AUDITTYPE_SYSCALL) {
631
 
                /* Currently SD_AUDITTYPE_SYSCALL is for rejects only.
632
 
                 * Values set by sd_audit_syscallreject will get us here.
633
 
                 */
634
 
                logcls = "REJECTING";
635
 
        } else {
636
 
                sdcomplain = SUBDOMAIN_COMPLAIN(sd);
637
 
                logcls = sdcomplain ? "PERMITTING" : "REJECTING";
638
 
        }
639
 
 
640
 
        /* test if event has already been logged and cached used to log
641
 
         * only first time event occurs.
642
 
         */
643
 
        if (sa->type == SD_AUDITTYPE_CAP) {
644
 
                if (cap_is_cached(sa->ival)) {
645
 
                        opspec_error = -EPERM;
646
 
                        goto skip_logging;
647
 
                }
648
 
        }
649
 
 
650
 
        /* In future extend w/ per-profile flags
651
 
         * (flags |= sa->active->flags)
652
 
         */
653
 
        flags = sa->flags;
654
 
        if (subdomain_logsyscall)
655
 
                flags |= SD_AUDITFLAG_AUDITSS_SYSCALL;
656
 
 
657
 
 
658
 
        /* Force full audit syscall logging regardless of global setting if
659
 
         * we are rejecting a syscall
660
 
         */
661
 
        if (sa->type == SD_AUDITTYPE_SYSCALL) {
662
 
                ctx = current->audit_context;
663
 
        } else {
664
 
                ctx = (flags & SD_AUDITFLAG_AUDITSS_SYSCALL) ?
665
 
                        current->audit_context : NULL;
666
 
        }
667
 
 
668
 
        ab = audit_log_start(ctx, gfp_mask, AUDIT_SD);
669
 
 
670
 
        if (!ab) {
671
 
                SD_ERROR("Unable to log event (%d) to audit subsys\n",
672
 
                        sa->type);
673
 
                if (sdcomplain)
674
 
                        error = 0;
675
 
                goto out;
676
 
        }
677
 
 
678
 
        /* messages get special handling */
679
 
        if (sa->type == SD_AUDITTYPE_MSG) {
680
 
                audit_log_vformat(ab, sa->name, sa->vaval);
681
 
                audit_log_end(ab);
682
 
                error = 0;
683
 
                goto out;
684
 
        }
685
 
 
686
 
        /* log operation */
687
 
 
688
 
        audit_log_format(ab, "%s ", logcls);    /* REJECTING/ALLOWING/etc */
689
 
 
690
 
        if (sa->type == SD_AUDITTYPE_FILE) {
691
 
                int perm = sdaudit ? sa->ival : sa->errorcode;
692
 
 
693
 
                audit_log_format(ab, "%s%s%s%s%s access to %s ",
694
 
                        perm & SD_EXEC_MMAP ? "m" : "",
695
 
                        perm & SD_MAY_READ  ? "r" : "",
696
 
                        perm & SD_MAY_WRITE ? "w" : "",
697
 
                        perm & SD_MAY_EXEC  ? "x" : "",
698
 
                        perm & SD_MAY_LINK  ? "l" : "",
699
 
                        sa->name);
700
 
 
701
 
                opspec_error = -EPERM;
702
 
 
703
 
        } else if (sa->type == SD_AUDITTYPE_DIR) {
704
 
                audit_log_format(ab, "%s on %s ",
705
 
                        sa->ival == SD_DIR_MKDIR ? "mkdir" : "rmdir",
706
 
                        sa->name);
707
 
 
708
 
        } else if (sa->type == SD_AUDITTYPE_ATTR) {
709
 
                struct iattr *iattr = (struct iattr*)sa->pval;
710
 
 
711
 
                audit_log_format(ab,
712
 
                        "attribute (%s%s%s%s%s%s%s) change to %s ",
713
 
                        iattr->ia_valid & ATTR_MODE ? "mode," : "",
714
 
                        iattr->ia_valid & ATTR_UID ? "uid," : "",
715
 
                        iattr->ia_valid & ATTR_GID ? "gid," : "",
716
 
                        iattr->ia_valid & ATTR_SIZE ? "size," : "",
717
 
                        ((iattr->ia_valid & ATTR_ATIME_SET) ||
718
 
                         (iattr->ia_valid & ATTR_ATIME)) ? "atime," : "",
719
 
                        ((iattr->ia_valid & ATTR_MTIME_SET) ||
720
 
                         (iattr->ia_valid & ATTR_MTIME)) ? "mtime," : "",
721
 
                        iattr->ia_valid & ATTR_CTIME ? "ctime," : "",
722
 
                        sa->name);
723
 
 
724
 
        } else if (sa->type == SD_AUDITTYPE_XATTR) {
725
 
                const char *fmt;
726
 
                switch (sa->ival) {
727
 
                        case SD_XATTR_GET:
728
 
                                fmt = "xattr get";
729
 
                                break;
730
 
                        case SD_XATTR_SET:
731
 
                                fmt = "xattr set";
732
 
                                break;
733
 
                        case SD_XATTR_LIST:
734
 
                                fmt = "xattr list";
735
 
                                break;
736
 
                        case SD_XATTR_REMOVE:
737
 
                                fmt = "xattr remove";
738
 
                                break;
739
 
                        default:
740
 
                                fmt = "xattr <unknown>";
741
 
                                break;
742
 
                }
743
 
 
744
 
                audit_log_format(ab, "%s on %s ", fmt, sa->name);
745
 
 
746
 
        } else if (sa->type == SD_AUDITTYPE_LINK) {
747
 
                audit_log_format(ab,
748
 
                        "link access from %s to %s ",
749
 
                        sa->name,
750
 
                        (char*)sa->pval);
751
 
 
752
 
        } else if (sa->type == SD_AUDITTYPE_CAP) {
753
 
                audit_log_format(ab,
754
 
                        "access to capability '%s' ",
755
 
                        capability_to_name(sa->ival));
756
 
                add_to_cached_caps(sa->ival);
757
 
                opspec_error = -EPERM;
758
 
        } else if (sa->type == SD_AUDITTYPE_SYSCALL) {
759
 
                audit_log_format(ab, "access to syscall '%s' ", sa->name);
760
 
 
761
 
                opspec_error = -EPERM;
762
 
        } else {
763
 
                /* -EINVAL -- will WARN_ON above */
764
 
                goto out;
765
 
        }
766
 
 
767
 
        audit_log_format(ab, "(%s(%d) profile %s active %s)",
768
 
                current->comm, current->pid,
769
 
                sd->profile->name, sd->active->name);
770
 
 
771
 
        audit_log_end(ab);
772
 
 
773
 
skip_logging:
774
 
        if (sdcomplain)
775
 
                error = 0;
776
 
        else
777
 
                error = sa->result ? 0 : opspec_error;
778
 
 
779
 
out:
780
 
        return error;
781
 
}
782
 
 
783
 
/**
784
 
 * sd_get_name - retrieve fully qualified path name
785
 
 * @dentry: relative path element
786
 
 * @mnt: where in tree
787
 
 *
788
 
 * Returns fully qualified path name on sucess, NULL on failure.
789
 
 * sd_put_name must be used to free allocated buffer.
790
 
 */
791
 
char *sd_get_name(struct dentry *dentry, struct vfsmount *mnt)
792
 
{
793
 
        char *page, *name;
794
 
 
795
 
        page = (char *)__get_free_page(GFP_KERNEL);
796
 
        if (!page) {
797
 
                name = ERR_PTR(-ENOMEM);
798
 
                goto out;
799
 
        }
800
 
 
801
 
        name = d_path(dentry, mnt, page, PAGE_SIZE);
802
 
 
803
 
        /* check for (deleted) that d_path appends to pathnames if the dentry
804
 
         * has been removed from the cache.
805
 
         * The size > deleted_size and strcmp checks are redundant safe guards.
806
 
         */
807
 
        if (IS_ERR(name)) {
808
 
                free_page((unsigned long)page);
809
 
        } else {
810
 
                const char deleted_str[] = " (deleted)";
811
 
                const size_t deleted_size = sizeof(deleted_str) - 1;
812
 
                size_t size;
813
 
                size = strlen(name);
814
 
                if (!IS_ROOT(dentry) && d_unhashed(dentry) &&
815
 
                    size > deleted_size &&
816
 
                    strcmp(name + size - deleted_size, deleted_str) == 0)
817
 
                        name[size - deleted_size] = '\0';
818
 
 
819
 
                SD_DEBUG("%s: full_path=%s\n", __FUNCTION__, name);
820
 
        }
821
 
 
822
 
out:
823
 
        return name;
824
 
}
825
 
 
826
 
/***********************************
827
 
 * GLOBAL PERMISSION CHECK FUNCTIONS
828
 
 ***********************************/
829
 
 
830
 
/**
831
 
 * sd_attr - check whether attribute change allowed
832
 
 * @sd: subdomain to check against to check against
833
 
 * @dentry: file to check
834
 
 * @iattr: attribute changes requested
835
 
 */
836
 
int sd_attr(struct subdomain *sd, struct dentry *dentry, struct iattr *iattr)
837
 
{
838
 
        int error = 0, permerror;
839
 
        struct sd_audit sa;
840
 
 
841
 
        if (!__sd_is_confined(sd))
842
 
                goto out;
843
 
 
844
 
        sa.type = SD_AUDITTYPE_ATTR;
845
 
        sa.pval = iattr;
846
 
        sa.flags = 0;
847
 
        sa.gfp_mask = GFP_KERNEL;
848
 
 
849
 
        permerror = _sd_perm_dentry(sd, dentry, MAY_WRITE, &sa.name);
850
 
        sd_permerror2result(permerror, &sa);
851
 
 
852
 
        error = sd_audit(sd, &sa);
853
 
 
854
 
        sd_put_name(sa.name);
855
 
 
856
 
out:
857
 
        return error;
858
 
}
859
 
 
860
 
int sd_xattr(struct subdomain *sd, struct dentry *dentry, const char *xattr,
861
 
             int xattroptype)
862
 
{
863
 
        int error = 0, permerror, mask = 0;
864
 
        struct sd_audit sa;
865
 
 
866
 
        /* if not confined or empty mask permission granted */
867
 
        if (!__sd_is_confined(sd))
868
 
                goto out;
869
 
 
870
 
        if (xattroptype == SD_XATTR_GET || xattroptype == SD_XATTR_LIST)
871
 
                mask = MAY_READ;
872
 
        else if (xattroptype == SD_XATTR_SET || xattroptype == SD_XATTR_REMOVE)
873
 
                mask = MAY_WRITE;
874
 
 
875
 
        sa.type = SD_AUDITTYPE_XATTR;
876
 
        sa.ival = xattroptype;
877
 
        sa.pval = xattr;
878
 
        sa.flags = 0;
879
 
        sa.gfp_mask = GFP_KERNEL;
880
 
 
881
 
        permerror = _sd_perm_dentry(sd, dentry, mask, &sa.name);
882
 
        sd_permerror2result(permerror, &sa);
883
 
 
884
 
        error = sd_audit(sd, &sa);
885
 
 
886
 
        sd_put_name(sa.name);
887
 
 
888
 
out:
889
 
        return error;
890
 
}
891
 
 
892
 
/**
893
 
 * sd_perm - basic subdomain permissions check
894
 
 * @sd: subdomain to check against
895
 
 * @dentry: dentry
896
 
 * @mnt: mountpoint
897
 
 * @mask: access mode requested
898
 
 *
899
 
 * Determine if access (mask) for dentry is authorized by subdomain sd.
900
 
 * Result, 0 (success), -ve (error)
901
 
 */
902
 
int sd_perm(struct subdomain *sd, struct dentry *dentry, struct vfsmount *mnt,
903
 
            int mask)
904
 
{
905
 
        int error = 0, permerror;
906
 
        struct sd_audit sa;
907
 
 
908
 
        if (!__sd_is_confined(sd))
909
 
                goto out;
910
 
 
911
 
        if ((mask = sd_filter_mask(mask, dentry->d_inode)) == 0)
912
 
                goto out;
913
 
 
914
 
        sa.type = SD_AUDITTYPE_FILE;
915
 
        sa.name = sd_get_name(dentry, mnt);
916
 
        sa.ival = mask;
917
 
        sa.flags = 0;
918
 
        sa.gfp_mask = GFP_KERNEL;
919
 
 
920
 
        if (IS_ERR(sa.name)) {
921
 
                permerror = PTR_ERR(sa.name);
922
 
                sa.name = NULL;
923
 
        } else {
924
 
                permerror = sd_file_perm(sd, sa.name, mask);
925
 
        }
926
 
 
927
 
        sd_permerror2result(permerror, &sa);
928
 
 
929
 
        error = sd_audit(sd, &sa);
930
 
 
931
 
        sd_put_name(sa.name);
932
 
 
933
 
out:
934
 
        return error;
935
 
}
936
 
 
937
 
/**
938
 
 * sd_perm_nameidata: interface to sd_perm accepting nameidata
939
 
 * @sd: subdomain to check against
940
 
 * @nd: namespace data (for vfsmnt and dentry)
941
 
 * @mask: access mode requested
942
 
 */
943
 
int sd_perm_nameidata(struct subdomain *sd, struct nameidata *nd, int mask)
944
 
{
945
 
        int error = 0;
946
 
 
947
 
        if (nd)
948
 
                error = sd_perm(sd, nd->dentry, nd->mnt, mask);
949
 
 
950
 
        return error;
951
 
}
952
 
 
953
 
/**
954
 
 * sd_perm_dentry - file permissions interface when no vfsmnt available
955
 
 * @sd: current subdomain
956
 
 * @dentry: requested dentry
957
 
 * @mask: access mode requested
958
 
 *
959
 
 * Determine if access (mask) for dentry is authorized by subdomain sd.
960
 
 * Result, 0 (success), -ve (error)
961
 
 */
962
 
int sd_perm_dentry(struct subdomain *sd, struct dentry *dentry, int mask)
963
 
{
964
 
        int error = 0, permerror;
965
 
        struct sd_audit sa;
966
 
 
967
 
        if (!__sd_is_confined(sd))
968
 
                goto out;
969
 
 
970
 
        if ((mask = sd_filter_mask(mask, dentry->d_inode)) == 0)
971
 
                goto out;
972
 
 
973
 
        sa.type = SD_AUDITTYPE_FILE;
974
 
        sa.ival = mask;
975
 
        sa.flags = 0;
976
 
        sa.gfp_mask = GFP_KERNEL;
977
 
 
978
 
        permerror = _sd_perm_dentry(sd, dentry, mask, &sa.name);
979
 
        sd_permerror2result(permerror, &sa);
980
 
 
981
 
        error = sd_audit(sd, &sa);
982
 
 
983
 
        sd_put_name(sa.name);
984
 
 
985
 
out:
986
 
        return error;
987
 
}
988
 
 
989
 
/**
990
 
 * sd_perm_dir
991
 
 * @sd: current subdomain
992
 
 * @dentry: requested dentry
993
 
 * @mode: SD_DIR_MKDIR or SD_DIR_RMDIR
994
 
 *
995
 
 * Determine if directory operation (make/remove) for dentry is authorized
996
 
 * by subdomain sd.
997
 
 * Result, 0 (success), -ve (error)
998
 
 */
999
 
int sd_perm_dir(struct subdomain *sd, struct dentry *dentry, int diroptype)
1000
 
{
1001
 
        int error = 0, permerror, mask;
1002
 
        struct sd_audit sa;
1003
 
 
1004
 
        BUG_ON(diroptype != SD_DIR_MKDIR && diroptype != SD_DIR_RMDIR);
1005
 
 
1006
 
        if (!__sd_is_confined(sd))
1007
 
                goto out;
1008
 
 
1009
 
        mask = MAY_WRITE;
1010
 
 
1011
 
        sa.type = SD_AUDITTYPE_DIR;
1012
 
        sa.ival = diroptype;
1013
 
        sa.flags = 0;
1014
 
        sa.gfp_mask = GFP_KERNEL;
1015
 
 
1016
 
        permerror = _sd_perm_dentry(sd, dentry, mask, &sa.name);
1017
 
        sd_permerror2result(permerror, &sa);
1018
 
 
1019
 
        error = sd_audit(sd, &sa);
1020
 
 
1021
 
        sd_put_name(sa.name);
1022
 
 
1023
 
out:
1024
 
        return error;
1025
 
}
1026
 
 
1027
 
/**
1028
 
 * sd_capability - test permission to use capability
1029
 
 * @sd: subdomain to check against
1030
 
 * @cap: capability to be tested
1031
 
 *
1032
 
 * Look up capability in active profile capability set.
1033
 
 * Return 0 (success), -EPERM (error)
1034
 
 */
1035
 
int sd_capability(struct subdomain *sd, int cap)
1036
 
{
1037
 
        int error = 0;
1038
 
 
1039
 
        if (__sd_is_confined(sd)) {
1040
 
                struct sd_audit sa;
1041
 
 
1042
 
                sa.type = SD_AUDITTYPE_CAP;
1043
 
                sa.name = NULL;
1044
 
                sa.ival = cap;
1045
 
                sa.flags = 0;
1046
 
                sa.errorcode = 0;
1047
 
                sa.result = cap_raised(sd->active->capabilities, cap);
1048
 
                sa.gfp_mask = GFP_ATOMIC;
1049
 
 
1050
 
                error = sd_audit(sd, &sa);
1051
 
        }
1052
 
 
1053
 
        return error;
1054
 
}
1055
 
 
1056
 
/**
1057
 
 * sd_link - hard link check
1058
 
 * @link: dentry for link being created
1059
 
 * @target: dentry for link target
1060
 
 * @sd: subdomain to check against
1061
 
 *
1062
 
 * Checks link permissions for all possible name combinations.  This is
1063
 
 * particularly ugly.  Returns 0 on sucess, error otherwise.
1064
 
 */
1065
 
int sd_link(struct subdomain *sd, struct dentry *link, struct dentry *target)
1066
 
{
1067
 
        char *iname = NULL, *oname = NULL,
1068
 
             *failed_iname = NULL, *failed_oname = NULL;
1069
 
        unsigned int result = 0;
1070
 
        int error, sdpath_error, errorcode = 0, match = 0,
1071
 
            sdcomplain = SUBDOMAIN_COMPLAIN(sd);
1072
 
        struct sd_path_data idata, odata;
1073
 
        struct sd_audit sa;
1074
 
 
1075
 
        if (!__sd_is_confined(sd))
1076
 
                return 0;
1077
 
 
1078
 
        /* Perform nested lookup for names.
1079
 
         * This is necessary in the case where /dev/block is mounted
1080
 
         * multiple times,  i.e /dev/block->/a and /dev/block->/b
1081
 
         * This allows us to detect links where src/dest are on different
1082
 
         * mounts.   N.B no support yet for links across bind mounts of
1083
 
         * the form mount -bind /mnt/subpath /mnt2
1084
 
         *
1085
 
         * Getting direct access to vfsmounts (via nameidata) for link and
1086
 
         * target would allow all this uglyness to go away.
1087
 
         *
1088
 
         * If more than one mountpoint matches but none satisfy the profile,
1089
 
         * only the first pathname (mountpoint) is logged.
1090
 
         */
1091
 
 
1092
 
        sd_path_begin2(target, link, &odata);
1093
 
        do {
1094
 
                oname = sd_path_getname(&odata);
1095
 
                if (oname) {
1096
 
                        sd_path_begin(target, &idata);
1097
 
                        do {
1098
 
                                iname = sd_path_getname(&idata);
1099
 
                                if (iname) {
1100
 
                                        result = sd_link_perm(sd, oname, iname);
1101
 
 
1102
 
                                        /* access via any path is enough */
1103
 
                                        if (result || sdcomplain) {
1104
 
                                                match = 1;
1105
 
                                                break;
1106
 
                                        }
1107
 
 
1108
 
                                        /* Already have an path that failed? */
1109
 
                                        if (failed_iname) {
1110
 
                                                sd_put_name(iname);
1111
 
                                        } else {
1112
 
                                                failed_iname = iname;
1113
 
                                                failed_oname = oname;
1114
 
                                        }
1115
 
                                }
1116
 
                        } while (iname && !match);
1117
 
 
1118
 
                        /* should not be possible if we matched */
1119
 
                        if ((sdpath_error = sd_path_end(&idata)) != 0) {
1120
 
                                dentry_xlate_error(target, sdpath_error,
1121
 
                                                   "inner dentry [link]");
1122
 
 
1123
 
                                /* name should not be set if error */
1124
 
                                WARN_ON(iname);
1125
 
 
1126
 
                                errorcode = sdpath_error;
1127
 
                        }
1128
 
 
1129
 
                        /* don't release if we're saving it */
1130
 
                        if (!match && failed_oname != oname)
1131
 
                                sd_put_name(oname);
1132
 
                }
1133
 
        } while (oname && !match);
1134
 
 
1135
 
        if (errorcode != 0) {
1136
 
                /* inner error */
1137
 
                (void)sd_path_end(&odata);
1138
 
        } else if ((sdpath_error = sd_path_end(&odata)) != 0) {
1139
 
                dentry_xlate_error(link, sdpath_error, "outer dentry [link]");
1140
 
 
1141
 
                errorcode = sdpath_error;
1142
 
        }
1143
 
 
1144
 
        if (errorcode != 0) {
1145
 
                /* inner or outer error */
1146
 
                result = 0;
1147
 
        } else if (!match) {
1148
 
                /* failed to match */
1149
 
                WARN_ON(iname);
1150
 
                WARN_ON(oname);
1151
 
 
1152
 
                result = 0;
1153
 
                iname = failed_iname;
1154
 
                oname = failed_oname;
1155
 
        }
1156
 
 
1157
 
        sa.type = SD_AUDITTYPE_LINK;
1158
 
        sa.name = oname;        /* link */
1159
 
        sa.pval = iname;        /* target */
1160
 
        sa.flags = 0;
1161
 
        sa.errorcode = errorcode;
1162
 
        sa.result = result;
1163
 
        sa.gfp_mask = GFP_KERNEL;
1164
 
 
1165
 
        error = sd_audit(sd, &sa);
1166
 
 
1167
 
        if (failed_oname != oname)
1168
 
                sd_put_name(failed_oname);
1169
 
        if (failed_iname != iname)
1170
 
                sd_put_name(failed_iname);
1171
 
 
1172
 
        sd_put_name(oname);
1173
 
        sd_put_name(iname);
1174
 
 
1175
 
        return error;
1176
 
}
1177
 
 
1178
 
/**********************************
1179
 
 * GLOBAL PROCESS RELATED FUNCTIONS
1180
 
 *********************************/
1181
 
 
1182
 
/**
1183
 
 * sd_fork - create a new subdomain
1184
 
 * @p: new process
1185
 
 *
1186
 
 * Create a new subdomain for newly created process @p if it's parent
1187
 
 * is already confined.  Otherwise a subdomain will be lazily allocated
1188
 
 * for the child if it subsequently execs (in sd_register).
1189
 
 * Return 0 on sucess.
1190
 
 */
1191
 
 
1192
 
int sd_fork(struct task_struct *p)
1193
 
{
1194
 
        struct subdomain *sd = SD_SUBDOMAIN(current->security);
1195
 
        struct subdomain *newsd = NULL;
1196
 
 
1197
 
        SD_DEBUG("%s\n", __FUNCTION__);
1198
 
 
1199
 
        if (__sd_is_confined(sd)) {
1200
 
                unsigned long flags;
1201
 
 
1202
 
                newsd = alloc_subdomain(p);
1203
 
 
1204
 
                if (!newsd)
1205
 
                        return -ENOMEM;
1206
 
 
1207
 
                /* Can get away with a read rather than write lock here
1208
 
                 * as we just allocated newsd above, so we can guarantee
1209
 
                 * that it's active/profile are null and therefore a replace
1210
 
                 * cannot happen.
1211
 
                 */
1212
 
                read_lock_irqsave(&sd_lock, flags);
1213
 
                sd_switch(newsd, sd->profile, sd->active);
1214
 
                newsd->sd_hat_magic = sd->sd_hat_magic;
1215
 
                read_unlock_irqrestore(&sd_lock, flags);
1216
 
 
1217
 
                if (SUBDOMAIN_COMPLAIN(sd) &&
1218
 
                    sd->active == null_complain_profile)
1219
 
                        LOG_HINT(sd, GFP_KERNEL, HINT_FORK,
1220
 
                                "pid=%d child=%d\n",
1221
 
                                current->pid, p->pid);
1222
 
        }
1223
 
        p->security = newsd;
1224
 
        return 0;
1225
 
}
1226
 
 
1227
 
/**
1228
 
 * sd_register - register a new program
1229
 
 * @filp: file of program being registered
1230
 
 *
1231
 
 * Try to register a new program during execve().  This should give the
1232
 
 * new program a valid subdomain.
1233
 
 *
1234
 
 * This _used_ to be a really simple piece of code :-(
1235
 
 *
1236
 
 */
1237
 
int sd_register(struct linux_binprm *bprm)
1238
 
{
1239
 
        char *filename;
1240
 
        struct file *filp = bprm->file;
1241
 
        struct subdomain *sd, sdcopy;
1242
 
        struct sdprofile *newprofile = NULL, unconstrained_flag;
1243
 
        int     error = -ENOMEM,
1244
 
                exec_mode = 0,
1245
 
                findprofile = 0,
1246
 
                findprofile_mandatory = 0,
1247
 
                unsafe_exec = 0,
1248
 
                complain = 0;
1249
 
 
1250
 
        SD_DEBUG("%s\n", __FUNCTION__);
1251
 
 
1252
 
        sd = get_sdcopy(&sdcopy);
1253
 
 
1254
 
        filename = sd_get_name(filp->f_dentry, filp->f_vfsmnt);
1255
 
        if (IS_ERR(filename)) {
1256
 
                SD_WARN("%s: Failed to get filename\n", __FUNCTION__);
1257
 
                goto out;
1258
 
        }
1259
 
 
1260
 
        error = 0;
1261
 
 
1262
 
        if (!__sd_is_confined(sd)) {
1263
 
                /* Unconfined task, load profile if it exists */
1264
 
                findprofile = 1;
1265
 
                goto find_profile;
1266
 
        }
1267
 
 
1268
 
        complain = SUBDOMAIN_COMPLAIN(sd);
1269
 
 
1270
 
        /* Confined task, determine what mode inherit, unconstrained or
1271
 
         * mandatory to load new profile
1272
 
         */
1273
 
        if (sd_get_execmode(sd, filename, &exec_mode, &unsafe_exec)) {
1274
 
                switch (exec_mode) {
1275
 
                case SD_EXEC_INHERIT:
1276
 
                        /* do nothing - setting of profile
1277
 
                         * already handed in sd_fork
1278
 
                         */
1279
 
                        SD_DEBUG("%s: INHERIT %s\n",
1280
 
                                 __FUNCTION__,
1281
 
                                 filename);
1282
 
                        break;
1283
 
 
1284
 
                case SD_EXEC_UNCONSTRAINED:
1285
 
                        SD_DEBUG("%s: UNCONSTRAINED %s\n",
1286
 
                                 __FUNCTION__,
1287
 
                                 filename);
1288
 
 
1289
 
                        /* unload profile */
1290
 
                        newprofile = &unconstrained_flag;
1291
 
                        break;
1292
 
 
1293
 
                case SD_EXEC_PROFILE:
1294
 
                        SD_DEBUG("%s: PROFILE %s\n",
1295
 
                                 __FUNCTION__,
1296
 
                                 filename);
1297
 
 
1298
 
                        findprofile = 1;
1299
 
                        findprofile_mandatory = 1;
1300
 
                        break;
1301
 
 
1302
 
                case SD_MAY_EXEC:
1303
 
                        /* this should not happen, entries
1304
 
                         * with just EXEC only should be
1305
 
                         * rejected at profile load time
1306
 
                         */
1307
 
                        SD_ERROR("%s: Rejecting exec(2) of image '%s'. "
1308
 
                                "SD_MAY_EXEC without exec qualifier invalid "
1309
 
                                "(%s(%d) profile %s active %s\n",
1310
 
                                 __FUNCTION__,
1311
 
                                 filename,
1312
 
                                 current->comm, current->pid,
1313
 
                                 sd->profile->name, sd->active->name);
1314
 
                        error = -EPERM;
1315
 
                        break;
1316
 
 
1317
 
                default:
1318
 
                        SD_ERROR("%s: Rejecting exec(2) of image '%s'. "
1319
 
                                 "Unknown exec qualifier %x "
1320
 
                                 "(%s (pid %d) profile %s active %s)\n",
1321
 
                                 __FUNCTION__,
1322
 
                                 filename,
1323
 
                                 exec_mode,
1324
 
                                 current->comm, current->pid,
1325
 
                                 sd->profile->name, sd->active->name);
1326
 
                        error = -EPERM;
1327
 
                        break;
1328
 
                }
1329
 
 
1330
 
        } else if (complain) {
1331
 
                /* There was no entry in calling profile
1332
 
                 * describing mode to execute image in.
1333
 
                 * Drop into null-profile (disabling secure exec).
1334
 
                 */
1335
 
                newprofile = get_sdprofile(null_complain_profile);
1336
 
                unsafe_exec = 1;
1337
 
        } else {
1338
 
                SD_WARN("%s: Rejecting exec(2) of image '%s'. "
1339
 
                        "Unable to determine exec qualifier "
1340
 
                        "(%s (pid %d) profile %s active %s)\n",
1341
 
                        __FUNCTION__,
1342
 
                        filename,
1343
 
                        current->comm, current->pid,
1344
 
                        sd->profile->name, sd->active->name);
1345
 
                error = -EPERM;
1346
 
        }
1347
 
 
1348
 
 
1349
 
find_profile:
1350
 
        if (!findprofile)
1351
 
                goto apply_profile;
1352
 
 
1353
 
        /* Locate new profile */
1354
 
        newprofile = sd_profilelist_find(filename);
1355
 
        if (newprofile) {
1356
 
                SD_DEBUG("%s: setting profile %s\n",
1357
 
                         __FUNCTION__, newprofile->name);
1358
 
        } else if (findprofile_mandatory) {
1359
 
                /* Profile (mandatory) could not be found */
1360
 
 
1361
 
                if (complain) {
1362
 
                        LOG_HINT(sd, GFP_KERNEL, HINT_MANDPROF,
1363
 
                                "image=%s pid=%d profile=%s active=%s\n",
1364
 
                                filename,
1365
 
                                current->pid,
1366
 
                                sd->profile->name,
1367
 
                                sd->active->name);
1368
 
 
1369
 
                        newprofile = get_sdprofile(null_complain_profile);
1370
 
                } else {
1371
 
                        SD_WARN("REJECTING exec(2) of image '%s'. "
1372
 
                                "Profile mandatory and not found "
1373
 
                                "(%s(%d) profile %s active %s)\n",
1374
 
                                filename,
1375
 
                                current->comm, current->pid,
1376
 
                                sd->profile->name, sd->active->name);
1377
 
                        error = -EPERM;
1378
 
                }
1379
 
        } else {
1380
 
                /* Profile (non-mandatory) could not be found */
1381
 
 
1382
 
                /* Only way we can get into this code is if task
1383
 
                 * is unconstrained.
1384
 
                 */
1385
 
 
1386
 
                BUG_ON(__sd_is_confined(sd));
1387
 
 
1388
 
                SD_DEBUG("%s: No profile found for exec image %s\n",
1389
 
                         __FUNCTION__,
1390
 
                         filename);
1391
 
        } /* newprofile */
1392
 
 
1393
 
 
1394
 
apply_profile:
1395
 
        /* Apply profile if necessary */
1396
 
        if (newprofile) {
1397
 
                struct subdomain *latest_sd, *lazy_sd = NULL;
1398
 
                unsigned long flags;
1399
 
 
1400
 
                if (newprofile == &unconstrained_flag)
1401
 
                        newprofile = NULL;
1402
 
 
1403
 
                /* grab a write lock
1404
 
                 *
1405
 
                 * - Task may be presently unconfined (have no sd). In which
1406
 
                 *   case we have to lazily allocate one.  Note we may be raced
1407
 
                 *   to this allocation by a setprofile.
1408
 
                 *
1409
 
                 * - sd is a refcounted copy of the subdomain (get_sdcopy) and
1410
 
                 *   not the actual subdomain. This allows us to not have to
1411
 
                 *   hold a read lock around all this code. However, we need to
1412
 
                 *   change the actual subdomain, not the copy.
1413
 
                 *
1414
 
                 * - If newprofile points to an actual profile (result of
1415
 
                 *   sd_profilelist_find above), this profile may have been
1416
 
                 *   replaced.  We need to fix it up.  Doing this to avoid
1417
 
                 *   having to hold a write lock around all this code.
1418
 
                 */
1419
 
 
1420
 
                if (!sd) {
1421
 
                        lazy_sd = alloc_subdomain(current);
1422
 
                }
1423
 
 
1424
 
                write_lock_irqsave(&sd_lock, flags);
1425
 
 
1426
 
                latest_sd = SD_SUBDOMAIN(current->security);
1427
 
 
1428
 
                if (latest_sd) {
1429
 
                        if (lazy_sd) {
1430
 
                                /* raced by setprofile (created latest_sd) */
1431
 
                                free_subdomain(lazy_sd);
1432
 
                                lazy_sd = NULL;
1433
 
                        }
1434
 
                } else {
1435
 
                        if (lazy_sd) {
1436
 
                                latest_sd = lazy_sd;
1437
 
                                current->security = lazy_sd;
1438
 
                        } else {
1439
 
                                SD_ERROR("%s: Failed to allocate subdomain\n",
1440
 
                                        __FUNCTION__);
1441
 
 
1442
 
                                error = -ENOMEM;
1443
 
                                write_unlock_irqrestore(&sd_lock, flags);
1444
 
                                goto done;
1445
 
                        }
1446
 
                }
1447
 
 
1448
 
                /* Determine if profile we found earlier is stale.
1449
 
                 * If so, reobtain it.  N.B stale flag should never be
1450
 
                 * set on null_complain profile.
1451
 
                 */
1452
 
                if (newprofile && unlikely(newprofile->isstale)) {
1453
 
                        BUG_ON(newprofile == null_complain_profile);
1454
 
 
1455
 
                        /* drop refcnt obtained from earlier get_sdprofile */
1456
 
                        put_sdprofile(newprofile);
1457
 
 
1458
 
                        newprofile = sd_profilelist_find(filename);
1459
 
 
1460
 
                        if (!newprofile) {
1461
 
                                /* Race, profile was removed, not replaced.
1462
 
                                 * Redo with error checking
1463
 
                                 */
1464
 
                                write_unlock_irqrestore(&sd_lock, flags);
1465
 
                                goto find_profile;
1466
 
                        }
1467
 
                }
1468
 
 
1469
 
                /* Handle confined exec.
1470
 
                 * Can be at this point for the following reasons:
1471
 
                 * 1. unconfined switching to confined
1472
 
                 * 2. confined switching to different confinement
1473
 
                 * 3. confined switching to unconfined
1474
 
                 *
1475
 
                 * Cases 2 and 3 are marked as requiring secure exec
1476
 
                 * (unless policy specified "unsafe exec")
1477
 
                 */
1478
 
                if (__sd_is_confined(latest_sd) && !unsafe_exec) {
1479
 
                        unsigned long bprm_flags;
1480
 
 
1481
 
                        bprm_flags = SD_SECURE_EXEC_NEEDED;
1482
 
                        bprm->security = (void*)
1483
 
                                ((unsigned long)bprm->security | bprm_flags);
1484
 
                }
1485
 
 
1486
 
                sd_switch(latest_sd, newprofile, newprofile);
1487
 
                put_sdprofile(newprofile);
1488
 
 
1489
 
                if (complain && newprofile == null_complain_profile)
1490
 
                        LOG_HINT(latest_sd, GFP_ATOMIC, HINT_CHGPROF,
1491
 
                                "pid=%d\n",
1492
 
                                current->pid);
1493
 
 
1494
 
                write_unlock_irqrestore(&sd_lock, flags);
1495
 
        }
1496
 
 
1497
 
done:
1498
 
        sd_put_name(filename);
1499
 
 
1500
 
        if (sd)
1501
 
                put_sdcopy(sd);
1502
 
 
1503
 
out:
1504
 
        return error;
1505
 
}
1506
 
 
1507
 
/**
1508
 
 * sd_release - release the task's subdomain
1509
 
 * @p: task being released
1510
 
 *
1511
 
 * This is called after a task has exited and the parent has reaped it.
1512
 
 * p->security must be !NULL. The @p->security blob is freed.
1513
 
 */
1514
 
void sd_release(struct task_struct *p)
1515
 
{
1516
 
        struct subdomain *sd = SD_SUBDOMAIN(p->security);
1517
 
        p->security = NULL;
1518
 
 
1519
 
        sd_subdomainlist_remove(sd);
1520
 
 
1521
 
        /* release profiles */
1522
 
        put_sdprofile(sd->profile);
1523
 
        put_sdprofile(sd->active);
1524
 
 
1525
 
        kfree(sd);
1526
 
}
1527
 
 
1528
 
/*****************************
1529
 
 * GLOBAL SUBPROFILE FUNCTIONS
1530
 
 ****************************/
1531
 
 
1532
 
/**
1533
 
 * do_change_hat - actually switch hats
1534
 
 * @name: name of hat to swtich to
1535
 
 * @sd: current subdomain
1536
 
 *
1537
 
 * Switch to a new hat.  Return 0 on success, error otherwise.
1538
 
 */
1539
 
static inline int do_change_hat(const char *hat_name, struct subdomain *sd)
1540
 
{
1541
 
        struct sdprofile *sub;
1542
 
        struct sdprofile *p = sd->active;
1543
 
        int error = 0;
1544
 
 
1545
 
        sub = __sd_find_profile(hat_name, &sd->profile->sub);
1546
 
 
1547
 
        if (sub) {
1548
 
                /* change hat */
1549
 
                sd->active = sub;
1550
 
        } else {
1551
 
                /* There is no such subprofile change to a NULL profile.
1552
 
                 * The NULL profile grants no file access.
1553
 
                 *
1554
 
                 * This feature is used by changehat_apache.
1555
 
                 *
1556
 
                 * N.B from the null-profile the task can still changehat back
1557
 
                 * out to the parent profile (assuming magic != NULL)
1558
 
                 */
1559
 
                if (SUBDOMAIN_COMPLAIN(sd)) {
1560
 
                        LOG_HINT(sd, GFP_ATOMIC, HINT_UNKNOWN_HAT,
1561
 
                                "%s pid=%d "
1562
 
                                "profile=%s active=%s\n",
1563
 
                                hat_name,
1564
 
                                current->pid,
1565
 
                                sd->profile->name,
1566
 
                                sd->active->name);
1567
 
                        sd->active = get_sdprofile(null_complain_profile);
1568
 
                } else {
1569
 
                        SD_DEBUG("%s: Unknown hatname '%s'. "
1570
 
                                "Changing to NULL profile "
1571
 
                                "(%s(%d) profile %s active %s)\n",
1572
 
                                 __FUNCTION__,
1573
 
                                 hat_name,
1574
 
                                 current->comm, current->pid,
1575
 
                                 sd->profile->name, sd->active->name);
1576
 
 
1577
 
                        sd->active = get_sdprofile(null_profile);
1578
 
                        error = -EACCES;
1579
 
                }
1580
 
        }
1581
 
        put_sdprofile(p);
1582
 
 
1583
 
        return error;
1584
 
}
1585
 
 
1586
 
/**
1587
 
 * sd_change_hat - change hat to/from subprofile
1588
 
 * @hat_name: specifies hat to change to
1589
 
 * @hat_magic: token to validate hat change
1590
 
 *
1591
 
 * Change to new @hat_name when current hat is top level profile, and store
1592
 
 * the @hat_magic in the current subdomain.  If the new @hat_name is
1593
 
 * NULL, and the @hat_magic matches that stored in the current subdomain
1594
 
 * return to original top level profile.  Returns 0 on success, error
1595
 
 * otherwise.
1596
 
 */
1597
 
#define IN_SUBPROFILE(sd)       ((sd)->profile != (sd)->active)
1598
 
int sd_change_hat(const char *hat_name, __u32 hat_magic)
1599
 
{
1600
 
        struct subdomain *sd = SD_SUBDOMAIN(current->security);
1601
 
        int error = 0;
1602
 
 
1603
 
        SD_DEBUG("%s: %p, 0x%x (pid %d)\n",
1604
 
                 __FUNCTION__,
1605
 
                 hat_name, hat_magic,
1606
 
                 current->pid);
1607
 
 
1608
 
        /* Dump out above debugging in WARN mode if we are in AUDIT mode */
1609
 
        if (SUBDOMAIN_AUDIT(sd)) {
1610
 
                SD_WARN("%s: %s, 0x%x (pid %d)\n",
1611
 
                        __FUNCTION__, hat_name ? hat_name : "NULL",
1612
 
                        hat_magic, current->pid);
1613
 
        }
1614
 
 
1615
 
        /* no subdomain: changehat into the null_profile, since the process
1616
 
           has no subdomain do_change_hat won't find a match which will cause
1617
 
           a changehat to null_profile.  We could short circuit this but since
1618
 
           the subdprofile (hat) list is empty we would save very little. */
1619
 
 
1620
 
        /* check to see if an unconfined process is doing a changehat. */
1621
 
        if (!__sd_is_confined(sd)) {
1622
 
                error = -EPERM;
1623
 
                goto out;
1624
 
        }
1625
 
 
1626
 
        /* check to see if the confined process has any hats. */
1627
 
        if (list_empty(&sd->profile->sub) && !SUBDOMAIN_COMPLAIN(sd)) {
1628
 
                error = -ECHILD;
1629
 
                goto out;
1630
 
        }
1631
 
 
1632
 
        /* Check whether current domain is parent
1633
 
         * or one of the sibling children
1634
 
         */
1635
 
        if (sd->profile == sd->active) {
1636
 
                /*
1637
 
                 * parent
1638
 
                 */
1639
 
                if (hat_name) {
1640
 
                        SD_DEBUG("%s: switching to %s, 0x%x\n",
1641
 
                                 __FUNCTION__,
1642
 
                                 hat_name,
1643
 
                                 hat_magic);
1644
 
 
1645
 
                        /*
1646
 
                         * N.B hat_magic == 0 has a special meaning
1647
 
                         * this indicates that the task may never changehat
1648
 
                         * back to it's parent, it will stay in this subhat
1649
 
                         * (or null-profile, if the hat doesn't exist) until
1650
 
                         * the task terminates
1651
 
                         */
1652
 
                        sd->sd_hat_magic = hat_magic;
1653
 
                        error = do_change_hat(hat_name, sd);
1654
 
                } else {
1655
 
                        /* Got here via changehat(NULL, magic)
1656
 
                         *
1657
 
                         * We used to simply update the magic cookie.
1658
 
                         * That's an odd behaviour, so just do nothing.
1659
 
                         */
1660
 
                }
1661
 
        } else {
1662
 
                /*
1663
 
                 * child -- check to make sure magic is same as what was
1664
 
                 * passed when we switched into this profile,
1665
 
                 * Handle special casing of NULL magic which confines task
1666
 
                 * to subprofile and prohibits further changehats
1667
 
                 */
1668
 
                if (hat_magic == sd->sd_hat_magic && sd->sd_hat_magic) {
1669
 
                        if (!hat_name) {
1670
 
                                /*
1671
 
                                 * Got here via changehat(NULL, magic)
1672
 
                                 * Return from subprofile, back to parent
1673
 
                                 */
1674
 
                                put_sdprofile(sd->active);
1675
 
                                sd->active = get_sdprofile(sd->profile);
1676
 
 
1677
 
                                /* Reset hat_magic to zero.
1678
 
                                 * New value will be passed on next changehat
1679
 
                                 */
1680
 
                                sd->sd_hat_magic = 0;
1681
 
                        } else {
1682
 
                                /* change to another (sibling) profile */
1683
 
                                error = do_change_hat(hat_name, sd);
1684
 
                        }
1685
 
                } else if (sd->sd_hat_magic) {
1686
 
                        SD_ERROR("KILLING process %s(%d) "
1687
 
                                 "Invalid change_hat() magic# 0x%x "
1688
 
                                 "(hatname %s profile %s active %s)\n",
1689
 
                                 current->comm, current->pid,
1690
 
                                 hat_magic,
1691
 
                                 hat_name ? hat_name : "NULL",
1692
 
                                 sd->profile->name, sd->active->name);
1693
 
 
1694
 
                        /* terminate current process */
1695
 
                        (void)send_sig_info(SIGKILL, NULL, current);
1696
 
                } else {        /* sd->sd_hat_magic == NULL */
1697
 
                        SD_ERROR("KILLING process %s(%d) "
1698
 
                                 "Task was confined to current subprofile "
1699
 
                                 "(profile %s active %s)\n",
1700
 
                                 current->comm, current->pid,
1701
 
                                 sd->profile->name, sd->active->name);
1702
 
 
1703
 
                        /* terminate current process */
1704
 
                        (void)send_sig_info(SIGKILL, NULL, current);
1705
 
                }
1706
 
 
1707
 
        }
1708
 
 
1709
 
out:
1710
 
        return error;
1711
 
}