Skip to content

Commit

Permalink
Improve Windows mode bits
Browse files Browse the repository at this point in the history
We were too zealous about security before by only setting the owner bits
and that would cause issues for projects like redbean that check "other"
bits to determine if it's safe to serve a file. Since that doesn't exist
on Windows, it's better to have things work than not work. So what we'll
do instead is return modes like 0664 for files and 0775 for directories.
  • Loading branch information
jart committed Sep 22, 2024
1 parent 80804cc commit 556a294
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
21 changes: 8 additions & 13 deletions libc/calls/fstat-nt.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,34 +122,29 @@ textwindows int sys_fstat_nt_handle(int64_t handle, const char16_t *path,
st.st_blksize = 4096;
st.st_gid = st.st_uid = sys_getuid_nt();

// We'll use the "umask" to fake out the mode bits.
uint32_t umask = atomic_load_explicit(&__umask, memory_order_acquire);

switch (GetFileType(handle)) {
case kNtFileTypeUnknown:
break;
case kNtFileTypeChar:
st.st_mode = S_IFCHR | (0666 & ~umask);
st.st_mode = S_IFCHR | 0664;
st.st_dev = 0x66666666;
st.st_ino = handle;
break;
case kNtFileTypePipe:
st.st_mode = S_IFIFO | (0666 & ~umask);
st.st_mode = S_IFIFO | 0664;
st.st_dev = 0x55555555;
st.st_ino = handle;
break;
case kNtFileTypeDisk: {
struct NtByHandleFileInformation wst;
if (GetFileInformationByHandle(handle, &wst)) {
st.st_mode = 0444 & ~umask;
st.st_mode = 0444;
if ((wst.dwFileAttributes & kNtFileAttributeDirectory) ||
IsWindowsExecutable(handle, path)) {
st.st_mode |= 0111 & ~umask;
}
IsWindowsExecutable(handle, path))
st.st_mode |= 0111;
st.st_flags = wst.dwFileAttributes;
if (!(wst.dwFileAttributes & kNtFileAttributeReadonly)) {
st.st_mode |= 0222 & ~umask;
}
if (!(wst.dwFileAttributes & kNtFileAttributeReadonly))
st.st_mode |= 0220;
if (wst.dwFileAttributes & kNtFileAttributeReparsePoint) {
st.st_mode |= S_IFLNK;
} else if (wst.dwFileAttributes & kNtFileAttributeDirectory) {
Expand Down Expand Up @@ -195,7 +190,7 @@ textwindows int sys_fstat_nt_handle(int64_t handle, const char16_t *path,
} else if (GetVolumeInformationByHandle(
handle, 0, 0, &wst.dwVolumeSerialNumber, 0, 0, 0, 0)) {
st.st_dev = wst.dwVolumeSerialNumber;
st.st_mode = S_IFDIR | (0444 & ~umask);
st.st_mode = S_IFDIR | 0555;
} else {
// both GetFileInformationByHandle and
// GetVolumeInformationByHandle failed
Expand Down
2 changes: 1 addition & 1 deletion libc/intrin/umask.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
#include "libc/atomic.h"
#include "libc/calls/internal.h"

atomic_int __umask;
atomic_int __umask = 0777;
1 change: 0 additions & 1 deletion libc/runtime/winmain.greg.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,6 @@ abi int64_t WinMain(int64_t hInstance, int64_t hPrevInstance,
__imp_GetSystemInfo(&si);
__pagesize = si.dwPageSize;
__gransize = si.dwAllocationGranularity;
__umask = 077;
__pid = __imp_GetCurrentProcessId();
if (!(__sig.process = __sig_map_process(__pid, kNtOpenAlways)))
__sig.process = &fake_process_signals;
Expand Down

0 comments on commit 556a294

Please sign in to comment.