tst-nss-files-alias-leak.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* Check for file descriptor leak in alias :include: processing (bug 23521).
  2. Copyright (C) 2018-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <aliases.h>
  16. #include <array_length.h>
  17. #include <dlfcn.h>
  18. #include <errno.h>
  19. #include <gnu/lib-names.h>
  20. #include <nss.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <support/check.h>
  24. #include <support/namespace.h>
  25. #include <support/support.h>
  26. #include <support/temp_file.h>
  27. #include <support/test-driver.h>
  28. #include <support/xstdio.h>
  29. #include <support/xunistd.h>
  30. static struct support_chroot *chroot_env;
  31. /* Number of the aliases for the "many" user. This must be large
  32. enough to trigger reallocation for the pointer array, but result in
  33. answers below the maximum size tried in do_test. */
  34. enum { many_aliases = 30 };
  35. static void
  36. prepare (int argc, char **argv)
  37. {
  38. chroot_env = support_chroot_create
  39. ((struct support_chroot_configuration) { } );
  40. char *path = xasprintf ("%s/etc/aliases", chroot_env->path_chroot);
  41. add_temp_file (path);
  42. support_write_file_string
  43. (path,
  44. "user1: :include:/etc/aliases.user1\n"
  45. "user2: :include:/etc/aliases.user2\n"
  46. "comment: comment1, :include:/etc/aliases.comment\n"
  47. "many: :include:/etc/aliases.many\n");
  48. free (path);
  49. path = xasprintf ("%s/etc/aliases.user1", chroot_env->path_chroot);
  50. add_temp_file (path);
  51. support_write_file_string (path, "alias1\n");
  52. free (path);
  53. path = xasprintf ("%s/etc/aliases.user2", chroot_env->path_chroot);
  54. add_temp_file (path);
  55. support_write_file_string (path, "alias1a, alias2\n");
  56. free (path);
  57. path = xasprintf ("%s/etc/aliases.comment", chroot_env->path_chroot);
  58. add_temp_file (path);
  59. support_write_file_string
  60. (path,
  61. /* The line must be longer than the line with the :include:
  62. directive in /etc/aliases. */
  63. "# Long line. ##############################################\n"
  64. "comment2\n");
  65. free (path);
  66. path = xasprintf ("%s/etc/aliases.many", chroot_env->path_chroot);
  67. add_temp_file (path);
  68. FILE *fp = xfopen (path, "w");
  69. for (int i = 0; i < many_aliases; ++i)
  70. fprintf (fp, "a%d\n", i);
  71. TEST_VERIFY_EXIT (! ferror (fp));
  72. xfclose (fp);
  73. free (path);
  74. }
  75. /* The names of the users to test. */
  76. static const char *users[] = { "user1", "user2", "comment", "many" };
  77. static void
  78. check_aliases (int id, const struct aliasent *e)
  79. {
  80. TEST_VERIFY_EXIT (id >= 0 || id < array_length (users));
  81. const char *name = users[id];
  82. TEST_COMPARE_BLOB (e->alias_name, strlen (e->alias_name),
  83. name, strlen (name));
  84. switch (id)
  85. {
  86. case 0:
  87. TEST_COMPARE (e->alias_members_len, 1);
  88. TEST_COMPARE_BLOB (e->alias_members[0], strlen (e->alias_members[0]),
  89. "alias1", strlen ("alias1"));
  90. break;
  91. case 1:
  92. TEST_COMPARE (e->alias_members_len, 2);
  93. TEST_COMPARE_BLOB (e->alias_members[0], strlen (e->alias_members[0]),
  94. "alias1a", strlen ("alias1a"));
  95. TEST_COMPARE_BLOB (e->alias_members[1], strlen (e->alias_members[1]),
  96. "alias2", strlen ("alias2"));
  97. break;
  98. case 2:
  99. TEST_COMPARE (e->alias_members_len, 2);
  100. TEST_COMPARE_BLOB (e->alias_members[0], strlen (e->alias_members[0]),
  101. "comment1", strlen ("comment1"));
  102. TEST_COMPARE_BLOB (e->alias_members[1], strlen (e->alias_members[1]),
  103. "comment2", strlen ("comment2"));
  104. break;
  105. case 3:
  106. TEST_COMPARE (e->alias_members_len, many_aliases);
  107. for (int i = 0; i < e->alias_members_len; ++i)
  108. {
  109. char alias[30];
  110. int len = snprintf (alias, sizeof (alias), "a%d", i);
  111. TEST_VERIFY_EXIT (len > 0);
  112. TEST_COMPARE_BLOB (e->alias_members[i], strlen (e->alias_members[i]),
  113. alias, len);
  114. }
  115. break;
  116. }
  117. }
  118. static int
  119. do_test (void)
  120. {
  121. /* Make sure we don't try to load the module in the chroot. */
  122. if (dlopen (LIBNSS_FILES_SO, RTLD_NOW) == NULL)
  123. FAIL_EXIT1 ("could not load " LIBNSS_FILES_SO ": %s", dlerror ());
  124. /* Some of these descriptors will become unavailable if there is a
  125. file descriptor leak. 10 is chosen somewhat arbitrarily. The
  126. array must be longer than the number of files opened by nss_files
  127. at the same time (currently that number is 2). */
  128. int next_descriptors[10];
  129. for (size_t i = 0; i < array_length (next_descriptors); ++i)
  130. {
  131. next_descriptors[i] = dup (0);
  132. TEST_VERIFY_EXIT (next_descriptors[i] > 0);
  133. }
  134. for (size_t i = 0; i < array_length (next_descriptors); ++i)
  135. xclose (next_descriptors[i]);
  136. support_become_root ();
  137. if (!support_can_chroot ())
  138. return EXIT_UNSUPPORTED;
  139. __nss_configure_lookup ("aliases", "files");
  140. xchroot (chroot_env->path_chroot);
  141. /* Attempt various buffer sizes. If the operation succeeds, we
  142. expect correct data. */
  143. for (int id = 0; id < array_length (users); ++id)
  144. {
  145. bool found = false;
  146. for (size_t size = 1; size <= 1000; ++size)
  147. {
  148. void *buffer = malloc (size);
  149. struct aliasent result;
  150. struct aliasent *res;
  151. errno = EINVAL;
  152. int ret = getaliasbyname_r (users[id], &result, buffer, size, &res);
  153. if (ret == 0)
  154. {
  155. if (res != NULL)
  156. {
  157. found = true;
  158. check_aliases (id, res);
  159. }
  160. else
  161. {
  162. support_record_failure ();
  163. printf ("error: failed lookup for user \"%s\", size %zu\n",
  164. users[id], size);
  165. }
  166. }
  167. else if (ret != ERANGE)
  168. {
  169. support_record_failure ();
  170. printf ("error: invalid return code %d (user \"%s\", size %zu)\n",
  171. ret, users[id], size);
  172. }
  173. free (buffer);
  174. /* Make sure that we did not have a file descriptor leak. */
  175. for (size_t i = 0; i < array_length (next_descriptors); ++i)
  176. {
  177. int new_fd = dup (0);
  178. if (new_fd != next_descriptors[i])
  179. {
  180. support_record_failure ();
  181. printf ("error: descriptor %d at index %zu leaked"
  182. " (user \"%s\", size %zu)\n",
  183. next_descriptors[i], i, users[id], size);
  184. /* Close unexpected descriptor, the leak probing
  185. descriptors, and the leaked descriptor
  186. next_descriptors[i]. */
  187. xclose (new_fd);
  188. for (size_t j = 0; j <= i; ++j)
  189. xclose (next_descriptors[j]);
  190. goto next_size;
  191. }
  192. }
  193. for (size_t i = 0; i < array_length (next_descriptors); ++i)
  194. xclose (next_descriptors[i]);
  195. next_size:
  196. ;
  197. }
  198. if (!found)
  199. {
  200. support_record_failure ();
  201. printf ("error: user %s not found\n", users[id]);
  202. }
  203. }
  204. support_chroot_free (chroot_env);
  205. return 0;
  206. }
  207. #define PREPARE prepare
  208. #include <support/test-driver.c>