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

security: use quote with command, shell and validate with variable #245

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

maage
Copy link
Contributor

@maage maage commented Aug 5, 2023

Enhancement:

Use quote always when using command plugin, shell plugin, validate content as command to execute or system to configure.

Reject bad content for systemd.unit and sysctl config.

Reason:

This improves security and robustness. At least TMPDIR can be defined outside in various ways.

This follows example in tests/tests_all_options.yml where pkg_mgr is quoted.

When using command plugin, it could be possible to use argv functionality and avoid quoting.

Initial reason was single quotes in '{{ sshd_test_hostkey.path }}/rsa_key' in tasks/install.yml but not in tasks/install_config.yml and tasks/install_namespace.yml.

Result:

Included test passes. Care should be taken if test fails. It might leave BADFLAG user or related files behind.

Issue Tracker Tickets (Jira or BZ if any): -

@maage
Copy link
Contributor Author

maage commented Aug 7, 2023

Changed test to use flagfile instead of user.
Also fixed assert fail_msg -> msg to support older ansible versions.

This avoids issues if file names are not safepaths.
Skip quotation only if variable is checked.

Add test suit to excercise some quote use cases.
Ensure systemd.unit contents is robust. This disables possibility to
have something that needs to be quoted there. But as ansible lacks
proper way to quote systemd unit files (see man systemd.syntax, rules
are not shell rules), it is better to fail such configs. If you are
trying to do that, you are doing it wrong anyway or have malicious
intent.

Also ensure similar issue with sysctl.conf.

Issue can be seen with `tests_hostkeys_unsafe_path.yml`, when adding
following to role params:

       sshd_install_service: true
       sshd_config_file: "{{ ansible_facts.env.TMPDIR }}/sshd.d/foo.conf"
       sshd_binary: "{{ ansible_facts.env.TMPDIR }}/sshd"
       __sshd_runtime_directory: "{{ ansible_facts.env.TMPDIR }}/run"
@maage
Copy link
Contributor Author

maage commented Aug 7, 2023

Decided to split test backup / restore from main commit and some commit message fixes.

Also changed test as testing with sshd_config_file caused problems with other distros than Fedora and CentOS 9.

Copy link
Collaborator

@Jakuje Jakuje left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution! Looks good. I just reran the centos CI.

@Jakuje
Copy link
Collaborator

Jakuje commented Aug 29, 2023

The centos 8 fails reproducibly in your new test for some reason. Can you have a look for the reason? I see it fails on SyntaxError, but I did not dig deep enough to figure out what is the issue there (something on ansible side missing some sanitization?)

@spetrosi
Copy link
Contributor

Please change the title to fix: use quote with command, shell and validate with variable #245, pr type security is not supported.
Just to note, the supported types are listed in https://github.com/willshersystems/ansible-sshd/blob/master/.commitlintrc.js#L21
And rebase on master, this should fix ansible-lint

@richm
Copy link
Collaborator

richm commented Jan 25, 2024

ping - can this be rebased?

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.

None yet

4 participants