Overview
| Artifact ID: | 4f3dddaa1350e1257153bd4f14f27fadfd9fb8f4 |
|---|---|
| Ticket: | 1193497fffffffffffffffffffffffffffffffff
file writable lies for certain XP directories |
| User & Date: | jfg118 on 2005-10-22 06:24:00 |
Changes
- icomment:
Logged In: YES user_id=1266631 Below is a patch which implements the unicode call since Tcl seems to use unicode for WinNT by default. It _corrects_ the functionality of the AccessCheck and removes the buffer on the stack. The source is based on the Tcl 8.4.11 source and encompasses the changes by dkf. The patch should still be easily applied on the CVS HEAD. The patch is generated with diff -u (3 files) --- ../src/tcl8.4.11/win/tclWinInt.h2004-05-03 11:01:36.000000000 -0700 +++ win/tclWinInt.h2005-10-21 11:20:21.390410900 -0700 @@ -76,6 +76,8 @@ BOOL (WINAPI *getComputerNameProc)(WCHAR *, LPDWORD); DWORD (WINAPI *getCurrentDirectoryProc)(DWORD, WCHAR *); DWORD (WINAPI *getFileAttributesProc)(CONST TCHAR *); + BOOL (WINAPI *getFileSecurityProc)(CONST TCHAR *, SECURITY_INFORMATION, + PSECURITY_DESCRIPTOR, DWORD, LPDWORD); DWORD (WINAPI *getFullPathNameProc)(CONST TCHAR *, DWORD nBufferLength, WCHAR *, TCHAR **); DWORD (WINAPI *getModuleFileNameProc)(HMODULE, WCHAR *, int); --- ../src/tcl8.4.11/win/tclWin32Dll.c2005-06-06 14:04:46.000000000 -0700 +++ win/tclWin32Dll.c2005-10-21 11:20:25.075710100 -0700 @@ -89,6 +89,8 @@ (BOOL (WINAPI *)(WCHAR *, LPDWORD)) GetComputerNameA, (DWORD (WINAPI *)(DWORD, WCHAR *)) GetCurrentDirectoryA, (DWORD (WINAPI *)(CONST TCHAR *)) GetFileAttributesA, + (BOOL (WINAPI *)(CONST TCHAR *, SECURITY_INFORMATION, + PSECURITY_DESCRIPTOR, DWORD, LPDWORD)) GetFileSecurityA, (DWORD (WINAPI *)(CONST TCHAR *, DWORD nBufferLength, WCHAR *, TCHAR **)) GetFullPathNameA, (DWORD (WINAPI *)(HMODULE, WCHAR *, int)) GetModuleFileNameA, @@ -138,6 +140,8 @@ (BOOL (WINAPI *)(WCHAR *, LPDWORD)) GetComputerNameW, (DWORD (WINAPI *)(DWORD, WCHAR *)) GetCurrentDirectoryW, (DWORD (WINAPI *)(CONST TCHAR *)) GetFileAttributesW, + (BOOL (WINAPI *)(CONST TCHAR *, SECURITY_INFORMATION, + PSECURITY_DESCRIPTOR, DWORD, LPDWORD)) GetFileSecurityW, (DWORD (WINAPI *)(CONST TCHAR *, DWORD nBufferLength, WCHAR *, TCHAR **)) GetFullPathNameW, (DWORD (WINAPI *)(HMODULE, WCHAR *, int)) GetModuleFileNameW, --- ../src/tcl8.4.11/win/tclWinFile.c2005-06-22 14:23:32.000000000 -0700 +++ win/tclWinFile.c2005-10-21 16:06:21.584662400 -0700 @@ -171,6 +171,7 @@ static int NativeStat(CONST TCHAR *path, Tcl_StatBuf *statPtr, int checkLinks); static unsigned short NativeStatMode(DWORD attr, int checkLinks, int isExec); static int NativeIsExec(CONST TCHAR *path); +static int NativeIsWritable(CONST TCHAR *path); static int NativeReadReparse(CONST TCHAR* LinkDirectory, REPARSE_DATA_BUFFER* buffer); static int NativeWriteReparse(CONST TCHAR* LinkDirectory, @@ -1333,7 +1334,7 @@ return -1; } - if ((mode & W_OK) && (attr & FILE_ATTRIBUTE_READONLY)) { + if ((mode & W_OK) && !NativeIsWritable(nativePath)) { /* * File is not writable. */ @@ -1360,6 +1361,102 @@ return 0; } + /* + *---------------------------------------------------------------------- + * + * NativeIsWritable -- + * + *Determine if a path is writable. On Windows this requires more than a + *simple read only flag check for directories because Windows uses this + *as a flag to customize the folder options. [Bug 1193497] + * + * Results: + *1 = writable, 0 = not. + * + * Side effects: + *May smash the last error value (errors are just reported by this + *function as non-writable files). + * + *---------------------------------------------------------------------- + */ +static int +NativeIsWritable( + CONST TCHAR *nativePath) +{ + BYTE *secDescPtr = 0; + DWORD secDescLen = 0; + HANDLE hToken = NULL; + BOOL isWritable = FALSE; + +#define FILE_SEC_INFO_BITS \ +OWNER_SECURITY_INFORMATION \ +| GROUP_SECURITY_INFORMATION \ +| DACL_SECURITY_INFORMATION + + /* + * Read the security descriptor for the file or directory. Note the + * first call obtains the size of the security descriptor. + */ + + if (!tclWinProcs->getFileSecurityProc(nativePath, FILE_SEC_INFO_BITS, +NULL, 0, &secDescLen)) { + if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { + DWORD secDescLen2 = 0; + secDescPtr = (BYTE *) ckalloc(secDescLen); + if (!tclWinProcs->getFileSecurityProc (nativePath, FILE_SEC_INFO_BITS, + secDescPtr, secDescLen, &secDescLen2) || (secDescLen < secDescLen2)) { + goto done; + } + } else { + goto done; + } + } + + /* + * Get the security identity of the process and (if successful) use it + * with the security descriptor of the file to find out if the file really + * is writable. + */ + + if (OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, &hToken)) { +HANDLE hIToken = NULL; +BOOL status = DuplicateToken(hToken, SecurityIdentification, &hIToken); + +CloseHandle(hToken); +if (status) { + PRIVILEGE_SET privilegeSet; + DWORD privilegeSetLength = sizeof(privilegeSet); + DWORD granted; /* unused */ + DWORD accessDesired = ACCESS_WRITE; + + /* Initialize generic mapping structure to map all. */ + GENERIC_MAPPING GenericMapping; + memset(&GenericMapping, 0xff, sizeof (GENERIC_MAPPING)); + GenericMapping.GenericRead = ACCESS_READ; + GenericMapping.GenericWrite = ACCESS_WRITE; + GenericMapping.GenericExecute = 0; + GenericMapping.GenericAll = ACCESS_READ | ACCESS_WRITE; + MapGenericMask(&accessDesired, &GenericMapping); + + AccessCheck(secDescPtr, hIToken, accessDesired, + &GenericMapping, &privilegeSet, &privilegeSetLength, + &granted, &isWritable); + CloseHandle(hIToken); +} + } + + /* + * Release any temporary space we may be using and convert the writability + * value into a boolean for return. + */ + + done: + if (secDescPtr) { +ckfree(secDescPtr); + } + return isWritable != 0; +} + /* *---------------------------------------------------------------------- * - login: "jfg118"
- mimetype: "text/plain"
- username: "jfg118"