Skip to content

Commit ea101a9

Browse files
authored
[device/delta] Mitigation for command injection vulnerability (sonic-net#11865)
#### Why I did it `os` execution functions are not secure against maliciously constructed input. #### How I did it Use `subprocess` module
1 parent fad4034 commit ea101a9

File tree

6 files changed

+34
-28
lines changed

6 files changed

+34
-28
lines changed

device/delta/x86_64-delta_ag5648-r0/plugins/psuutil.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import os.path
1+
import subprocess
22

33
try:
44
from sonic_psu.psu_base import PsuBase
@@ -14,7 +14,7 @@ def __init__(self):
1414

1515
self.psu_path = "/sys/bus/i2c/devices/6-00{}/"
1616
self.psu_oper_status = "in1_input"
17-
self.psu_presence = "i2cget -y 6 0x{} 0x00"
17+
self.psu_presence = ["i2cget", "-y", "6", "", "0x00"]
1818

1919
def get_num_psus(self):
2020
"""
@@ -44,9 +44,10 @@ def get_psu_presence(self, index):
4444
return False
4545
Base_bus_number = 49
4646
status = 0
47+
self.psu_presence[3] = "0x" + str(index + Base_bus_number)
4748
try:
48-
p = os.popen(self.psu_presence.format(index + Base_bus_number) + "> /dev/null 2>&1")
49-
if p.readline() != None:
49+
p = subprocess.Popen(self.psu_presence, stdout=subprocess.PIPE, universal_newlines=True)
50+
if p.stdout.readline() != None:
5051
status = 1
5152
p.close()
5253
except IOError:

device/delta/x86_64-delta_ag9032v1-r0/plugins/psuutil.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import os.path
1+
import subprocess
22

33
try:
44
from sonic_psu.psu_base import PsuBase
@@ -14,7 +14,7 @@ def __init__(self):
1414

1515
self.psu_path = "/sys/bus/i2c/devices/{}-0058/"
1616
self.psu_oper_status = "in1_input"
17-
self.psu_presence = "i2cget -y {} 0x50 0x00"
17+
self.psu_presence = ["i2cget", "-y", "", "0x50", "0x00"]
1818

1919
def get_num_psus(self):
2020
"""
@@ -45,9 +45,10 @@ def get_psu_presence(self, index):
4545
return False
4646
Base_bus_number = 39
4747
status = 0
48+
self.psu_presence[2] = str(index + Base_bus_number)
4849
try:
49-
p = os.popen(self.psu_presence.format(index + Base_bus_number) + "> /dev/null 2>&1")
50-
if p.readline() != None:
50+
p = subprocess.Popen(self.psu_presence, stdout=subprocess.PIPE, universal_newlines=True)
51+
if p.stdout.readline() != None:
5152
status = 1
5253
p.close()
5354
except IOError:

device/delta/x86_64-delta_ag9032v2a-r0/plugins/psuutil.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# provides the PSUs status which are available in the platform
44
#
55

6-
import os.path
76
import subprocess
87

98
try:
@@ -38,8 +37,9 @@ def get_psu_status(self, index):
3837
return False
3938
status = 0
4039
try:
41-
p = os.popen("ipmitool raw 0x38 0x2 3 0x6a 0x3 1")
42-
content = p.readline().rstrip()
40+
cmd = ["ipmitool", "raw", "0x38", "0x2", "3", "0x6a", "0x3", "1"]
41+
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
42+
content = p.stdout.readline().rstrip()
4343
reg_value = int(content, 16)
4444
if index == 1:
4545
mask = (1 << 6)
@@ -66,8 +66,9 @@ def get_psu_presence(self, index):
6666

6767
status = 0
6868
try:
69-
p = os.popen("ipmitool raw 0x38 0x2 3 0x6a 0x3 1")
70-
content = p.readline().rstrip()
69+
cmd = ["ipmitool", "raw", "0x38", "0x2", "3", "0x6a", "0x3", "1"]
70+
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
71+
content = p.stdout.readline().rstrip()
7172
reg_value = int(content, 16)
7273
if index == 1:
7374
mask = (1 << 7)

device/delta/x86_64-delta_ag9064-r0/plugins/psuutil.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# provides the PSUs status which are available in the platform
44
#
55

6-
import os.path
76
import subprocess
87

98
try:
@@ -17,8 +16,8 @@ class PsuUtil(PsuBase):
1716

1817
def __init__(self):
1918
PsuBase.__init__(self)
20-
self.psu_presence = "cat /sys/devices/platform/delta-ag9064-cpld.0/psu{}_scan"
21-
self.psu_status = "cat /sys/devices/platform/delta-ag9064-swpld1.0/psu{}_pwr_ok"
19+
self.psu_presence = "/sys/devices/platform/delta-ag9064-cpld.0/psu{}_scan"
20+
self.psu_status = "/sys/devices/platform/delta-ag9064-swpld1.0/psu{}_pwr_ok"
2221

2322
def get_num_psus(self):
2423
"""
@@ -40,8 +39,9 @@ def get_psu_status(self, index):
4039
return False
4140

4241
status = 0
42+
self.psu_status = self.psu_status.format(index)
4343
try:
44-
p = os.popen(self.psu_status.format(index))
44+
p = open(self.psu_status, 'r')
4545
content = p.readline().rstrip()
4646
reg_value = int(content)
4747
if reg_value != 0:
@@ -63,8 +63,9 @@ def get_psu_presence(self, index):
6363
if index is None:
6464
return False
6565
status = 0
66+
self.psu_presence = self.psu_presence.format(index)
6667
try:
67-
p = os.popen(self.psu_presence.format(index))
68+
p = open(self.psu_presence, 'r')
6869
content = p.readline().rstrip()
6970
reg_value = int(content, 16)
7071
if reg_value != 0:

device/delta/x86_64-delta_agc032-r0/plugins/psuutil.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import os.path
1+
import subprocess
22

33
try:
44
from sonic_psu.psu_base import PsuBase
@@ -15,7 +15,7 @@ def __init__(self):
1515
self.psu_path = "/sys/bus/i2c/devices/{}-0058/"
1616
self.psu_oper_status = "in1_input"
1717
self.psu_oper_status2 = "in2_input"
18-
self.psu_presence = "i2cget -y {} 0x50 0x00"
18+
self.psu_presence = ["i2cget", "-y", "", "0x50", "0x00"]
1919

2020
def get_num_psus(self):
2121
"""
@@ -50,9 +50,10 @@ def get_psu_presence(self, index):
5050
return False
5151
Base_bus_number = 0
5252
status = 0
53+
self.psu_presence[2] = str(index + Base_bus_number)
5354
try:
54-
p = os.popen(self.psu_presence.format(index + Base_bus_number) + "> /dev/null 2>&1")
55-
if p.readline() != None:
55+
p = subprocess.Popen(self.psu_presence, stdout=subprocess.PIPE, universal_newlines=True)
56+
if p.stdout.readline() != None:
5657
status = 1
5758
p.close()
5859
except IOError:

device/delta/x86_64-delta_et-c032if-r0/plugins/psuutil.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# provides the PSUs status which are available in the platform
44
#
55

6-
import os.path
76
import subprocess
87

98
try:
@@ -17,7 +16,7 @@ class PsuUtil(PsuBase):
1716

1817
def __init__(self):
1918
PsuBase.__init__(self)
20-
self.psu_status = "ipmitool raw 0x38 0x1 {} 0x50"
19+
self.psu_status = ["ipmitool", "raw", "0x38", "0x1", "", "0x50"]
2120

2221
def get_num_psus(self):
2322
"""
@@ -39,9 +38,10 @@ def get_psu_status(self, index):
3938
return False
4039

4140
status = 0
41+
cmd = ["ipmitool", "raw", "0x38", "0x2", "7", "0x32", "0x28", "1"]
4242
try:
43-
p = os.popen("ipmitool raw 0x38 0x2 7 0x32 0x28 1")
44-
content = p.readline().rstrip()
43+
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
44+
content = p.stdout.readline().rstrip()
4545
reg_value = int(content, 16)
4646
mask = (1 << (8 - index))
4747
if reg_value & mask == 0:
@@ -63,9 +63,10 @@ def get_psu_presence(self, index):
6363
if index is None:
6464
return False
6565
status = 0
66+
self.psu_status[4] = str(index-1)
6667
try:
67-
p = os.popen(self.psu_status.format(index - 1))
68-
content = p.readline().rstrip()
68+
p = subprocess.Popen(self.psu_status, stdout=subprocess.PIPE, universal_newlines=True)
69+
content = p.stdout.readline().rstrip()
6970
reg_value = int(content, 16)
7071
if reg_value != 0:
7172
return False

0 commit comments

Comments
 (0)