Skip to content

Commit 91e8e83

Browse files
fix: fix incorrect permission may introduce security vulnerabilities.
1 parent 37d886e commit 91e8e83

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

apps/common/utils/tool_code.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ def __init__(self, sandbox=False):
2323
else:
2424
self.sandbox_path = os.path.join(PROJECT_DIR, 'data', 'sandbox')
2525
self.user = None
26-
self._createdir()
27-
if self.sandbox:
28-
os.system(f"chown -R {self.user}:root {self.sandbox_path}")
26+
self._init_dir()
2927
self.banned_keywords = CONFIG.get("SANDBOX_PYTHON_BANNED_KEYWORDS", 'nothing_is_banned').split(',');
28+
self.sandbox_so_path = f'{self.sandbox_path}/sandbox.so'
3029
try:
30+
if os.path.exists(self.sandbox_so_path):
31+
os.chmod(self.sandbox_so_path, 0o644)
32+
# 初始化host黑名单
3133
banned_hosts_file_path = f'{self.sandbox_path}/.SANDBOX_BANNED_HOSTS'
3234
if os.path.exists(banned_hosts_file_path):
3335
os.remove(banned_hosts_file_path)
@@ -43,14 +45,15 @@ def __init__(self, sandbox=False):
4345
maxkb_logger.error(f'Failed to init SANDBOX_BANNED_HOSTS due to exception: {e}', exc_info=True)
4446
pass
4547

46-
def _createdir(self):
47-
old_mask = os.umask(0o077)
48-
try:
49-
os.makedirs(self.sandbox_path, 0o700, exist_ok=True)
50-
os.makedirs(os.path.join(self.sandbox_path, 'execute'), 0o700, exist_ok=True)
51-
os.makedirs(os.path.join(self.sandbox_path, 'result'), 0o700, exist_ok=True)
52-
finally:
53-
os.umask(old_mask)
48+
def _init_dir(self):
49+
execute_file_path = os.path.join(self.sandbox_path, 'execute')
50+
os.makedirs(execute_file_path, 0o500, exist_ok=True)
51+
result_file_path = os.path.join(self.sandbox_path, 'result')
52+
os.makedirs(result_file_path, 0o300, exist_ok=True)
53+
if self.sandbox:
54+
os.system(f"chown {self.user}:root {self.sandbox_path}")
55+
os.system(f"chown -R {self.user}:root {execute_file_path}")
56+
os.system(f"chown -R {self.user}:root {result_file_path}")
5457

5558
def exec_code(self, code_str, keywords):
5659
self.validate_banned_keywords(code_str)
@@ -184,8 +187,6 @@ def get_tool_mcp_config(self, code, params):
184187
with open(code_path, 'w') as f:
185188
f.write(code)
186189
if self.sandbox:
187-
os.system(f"chown {self.user}:root {code_path}")
188-
189190
tool_config = {
190191
'command': 'su',
191192
'args': [
@@ -195,7 +196,7 @@ def get_tool_mcp_config(self, code, params):
195196
],
196197
'cwd': self.sandbox_path,
197198
'env': {
198-
'LD_PRELOAD': f'{self.sandbox_path}/sandbox.so',
199+
'LD_PRELOAD': self.sandbox_so_path,
199200
},
200201
'transport': 'stdio',
201202
}
@@ -211,10 +212,9 @@ def _exec_sandbox(self, _code, _id):
211212
exec_python_file = f'{self.sandbox_path}/execute/{_id}.py'
212213
with open(exec_python_file, 'w') as file:
213214
file.write(_code)
214-
os.system(f"chown {self.user}:root {exec_python_file}")
215215
kwargs = {'cwd': BASE_DIR}
216216
kwargs['env'] = {
217-
'LD_PRELOAD': f'{self.sandbox_path}/sandbox.so',
217+
'LD_PRELOAD': self.sandbox_so_path,
218218
}
219219
subprocess_result = subprocess.run(
220220
['su', '-s', python_directory, '-c', "exec(open('" + exec_python_file + "').read())", self.user],

0 commit comments

Comments
 (0)