Skip to content

Commit 2d7da9e

Browse files
committed
cptbox: 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 2d7da9e

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

dmoj/cptbox/isolate.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,13 +344,19 @@ 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'):
349+
# modify file so that the correct realpath is used
348350
file = os.path.join(f'/proc/{debugger.tid}', os.path.relpath(file, '/proc/self'))
349351
projected = '/' + os.path.normpath(file).lstrip('/')
350-
elif normalized.startswith(f'/proc/{debugger.tid}/'):
352+
elif normalized.startswith(
353+
proc_dir + '/'
354+
): # Use a slash because otherwise if we are 123 then /proc/12345 matches
351355
# If the child process uses /proc/getpid()/foo, set the normalized path to be /proc/self/foo.
352356
# Access rules can more easily check /proc/self.
353-
normalized = os.path.join('/proc/self', os.path.relpath(file, f'/proc/{debugger.tid}'))
357+
normalized = os.path.join('/proc/self', os.path.relpath(file, proc_dir))
358+
elif normalized == proc_dir:
359+
normalized = '/proc/self'
354360
real = os.path.realpath(file)
355361

356362
try:
@@ -367,7 +373,6 @@ def _access_check(self, debugger: Debugger, file: str, fs_jail: FilesystemPolicy
367373
raise DeniedSyscall(ACCESS_EACCES, f'Denying {file}, normalized to {normalized}')
368374

369375
if normalized != real:
370-
proc_dir = f'/proc/{debugger.tid}'
371376
if real.startswith(proc_dir):
372377
real = os.path.join('/proc/self', os.path.relpath(real, proc_dir))
373378

0 commit comments

Comments
 (0)