hurd: Avoid PLTs for longjmp & siglongjmp
[glibc.git] / hurd / lookup-retry.c
blobb5968486242b213b47760d8f21e5a926fd9fbdca
1 /* hairy bits of Hurd file name lookup
2 Copyright (C) 1992-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <hurd.h>
20 #include <hurd/lookup.h>
21 #include <hurd/term.h>
22 #include <hurd/paths.h>
23 #include <limits.h>
24 #include <fcntl.h>
25 #include <string.h>
26 #include <_itoa.h>
27 #include <eloop-threshold.h>
29 /* Translate the error from dir_lookup into the error the user sees. */
30 static inline error_t
31 lookup_error (error_t error)
33 switch (error)
35 case EOPNOTSUPP:
36 case MIG_BAD_ID:
37 /* These indicate that the server does not understand dir_lookup
38 at all. If it were a directory, it would, by definition. */
39 return ENOTDIR;
40 default:
41 return error;
45 error_t
46 __hurd_file_name_lookup_retry (error_t (*use_init_port)
47 (int which, error_t (*operate) (file_t)),
48 file_t (*get_dtable_port) (int fd),
49 error_t (*lookup)
50 (file_t dir, const char *name,
51 int flags, mode_t mode,
52 retry_type *do_retry, string_t retry_name,
53 mach_port_t *result),
54 enum retry_type doretry,
55 char retryname[1024],
56 int flags, mode_t mode,
57 file_t *result)
59 error_t err;
60 char *file_name;
61 int nloops;
63 error_t lookup_op (file_t startdir)
65 if (file_name[0] == '/' && file_name[1] != '\0')
67 while (file_name[1] == '/')
68 /* Remove double leading slash. */
69 file_name++;
70 if (file_name[1] != '\0')
71 /* Remove leading slash when we have more than the slash. */
72 file_name++;
75 return lookup_error ((*lookup) (startdir, file_name, flags, mode,
76 &doretry, retryname, result));
78 error_t reauthenticate (file_t unauth)
80 error_t err;
81 mach_port_t ref = __mach_reply_port ();
82 error_t reauth (auth_t auth)
84 return __auth_user_authenticate (auth, ref,
85 MACH_MSG_TYPE_MAKE_SEND,
86 result);
88 err = __io_reauthenticate (unauth, ref, MACH_MSG_TYPE_MAKE_SEND);
89 if (! err)
90 err = (*use_init_port) (INIT_PORT_AUTH, &reauth);
91 __mach_port_destroy (__mach_task_self (), ref);
92 __mach_port_deallocate (__mach_task_self (), unauth);
93 return err;
96 if (! lookup)
97 lookup = __dir_lookup;
99 nloops = 0;
100 err = 0;
103 file_t startdir = MACH_PORT_NULL;
104 int dirport = INIT_PORT_CWDIR;
106 switch (doretry)
108 case FS_RETRY_REAUTH:
109 if (err = reauthenticate (*result))
110 return err;
111 /* Fall through. */
113 case FS_RETRY_NORMAL:
114 if (nloops++ >= __eloop_threshold ())
116 __mach_port_deallocate (__mach_task_self (), *result);
117 return ELOOP;
120 /* An empty RETRYNAME indicates we have the final port. */
121 if (retryname[0] == '\0' &&
122 /* If reauth'd, we must do one more retry on "" to give the new
123 translator a chance to make a new port for us. */
124 doretry == FS_RETRY_NORMAL)
126 if (flags & O_NOFOLLOW)
128 /* In Linux, O_NOFOLLOW means to reject symlinks. If we
129 did an O_NOLINK lookup above and io_stat here to check
130 for S_IFLNK only, a translator like firmlink could easily
131 spoof this check by not showing S_IFLNK, but in fact
132 redirecting the lookup to some other name
133 (i.e. opening the very same holes a symlink would).
135 Instead we do an O_NOTRANS lookup above, and stat the
136 underlying node: if it has a translator set, and its
137 owner is not root (st_uid 0) then we reject it.
138 Since the motivation for this feature is security, and
139 that security presumes we trust the containing
140 directory, this check approximates the security of
141 refusing symlinks while accepting mount points.
142 Note that we actually permit something Linux doesn't:
143 we follow root-owned symlinks; if that is deemed
144 undesireable, we can add a final check for that
145 one exception to our general translator-based rule. */
146 struct stat64 st;
147 err = __io_stat (*result, &st);
148 if (!err)
150 if (flags & O_DIRECTORY && !S_ISDIR (st.st_mode))
151 err = ENOTDIR;
152 if (S_ISLNK (st.st_mode))
153 err = ELOOP;
154 else if (st.st_mode & (S_IPTRANS|S_IATRANS))
156 if (st.st_uid != 0)
157 err = ELOOP;
158 else if (st.st_mode & S_IPTRANS)
160 char buf[1024];
161 char *trans = buf;
162 size_t translen = sizeof buf;
163 err = __file_get_translator (*result,
164 &trans, &translen);
165 if (!err
166 && translen > sizeof _HURD_SYMLINK
167 && !memcmp (trans,
168 _HURD_SYMLINK, sizeof _HURD_SYMLINK))
169 err = ELOOP;
175 /* We got a successful translation. Now apply any open-time
176 action flags we were passed. */
178 if (!err && (flags & O_TRUNC)) /* Asked to truncate the file. */
179 err = __file_set_size (*result, 0);
181 if (err)
182 __mach_port_deallocate (__mach_task_self (), *result);
183 return err;
186 startdir = *result;
187 file_name = retryname;
188 break;
190 case FS_RETRY_MAGICAL:
191 switch (retryname[0])
193 case '/':
194 dirport = INIT_PORT_CRDIR;
195 if (*result != MACH_PORT_NULL)
196 __mach_port_deallocate (__mach_task_self (), *result);
197 if (nloops++ >= __eloop_threshold ())
198 return ELOOP;
199 file_name = &retryname[1];
200 break;
202 case 'f':
203 if (retryname[1] == 'd' && retryname[2] == '/')
205 int fd;
206 char *end;
207 int save = errno;
208 errno = 0;
209 fd = (int) __strtoul_internal (&retryname[3], &end, 10, 0);
210 if (end == NULL || errno || /* Malformed number. */
211 /* Check for excess text after the number. A slash
212 is valid; it ends the component. Anything else
213 does not name a numeric file descriptor. */
214 (*end != '/' && *end != '\0'))
216 errno = save;
217 return ENOENT;
219 if (! get_dtable_port)
220 err = EGRATUITOUS;
221 else
223 *result = (*get_dtable_port) (fd);
224 if (*result == MACH_PORT_NULL)
226 /* If the name was a proper number, but the file
227 descriptor does not exist, we return EBADF instead
228 of ENOENT. */
229 err = errno;
230 errno = save;
233 errno = save;
234 if (err)
235 return err;
236 if (*end == '\0')
237 return 0;
238 else
240 /* Do a normal retry on the remaining components. */
241 startdir = *result;
242 file_name = end + 1; /* Skip the slash. */
243 break;
246 else
247 goto bad_magic;
248 break;
250 case 'm':
251 if (retryname[1] == 'a' && retryname[2] == 'c' &&
252 retryname[3] == 'h' && retryname[4] == 't' &&
253 retryname[5] == 'y' && retryname[6] == 'p' &&
254 retryname[7] == 'e')
256 error_t err;
257 struct host_basic_info hostinfo;
258 mach_msg_type_number_t hostinfocnt = HOST_BASIC_INFO_COUNT;
259 char *p;
260 /* XXX want client's host */
261 if (err = __host_info (__mach_host_self (), HOST_BASIC_INFO,
262 (integer_t *) &hostinfo,
263 &hostinfocnt))
264 return err;
265 if (hostinfocnt != HOST_BASIC_INFO_COUNT)
266 return EGRATUITOUS;
267 p = _itoa (hostinfo.cpu_subtype, &retryname[8], 10, 0);
268 *--p = '/';
269 p = _itoa (hostinfo.cpu_type, &retryname[8], 10, 0);
270 if (p < retryname)
271 abort (); /* XXX write this right if this ever happens */
272 if (p > retryname)
273 strcpy (retryname, p);
274 startdir = *result;
276 else
277 goto bad_magic;
278 break;
280 case 't':
281 if (retryname[1] == 't' && retryname[2] == 'y')
282 switch (retryname[3])
284 error_t opentty (file_t *result)
286 error_t err;
287 error_t ctty_open (file_t port)
289 if (port == MACH_PORT_NULL)
290 return ENXIO; /* No controlling terminal. */
291 return __termctty_open_terminal (port,
292 flags,
293 result);
295 err = (*use_init_port) (INIT_PORT_CTTYID, &ctty_open);
296 if (! err)
297 err = reauthenticate (*result);
298 return err;
301 case '\0':
302 return opentty (result);
303 case '/':
304 if (err = opentty (&startdir))
305 return err;
306 strcpy (retryname, &retryname[4]);
307 break;
308 default:
309 goto bad_magic;
311 else
312 goto bad_magic;
313 break;
315 default:
316 bad_magic:
317 return EGRATUITOUS;
319 break;
321 default:
322 return EGRATUITOUS;
325 if (startdir != MACH_PORT_NULL)
327 err = lookup_op (startdir);
328 __mach_port_deallocate (__mach_task_self (), startdir);
329 startdir = MACH_PORT_NULL;
331 else
332 err = (*use_init_port) (dirport, &lookup_op);
333 } while (! err);
335 return err;
337 weak_alias (__hurd_file_name_lookup_retry, hurd_file_name_lookup_retry)