Skip to content

File permissions library dirs shall act on files when 'file_regex' is set#8420

Merged
vojtapolasek merged 3 commits intoComplianceAsCode:stabilization-v0.1.61from
yuumasato:file_permissions_library_dirs_shall_act_on_files
Mar 25, 2022
Merged

File permissions library dirs shall act on files when 'file_regex' is set#8420
vojtapolasek merged 3 commits intoComplianceAsCode:stabilization-v0.1.61from
yuumasato:file_permissions_library_dirs_shall_act_on_files

Conversation

@yuumasato
Copy link
Copy Markdown
Member

Description:

  • Change the remediation of templates file_owner, file_groupowner and file_permission to act only on regular files when file_regex is set,
    • Following the symlinks under /lib, lib64, /usr/lib and /usr/lib64 and making changes can have catastrophic consequences.

Rationale:

'/lib/.build-id/a4/67cb9c8fa7306d41b96a820b0178f3a9c66055' -> '../../../../usr/bin/passwd'
'/lib/.build-id/a4/8e8ae0d029dbbc1c1b0bb0fcea424860a6c412' -> '../../../../usr/bin/sudo'
'/lib/.build-id/a4/2c53d4543f5c0bb8db47d65e4b766d12f3b7bd' -> '../../../../usr/lib64/python3.9/lib-dynload/_lsprof.cpython-39-x86_64-linux-gnu.so'

This became visible when sudo command stopped working after remediation was applied:

[admin@localhost ~]# ls -lh /usr/bin/sudo
-rwxr-xr-x. 1 root root 182K Aug 26  2021 /usr/bin/sudo
....
[admin@localhost ~]# sudo ls
sudo: /usr/bin/sudo must be owned by uid 0 and have the setuid bit set

The remediations should remediate regular files.
No symlinks or the files they are pointing to should be changed.

There are symlinks in `/lib/.buid-id/' that point to installed binaries.
For example (the IDs will vary):
'/lib/.build-id/a4/67cb9c8fa7306d41b96a820b0178f3a9c66055' ->
'../../../../usr/bin/passwd'
'/lib/.build-id/a4/8e8ae0d029dbbc1c1b0bb0fcea424860a6c412' ->
'../../../../usr/bin/sudo'
'/lib/.build-id/a4/2c53d4543f5c0bb8db47d65e4b766d12f3b7bd' ->
'../../../../usr/lib64/python3.9/lib-dynload/_lsprof.cpython-39-x86_64-linux-gnu.so'
@yuumasato yuumasato added the bugfix Fixes to reported bugs. label Mar 24, 2022
@yuumasato yuumasato added this to the 0.1.61 milestone Mar 24, 2022
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 24, 2022

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

Open in Gitpod

@yuumasato
Copy link
Copy Markdown
Member Author

@dodys This reverts the chmod -h change, and changes the find command to return only regular files.

@yuumasato yuumasato requested a review from ggbecker March 24, 2022 18:44
@github-actions
Copy link
Copy Markdown

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_file_groupownership_audit_configuration' differs:
--- old datastream
+++ new datastream
@@ -1,18 +1,18 @@
 # Remediation is applicable only in certain platforms
 if rpm --quiet -q audit; then
 
-readarray -t files < <(find /etc/audit/ -maxdepth 1 ! -gid 0)
+readarray -t files < <(find /etc/audit/ -maxdepth 1 -type f ! -gid 0)
 for file in "${files[@]}"; do
- if basename $file | grep -qE '^audit(\.rules|d\.conf)$'; then
- chgrp -h 0 "$file"
+ if basename "$file" | grep -qE '^audit(\.rules|d\.conf)$'; then
+ chgrp 0 "$file"
 fi
 done
 
 
-readarray -t files < <(find /etc/audit/rules.d/ -maxdepth 1 ! -gid 0)
+readarray -t files < <(find /etc/audit/rules.d/ -maxdepth 1 -type f ! -gid 0)
 for file in "${files[@]}"; do
- if basename $file | grep -qE '^.*\.rules$'; then
- chgrp -h 0 "$file"
+ if basename "$file" | grep -qE '^.*\.rules$'; then
+ chgrp 0 "$file"
 fi
 done
 

bash remediation for rule 'xccdf_org.ssgproject.content_rule_file_ownership_audit_configuration' differs:
--- old datastream
+++ new datastream
@@ -1,17 +1,17 @@
 # Remediation is applicable only in certain platforms
 if rpm --quiet -q audit; then
 
-readarray -t files < <(find /etc/audit/ -maxdepth 1 ! -uid 0)
+readarray -t files < <(find /etc/audit/ -maxdepth 1 -type f ! -uid 0)
 for file in "${files[@]}"; do
- if basename $file | grep -qE '^audit(\.rules|d\.conf)$'; then
- chown -h 0 "$file"
+ if basename "$file" | grep -qE '^audit(\.rules|d\.conf)$'; then
+ chown 0 "$file"
 fi
 done
 
-readarray -t files < <(find /etc/audit/rules.d/ -maxdepth 1 ! -uid 0)
+readarray -t files < <(find /etc/audit/rules.d/ -maxdepth 1 -type f ! -uid 0)
 for file in "${files[@]}"; do
- if basename $file | grep -qE '^.*\.rules$'; then
- chown -h 0 "$file"
+ if basename "$file" | grep -qE '^.*\.rules$'; then
+ chown 0 "$file"
 fi
 done
 

bash remediation for rule 'xccdf_org.ssgproject.content_rule_file_permissions_etc_audit_rulesd' differs:
--- old datastream
+++ new datastream
@@ -1,9 +1,9 @@
 
 
 
-readarray -t files < <(find /etc/audit/rules.d/ -maxdepth 1)
+readarray -t files < <(find /etc/audit/rules.d/ -maxdepth 1 -type f)
 for file in "${files[@]}"; do
- if basename $file | grep -qE '^.*rules$'; then
+ if basename "$file" | grep -qE '^.*rules$'; then
 chmod 0640 "$file"
 fi 
 done

bash remediation for rule 'xccdf_org.ssgproject.content_rule_file_ownership_library_dirs' differs:
--- old datastream
+++ new datastream
@@ -1,30 +1,30 @@
 
 
 
-readarray -t files < <(find /lib/ ! -uid 0)
+readarray -t files < <(find /lib/ -type f ! -uid 0)
 for file in "${files[@]}"; do
- if basename $file | grep -qE '^.*$'; then
- chown -h 0 "$file"
+ if basename "$file" | grep -qE '^.*$'; then
+ chown 0 "$file"
 fi
 done
 
-readarray -t files < <(find /lib64/ ! -uid 0)
+readarray -t files < <(find /lib64/ -type f ! -uid 0)
 for file in "${files[@]}"; do
- if basename $file | grep -qE '^.*$'; then
- chown -h 0 "$file"
+ if basename "$file" | grep -qE '^.*$'; then
+ chown 0 "$file"
 fi
 done
 
-readarray -t files < <(find /usr/lib/ ! -uid 0)
+readarray -t files < <(find /usr/lib/ -type f ! -uid 0)
 for file in "${files[@]}"; do
- if basename $file | grep -qE '^.*$'; then
- chown -h 0 "$file"
+ if basename "$file" | grep -qE '^.*$'; then
+ chown 0 "$file"
 fi
 done
 
-readarray -t files < <(find /usr/lib64/ ! -uid 0)
+readarray -t files < <(find /usr/lib64/ -type f ! -uid 0)
 for file in "${files[@]}"; do
- if basename $file | grep -qE '^.*$'; then
- chown -h 0 "$file"
+ if basename "$file" | grep -qE '^.*$'; then
+ chown 0 "$file"
 fi
 done

bash remediation for rule 'xccdf_org.ssgproject.content_rule_file_permissions_library_dirs' differs:
--- old datastream
+++ new datastream
@@ -1,30 +1,30 @@
 
 
 
-readarray -t files < <(find /lib/ )
+readarray -t files < <(find /lib/ -type f)
 for file in "${files[@]}"; do
- if basename $file | grep -qE '^.*$'; then
+ if basename "$file" | grep -qE '^.*$'; then
 chmod 0755 "$file"
 fi 
 done
 
-readarray -t files < <(find /lib64/ )
+readarray -t files < <(find /lib64/ -type f)
 for file in "${files[@]}"; do
- if basename $file | grep -qE '^.*$'; then
+ if basename "$file" | grep -qE '^.*$'; then
 chmod 0755 "$file"
 fi 
 done
 
-readarray -t files < <(find /usr/lib/ )
+readarray -t files < <(find /usr/lib/ -type f)
 for file in "${files[@]}"; do
- if basename $file | grep -qE '^.*$'; then
+ if basename "$file" | grep -qE '^.*$'; then
 chmod 0755 "$file"
 fi 
 done
 
-readarray -t files < <(find /usr/lib64/ )
+readarray -t files < <(find /usr/lib64/ -type f)
 for file in "${files[@]}"; do
- if basename $file | grep -qE '^.*$'; then
+ if basename "$file" | grep -qE '^.*$'; then
 chmod 0755 "$file"
 fi 
 done

bash remediation for rule 'xccdf_org.ssgproject.content_rule_root_permissions_syslibrary_files' differs:
--- old datastream
+++ new datastream
@@ -1,33 +1,33 @@
 
 
 
-readarray -t files < <(find /lib/ -maxdepth 1 ! -gid 0)
+readarray -t files < <(find /lib/ -maxdepth 1 -type f ! -gid 0)
 for file in "${files[@]}"; do
- if basename $file | grep -qE '^.*$'; then
- chgrp -h 0 "$file"
+ if basename "$file" | grep -qE '^.*$'; then
+ chgrp 0 "$file"
 fi
 done
 
 
-readarray -t files < <(find /lib64/ -maxdepth 1 ! -gid 0)
+readarray -t files < <(find /lib64/ -maxdepth 1 -type f ! -gid 0)
 for file in "${files[@]}"; do
- if basename $file | grep -qE '^.*$'; then
- chgrp -h 0 "$file"
+ if basename "$file" | grep -qE '^.*$'; then
+ chgrp 0 "$file"
 fi
 done
 
 
-readarray -t files < <(find /usr/lib/ -maxdepth 1 ! -gid 0)
+readarray -t files < <(find /usr/lib/ -maxdepth 1 -type f ! -gid 0)
 for file in "${files[@]}"; do
- if basename $file | grep -qE '^.*$'; then
- chgrp -h 0 "$file"
+ if basename "$file" | grep -qE '^.*$'; then
+ chgrp 0 "$file"
 fi
 done
 
 
-readarray -t files < <(find /usr/lib64/ -maxdepth 1 ! -gid 0)
+readarray -t files < <(find /usr/lib64/ -maxdepth 1 -type f ! -gid 0)
 for file in "${files[@]}"; do
- if basename $file | grep -qE '^.*$'; then
- chgrp -h 0 "$file"
+ if basename "$file" | grep -qE '^.*$'; then
+ chgrp 0 "$file"
 fi
 done

bash remediation for rule 'xccdf_org.ssgproject.content_rule_file_permissions_httpd_server_conf_d_files' differs:
--- old datastream
+++ new datastream
@@ -1,9 +1,9 @@
 
 
 
-readarray -t files < <(find /etc/httpd/conf.d/ -maxdepth 1)
+readarray -t files < <(find /etc/httpd/conf.d/ -maxdepth 1 -type f)
 for file in "${files[@]}"; do
- if basename $file | grep -qE '^.*$'; then
+ if basename "$file" | grep -qE '^.*$'; then
 chmod 0640 "$file"
 fi 
 done

bash remediation for rule 'xccdf_org.ssgproject.content_rule_file_permissions_httpd_server_conf_files' differs:
--- old datastream
+++ new datastream
@@ -1,9 +1,9 @@
 
 
 
-readarray -t files < <(find /etc/httpd/conf/ -maxdepth 1)
+readarray -t files < <(find /etc/httpd/conf/ -maxdepth 1 -type f)
 for file in "${files[@]}"; do
- if basename $file | grep -qE '^.*$'; then
+ if basename "$file" | grep -qE '^.*$'; then
 chmod 0640 "$file"
 fi 
 done

bash remediation for rule 'xccdf_org.ssgproject.content_rule_file_permissions_sshd_private_key' differs:
--- old datastream
+++ new datastream
@@ -1,9 +1,9 @@
 # Remediation is applicable only in certain platforms
 if [ ! -f /.dockerenv ] && [ ! -f /run/.containerenv ]; then
 
-readarray -t files < <(find /etc/ssh/ -maxdepth 1)
+readarray -t files < <(find /etc/ssh/ -maxdepth 1 -type f)
 for file in "${files[@]}"; do
- if basename $file | grep -qE '^.*_key$'; then
+ if basename "$file" | grep -qE '^.*_key$'; then
 chmod 0600 "$file"
 fi 
 done

bash remediation for rule 'xccdf_org.ssgproject.content_rule_file_permissions_sshd_pub_key' differs:
--- old datastream
+++ new datastream
@@ -1,9 +1,9 @@
 # Remediation is applicable only in certain platforms
 if [ ! -f /.dockerenv ] && [ ! -f /run/.containerenv ]; then
 
-readarray -t files < <(find /etc/ssh/ -maxdepth 1)
+readarray -t files < <(find /etc/ssh/ -maxdepth 1 -type f)
 for file in "${files[@]}"; do
- if basename $file | grep -qE '^.*.pub$'; then
+ if basename "$file" | grep -qE '^.*.pub$'; then
 chmod 0644 "$file"
 fi 
 done

@Mab879
Copy link
Copy Markdown
Member

Mab879 commented Mar 24, 2022

Looks like another push is needed to get GH Actions to run.

This test sets expectation on behavior of the rule.
Symlinks are ignored, even when they have incompliant owner, and point
to nowhere.
@yuumasato yuumasato force-pushed the file_permissions_library_dirs_shall_act_on_files branch from dff39d1 to c5feb75 Compare March 25, 2022 08:21
@vojtapolasek vojtapolasek self-assigned this Mar 25, 2022
Copy link
Copy Markdown
Collaborator

@vojtapolasek vojtapolasek left a comment

Choose a reason for hiding this comment

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

LGTM, thank you for quick fix

@vojtapolasek vojtapolasek merged commit a8bb925 into ComplianceAsCode:stabilization-v0.1.61 Mar 25, 2022
@yuumasato yuumasato deleted the file_permissions_library_dirs_shall_act_on_files branch March 25, 2022 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Fixes to reported bugs.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants