[PATCH 1/7] Cygwin: fix fstat on sockets that are not socket files

Ken Brown kbrown@cornell.edu
Thu Feb 25 22:48:06 GMT 2021


If fstat(2) is called on an AF_LOCAL or AF_UNIX socket that is not a
socket file, the current code calls fstat_fs.  The latter expects to
be operating on a disk file and uses the socket's io_handle, which is
not a file handle.

Fix this by calling fstat_fs only if the fhandler_socket object is a
file (determined by testing dev().isfs()).
---
 winsup/cygwin/fhandler_socket_local.cc |  9 +++++----
 winsup/cygwin/fhandler_socket_unix.cc  | 11 +++++------
 2 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/winsup/cygwin/fhandler_socket_local.cc b/winsup/cygwin/fhandler_socket_local.cc
index 964f3e819..f8adf6c46 100644
--- a/winsup/cygwin/fhandler_socket_local.cc
+++ b/winsup/cygwin/fhandler_socket_local.cc
@@ -673,11 +673,12 @@ fhandler_socket_local::fcntl (int cmd, intptr_t arg)
 int __reg2
 fhandler_socket_local::fstat (struct stat *buf)
 {
-  int res;
-
-  if (get_sun_path () && get_sun_path ()[0] == '\0')
+  if (!dev ().isfs ())
+    /* fstat called on a socket. */
     return fhandler_socket_wsock::fstat (buf);
-  res = fhandler_base::fstat_fs (buf);
+
+  /* stat/lstat on a socket file or fstat on a socket opened w/ O_PATH. */
+  int res = fhandler_base::fstat_fs (buf);
   if (!res)
     {
       buf->st_mode = (buf->st_mode & ~S_IFMT) | S_IFSOCK;
diff --git a/winsup/cygwin/fhandler_socket_unix.cc b/winsup/cygwin/fhandler_socket_unix.cc
index eedb0847e..8091fa820 100644
--- a/winsup/cygwin/fhandler_socket_unix.cc
+++ b/winsup/cygwin/fhandler_socket_unix.cc
@@ -2337,13 +2337,12 @@ fhandler_socket_unix::fcntl (int cmd, intptr_t arg)
 int __reg2
 fhandler_socket_unix::fstat (struct stat *buf)
 {
-  int ret = 0;
-
-  if (sun_path ()
-      && (sun_path ()->un_len <= (socklen_t) sizeof (sa_family_t)
-	  || sun_path ()->un.sun_path[0] == '\0'))
+  if (!dev ().isfs ())
+    /* fstat called on a socket. */
     return fhandler_socket::fstat (buf);
-  ret = fhandler_base::fstat_fs (buf);
+
+  /* stat/lstat on a socket file or fstat on a socket opened w/ O_PATH. */
+  int ret = fhandler_base::fstat_fs (buf);
   if (!ret)
     {
       buf->st_mode = (buf->st_mode & ~S_IFMT) | S_IFSOCK;
-- 
2.30.0



More information about the Cygwin-patches mailing list