1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker * Copyright (C) 2013 Linux Test Project
3*49cdfc7eSAndroid Build Coastguard Worker *
4*49cdfc7eSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or
5*49cdfc7eSAndroid Build Coastguard Worker * modify it under the terms of version 2 of the GNU General Public
6*49cdfc7eSAndroid Build Coastguard Worker * License as published by the Free Software Foundation.
7*49cdfc7eSAndroid Build Coastguard Worker *
8*49cdfc7eSAndroid Build Coastguard Worker * This program is distributed in the hope that it would be useful,
9*49cdfc7eSAndroid Build Coastguard Worker * but WITHOUT ANY WARRANTY; without even the implied warranty of
10*49cdfc7eSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11*49cdfc7eSAndroid Build Coastguard Worker *
12*49cdfc7eSAndroid Build Coastguard Worker * Further, this software is distributed without any warranty that it
13*49cdfc7eSAndroid Build Coastguard Worker * is free of the rightful claim of any third person regarding
14*49cdfc7eSAndroid Build Coastguard Worker * infringement or the like. Any license provided herein, whether
15*49cdfc7eSAndroid Build Coastguard Worker * implied or otherwise, applies only to this software file. Patent
16*49cdfc7eSAndroid Build Coastguard Worker * licenses, if any, provided herein do not apply to combinations of
17*49cdfc7eSAndroid Build Coastguard Worker * this program with other software, or any other product whatsoever.
18*49cdfc7eSAndroid Build Coastguard Worker *
19*49cdfc7eSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License
20*49cdfc7eSAndroid Build Coastguard Worker * along with this program; if not, write the Free Software
21*49cdfc7eSAndroid Build Coastguard Worker * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22*49cdfc7eSAndroid Build Coastguard Worker * 02110-1301, USA.
23*49cdfc7eSAndroid Build Coastguard Worker */
24*49cdfc7eSAndroid Build Coastguard Worker /*
25*49cdfc7eSAndroid Build Coastguard Worker * reproducer for:
26*49cdfc7eSAndroid Build Coastguard Worker * BUG: unable to handle kernel NULL ptr deref in selinux_socket_unix_may_send
27*49cdfc7eSAndroid Build Coastguard Worker * fixed in 3.9.0-0.rc5:
28*49cdfc7eSAndroid Build Coastguard Worker * commit ded34e0fe8fe8c2d595bfa30626654e4b87621e0
29*49cdfc7eSAndroid Build Coastguard Worker * Author: Paul Moore <[email protected]>
30*49cdfc7eSAndroid Build Coastguard Worker * Date: Mon Mar 25 03:18:33 2013 +0000
31*49cdfc7eSAndroid Build Coastguard Worker * unix: fix a race condition in unix_release()
32*49cdfc7eSAndroid Build Coastguard Worker */
33*49cdfc7eSAndroid Build Coastguard Worker
34*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
35*49cdfc7eSAndroid Build Coastguard Worker #include <sys/ipc.h>
36*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
39*49cdfc7eSAndroid Build Coastguard Worker #include <sys/un.h>
40*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include <limits.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
45*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
46*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
47*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/sem.h"
48*49cdfc7eSAndroid Build Coastguard Worker
49*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "sendmsg02";
50*49cdfc7eSAndroid Build Coastguard Worker
51*49cdfc7eSAndroid Build Coastguard Worker static int sem_id;
52*49cdfc7eSAndroid Build Coastguard Worker static int tflag;
53*49cdfc7eSAndroid Build Coastguard Worker static char *t_opt;
54*49cdfc7eSAndroid Build Coastguard Worker static option_t options[] = {
55*49cdfc7eSAndroid Build Coastguard Worker {"s:", &tflag, &t_opt},
56*49cdfc7eSAndroid Build Coastguard Worker {NULL, NULL, NULL}
57*49cdfc7eSAndroid Build Coastguard Worker };
58*49cdfc7eSAndroid Build Coastguard Worker
59*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
60*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
61*49cdfc7eSAndroid Build Coastguard Worker
client(int id,int pipefd[])62*49cdfc7eSAndroid Build Coastguard Worker static void client(int id, int pipefd[])
63*49cdfc7eSAndroid Build Coastguard Worker {
64*49cdfc7eSAndroid Build Coastguard Worker int fd, semval;
65*49cdfc7eSAndroid Build Coastguard Worker char data[] = "123456789";
66*49cdfc7eSAndroid Build Coastguard Worker struct iovec w;
67*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr_un sa;
68*49cdfc7eSAndroid Build Coastguard Worker struct msghdr mh;
69*49cdfc7eSAndroid Build Coastguard Worker struct cmsghdr cmh;
70*49cdfc7eSAndroid Build Coastguard Worker
71*49cdfc7eSAndroid Build Coastguard Worker close(pipefd[0]);
72*49cdfc7eSAndroid Build Coastguard Worker
73*49cdfc7eSAndroid Build Coastguard Worker memset(&sa, 0, sizeof(sa));
74*49cdfc7eSAndroid Build Coastguard Worker sa.sun_family = AF_UNIX;
75*49cdfc7eSAndroid Build Coastguard Worker snprintf(sa.sun_path, sizeof(sa.sun_path), "socket_test%d", id);
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker w.iov_base = data;
78*49cdfc7eSAndroid Build Coastguard Worker w.iov_len = 10;
79*49cdfc7eSAndroid Build Coastguard Worker
80*49cdfc7eSAndroid Build Coastguard Worker memset(&cmh, 0, sizeof(cmh));
81*49cdfc7eSAndroid Build Coastguard Worker mh.msg_control = &cmh;
82*49cdfc7eSAndroid Build Coastguard Worker mh.msg_controllen = sizeof(cmh);
83*49cdfc7eSAndroid Build Coastguard Worker
84*49cdfc7eSAndroid Build Coastguard Worker memset(&mh, 0, sizeof(mh));
85*49cdfc7eSAndroid Build Coastguard Worker mh.msg_name = &sa;
86*49cdfc7eSAndroid Build Coastguard Worker mh.msg_namelen = sizeof(struct sockaddr_un);
87*49cdfc7eSAndroid Build Coastguard Worker mh.msg_iov = &w;
88*49cdfc7eSAndroid Build Coastguard Worker mh.msg_iovlen = 1;
89*49cdfc7eSAndroid Build Coastguard Worker
90*49cdfc7eSAndroid Build Coastguard Worker do {
91*49cdfc7eSAndroid Build Coastguard Worker fd = socket(AF_UNIX, SOCK_DGRAM, 0);
92*49cdfc7eSAndroid Build Coastguard Worker write(pipefd[1], &fd, 1);
93*49cdfc7eSAndroid Build Coastguard Worker sendmsg(fd, &mh, MSG_NOSIGNAL);
94*49cdfc7eSAndroid Build Coastguard Worker close(fd);
95*49cdfc7eSAndroid Build Coastguard Worker semval = semctl(sem_id, 0, GETVAL);
96*49cdfc7eSAndroid Build Coastguard Worker } while (semval != 0);
97*49cdfc7eSAndroid Build Coastguard Worker close(pipefd[1]);
98*49cdfc7eSAndroid Build Coastguard Worker }
99*49cdfc7eSAndroid Build Coastguard Worker
server(int id,int pipefd[])100*49cdfc7eSAndroid Build Coastguard Worker static void server(int id, int pipefd[])
101*49cdfc7eSAndroid Build Coastguard Worker {
102*49cdfc7eSAndroid Build Coastguard Worker int fd, semval;
103*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr_un sa;
104*49cdfc7eSAndroid Build Coastguard Worker
105*49cdfc7eSAndroid Build Coastguard Worker close(pipefd[1]);
106*49cdfc7eSAndroid Build Coastguard Worker
107*49cdfc7eSAndroid Build Coastguard Worker memset(&sa, 0, sizeof(sa));
108*49cdfc7eSAndroid Build Coastguard Worker sa.sun_family = AF_UNIX;
109*49cdfc7eSAndroid Build Coastguard Worker snprintf(sa.sun_path, sizeof(sa.sun_path), "socket_test%d", id);
110*49cdfc7eSAndroid Build Coastguard Worker
111*49cdfc7eSAndroid Build Coastguard Worker do {
112*49cdfc7eSAndroid Build Coastguard Worker fd = socket(AF_UNIX, SOCK_DGRAM, 0);
113*49cdfc7eSAndroid Build Coastguard Worker unlink(sa.sun_path);
114*49cdfc7eSAndroid Build Coastguard Worker bind(fd, (struct sockaddr *) &sa, sizeof(struct sockaddr_un));
115*49cdfc7eSAndroid Build Coastguard Worker read(pipefd[0], &fd, 1);
116*49cdfc7eSAndroid Build Coastguard Worker close(fd);
117*49cdfc7eSAndroid Build Coastguard Worker semval = semctl(sem_id, 0, GETVAL);
118*49cdfc7eSAndroid Build Coastguard Worker } while (semval != 0);
119*49cdfc7eSAndroid Build Coastguard Worker close(pipefd[0]);
120*49cdfc7eSAndroid Build Coastguard Worker }
121*49cdfc7eSAndroid Build Coastguard Worker
reproduce(int seconds)122*49cdfc7eSAndroid Build Coastguard Worker static void reproduce(int seconds)
123*49cdfc7eSAndroid Build Coastguard Worker {
124*49cdfc7eSAndroid Build Coastguard Worker int i, status, pipefd[2];
125*49cdfc7eSAndroid Build Coastguard Worker int child_pairs = sysconf(_SC_NPROCESSORS_ONLN)*4;
126*49cdfc7eSAndroid Build Coastguard Worker int child_count = 0;
127*49cdfc7eSAndroid Build Coastguard Worker int *child_pids;
128*49cdfc7eSAndroid Build Coastguard Worker int child_pid;
129*49cdfc7eSAndroid Build Coastguard Worker union semun u;
130*49cdfc7eSAndroid Build Coastguard Worker
131*49cdfc7eSAndroid Build Coastguard Worker child_pids = SAFE_MALLOC(cleanup, sizeof(int) * child_pairs * 2);
132*49cdfc7eSAndroid Build Coastguard Worker
133*49cdfc7eSAndroid Build Coastguard Worker u.val = 1;
134*49cdfc7eSAndroid Build Coastguard Worker if (semctl(sem_id, 0, SETVAL, u) == -1)
135*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup, "couldn't set semval to 1");
136*49cdfc7eSAndroid Build Coastguard Worker
137*49cdfc7eSAndroid Build Coastguard Worker /* fork child for each client/server pair */
138*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < child_pairs*2; i++) {
139*49cdfc7eSAndroid Build Coastguard Worker if (i%2 == 0) {
140*49cdfc7eSAndroid Build Coastguard Worker if (pipe(pipefd) < 0) {
141*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TBROK | TERRNO, "pipe failed");
142*49cdfc7eSAndroid Build Coastguard Worker break;
143*49cdfc7eSAndroid Build Coastguard Worker }
144*49cdfc7eSAndroid Build Coastguard Worker }
145*49cdfc7eSAndroid Build Coastguard Worker
146*49cdfc7eSAndroid Build Coastguard Worker child_pid = fork();
147*49cdfc7eSAndroid Build Coastguard Worker switch (child_pid) {
148*49cdfc7eSAndroid Build Coastguard Worker case -1:
149*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TBROK | TERRNO, "fork");
150*49cdfc7eSAndroid Build Coastguard Worker break;
151*49cdfc7eSAndroid Build Coastguard Worker case 0:
152*49cdfc7eSAndroid Build Coastguard Worker if (i%2 == 0)
153*49cdfc7eSAndroid Build Coastguard Worker server(i, pipefd);
154*49cdfc7eSAndroid Build Coastguard Worker else
155*49cdfc7eSAndroid Build Coastguard Worker client(i-1, pipefd);
156*49cdfc7eSAndroid Build Coastguard Worker exit(0);
157*49cdfc7eSAndroid Build Coastguard Worker default:
158*49cdfc7eSAndroid Build Coastguard Worker child_pids[child_count++] = child_pid;
159*49cdfc7eSAndroid Build Coastguard Worker };
160*49cdfc7eSAndroid Build Coastguard Worker
161*49cdfc7eSAndroid Build Coastguard Worker /* this process can close the pipe now */
162*49cdfc7eSAndroid Build Coastguard Worker if (i%2 == 0) {
163*49cdfc7eSAndroid Build Coastguard Worker close(pipefd[0]);
164*49cdfc7eSAndroid Build Coastguard Worker close(pipefd[1]);
165*49cdfc7eSAndroid Build Coastguard Worker }
166*49cdfc7eSAndroid Build Coastguard Worker }
167*49cdfc7eSAndroid Build Coastguard Worker
168*49cdfc7eSAndroid Build Coastguard Worker /* let clients/servers run for a while, then clear semval to signal
169*49cdfc7eSAndroid Build Coastguard Worker * they should stop running now */
170*49cdfc7eSAndroid Build Coastguard Worker if (child_count == child_pairs*2)
171*49cdfc7eSAndroid Build Coastguard Worker sleep(seconds);
172*49cdfc7eSAndroid Build Coastguard Worker
173*49cdfc7eSAndroid Build Coastguard Worker u.val = 0;
174*49cdfc7eSAndroid Build Coastguard Worker if (semctl(sem_id, 0, SETVAL, u) == -1) {
175*49cdfc7eSAndroid Build Coastguard Worker /* kill children if setting semval failed */
176*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < child_count; i++)
177*49cdfc7eSAndroid Build Coastguard Worker kill(child_pids[i], SIGKILL);
178*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TBROK | TERRNO, "couldn't set semval to 0");
179*49cdfc7eSAndroid Build Coastguard Worker }
180*49cdfc7eSAndroid Build Coastguard Worker
181*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < child_count; i++) {
182*49cdfc7eSAndroid Build Coastguard Worker if (waitpid(child_pids[i], &status, 0) == -1)
183*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TBROK | TERRNO, "waitpid for %d failed",
184*49cdfc7eSAndroid Build Coastguard Worker child_pids[i]);
185*49cdfc7eSAndroid Build Coastguard Worker if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
186*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "child %d returns %d", i, status);
187*49cdfc7eSAndroid Build Coastguard Worker }
188*49cdfc7eSAndroid Build Coastguard Worker free(child_pids);
189*49cdfc7eSAndroid Build Coastguard Worker }
190*49cdfc7eSAndroid Build Coastguard Worker
help(void)191*49cdfc7eSAndroid Build Coastguard Worker static void help(void)
192*49cdfc7eSAndroid Build Coastguard Worker {
193*49cdfc7eSAndroid Build Coastguard Worker printf(" -s NUM Number of seconds to run.\n");
194*49cdfc7eSAndroid Build Coastguard Worker }
195*49cdfc7eSAndroid Build Coastguard Worker
main(int argc,char * argv[])196*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
197*49cdfc7eSAndroid Build Coastguard Worker {
198*49cdfc7eSAndroid Build Coastguard Worker int lc;
199*49cdfc7eSAndroid Build Coastguard Worker long seconds;
200*49cdfc7eSAndroid Build Coastguard Worker
201*49cdfc7eSAndroid Build Coastguard Worker tst_parse_opts(argc, argv, options, &help);
202*49cdfc7eSAndroid Build Coastguard Worker setup();
203*49cdfc7eSAndroid Build Coastguard Worker
204*49cdfc7eSAndroid Build Coastguard Worker seconds = tflag ? SAFE_STRTOL(NULL, t_opt, 1, LONG_MAX) : 15;
205*49cdfc7eSAndroid Build Coastguard Worker for (lc = 0; TEST_LOOPING(lc); lc++)
206*49cdfc7eSAndroid Build Coastguard Worker reproduce(seconds);
207*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "finished after %ld seconds", seconds);
208*49cdfc7eSAndroid Build Coastguard Worker
209*49cdfc7eSAndroid Build Coastguard Worker cleanup();
210*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
211*49cdfc7eSAndroid Build Coastguard Worker }
212*49cdfc7eSAndroid Build Coastguard Worker
setup(void)213*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
214*49cdfc7eSAndroid Build Coastguard Worker {
215*49cdfc7eSAndroid Build Coastguard Worker tst_require_root();
216*49cdfc7eSAndroid Build Coastguard Worker tst_tmpdir();
217*49cdfc7eSAndroid Build Coastguard Worker
218*49cdfc7eSAndroid Build Coastguard Worker sem_id = semget(IPC_PRIVATE, 1, IPC_CREAT | S_IRWXU);
219*49cdfc7eSAndroid Build Coastguard Worker if (sem_id == -1)
220*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, NULL, "Couldn't allocate semaphore");
221*49cdfc7eSAndroid Build Coastguard Worker
222*49cdfc7eSAndroid Build Coastguard Worker TEST_PAUSE;
223*49cdfc7eSAndroid Build Coastguard Worker }
224*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)225*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
226*49cdfc7eSAndroid Build Coastguard Worker {
227*49cdfc7eSAndroid Build Coastguard Worker semctl(sem_id, 0, IPC_RMID);
228*49cdfc7eSAndroid Build Coastguard Worker tst_rmdir();
229*49cdfc7eSAndroid Build Coastguard Worker }
230