hurd: Make readlink* just reopen the file used for stat

9e5c991106 ("hurd: Fix readlink() hanging on fifo") separated opening
the file for the stat call from opening the file for the read call. That
however opened a small window for the file to change. Better make this
atomic by reopening the file with O_READ.
This commit is contained in:
Samuel Thibault 2022-09-15 21:53:57 +02:00
parent 9e5c991106
commit 5652e12cce
2 changed files with 40 additions and 24 deletions

View File

@ -28,33 +28,41 @@ ssize_t
__readlink (const char *file_name, char *buf, size_t len)
{
error_t err;
file_t file;
file_t file_stat;
struct stat64 st;
file = __file_name_lookup (file_name, O_NOLINK, 0);
if (file == MACH_PORT_NULL)
file_stat = __file_name_lookup (file_name, O_NOLINK, 0);
if (file_stat == MACH_PORT_NULL)
return -1;
err = __io_stat (file, &st);
err = __io_stat (file_stat, &st);
if (! err)
if (S_ISLNK (st.st_mode))
{
enum retry_type doretry;
char retryname[1024];
file_t file;
char *rbuf = buf;
__mach_port_deallocate (__mach_task_self (), file);
file = __file_name_lookup (file_name, O_READ | O_NOLINK, 0);
err = __io_read (file, &rbuf, &len, 0, len);
if (!err && rbuf != buf)
err = __dir_lookup (file_stat, "", O_READ | O_NOLINK, 0, &doretry, retryname, &file);
if (! err && (doretry != FS_RETRY_NORMAL || retryname[0] != '\0'))
err = EGRATUITOUS;
if (! err)
{
memcpy (buf, rbuf, len);
__vm_deallocate (__mach_task_self (), (vm_address_t)rbuf, len);
err = __io_read (file, &rbuf, &len, 0, len);
if (!err && rbuf != buf)
{
memcpy (buf, rbuf, len);
__vm_deallocate (__mach_task_self (), (vm_address_t)rbuf, len);
}
__mach_port_deallocate (__mach_task_self (), file);
}
}
else
err = EINVAL;
__mach_port_deallocate (__mach_task_self (), file);
__mach_port_deallocate (__mach_task_self (), file_stat);
if (err)
return __hurd_fail (err);

View File

@ -29,33 +29,41 @@ ssize_t
readlinkat (int fd, const char *file_name, char *buf, size_t len)
{
error_t err;
file_t file;
file_t file_stat;
struct stat64 st;
file = __file_name_lookup_at (fd, 0, file_name, O_NOLINK, 0);
if (file == MACH_PORT_NULL)
file_stat = __file_name_lookup_at (fd, 0, file_name, O_NOLINK, 0);
if (file_stat == MACH_PORT_NULL)
return -1;
err = __io_stat (file, &st);
err = __io_stat (file_stat, &st);
if (! err)
if (S_ISLNK (st.st_mode))
{
enum retry_type doretry;
char retryname[1024];
file_t file;
char *rbuf = buf;
__mach_port_deallocate (__mach_task_self (), file);
file = __file_name_lookup_at (fd, 0, file_name, O_READ | O_NOLINK, 0);
err = __io_read (file, &rbuf, &len, 0, len);
if (!err && rbuf != buf)
err = __dir_lookup (file_stat, "", O_READ | O_NOLINK, 0, &doretry, retryname, &file);
if (! err && (doretry != FS_RETRY_NORMAL || retryname[0] != '\0'))
err = EGRATUITOUS;
if (! err)
{
memcpy (buf, rbuf, len);
__vm_deallocate (__mach_task_self (), (vm_address_t)rbuf, len);
err = __io_read (file, &rbuf, &len, 0, len);
if (!err && rbuf != buf)
{
memcpy (buf, rbuf, len);
__vm_deallocate (__mach_task_self (), (vm_address_t)rbuf, len);
}
__mach_port_deallocate (__mach_task_self (), file);
}
}
else
err = EINVAL;
__mach_port_deallocate (__mach_task_self (), file);
__mach_port_deallocate (__mach_task_self (), file_stat);
return err ? __hurd_fail (err) : len;
}