Skip to content

Commit 662a421

Browse files
authored
Safe usage of popen (#6490)
Avoid shell=True security issues with Popen
1 parent ddd3571 commit 662a421

File tree

6 files changed

+12
-10
lines changed

6 files changed

+12
-10
lines changed

deepspeed/utils/numa.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ def check_for_numactl_pkg():
4949
flag, lib, tool = data
5050
path = distutils.spawn.find_executable(pkgmgr)
5151
if path is not None:
52-
cmd = f"{pkgmgr} {flag} {lib}"
53-
result = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
52+
cmd = [pkgmgr, flag, lib]
53+
result = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
5454
if result.wait() == 0:
5555
found = True
5656
else:

op_builder/async_io.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ def check_for_libaio_pkg(self):
8181
flag, lib, tool = data
8282
path = distutils.spawn.find_executable(pkgmgr)
8383
if path is not None:
84-
cmd = f"{pkgmgr} {flag} {lib}"
85-
result = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
84+
cmd = [pkgmgr, flag, lib]
85+
result = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
8686
if result.wait() == 0:
8787
found = True
8888
else:

op_builder/builder.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,8 @@ def command_exists(self, cmd):
482482
cmds = [cmd]
483483
valid = False
484484
for cmd in cmds:
485-
result = subprocess.Popen(f'type {cmd}', stdout=subprocess.PIPE, shell=True)
485+
safe_cmd = ["bash", "-c", f"type {cmd}"]
486+
result = subprocess.Popen(safe_cmd, stdout=subprocess.PIPE)
486487
valid = valid or result.wait() == 0
487488

488489
if not valid and len(cmds) > 1:

op_builder/npu/async_io.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ def check_for_libaio_pkg(self):
7474
flag, lib, tool = data
7575
path = distutils.spawn.find_executable(pkgmgr)
7676
if path is not None:
77-
cmd = f"{pkgmgr} {flag} {lib}"
78-
result = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
77+
cmd = [pkgmgr, flag, lib]
78+
result = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
7979
if result.wait() == 0:
8080
found = True
8181
else:

op_builder/xpu/async_io.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ def check_for_libaio_pkg(self):
7070
flag, lib, tool = data
7171
path = distutils.spawn.find_executable(pkgmgr)
7272
if path is not None:
73-
cmd = f"{pkgmgr} {flag} {lib}"
74-
result = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
73+
cmd = [pkgmgr, flag, lib]
74+
result = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
7575
if result.wait() == 0:
7676
found = True
7777
else:

setup.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ def command_exists(cmd):
160160
result = subprocess.Popen(f'{cmd}', stdout=subprocess.PIPE, shell=True)
161161
return result.wait() == 1
162162
else:
163-
result = subprocess.Popen(f'type {cmd}', stdout=subprocess.PIPE, shell=True)
163+
safe_cmd = ["bash", "-c", f"type {cmd}"]
164+
result = subprocess.Popen(safe_cmd, stdout=subprocess.PIPE)
164165
return result.wait() == 0
165166

166167

0 commit comments

Comments
 (0)