1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) 2019 Petr Vorel <pvorel@suse.cz>
4# Based on reproducer and further discussion with Ignaz Forster <iforster@suse.de>
5# Reproducer for not upstreamed patchset [1] and previous report [2].
6# [1] https://www.spinics.net/lists/linux-integrity/msg05926.html
7# [2] https://www.spinics.net/lists/linux-integrity/msg03593.html
8
9TST_SETUP="setup"
10TST_CLEANUP="cleanup"
11TST_NEEDS_DEVICE=1
12TST_CNT=4
13. ima_setup.sh
14
15setup()
16{
17	EVM_FILE="/sys/kernel/security/evm"
18
19	[ -f "$EVM_FILE" ] || tst_brk TCONF "EVM not enabled in kernel"
20	[ $(cat $EVM_FILE) -eq 1 ] || tst_brk TCONF "EVM not enabled for this boot"
21
22	check_ima_policy "appraise_tcb"
23
24	lower="$TST_MNTPOINT/lower"
25	upper="$TST_MNTPOINT/upper"
26	work="$TST_MNTPOINT/work"
27	merged="$TST_MNTPOINT/merged"
28	mkdir -p $lower $upper $work $merged
29
30	device_backup="$TST_DEVICE"
31	TST_DEVICE="overlay"
32
33	fs_type_backup="$TST_FS_TYPE"
34	TST_FS_TYPE="overlay"
35
36	mntpoint_backup="$TST_MNTPOINT"
37	TST_MNTPOINT="$merged"
38
39	params_backup="$TST_MNT_PARAMS"
40	TST_MNT_PARAMS="-o lowerdir=$lower,upperdir=$upper,workdir=$work"
41
42	tst_mount
43	mounted=1
44}
45
46test1()
47{
48	local file="foo1.txt"
49
50	tst_res TINFO "overwrite file in overlay"
51	EXPECT_PASS echo lower \> $lower/$file
52	EXPECT_PASS echo overlay \> $merged/$file
53}
54
55test2()
56{
57	local file="foo2.txt"
58
59	tst_res TINFO "append file in overlay"
60	EXPECT_PASS echo lower \> $lower/$file
61	EXPECT_PASS echo overlay \>\> $merged/$file
62}
63
64test3()
65{
66	local file="foo3.txt"
67
68	tst_res TINFO "create a new file in overlay"
69	EXPECT_PASS echo overlay \> $merged/$file
70}
71
72test4()
73{
74	local f
75
76	tst_res TINFO "read all created files"
77	for f in $(find $TST_MNTPOINT -type f); do
78		EXPECT_PASS cat $f \> /dev/null 2\> /dev/null
79	done
80}
81
82cleanup()
83{
84	[ -n "$mounted" ] || return 0
85
86	tst_umount $TST_DEVICE
87
88	TST_DEVICE="$device_backup"
89	TST_FS_TYPE="$fs_type_backup"
90	TST_MNTPOINT="$mntpoint_backup"
91	TST_MNT_PARAMS="$params_backup"
92}
93
94tst_run
95