[newlib-cygwin/cygwin-3_5-branch] Cygwin: path_conv: allow NULL handle in init_reopen_attr()

Corinna Vinschen corinna@sourceware.org
Tue Dec 10 15:57:48 GMT 2024


https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=dac9ad1e0d3de1c844c79c18d3c29d1d5f2059ec

commit dac9ad1e0d3de1c844c79c18d3c29d1d5f2059ec
Author:     Corinna Vinschen <corinna@vinschen.de>
AuthorDate: Tue Dec 10 13:55:54 2024 +0100
Commit:     Corinna Vinschen <corinna@vinschen.de>
CommitDate: Tue Dec 10 16:57:10 2024 +0100

    Cygwin: path_conv: allow NULL handle in init_reopen_attr()
    
    init_reopen_attr() doesn't guard against a NULL handle.  However,
    there are scenarios calling functions deliberately with a NULL handle,
    for instance, av::setup() calling check_file_access() only if opening
    the file did NOT succeed.
    
    So check for a NULL handle in init_reopen_attr() and if so, use the
    name based approach filling the OBJECT_ATTRIBUTES struct, just as in
    the has_buggy_reopen() case.
    
    Fixes: 4c9d01fdad2a ("* mount.h (class fs_info): Add has_buggy_reopen flag and accessor methods.")
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
    (cherry picked from commit 815eba882e32ecadd6862c71c36fccdcb0842a76)

Diff:
---
 winsup/cygwin/local_includes/path.h | 2 +-
 winsup/cygwin/sec/base.cc           | 7 ++-----
 2 files changed, 3 insertions(+), 6 deletions(-)

diff --git a/winsup/cygwin/local_includes/path.h b/winsup/cygwin/local_includes/path.h
index cd0cc8a41256..5f5cf5cb5b21 100644
--- a/winsup/cygwin/local_includes/path.h
+++ b/winsup/cygwin/local_includes/path.h
@@ -316,7 +316,7 @@ class path_conv
   }
   inline POBJECT_ATTRIBUTES init_reopen_attr (OBJECT_ATTRIBUTES &attr, HANDLE h)
   {
-    if (has_buggy_reopen ())
+    if (!h || has_buggy_reopen ())
       InitializeObjectAttributes (&attr, get_nt_native_path (),
 				  objcaseinsensitive (), NULL, NULL)
     else
diff --git a/winsup/cygwin/sec/base.cc b/winsup/cygwin/sec/base.cc
index 0fc8699bfc9e..14606242d835 100644
--- a/winsup/cygwin/sec/base.cc
+++ b/winsup/cygwin/sec/base.cc
@@ -62,8 +62,7 @@ get_file_sd (HANDLE fh, path_conv &pc, security_descriptor &sd,
   if (!fh || !NT_SUCCESS (status))
     {
       status = NtOpenFile (&fh, READ_CONTROL,
-			   fh ? pc.init_reopen_attr (attr, fh)
-			      : pc.get_object_attr (attr, sec_none_nih),
+			   pc.init_reopen_attr (attr, fh),
 			   &io, FILE_SHARE_VALID_FLAGS,
 			   FILE_OPEN_NO_RECALL
 			   | FILE_OPEN_FOR_BACKUP_INTENT
@@ -232,9 +231,7 @@ set_file_sd (HANDLE fh, path_conv &pc, security_descriptor &sd, bool is_chown)
 	  OBJECT_ATTRIBUTES attr;
 	  IO_STATUS_BLOCK io;
 	  status = NtOpenFile (&fh, (is_chown ? WRITE_OWNER  : 0) | WRITE_DAC,
-			       fh ? pc.init_reopen_attr (attr, fh)
-				  : pc.get_object_attr (attr, sec_none_nih),
-			       &io,
+			       pc.init_reopen_attr (attr, fh), &io,
 			       FILE_SHARE_VALID_FLAGS,
 			       FILE_OPEN_NO_RECALL
 			       | FILE_OPEN_FOR_BACKUP_INTENT


More information about the Cygwin-cvs mailing list