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/>. */
20 #include <hurd/lookup.h>
21 #include <hurd/term.h>
22 #include <hurd/paths.h>
27 #include <eloop-threshold.h>
29 /* Translate the error from dir_lookup into the error the user sees. */
31 lookup_error (error_t error
)
37 /* These indicate that the server does not understand dir_lookup
38 at all. If it were a directory, it would, by definition. */
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
),
50 (file_t dir
, const char *name
,
51 int flags
, mode_t mode
,
52 retry_type
*do_retry
, string_t retry_name
,
54 enum retry_type doretry
,
56 int flags
, mode_t mode
,
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. */
70 if (file_name
[1] != '\0')
71 /* Remove leading slash when we have more than the slash. */
75 return lookup_error ((*lookup
) (startdir
, file_name
, flags
, mode
,
76 &doretry
, retryname
, result
));
78 error_t
reauthenticate (file_t unauth
)
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
,
88 err
= __io_reauthenticate (unauth
, ref
, MACH_MSG_TYPE_MAKE_SEND
);
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
);
97 lookup
= __dir_lookup
;
103 file_t startdir
= MACH_PORT_NULL
;
104 int dirport
= INIT_PORT_CWDIR
;
108 case FS_RETRY_REAUTH
:
109 if (err
= reauthenticate (*result
))
113 case FS_RETRY_NORMAL
:
114 if (nloops
++ >= __eloop_threshold ())
116 __mach_port_deallocate (__mach_task_self (), *result
);
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. */
147 err
= __io_stat (*result
, &st
);
150 if (flags
& O_DIRECTORY
&& !S_ISDIR (st
.st_mode
))
152 if (S_ISLNK (st
.st_mode
))
154 else if (st
.st_mode
& (S_IPTRANS
|S_IATRANS
))
158 else if (st
.st_mode
& S_IPTRANS
)
162 size_t translen
= sizeof buf
;
163 err
= __file_get_translator (*result
,
166 && translen
> sizeof _HURD_SYMLINK
168 _HURD_SYMLINK
, sizeof _HURD_SYMLINK
))
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);
182 __mach_port_deallocate (__mach_task_self (), *result
);
187 file_name
= retryname
;
190 case FS_RETRY_MAGICAL
:
191 switch (retryname
[0])
194 dirport
= INIT_PORT_CRDIR
;
195 if (*result
!= MACH_PORT_NULL
)
196 __mach_port_deallocate (__mach_task_self (), *result
);
197 if (nloops
++ >= __eloop_threshold ())
199 file_name
= &retryname
[1];
203 if (retryname
[1] == 'd' && retryname
[2] == '/')
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'))
219 if (! get_dtable_port
)
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
240 /* Do a normal retry on the remaining components. */
242 file_name
= end
+ 1; /* Skip the slash. */
251 if (retryname
[1] == 'a' && retryname
[2] == 'c' &&
252 retryname
[3] == 'h' && retryname
[4] == 't' &&
253 retryname
[5] == 'y' && retryname
[6] == 'p' &&
257 struct host_basic_info hostinfo
;
258 mach_msg_type_number_t hostinfocnt
= HOST_BASIC_INFO_COUNT
;
260 /* XXX want client's host */
261 if (err
= __host_info (__mach_host_self (), HOST_BASIC_INFO
,
262 (integer_t
*) &hostinfo
,
265 if (hostinfocnt
!= HOST_BASIC_INFO_COUNT
)
267 p
= _itoa (hostinfo
.cpu_subtype
, &retryname
[8], 10, 0);
269 p
= _itoa (hostinfo
.cpu_type
, &retryname
[8], 10, 0);
271 abort (); /* XXX write this right if this ever happens */
273 strcpy (retryname
, p
);
281 if (retryname
[1] == 't' && retryname
[2] == 'y')
282 switch (retryname
[3])
284 error_t
opentty (file_t
*result
)
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
,
295 err
= (*use_init_port
) (INIT_PORT_CTTYID
, &ctty_open
);
297 err
= reauthenticate (*result
);
302 return opentty (result
);
304 if (err
= opentty (&startdir
))
306 strcpy (retryname
, &retryname
[4]);
325 if (startdir
!= MACH_PORT_NULL
)
327 err
= lookup_op (startdir
);
328 __mach_port_deallocate (__mach_task_self (), startdir
);
329 startdir
= MACH_PORT_NULL
;
332 err
= (*use_init_port
) (dirport
, &lookup_op
);
337 weak_alias (__hurd_file_name_lookup_retry
, hurd_file_name_lookup_retry
)