[ruijie] Replace os.system and remove subprocess with shell=True#12107
Merged
maipbui merged 5 commits intosonic-net:masterfrom Nov 28, 2022
Merged
[ruijie] Replace os.system and remove subprocess with shell=True#12107maipbui merged 5 commits intosonic-net:masterfrom
maipbui merged 5 commits intosonic-net:masterfrom
Conversation
Signed-off-by: maipbui <[email protected]>
Signed-off-by: maipbui <[email protected]>
|
This pull request introduces 3 alerts when merging e6a7151 into 1effff9 - view on LGTM.com new alerts:
|
qiluo-msft
reviewed
Sep 19, 2022
| 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)) |
Collaborator
Collaborator
There was a problem hiding this comment.
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)
qiluo-msft
reviewed
Sep 19, 2022
| @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"] |
Collaborator
qiluo-msft
reviewed
Sep 19, 2022
| 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') |
Collaborator
qiluo-msft
reviewed
Sep 19, 2022
| @@ -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) | |||
Collaborator
qiluo-msft
reviewed
Sep 19, 2022
| with open(location, 'w') as f: | ||
| f.write('0x'+'%02x'%value+'\n') | ||
| except (IOError, FileNotFoundError): | ||
| return False, 'cannot write to file' |
Collaborator
qiluo-msft
reviewed
Sep 19, 2022
| error = "cmd find dmidecode" | ||
| return False, error | ||
| cmd = log + "|grep -P -A5 \"Memory\s+Device\"|grep Size|grep -v Range" | ||
| cmd1 = split(log) |
Collaborator
qiluo-msft
reviewed
Sep 19, 2022
| error = "cmd find dmidecode" | ||
| return False, error | ||
| cmd = log + " -t 17 | grep -A21 \"Memory Device\"" # 17 | ||
| cmd1 = split(log) + ["-t", "17"] |
Collaborator
Signed-off-by: maipbui <[email protected]>
|
This pull request introduces 3 alerts when merging 7c160f9 into 1f0699f - view on LGTM.com new alerts:
|
Signed-off-by: maipbui <[email protected]>
|
This pull request introduces 3 alerts when merging 9f41865 into 51eac0b - view on LGTM.com new alerts:
|
Signed-off-by: maipbui <[email protected]>
qiluo-msft
approved these changes
Oct 18, 2022
Contributor
Author
|
@tim-rj could you help review and verify? |
StormLiangMS
pushed a commit
to StormLiangMS/sonic-buildimage
that referenced
this pull request
Dec 8, 2022
…ic-net#12107) Signed-off-by: maipbui <[email protected]> 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`
StormLiangMS
pushed a commit
that referenced
this pull request
Dec 10, 2022
) Signed-off-by: maipbui <[email protected]> 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`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Dependency: #12065
Why I did it
getstatusoutputis used without a static string and it usesshell=Truesubprocess()- when using withshell=Trueis 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
getstatusoutputwithout shell=Truesubprocess()- useshell=Falseinstead. use an array string. Ref: https://semgrep.dev/docs/cheat-sheets/python-command-injection/#mitigationos- use withsubprocessHow 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)