1 /* Check if realpath does not consume extra stack space based on symlink
2 existance in the path (BZ #26341)
3 Copyright (C) 2021-2022 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/param.h>
23 #include <unistd.h>
24
25 #define __sysconf sysconf
26 #include <eloop-threshold.h>
27 #include <support/check.h>
28 #include <support/support.h>
29 #include <support/temp_file.h>
30 #include <support/xunistd.h>
31 #include <support/xthread.h>
32
33 static char *filename;
34 static size_t filenamelen;
35 static char *linkname;
36
37 #ifndef PATH_MAX
38 # define PATH_MAX 1024
39 #endif
40
41 static void
create_link(void)42 create_link (void)
43 {
44 int fd = create_temp_file ("tst-canon-bz26341", &filename);
45 TEST_VERIFY_EXIT (fd != -1);
46 xclose (fd);
47
48 /* Make filename a canonical path. */
49 char *saved_filename = filename;
50 filename = realpath (filename, NULL);
51 free (saved_filename);
52 TEST_VERIFY (filename != NULL);
53
54 /* Create MAXLINKS symbolic links to the temporary filename.
55 On exit, linkname has the last link created. */
56 char *prevlink = filename;
57 int maxlinks = __eloop_threshold ();
58 for (int i = 0; i < maxlinks; i++)
59 {
60 linkname = xasprintf ("%s%d", filename, i);
61 xsymlink (prevlink, linkname);
62 add_temp_file (linkname);
63 prevlink = linkname;
64 }
65
66 filenamelen = strlen (filename);
67 }
68
69 static void *
do_realpath(void * arg)70 do_realpath (void *arg)
71 {
72 /* Old implementation of realpath allocates a PATH_MAX using alloca
73 for each symlink in the path, leading to MAXSYMLINKS times PATH_MAX
74 maximum stack usage.
75 This stack allocations tries fill the thread allocated stack minus
76 the resolved path (plus some slack), the realpath (plus some
77 slack), and the system call usage (plus some slack).
78 If realpath uses more than 2 * PATH_MAX plus some slack it will trigger
79 a stackoverflow. */
80
81 const size_t syscall_usage = 1 * PATH_MAX + 1024;
82 const size_t realpath_usage = 2 * PATH_MAX + 1024;
83 const size_t thread_usage = 1 * PATH_MAX + 1024;
84 size_t stack_size = support_small_thread_stack_size ()
85 - syscall_usage - realpath_usage - thread_usage;
86 char stack[stack_size];
87 char *resolved = stack + stack_size - thread_usage + 1024;
88
89 char *p = realpath (linkname, resolved);
90 TEST_VERIFY (p != NULL);
91 TEST_COMPARE_BLOB (resolved, filenamelen, filename, filenamelen);
92
93 return NULL;
94 }
95
96 static int
do_test(void)97 do_test (void)
98 {
99 create_link ();
100
101 pthread_t th = xpthread_create (support_small_stack_thread_attribute (),
102 do_realpath, NULL);
103 xpthread_join (th);
104
105 return 0;
106 }
107
108 #include <support/test-driver.c>
109