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

« back to all changes in this revision

Viewing changes to tests/regression/apparmor/unix_fd_server.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
/* fd passing over unix sockets */
 
2
 
 
3
/*
 
4
 *      Copyright (C) 2002-2005 Novell/SUSE
 
5
 *      Copyright (C) 2010 Canonical, Ltd.
 
6
 *
 
7
 *      This program is free software; you can redistribute it and/or
 
8
 *      modify it under the terms of the GNU General Public License as
 
9
 *      published by the Free Software Foundation, version 2 of the
 
10
 *      License.
 
11
 */
 
12
 
 
13
/* this is very ugly */
 
14
 
 
15
#define _XOPEN_SOURCE 500
 
16
#include <stdio.h>
 
17
#include <unistd.h>
 
18
#include <sys/types.h>
 
19
#include <string.h>
 
20
#include <stdlib.h>
 
21
#include <sys/socket.h>
 
22
#include <alloca.h>
 
23
#include <fcntl.h>
 
24
#include <sys/uio.h>
 
25
#include <sys/un.h>
 
26
#include <sys/wait.h>
 
27
#include <sys/poll.h>
 
28
#include <errno.h>
 
29
#include <stdlib.h>
 
30
 
 
31
int main (int argc, char * argv[]) {
 
32
        int sock, in_sock, fd;
 
33
        struct sockaddr_un local, remote;
 
34
        int len, exec_now, pfd_ret;
 
35
        socklen_t len2;
 
36
        char comparison_buffer[17];
 
37
        char inbound_buffer[17];
 
38
        struct iovec vect;
 
39
        struct msghdr mesg;
 
40
        struct cmsghdr *ctrl_mesg;
 
41
        struct pollfd pfd;
 
42
 
 
43
 
 
44
        exec_now = 0;
 
45
 
 
46
        if (argc < 4 || argc > 5 || (argc == 5 && (strcmp(argv[4], "delete_file") != 0))) {
 
47
                fprintf(stderr, "Usage: %s <file>\n", argv[0]);
 
48
                return(1);
 
49
        }
 
50
 
 
51
        if ((fd = open(argv[1], O_RDONLY)) < 0) {
 
52
                fprintf(stderr, "FAIL - open failed: %s\n",
 
53
                        strerror(errno));
 
54
                return(1);
 
55
        }
 
56
 
 
57
        if (pread(fd, comparison_buffer, 16,0) <= 0) {
 
58
                fprintf(stderr, "FAIL - read failed: %s\n",
 
59
                        strerror(errno));
 
60
                return(1);
 
61
        }
 
62
 
 
63
        if (argc == 5) {
 
64
                if (unlink(argv[1]) == -1){
 
65
                    fprintf(stderr, "FAIL: unlink before passing fd - %s\n",
 
66
                            strerror(errno));
 
67
                    return 1;
 
68
                }
 
69
        }
 
70
 
 
71
        sock = socket(AF_UNIX, SOCK_STREAM, 0);
 
72
        if (sock == -1) {
 
73
                fprintf(stderr, "FAIL - socket failed: %s\n",
 
74
                        strerror(errno));
 
75
                return(1);
 
76
        }
 
77
 
 
78
        local.sun_family = AF_UNIX;
 
79
        strcpy(local.sun_path, argv[2]);
 
80
        unlink(local.sun_path);
 
81
        len = strlen(local.sun_path) + sizeof(local.sun_family);
 
82
        
 
83
        if (bind(sock, (struct sockaddr *) &local, len) != 0) {
 
84
                fprintf(stderr, "FAIL - bind failed: %s\n",
 
85
                        strerror(errno));
 
86
                return(1);
 
87
        }
 
88
        
 
89
        if (listen(sock, 2) != 0) {
 
90
                fprintf(stderr, "FAIL - listen failed: %s\n",
 
91
                        strerror(errno));
 
92
                return(1);
 
93
        }
 
94
 
 
95
        /* exec the client */   
 
96
        int pid = fork();
 
97
        if (!pid) {
 
98
                execlp(argv[3], argv[3], argv[2], NULL);
 
99
                exit(0);
 
100
        }
 
101
 
 
102
        len2 = sizeof(remote);
 
103
 
 
104
        pfd.fd = sock;
 
105
        pfd.events = POLLIN;
 
106
        pfd_ret = poll(&pfd, 1, 500);
 
107
        if (pfd_ret == 1) {
 
108
                if ((in_sock = accept(sock, (struct sockaddr*)&remote, &len2)) == -1) {
 
109
                        fprintf(stderr, "FAIL - accept: %s\n",
 
110
                                strerror(errno));
 
111
                        exit(1);
 
112
                }
 
113
 
 
114
        vect.iov_base = argv[2];
 
115
        vect.iov_len = strlen(argv[2]) + 1;
 
116
 
 
117
        mesg.msg_name = NULL;
 
118
        mesg.msg_namelen = 0;
 
119
        mesg.msg_iov = &vect;
 
120
        mesg.msg_iovlen = 1;
 
121
 
 
122
        ctrl_mesg = alloca(sizeof(struct cmsghdr) + sizeof(fd));
 
123
        ctrl_mesg->cmsg_len = sizeof(struct cmsghdr) + sizeof(fd);
 
124
        ctrl_mesg->cmsg_level = SOL_SOCKET;
 
125
        ctrl_mesg->cmsg_type = SCM_RIGHTS;
 
126
 
 
127
        memcpy(CMSG_DATA(ctrl_mesg), &fd, sizeof(fd));
 
128
        mesg.msg_control = ctrl_mesg;
 
129
        mesg.msg_controllen = ctrl_mesg->cmsg_len;
 
130
 
 
131
        /* try to send it */
 
132
        if (sendmsg(in_sock, &mesg, 0) != vect.iov_len) {
 
133
                fprintf(stderr, "FAIL - could not sendmsg\n");
 
134
                exit(1);
 
135
        }
 
136
 
 
137
        /* Check for info re: reading the file */
 
138
        memset(inbound_buffer, 0, sizeof(inbound_buffer));
 
139
        if (recv(in_sock, inbound_buffer, 16,0) == -1 ) {
 
140
                fprintf(stderr, "FAIL - recv %s\n",
 
141
                        strerror(errno));
 
142
                exit(1);
 
143
        }
 
144
 
 
145
        if (strncmp(comparison_buffer, inbound_buffer,10) != 0) {
 
146
                fprintf(stderr, "FAIL - buffer comparison.  Got \"%s\", expected \"%s\"\n", inbound_buffer, comparison_buffer);
 
147
                exit(1);
 
148
        } else {
 
149
                printf("PASS\n");
 
150
        }       
 
151
                
 
152
        exit(0);
 
153
        } else {
 
154
                /* they timed out */
 
155
                fprintf(stderr, "FAIL - poll() timed out\n");
 
156
                exit(1);
 
157
        }
 
158
 
 
159
}
 
160