File selinux-ready of Package libselinux
273
1
#!/bin/bash
2
3
KERNEL="unknown"
4
INITRD="unknown"
5
TD=""
6
7
8
# init needs /selinux to be there
9
check_dir()
10
{
11
SLDIRS="/selinux /sys/fs/selinux"
12
FOUND="no"
13
14
for DIR in $SLDIRS; do
15
if [ -d $DIR ]; then
16
printf "\tcheck_dir: OK. $DIR exists.\n"
17
FOUND="yes"
18
fi
19
done
20
21
if [ $FOUND == "yes" ]; then
22
return 0
23
else
24
printf "\tcheck_dir: ERR. Neither of $SLDIRS does exist. Please execute 'mkdir /sys/fs/selinux' as root\n"
25
return 1
26
fi
27
}
28
29
check_filesystem()
30
{
31
FSPATH="/proc/filesystems"
32
FSNAMES="securityfs selinuxfs"
33
OK="O"
34
35
for FSNAME in $FSNAMES; do
36
grep -w $FSNAME $FSPATH 1>&2 >/dev/null
37
38
if [ $? == 0 ]; then
39
printf "\tcheck_filesystem: OK. Filesystem '$FSNAME' exists.\n"
40
else
41
printf "\tcheck_filesystem: ERR. Filesystem '$FSNAME' is missing. Please enable SELinux while compiling the kernel.\n"
42
OK="1"
43
fi
44
done
45
if [ "$OK" == "0" ]; then
46
return 0;
47
else
48
return 1;
49
fi
50
}
51
52
check_boot()
53
{
54
BPARAM1="security=selinux"
55
BPARAM2="selinux=1"
56
57
printf "\tcheck_boot: Assuming GRUB2 as bootloader.\n"
58
59
# look for parameters of the current kernel
60
CURRENT_KERNEL=$(uname -r)
61
OTHERS=""
62
RETVAL="FAIL"
63
while read BLINE
64
do
65
K=$(echo $BLINE | awk -F' ' '{print $2}')
66
KERNEL=$(basename $K)
67
K=$(echo $KERNEL | sed s/vmlinuz-//)
68
69
if [ "$K" == "$CURRENT_KERNEL" ]; then
70
INITRD=initrd-$K
71
RETVAL="OK"
72
else
73
OTHERS="$KERNEL $OTHERS"
74
fi
75
done < <(grep -- $BPARAM1 /boot/grub2/grub.cfg 2>/dev/null | grep -- $BPARAM2)
76
77
if [ "$RETVAL" == OK ]; then
78
printf "\tcheck_boot: OK. Current kernel '$KERNEL' has boot-parameters '$BPARAM1 $BPARAM2'\n"
79
printf "\tcheck_boot: OK. Other kernels with correct parameters: $OTHERS\n"
80
return 0
81
else
82
printf "\tcheck_boot: ERR. Boot-parameter missing for booting the kernel.\n"
83
printf "\t Please use YaST2 to add 'security=selinux selinux=1' to the kernel boot-parameter list.\n"
84
return 1
85
fi
86
}
87
88
check_mkinitrd()
89
{
90
if [ "$INITRD" == "unknown" ]; then
91
return 1
92
fi
93
MCMD="mount.*/root/proc.*"
94
95
if ! [ -f "/boot/$INITRD" ];then
96
printf "\tcheck_mkinitrd: ERR. Unable to locate '/boot/$INITRD'\n"
97
return 2
98
fi
99
100
cp /boot/$INITRD $TD/ 2>/dev/null
101
102
if ! [ -f "$TD/$INITRD" ];then
103
printf "\tcheck_mkinitrd: ERR. Error while copying initrd file.'\n"
104
return 2
105
fi
106
107
108
pushd . 2>&1>/dev/null
109
cd $TD
110
mkdir initrd-extracted
111
cd initrd-extracted
112
INITRD_FORMAT=$(file $TD/$INITRD | awk -F' ' '{print $2}')
113
case $INITRD_FORMAT in
114
'XZ' )
115
xz -d -c $TD/$INITRD | cpio -i --force-local --no-absolute-filenames 2>/dev/null ;;
116
'ASCII' )
117
/usr/lib/dracut/skipcpio $TD/$INITRD | xz -d | cpio -i --force-local --no-absolute-filenames 2>/dev/null ;;
118
'gzip' )
119
gzip -d -c $TD/$INITRD | cpio -i --force-local --no-absolute-filenames 2>/dev/null ;;
120
* )
121
printf "\tcheck_mkinitrd: ERR. Error while extracting initrd file.'\n"
122
return 2
123
esac
124
if [ -d boot ]; then
125
grep -E -- $MCMD boot/* 2>&1 >/dev/null
126
FLG1=$?
127
grep -E -- load_policy boot/* 2>&1 >/dev/null
128
FLG2=$?
129
else
130
# looks like we're using dracut/systemd. We can only check if libselinux1
131
# exists
132
if [ -f lib64/libselinux.so.1 ]; then
133
# if this exists
134
FLG1=0
135
FLG2=0
136
fi
137
fi
138
popd 2>&1>/dev/null
139
140
if [ $FLG1 == 0 -a $FLG2 == 0 ];then
141
printf "\tcheck_mkinitrd: OK. Your initrd seems to be correct.\n"
142
return 0
143
else
144
printf "\tcheck_mkinitrd: ERR. Your initrd seems not to mount /proc of\n"
145
printf "\t the root filesystem during boot and/or load_policy\n"
146
printf "\t is missing,\n"
147
printf "\t this may be a reason for SELinux not working.\n"
148
return 1
149
fi
150
}
151
152
check_pam()
153
{
154
AA_PAM=0
155
SE_PAM=0
156
157
# test for AA pam module
158
grep apparmor /etc/pam.d/* 2>&1 >/dev/null
159
FLG=$?
160
if [ $FLG == 0 ]; then
161
AA_PAM=1
162
fi
163
164
# test for SELinux pam module
165
grep selinux /etc/pam.d/* 2>&1 >/dev/null
166
FLG=$?
167
if [ $FLG == 0 ]; then
168
SE_PAM=1
169
fi
170
171
# suggest config
172
if [ $SE_PAM == 1 ] && [ $AA_PAM == 0 ]; then
173
printf "\tcheck_pam: OK. Your PAM configuration seems to be correct.\n"
174
return 0
175
fi
176
printf "\tcheck_pam: ERR. Your PAM configuration seems to be incorrect.\n"
177
if [ $AA_PAM == 1 ]; then
178
printf " execute 'pam-config -d --apparmor' as root\n"
179
fi
180
if [ $SE_PAM == 0 ]; then
181
printf " execute 'pam-config -a --selinux' as root\n"
182
fi
183
184
return 1
185
}
186
187
check_initupstart()
188
{
189
CFGFILE="/etc/selinux/config"
190
191
if ! [ -f $CFGFILE ]; then
192
printf "\tcheck_initupstart: ERR. $CFGFILE does not exist.\n"
193
return 1;
194
fi
195
}
196
197
check_runlevel()
198
{
199
if [ "$(systemctl is-enabled restorecond.service)" == "enabled" ]; then
200
printf "\tcheck_runlevel: OK. restorecond is enabled on your system\n"
201
return 0;
202
fi
203
printf "\tcheck_runlevel: ERR. please execute 'yast2 runlevel' and enable restorecond.\n"
204
return 1
205
}
206
207
check_packages()
208
{
209
PKGLST="checkpolicy policycoreutils selinux-tools libselinux1 libsepol1 libsemanage1 selinux-policy"
210
FAIL=0
211
212
for i in $PKGLST
213
do
214
rpm -q $i 1>&2 >/dev/null
215
if [ $? == 1 ];then
216
printf "\tcheck_packages: ERR. Package '$i' not installed, please run 'zypper in $i' as root\n"
217
FAIL=1
218
fi
219
done
220
221
if [ $FAIL == 0 ]; then
222
printf "\tcheck_packages: OK. All essential packages are installed\n"
223
return 0
224
else
225
return 1
226
fi
227
}
228
229
check_config()
230
{
231
CF="/etc/selinux/config"
232
233
if [ -f $CF ];then
234
printf "\tcheck_config: OK. Config file seems to be there.\n"
235
# with -L because /etc/selinux/config is now a link to /etc/sysconfig/selinux-policy
236
if ! [ $(stat -L --printf=%a $CF) -eq "644" ]; then
237
printf "\tcheck_config: ERR. Config file '$CF' has wrong permissions.\n"
238
return 1
239
fi
240
241
# check that SELINUX is not disabled there
242
SELINUX_MODE=$(grep "^\s*SELINUX\s*=" $CF | sed "s/SELINUX\s*=\(\S*\)\s*"/\\1/)
243
case "$SELINUX_MODE" in
244
permissive | enforcing )
245
printf "\tcheck_config: OK. SELINUX is set to '$SELINUX_MODE'.\n"
246
return 0
247
;;
248
* )
249
printf "\tcheck_config: ERR. SELINUX is set to '$SELINUX_MODE' in '$CF'. Should be either 'permissive' or 'enforcing'\n"
250
return 1
251
;;
252
esac
253
else
254
printf "\tcheck_config: ERR. Config file '$CF' is missing.\n"
255
return 1
256
fi
257
}
258
259
TD=$(mktemp -q -d /tmp/selinux-ready.XXXXXX)
260
261
echo "Start checking your system if it is selinux-ready or not:"
262
check_dir
263
check_filesystem
264
check_boot
265
check_mkinitrd
266
check_packages
267
check_config
268
check_initupstart
269
check_pam
270
check_runlevel
271
272
rm -rf $TD
273