This is the mail archive of the
cygwin-patches
mailing list for the Cygwin project.
[PATCH] Cygwin: rmdir: fail if last component is a symlink, as on Linux
- From: Ken Brown <kbrown at cornell dot edu>
- To: "cygwin-patches at cygwin dot com" <cygwin-patches at cygwin dot com>
- Cc: "eblake at redhat dot com" <eblake at redhat dot com>
- Date: Sun, 22 Sep 2019 17:18:46 +0000
- Subject: [PATCH] Cygwin: rmdir: fail if last component is a symlink, as on Linux
- 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=vfTxqOLuiez3Xaalw/NWZXg98H5p11b+n5cTtCgbH+M=; b=lM8XWVXeqxwqCKvHiqqJYqSSBWX20ZLaACSc3TQolXyiQRVX+zHFSFf03W9m4KL8+1NCty75Nb3S6CukGQy2/fnNDn68pu4XiY1dZ2XHh8L2P3VHxdDDCd6T1f6TQIgXm4Y1nys3fybz1FWLPDttOxMiHiQDtYz/lrEV0GCqF/TwlZrf2tdec4tnUAiZEi/GmpDOQpAkA2GBJhY5RHojWwdg9QU+SFJSbjwTuFWgphb+65UKRW1P9fhdNinYGW1Eew2PP+Qdy6+Pc3lvXIH2lKjVhhid7z+phw6lvNM5y5AFPaBaiYjxSZE5XNjoGhRRJS0p2QqvKE2Zo559LbYQNw==
- Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ctuh6Jy6ZEckUsHSY5LVYwnTooDJeiUg6C/SSBz11Ro8sIGJavHkKfaOOFNowGaMOM1KhXmCzpECItDMrPL7QpBnoBDSYHb8Fsn9ckr0GCFCg9JgkwJ4gUGFEnj90QJWkYQCDv5fvc9yhlkNSuuqE1wjtREZXWDVfwluu+8GHq+FVIr86P1bd7W9hEoK2vAr41SJ/55ia+foJP440VGhqzicySW+TZ9G2trRJeaVS2z0T8b1vjItxeC5MH4VWJHlUSIsJu/VKKTvmqs/H54QyorGZ2FJXNBLbU4NzCuSkeBFJyrz9jgkFfU1Vdykss9jl6TGAWlu7gwTyW/+gTuDiw==
If the last component of the directory name is a symlink followed by a
slash, rmdir should fail, even if the symlink resolves to an existing
empty directory.
mkdir was similarly fixed in 2009 in commit
52dba6a5c45e8d8ba1e237a15213311dc11d91fb. Modify a comment to clarify
the purpose of that commit.
Addresses https://cygwin.com/ml/cygwin/2019-09/msg00221.html.
---
winsup/cygwin/dir.cc | 26 ++++++++++++++++++++++----
1 file changed, 22 insertions(+), 4 deletions(-)
diff --git a/winsup/cygwin/dir.cc b/winsup/cygwin/dir.cc
index b757851d5..f65c9bdad 100644
--- a/winsup/cygwin/dir.cc
+++ b/winsup/cygwin/dir.cc
@@ -305,15 +305,14 @@ mkdir (const char *dir, mode_t mode)
__try
{
- /* POSIX says mkdir("symlink-to-missing/") should create the
- directory "missing", but Linux rejects it with EEXIST. Copy
- Linux behavior for now. */
-
if (!*dir)
{
set_errno (ENOENT);
__leave;
}
+ /* Following Linux, do not resolve the last component of DIR if
+ it is a symlink, even if DIR has a trailing slash. Achieve
+ this by stripping trailing slashes or backslashes. */
if (isdirsep (dir[strlen (dir) - 1]))
{
/* This converts // to /, but since both give EEXIST, we're okay. */
@@ -354,6 +353,25 @@ rmdir (const char *dir)
__try
{
+ if (!*dir)
+ {
+ set_errno (ENOENT);
+ __leave;
+ }
+
+ /* Following Linux, do not resolve the last component of DIR if
+ it is a symlink, even if DIR has a trailing slash. Achieve
+ this by stripping trailing slashes or backslashes. */
+ if (isdirsep (dir[strlen (dir) - 1]))
+ {
+ /* This converts // to /, but since both give ENOTEMPTY,
+ we're okay. */
+ char *buf;
+ char *p = stpcpy (buf = tp.c_get (), dir) - 1;
+ dir = buf;
+ while (p > dir && isdirsep (*p))
+ *p-- = '\0';
+ }
if (!(fh = build_fh_name (dir, PC_SYM_NOFOLLOW)))
__leave; /* errno already set */;
--
2.21.0