Skip to content

Commit d12b74f

Browse files
fix: fix incorrect permission may introduce security vulnerabilities.
1 parent 94df0bb commit d12b74f

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

apps/common/utils/tool_code.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __init__(self, sandbox=False):
2828
self.sandbox_so_path = f'{self.sandbox_path}/sandbox.so'
2929
try:
3030
if os.path.exists(self.sandbox_so_path):
31-
os.chmod(self.sandbox_so_path, 0o444)
31+
os.chmod(self.sandbox_so_path, 0o440)
3232
# 初始化host黑名单
3333
banned_hosts_file_path = f'{self.sandbox_path}/.SANDBOX_BANNED_HOSTS'
3434
if os.path.exists(banned_hosts_file_path):
@@ -40,7 +40,7 @@ def __init__(self, sandbox=False):
4040
banned_hosts = f"{banned_hosts},{hostname},{local_ip}"
4141
with open(banned_hosts_file_path, "w") as f:
4242
f.write(banned_hosts)
43-
os.chmod(banned_hosts_file_path, 0o444)
43+
os.chmod(banned_hosts_file_path, 0o440)
4444
except Exception as e:
4545
maxkb_logger.error(f'Failed to init SANDBOX_BANNED_HOSTS due to exception: {e}', exc_info=True)
4646
pass
@@ -50,10 +50,14 @@ def _init_dir(self):
5050
os.makedirs(execute_file_path, 0o500, exist_ok=True)
5151
result_file_path = os.path.join(self.sandbox_path, 'result')
5252
os.makedirs(result_file_path, 0o300, exist_ok=True)
53+
tmp_file_path = os.path.join(self.sandbox_path, 'tmp')
54+
os.makedirs(tmp_file_path, 0o600, exist_ok=True)
5355
if self.sandbox:
5456
os.system(f"chown {self.user}:root {self.sandbox_path}")
5557
os.system(f"chown -R {self.user}:root {execute_file_path}")
5658
os.system(f"chown -R {self.user}:root {result_file_path}")
59+
os.system(f"chown -R {self.user}:root {tmp_file_path}")
60+
os.chmod(self.sandbox_path, 0o550)
5761

5862
def exec_code(self, code_str, keywords):
5963
self.validate_banned_keywords(code_str)

0 commit comments

Comments
 (0)