~apparmor-dev/apparmor/master

« back to all changes in this revision

Viewing changes to libraries/libapparmor/src/kernel.c

  • Committer: Steve Beattie
  • Date: 2019-02-19 09:38:13 UTC
  • Revision ID: sbeattie@ubuntu.com-20190219093813-ud526ee6hwn8nljz
The AppArmor project has been converted to git and is now hosted on
gitlab.

To get the converted repository, please do
  git clone https://gitlab.com/apparmor/apparmor

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (c) 2003-2008 Novell, Inc. (All rights reserved)
3
 
 * Copyright 2009-2010 Canonical Ltd.
4
 
 *
5
 
 * The libapparmor library is licensed under the terms of the GNU
6
 
 * Lesser General Public License, version 2.1. Please see the file
7
 
 * COPYING.LGPL.
8
 
 *
9
 
 * This library is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 * GNU Lesser General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU Lesser General Public License
15
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 
 */
17
 
 
18
 
#include <stdlib.h>
19
 
#include <stdio.h>
20
 
#include <string.h>
21
 
#include <unistd.h>
22
 
#include <sys/types.h>
23
 
#include <sys/stat.h>
24
 
#include <sys/syscall.h>
25
 
#include <sys/socket.h>
26
 
#include <fcntl.h>
27
 
#include <errno.h>
28
 
#include <limits.h>
29
 
#include <stdarg.h>
30
 
#include <mntent.h>
31
 
#include <inttypes.h>
32
 
#include <pthread.h>
33
 
 
34
 
#include <sys/apparmor.h>
35
 
#include "private.h"
36
 
 
37
 
/* some non-Linux systems do not define a static value */
38
 
#ifndef PATH_MAX
39
 
# define PATH_MAX 4096
40
 
#endif
41
 
 
42
 
#define symbol_version(real, name, version) \
43
 
                __asm__ (".symver " #real "," #name "@" #version)
44
 
#define default_symbol_version(real, name, version) \
45
 
                __asm__ (".symver " #real "," #name "@@" #version)
46
 
 
47
 
#define UNCONFINED              "unconfined"
48
 
#define UNCONFINED_SIZE         strlen(UNCONFINED)
49
 
 
50
 
/**
51
 
 * aa_find_mountpoint - find where the apparmor interface filesystem is mounted
52
 
 * @mnt: returns buffer with the mountpoint string
53
 
 *
54
 
 * Returns: 0 on success else -1 on error
55
 
 *
56
 
 * NOTE: this function only supports versions of apparmor using securityfs
57
 
 */
58
 
int aa_find_mountpoint(char **mnt)
59
 
{
60
 
        struct stat statbuf;
61
 
        struct mntent *mntpt;
62
 
        FILE *mntfile;
63
 
        int rc = -1;
64
 
 
65
 
        if (!mnt) {
66
 
                errno = EINVAL;
67
 
                return -1;
68
 
        }
69
 
 
70
 
        mntfile = setmntent("/proc/mounts", "r");
71
 
        if (!mntfile)
72
 
                return -1;
73
 
 
74
 
        while ((mntpt = getmntent(mntfile))) {
75
 
                char *proposed = NULL;
76
 
                if (strcmp(mntpt->mnt_type, "securityfs") != 0)
77
 
                        continue;
78
 
 
79
 
                if (asprintf(&proposed, "%s/apparmor", mntpt->mnt_dir) < 0)
80
 
                        /* ENOMEM */
81
 
                        break;
82
 
 
83
 
                if (stat(proposed, &statbuf) == 0) {
84
 
                        *mnt = proposed;
85
 
                        rc = 0;
86
 
                        break;
87
 
                }
88
 
                free(proposed);
89
 
        }
90
 
        endmntent(mntfile);
91
 
        if (rc == -1)
92
 
                errno = ENOENT;
93
 
        return rc;
94
 
}
95
 
 
96
 
/**
97
 
 * aa_is_enabled - determine if apparmor is enabled
98
 
 *
99
 
 * Returns: 1 if enabled else reason it is not, or 0 on error
100
 
 *
101
 
 * ENOSYS - no indication apparmor is present in the system
102
 
 * ENOENT - enabled but interface could not be found
103
 
 * ECANCELED - disabled at boot
104
 
 * ENOMEM - out of memory
105
 
 */
106
 
int aa_is_enabled(void)
107
 
{
108
 
        int serrno, fd, rc, size;
109
 
        char buffer[2];
110
 
        char *mnt;
111
 
 
112
 
        /* if the interface mountpoint is available apparmor is enabled */
113
 
        rc = aa_find_mountpoint(&mnt);
114
 
        if (rc == 0) {
115
 
                free(mnt);
116
 
                return 1;
117
 
        }
118
 
 
119
 
        /* determine why the interface mountpoint isn't available */
120
 
        fd = open("/sys/module/apparmor/parameters/enabled", O_RDONLY);
121
 
        if (fd == -1) {
122
 
                if (errno == ENOENT)
123
 
                        errno = ENOSYS;
124
 
                return 0;
125
 
        }
126
 
 
127
 
        size = read(fd, &buffer, 2);
128
 
        serrno = errno;
129
 
        close(fd);
130
 
        errno = serrno;
131
 
 
132
 
        if (size > 0) {
133
 
                if (buffer[0] == 'Y')
134
 
                        errno = ENOENT;
135
 
                else
136
 
                        errno = ECANCELED;
137
 
        }
138
 
        return 0;
139
 
}
140
 
 
141
 
static inline pid_t aa_gettid(void)
142
 
{
143
 
#ifdef SYS_gettid
144
 
        return syscall(SYS_gettid);
145
 
#else
146
 
        return getpid();
147
 
#endif
148
 
}
149
 
 
150
 
static char *procattr_path(pid_t pid, const char *attr)
151
 
{
152
 
        char *path = NULL;
153
 
        if (asprintf(&path, "/proc/%d/attr/%s", pid, attr) > 0)
154
 
                return path;
155
 
        return NULL;
156
 
}
157
 
 
158
 
/**
159
 
 * parse_unconfined - check for the unconfined label
160
 
 * @con: the confinement context
161
 
 * @size: size of the confinement context (not including the NUL terminator)
162
 
 *
163
 
 * Returns: True if the con is the unconfined label or false otherwise
164
 
 */
165
 
static bool parse_unconfined(char *con, int size)
166
 
{
167
 
        return size == UNCONFINED_SIZE &&
168
 
               strncmp(con, UNCONFINED, UNCONFINED_SIZE) == 0;
169
 
}
170
 
 
171
 
/**
172
 
 * splitcon - split the confinement context into a label and mode
173
 
 * @con: the confinement context
174
 
 * @size: size of the confinement context (not including the NUL terminator)
175
 
 * @strip_newline: true if a trailing newline character should be stripped
176
 
 * @mode: if non-NULL and a mode is present, will point to mode string in @con
177
 
 *  on success
178
 
 *
179
 
 * Modifies the @con string to split it into separate label and mode strings.
180
 
 * If @strip_newline is true and @con contains a single trailing newline, it
181
 
 * will be stripped on success (it will not be stripped on error). The @mode
182
 
 * argument is optional. If @mode is NULL, @con will still be split between the
183
 
 * label and mode (if present) but @mode will not be set.
184
 
 *
185
 
 * Returns: a pointer to the label string or NULL on error
186
 
 */
187
 
static char *splitcon(char *con, int size, bool strip_newline, char **mode)
188
 
{
189
 
        char *label = NULL;
190
 
        char *mode_str = NULL;
191
 
        char *newline = NULL;
192
 
 
193
 
        if (size == 0)
194
 
                goto out;
195
 
 
196
 
        if (strip_newline && con[size - 1] == '\n') {
197
 
                newline = &con[size - 1];
198
 
                size--;
199
 
        }
200
 
 
201
 
        if (parse_unconfined(con, size)) {
202
 
                label = con;
203
 
                goto out;
204
 
        }
205
 
 
206
 
        if (size > 3 && con[size - 1] == ')') {
207
 
                int pos = size - 2;
208
 
 
209
 
                while (pos > 0 && !(con[pos] == ' ' && con[pos + 1] == '('))
210
 
                        pos--;
211
 
                if (pos > 0) {
212
 
                        con[pos] = 0; /* overwrite ' ' */
213
 
                        con[size - 1] = 0; /* overwrite trailing ) */
214
 
                        mode_str = &con[pos + 2]; /* skip '(' */
215
 
                        label = con;
216
 
                }
217
 
        }
218
 
out:
219
 
        if (label && strip_newline && newline)
220
 
                *newline = 0; /* overwrite '\n', if requested, on success */
221
 
        if (mode)
222
 
                *mode = mode_str;
223
 
        return label;
224
 
}
225
 
 
226
 
/**
227
 
 * aa_splitcon - split the confinement context into a label and mode
228
 
 * @con: the confinement context
229
 
 * @mode: if non-NULL and a mode is present, will point to mode string in @con
230
 
 *  on success
231
 
 *
232
 
 * Modifies the @con string to split it into separate label and mode strings. A
233
 
 * single trailing newline character will be stripped from @con, if found. The
234
 
 * @mode argument is optional. If @mode is NULL, @con will still be split
235
 
 * between the label and mode (if present) but @mode will not be set.
236
 
 *
237
 
 * Returns: a pointer to the label string or NULL on error
238
 
 */
239
 
char *aa_splitcon(char *con, char **mode)
240
 
{
241
 
        return splitcon(con, strlen(con), true, mode);
242
 
}
243
 
 
244
 
/**
245
 
 * aa_getprocattr_raw - get the contents of @attr for @tid into @buf
246
 
 * @tid: tid of task to query
247
 
 * @attr: which /proc/<tid>/attr/<attr> to query
248
 
 * @buf: buffer to store the result in
249
 
 * @len: size of the buffer
250
 
 * @mode: if non-NULL and a mode is present, will point to mode string in @buf
251
 
 *
252
 
 * Returns: size of data read or -1 on error, and sets errno
253
 
 */
254
 
int aa_getprocattr_raw(pid_t tid, const char *attr, char *buf, int len,
255
 
                       char **mode)
256
 
{
257
 
        int rc = -1;
258
 
        int fd, ret;
259
 
        char *tmp = NULL;
260
 
        int size = 0;
261
 
 
262
 
        if (!buf || len <= 0) {
263
 
                errno = EINVAL;
264
 
                goto out;
265
 
        }
266
 
 
267
 
        tmp = procattr_path(tid, attr);
268
 
        if (!tmp)
269
 
                goto out;
270
 
 
271
 
        fd = open(tmp, O_RDONLY);
272
 
        free(tmp);
273
 
        if (fd == -1) {
274
 
                goto out;
275
 
        }
276
 
 
277
 
        tmp = buf;
278
 
        do {
279
 
                ret = read(fd, tmp, len);
280
 
                if (ret <= 0)
281
 
                        break;
282
 
                tmp += ret;
283
 
                size += ret;
284
 
                len -= ret;
285
 
                if (len < 0) {
286
 
                        errno = ERANGE;
287
 
                        goto out2;
288
 
                }
289
 
        } while (ret > 0);
290
 
 
291
 
        if (ret < 0) {
292
 
                int saved;
293
 
                if (ret != -1) {
294
 
                        errno = EPROTO;
295
 
                }
296
 
                saved = errno;
297
 
                (void)close(fd);
298
 
                errno = saved;
299
 
                goto out;
300
 
        } else if (size > 0 && buf[size - 1] != 0) {
301
 
                /* check for null termination */
302
 
                if (buf[size - 1] != '\n') {
303
 
                        if (len == 0) {
304
 
                                errno = ERANGE;
305
 
                                goto out2;
306
 
                        } else {
307
 
                                buf[size] = 0;
308
 
                                size++;
309
 
                        }
310
 
                }
311
 
 
312
 
                if (splitcon(buf, size, true, mode) != buf) {
313
 
                        errno = EINVAL;
314
 
                        goto out2;
315
 
                }
316
 
        }
317
 
        rc = size;
318
 
 
319
 
out2:
320
 
        (void)close(fd);
321
 
out:
322
 
        return rc;
323
 
}
324
 
 
325
 
#define INITIAL_GUESS_SIZE 128
326
 
 
327
 
/**
328
 
 * aa_getprocattr - get the contents of @attr for @tid into @label and @mode
329
 
 * @tid: tid of task to query
330
 
 * @attr: which /proc/<tid>/attr/<attr> to query
331
 
 * @label: allocated buffer the label is stored in
332
 
 * @mode: if non-NULL and a mode is present, will point to mode string in @label
333
 
 *
334
 
 * Returns: size of data read or -1 on error, and sets errno
335
 
 *
336
 
 * Guarantees that @label and @mode are null terminated.  The length returned
337
 
 * is for all data including both @label and @mode, and maybe > than
338
 
 * strlen(@label) even if @mode is NULL
339
 
 *
340
 
 * Caller is responsible for freeing the buffer returned in @label.  @mode is
341
 
 * always contained within @label's buffer and so NEVER do free(@mode)
342
 
 */
343
 
int aa_getprocattr(pid_t tid, const char *attr, char **label, char **mode)
344
 
{
345
 
        int rc, size = INITIAL_GUESS_SIZE/2;
346
 
        char *buffer = NULL;
347
 
 
348
 
        if (!label) {
349
 
                errno = EINVAL;
350
 
                return -1;
351
 
        }
352
 
 
353
 
        do {
354
 
                char *tmp;
355
 
 
356
 
                size <<= 1;
357
 
                tmp = realloc(buffer, size);
358
 
                if (!tmp) {
359
 
                        free(buffer);
360
 
                        return -1;
361
 
                }
362
 
                buffer = tmp;
363
 
                memset(buffer, 0, size);
364
 
 
365
 
                rc = aa_getprocattr_raw(tid, attr, buffer, size, mode);
366
 
        } while (rc == -1 && errno == ERANGE);
367
 
 
368
 
        if (rc == -1) {
369
 
                free(buffer);
370
 
                *label = NULL;
371
 
                if (mode)
372
 
                        *mode = NULL;
373
 
        } else
374
 
                *label = buffer;
375
 
 
376
 
        return rc;
377
 
}
378
 
 
379
 
static int setprocattr(pid_t tid, const char *attr, const char *buf, int len)
380
 
{
381
 
        int rc = -1;
382
 
        int fd, ret;
383
 
        char *ctl = NULL;
384
 
 
385
 
        if (!buf) {
386
 
                errno = EINVAL;
387
 
                goto out;
388
 
        }
389
 
 
390
 
        ctl = procattr_path(tid, attr);
391
 
        if (!ctl)
392
 
                goto out;
393
 
 
394
 
        fd = open(ctl, O_WRONLY);
395
 
        if (fd == -1) {
396
 
                goto out;
397
 
        }
398
 
 
399
 
        ret = write(fd, buf, len);
400
 
        if (ret != len) {
401
 
                int saved;
402
 
                if (ret != -1) {
403
 
                        errno = EPROTO;
404
 
                }
405
 
                saved = errno;
406
 
                (void)close(fd);
407
 
                errno = saved;
408
 
                goto out;
409
 
        }
410
 
 
411
 
        rc = 0;
412
 
        (void)close(fd);
413
 
 
414
 
out:
415
 
        if (ctl) {
416
 
                free(ctl);
417
 
        }
418
 
        return rc;
419
 
}
420
 
 
421
 
int aa_change_hat(const char *subprofile, unsigned long token)
422
 
{
423
 
        int rc = -1;
424
 
        int len = 0;
425
 
        char *buf = NULL;
426
 
        const char *fmt = "changehat %016lx^%s";
427
 
 
428
 
        /* both may not be null */
429
 
        if (!(token || subprofile)) {
430
 
                errno = EINVAL;
431
 
                goto out;
432
 
        }
433
 
 
434
 
        if (subprofile && strnlen(subprofile, PATH_MAX + 1) > PATH_MAX) {
435
 
                errno = EPROTO;
436
 
                goto out;
437
 
        }
438
 
 
439
 
        len = asprintf(&buf, fmt, token, subprofile ? subprofile : "");
440
 
        if (len < 0) {
441
 
                goto out;
442
 
        }
443
 
 
444
 
        rc = setprocattr(aa_gettid(), "current", buf, len);
445
 
out:
446
 
        if (buf) {
447
 
                /* clear local copy of magic token before freeing */
448
 
                memset(buf, '\0', len);
449
 
                free(buf);
450
 
        }
451
 
        return rc;
452
 
}
453
 
 
454
 
/* original change_hat interface */
455
 
int __change_hat(char *subprofile, unsigned int token)
456
 
{
457
 
        return aa_change_hat(subprofile, (unsigned long) token);
458
 
}
459
 
 
460
 
int aa_change_profile(const char *profile)
461
 
{
462
 
        char *buf = NULL;
463
 
        int len;
464
 
        int rc;
465
 
 
466
 
        if (!profile) {
467
 
                errno = EINVAL;
468
 
                return -1;
469
 
        }
470
 
 
471
 
        len = asprintf(&buf, "changeprofile %s", profile);
472
 
        if (len < 0)
473
 
                return -1;
474
 
 
475
 
        rc = setprocattr(aa_gettid(), "current", buf, len);
476
 
 
477
 
        free(buf);
478
 
        return rc;
479
 
}
480
 
 
481
 
int aa_change_onexec(const char *profile)
482
 
{
483
 
        char *buf = NULL;
484
 
        int len;
485
 
        int rc;
486
 
 
487
 
        if (!profile) {
488
 
                errno = EINVAL;
489
 
                return -1;
490
 
        }
491
 
 
492
 
        len = asprintf(&buf, "exec %s", profile);
493
 
        if (len < 0)
494
 
                return -1;
495
 
 
496
 
        rc = setprocattr(aa_gettid(), "exec", buf, len);
497
 
 
498
 
        free(buf);
499
 
        return rc;
500
 
}
501
 
 
502
 
/* create an alias for the old change_hat@IMMUNIX_1.0 symbol */
503
 
extern typeof((__change_hat)) __old_change_hat __attribute__((alias ("__change_hat")));
504
 
symbol_version(__old_change_hat, change_hat, IMMUNIX_1.0);
505
 
default_symbol_version(__change_hat, change_hat, APPARMOR_1.0);
506
 
 
507
 
 
508
 
int aa_change_hatv(const char *subprofiles[], unsigned long token)
509
 
{
510
 
        int size, totallen = 0, hatcount = 0;
511
 
        int rc = -1;
512
 
        const char **hats;
513
 
        char *pos, *buf = NULL;
514
 
        const char *cmd = "changehat";
515
 
 
516
 
        /* both may not be null */
517
 
        if (!token && !(subprofiles && *subprofiles)) {
518
 
                errno = EINVAL;
519
 
                goto out;
520
 
        }
521
 
 
522
 
        /* validate hat lengths and while we are at it count how many and
523
 
         * mem required */
524
 
        if (subprofiles) {
525
 
                for (hats = subprofiles; *hats; hats++) {
526
 
                        int len = strnlen(*hats, PATH_MAX + 1);
527
 
                        if (len > PATH_MAX) {
528
 
                                errno = EPROTO;
529
 
                                goto out;
530
 
                        }
531
 
                        totallen += len + 1;
532
 
                        hatcount++;
533
 
                }
534
 
        }
535
 
 
536
 
        /* allocate size of cmd + space + token + ^ + vector of hats */
537
 
        size = strlen(cmd) + 18 + totallen + 1;
538
 
        buf = malloc(size);
539
 
        if (!buf) {
540
 
                goto out;
541
 
        }
542
 
 
543
 
        /* setup command string which is of the form
544
 
         * changehat <token>^hat1\0hat2\0hat3\0..\0
545
 
         */
546
 
        sprintf(buf, "%s %016lx^", cmd, token);
547
 
        pos = buf + strlen(buf);
548
 
        if (subprofiles) {
549
 
                for (hats = subprofiles; *hats; hats++) {
550
 
                        strcpy(pos, *hats);
551
 
                        pos += strlen(*hats) + 1;
552
 
                }
553
 
        } else
554
 
                /* step pos past trailing \0 */
555
 
                pos++;
556
 
 
557
 
        rc = setprocattr(aa_gettid(), "current", buf, pos - buf);
558
 
 
559
 
out:
560
 
        if (buf) {
561
 
                /* clear local copy of magic token before freeing */
562
 
                memset(buf, '\0', size);
563
 
                free(buf);
564
 
        }
565
 
 
566
 
        return rc;
567
 
}
568
 
 
569
 
/**
570
 
 * change_hat_vargs - change_hatv but passing the hats as fn arguments
571
 
 * @token: the magic token
572
 
 * @nhat: the number of hats being passed in the arguments
573
 
 * ...: a argument list of const char * being passed
574
 
 *
575
 
 * change_hat_vargs can be called directly but it is meant to be called
576
 
 * through its macro wrapper of the same name.  Which automatically
577
 
 * fills in the nhats arguments based on the number of parameters
578
 
 * passed.
579
 
 * to call change_hat_vargs direction do
580
 
 * (change_hat_vargs)(token, nhats, hat1, hat2...)
581
 
 */
582
 
int (aa_change_hat_vargs)(unsigned long token, int nhats, ...)
583
 
{
584
 
        va_list ap;
585
 
        const char *argv[nhats+1];
586
 
        int i;
587
 
 
588
 
        va_start(ap, nhats);
589
 
        for (i = 0; i < nhats ; i++) {
590
 
                argv[i] = va_arg(ap, char *);
591
 
        }
592
 
        argv[nhats] = NULL;
593
 
        va_end(ap);
594
 
        return aa_change_hatv(argv, token);
595
 
}
596
 
 
597
 
int aa_stack_profile(const char *profile)
598
 
{
599
 
        char *buf = NULL;
600
 
        int len;
601
 
        int rc;
602
 
 
603
 
        if (!profile) {
604
 
                errno = EINVAL;
605
 
                return -1;
606
 
        }
607
 
 
608
 
        len = asprintf(&buf, "stack %s", profile);
609
 
        if (len < 0)
610
 
                return -1;
611
 
 
612
 
        rc = setprocattr(aa_gettid(), "current", buf, len);
613
 
 
614
 
        free(buf);
615
 
        return rc;
616
 
}
617
 
 
618
 
int aa_stack_onexec(const char *profile)
619
 
{
620
 
        char *buf = NULL;
621
 
        int len;
622
 
        int rc;
623
 
 
624
 
        if (!profile) {
625
 
                errno = EINVAL;
626
 
                return -1;
627
 
        }
628
 
 
629
 
        len = asprintf(&buf, "stack %s", profile);
630
 
        if (len < 0)
631
 
                return -1;
632
 
 
633
 
        rc = setprocattr(aa_gettid(), "exec", buf, len);
634
 
 
635
 
        free(buf);
636
 
        return rc;
637
 
}
638
 
 
639
 
/**
640
 
 * aa_gettaskcon - get the confinement context for task @target in an allocated buffer
641
 
 * @target: task to query
642
 
 * @label: pointer to returned buffer with the label
643
 
 * @mode: if non-NULL and a mode is present, will point to mode string in @label
644
 
 *
645
 
 * Returns: length of confinement context or -1 on error and sets errno
646
 
 *
647
 
 * Guarantees that @label and @mode are null terminated.  The length returned
648
 
 * is for all data including both @label and @mode, and maybe > than
649
 
 * strlen(@label) even if @mode is NULL
650
 
 *
651
 
 * Caller is responsible for freeing the buffer returned in @label.  @mode is
652
 
 * always contained within @label's buffer and so NEVER do free(@mode)
653
 
 */
654
 
int aa_gettaskcon(pid_t target, char **label, char **mode)
655
 
{
656
 
        return aa_getprocattr(target, "current", label, mode);
657
 
}
658
 
 
659
 
/**
660
 
 * aa_getcon - get the confinement context for current task in an allocated buffer
661
 
 * @label: pointer to return buffer with the label if successful
662
 
 * @mode: if non-NULL and a mode is present, will point to mode string in @label
663
 
 *
664
 
 * Returns: length of confinement context or -1 on error and sets errno
665
 
 *
666
 
 * Guarantees that @label and @mode are null terminated.  The length returned
667
 
 * is for all data including both @label and @mode, and may > than
668
 
 * strlen(@label) even if @mode is NULL
669
 
 *
670
 
 * Caller is responsible for freeing the buffer returned in @label.  @mode is
671
 
 * always contained within @label's buffer and so NEVER do free(@mode)
672
 
 */
673
 
int aa_getcon(char **label, char **mode)
674
 
{
675
 
        return aa_gettaskcon(aa_gettid(), label, mode);
676
 
}
677
 
 
678
 
 
679
 
#ifndef SO_PEERSEC
680
 
#define SO_PEERSEC 31
681
 
#endif
682
 
 
683
 
/**
684
 
 * aa_getpeercon_raw - get the confinement context of the socket's peer (other end)
685
 
 * @fd: socket to get peer confinement context for
686
 
 * @buf: buffer to store the result in
687
 
 * @len: initially contains size of the buffer, returns size of data read
688
 
 * @mode: if non-NULL and a mode is present, will point to mode string in @buf
689
 
 *
690
 
 * Returns: length of confinement context including null termination or -1 on
691
 
 *          error if errno == ERANGE then @len will hold the size needed
692
 
 */
693
 
int aa_getpeercon_raw(int fd, char *buf, int *len, char **mode)
694
 
{
695
 
        socklen_t optlen = *len;
696
 
        int rc;
697
 
 
698
 
        if (optlen <= 0 || buf == NULL) {
699
 
                errno = EINVAL;
700
 
                return -1;
701
 
        }
702
 
 
703
 
        rc = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, buf, &optlen);
704
 
        if (rc == -1 || optlen <= 0)
705
 
                goto out;
706
 
 
707
 
        /* check for null termination */
708
 
        if (buf[optlen - 1] != 0) {
709
 
                if (optlen < *len) {
710
 
                        buf[optlen] = 0;
711
 
                        optlen++;
712
 
                } else {
713
 
                        /* buf needs to be bigger by 1 */
714
 
                        rc = -1;
715
 
                        errno = ERANGE;
716
 
                        optlen++;
717
 
                        goto out;
718
 
                }
719
 
        }
720
 
 
721
 
        if (splitcon(buf, optlen - 1, false, mode) != buf) {
722
 
                rc = -1;
723
 
                errno = EINVAL;
724
 
                goto out;
725
 
        }
726
 
 
727
 
        rc = optlen;
728
 
out:
729
 
        *len = optlen;
730
 
        return rc;
731
 
}
732
 
 
733
 
/**
734
 
 * aa_getpeercon - get the confinement context of the socket's peer (other end)
735
 
 * @fd: socket to get peer confinement context for
736
 
 * @label: pointer to allocated buffer with the label
737
 
 * @mode: if non-NULL and a mode is present, will point to mode string in @label
738
 
 *
739
 
 * Returns: length of confinement context including null termination or -1 on error
740
 
 *
741
 
 * Guarantees that @label and @mode are null terminated.  The length returned
742
 
 * is for all data including both @label and @mode, and maybe > than
743
 
 * strlen(@label) even if @mode is NULL
744
 
 *
745
 
 * Caller is responsible for freeing the buffer returned in @label.  @mode is
746
 
 * always contained within @label's buffer and so NEVER do free(@mode)
747
 
 */
748
 
int aa_getpeercon(int fd, char **label, char **mode)
749
 
{
750
 
        int rc, last_size, size = INITIAL_GUESS_SIZE;
751
 
        char *buffer = NULL;
752
 
 
753
 
        if (!label) {
754
 
                errno = EINVAL;
755
 
                return -1;
756
 
        }
757
 
 
758
 
        do {
759
 
                char *tmp;
760
 
 
761
 
                last_size = size;
762
 
                tmp = realloc(buffer, size);
763
 
                if (!tmp) {
764
 
                        free(buffer);
765
 
                        return -1;
766
 
                }
767
 
                buffer = tmp;
768
 
                memset(buffer, 0, size);
769
 
 
770
 
                rc = aa_getpeercon_raw(fd, buffer, &size, mode);
771
 
                /* size should contain actual size needed if errno == ERANGE */
772
 
        } while (rc == -1 && errno == ERANGE && size > last_size);
773
 
 
774
 
        if (rc == -1) {
775
 
                free(buffer);
776
 
                *label = NULL;
777
 
                if (mode)
778
 
                        *mode = NULL;
779
 
                size = -1;
780
 
        } else
781
 
                *label = buffer;
782
 
 
783
 
        return size;
784
 
}
785
 
 
786
 
static pthread_once_t aafs_access_control = PTHREAD_ONCE_INIT;
787
 
static char *aafs_access = NULL;
788
 
 
789
 
static void aafs_access_init_once(void)
790
 
{
791
 
        char *aafs;
792
 
        int ret;
793
 
 
794
 
        ret = aa_find_mountpoint(&aafs);
795
 
        if (ret < 0)
796
 
                return;
797
 
 
798
 
        ret = asprintf(&aafs_access, "%s/.access", aafs);
799
 
        if (ret < 0)
800
 
                aafs_access = NULL;
801
 
 
802
 
        free(aafs);
803
 
}
804
 
 
805
 
/* "allow 0x00000000\ndeny 0x00000000\naudit 0x00000000\nquiet 0x00000000\n" */
806
 
#define QUERY_LABEL_REPLY_LEN   67
807
 
 
808
 
/**
809
 
 * aa_query_label - query the access(es) of a label
810
 
 * @mask: permission bits to query
811
 
 * @query: binary query string, must be offset by AA_QUERY_CMD_LABEL_SIZE
812
 
 * @size: size of the query string must include AA_QUERY_CMD_LABEL_SIZE
813
 
 * @allowed: upon successful return, will be 1 if query is allowed and 0 if not
814
 
 * @audited: upon successful return, will be 1 if query should be audited and 0
815
 
 *           if not
816
 
 *
817
 
 * Returns: 0 on success else -1 and sets errno. If -1 is returned and errno is
818
 
 *          ENOENT, the subject label in the query string is unknown to the
819
 
 *          kernel.
820
 
 */
821
 
int query_label(uint32_t mask, char *query, size_t size, int *allowed,
822
 
                int *audited)
823
 
{
824
 
        char buf[QUERY_LABEL_REPLY_LEN];
825
 
        uint32_t allow, deny, audit, quiet;
826
 
        int fd, ret, saved;
827
 
 
828
 
        if (!mask || size <= AA_QUERY_CMD_LABEL_SIZE) {
829
 
                errno = EINVAL;
830
 
                return -1;
831
 
        }
832
 
 
833
 
        ret = pthread_once(&aafs_access_control, aafs_access_init_once);
834
 
        if (ret) {
835
 
                errno = EINVAL;
836
 
                return -1;
837
 
        } else if (!aafs_access) {
838
 
                errno = ENOMEM;
839
 
                return -1;
840
 
        }
841
 
 
842
 
        fd = open(aafs_access, O_RDWR);
843
 
        if (fd == -1) {
844
 
                if (errno == ENOENT)
845
 
                        errno = EPROTONOSUPPORT;
846
 
                return -1;
847
 
        }
848
 
 
849
 
        memcpy(query, AA_QUERY_CMD_LABEL, AA_QUERY_CMD_LABEL_SIZE);
850
 
        errno = 0;
851
 
        ret = write(fd, query, size);
852
 
        if (ret != size) {
853
 
                if (ret >= 0)
854
 
                        errno = EPROTO;
855
 
                /* IMPORTANT: This is the only valid error path that can have
856
 
                 * errno set to ENOENT. It indicates that the subject label
857
 
                 * could not be found by the kernel.
858
 
                 */
859
 
                (void)close(fd);
860
 
                return -1;
861
 
        }
862
 
 
863
 
        ret = read(fd, buf, QUERY_LABEL_REPLY_LEN);
864
 
        saved = errno;
865
 
        (void)close(fd);
866
 
        errno = saved;
867
 
        if (ret != QUERY_LABEL_REPLY_LEN) {
868
 
                errno = EPROTO;
869
 
                return -1;
870
 
        }
871
 
 
872
 
        ret = sscanf(buf, "allow 0x%8" SCNx32 "\n"
873
 
                          "deny 0x%8"  SCNx32 "\n"
874
 
                          "audit 0x%8" SCNx32 "\n"
875
 
                          "quiet 0x%8" SCNx32 "\n",
876
 
                     &allow, &deny, &audit, &quiet);
877
 
        if (ret != 4) {
878
 
                errno = EPROTONOSUPPORT;
879
 
                return -1;
880
 
        }
881
 
 
882
 
        *allowed = mask & ~(allow & ~deny) ? 0 : 1;
883
 
        if (!(*allowed))
884
 
                audit = 0xFFFFFFFF;
885
 
        *audited = mask & ~(audit & ~quiet) ? 0 : 1;
886
 
 
887
 
        return 0;
888
 
}
889
 
 
890
 
/* export multiple aa_query_label symbols to compensate for downstream
891
 
 * releases with differing symbol versions. */
892
 
extern typeof((query_label)) __aa_query_label __attribute__((alias ("query_label")));
893
 
symbol_version(__aa_query_label, aa_query_label, APPARMOR_1.1);
894
 
default_symbol_version(query_label, aa_query_label, APPARMOR_2.9);
895
 
 
896
 
 
897
 
/**
898
 
 * aa_query_file_path_len - query access permissions for a file @path
899
 
 * @mask: permission bits to query
900
 
 * @label: apparmor label
901
 
 * @label_len: length of @label (does not include any terminating nul byte)
902
 
 * @path: file path to query permissions for
903
 
 * @path_len: length of @path (does not include any terminating nul byte)
904
 
 * @allowed: upon successful return, will be 1 if query is allowed and 0 if not
905
 
 * @audited: upon successful return, will be 1 if query should be audited and 0
906
 
 *           if not
907
 
 *
908
 
 * Returns: 0 on success else -1 and sets errno. If -1 is returned and errno is
909
 
 *          ENOENT, the subject label in the query string is unknown to the
910
 
 *          kernel.
911
 
 */
912
 
int aa_query_file_path_len(uint32_t mask, const char *label, size_t label_len,
913
 
                           const char *path, size_t path_len, int *allowed,
914
 
                           int *audited)
915
 
{
916
 
        autofree char *query = NULL;
917
 
 
918
 
        /* + 1 for null separator */
919
 
        size_t size = AA_QUERY_CMD_LABEL_SIZE + label_len + 1 + path_len;
920
 
        query = malloc(size + 1);
921
 
        if (!query)
922
 
                return -1;
923
 
        memcpy(query + AA_QUERY_CMD_LABEL_SIZE, label, label_len);
924
 
        /* null separator */
925
 
        query[AA_QUERY_CMD_LABEL_SIZE + label_len] = 0;
926
 
        query[AA_QUERY_CMD_LABEL_SIZE + label_len + 1] = AA_CLASS_FILE;
927
 
        memcpy(query + AA_QUERY_CMD_LABEL_SIZE + label_len + 2, path, path_len);
928
 
        return aa_query_label(mask, query, size , allowed, audited);
929
 
}
930
 
 
931
 
/**
932
 
 * aa_query_file_path - query access permissions for a file @path
933
 
 * @mask: permission bits to query
934
 
 * @label: apparmor label
935
 
 * @path: file path to query permissions for
936
 
 * @allowed: upon successful return, will be 1 if query is allowed and 0 if not
937
 
 * @audited: upon successful return, will be 1 if query should be audited and 0
938
 
 *           if not
939
 
 *
940
 
 * Returns: 0 on success else -1 and sets errno. If -1 is returned and errno is
941
 
 *          ENOENT, the subject label in the query string is unknown to the
942
 
 *          kernel.
943
 
 */
944
 
int aa_query_file_path(uint32_t mask, const char *label, const char *path,
945
 
                       int *allowed, int *audited)
946
 
{
947
 
        return aa_query_file_path_len(mask, label, strlen(label), path,
948
 
                                      strlen(path), allowed, audited);
949
 
}
950
 
 
951
 
/**
952
 
 * aa_query_link_path_len - query access permissions for a hard link @link
953
 
 * @label: apparmor label
954
 
 * @label_len: length of @label (does not include any terminating nul byte)
955
 
 * @target: file path that hard link will point to
956
 
 * @target_len: length of @target (does not include any terminating nul byte)
957
 
 * @link: file path of hard link
958
 
 * @link_len: length of @link (does not include any terminating nul byte)
959
 
 * @allowed: upon successful return, will be 1 if query is allowed and 0 if not
960
 
 * @audited: upon successful return, will be 1 if query should be audited and 0
961
 
 *           if not
962
 
 *
963
 
 * Returns: 0 on success else -1 and sets errno. If -1 is returned and errno is
964
 
 *          ENOENT, the subject label in the query string is unknown to the
965
 
 *          kernel.
966
 
 */
967
 
int aa_query_link_path_len(const char *label, size_t label_len,
968
 
                           const char *target, size_t target_len,
969
 
                           const char *link, size_t link_len,
970
 
                           int *allowed, int *audited)
971
 
{
972
 
        autofree char *query = NULL;
973
 
 
974
 
        /* + 1 for null separators */
975
 
        size_t size = AA_QUERY_CMD_LABEL_SIZE + label_len + 1 + target_len +
976
 
                1 + link_len;
977
 
        size_t pos = AA_QUERY_CMD_LABEL_SIZE;
978
 
 
979
 
        query = malloc(size);
980
 
        if (!query)
981
 
                return -1;
982
 
        memcpy(query + pos, label, label_len);
983
 
        /* null separator */
984
 
        pos += label_len;
985
 
        query[pos] = 0;
986
 
        query[++pos] = AA_CLASS_FILE;
987
 
        memcpy(query + pos + 1, link, link_len);
988
 
        /* The kernel does the query in two parts we could similate this
989
 
         * doing the following, however as long as policy is compiled
990
 
         * correctly this isn't requied, and it requires and extra round
991
 
         * trip to the kernel and adds a race on policy replacement between
992
 
         * the two queries.
993
 
         *
994
 
        int rc = aa_query_label(AA_MAY_LINK, query, size, allowed, audited);
995
 
        if (rc || !*allowed)
996
 
                return rc;
997
 
        */
998
 
        pos += 1 + link_len;
999
 
        query[pos] = 0;
1000
 
        memcpy(query + pos + 1, target, target_len);
1001
 
        return aa_query_label(AA_MAY_LINK, query, size, allowed, audited);
1002
 
}
1003
 
 
1004
 
/**
1005
 
 * aa_query_link_path - query access permissions for a hard link @link
1006
 
 * @label: apparmor label
1007
 
 * @target: file path that hard link will point to
1008
 
 * @link: file path of hard link
1009
 
 * @allowed: upon successful return, will be 1 if query is allowed and 0 if not
1010
 
 * @audited: upon successful return, will be 1 if query should be audited and 0
1011
 
 *           if not
1012
 
 *
1013
 
 * Returns: 0 on success else -1 and sets errno. If -1 is returned and errno is
1014
 
 *          ENOENT, the subject label in the query string is unknown to the
1015
 
 *          kernel.
1016
 
 */
1017
 
int aa_query_link_path(const char *label, const char *target, const char *link,
1018
 
                       int *allowed, int *audited)
1019
 
{
1020
 
        return aa_query_link_path_len(label, strlen(label), target,
1021
 
                                      strlen(target), link, strlen(link),
1022
 
                                      allowed, audited);
1023
 
}