Skip to content

Commit 3cd9b2e

Browse files
authored
[device/centec] Replace os.system and remove subprocess with shell=True (sonic-net#12024)
Signed-off-by: maipbui <[email protected]> #### Why I did it `subprocess.Popen()` and `subprocess.run()` is used with `shell=True`, which is very dangerous for shell injection. `os` - not secure against maliciously constructed input and dangerous if used to evaluate dynamic content #### How I did it Replace `os` by `subprocess`, remove `shell=True` Remove unused functions
1 parent d5a3613 commit 3cd9b2e

File tree

12 files changed

+52
-72
lines changed

12 files changed

+52
-72
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#!/usr/bin/python
2-
import os
32

43
def main():
54
# reboot the system
6-
os.system('echo 502 > /sys/class/gpio/export')
7-
os.system('echo out > /sys/class/gpio/gpio502/direction')
8-
os.system('echo 1 > /sys/class/gpio/gpio502/value')
5+
with open('/sys/class/gpio/export', 'w') as file:
6+
file.write('502\n')
7+
with open('/sys/class/gpio/gpio502/direction', 'w') as file:
8+
file.write('out\n')
9+
with open('/sys/class/gpio/gpio502/value', 'w') as file:
10+
file.write('1\n')
911

1012
if __name__ == "__main__":
1113
main()
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#!/usr/bin/python
2-
import os
32

43
def main():
54
# reboot the system
6-
os.system('echo 502 > /sys/class/gpio/export')
7-
os.system('echo out > /sys/class/gpio/gpio502/direction')
8-
os.system('echo 1 > /sys/class/gpio/gpio502/value')
5+
with open('/sys/class/gpio/export', 'w') as file:
6+
file.write('502\n')
7+
with open('/sys/class/gpio/gpio502/direction', 'w') as file:
8+
file.write('out\n')
9+
with open('/sys/class/gpio/gpio502/value', 'w') as file:
10+
file.write('1\n')
911

1012
if __name__ == "__main__":
1113
main()
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/usr/bin/python
2-
import os
2+
import subprocess
33

44
def main():
55
# reboot the system
6-
os.system('modprobe i2c-dev')
7-
os.system('i2cset -y 0 0x36 0x23 0x0')
8-
os.system('sleep 1')
9-
os.system('i2cset -y 0 0x36 0x23 0x3')
6+
subprocess.call(['modprobe', 'i2c-dev'])
7+
subprocess.call(['i2cset', '-y', '0', '0x36', '0x23', '0x0'])
8+
subprocess.call(['sleep', '1'])
9+
subprocess.call(['i2cset', '-y', '0', '0x36', '0x23', '0x3'])
1010

1111
if __name__ == "__main__":
1212
main()
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#!/usr/bin/python
2-
import os
32

43
def main():
54
# reboot the system
6-
os.system('echo 502 > /sys/class/gpio/export')
7-
os.system('echo out > /sys/class/gpio/gpio502/direction')
8-
os.system('echo 1 > /sys/class/gpio/gpio502/value')
5+
with open('/sys/class/gpio/export', 'w') as file:
6+
file.write('502\n')
7+
with open('/sys/class/gpio/gpio502/direction', 'w') as file:
8+
file.write('out\n')
9+
with open('/sys/class/gpio/gpio502/value', 'w') as file:
10+
file.write('1\n')
911

1012
if __name__ == "__main__":
1113
main()
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#!/usr/bin/env python
22

3-
import os
3+
import subprocess
44
import time
55

66
def main():
7-
os.system('hwclock -w -f /dev/rtc1')
7+
subprocess.call(['hwclock', '-w', '-f', '/dev/rtc1'])
88
time.sleep(1)
99

10-
os.system('i2cset -y 0 0x36 0x23 0')
10+
subprocess.call(['i2cset', '-y', '0', '0x36', '0x23', '0'])
1111
time.sleep(1)
12-
os.system('i2cset -y 0 0x36 0x23 1')
12+
subprocess.call(['i2cset', '-y', '0', '0x36', '0x23', '1'])
1313

1414
if __name__ == '__main__':
1515
main()

device/centec/x86_64-centec_v682_48x8c-r0/plugins/led_control.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@ def _port_led_mode_update(self, port_idx, ledMode):
166166

167167
def _initSystemLed(self):
168168
try:
169-
cmd = 'i2cset -y 0 0x36 0x2 0x5'
170-
Popen(cmd, shell=True)
169+
cmd = ['i2cset', '-y', '0', '0x36', '0x2', '0x5']
170+
Popen(cmd)
171171
DBG_PRINT("init system led to normal")
172-
cmd = 'i2cset -y 0 0x36 0x3 0x1'
173-
Popen(cmd, shell=True)
172+
cmd = ['i2cset', '-y', '0', '0x36', '0x3', '0x1']
173+
Popen(cmd)
174174
DBG_PRINT("init idn led to off")
175175
except IOError as e:
176176
DBG_PRINT(str(e))

device/centec/x86_64-centec_v682_48x8c-r0/plugins/psuutil.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ def get_psu_status(self, index):
4040
if index is None:
4141
return False
4242

43-
cmd = 'i2cget -y 0 0x36 0x1e'
44-
status = int(Popen(cmd, stdout=PIPE, stderr=STDOUT, shell=True).stdout.readline(), 16)
43+
cmd = ['i2cget', '-y', '0', '0x36', '0x1e']
44+
status = int(Popen(cmd, stdout=PIPE, stderr=STDOUT).stdout.readline(), 16)
4545
powergood = ((status & (1 << (3 * (index - 1) + 2))) != 0)
4646
return powergood
4747

@@ -56,7 +56,7 @@ def get_psu_presence(self, index):
5656
if index is None:
5757
return False
5858

59-
cmd = 'i2cget -y 0 0x36 0x1e'
60-
status = int(Popen(cmd, stdout=PIPE, stderr=STDOUT, shell=True).stdout.readline(), 16)
59+
cmd = ['i2cget', '-y', '0', '0x36', '0x1e']
60+
status = int(Popen(cmd, stdout=PIPE, stderr=STDOUT).stdout.readline(), 16)
6161
presence = ((status & (1 << (3 * (index - 1) + 1))) == 0)
6262
return presence
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#!/usr/bin/env python
22

3-
import os
3+
import subprocess
44
import time
55

66
def main():
7-
os.system('hwclock -w -f /dev/rtc1')
7+
subprocess.call(['hwclock', '-w', '-f', '/dev/rtc1'])
88
time.sleep(1)
99

10-
os.system('i2cset -y 0 0x36 0x23 0')
10+
subprocess.call(['i2cset', '-y', '0', '0x36', '0x23', '0'])
1111
time.sleep(1)
12-
os.system('i2cset -y 0 0x36 0x23 1')
12+
subprocess.call(['i2cset', '-y', '0', '0x36', '0x23', '1'])
1313

1414
if __name__ == '__main__':
1515
main()

device/centec/x86_64-centec_v682_48y8c-r0/plugins/led_control.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@ def _port_led_mode_update(self, port_idx, ledMode):
166166

167167
def _initSystemLed(self):
168168
try:
169-
cmd = 'i2cset -y 0 0x36 0x2 0x5'
170-
Popen(cmd, shell=True)
169+
cmd = ['i2cset', '-y', '0', '0x36', '0x2', '0x5']
170+
Popen(cmd)
171171
DBG_PRINT("init system led to normal")
172-
cmd = 'i2cset -y 0 0x36 0x3 0x1'
173-
Popen(cmd, shell=True)
172+
cmd = ['i2cset', '-y', '0', '0x36', '0x3', '0x1']
173+
Popen(cmd)
174174
DBG_PRINT("init idn led to off")
175175
except IOError as e:
176176
DBG_PRINT(str(e))

device/centec/x86_64-centec_v682_48y8c-r0/plugins/psuutil.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ def get_psu_status(self, index):
4040
if index is None:
4141
return False
4242

43-
cmd = 'i2cget -y 0 0x36 0x1e'
44-
status = int(Popen(cmd, stdout=PIPE, stderr=STDOUT, shell=True).stdout.readline(), 16)
43+
cmd = ['i2cget', '-y', '0', '0x36', '0x1e']
44+
status = int(Popen(cmd, stdout=PIPE, stderr=STDOUT).stdout.readline(), 16)
4545
powergood = ((status & (1 << (3 * (index - 1) + 2))) != 0)
4646
return powergood
4747

@@ -56,7 +56,7 @@ def get_psu_presence(self, index):
5656
if index is None:
5757
return False
5858

59-
cmd = 'i2cget -y 0 0x36 0x1e'
60-
status = int(Popen(cmd, stdout=PIPE, stderr=STDOUT, shell=True).stdout.readline(), 16)
59+
cmd = ['i2cget', '-y', '0', '0x36', '0x1e']
60+
status = int(Popen(cmd, stdout=PIPE, stderr=STDOUT).stdout.readline(), 16)
6161
presence = ((status & (1 << (3 * (index - 1) + 1))) == 0)
6262
return presence

device/centec/x86_64-ew_es6220_x48q2h4-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
"""
@@ -46,8 +46,9 @@ def get_psu_presence(self, index):
4646
Base_bus_number = 39
4747
status = 0
4848
try:
49-
p = os.popen(self.psu_presence.format(index + Base_bus_number) + "> /dev/null 2>&1")
50-
if p.readline() != None:
49+
self.psu_presence[2] = str(index + Base_bus_number)
50+
p = subprocess.Popen(self.psu_presence)
51+
if p.stdout.readline() is not None:
5152
status = 1
5253
p.close()
5354
except IOError:

device/centec/x86_64-ew_es6220_x48q2h4-r0/plugins/sfputil.py

-27
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
try:
77
import time
8-
import os
98
from sonic_sfp.sfputilbase import SfpUtilBase
109
except ImportError as e:
1110
raise ImportError("%s - required module not found" % str(e))
@@ -47,32 +46,6 @@ def __init__(self):
4746

4847
SfpUtilBase.__init__(self)
4948

50-
def get_presence(self, port_name):
51-
# modify by zhw to get sfp presence
52-
# Check for invalid port_num
53-
port_num = int(port_name[8:])
54-
55-
if port_num < (self.port_start+1) or port_num > (self.port_end+1):
56-
return False
57-
58-
# cpld info from "CPLD Register for es5800A2.2(V1.1)"
59-
cpld_map = {0: '0x82', 1: '0x83', 2: '0x84',
60-
3: '0x85', 4: '0x86', 5: '0x87', 6: '0x8E'}
61-
cpld_key = (port_num - 1)/8
62-
cpld_mask = (1 << (port_num - 1) % 8)
63-
64-
# use i2cget cmd to get cpld data
65-
output = os.popen('i2cdetect -l | grep CP')
66-
bus_num = output.read()[4]
67-
cmd = "i2cget -y "+bus_num+" 0x5 "+cpld_map[cpld_key]
68-
tmp = os.popen(cmd).read().replace("\n", "")
69-
cpld_value = int(tmp, 16)
70-
71-
if cpld_value & cpld_mask == 0:
72-
return True
73-
else:
74-
return False
75-
7649
def get_low_power_mode(self, port_num):
7750
'''
7851
# Check for invalid port_num

0 commit comments

Comments
 (0)