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

[device/celestica] Mitigation for command injection vulnerability #11740

Merged
merged 30 commits into from
Dec 9, 2022

Conversation

maipbui
Copy link
Contributor

@maipbui maipbui commented Aug 15, 2022

Signed-off-by: maipbui maibui@microsoft.com

Dependency: PR (#12065) needs to merge first.

Why I did it

  1. eval() - not secure against maliciously constructed input, can be dangerous if used to evaluate dynamic content. This may be a code injection vulnerability.
  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.
  4. is operator - string comparison should not be used with reference equality.
  5. globals() - extremely dangerous because it may allow an attacker to execute arbitrary code on the system

How I did it

  1. eval() - use literal_eval()
  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
  4. is - replace by == operator for value equality
  5. globals() - avoid the use of globals()

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

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>
Signed-off-by: maipbui <maibui@microsoft.com>
@maipbui maipbui changed the title [device/celestica][e1031] Mitigation for command injection vulnerability [device/celestica] Mitigation for command injection vulnerability Aug 15, 2022
Signed-off-by: maipbui <maibui@microsoft.com>
@lgtm-com
Copy link

lgtm-com bot commented Aug 15, 2022

This pull request introduces 1 alert and fixes 6 when merging c24eec3 into a66941a - view on LGTM.com

new alerts:

  • 1 for Syntax error

fixed alerts:

  • 3 for Variable defined multiple times
  • 2 for Unused import
  • 1 for Wrong name for an argument in a class instantiation

Signed-off-by: maipbui <maibui@microsoft.com>
Signed-off-by: maipbui <maibui@microsoft.com>
@qiluo-msft
Copy link
Collaborator

def get_output(self, index, config, default):

Seems this function is not used. Please remove.
@mudsut4ke Could you help review?


Refers to: device/celestica/x86_64-cel_e1031-r0/sonic_platform/common.py:226 in ca56944. [](commit_id = ca56944, deletion_comment = False)

@qiluo-msft
Copy link
Collaborator

def set_output(self, index, input, config):

Seems this function is not used. Please remove.
@mudsut4ke Could you help review?


Refers to: device/celestica/x86_64-cel_e1031-r0/sonic_platform/common.py:281 in ca56944. [](commit_id = ca56944, deletion_comment = False)

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

lgtm-com bot commented Sep 1, 2022

This pull request introduces 7 alerts and fixes 6 when merging dada9ae into fdd9130 - view on LGTM.com

new alerts:

  • 3 for Unused local variable
  • 2 for Unused import
  • 1 for Unreachable code
  • 1 for Wrong number of arguments in a call

fixed alerts:

  • 5 for Except block handles 'BaseException'
  • 1 for Unused import

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

lgtm-com bot commented Sep 1, 2022

This pull request introduces 5 alerts and fixes 6 when merging 91781fe into fdd9130 - view on LGTM.com

new alerts:

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

fixed alerts:

  • 5 for Except block handles 'BaseException'
  • 1 for Unused import

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

lgtm-com bot commented Sep 1, 2022

This pull request introduces 1 alert and fixes 6 when merging 6647f81 into fdd9130 - view on LGTM.com

new alerts:

  • 1 for Wrong number of arguments in a call

fixed alerts:

  • 5 for Except block handles 'BaseException'
  • 1 for Unused import

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

lgtm-com bot commented Sep 6, 2022

This pull request introduces 1 alert and fixes 12 when merging a7b8055 into 1b5d07f - view on LGTM.com

new alerts:

  • 1 for Wrong number of arguments in a call

fixed alerts:

  • 6 for Except block handles 'BaseException'
  • 6 for Unused import

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

lgtm-com bot commented Sep 21, 2022

This pull request introduces 1 alert and fixes 13 when merging 65b4300 into fd6a1b0 - view on LGTM.com

new alerts:

  • 1 for Wrong number of arguments in a call

fixed alerts:

  • 8 for Unused import
  • 5 for Except block handles 'BaseException'

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

lgtm-com bot commented Sep 21, 2022

This pull request introduces 1 alert and fixes 13 when merging 0ce54ef into fd6a1b0 - view on LGTM.com

new alerts:

  • 1 for Wrong number of arguments in a call

fixed alerts:

  • 8 for Unused import
  • 5 for Except block handles 'BaseException'

@lgtm-com
Copy link

lgtm-com bot commented Oct 5, 2022

This pull request fixes 13 alerts when merging a971633 into 1f0699f - view on LGTM.com

fixed alerts:

  • 8 for Unused import
  • 5 for Except block handles 'BaseException'

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

lgtm-com bot commented Oct 6, 2022

This pull request fixes 13 alerts when merging 92147d7 into 1073a47 - view on LGTM.com

fixed alerts:

  • 8 for Unused import
  • 5 for Except block handles 'BaseException'

@maipbui maipbui marked this pull request as ready for review November 30, 2022 01:48
@maipbui
Copy link
Contributor Author

maipbui commented Nov 30, 2022

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@qiluo-msft
Copy link
Collaborator

@Blueve is connecting with Celestica reviewers.

@Blueve
Copy link
Contributor

Blueve commented Dec 6, 2022

Celestica team acked to review, ETA: By Dec 11

@@ -195,7 +185,7 @@ def write_txt_file(self, file_path, value):
return True

def is_host(self):
return os.system(self.HOST_CHK_CMD) == 0
return subprocess.call(self.HOST_CHK_CMD) == 0
Copy link
Contributor

@qnos qnos Dec 8, 2022

Choose a reason for hiding this comment

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

If use subprocess.call, need to suppress stdout/stderr and handle docker command not found exception.
The following example codes work in both host and docker mode.

def is_host():
    try:
        subprocess.call(HOST_CHK_CMD, stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL)
    except FileNotFoundError:
        return False
    return True

Copy link
Contributor Author

@maipbui maipbui Dec 8, 2022

Choose a reason for hiding this comment

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

I think os.system is equivalent with subprocess.call without specifying stdout and stderr to DEVNULL. Ref: https://docs.python.org/3/library/subprocess.html#replacing-os-system

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I got what you meant, I will change per your suggestion

Copy link
Contributor

Choose a reason for hiding this comment

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

I think os.system is equivalent with subprocess.call without specifying stdout and stderr to DEVNULL. Ref: https://docs.python.org/3/library/subprocess.html#replacing-os-system

Yes, no essential differences between the two functions, but HOST_CHK_CMD had been changed from "docker > /dev/null 2>&1" to ["docker"], so there is no way to suppress the 'docker' command output any more in host mode. And handling the FileNotFoundError exception is also necessary for docker mode.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. Could you review and approve? Thank you!

@@ -15,7 +15,7 @@ def __init__(self):
(self.platform, self.hwsku) = device_info.get_platform_and_hwsku()

def is_host(self):
return os.system(HOST_CHK_CMD) == 0
return subprocess.call(HOST_CHK_CMD) == 0
Copy link
Contributor

Choose a reason for hiding this comment

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

Please consider changing the is_host function implementation as suggested above.

@@ -13,7 +14,7 @@ def __init__(self):
pass

def is_host(self):
return os.system(HOST_CHK_CMD) == 0
return subprocess.call(HOST_CHK_CMD) == 0
Copy link
Contributor

Choose a reason for hiding this comment

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

Please consider changing the is_host function implementation as suggested above.

@@ -270,7 +269,7 @@ def __convert_string_to_num(self, value_str):
return 'N/A'

def __is_host(self):
return os.system(self.HOST_CHK_CMD) == 0
return subprocess.call(self.HOST_CHK_CMD) == 0
Copy link
Contributor

Choose a reason for hiding this comment

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

Please consider changing the is_host function implementation as suggested above.

@@ -166,7 +167,7 @@ def write_txt_file(self, file_path, value):
return True

def is_host(self):
return os.system(self.HOST_CHK_CMD) == 0
return subprocess.call(self.HOST_CHK_CMD) == 0
Copy link
Contributor

Choose a reason for hiding this comment

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

Please consider changing the is_host function implementation as suggested above.

Copy link
Contributor

@qnos qnos left a comment

Choose a reason for hiding this comment

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

Except the is_host() function implementation, agree with all other changes.

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

lgtm-com bot commented Dec 8, 2022

This pull request fixes 13 alerts when merging 34991a5 into d993444 - view on LGTM.com

fixed alerts:

  • 8 for Unused import
  • 5 for Except block handles 'BaseException'

Heads-up: LGTM.com's PR analysis will be disabled on the 5th of December, and LGTM.com will be shut down ⏻ completely on the 16th of December 2022. Please enable GitHub code scanning, which uses the same CodeQL engine ⚙️ that powers LGTM.com. For more information, please check out our post on the GitHub blog.

@maipbui maipbui merged commit 51a1eb1 into sonic-net:master Dec 9, 2022
@maipbui maipbui deleted the celestica_e1031_security branch December 9, 2022 15:30
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.

4 participants