o_cloexec; preserve leading slashes in root-level paths

This commit is contained in:
Ryan Fleury
2025-11-07 16:37:30 -08:00
parent d4d2159389
commit 035e72306d
4 changed files with 327 additions and 312 deletions
+10 -10
View File
@@ -338,7 +338,7 @@ dmn_lnx_ehdr_from_pid(pid_t pid)
String8 exe_path = dmn_lnx_exe_path_from_pid(scratch.arena, pid); String8 exe_path = dmn_lnx_exe_path_from_pid(scratch.arena, pid);
if(exe_path.size != 0) if(exe_path.size != 0)
{ {
int exe_fd = open((char *)exe_path.str, O_RDONLY); int exe_fd = open((char *)exe_path.str, O_RDONLY|O_CLOEXEC);
if(exe_fd != -1) if(exe_fd != -1)
{ {
is_read = dmn_lnx_read_ehdr(exe_fd, 0, &exe); is_read = dmn_lnx_read_ehdr(exe_fd, 0, &exe);
@@ -358,7 +358,7 @@ dmn_lnx_auxv_from_pid(pid_t pid, ELF_Class elf_class)
// rjf: open aux data // rjf: open aux data
String8 auxv_path = push_str8f(scratch.arena, "/proc/%d/auxv", pid); String8 auxv_path = push_str8f(scratch.arena, "/proc/%d/auxv", pid);
int auxv_fd = open((char*)auxv_path.str, O_RDONLY); int auxv_fd = open((char*)auxv_path.str, O_RDONLY|O_CLOEXEC);
// rjf: scan aux data // rjf: scan aux data
if(auxv_fd >= 0) if(auxv_fd >= 0)
@@ -583,7 +583,7 @@ dmn_lnx_rdebug_vaddr_from_memory(int memory_fd, U64 loader_vbase)
} }
} }
exit:; exit:;
scratch_end(scratch); scratch_end(scratch);
return rdebug_vaddr; return rdebug_vaddr;
} }
@@ -712,7 +712,7 @@ dmn_lnx_read_probes(Arena *arena, int fd, U64 offset, U64 image_base)
probes.count += 1; probes.count += 1;
} }
exit:; exit:;
scratch_end(scratch); scratch_end(scratch);
return probes; return probes;
} }
@@ -1452,7 +1452,7 @@ dmn_lnx_process_unloaded_modules(Arena *arena, DMN_EventList *events, DMN_LNX_En
} }
is_unmap_complete_finished = 1; is_unmap_complete_finished = 1;
exit:; exit:;
Assert(is_unmap_complete_finished); Assert(is_unmap_complete_finished);
scratch_end(scratch); scratch_end(scratch);
} }
@@ -1667,7 +1667,7 @@ dmn_ctrl_launch(DMN_CtrlCtx *ctx, OS_ProcessLaunchParams *params)
case LaunchStatus_Success: case LaunchStatus_Success:
{ {
ELF_Hdr64 exe_ehdr = dmn_lnx_ehdr_from_pid(pid); ELF_Hdr64 exe_ehdr = dmn_lnx_ehdr_from_pid(pid);
int memory_fd = open((char*)str8f(scratch.arena, "/proc/%d/mem", pid).str, O_RDWR); int memory_fd = open((char*)str8f(scratch.arena, "/proc/%d/mem", pid).str, O_RDWR|O_CLOEXEC);
DMN_LNX_ProcessAuxv auxv = dmn_lnx_auxv_from_pid(pid, exe_ehdr.e_ident[ELF_Identifier_Class]); DMN_LNX_ProcessAuxv auxv = dmn_lnx_auxv_from_pid(pid, exe_ehdr.e_ident[ELF_Identifier_Class]);
Arch arch = arch_from_elf_machine(exe_ehdr.e_machine); Arch arch = arch_from_elf_machine(exe_ehdr.e_machine);
U64 rdebug_vaddr = dmn_lnx_rdebug_vaddr_from_memory(memory_fd, auxv.base); U64 rdebug_vaddr = dmn_lnx_rdebug_vaddr_from_memory(memory_fd, auxv.base);
@@ -1702,7 +1702,7 @@ dmn_ctrl_launch(DMN_CtrlCtx *ctx, OS_ProcessLaunchParams *params)
String8 dl_path = {0}; String8 dl_path = {0};
{ {
int maps_fd = open((char *)str8f(scratch.arena, "/proc/%d/maps", pid).str, O_RDONLY); int maps_fd = open((char *)str8f(scratch.arena, "/proc/%d/maps", pid).str, O_RDONLY|O_CLOEXEC);
if(maps_fd != -1) if(maps_fd != -1)
{ {
struct stat st = {0}; struct stat st = {0};
@@ -1785,7 +1785,7 @@ dmn_ctrl_launch(DMN_CtrlCtx *ctx, OS_ProcessLaunchParams *params)
DMN_LNX_Probe **known_probes = push_array(process_arena, DMN_LNX_Probe *, DMN_LNX_ProbeType_Count); DMN_LNX_Probe **known_probes = push_array(process_arena, DMN_LNX_Probe *, DMN_LNX_ProbeType_Count);
{ {
DMN_LNX_ProbeList probes = {0}; DMN_LNX_ProbeList probes = {0};
int dl_fd = open((char *)dl_path.str, O_RDONLY); int dl_fd = open((char *)dl_path.str, O_RDONLY|O_CLOEXEC);
if(dl_fd >= 0) if(dl_fd >= 0)
{ {
probes = dmn_lnx_read_probes(process_arena, dl_fd, 0, auxv.base); probes = dmn_lnx_read_probes(process_arena, dl_fd, 0, auxv.base);
@@ -1797,9 +1797,9 @@ dmn_ctrl_launch(DMN_CtrlCtx *ctx, OS_ProcessLaunchParams *params)
DMN_LNX_Probe *p = &n->v; DMN_LNX_Probe *p = &n->v;
if(str8_match(p->provider, str8_lit("rtld"), 0)) if(str8_match(p->provider, str8_lit("rtld"), 0))
{ {
#define X(_N,_A,_S) if(str8_match(p->name, str8_lit(_S), 0)) { AssertAlways(p->args.count == _A); known_probes[DMN_LNX_ProbeType_##_N] = p; continue ; } #define X(_N,_A,_S) if(str8_match(p->name, str8_lit(_S), 0)) { AssertAlways(p->args.count == _A); known_probes[DMN_LNX_ProbeType_##_N] = p; continue ; }
DMN_LNX_Probe_XList DMN_LNX_Probe_XList
#undef X #undef X
} }
} }
} }
+4 -4
View File
@@ -110,7 +110,7 @@ internal int
demon_lnx_open_memory_fd_for_pid(pid_t pid){ demon_lnx_open_memory_fd_for_pid(pid_t pid){
Temp scratch = scratch_begin(0, 0); Temp scratch = scratch_begin(0, 0);
String8 memory_path = push_str8f(scratch.arena, "/proc/%i/mem", pid); String8 memory_path = push_str8f(scratch.arena, "/proc/%i/mem", pid);
int result = open((char*)memory_path.str, O_RDWR); int result = open((char*)memory_path.str, O_RDWR|O_CLOEXEC);
scratch_end(scratch); scratch_end(scratch);
return(result); return(result);
} }
@@ -126,7 +126,7 @@ demon_lnx_arch_from_pid(pid_t pid){
// handle to exe // handle to exe
int exe_fd = -1; int exe_fd = -1;
if (exe_path.size != 0){ if (exe_path.size != 0){
exe_fd = open((char*)exe_path.str, O_RDONLY); exe_fd = open((char*)exe_path.str, O_RDONLY|O_CLOEXEC);
} }
// elf identification // elf identification
@@ -199,7 +199,7 @@ demon_lnx_aux_from_pid(pid_t pid, Arch arch){
// open aux data // open aux data
Temp scratch = scratch_begin(0, 0); Temp scratch = scratch_begin(0, 0);
String8 auxv_symbol_path = push_str8f(scratch.arena, "/proc/%d/auxv", pid); String8 auxv_symbol_path = push_str8f(scratch.arena, "/proc/%d/auxv", pid);
int aux_fd = open((char*)auxv_symbol_path.str, O_RDONLY); int aux_fd = open((char*)auxv_symbol_path.str, O_RDONLY|O_CLOEXEC);
// scan aux data // scan aux data
if (aux_fd >= 0){ if (aux_fd >= 0){
@@ -670,7 +670,7 @@ internal int
demon_lnx_open_maps(pid_t pid){ demon_lnx_open_maps(pid_t pid){
Temp scratch = scratch_begin(0, 0); Temp scratch = scratch_begin(0, 0);
String8 path = push_str8f(scratch.arena, "/proc/%d/maps", pid); String8 path = push_str8f(scratch.arena, "/proc/%d/maps", pid);
int maps = open((char*)path.str, O_RDONLY); int maps = open((char*)path.str, O_RDONLY|O_CLOEXEC);
scratch_end(scratch); scratch_end(scratch);
return(maps); return(maps);
} }
+16 -2
View File
@@ -1977,12 +1977,26 @@ rdim_bake_path_tree_insert(RDIM_Arena *arena, RDIM_BakePathTree *tree, RDIM_Stri
RDI_U8 *opl = string.str + string.size; RDI_U8 *opl = string.str + string.size;
for(;ptr < opl;) for(;ptr < opl;)
{ {
// rjf: skip past slashes // rjf: skip past non-leading slashes
RDI_U32 leading_slash = 0;
if(ptr > string.str)
{
for(;ptr < opl && (*ptr == '/' || *ptr == '\\'); ptr += 1); for(;ptr < opl && (*ptr == '/' || *ptr == '\\'); ptr += 1);
}
else if(ptr < opl)
{
leading_slash = (*ptr == '/');
}
// rjf: save beginning of non-slash range // rjf: save beginning of path part
RDI_U8 *range_first = ptr; RDI_U8 *range_first = ptr;
// rjf: advance past leading slash
if(leading_slash)
{
ptr += 1;
}
// rjf: skip past non-slashes // rjf: skip past non-slashes
for(;ptr < opl && !(*ptr == '/' || *ptr == '\\'); ptr += 1); for(;ptr < opl && !(*ptr == '/' || *ptr == '\\'); ptr += 1);
+2 -1
View File
@@ -284,6 +284,7 @@ os_file_open(OS_AccessFlags flags, String8 path)
{ {
lnx_flags |= O_CREAT; lnx_flags |= O_CREAT;
} }
lnx_flags |= O_CLOEXEC;
int fd = open((char *)path_copy.str, lnx_flags, 0755); int fd = open((char *)path_copy.str, lnx_flags, 0755);
OS_Handle handle = {0}; OS_Handle handle = {0};
if(fd != -1) if(fd != -1)
@@ -652,7 +653,7 @@ os_shared_memory_alloc(U64 size, String8 name)
{ {
Temp scratch = scratch_begin(0, 0); Temp scratch = scratch_begin(0, 0);
String8 name_copy = push_str8_copy(scratch.arena, name); String8 name_copy = push_str8_copy(scratch.arena, name);
int id = shm_open((char *)name_copy.str, O_RDWR | O_CREAT, 0666); int id = shm_open((char *)name_copy.str, O_RDWR|O_CREAT, 0666);
ftruncate(id, size); ftruncate(id, size);
OS_Handle result = {(U64)id}; OS_Handle result = {(U64)id};
scratch_end(scratch); scratch_end(scratch);