Skip to content

Commit aac97cc

Browse files
committed
ima_setup.sh: Allow to load predefined policy
environment variable LTP_IMA_LOAD_POLICY=1 tries to load example policy if available. This should be used only if tooling running LTP tests allows to reboot afterwards because policy may be writable only once, e.g. missing CONFIG_IMA_WRITE_POLICY=y, or policies can influence each other. Loading may fail due various reasons (e.g. previously mentioned missing CONFIG_IMA_WRITE_POLICY=y and policy already loaded or when secure boot is enabled and the kernel is configured with CONFIG_IMA_ARCH_POLICY enabled, an appraise func=POLICY_CHECK appraise_type=imasig rule is loaded, requiring the IMA policy itself to be signed). Link: https://lore.kernel.org/ltp/[email protected]/ Reviewed-by: Mimi Zohar <[email protected]> Signed-off-by: Petr Vorel <[email protected]>
1 parent f53871d commit aac97cc

File tree

7 files changed

+82
-10
lines changed

7 files changed

+82
-10
lines changed

doc/users/setup_tests.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ users.
5959
both up and down with this multiplier. This is not yet implemented in the
6060
shell API.
6161

62+
* - LTP_IMA_LOAD_POLICY
63+
- Load IMA example policy, see :master:`testcases/kernel/security/integrity/ima/README.md`.
64+
6265
* - LTP_VIRT_OVERRIDE
6366
- Overrides virtual machine detection in the test library. Setting it to
6467
empty string, tells the library that system is not a virtual machine.

testcases/kernel/security/integrity/ima/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ CONFIG_INTEGRITY=y
88
CONFIG_IMA=y
99
```
1010

11+
### Loading policy for testing (optional)
12+
Setting environment variable `LTP_IMA_LOAD_POLICY=1` tries to load example
13+
policy if available. This should be used only if tooling running LTP tests
14+
allows to reboot afterwards because policy may be writable only once, e.g.
15+
missing `CONFIG_IMA_WRITE_POLICY=y`, or policies can influence each other.
16+
17+
Loading may fail due various reasons (e.g. previously mentioned missing
18+
`CONFIG_IMA_WRITE_POLICY=y` and policy already loaded or when secure boot is
19+
enabled and the kernel is configured with `CONFIG_IMA_ARCH_POLICY` enabled, an
20+
`appraise func=POLICY_CHECK appraise_type=imasig` rule is loaded, requiring the
21+
IMA policy itself to be signed).
22+
1123
### IMA measurement tests
1224
`ima_measurements.sh` require builtin IMA tcb policy to be loaded
1325
(`ima_policy=tcb` kernel parameter).

testcases/kernel/security/integrity/ima/tests/ima_kexec.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# Verify that kexec cmdline is measured correctly.
88
# Test attempts to kexec the existing running kernel image.
99
# To kexec a different kernel image export IMA_KEXEC_IMAGE=<pathname>.
10+
# Test requires example IMA policy loadable with LTP_IMA_LOAD_POLICY=1.
1011

1112
TST_NEEDS_CMDS="grep kexec sed"
1213
TST_CNT=3

testcases/kernel/security/integrity/ima/tests/ima_keys.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Author: Lachlan Sneff <[email protected]>
66
#
77
# Verify that keys are measured correctly based on policy.
8+
# Test requires example IMA policy loadable with LTP_IMA_LOAD_POLICY=1.
89

910
TST_NEEDS_CMDS="cmp cut grep sed"
1011
TST_CNT=2

testcases/kernel/security/integrity/ima/tests/ima_measurements.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Author: Mimi Zohar <[email protected]>
66
#
77
# Verify that measurements are added to the measurement list based on policy.
8-
# Test requires ima_policy=tcb.
8+
# Test requires either ima_policy=tcb or example policy loadable with LTP_IMA_LOAD_POLICY=1.
99

1010
TST_NEEDS_CMDS="awk cut sed"
1111
TST_SETUP="setup"

testcases/kernel/security/integrity/ima/tests/ima_selinux.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Author: Lakshmi Ramasubramanian <[email protected]>
66
#
77
# Verify measurement of SELinux policy hash and state.
8+
# Test requires example IMA policy loadable with LTP_IMA_LOAD_POLICY=1.
89
#
910
# Relevant kernel commits:
1011
# * fdd1ffe8a812 ("selinux: include a consumer of the new IMA critical data hook")

testcases/kernel/security/integrity/ima/tests/ima_setup.sh

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,20 @@ require_policy_readable()
7676
fi
7777
}
7878

79-
require_policy_writable()
79+
check_policy_writable()
8080
{
81-
local err="IMA policy already loaded and kernel not configured to enable multiple writes to it (need CONFIG_IMA_WRITE_POLICY=y)"
82-
83-
[ -f $IMA_POLICY ] || tst_brk TCONF "$err"
84-
# CONFIG_IMA_READ_POLICY
81+
[ -f $IMA_POLICY ] || return 1
82+
# workaround for kernels < v4.18 without fix
83+
# ffb122de9a60b ("ima: Reflect correct permissions for policy")
8584
echo "" 2> log > $IMA_POLICY
86-
grep -q "Device or resource busy" log && tst_brk TCONF "$err"
85+
grep -q "Device or resource busy" log && return 1
86+
return 0
87+
}
88+
89+
require_policy_writable()
90+
{
91+
check_policy_writable || tst_brk TCONF \
92+
"IMA policy already loaded and kernel not configured to enable multiple writes to it (need CONFIG_IMA_WRITE_POLICY=y)"
8793
}
8894

8995
check_ima_policy_content()
@@ -183,16 +189,58 @@ verify_ima_policy()
183189
# check IMA policy content
184190
while read line; do
185191
if ! grep -q "$line" $IMA_POLICY; then
186-
tst_brk TCONF "missing required policy '$line'"
192+
tst_res TINFO "WARNING: missing required policy content: '$line'"
193+
return 1
187194
fi
188-
IMA_POLICY_CHECKED=1
189195
done < $file
196+
IMA_POLICY_CHECKED=1
190197
else
191198
tst_res TINFO "policy is not readable, failure will be treated as TCONF"
192199
IMA_FAIL="TCONF"
193200
IMA_BROK="TCONF"
201+
return 1
194202
fi
195203
fi
204+
return 0
205+
}
206+
207+
load_ima_policy()
208+
{
209+
local file="$TST_DATAROOT/$REQUIRED_POLICY_CONTENT"
210+
211+
if [ "$LTP_IMA_LOAD_POLICY" != 1 -a "$IMA_POLICY_CHECKED" != 1 ]; then
212+
tst_res TCONF "missing required policy, example policy can be loaded with LTP_IMA_LOAD_POLICY=1"
213+
return 0
214+
fi
215+
216+
if [ "$IMA_POLICY_CHECKED" = 1 ]; then
217+
tst_res TINFO "valid policy already loaded, ignore LTP_IMA_LOAD_POLICY=1"
218+
fi
219+
220+
tst_res TINFO "trying to load '$file' policy:"
221+
cat $file
222+
if ! check_policy_writable; then
223+
tst_res TINFO "WARNING: IMA policy already loaded and kernel not configured to enable multiple writes to it (need CONFIG_IMA_WRITE_POLICY=y), reboot required, failures will be treated as TCONF"
224+
IMA_FAIL="TCONF"
225+
IMA_BROK="TCONF"
226+
LTP_IMA_LOAD_POLICY=
227+
return
228+
fi
229+
230+
cat "$file" 2> log > $IMA_POLICY
231+
if grep -q "Device or resource busy" log; then
232+
tst_brk TBROK "loading policy failed"
233+
fi
234+
235+
if grep -q "write error: Permission denied" log; then
236+
tst_brk TCONF "loading unsigned policy failed"
237+
fi
238+
239+
IMA_POLICY_LOADED=1
240+
241+
tst_res TINFO "example policy successfully loaded"
242+
IMA_FAIL="TFAIL"
243+
IMA_BROK="TBROK"
196244
}
197245

198246
ima_setup()
@@ -217,7 +265,9 @@ ima_setup()
217265
cd "$TST_MNTPOINT"
218266
fi
219267

220-
verify_ima_policy
268+
if ! verify_ima_policy; then
269+
load_ima_policy
270+
fi
221271

222272
[ -n "$TST_SETUP_CALLER" ] && $TST_SETUP_CALLER
223273
}
@@ -231,6 +281,10 @@ ima_cleanup()
231281
for dir in $UMOUNT; do
232282
umount $dir
233283
done
284+
285+
if [ "$IMA_POLICY_LOADED" = 1 ]; then
286+
tst_res TINFO "WARNING: policy loaded via LTP_IMA_LOAD_POLICY=1, reboot recommended"
287+
fi
234288
}
235289

236290
set_digest_index()

0 commit comments

Comments
 (0)