Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cherry-Pick][Security] check path in ls_dir #61702

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions python/paddle/distributed/fleet/utils/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import os
import re
import shutil
import subprocess
import time

# (TODO: GhostScreaming) It will be removed later.
Expand Down Expand Up @@ -513,6 +514,34 @@ def _run_cmd(self, cmd, redirect_stderr=False, retry_times=5):

return ret, output.splitlines()

def _run_safe_cmd(self, cmd, redirect_stderr=False, retry_times=5):
exe_cmd = [self._base_cmd] + cmd.split()
ret = 0
output = ""
retry_sleep_second = 3
for x in range(retry_times + 1):
try:
process = subprocess.run(
exe_cmd,
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
if redirect_stderr
else subprocess.PIPE,
text=True,
)
output = process.stdout
break
except subprocess.CalledProcessError as e:
ret = e.returncode
output = e.output
time.sleep(retry_sleep_second)
except Exception as e:
break

if ret == 134:
raise FSShellCmdAborted(cmd)

@_handle_errors()
def list_dirs(self, fs_path):
"""
Expand Down Expand Up @@ -582,8 +611,8 @@ def ls_dir(self, fs_path):
return self._ls_dir(fs_path)

def _ls_dir(self, fs_path):
cmd = f"ls {fs_path}"
ret, lines = self._run_cmd(cmd)
cmd = ["-ls", fs_path]
ret, lines = self._run_safe_cmd(cmd)

if ret != 0:
raise ExecuteError(cmd)
Expand Down