Skip to content
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

Merged
merged 5 commits into from
Nov 28, 2022

Conversation

maipbui
Copy link
Contributor

@maipbui maipbui commented Sep 19, 2022

Dependency: #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
  3. os - use with subprocess

How to verify it

Which release branch to backport (provide reason below if selected)

  • 201811
  • 201911
  • 202006
  • 202012
  • 202106
  • 202111
  • 202205

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)

Signed-off-by: maipbui <maibui@microsoft.com>
Signed-off-by: maipbui <maibui@microsoft.com>
@lgtm-com
Copy link

lgtm-com bot commented Sep 19, 2022

This pull request introduces 3 alerts when merging e6a7151 into 1effff9 - view on LGTM.com

new alerts:

  • 3 for Unused local variable

@maipbui maipbui requested a review from qiluo-msft September 19, 2022 14:34
@@ -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))
Copy link
Collaborator

@qiluo-msft qiluo-msft Sep 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

append

It will change variable, so next call will be unexpected. #Closed

Copy link
Collaborator

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"]
Copy link
Collaborator

@qiluo-msft qiluo-msft Sep 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"0x"+"%02x"

Just use one string? #Closed

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')
Copy link
Collaborator

@qiluo-msft qiluo-msft Sep 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'0x'+'%02x'

Just one string? #Closed

@@ -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)
Copy link
Collaborator

@qiluo-msft qiluo-msft Sep 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cmd

This line can be removed. #Closed

with open(location, 'w') as f:
f.write('0x'+'%02x'%value+'\n')
except (IOError, FileNotFoundError):
return False, 'cannot write to file'
Copy link
Collaborator

@qiluo-msft qiluo-msft Sep 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'cannot write to file'

str(ex) may be better and more meaningful?
#Closed

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)
Copy link
Collaborator

@qiluo-msft qiluo-msft Sep 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

split(log)

The original behavior actually could be improved.

cmd1 = [log[0].rstrip('\n')]
``` #Closed

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"]
Copy link
Collaborator

@qiluo-msft qiluo-msft Sep 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

split

The same #Closed

Signed-off-by: maipbui <maibui@microsoft.com>
@lgtm-com
Copy link

lgtm-com bot commented Oct 5, 2022

This pull request introduces 3 alerts when merging 7c160f9 into 1f0699f - view on LGTM.com

new alerts:

  • 2 for Unused local variable
  • 1 for Wrong number of arguments in a call

@maipbui maipbui requested a review from qiluo-msft October 7, 2022 15:15
Signed-off-by: maipbui <maibui@microsoft.com>
@lgtm-com
Copy link

lgtm-com bot commented Oct 17, 2022

This pull request introduces 3 alerts when merging 9f41865 into 51eac0b - view on LGTM.com

new alerts:

  • 2 for Unused local variable
  • 1 for Wrong number of arguments in a call

Signed-off-by: maipbui <maibui@microsoft.com>
@maipbui
Copy link
Contributor Author

maipbui commented Oct 26, 2022

@tim-rj could you help review and verify?

@maipbui maipbui marked this pull request as ready for review November 28, 2022 17:42
@maipbui maipbui requested a review from lguohan as a code owner November 28, 2022 17:42
@maipbui maipbui merged commit 35c4e99 into sonic-net:master Nov 28, 2022
@maipbui maipbui deleted the ruijie_sec branch November 28, 2022 17:43
StormLiangMS pushed a commit to StormLiangMS/sonic-buildimage that referenced this pull request Dec 8, 2022
…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`
StormLiangMS pushed a commit that referenced this pull request Dec 10, 2022
)

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`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants