This is the mail archive of the
cygwin-patches
mailing list for the Cygwin project.
[PATCH 3/3] Cygwin: fchownat and fstatat: support the AT_EMPTY_PATH flag
- From: Ken Brown <kbrown at cornell dot edu>
- To: "cygwin-patches at cygwin dot com" <cygwin-patches at cygwin dot com>
- Date: Sat, 28 Dec 2019 19:52:31 +0000
- Subject: [PATCH 3/3] Cygwin: fchownat and fstatat: support the AT_EMPTY_PATH flag
- Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=cornell.edu; dmarc=pass action=none header.from=cornell.edu; dkim=pass header.d=cornell.edu; arc=none
- Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=f1di2E5274nSlcx2/gRt2QK8fiZR0kIcPowCmjHlPiI=; b=nuWIrcZAhlM7ZXYwhhmRFOrDA27jciT8CeUQoXiFNA2Smc7D9Xa7iVH6G27BQhZWoz+zLKBn7+UQwbsZIXvGmgs+mvH1Luk8Uv7CB0xN7jQf5aJ9qFBvYJRkNi2Vrs0RB/6RDT32LXLfadS8zwGa5jf9m0J0LRaiBY4CQNmlGRn1XiNQdtXXUIKZy4Ch4uuzk10IvIe6HpJ9Y8nY9iWqT1dOS7iAOi5N68I2OxqIMigpEzMnkDwR7drdboS22hGAswKoUln+e2KoFI6Im97PDRJjpCC438GzWurRjKHP8JqhrwWPJTdTGDC3+T45a1v3znC5oa6fJ4JewqCR3Y/MXQ==
- Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XiR+fqcZ88VD+Mbc/KCqfD9JyXz1dCn+JTH28TuAvTLokwRJjdjTddmZy7oVDQJsEBhMBmwHZoZb/2EsV2jNNev6F3AkQIVnUMUsYtol4kt+a8GuhrQCn5rwgGSjqPsd9SxVl/CCb6HHd99S1JBXUyNXDPu9djZaYmKEkCZHLrmuOXMeYJnlIKB/vCsCjSP2XfpXXCGbmXSZG3TY50bBl00YmCKAlTdobA0u1amm1wo8q5di7NufaIhSRdt0u1YCTM9CHAfs8sfeFF6pZw0R7sljCF5Rv2FJXgFjkCWj3E8H6GJWcHVboH5kbRRLvEHdJbnYG6d8FZInsFHQFFCcuQ==
- References: <20191228195213.1570-1-kbrown@cornell.edu>
Following Linux, allow the pathname argument to be empty if the
AT_EMPTY_PATH is specified. In this case the dirfd argument can refer
to any type of file, not just a directory, and the call operates on
that file. In particular, dirfd can refer to a symlink that was
opened with O_PATH and O_NOFOLLOW.
Add a new optional argument to gen_full_path_at to help implement
this.
---
winsup/cygwin/syscalls.cc | 30 ++++++++++++++++++++----------
1 file changed, 20 insertions(+), 10 deletions(-)
diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc
index 2be8693c9..1bc856268 100644
--- a/winsup/cygwin/syscalls.cc
+++ b/winsup/cygwin/syscalls.cc
@@ -4635,7 +4635,8 @@ pclose (FILE *fp)
static int
gen_full_path_at (char *path_ret, int dirfd, const char *pathname,
- bool null_pathname_allowed = false)
+ bool null_pathname_allowed = false,
+ bool at_empty_path_flag = false)
{
/* Set null_pathname_allowed to true to allow GLIBC compatible behaviour
for NULL pathname. Only used by futimesat. */
@@ -4644,20 +4645,25 @@ gen_full_path_at (char *path_ret, int dirfd, const char *pathname,
set_errno (EFAULT);
return -1;
}
+ bool empty_path = false;
if (pathname)
{
if (!*pathname)
{
- set_errno (ENOENT);
- return -1;
+ empty_path = true;
+ if (!at_empty_path_flag)
+ {
+ set_errno (ENOENT);
+ return -1;
+ }
}
- if (strlen (pathname) >= PATH_MAX)
+ else if (strlen (pathname) >= PATH_MAX)
{
set_errno (ENAMETOOLONG);
return -1;
}
}
- if (pathname && isabspath (pathname))
+ if (pathname && !empty_path && isabspath (pathname))
stpcpy (path_ret, pathname);
else
{
@@ -4674,12 +4680,14 @@ gen_full_path_at (char *path_ret, int dirfd, const char *pathname,
cygheap_fdget cfd (dirfd);
if (cfd < 0)
return -1;
- if (!cfd->pc.isdir ())
+ if (!empty_path && !cfd->pc.isdir ())
{
set_errno (ENOTDIR);
return -1;
}
p = stpcpy (path_ret, cfd->get_name ());
+ if (empty_path)
+ return 0;
}
if (!p)
{
@@ -4785,13 +4793,14 @@ fchownat (int dirfd, const char *pathname, uid_t uid, gid_t gid, int flags)
tmp_pathbuf tp;
__try
{
- if (flags & ~AT_SYMLINK_NOFOLLOW)
+ if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH))
{
set_errno (EINVAL);
__leave;
}
char *path = tp.c_get ();
- if (gen_full_path_at (path, dirfd, pathname))
+ if (gen_full_path_at (path, dirfd, pathname, false,
+ flags & AT_EMPTY_PATH))
__leave;
return chown_worker (path, (flags & AT_SYMLINK_NOFOLLOW)
? PC_SYM_NOFOLLOW : PC_SYM_FOLLOW, uid, gid);
@@ -4808,13 +4817,14 @@ fstatat (int dirfd, const char *__restrict pathname, struct stat *__restrict st,
tmp_pathbuf tp;
__try
{
- if (flags & ~AT_SYMLINK_NOFOLLOW)
+ if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH))
{
set_errno (EINVAL);
__leave;
}
char *path = tp.c_get ();
- if (gen_full_path_at (path, dirfd, pathname))
+ if (gen_full_path_at (path, dirfd, pathname, false,
+ flags & AT_EMPTY_PATH))
__leave;
path_conv pc (path, ((flags & AT_SYMLINK_NOFOLLOW)
? PC_SYM_NOFOLLOW : PC_SYM_FOLLOW)
--
2.21.0