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

Update bash remediation to fix bug into account_disable_inactivity* #12134

Merged

Conversation

mrkanon
Copy link
Contributor

@mrkanon mrkanon commented Jul 5, 2024

Description:

Update bash remediation to fix bug into account_disable_inactivity_system_auth and account_disable_inactivity_password_auth.

Fix the way to delete and insert the existent lines.

Rationale:

The account_disable_inactivity_password_auth and account_disable_inactivity_system_auth bash remediation has a bug when ensure the correct order of lines into file. The script inserts the pam_lastlog.so line after each line from 0 to where it finds the pam_unix.so line

Note:

I believe this error can be resolved with a change to the bash_ensure_pam_module_line macro. That macro accepts a regular expression to ensure the expected line is inserted this regex match, but only if the expected line is not there, it doesn't reorder in case order was wrong.

The account_disable_inactivity_password_auth &
account_disable_inactivity_system_auth bash
remediation has a bug when ensure the order of
lines into file, if the value of pam_lastlog.so
is after pam_unix.so the remediation insert a
new line after each line.

Signed-off-by: Armando Acosta <[email protected]>
Copy link

openshift-ci bot commented Jul 5, 2024

Hi @mrkanon. Thanks for your PR.

I'm waiting for a ComplianceAsCode member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@openshift-ci openshift-ci bot added the needs-ok-to-test Used by openshift-ci bot. label Jul 5, 2024
Copy link

github-actions bot commented Jul 5, 2024

Start a new ephemeral environment with changes proposed in this pull request:

ol8 (from CTF) Environment (using Fedora as testing environment)
Open in Gitpod

Fedora Testing Environment
Open in Gitpod

Oracle Linux 8 Environment
Open in Gitpod

Copy link

github-actions bot commented Jul 5, 2024

This datastream diff is auto generated by the check Compare DS/Generate Diff

Click here to see the full diff
bash remediation for rule 'xccdf_org.ssgproject.content_rule_account_disable_inactivity_password_auth' differs.
--- xccdf_org.ssgproject.content_rule_account_disable_inactivity_password_auth
+++ xccdf_org.ssgproject.content_rule_account_disable_inactivity_password_auth
@@ -51,15 +51,23 @@
         # The control is updated only if one single line matches.
         sed -i -E --follow-symlinks 's/^(\s*auth\s+).*(\bpam_unix.so.*)/\1'"sufficient"' \2/' "$PAM_FILE_PATH"
     else
-        echo 'auth    '"sufficient"'    pam_unix.so' >> "$PAM_FILE_PATH"
+        LAST_MATCH_LINE=$(grep -nP "^\s*auth.*required.*pam_lastlog\.so.*" "$PAM_FILE_PATH" | tail -n 1 | cut -d: -f 1)
+        if [ ! -z $LAST_MATCH_LINE ]; then
+            sed -i --follow-symlinks $LAST_MATCH_LINE' a auth     '"sufficient"'    pam_unix.so' "$PAM_FILE_PATH"
+        else
+            echo 'auth    '"sufficient"'    pam_unix.so' >> "$PAM_FILE_PATH"
+        fi
     fi
 fi
 # Ensure pam_unix.so is configured after pam_lastlog.so
 if ! grep -Pz \
     "auth\s*required\s*pam_lastlog\.so[^#]*inactive=35[\s\S]*\n\s*auth\s*sufficient\s*pam_unix\.so"\
     "$PAM_FILE_PATH"  ; then
-    PAM_LASTLOG_LINE="$(grep -oP '^\s*auth.*pam_lastlog\.so.*' $PAM_FILE_PATH)"
-    sed -i "0,/^\s*auth.*pam_unix\.so.*/i$PAM_LASTLOG_LINE" "$PAM_FILE_PATH"
+    readarray -t pam_lastlog_lines <<< "$(grep -oP '^\s*auth.*pam_lastlog\.so[^#]*inactive=35.*' $PAM_FILE_PATH)"
+    sed -i "/^\s*auth.*pam_lastlog\.so[^#]*inactive=35.*/d" "$PAM_FILE_PATH"
+    for line in "${pam_lastlog_lines[@]}"; do
+        sed -i "/^\s*auth.*pam_unix\.so.*/i$line" "$PAM_FILE_PATH"
+    done
 fi
 if [ -f /usr/bin/authselect ]; then
     authselect apply-changes -b --backup=after-hardening-pam_lastlog.so-inactive.backup

bash remediation for rule 'xccdf_org.ssgproject.content_rule_account_disable_inactivity_system_auth' differs.
--- xccdf_org.ssgproject.content_rule_account_disable_inactivity_system_auth
+++ xccdf_org.ssgproject.content_rule_account_disable_inactivity_system_auth
@@ -51,15 +51,23 @@
         # The control is updated only if one single line matches.
         sed -i -E --follow-symlinks 's/^(\s*auth\s+).*(\bpam_unix.so.*)/\1'"sufficient"' \2/' "$PAM_FILE_PATH"
     else
-        echo 'auth    '"sufficient"'    pam_unix.so' >> "$PAM_FILE_PATH"
+        LAST_MATCH_LINE=$(grep -nP "^\s*auth.*required.*pam_lastlog\.so.*" "$PAM_FILE_PATH" | tail -n 1 | cut -d: -f 1)
+        if [ ! -z $LAST_MATCH_LINE ]; then
+            sed -i --follow-symlinks $LAST_MATCH_LINE' a auth     '"sufficient"'    pam_unix.so' "$PAM_FILE_PATH"
+        else
+            echo 'auth    '"sufficient"'    pam_unix.so' >> "$PAM_FILE_PATH"
+        fi
     fi
 fi
 # Ensure pam_unix.so is configured after pam_lastlog.so
 if ! grep -Pz \
     "auth\s*required\s*pam_lastlog\.so[^#]*inactive=35[\s\S]*\n\s*auth\s*sufficient\s*pam_unix\.so"\
     "$PAM_FILE_PATH"  ; then
-    PAM_LASTLOG_LINE="$(grep -oP '^\s*auth.*pam_lastlog\.so.*' $PAM_FILE_PATH)"
-    sed -i "0,/^\s*auth.*pam_unix\.so.*/i$PAM_LASTLOG_LINE" "$PAM_FILE_PATH"
+    readarray -t pam_lastlog_lines <<< "$(grep -oP '^\s*auth.*pam_lastlog\.so[^#]*inactive=35.*' $PAM_FILE_PATH)"
+    sed -i "/^\s*auth.*pam_lastlog\.so[^#]*inactive=35.*/d" "$PAM_FILE_PATH"
+    for line in "${pam_lastlog_lines[@]}"; do
+        sed -i "/^\s*auth.*pam_unix\.so.*/i$line" "$PAM_FILE_PATH"
+    done
 fi
 if [ -f /usr/bin/authselect ]; then
     authselect apply-changes -b --backup=after-hardening-pam_lastlog.so-inactive.backup

Copy link

github-actions bot commented Jul 5, 2024

🤖 A k8s content image for this PR is available at:
ghcr.io/complianceascode/k8scontent:12134
This image was built from commit: b2cc9e6

Click here to see how to deploy it

If you alread have Compliance Operator deployed:
utils/build_ds_container.py -i ghcr.io/complianceascode/k8scontent:12134

Otherwise deploy the content and operator together by checking out ComplianceAsCode/compliance-operator and:
CONTENT_IMAGE=ghcr.io/complianceascode/k8scontent:12134 make deploy-local

@marcusburghardt marcusburghardt added Bash Bash remediation update. Oracle Linux Oracle Linux product related. labels Jul 8, 2024
@jan-cerny jan-cerny self-assigned this Jul 29, 2024
Copy link
Collaborator

@jan-cerny jan-cerny left a comment

Choose a reason for hiding this comment

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

Can you add a test scenario covering this situation?

# Ensure pam_unix.so is configured after pam_lastlog.so
if ! grep -Pz \
"auth\s*required\s*pam_lastlog\.so[^#]*inactive=35[\s\S]*\n\s*auth\s*sufficient\s*pam_unix\.so"\
"$PAM_FILE_PATH" ; then
PAM_LASTLOG_LINE="$(grep -oP '^\s*auth.*pam_lastlog\.so.*' $PAM_FILE_PATH)"
sed -i "0,/^\s*auth.*pam_unix\.so.*/i$PAM_LASTLOG_LINE" "$PAM_FILE_PATH"
readarray -t pam_lastlog_lines <<< $(grep -oP '^\s*auth.*pam_lastlog\.so[^#]*inactive=35.*' $PAM_FILE_PATH)
Copy link
Collaborator

Choose a reason for hiding this comment

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

the shellcheck warns about this, see the CI results

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The scenario is the test wrong_order.fail.sh, in that test we have as initial file

# Generated by authselect on Tue Jul 30 19:05:11 2024
# Do not modify this file manually.

auth        required                                     pam_env.so
auth        required                                     pam_faildelay.so delay=2000000
auth        sufficient                                   pam_unix.so nullok
auth required pam_lastlog.so inactive=35
auth        required                                     pam_deny.so

account     required                                     pam_unix.so

password    requisite                                    pam_pwquality.so
password    sufficient                                   pam_unix.so sha512 shadow nullok use_authtok
password    required                                     pam_deny.so

session     optional                                     pam_keyinit.so revoke
session     required                                     pam_limits.so
-session    optional                                     pam_systemd.so
session     [success=1 default=ignore]                   pam_succeed_if.so service in crond quiet use_uid
session     required                                     pam_unix.so

and after the bash remediation we have

# Generated by authselect on Tue Jul 30 19:09:20 2024
# Do not modify this file manually.

auth required pam_lastlog.so inactive=35
auth        required                                     pam_env.so
auth required pam_lastlog.so inactive=35
auth        required                                     pam_faildelay.so delay=2000000
auth required pam_lastlog.so inactive=35
auth required pam_lastlog.so inactive=35
auth        sufficient                                   pam_unix.so nullok
auth required pam_lastlog.so inactive=35
auth        required                                     pam_deny.so

account     required                                     pam_unix.so

password    requisite                                    pam_pwquality.so
password    sufficient                                   pam_unix.so sha512 shadow nullok use_authtok
password    required                                     pam_deny.so

The automatus tool mark that test as correct because the condition pass but not as expected.

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK makes sense

So just please fix the shellcheck issues

@jan-cerny jan-cerny added this to the 0.1.75 milestone Aug 1, 2024
Fix missing quote in
account_disable_inactivity_system_auth &
account_disable_inactivity_password_auth
bash remediation

Signed-off-by: Armando Acosta <[email protected]>
Copy link

codeclimate bot commented Aug 1, 2024

Code Climate has analyzed commit b2cc9e6 and detected 0 issues on this pull request.

The test coverage on the diff in this pull request is 100.0% (50% is the threshold).

This pull request will bring the total coverage in the repository to 59.4% (0.0% change).

View more on Code Climate.

Copy link
Collaborator

@jan-cerny jan-cerny left a comment

Choose a reason for hiding this comment

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

I have add these rules to RHEL 9 default profile and I have added the bash remediations to RHEL. Then, I have executed the test scenarios.

jcerny@fedora:~/work/git/scap-security-guide (pr/12134)$ python3 tests/automatus.py rule --libvirt qemu:///system ssgts_rhel9 account_disable_inactivity_password_auth account_disable_inactivity_system_auth
Setting console output to log level INFO
INFO - The base image option has not been specified, choosing libvirt-based test environment.
INFO - Logging into /home/jcerny/work/git/scap-security-guide/logs/rule-custom-2024-08-02-0814/test_suite.log
INFO - xccdf_org.ssgproject.content_rule_account_disable_inactivity_password_auth
INFO - Script correct_value.pass.sh using profile (all) OK
INFO - Script line_not_there.fail.sh using profile (all) OK
INFO - Script stricter_value.pass.sh using profile (all) OK
INFO - Script wrong_order.fail.sh using profile (all) OK
INFO - Script wrong_value.fail.sh using profile (all) OK
INFO - xccdf_org.ssgproject.content_rule_account_disable_inactivity_system_auth
INFO - Script correct_value.pass.sh using profile (all) OK
INFO - Script line_not_there.fail.sh using profile (all) OK
INFO - Script stricter_value.pass.sh using profile (all) OK
INFO - Script wrong_order.fail.sh using profile (all) OK
INFO - Script wrong_value.fail.sh using profile (all) OK

@jan-cerny jan-cerny merged commit 6d06200 into ComplianceAsCode:master Aug 2, 2024
84 of 89 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bash Bash remediation update. needs-ok-to-test Used by openshift-ci bot. Oracle Linux Oracle Linux product related.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants