~ubuntu-branches/ubuntu/natty/eglibc/natty-security

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* Copyright (C) 2006 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA.  */

#include <pthread.h>
#include <errno.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <limits.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

static void *
tf (void *arg)
{
  int fd = (long) arg;
  int pid = getpid ();
#ifdef TIMED
  struct timespec ts;
#endif

  if (pthread_mutex_lock (&mutex) != 0)
    {
      puts ("Could not lock mutex in thread");
      return (void *) 1L;
    }

  write (fd, &pid, sizeof (int));

  if (pthread_mutex_unlock (&mutex2) != 0)
    {
      puts ("Could not unlock mutex2 in thread");
      return (void *) 1L;
    }

#ifdef TIMED
  ts.tv_sec = INT_MAX;
  ts.tv_nsec = 0;
  if (pthread_cond_timedwait (&cond, &mutex, &ts) != 0)
    {
      puts ("pthread_cond_wait failed in thread");
      return (void *) 1L;
    }
#else
  if (pthread_cond_wait (&cond, &mutex) != 0)
    {
      puts ("pthread_cond_wait failed in thread");
      return (void *) 1L;
    }
#endif

  if (pthread_mutex_unlock (&mutex) != 0)
    {
      puts ("Could not unlock mutex in thread");
      return (void *) 1L;
    }

  return NULL;
}

static void
sig_segv (int unused)
{
  exit (2);
}

int
do_test (void)
{
  int fds[2];
  pthread_t thr;

  if (pipe (fds) < 0)
    {
      printf ("couldn't create pipe: %m\n");
      return 1;
    }

  pid_t pid = fork ();
  if (pid < 0)
    {
      printf ("fork failed: %m\n");
      return 1;
    }

  if (!pid)
    {
      void *ret;

      close (fds[0]);
      signal (SIGSEGV, sig_segv);
      pthread_mutex_lock (&mutex2);
      pthread_create (&thr, NULL, tf, (void *) (long) (fds[1]));
      /* Wait for the thread to go into cond_wait.  */
      pthread_mutex_lock (&mutex2);
      pthread_mutex_lock (&mutex);
      write (fds[1], "", 1);
      pthread_join (thr, &ret);
      exit (0);
    }

  close (fds[1]);

  int child_pid;
  if (TEMP_FAILURE_RETRY (read (fds[0], &child_pid, sizeof (int)))
      != sizeof (int))
    {
      puts ("could not read pid from child");
      return 1;
    }

  char dummy;
  if (TEMP_FAILURE_RETRY (read (fds[0], &dummy, 1)) != 1)
    {
      puts ("could not read sync byte from child");
      return 1;
    }

  close (fds[0]);

  /* The sync byte may come immediately after the mutex is released;
     wait a little bit more to make sure the thread has suspended, to
     test the right bug.  */
  struct timespec ts;
  ts.tv_sec = 0;
  ts.tv_nsec = 200000000;
  nanosleep (&ts, NULL);

  kill (child_pid, SIGSEGV);

  pid_t termpid;
  int status;
  termpid = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
  if (termpid == -1)
    {
      printf ("waitpid failed: %m\n");
      return 1;
    }
  else if (termpid != pid)
    {
      printf ("waitpid returned %ld != %ld\n",
	      (long int) termpid, (long int) pid);
      return 1;
    }
  else if (!WIFEXITED (status) || WEXITSTATUS (status) != 2)
    {
      puts ("child hasn't exited with exit status 2");
      return 1;
    }

  return 0;
}

#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"