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

testcase for bgp container in container hardening #8694

Merged
merged 7 commits into from
Jul 21, 2023

Conversation

maipbui
Copy link
Contributor

@maipbui maipbui commented Jun 22, 2023

Description of PR

HLD implementation: Container Hardening (sonic-net/SONiC#1364)
Dependency: sonic-net/sonic-buildimage#14932

Summary:
Fixes # (issue)

Type of change

  • Bug fix
  • Testbed and Framework(new/improvement)
  • Test case(new/improvement)

Back port request

  • 201911
  • 202012
  • 202205

Approach

What is the motivation for this PR?

Check bgp container has access to /dev/sda* or /dev/vda* after limiting privileged flag to less Linux capabilities.

How did you do it?

How did you verify/test it?

container_hardening/test_container_hardening.py::test_bgp_dev PASSED                                                                            [100%]

Any platform specific information?

Supported testbed topology if it's a new test case?

Any topology

Documentation

@mssonicbld
Copy link
Collaborator

The pre-commit check detected issues in the files touched by this pull request.
The pre-commit check is a mandatory check, please fix detected issues.

Detailed pre-commit check results:
trim trailing whitespace.................................................Passed
fix end of files.........................................................Failed
- hook id: end-of-file-fixer
- exit code: 1
- files were modified by this hook

Fixing tests/container_hardening/test_container_hardening.py

check yaml...........................................(no files to check)Skipped
check for added large files..............................................Passed
check python ast.........................................................Passed
flake8...................................................................Passed
check conditional mark sort..........................(no files to check)Skipped

To run the pre-commit checks locally, you can follow below steps:

  1. Ensure that default python is python3. In sonic-mgmt docker container, default python is python2. You can run
    the check by activating the python3 virtual environment in sonic-mgmt docker container or outside of sonic-mgmt
    docker container.
  2. Ensure that the pre-commit package is installed:
sudo pip install pre-commit
  1. Go to repository root folder
  2. Install the pre-commit hooks:
pre-commit install
  1. Use pre-commit to check staged file:
pre-commit
  1. Alternatively, you can check committed files using:
pre-commit run --from-ref <commit_id> --to-ref <commit_id>

"""
Test bgp container has no access to /dev/vda*
"""
output = duthost.shell("docker exec bgp bash -c 'ls /dev | grep vda'", module_ignore_errors=True)['stdout']
Copy link
Contributor

@qiluo-msft qiluo-msft Jun 22, 2023

Choose a reason for hiding this comment

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

what is vda and why grep it? #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

/dev/vda in vs image is similar to /dev/sda

admin@vlab-03:~$ docker exec -it bgp bash -c 'ls /dev | grep vda'
admin@vlab-03:~$ docker exec -it telemetry bash -c 'ls /dev | grep vda'
vda
vda1
vda2
vda3

Copy link
Contributor

Choose a reason for hiding this comment

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

It's only working in KVM environments. In most DUT, you want to check /dev/sdaN.
You may find a flexible way to check the main disk device.

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 modified to a more general way to check the device, thanks!

@maipbui maipbui marked this pull request as ready for review June 23, 2023 20:07
"""
Test bgp container has no access to /dev/vda* or /dev/sda*
"""
device = duthost.shell("docker exec bgp bash -c 'df -h | grep /etc/hosts' | awk '{print $1}'")['stdout']
Copy link
Contributor

@qiluo-msft qiluo-msft Jun 26, 2023

Choose a reason for hiding this comment

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

device

Will you check shell return code, and device is a valid path? If not, there may be risk to be inject into another command. #Closed

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 checked return code and valid path for device

Signed-off-by: Mai Bui <[email protected]>
@maipbui maipbui requested a review from qiluo-msft June 29, 2023 19:35
qiluo-msft
qiluo-msft previously approved these changes Jun 29, 2023

def test_bgp_dev(duthost):
"""
Test bgp container has no access to /dev/vda* or /dev/sda*
Copy link

Choose a reason for hiding this comment

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

Is this test related to the --privileged flag removal or net=host?
I think we should mention this and also change the name of the test to accommodate it.
Also - in the future, I think we should make this test generic for any container that wants to test their solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Yarden-Z it's related to test --privileged flag, I modified the test to make it generic.

Copy link

Choose a reason for hiding this comment

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

It's a good improvement.
I think that in the future we can make this test even more generic.
i.e - maintain a list of containers that do not have the --privileged flag. (define this list in some global or maintain it in the section of container hardening)
Then - for each container (name) in this list, iterate on top of them and execute this test. That way - we won't need the if case and we'll be able to scale this test for each new container.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed your comment, thanks

Signed-off-by: Mai Bui <[email protected]>
@maipbui maipbui requested a review from Yarden-Z July 6, 2023 17:22
@maipbui maipbui merged commit 5da711e into sonic-net:master Jul 21, 2023
@maipbui maipbui deleted the bgp_dev branch July 21, 2023 13:28
AharonMalkin pushed a commit to AharonMalkin/sonic-mgmt that referenced this pull request Jan 25, 2024
Description of PR
HLD implementation: Container Hardening (sonic-net/SONiC#1364)
Dependency: sonic-net/sonic-buildimage#14932
#### What is the motivation for this PR?
Check bgp container has access to /dev/sda* or /dev/vda* after limiting privileged flag to less Linux capabilities.
#### How did you do it?
#### How did you verify/test it?
```
container_hardening/test_container_hardening.py::test_bgp_dev PASSED                                                                            [100%]
```
Signed-off-by: Mai Bui <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants