Skip to content

Commit

Permalink
Safe usage of popen (#6490)
Browse files Browse the repository at this point in the history
Avoid shell=True security issues with Popen
  • Loading branch information
tjruwase authored Sep 4, 2024
1 parent ddd3571 commit 662a421
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions deepspeed/utils/numa.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def check_for_numactl_pkg():
flag, lib, tool = data
path = distutils.spawn.find_executable(pkgmgr)
if path is not None:
cmd = f"{pkgmgr} {flag} {lib}"
result = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
cmd = [pkgmgr, flag, lib]
result = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result.wait() == 0:
found = True
else:
Expand Down
4 changes: 2 additions & 2 deletions op_builder/async_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ def check_for_libaio_pkg(self):
flag, lib, tool = data
path = distutils.spawn.find_executable(pkgmgr)
if path is not None:
cmd = f"{pkgmgr} {flag} {lib}"
result = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
cmd = [pkgmgr, flag, lib]
result = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result.wait() == 0:
found = True
else:
Expand Down
3 changes: 2 additions & 1 deletion op_builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,8 @@ def command_exists(self, cmd):
cmds = [cmd]
valid = False
for cmd in cmds:
result = subprocess.Popen(f'type {cmd}', stdout=subprocess.PIPE, shell=True)
safe_cmd = ["bash", "-c", f"type {cmd}"]
result = subprocess.Popen(safe_cmd, stdout=subprocess.PIPE)
valid = valid or result.wait() == 0

if not valid and len(cmds) > 1:
Expand Down
4 changes: 2 additions & 2 deletions op_builder/npu/async_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ def check_for_libaio_pkg(self):
flag, lib, tool = data
path = distutils.spawn.find_executable(pkgmgr)
if path is not None:
cmd = f"{pkgmgr} {flag} {lib}"
result = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
cmd = [pkgmgr, flag, lib]
result = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result.wait() == 0:
found = True
else:
Expand Down
4 changes: 2 additions & 2 deletions op_builder/xpu/async_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def check_for_libaio_pkg(self):
flag, lib, tool = data
path = distutils.spawn.find_executable(pkgmgr)
if path is not None:
cmd = f"{pkgmgr} {flag} {lib}"
result = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
cmd = [pkgmgr, flag, lib]
result = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result.wait() == 0:
found = True
else:
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ def command_exists(cmd):
result = subprocess.Popen(f'{cmd}', stdout=subprocess.PIPE, shell=True)
return result.wait() == 1
else:
result = subprocess.Popen(f'type {cmd}', stdout=subprocess.PIPE, shell=True)
safe_cmd = ["bash", "-c", f"type {cmd}"]
result = subprocess.Popen(safe_cmd, stdout=subprocess.PIPE)
return result.wait() == 0


Expand Down

0 comments on commit 662a421

Please sign in to comment.