about summary refs log tree commit diff
path: root/nss/tst-nss-files-alias-leak.c
blob: 946739bd725332641ec403f749ade03fe40b0548 (plain) (blame)
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
236
237
/* Check for file descriptor leak in alias :include: processing (bug 23521).
   Copyright (C) 2018-2019 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 <aliases.h>
#include <array_length.h>
#include <dlfcn.h>
#include <errno.h>
#include <gnu/lib-names.h>
#include <nss.h>
#include <stdlib.h>
#include <string.h>
#include <support/check.h>
#include <support/namespace.h>
#include <support/support.h>
#include <support/temp_file.h>
#include <support/test-driver.h>
#include <support/xstdio.h>
#include <support/xunistd.h>

static struct support_chroot *chroot_env;

/* Number of the aliases for the "many" user.  This must be large
   enough to trigger reallocation for the pointer array, but result in
   answers below the maximum size tried in do_test.  */
enum { many_aliases = 30 };

static void
prepare (int argc, char **argv)
{
  chroot_env = support_chroot_create
    ((struct support_chroot_configuration) { } );

  char *path = xasprintf ("%s/etc/aliases", chroot_env->path_chroot);
  add_temp_file (path);
  support_write_file_string
    (path,
     "user1: :include:/etc/aliases.user1\n"
     "user2: :include:/etc/aliases.user2\n"
     "comment: comment1, :include:/etc/aliases.comment\n"
     "many: :include:/etc/aliases.many\n");
  free (path);

  path = xasprintf ("%s/etc/aliases.user1", chroot_env->path_chroot);
  add_temp_file (path);
  support_write_file_string (path, "alias1\n");
  free (path);

  path = xasprintf ("%s/etc/aliases.user2", chroot_env->path_chroot);
  add_temp_file (path);
  support_write_file_string (path, "alias1a, alias2\n");
  free (path);

  path = xasprintf ("%s/etc/aliases.comment", chroot_env->path_chroot);
  add_temp_file (path);
  support_write_file_string
    (path,
     /* The line must be longer than the line with the :include:
        directive in /etc/aliases.  */
     "# Long line.  ##############################################\n"
     "comment2\n");
  free (path);

  path = xasprintf ("%s/etc/aliases.many", chroot_env->path_chroot);
  add_temp_file (path);
  FILE *fp = xfopen (path, "w");
  for (int i = 0; i < many_aliases; ++i)
    fprintf (fp, "a%d\n", i);
  TEST_VERIFY_EXIT (! ferror (fp));
  xfclose (fp);
  free (path);
}

/* The names of the users to test.  */
static const char *users[] = { "user1", "user2", "comment", "many" };

static void
check_aliases (int id, const struct aliasent *e)
{
  TEST_VERIFY_EXIT (id >= 0 || id < array_length (users));
  const char *name = users[id];
  TEST_COMPARE_BLOB (e->alias_name, strlen (e->alias_name),
                     name, strlen (name));

  switch (id)
    {
    case 0:
      TEST_COMPARE (e->alias_members_len, 1);
      TEST_COMPARE_BLOB (e->alias_members[0], strlen (e->alias_members[0]),
                         "alias1", strlen ("alias1"));
      break;

    case 1:
      TEST_COMPARE (e->alias_members_len, 2);
      TEST_COMPARE_BLOB (e->alias_members[0], strlen (e->alias_members[0]),
                         "alias1a", strlen ("alias1a"));
      TEST_COMPARE_BLOB (e->alias_members[1], strlen (e->alias_members[1]),
                         "alias2", strlen ("alias2"));
      break;

    case 2:
      TEST_COMPARE (e->alias_members_len, 2);
      TEST_COMPARE_BLOB (e->alias_members[0], strlen (e->alias_members[0]),
                         "comment1", strlen ("comment1"));
      TEST_COMPARE_BLOB (e->alias_members[1], strlen (e->alias_members[1]),
                         "comment2", strlen ("comment2"));
      break;

    case 3:
      TEST_COMPARE (e->alias_members_len, many_aliases);
      for (int i = 0; i < e->alias_members_len; ++i)
        {
          char alias[30];
          int len = snprintf (alias, sizeof (alias), "a%d", i);
          TEST_VERIFY_EXIT (len > 0);
          TEST_COMPARE_BLOB (e->alias_members[i], strlen (e->alias_members[i]),
                             alias, len);
        }
      break;
    }
}

static int
do_test (void)
{
  /* Make sure we don't try to load the module in the chroot.  */
  if (dlopen (LIBNSS_FILES_SO, RTLD_NOW) == NULL)
    FAIL_EXIT1 ("could not load " LIBNSS_FILES_SO ": %s", dlerror ());

  /* Some of these descriptors will become unavailable if there is a
     file descriptor leak.  10 is chosen somewhat arbitrarily.  The
     array must be longer than the number of files opened by nss_files
     at the same time (currently that number is 2).  */
  int next_descriptors[10];
  for (size_t i = 0; i < array_length (next_descriptors); ++i)
    {
      next_descriptors[i] = dup (0);
      TEST_VERIFY_EXIT (next_descriptors[i] > 0);
    }
  for (size_t i = 0; i < array_length (next_descriptors); ++i)
    xclose (next_descriptors[i]);

  support_become_root ();
  if (!support_can_chroot ())
    return EXIT_UNSUPPORTED;

  __nss_configure_lookup ("aliases", "files");

  xchroot (chroot_env->path_chroot);

  /* Attempt various buffer sizes.  If the operation succeeds, we
     expect correct data.  */
  for (int id = 0; id < array_length (users); ++id)
    {
      bool found = false;
      for (size_t size = 1; size <= 1000; ++size)
        {
          void *buffer = malloc (size);
          struct aliasent result;
          struct aliasent *res;
          errno = EINVAL;
          int ret = getaliasbyname_r (users[id], &result, buffer, size, &res);
          if (ret == 0)
            {
              if (res != NULL)
                {
                  found = true;
                  check_aliases (id, res);
                }
              else
                {
                  support_record_failure ();
                  printf ("error: failed lookup for user \"%s\", size %zu\n",
                          users[id], size);
                }
            }
          else if (ret != ERANGE)
            {
              support_record_failure ();
              printf ("error: invalid return code %d (user \"%s\", size %zu)\n",
                      ret, users[id], size);
            }
          free (buffer);

          /* Make sure that we did not have a file descriptor leak.  */
          for (size_t i = 0; i < array_length (next_descriptors); ++i)
            {
              int new_fd = dup (0);
              if (new_fd != next_descriptors[i])
                {
                  support_record_failure ();
                  printf ("error: descriptor %d at index %zu leaked"
                          " (user \"%s\", size %zu)\n",
                          next_descriptors[i], i, users[id], size);

                  /* Close unexpected descriptor, the leak probing
                     descriptors, and the leaked descriptor
                     next_descriptors[i].  */
                  xclose (new_fd);
                  for (size_t j = 0; j <= i; ++j)
                    xclose (next_descriptors[j]);
                  goto next_size;
                }
            }
          for (size_t i = 0; i < array_length (next_descriptors); ++i)
            xclose (next_descriptors[i]);

        next_size:
          ;
        }
      if (!found)
        {
          support_record_failure ();
          printf ("error: user %s not found\n", users[id]);
        }
    }

  support_chroot_free (chroot_env);
  return 0;
}

#define PREPARE prepare
#include <support/test-driver.c>