Skip to content

Commit 92d25be

Browse files
authored
[inventec] Replace os.system and remove subprocess with shell=True (#12108)
Signed-off-by: maipbui <maibui@microsoft.com> Dependency: [https://github.com/sonic-net/sonic-buildimage/pull/12065](https://github.com/sonic-net/sonic-buildimage/pull/12065) #### Why I did it 1. `getstatusoutput` is used without a static string and it uses `shell=True` 2. `subprocess()` - when using with `shell=True` is dangerous. Using subprocess function without a static string can lead to command injection. 3. `os` - not secure against maliciously constructed input and dangerous if used to evaluate dynamic content. #### How I did it 1. use `getstatusoutput` without shell=True 2. `subprocess()` - use `shell=False` instead. use an array string. Ref: [https://semgrep.dev/docs/cheat-sheets/python-command-injection/#mitigation](https://semgrep.dev/docs/cheat-sheets/python-command-injection/#mitigation) 3. `os` - use with `subprocess`
1 parent ea101a9 commit 92d25be

File tree

9 files changed

+32
-23
lines changed

9 files changed

+32
-23
lines changed

platform/broadcom/sonic-platform-modules-inventec/common/utils/asic_monitor.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717

1818
try:
1919
import os
20-
import commands
21-
import sys, getopt
22-
import logging
2320
import re
2421
import time
2522
import syslog
2623
from sonic_sfp.bcmshell import bcmshell
24+
from sonic_py_common.general import getstatusoutput_noshell
2725

2826
except ImportError as e:
2927
raise ImportError("%s - required module not found" % str(e))
@@ -53,10 +51,10 @@ def initialLoop():
5351
bcm_obj = BCMUtil()
5452
bcm_obj.execute_command("echo")
5553
initialNotOK = False
56-
print bcm_obj
54+
print(bcm_obj)
5755
log_message( syslog.LOG_INFO, "BCMUtil Object initialed successfully" )
58-
except Exception, e:
59-
print "Exception. The warning is {0}".format(str(e))
56+
except Exception as e:
57+
print("Exception. The warning is {0}".format(str(e)))
6058
time.sleep(10)
6159

6260
class BCMUtil(bcmshell):
@@ -66,7 +64,7 @@ class BCMUtil(bcmshell):
6664

6765
def get_platform(self):
6866
if self.platform is None:
69-
self.platform = os.popen("uname -n").read().strip()
67+
_, self.platform = getstatusoutput_noshell(["uname", "-n"]).strip()
7068
return self.platform
7169

7270
def get_asic_temperature( self ):
@@ -102,14 +100,18 @@ def main():
102100
content = readPtr.read().strip()
103101
if bcm_obj.get_platform() == INV_SEQUOIA_PLATFORM :
104102
if content == "inv_bmc" and SWITCH_TEMP_FILE_NAME in file_list :
105-
os.system("echo {0} > {1}/{2}/device/{3}".format( ( bcm_obj.get_asic_temperature() * 1000 ), HWMON_PATH, index, SWITCH_TEMP_FILE_NAME ))
103+
file = "{0}/{1}/device/{2}".format(HWMON_PATH, index, SWITCH_TEMP_FILE_NAME)
104+
with open(file, 'w') as f:
105+
f.write(str(bcm_obj.get_asic_temperature() * 1000) + '\n')
106106
break
107107
else :
108108
if content == "inv_psoc" and SWITCH_TEMP_FILE_NAME in file_list :
109-
print "echo {0} > {1}/{2}/device/{3}".format( ( bcm_obj.get_asic_temperature() * 1000 ), HWMON_PATH, index, SWITCH_TEMP_FILE_NAME )
110-
os.system("echo {0} > {1}/{2}/device/{3}".format( ( bcm_obj.get_asic_temperature() * 1000 ), HWMON_PATH, index, SWITCH_TEMP_FILE_NAME ))
109+
print("echo {0} > {1}/{2}/device/{3}".format( ( bcm_obj.get_asic_temperature() * 1000 ), HWMON_PATH, index, SWITCH_TEMP_FILE_NAME))
110+
file = "{0}/{1}/device/{2}".format(HWMON_PATH, index, SWITCH_TEMP_FILE_NAME)
111+
with open(file, 'w') as f:
112+
f.write(str(bcm_obj.get_asic_temperature() * 1000) + '\n')
111113
break
112-
except Exception, e:
114+
except Exception as e:
113115
log_message( syslog.LOG_WARNING, "Exception. The warning is {0}".format(str(e)) )
114116
initialLoop()
115117
time.sleep(5)

platform/broadcom/sonic-platform-modules-inventec/common/utils/led_proc.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import syslog
2121
import re
2222
from sonic_sfp.bcmshell import bcmshell
23+
from sonic_py_common.general import getstatusoutput_noshell
2324

2425

2526
# =====================================================================
@@ -120,8 +121,8 @@ def _board_init():
120121
global SYNC_S
121122
global SYNC_P
122123

123-
cmd = "uname -n"
124-
platform = os.popen(cmd).read()
124+
cmd = ["uname", "-n"]
125+
_, platform = getstatusoutput_noshell(cmd)
125126

126127
if platform.rstrip() == INV_MAGNOLIA:
127128
BOARD_TPYE = "inventec_d6254qs"

platform/broadcom/sonic-platform-modules-inventec/common/utils/platform_status.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os
44
import socket
5+
import subprocess
56
from collections import OrderedDict
67

78
# Purpose: Shutdown DUT upon receiving thermaltrip event from kernel (inv_pthread)
@@ -59,6 +60,6 @@ def next_events(self):
5960

6061
# Receive thermaltrip event
6162
if event['ACTION'] == 'remove' and event['DEVPATH'] == '/kernel/platform_status/fan':
62-
os.system("shutdown -h now")
63+
subprocess.call(["shutdown", "-h", "now"])
6364

6465

platform/broadcom/sonic-platform-modules-inventec/common/utils/transceiver_monitor.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import syslog
2929
from sfputil import SfpUtil
3030
from sonic_sfp.bcmshell import bcmshell
31+
from sonic_py_common.general import getstatusoutput_noshell
3132

3233
except ImportError as e:
3334
raise ImportError("%s - required module not found" % str(e))
@@ -127,7 +128,7 @@ class BCMUtil(bcmshell):
127128

128129
def get_platform(self):
129130
if self.platform is None:
130-
self.platform = os.popen("uname -n").read().strip()
131+
_, self.platform = getstatusoutput_noshell(["uname", "-n"])
131132
return self.platform
132133

133134
def get_port_to_bcm_mapping(self):

platform/broadcom/sonic-platform-modules-inventec/d6332/sonic_platform/qsfp.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
try:
99
import os
1010
import logging
11+
import subprocess
1112
from ctypes import create_string_buffer
1213
from sonic_platform_base.sfp_base import SfpBase
1314
from sonic_platform_base.sonic_sfp.sff8436 import sff8436Dom
@@ -120,7 +121,7 @@ def __set_attr_value(self, attr_path, value):
120121
return True
121122

122123
def __is_host(self):
123-
return os.system("docker > /dev/null 2>&1") == 0
124+
return subprocess.call(["docker"]) == 0
124125

125126
def __get_path_to_port_config_file(self):
126127
host_platform_root_path = '/usr/share/sonic/device'

platform/broadcom/sonic-platform-modules-inventec/d6332/utils/inventec_d6332_util.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import logging
3434
import syslog
3535
import time
36+
from sonic_py_common.general import getstatusoutput_noshell_pipe
3637

3738
DEBUG = False
3839
args = []
@@ -236,8 +237,9 @@ def system_install(boot_option):
236237
return status
237238
for addr_offset in range (0,FAN_NUM):
238239
addr=FAN_VPD_ADDR_BASE+addr_offset
239-
cmd = "i2cdetect -y "+str(FAN_VPD_CHANNEL)+" "+str(addr)+" "+str(addr)+" | grep "+str(hex(addr)).replace('0x','')
240-
result=os.system(cmd)
240+
cmd1 = ["i2cdetect", "-y", str(FAN_VPD_CHANNEL), str(addr), str(addr)]
241+
cmd2 = ["grep", f'{addr:x}']
242+
result, _ = getstatusoutput_noshell_pipe(cmd1, cmd2)
241243
if( result==0 ):
242244
cmd="echo inv_eeprom "+str(addr)+" > /sys/bus/i2c/devices/i2c-"+FAN_VPD_CHANNEL
243245
status, output = exec_cmd(cmd,1)

platform/broadcom/sonic-platform-modules-inventec/d6356/sonic_platform/qsfp.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
import sys
1111
import time
12+
import subprocess
1213
from sonic_platform_base.sfp_base import SfpBase
1314
from sonic_platform_base.sonic_sfp.sff8436 import sff8436Dom
1415
from sonic_platform_base.sonic_sfp.sff8436 import sff8436InterfaceId
@@ -108,7 +109,7 @@ def __get_attr_value(self, attr_path):
108109
return retval
109110

110111
def __is_host(self):
111-
return os.system("docker > /dev/null 2>&1") == 0
112+
return subprocess.call(["docker"]) == 0
112113

113114
def __get_path_to_port_config_file(self):
114115
host_platform_root_path = '/usr/share/sonic/device'

platform/broadcom/sonic-platform-modules-inventec/d6356/sonic_platform/sfp.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
try:
99
import os
10-
import sys
10+
import subprocess
1111
from sonic_platform_base.sfp_base import SfpBase
1212
from sonic_platform_base.sonic_sfp.sff8472 import sff8472Dom
1313
from sonic_platform_base.sonic_sfp.sff8472 import sff8472InterfaceId
@@ -95,7 +95,7 @@ def __get_attr_value(self, attr_path):
9595
return retval
9696

9797
def __is_host(self):
98-
return os.system("docker > /dev/null 2>&1") == 0
98+
return subprocess.call(["docker"]) == 0
9999

100100
def __get_path_to_port_config_file(self):
101101
host_platform_root_path = '/usr/share/sonic/device'

platform/broadcom/sonic-platform-modules-inventec/d7054q28b/sonic_platform/sfp.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class Sfp(SfpBase):
153153
# Path to QSFP sysfs
154154
PLATFORM_ROOT_PATH = "/usr/share/sonic/device"
155155
PMON_HWSKU_PATH = "/usr/share/sonic/hwsku"
156-
HOST_CHK_CMD = "docker > /dev/null 2>&1"
156+
HOST_CHK_CMD = ["docker"]
157157

158158
PLATFORM = "x86_64-inventec_d7054q28b-r0"
159159
HWSKU = "INVENTEC-D7054Q28B-S48-Q6"
@@ -285,7 +285,7 @@ def __read_txt_file(self, file_path):
285285
return ""
286286

287287
def __is_host(self):
288-
return os.system(self.HOST_CHK_CMD) == 0
288+
return subprocess.call(self.HOST_CHK_CMD) == 0
289289

290290
def __get_path_to_port_config_file(self):
291291
platform_path = "/".join([self.PLATFORM_ROOT_PATH, self.PLATFORM])

0 commit comments

Comments
 (0)