Skip to content

Commit 556a294

Browse files
committed
Improve Windows mode bits
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.
1 parent 80804cc commit 556a294

File tree

3 files changed

+9
-15
lines changed

3 files changed

+9
-15
lines changed

libc/calls/fstat-nt.c

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,34 +122,29 @@ textwindows int sys_fstat_nt_handle(int64_t handle, const char16_t *path,
122122
st.st_blksize = 4096;
123123
st.st_gid = st.st_uid = sys_getuid_nt();
124124

125-
// We'll use the "umask" to fake out the mode bits.
126-
uint32_t umask = atomic_load_explicit(&__umask, memory_order_acquire);
127-
128125
switch (GetFileType(handle)) {
129126
case kNtFileTypeUnknown:
130127
break;
131128
case kNtFileTypeChar:
132-
st.st_mode = S_IFCHR | (0666 & ~umask);
129+
st.st_mode = S_IFCHR | 0664;
133130
st.st_dev = 0x66666666;
134131
st.st_ino = handle;
135132
break;
136133
case kNtFileTypePipe:
137-
st.st_mode = S_IFIFO | (0666 & ~umask);
134+
st.st_mode = S_IFIFO | 0664;
138135
st.st_dev = 0x55555555;
139136
st.st_ino = handle;
140137
break;
141138
case kNtFileTypeDisk: {
142139
struct NtByHandleFileInformation wst;
143140
if (GetFileInformationByHandle(handle, &wst)) {
144-
st.st_mode = 0444 & ~umask;
141+
st.st_mode = 0444;
145142
if ((wst.dwFileAttributes & kNtFileAttributeDirectory) ||
146-
IsWindowsExecutable(handle, path)) {
147-
st.st_mode |= 0111 & ~umask;
148-
}
143+
IsWindowsExecutable(handle, path))
144+
st.st_mode |= 0111;
149145
st.st_flags = wst.dwFileAttributes;
150-
if (!(wst.dwFileAttributes & kNtFileAttributeReadonly)) {
151-
st.st_mode |= 0222 & ~umask;
152-
}
146+
if (!(wst.dwFileAttributes & kNtFileAttributeReadonly))
147+
st.st_mode |= 0220;
153148
if (wst.dwFileAttributes & kNtFileAttributeReparsePoint) {
154149
st.st_mode |= S_IFLNK;
155150
} else if (wst.dwFileAttributes & kNtFileAttributeDirectory) {
@@ -195,7 +190,7 @@ textwindows int sys_fstat_nt_handle(int64_t handle, const char16_t *path,
195190
} else if (GetVolumeInformationByHandle(
196191
handle, 0, 0, &wst.dwVolumeSerialNumber, 0, 0, 0, 0)) {
197192
st.st_dev = wst.dwVolumeSerialNumber;
198-
st.st_mode = S_IFDIR | (0444 & ~umask);
193+
st.st_mode = S_IFDIR | 0555;
199194
} else {
200195
// both GetFileInformationByHandle and
201196
// GetVolumeInformationByHandle failed

libc/intrin/umask.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919
#include "libc/atomic.h"
2020
#include "libc/calls/internal.h"
2121

22-
atomic_int __umask;
22+
atomic_int __umask = 0777;

libc/runtime/winmain.greg.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,6 @@ abi int64_t WinMain(int64_t hInstance, int64_t hPrevInstance,
316316
__imp_GetSystemInfo(&si);
317317
__pagesize = si.dwPageSize;
318318
__gransize = si.dwAllocationGranularity;
319-
__umask = 077;
320319
__pid = __imp_GetCurrentProcessId();
321320
if (!(__sig.process = __sig_map_process(__pid, kNtOpenAlways)))
322321
__sig.process = &fake_process_signals;

0 commit comments

Comments
 (0)