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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
/* Copyright (C) 2015 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, see
<http://www.gnu.org/licenses/>. */
#include <errno.h>
#include <pwd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Check if two passwd structs contain the same data. */
static bool
equal (const struct passwd *a, const struct passwd *b)
{
return strcmp (a->pw_name, b->pw_name) == 0
&& strcmp (a->pw_passwd, b->pw_passwd) == 0
&& a->pw_uid == b->pw_uid
&& a->pw_gid == b->pw_gid
&& strcmp (a->pw_gecos, b->pw_gecos) == 0
&& strcmp (a->pw_dir, b->pw_dir) == 0
&& strcmp (a->pw_shell, b->pw_shell) == 0;
}
enum { MAX_TEST_ITEMS = 10 };
static struct passwd test_items[MAX_TEST_ITEMS];
static int test_count;
/* Initialize test_items and test_count above, with data from the
passwd database. */
static bool
init_test_items (void)
{
setpwent ();
do
{
struct passwd *pwd = getpwent ();
if (pwd == NULL)
break;
struct passwd *target = test_items + test_count;
target->pw_name = strdup (pwd->pw_name);
target->pw_passwd = strdup (pwd->pw_passwd);
target->pw_uid = pwd->pw_uid;
target->pw_gid = pwd->pw_gid;
target->pw_gecos = strdup (pwd->pw_gecos);
target->pw_dir = strdup (pwd->pw_dir);
target->pw_shell = strdup (pwd->pw_shell);
}
while (++test_count < MAX_TEST_ITEMS);
endpwent ();
/* Filter out those test items which cannot be looked up by name or
UID. */
bool found = false;
for (int i = 0; i < test_count; ++i)
{
struct passwd *pwd1 = getpwnam (test_items[i].pw_name);
struct passwd *pwd2 = getpwuid (test_items[i].pw_uid);
if (pwd1 == NULL || !equal (pwd1, test_items + i)
|| pwd2 == NULL || !equal (pwd2, test_items + i))
test_items[i].pw_name = NULL;
else
found = true;
}
if (!found)
puts ("error: no accounts found which can be looked up by name and UID.");
return found;
}
/* Set to true if an error is encountered. */
static bool errors;
/* Return true if the padding has not been tampered with. */
static bool
check_padding (char *buffer, size_t size, char pad)
{
char *end = buffer + size;
while (buffer < end)
{
if (*buffer != pad)
return false;
++buffer;
}
return true;
}
/* Test one buffer size and padding combination. */
static void
test_one (const struct passwd *item, size_t buffer_size,
char pad, size_t padding_size)
{
char *buffer = malloc (buffer_size + padding_size);
if (buffer == NULL)
{
puts ("error: malloc failure");
errors = true;
return;
}
struct passwd pwd;
struct passwd *result;
int ret;
/* Test getpwname_r. */
memset (buffer, pad, buffer_size + padding_size);
pwd = (struct passwd) {};
ret = getpwnam_r (item->pw_name, &pwd, buffer, buffer_size, &result);
if (!check_padding (buffer + buffer_size, padding_size, pad))
{
printf ("error: padding change: "
"name \"%s\", buffer size %zu, padding size %zu, pad 0x%02x\n",
item->pw_name, buffer_size, padding_size, (unsigned char) pad);
errors = true;
}
if (ret == 0)
{
if (result == NULL)
{
printf ("error: no data: name \"%s\", buffer size %zu\n",
item->pw_name, buffer_size);
errors = true;
}
else if (!equal (item, result))
{
printf ("error: lookup mismatch: name \"%s\", buffer size %zu\n",
item->pw_name, buffer_size);
errors = true;
}
}
else if (ret != ERANGE)
{
errno = ret;
printf ("error: lookup failure for name \"%s\": %m (%d)\n",
item->pw_name, ret);
errors = true;
}
/* Test getpwuid_r. */
memset (buffer, pad, buffer_size + padding_size);
pwd = (struct passwd) {};
ret = getpwuid_r (item->pw_uid, &pwd, buffer, buffer_size, &result);
if (!check_padding (buffer + buffer_size, padding_size, pad))
{
printf ("error: padding change: "
"UID %ld, buffer size %zu, padding size %zu, pad 0x%02x\n",
(long) item->pw_uid, buffer_size, padding_size,
(unsigned char) pad);
errors = true;
}
if (ret == 0)
{
if (result == NULL)
{
printf ("error: no data: UID %ld, buffer size %zu\n",
(long) item->pw_uid, buffer_size);
errors = true;
}
else if (!equal (item, result))
{
printf ("error: lookup mismatch: UID %ld, buffer size %zu\n",
(long) item->pw_uid, buffer_size);
errors = true;
}
}
else if (ret != ERANGE)
{
errno = ret;
printf ("error: lookup failure for UID \"%ld\": %m (%d)\n",
(long) item->pw_uid, ret);
errors = true;
}
free (buffer);
}
/* Test one buffer size with different paddings. */
static void
test_buffer_size (size_t buffer_size)
{
for (int i = 0; i < test_count; ++i)
for (size_t padding_size = 0; padding_size < 3; ++padding_size)
{
test_one (test_items + i, buffer_size, '\0', padding_size);
if (padding_size > 0)
{
test_one (test_items + i, buffer_size, ':', padding_size);
test_one (test_items + i, buffer_size, '\n', padding_size);
test_one (test_items + i, buffer_size, '\xff', padding_size);
test_one (test_items + i, buffer_size, '@', padding_size);
}
}
}
int
do_test (void)
{
if (!init_test_items ())
return 1;
printf ("info: %d test items\n", test_count);
for (size_t buffer_size = 0; buffer_size <= 65; ++buffer_size)
test_buffer_size (buffer_size);
for (size_t buffer_size = 64 + 4; buffer_size < 256; buffer_size += 4)
test_buffer_size (buffer_size);
test_buffer_size (255);
test_buffer_size (257);
for (size_t buffer_size = 256; buffer_size < 512; buffer_size += 8)
test_buffer_size (buffer_size);
test_buffer_size (511);
test_buffer_size (513);
test_buffer_size (1024);
test_buffer_size (2048);
if (errors)
return 1;
else
return 0;
}
#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"
|