public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Takashi Yano <takashi.yano@nifty.ne.jp>
To: cygwin@cygwin.com
Subject: Re: SMBFS mount's file cannot be made executable
Date: Sun, 8 Dec 2024 08:13:38 +0900	[thread overview]
Message-ID: <20241208081338.e097563889a03619fc467930@nifty.ne.jp> (raw)
In-Reply-To: <Zzz7FJim9kIiqjyy@calimero.vinschen.de>

[-- Attachment #1: Type: text/plain, Size: 2382 bytes --]

On Tue, 19 Nov 2024 21:54:44 +0100
Corinna Vinschen wrote:
> On Nov 19 17:58, Takashi Yano via Cygwin wrote:
> > On Mon, 18 Nov 2024 17:26:12 +0100
> > Corinna Vinschen wrote:
> > > We can safely assume that the current user is already authorized on the
> > > SMB server.  So... shouldn't AuthzInitializeResourceManager be
> > > sufficient and the code from class authz_ctx already does what we want?
> > > We may just have to use in in place of calling NtCheckAccess(),
> > > maybe with a tweak or two...
> > 
> > I already tried AuthzInitializeResourceManager(), but the result
> > was the same with current implementation...
> 
> So you tried to call authz_get_user_attribute()?

Yes. But resulted in the same.

> > BTW, I come up with another implementation. This make the things
> > much simpler. What do you think of the patch attached?
> 
> > [...]
> >  int
> >  check_file_access (path_conv &pc, int flags, bool effective)
> >  {
> > @@ -711,10 +618,14 @@ check_file_access (path_conv &pc, int flags, bool effective)
> >      desired |= FILE_EXECUTE;
> >    if (!get_file_sd (pc.handle (), pc, sd, false))
> >      {
> > -      /* Tweak Samba security descriptor as necessary. */
> > -      if (pc.fs_is_samba ())
> > -	convert_samba_sd (sd);
> > -      ret = check_access (sd, file_mapping, desired, flags, effective);
> > +      HANDLE h = CreateFileW (pc.get_nt_native_path ()->Buffer, desired,
> > +			      0, NULL, OPEN_EXISTING,
> > +			      FILE_FLAG_BACKUP_SEMANTICS, NULL);
> > +      if (h != INVALID_HANDLE_VALUE)
> > +	{
> > +	  CloseHandle (h);
> > +	  ret = 0;
> > +	}
> >      }
> >    debug_printf ("flags %y, ret %d", flags, ret);
> >    return ret;
> 
> No, we can't do that, it's too simple.
> 
> Just kidding.
> 
> This is so simple, I'm puzzled we never tried that before.  Or, if we
> did, it's a loooong time ago...
> 
> If we really do this, we don't even need to call get_file_sd().  And it
> should use NtOpenFile and reopen semantics i.e.  pc.init_reopen_attr().
> Also, the sharing flags should allow all access.  And the `effective'
> argument needs to be taken into account.

I have a question. What pc.init_reopen_attr() is for? I tested with
pc.get_object_attr() instead, it works. What handle should I pass
to pc.init_reopen_attr()?

Anyway, I revised the patch as attached. What do you think?

-- 
Takashi Yano <takashi.yano@nifty.ne.jp>

[-- Attachment #2: 0001-Cygwin-access-Correction-for-samba-SMB-share.patch --]
[-- Type: text/plain, Size: 6366 bytes --]

From 777bdf75527f353ac83317a82e38794206bb6dd5 Mon Sep 17 00:00:00 2001
From: Takashi Yano <takashi.yano@nifty.ne.jp>
Date: Sun, 8 Dec 2024 07:34:48 +0900
Subject: [PATCH] Cygwin: access: Correction for samba/SMB share

Previously, access() and eaccess() does not determine the permissions
for files on samba/SMB share. Even if the user logs-in as the owner
of the file, access() and eaccess() referes to others' permissions.
With this patch, to determine the permissions correctly, NtOpenFile()
with desired access mask is used.

Fixes: cf762b08cfb0 ("* security.cc (check_file_access): Create.")
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
---
 winsup/cygwin/sec/base.cc | 136 +++++++++++---------------------------
 1 file changed, 37 insertions(+), 99 deletions(-)

diff --git a/winsup/cygwin/sec/base.cc b/winsup/cygwin/sec/base.cc
index d5e39d281..fcc5e1ff7 100644
--- a/winsup/cygwin/sec/base.cc
+++ b/winsup/cygwin/sec/base.cc
@@ -28,10 +28,6 @@ details. */
 				  | GROUP_SECURITY_INFORMATION \
 				  | OWNER_SECURITY_INFORMATION)
 
-static GENERIC_MAPPING NO_COPY_RO file_mapping = { FILE_GENERIC_READ,
-						   FILE_GENERIC_WRITE,
-						   FILE_GENERIC_EXECUTE,
-						   FILE_ALL_ACCESS };
 LONG
 get_file_sd (HANDLE fh, path_conv &pc, security_descriptor &sd,
 	     bool justcreated)
@@ -608,99 +604,9 @@ check_access (security_descriptor &sd, GENERIC_MAPPING &mapping,
   return ret;
 }
 
-/* Samba override.  Check security descriptor for Samba UNIX user and group
-   accounts and check if we have an RFC 2307 mapping to a Windows account.
-   Create a new security descriptor with all of the UNIX accounts with
-   valid mapping replaced with their Windows counterpart. */
-static void
-convert_samba_sd (security_descriptor &sd_ret)
-{
-  NTSTATUS status;
-  BOOLEAN dummy;
-  PSID sid;
-  cygsid owner;
-  cygsid group;
-  SECURITY_DESCRIPTOR sd;
-  cyg_ldap cldap;
-  tmp_pathbuf tp;
-  PACL acl, oacl;
-  size_t acl_len;
-  PACCESS_ALLOWED_ACE ace;
-
-  if (!NT_SUCCESS (RtlGetOwnerSecurityDescriptor (sd_ret, &sid, &dummy)))
-    return;
-  owner = sid;
-  if (!NT_SUCCESS (RtlGetGroupSecurityDescriptor (sd_ret, &sid, &dummy)))
-    return;
-  group = sid;
-
-  if (sid_id_auth (owner) == 22)
-    {
-      struct passwd *pwd;
-      uid_t uid = owner.get_uid (&cldap);
-      if (uid < UNIX_POSIX_OFFSET && (pwd = internal_getpwuid (uid)))
-	owner.getfrompw (pwd);
-    }
-  if (sid_id_auth (group) == 22)
-    {
-      struct group *grp;
-      gid_t gid = group.get_gid (&cldap);
-      if (gid < UNIX_POSIX_OFFSET && (grp = internal_getgrgid (gid)))
-	group.getfromgr (grp);
-    }
-
-  if (!NT_SUCCESS (RtlGetDaclSecurityDescriptor (sd_ret, &dummy,
-						 &oacl, &dummy)))
-    return;
-  acl = (PACL) tp.w_get ();
-  RtlCreateAcl (acl, ACL_MAXIMUM_SIZE, ACL_REVISION);
-  acl_len = sizeof (ACL);
-
-  for (DWORD i = 0; i < oacl->AceCount; ++i)
-    if (NT_SUCCESS (RtlGetAce (oacl, i, (PVOID *) &ace)))
-      {
-	cygsid ace_sid ((PSID) &ace->SidStart);
-	if (sid_id_auth (ace_sid) == 22)
-	  {
-	    if (sid_sub_auth (ace_sid, 0) == 1) /* user */
-	      {
-		struct passwd *pwd;
-		uid_t uid = ace_sid.get_uid (&cldap);
-		if (uid < UNIX_POSIX_OFFSET && (pwd = internal_getpwuid (uid)))
-		  ace_sid.getfrompw (pwd);
-	      }
-	    else if (sid_sub_auth (ace_sid, 0) == 2) /* group */
-	      {
-		struct group *grp;
-		gid_t gid = ace_sid.get_gid (&cldap);
-		if (gid < UNIX_POSIX_OFFSET && (grp = internal_getgrgid (gid)))
-		  ace_sid.getfromgr (grp);
-	      }
-	  }
-	if (!add_access_allowed_ace (acl, ace->Mask, ace_sid, acl_len,
-				     ace->Header.AceFlags))
-	  return;
-      }
-  acl->AclSize = acl_len;
-
-  RtlCreateSecurityDescriptor (&sd, SECURITY_DESCRIPTOR_REVISION);
-  RtlSetControlSecurityDescriptor (&sd, SE_DACL_PROTECTED, SE_DACL_PROTECTED);
-  RtlSetOwnerSecurityDescriptor (&sd, owner, FALSE);
-  RtlSetGroupSecurityDescriptor (&sd, group, FALSE);
-
-  status = RtlSetDaclSecurityDescriptor (&sd, TRUE, acl, FALSE);
-  if (!NT_SUCCESS (status))
-    return;
-  DWORD sd_size = 0;
-  status = RtlAbsoluteToSelfRelativeSD (&sd, sd_ret, &sd_size);
-  if (sd_size > 0 && sd_ret.malloc (sd_size))
-    RtlAbsoluteToSelfRelativeSD (&sd, sd_ret, &sd_size);
-}
-
 int
 check_file_access (path_conv &pc, int flags, bool effective)
 {
-  security_descriptor sd;
   int ret = -1;
   ACCESS_MASK desired = 0;
   if (flags & R_OK)
@@ -709,12 +615,44 @@ check_file_access (path_conv &pc, int flags, bool effective)
     desired |= FILE_WRITE_DATA;
   if (flags & X_OK)
     desired |= FILE_EXECUTE;
-  if (!get_file_sd (pc.handle (), pc, sd, false))
+
+  NTSTATUS status;
+  if (!effective && cygheap->user.issetuid ())
+    {
+      /* Strip impersonation token temporarily */
+      HANDLE tok = NO_IMPERSONATION;
+      status = NtSetInformationThread (GetCurrentThread (),
+				       ThreadImpersonationToken,
+				       &tok, sizeof (tok));
+      if (!NT_SUCCESS (status))
+	{
+	  debug_printf("NtSetInformationThread() for stripping "
+		       "impersonation token failed: %y", status);
+	  __seterrno_from_nt_status (status);
+	  return ret;
+	}
+    }
+  OBJECT_ATTRIBUTES attr;
+  pc.get_object_attr (attr, sec_none_nih);
+  IO_STATUS_BLOCK io;
+  HANDLE h;
+  status = NtOpenFile (&h, desired, &attr, &io, FILE_SHARE_VALID_FLAGS,
+		       FILE_OPEN_FOR_BACKUP_INTENT);
+  if (NT_SUCCESS (status))
     {
-      /* Tweak Samba security descriptor as necessary. */
-      if (pc.fs_is_samba ())
-	convert_samba_sd (sd);
-      ret = check_access (sd, file_mapping, desired, flags, effective);
+      NtClose (h);
+      ret = 0;
+    }
+  if (!effective && cygheap->user.issetuid ())
+    {
+      /* Recover impersonation token */
+      HANDLE tok = cygheap->user.imp_token () ?: hProcImpToken;
+      status = NtSetInformationThread (GetCurrentThread (),
+				       ThreadImpersonationToken,
+				       &tok, sizeof (tok));
+      if (!NT_SUCCESS (status))
+	debug_printf("NtSetInformationThread() for recovering "
+		     "impersonation token failed: %y", status);
     }
   debug_printf ("flags %y, ret %d", flags, ret);
   return ret;
-- 
2.45.1


  reply	other threads:[~2024-12-07 23:13 UTC|newest]

Thread overview: 72+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-08 15:42 Lavrentiev, Anton (NIH/NLM/NCBI) [C] via cygwin
2024-11-08 11:51 ` Takashi Yano
2024-11-08 13:11   ` Corinna Vinschen
2024-11-11 10:31     ` Takashi Yano
2024-11-11 10:31       ` Takashi Yano via Cygwin
2024-11-11 10:56       ` Corinna Vinschen
2024-11-11 10:56         ` Corinna Vinschen via Cygwin
2024-11-11 11:19         ` Takashi Yano
2024-11-11 11:19           ` Takashi Yano via Cygwin
2024-11-11 11:32           ` Takashi Yano
2024-11-11 11:32             ` Takashi Yano via Cygwin
2024-11-11 11:40             ` Takashi Yano
2024-11-11 11:40               ` Takashi Yano via Cygwin
2024-11-11 12:03               ` Corinna Vinschen
2024-11-11 12:03                 ` Corinna Vinschen via Cygwin
2024-11-11 12:19                 ` Takashi Yano
2024-11-11 12:19                   ` Takashi Yano via Cygwin
2024-11-11 13:35                   ` Corinna Vinschen
2024-11-11 13:35                     ` Corinna Vinschen via Cygwin
2024-11-11 19:29                     ` Takashi Yano
2024-11-11 19:29                       ` Takashi Yano via Cygwin
2024-11-12  8:54                       ` Takashi Yano
2024-11-12 11:56                         ` Corinna Vinschen
2024-11-13  9:17                           ` Takashi Yano
2024-11-13 15:10                             ` Bill Stewart
2024-11-13 15:37                               ` Takashi Yano
2024-11-13 15:58                                 ` Bill Stewart
2024-11-13 16:08                                   ` Takashi Yano
2024-11-15 15:21                                     ` Takashi Yano
2024-11-18 16:26                                       ` Corinna Vinschen
2024-11-19  8:58                                         ` Takashi Yano
2024-11-19 20:54                                           ` Corinna Vinschen
2024-12-07 23:13                                             ` Takashi Yano [this message]
2024-12-08  7:57                                               ` Takashi Yano
2024-12-09 11:11                                               ` Corinna Vinschen
2024-11-12 11:31                       ` Corinna Vinschen
2024-11-11 11:51           ` Takashi Yano
2024-11-11 11:51             ` Takashi Yano via Cygwin
2024-11-11 11:59           ` Corinna Vinschen
2024-11-11 11:59             ` Corinna Vinschen via Cygwin
2024-11-11 12:25             ` Takashi Yano
2024-11-11 12:25               ` Takashi Yano via Cygwin
2024-11-11 13:00             ` Takashi Yano
2024-11-11 13:00               ` Takashi Yano via Cygwin
2024-11-11 13:18               ` Corinna Vinschen
2024-11-11 13:18                 ` Corinna Vinschen via Cygwin
2024-11-08 16:07   ` [EXTERNAL] " Lavrentiev, Anton (NIH/NLM/NCBI) [C]
2024-11-11  9:04     ` Takashi Yano
2024-11-11  9:04       ` Takashi Yano via Cygwin
2019-08-12 19:05 Lavrentiev, Anton (NIH/NLM/NCBI) [C] via cygwin
2019-08-13  8:28 ` KAVALAGIOS Panagiotis (EEAS-EXT)
     [not found] ` <704986a5a4ab41709eb963dcd23887b1@BELBRU-EXMP101.eeas.europa.eu>
2019-08-13 12:27   ` Lavrentiev, Anton (NIH/NLM/NCBI) [C] via cygwin
2019-08-13 18:34     ` Achim Gratz
2019-08-13 18:35     ` Andrey Repin
2019-08-13 23:19       ` Lavrentiev, Anton (NIH/NLM/NCBI) [C] via cygwin
2019-08-14 20:05         ` Andrey Repin
2019-08-14  0:53 Lavrentiev, Anton (NIH/NLM/NCBI) [C] via cygwin
2019-08-14  2:36 ` Ken Brown
2019-08-14 16:59 ` Achim Gratz
2019-08-14  4:24 Lavrentiev, Anton (NIH/NLM/NCBI) [C] via cygwin
2019-08-14 11:22 ` Ken Brown
2019-08-14 22:58   ` Brian Inglis
2019-08-14 14:07 Lavrentiev, Anton (NIH/NLM/NCBI) [C] via cygwin
2019-08-15  1:28 ` Ken Brown
2019-08-14 20:39 Lavrentiev, Anton (NIH/NLM/NCBI) [C] via cygwin
2019-08-15  1:31 ` Ken Brown
2019-08-15  1:40 Lavrentiev, Anton (NIH/NLM/NCBI) [C] via cygwin
2019-08-15  2:59 ` Brian Inglis
2019-08-15  2:00 Lavrentiev, Anton (NIH/NLM/NCBI) [C] via cygwin
2019-08-15  9:21 ` L A Walsh
2019-08-15  9:23 ` L A Walsh
2019-08-21  7:12   ` Lavrentiev, Anton (NIH/NLM/NCBI) [C] via cygwin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241208081338.e097563889a03619fc467930@nifty.ne.jp \
    --to=takashi.yano@nifty.ne.jp \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).