Skip to content

Commit 3d15da9

Browse files
committed
declare proc_dir earlier and resolve exact proc_dir
We can declare the proc directory earlier in the code and use it. We intentionally add a trailing slash on line 352, in case for instance, the process PID is 123 and they request /proc/12345. Finally, we add a check for the literal directory /proc/getpid()
1 parent 749ab2f commit 3d15da9

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

dmoj/cptbox/isolate.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,13 +344,18 @@ def _access_check(self, debugger: Debugger, file: str, fs_jail: FilesystemPolicy
344344

345345
# normpath doesn't strip leading slashes
346346
projected = normalized = '/' + os.path.normpath(file).lstrip('/')
347+
proc_dir = f'/proc/{debugger.tid}'
347348
if normalized.startswith('/proc/self'):
348349
file = os.path.join(f'/proc/{debugger.tid}', os.path.relpath(file, '/proc/self'))
349350
projected = '/' + os.path.normpath(file).lstrip('/')
350-
elif normalized.startswith(f'/proc/{debugger.tid}/'):
351+
elif normalized.startswith(
352+
proc_dir + '/'
353+
): # Use a slash because otherwise if we are 123 then /proc/12345 matches
351354
# If the child process uses /proc/getpid()/foo, set the normalized path to be /proc/self/foo.
352355
# Access rules can more easily check /proc/self.
353-
normalized = os.path.join('/proc/self', os.path.relpath(file, f'/proc/{debugger.tid}'))
356+
normalized = os.path.join('/proc/self', os.path.relpath(file, proc_dir))
357+
elif normalized == proc_dir:
358+
normalized = '/proc/self'
354359
real = os.path.realpath(file)
355360

356361
try:
@@ -367,7 +372,6 @@ def _access_check(self, debugger: Debugger, file: str, fs_jail: FilesystemPolicy
367372
raise DeniedSyscall(ACCESS_EACCES, f'Denying {file}, normalized to {normalized}')
368373

369374
if normalized != real:
370-
proc_dir = f'/proc/{debugger.tid}'
371375
if real.startswith(proc_dir):
372376
real = os.path.join('/proc/self', os.path.relpath(real, proc_dir))
373377

0 commit comments

Comments
 (0)