Skip to content

Commit 9697ece

Browse files
authored
Merge pull request #14443 from mpurg/ubuntu_cis_log_perms
Align /var/log ownership rules with Ubuntu CIS
2 parents 7965ebd + 7d9f4fd commit 9697ece

18 files changed

Lines changed: 144 additions & 98 deletions

linux_os/guide/system/permissions/files/permissions_var_log_dir/file_groupownerships_var_log/bash/shared.sh

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77
# see https://workbench.cisecurity.org/benchmarks/18959/tickets/23964
88
# regarding sssd and gdm exclusions
99

10+
declare -A valid_shells
11+
while read -r line; do
12+
[[ "$line" == /* ]] && valid_shells["$line"]=1
13+
done < /etc/shells
14+
15+
declare -A users_with_valid_shells
16+
while IFS=: read -r user _ _ _ _ _ shell; do
17+
if [[ ${valid_shells["$shell"]} == 1 ]]; then
18+
users_with_valid_shells["$user"]=1
19+
fi
20+
done < /etc/passwd
21+
1022
find -P /var/log/ -type f -regextype posix-extended \
1123
! -group root ! -group adm \
1224
! -name 'gdm' ! -name 'gdm3' \
@@ -26,4 +38,11 @@ find -P /var/log/ -type f -regextype posix-extended \
2638
! -regex '.*/localmessages(.*)' \
2739
! -regex '.*/secure(.*)' \
2840
! -regex '.*/waagent.log(.*)' \
29-
-regex '.*' -exec chgrp --no-dereference root {} \;
41+
-print0 | while IFS= read -r -d '' log_file
42+
do
43+
# Set to root if owned by a user with a valid shell
44+
user=$(stat -c "%U" "$log_file")
45+
if [[ "${users_with_valid_shells["$user"]}" == "1" ]]; then
46+
chgrp --no-dereference root "$log_file"
47+
fi
48+
done

linux_os/guide/system/permissions/files/permissions_var_log_dir/file_groupownerships_var_log/oval/shared.xml

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
<unix:object object_ref="object_group_ownership_var_log" />
2222
<unix:state state_ref="state_group_ownership_adm_var_log_auth_log"/>
2323
<unix:state state_ref="state_group_ownership_root_var_log_auth_log"/>
24-
{{%- if product == "ubuntu2204" %}}
25-
<unix:state state_ref="{{{ rule_id }}}_group_only_has_sys_uids"/>
24+
{{%- if 'ubuntu' in product %}}
25+
<unix:state state_ref="{{{ rule_id }}}_state_owner_is_system_user"/>
2626
{{%- endif %}}
2727
</unix:file_test>
2828
<unix:file_object comment="/var/log/*" id="object_group_ownership_var_log" version="1">
@@ -96,47 +96,43 @@
9696
<unix:file_state id="{{{ rule_id }}}_exclude_files_waagent" version="1">
9797
<unix:filename operation="pattern match">^waagent\.log.*$</unix:filename>
9898
</unix:file_state>
99-
{{%- if product == "ubuntu2204" %}}
100-
<unix:file_state id="{{{ rule_id }}}_group_only_has_sys_uids" version="1">
101-
<unix:group_id datatype="int" var_ref="empty_group_ids" var_check="at least one"/>
99+
{{%- if 'ubuntu' in product %}}
100+
<unix:file_state id="{{{ rule_id }}}_state_owner_is_system_user" version="1">
101+
<unix:group_id datatype="int" operation="equals" var_ref="{{{ rule_id }}}_var_system_gids" var_check="at least one" />
102102
</unix:file_state>
103103

104-
<local_variable id="empty_group_ids" comment="Group IDs with no members" datatype="int" version="1">
105-
<object_component item_field="subexpression" object_ref="empty_members_in_etc_group"/>
106-
</local_variable>
107-
108-
<ind:textfilecontent54_object comment="Groups with no members" id="empty_members_in_etc_group" version="1">
109-
<ind:filepath>/etc/group</ind:filepath>
110-
<ind:pattern operation="pattern match" var_ref="variable_{{{ rule_id }}}_group_regex" var_check="at least one"/>
104+
<!-- Fetch all shells designated as valid login shells from /etc/shells -->
105+
<ind:textfilecontent54_object id="{{{ rule_id }}}_object_valid_shells" version="1" comment="valid shells">
106+
<ind:filepath>/etc/shells</ind:filepath>
107+
<ind:pattern operation="pattern match">^(/.*)$</ind:pattern>
111108
<ind:instance datatype="int" operation="greater than or equal">1</ind:instance>
112109
</ind:textfilecontent54_object>
113110

114-
<local_variable id="variable_{{{ rule_id }}}_group_regex" datatype="string" version="1" comment="gid rows retrieved from /etc/passwd">
115-
<concat>
116-
<literal_component>^[^:]+:[^:]*:(</literal_component>
117-
<object_component item_field="subexpression" object_ref="obj_{{{ rule_id }}}_gids_with_only_sys_uids" />
118-
<literal_component>):$</literal_component>
119-
</concat>
111+
<local_variable id="{{{ rule_id }}}_var_valid_shells" comment="list of valid shells" datatype="string" version="1">
112+
<object_component item_field="subexpression" object_ref="{{{ rule_id }}}_object_valid_shells" />
120113
</local_variable>
121-
122-
<ind:textfilecontent54_object id="obj_{{{ rule_id }}}_gids_with_only_sys_uids" version="1">
114+
115+
<ind:textfilecontent54_object id="{{{ rule_id }}}_object_system_users" version="1" comment="users who have an invalid shell">
123116
<ind:filepath>/etc/passwd</ind:filepath>
124-
<ind:pattern operation="pattern match" var_ref="variable_{{{ rule_id }}}_regex" var_check="at least one"/>
117+
<ind:pattern operation="pattern match">^[^:]+:[^:]+:[0-9]+:([0-9]+):.*$</ind:pattern>
125118
<ind:instance datatype="int" operation="greater than or equal">1</ind:instance>
119+
<filter action="exclude">{{{ rule_id }}}_state_shell_is_valid</filter>
126120
</ind:textfilecontent54_object>
127121

128-
<local_variable id="variable_{{{ rule_id }}}_regex" datatype="string" version="1" comment="uid rows retrieved from /etc/passwd">
122+
<ind:textfilecontent54_state id="{{{ rule_id }}}_state_shell_is_valid" version="1">
123+
<ind:text operation="pattern match" var_ref="{{{ rule_id }}}_var_valid_shells_regex" var_check="at least one"></ind:text>
124+
</ind:textfilecontent54_state>
125+
126+
<local_variable id="{{{ rule_id }}}_var_valid_shells_regex" datatype="string" version="1" comment="regex of valid shells">
129127
<concat>
130-
<literal_component>^[^:]*:[^:]*:</literal_component>
131-
<object_component item_field="subexpression" object_ref="obj_{{{ rule_id }}}_sys_uid" />
132-
<literal_component>:(\d+):.*$</literal_component>
128+
<literal_component>^.*:(</literal_component>
129+
<object_component item_field="subexpression" object_ref="{{{ rule_id }}}_object_valid_shells" />
130+
<literal_component>)$</literal_component>
133131
</concat>
134132
</local_variable>
135133

136-
<ind:textfilecontent54_object id="obj_{{{ rule_id }}}_sys_uid" version="1">
137-
<ind:filepath>/etc/passwd</ind:filepath>
138-
<ind:pattern operation="pattern match">^[^:]+:[^:]*:(\d\d?\d?):.*$</ind:pattern>
139-
<ind:instance datatype="int" operation="greater than or equal">1</ind:instance>
140-
</ind:textfilecontent54_object>
134+
<local_variable id="{{{ rule_id }}}_var_system_gids" comment="GIDs of users with invalid shells" datatype="int" version="1">
135+
<object_component item_field="subexpression" object_ref="{{{ rule_id }}}_object_system_users" />
136+
</local_variable>
141137
{{%- endif %}}
142138
</def-group>

linux_os/guide/system/permissions/files/permissions_var_log_dir/file_groupownerships_var_log/tests/excluded_files.pass.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/bin/bash
22
# platform = multi_platform_ubuntu
3-
# packages = rsyslog
43

54
chgrp root -R /var/log/*
65

linux_os/guide/system/permissions/files/permissions_var_log_dir/file_groupownerships_var_log/tests/owned_by_adm.pass.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/bin/bash
22
# platform = multi_platform_ubuntu
3-
# packages = rsyslog
43

54
chgrp root -R /var/log/*
65

linux_os/guide/system/permissions/files/permissions_var_log_dir/file_groupownerships_var_log/tests/owned_by_nobody.fail.sh

Lines changed: 0 additions & 13 deletions
This file was deleted.

linux_os/guide/system/permissions/files/permissions_var_log_dir/file_groupownerships_var_log/tests/owned_by_non_sys_acc_grp.fail.sh

Lines changed: 0 additions & 12 deletions
This file was deleted.

linux_os/guide/system/permissions/files/permissions_var_log_dir/file_groupownerships_var_log/tests/owned_by_root.pass.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/bin/bash
22
# platform = multi_platform_ubuntu
3-
# packages = rsyslog
43

54
chgrp root -R /var/log/*
65

linux_os/guide/system/permissions/files/permissions_var_log_dir/file_groupownerships_var_log/tests/owned_by_sys_acc_grp.pass.sh

Lines changed: 0 additions & 12 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
# platform = multi_platform_ubuntu
3+
4+
useradd -m -s /bin/bash test_user_with_shell
5+
6+
chown root:root -R /var/log/*
7+
8+
touch /var/log/test_log_file
9+
chgrp test_user_with_shell /var/log/test_log_file
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
# platform = multi_platform_ubuntu
3+
4+
useradd -m -s /usr/sbin/nologin test_user_no_shell
5+
6+
chown root:root -R /var/log/*
7+
8+
touch /var/log/test_log_file
9+
chgrp test_user_no_shell /var/log/test_log_file

0 commit comments

Comments
 (0)