Skip to content

Commit 5d8da16

Browse files
In a container, sshd should not run as root (#152)
* In a container, ssh should not run as root Add the option to choose the owner of configurations files and their locations. Signed-off-by: Michée Lengronne <[email protected]> * custom path and user added Signed-off-by: Michée Lengronne <[email protected]> * correct ssh custom parameters names Signed-off-by: Michée Lengronne <[email protected]> * variable interpolation Signed-off-by: Michée Lengronne <[email protected]> * ssh privilege separation for a non root user It should be 'no' Signed-off-by: Michée Lengronne <[email protected]> * syntax correction Signed-off-by: Michée Lengronne <[email protected]> * EnforcedStyle: assign_to_condition Will test this rubocop one. Maybe it is the other. Signed-off-by: Michée Lengronne <[email protected]> * variables names fixed Signed-off-by: Michée Lengronne <[email protected]>
1 parent d1affa2 commit 5d8da16

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

controls/ssh_spec.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,19 @@
2222
command('ssh').exist?
2323
end
2424

25+
ssh_custom_user = attribute('ssh_custom_user', value: 'root', description: 'The SSH user is not always root. It must be an unprivileged user in a container')
26+
ssh_custom_path = attribute('ssh_custom_path', value: '/etc/ssh', description: 'Sometimes ssh configuration files are present in another location and ssh use them with the -f flag')
27+
2528
control 'ssh-01' do
2629
impact 1.0
2730
title 'client: Check ssh_config owner, group and permissions.'
28-
desc 'The ssh_config should owned by root, only be writable by owner and readable to all.'
31+
desc 'The ssh_config should owned by root or a specified user, only be writable by owner and readable to all.'
2932

30-
describe file('/etc/ssh/ssh_config') do
33+
describe file(ssh_custom_path + '/ssh_config') do
3134
it { should exist }
3235
it { should be_file }
33-
it { should be_owned_by 'root' }
34-
it { should be_grouped_into os.darwin? ? 'wheel' : 'root' }
36+
it { should be_owned_by ssh_custom_user }
37+
it { should be_grouped_into os.darwin? ? 'wheel' : ssh_custom_user }
3538
it { should_not be_executable }
3639
it { should be_readable.by('owner') }
3740
it { should be_readable.by('group') }

controls/sshd_spec.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@
2828
sshd_x11forwarding = attribute('sshd_x11forwarding', value: 'no', description: 'Expected value for sshd_config X11Forwarding')
2929
sshd_banner = attribute('sshd_banner', value: 'none', description: 'Expected value for sshd_config Banner')
3030
sshd_max_auth_tries = attribute('sshd_max_auth_tries', value: 2, description: 'Expected value for max_auth_retries')
31+
sshd_custom_user = attribute('sshd_custom_user', value: 'root', description: 'The SSH user is not always root. It must be an unprivileged user in a container')
32+
sshd_custom_path = attribute('sshd_custom_path', value: '/etc/ssh', description: 'Sometimes ssh configuration files are present in another location and ssh use them with the -f flag')
33+
34+
sshd_valid_privseparation = if sshd_custom_user != 'root'
35+
'no'
36+
else
37+
ssh_crypto.valid_privseparation
38+
end
3139

3240
only_if do
3341
command('sshd').exist?
@@ -63,12 +71,12 @@
6371
control 'sshd-04' do
6472
impact 1.0
6573
title 'Server: Check SSH folder owner, group and permissions.'
66-
desc 'The SSH folder should owned by root, only be writable by owner and readable by others.'
67-
describe file('/etc/ssh') do
74+
desc 'The SSH folder should owned by root or a defined user, only be writable by owner and readable by others.'
75+
describe file(sshd_custom_path) do
6876
it { should exist }
6977
it { should be_directory }
7078
it { should be_owned_by 'root' }
71-
it { should be_grouped_into os.darwin? ? 'wheel' : 'root' }
79+
it { should be_grouped_into os.darwin? ? 'wheel' : sshd_custom_user }
7280
it { should be_executable }
7381
it { should be_readable.by('owner') }
7482
it { should be_readable.by('group') }
@@ -84,7 +92,7 @@
8492
title 'Server: Check sshd_config owner, group and permissions.'
8593
desc 'The sshd_config should owned by root, only be writable/readable by owner and not be executable.'
8694

87-
describe file('/etc/ssh/sshd_config') do
95+
describe file(sshd_custom_path + '/sshd_config') do
8896
it { should exist }
8997
it { should be_file }
9098
it { should be_owned_by 'root' }
@@ -194,7 +202,7 @@
194202
title 'Server: Use privilege separation'
195203
desc 'UsePrivilegeSeparation is an option, when enabled will allow the OpenSSH server to run a small (necessary) amount of code as root and the of the code in a chroot jail environment. This enables ssh to deal incoming network traffic in an unprivileged child process to avoid privilege escalation by an attacker.'
196204
describe sshd_config do
197-
its('UsePrivilegeSeparation') { should eq(ssh_crypto.valid_privseparation) }
205+
its('UsePrivilegeSeparation') { should eq(sshd_valid_privseparation) }
198206
end
199207
end
200208

@@ -481,7 +489,7 @@
481489
impact 1.0
482490
title 'Server: DH primes'
483491
desc 'Verifies if strong DH primes are used in /etc/ssh/moduli'
484-
describe bash("test $(awk '$5 < 2047 && $5 ~ /^[0-9]+$/ { print $5 }' /etc/ssh/moduli | uniq | wc -c) -eq 0") do
492+
describe bash("test $(awk '$5 < 2047 && $5 ~ /^[0-9]+$/ { print $5 }' #{sshd_custom_path}/moduli | uniq | wc -c) -eq 0") do
485493
its('exit_status') { should eq 0 }
486494
its('stdout') { should eq '' }
487495
its('stderr') { should eq '' }

0 commit comments

Comments
 (0)