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

« back to all changes in this revision

Viewing changes to changehat/libapparmor/change_hat.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
 
/*   $Id: change_hat.c 13 2006-04-12 21:43:34Z steve-beattie $
2
 
 
3
 
     Copyright (c) 2003, 2004, 2005, 2006 Novell, Inc. (All rights reserved)
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
 
*/
10
 
 
11
 
#define _GNU_SOURCE     /* for asprintf */
12
 
 
13
 
#include <stdlib.h>
14
 
#include <stdio.h>
15
 
#include <string.h>
16
 
#include <unistd.h>
17
 
#include <sys/types.h>
18
 
#include <sys/stat.h>
19
 
#include <sys/syscall.h>
20
 
#include <fcntl.h>
21
 
#include <errno.h>
22
 
#include <limits.h>
23
 
 
24
 
int change_hat(char *subprofile, unsigned int token)
25
 
{
26
 
        int rc = -1;
27
 
        int fd, ret, len = 0, ctlerr = 0;
28
 
        char *buf = NULL;
29
 
        const char *cmd = "changehat";
30
 
        char *ctl = NULL;
31
 
        pid_t tid = syscall(SYS_gettid);
32
 
 
33
 
        /* both may not be null */
34
 
        if (!(token || subprofile)) {
35
 
                errno = EINVAL;
36
 
                goto out;
37
 
        }
38
 
 
39
 
        if (subprofile && strnlen(subprofile, PATH_MAX + 1) > PATH_MAX) {
40
 
                errno = EPROTO;
41
 
                goto out;
42
 
        }
43
 
 
44
 
        len = asprintf(&buf, "%s %08x^%s", cmd, token,
45
 
                       subprofile ? subprofile : "");
46
 
        if (len < 0) {
47
 
                goto out;
48
 
        }
49
 
 
50
 
        ctlerr = asprintf(&ctl, "/proc/%d/attr/current", tid);
51
 
        if (ctlerr < 0) {
52
 
                goto out;
53
 
        }
54
 
 
55
 
        fd = open(ctl, O_WRONLY);
56
 
        if (fd == -1) {
57
 
                goto out;
58
 
        }
59
 
 
60
 
        ret = write(fd, buf, len);
61
 
        if (ret != len) {
62
 
                int saved;
63
 
                if (ret != -1) {
64
 
                        errno = EPROTO;
65
 
                }
66
 
                saved = errno;
67
 
                (void)close(fd);
68
 
                errno = saved;
69
 
                goto out;
70
 
        }
71
 
 
72
 
        rc = 0;
73
 
        (void)close(fd);
74
 
 
75
 
out:
76
 
        if (buf) {
77
 
                /* clear local copy of magic token before freeing */
78
 
                memset(buf, '\0', len);
79
 
                free(buf);
80
 
        }
81
 
        if (ctl) {
82
 
                free(ctl);
83
 
        }
84
 
        return rc;
85
 
}