-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ruijie] Replace os.system and remove subprocess with shell=True #12107
Conversation
Signed-off-by: maipbui <maibui@microsoft.com>
Signed-off-by: maipbui <maibui@microsoft.com>
This pull request introduces 3 alerts when merging e6a7151 into 1effff9 - view on LGTM.com new alerts:
|
@@ -417,7 +417,7 @@ def set_status_led(self, color): | |||
if regval is None: | |||
print("Invaild color input.") | |||
return False | |||
ret , log = subprocess.getstatusoutput(self.set_sys_led_cmd + regval) | |||
ret , log = getstatusoutput_noshell(self.set_sys_led_cmd.append(regval)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You initialize the member var self.set_sys_led_cmd
but actually not using it.
Suggest
cmd = self.set_sys_led_cmd + [regval]
getstatusoutput_noshell(cmd)
return retcode, output | ||
|
||
|
||
@staticmethod | ||
def geti2cword_i2ctool(bus, addr, offset): | ||
command_line = "i2cget -f -y %d 0x%02x 0x%02x wp" % (bus, addr, offset) | ||
command_line = ["i2cget", "-f", "-y", str(bus), "0x"+"%02x"%addr, "0x"+"%02x"%offset, "wp"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
log_os_system(cmd) | ||
file = "/sys/bus/i2c/devices/i2c-%d/delete_device" % bus | ||
with open(file, 'w') as f: | ||
f.write('0x'+'%02x'%str(bus)+'\n') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -166,7 +165,9 @@ def addDev(name, bus, loc): | |||
cmd = "echo %s 0x%02x > /sys/bus/i2c/devices/i2c-%d/new_device" % (name, loc, bus) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with open(location, 'w') as f: | ||
f.write('0x'+'%02x'%value+'\n') | ||
except (IOError, FileNotFoundError): | ||
return False, 'cannot write to file' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ret != 0 or len(log) <= 0: | ||
error = "cmd find dmidecode" | ||
return False, error | ||
cmd = log + "|grep -P -A5 \"Memory\s+Device\"|grep Size|grep -v Range" | ||
cmd1 = split(log) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ret != 0 or len(log) <= 0: | ||
error = "cmd find dmidecode" | ||
return False, error | ||
cmd = log + " -t 17 | grep -A21 \"Memory Device\"" # 17 | ||
cmd1 = split(log) + ["-t", "17"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Signed-off-by: maipbui <maibui@microsoft.com>
This pull request introduces 3 alerts when merging 7c160f9 into 1f0699f - view on LGTM.com new alerts:
|
Signed-off-by: maipbui <maibui@microsoft.com>
This pull request introduces 3 alerts when merging 9f41865 into 51eac0b - view on LGTM.com new alerts:
|
@tim-rj could you help review and verify? |
…ic-net#12107) 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`
) 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`
Dependency: #12065
Why I did it
getstatusoutput
is used without a static string and it usesshell=True
subprocess()
- when using withshell=True
is dangerous. Using subprocess function without a static string can lead to command injection.os
- not secure against maliciously constructed input and dangerous if used to evaluate dynamic content.How I did it
getstatusoutput
without shell=Truesubprocess()
- useshell=False
instead. use an array string. Ref: https://semgrep.dev/docs/cheat-sheets/python-command-injection/#mitigationos
- use withsubprocess
How to verify it
Which release branch to backport (provide reason below if selected)
Description for the changelog
Ensure to add label/tag for the feature raised. example - PR#2174 where, Generic Config and Update feature has been labelled as GCU.
Link to config_db schema for YANG module changes
A picture of a cute animal (not mandatory but encouraged)