~ubuntu-branches/ubuntu/quantal/apparmor/quantal-updates

« back to all changes in this revision

Viewing changes to tests/regression/subdomain/syscall_chroot.c

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2007-03-23 16:42:01 UTC
  • Revision ID: james.westby@ubuntu.com-20070323164201-jkax6f0oku087b7l
Tags: upstream-2.0.1+510.dfsg
ImportĀ upstreamĀ versionĀ 2.0.1+510.dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: syscall_chroot.c 61 2006-05-19 18:32:14Z steve-beattie $ */
 
2
 
 
3
/*
 
4
 *      Copyright (C) 2002-2005 Novell/SUSE
 
5
 *
 
6
 *      This program is free software; you can redistribute it and/or
 
7
 *      modify it under the terms of the GNU General Public License as
 
8
 *      published by the Free Software Foundation, version 2 of the
 
9
 *      License.
 
10
 */
 
11
 
 
12
#include <stdio.h>
 
13
#include <unistd.h>
 
14
#include <errno.h>
 
15
#include <sys/types.h>
 
16
#include <string.h>
 
17
#include <stdlib.h>
 
18
#include <dirent.h>
 
19
 
 
20
struct dirlist {
 
21
        struct dirlist * next;
 
22
        struct dirent dirent;
 
23
};
 
24
 
 
25
struct dirlist *
 
26
read_directory (char * dirname) {
 
27
 
 
28
        DIR * dir;
 
29
        struct dirent * dirent;
 
30
        struct dirlist * dirlist = NULL, * tmp;
 
31
 
 
32
        if (!dirname) return NULL;
 
33
        dir = opendir (dirname);
 
34
 
 
35
        if (!dir) {
 
36
                perror ("FAIL: couldn't open directory");
 
37
                exit (errno);
 
38
        }
 
39
 
 
40
        while ((dirent = readdir (dir)) != NULL) {
 
41
                tmp = malloc (sizeof (struct dirlist));
 
42
                if (!tmp) {
 
43
                        perror ("FAIL: failed allocation");
 
44
                        exit (1);
 
45
                }
 
46
 
 
47
                memcpy (&tmp->dirent, dirent, sizeof (struct dirent));
 
48
                tmp->next = dirlist;
 
49
                dirlist = tmp;
 
50
                /* printf ("Adding '%s'\n", tmp->dirent.d_name); */
 
51
        }
 
52
 
 
53
        return dirlist;
 
54
}
 
55
 
 
56
static inline int
 
57
contains_dir (struct dirlist * dirlist, char * dirname) {
 
58
        struct dirlist * this;
 
59
 
 
60
        for (this = dirlist; this != NULL; this = this->next)
 
61
                if (strcmp (this->dirent.d_name, dirname) == 0)
 
62
                        return 1;
 
63
 
 
64
        return 0;
 
65
}
 
66
 
 
67
int
 
68
compare_dirlists (struct dirlist * a, struct dirlist * b) {
 
69
        struct dirlist * this;
 
70
 
 
71
        for (this = a; this != NULL; this = this->next) 
 
72
                if (!contains_dir (b, this->dirent.d_name))
 
73
                        return 1;
 
74
 
 
75
        for (this = b; this != NULL; this = this->next) 
 
76
                if (!contains_dir (a, this->dirent.d_name))
 
77
                        return 1;
 
78
 
 
79
        return 0;
 
80
}       
 
81
        
 
82
                
 
83
int main(int argc, char *argv[])
 
84
{
 
85
        struct dirlist * before, * after;
 
86
 
 
87
        if (argc != 2) {
 
88
                fprintf(stderr, "usage: %s <dir>\n",
 
89
                                argv[0]);
 
90
                return 1;
 
91
        }
 
92
 
 
93
        before = read_directory("/");
 
94
 
 
95
        if (chroot(argv[1]) == -1) {
 
96
                perror("FAIL: chroot failed");
 
97
                return 1;
 
98
        }
 
99
 
 
100
        after = read_directory("/");
 
101
 
 
102
        if (compare_dirlists(before, after) == 0) {
 
103
                fprintf (stderr, "FAIL: root directories are the same\n");
 
104
                return 1;
 
105
        }
 
106
 
 
107
        printf("PASS\n");
 
108
 
 
109
        return 0;
 
110
}