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
+5 -5
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);
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)
{
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
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
if(auxv_fd >= 0)
@@ -1667,7 +1667,7 @@ dmn_ctrl_launch(DMN_CtrlCtx *ctx, OS_ProcessLaunchParams *params)
case LaunchStatus_Success:
{
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]);
Arch arch = arch_from_elf_machine(exe_ehdr.e_machine);
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};
{
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)
{
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_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)
{
probes = dmn_lnx_read_probes(process_arena, dl_fd, 0, auxv.base);
+4 -4
View File
@@ -110,7 +110,7 @@ internal int
demon_lnx_open_memory_fd_for_pid(pid_t pid){
Temp scratch = scratch_begin(0, 0);
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);
return(result);
}
@@ -126,7 +126,7 @@ demon_lnx_arch_from_pid(pid_t pid){
// handle to exe
int exe_fd = -1;
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
@@ -199,7 +199,7 @@ demon_lnx_aux_from_pid(pid_t pid, Arch arch){
// open aux data
Temp scratch = scratch_begin(0, 0);
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
if (aux_fd >= 0){
@@ -670,7 +670,7 @@ internal int
demon_lnx_open_maps(pid_t pid){
Temp scratch = scratch_begin(0, 0);
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);
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;
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);
}
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;
// rjf: advance past leading slash
if(leading_slash)
{
ptr += 1;
}
// rjf: skip past non-slashes
for(;ptr < opl && !(*ptr == '/' || *ptr == '\\'); ptr += 1);
+1
View File
@@ -284,6 +284,7 @@ os_file_open(OS_AccessFlags flags, String8 path)
{
lnx_flags |= O_CREAT;
}
lnx_flags |= O_CLOEXEC;
int fd = open((char *)path_copy.str, lnx_flags, 0755);
OS_Handle handle = {0};
if(fd != -1)