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

« back to all changes in this revision

Viewing changes to tests/regression/apparmor/deleted.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
 
 
10
#include <stdio.h>
 
11
#include <unistd.h>
 
12
#include <errno.h>
 
13
#include <sys/types.h>
 
14
#include <sys/stat.h>
 
15
#include <fcntl.h>
 
16
#include <string.h>
 
17
#include <stdlib.h>
 
18
#include <linux/unistd.h>
 
19
 
 
20
#include "changehat.h"
 
21
 
 
22
/* A test to validate that we are properly handling the kernel appending
 
23
 * (deleted) in d_path lookup.
 
24
 * To acheive this the file is opened (the read/write of the file is just to
 
25
 * make sure everything is working as expected), deleted without closing the
 
26
 * file reference, and doing a changehat.
 
27
 * The file is then used inside of the changehat.  This forces the file
 
28
 * access permission to be revalidated which will cause a path lookup and
 
29
 * expose if the module is not handling the kernel appending (deleted).
 
30
 */
 
31
int main(int argc, char *argv[])
 
32
{
 
33
        int fd, fd2, rc;
 
34
        int ret = 0;            /* run through all tests to see what breaks */
 
35
        const char *data="hello world";
 
36
        char buf[128];
 
37
 
 
38
        if (argc != 3){
 
39
            int i;
 
40
                fprintf(stderr, "usage: %s hat file\n",
 
41
                        argv[0]);
 
42
                for (i=0; i < argc; i++) {
 
43
                    fprintf(stderr, "arg%d: %s\n", i, argv[i]);
 
44
                }
 
45
                return 1;
 
46
        }
 
47
 
 
48
        fd = open(argv[2], O_RDWR, 0);
 
49
        if (fd == -1){
 
50
                fprintf(stderr, "FAIL: open %s before changehat failed - %s\n",
 
51
                        argv[2],
 
52
                        strerror(errno));
 
53
                return 1;
 
54
        }
 
55
 
 
56
        rc = write(fd, data, strlen(data));
 
57
        if (rc != strlen(data)){
 
58
                fprintf(stderr, "FAIL: write before changehat failed - %s\n",
 
59
                        strerror(errno));
 
60
                return 1;
 
61
        }
 
62
 
 
63
        rc = unlink(argv[2]);
 
64
        if (rc == -1){
 
65
                fprintf(stderr, "FAIL: unlink before changehat failed - %s\n",
 
66
                        strerror(errno));
 
67
                return 1;
 
68
        }
 
69
        
 
70
        if (strcmp(argv[1], "nochange") != 0){
 
71
                rc = change_hat(argv[1], SD_ID_MAGIC+1);
 
72
                if (rc == -1){
 
73
                        fprintf(stderr, "FAIL: changehat %s failed - %s\n",
 
74
                                argv[1], strerror(errno));
 
75
                        exit(1);
 
76
                }
 
77
        }
 
78
        /******** The actual tests for (deleted) begin here *******/
 
79
 
 
80
        /* changehat causes revalidation */
 
81
        (void)lseek(fd, 0, SEEK_SET);
 
82
        rc = read(fd, buf, sizeof(buf));
 
83
 
 
84
        if (rc != strlen(data)){
 
85
                fprintf(stderr, "FAIL: read1 after changehat failed - %s\n",
 
86
                        strerror(errno));
 
87
                ret = 1;
 
88
        }
 
89
 
 
90
        /* test that we can create the file.  Not necessarily a (deleted)
 
91
         * case but lets us flush out other combinations.
 
92
         */
 
93
        fd2=creat(argv[2], S_IRUSR | S_IWUSR);
 
94
        if (fd2 == -1){
 
95
                fprintf(stderr, "FAIL: creat %s - %s\n",
 
96
                        argv[2],
 
97
                        strerror(errno));
 
98
                ret = 1;
 
99
        }
 
100
        close(fd2);
 
101
 
 
102
        /* reread after closing the created file, this should be revalidated
 
103
         * and force a lookup that still has (deleted) appended
 
104
         */
 
105
        (void)lseek(fd, 0, SEEK_SET);
 
106
        rc=read(fd, buf, sizeof(buf));
 
107
 
 
108
        if (rc != strlen(data)){
 
109
                fprintf(stderr, "FAIL: read2 after changehat failed - %s\n",
 
110
                        strerror(errno));
 
111
                ret = 1;
 
112
        }
 
113
 
 
114
        close(fd);
 
115
 
 
116
        /* should be able to open it.  Shouldn't have (deleted) */
 
117
        fd=open(argv[2], O_RDWR, 0);
 
118
        if (fd == -1){
 
119
                fprintf(stderr, "FAIL: open %s after creat failed - %s\n",
 
120
                        argv[2],
 
121
                        strerror(errno));
 
122
                ret = 1;
 
123
        }
 
124
 
 
125
        close(fd);
 
126
 
 
127
        /* Hmm it would be nice to add a fork/exec test for (deleted) but
 
128
         * changehat does it for now.
 
129
         */
 
130
 
 
131
        if (!ret)
 
132
            printf("PASS\n");
 
133
 
 
134
        return ret;
 
135
}