]> git.proxmox.com Git - systemd.git/blame - test/units/testsuite-22.03.sh
New upstream version 249~rc1
[systemd.git] / test / units / testsuite-22.03.sh
CommitLineData
8b3d4ff0 1#!/bin/bash
6e866b33
MB
2#
3# Basic tests for types creating/writing files
4#
5
8b3d4ff0
MB
6set -eux
7set -o pipefail
6e866b33
MB
8
9rm -fr /tmp/{f,F,w}
10mkdir /tmp/{f,F,w}
11touch /tmp/file-owned-by-root
12
13#
14# 'f'
15#
16systemd-tmpfiles --create - <<EOF
17f /tmp/f/1 0644 - - - -
18f /tmp/f/2 0644 - - - This string should be written
19EOF
20
21### '1' should exist and be empty
8b3d4ff0
MB
22test -f /tmp/f/1; test ! -s /tmp/f/1
23test "$(stat -c %U:%G:%a /tmp/f/1)" = "root:root:644"
6e866b33 24
8b3d4ff0 25test "$(stat -c %U:%G:%a /tmp/f/2)" = "root:root:644"
6e866b33
MB
26test "$(< /tmp/f/2)" = "This string should be written"
27
28### The perms are supposed to be updated even if the file already exists.
29systemd-tmpfiles --create - <<EOF
30f /tmp/f/1 0666 daemon daemon - This string should not be written
31EOF
32
33# file should be empty
8b3d4ff0
MB
34test ! -s /tmp/f/1
35test "$(stat -c %U:%G:%a /tmp/f/1)" = "daemon:daemon:666"
6e866b33
MB
36
37### But we shouldn't try to set perms on an existing file which is not a
38### regular one.
39mkfifo /tmp/f/fifo
40chmod 644 /tmp/f/fifo
41
8b3d4ff0 42systemd-tmpfiles --create - <<EOF && { echo 'unexpected success'; exit 1; }
6e866b33
MB
43f /tmp/f/fifo 0666 daemon daemon - This string should not be written
44EOF
45
46test -p /tmp/f/fifo
8b3d4ff0 47test "$(stat -c %U:%G:%a /tmp/f/fifo)" = "root:root:644"
6e866b33
MB
48
49### 'f' should not follow symlinks.
50ln -s missing /tmp/f/dangling
51ln -s /tmp/file-owned-by-root /tmp/f/symlink
52
8b3d4ff0 53systemd-tmpfiles --create - <<EOF && { echo 'unexpected success'; exit 1; }
6e866b33
MB
54f /tmp/f/dangling 0644 daemon daemon - -
55f /tmp/f/symlink 0644 daemon daemon - -
56EOF
8b3d4ff0
MB
57test ! -e /tmp/f/missing
58test "$(stat -c %U:%G:%a /tmp/file-owned-by-root)" = "root:root:644"
6e866b33
MB
59
60### Handle read-only filesystem gracefully: we shouldn't fail if the target
61### already exists and have the correct perms.
62mkdir /tmp/f/rw-fs
63mkdir /tmp/f/ro-fs
64
65touch /tmp/f/rw-fs/foo
66chmod 644 /tmp/f/rw-fs/foo
67
68mount -o bind,ro /tmp/f/rw-fs /tmp/f/ro-fs
69
70systemd-tmpfiles --create - <<EOF
71f /tmp/f/ro-fs/foo 0644 - - - - This string should not be written
72EOF
8b3d4ff0 73test -f /tmp/f/ro-fs/foo; test ! -s /tmp/f/ro-fs/foo
6e866b33 74
8b3d4ff0 75systemd-tmpfiles --create - <<EOF && { echo 'unexpected success'; exit 1; }
6e866b33
MB
76f /tmp/f/ro-fs/foo 0666 - - - -
77EOF
8b3d4ff0 78test "$(stat -c %U:%G:%a /tmp/f/fifo)" = "root:root:644"
6e866b33 79
8b3d4ff0 80systemd-tmpfiles --create - <<EOF && { echo 'unexpected success'; exit 1; }
6e866b33
MB
81f /tmp/f/ro-fs/bar 0644 - - - -
82EOF
8b3d4ff0 83test ! -e /tmp/f/ro-fs/bar
6e866b33
MB
84
85### 'f' shouldn't follow unsafe paths.
86mkdir /tmp/f/daemon
87ln -s /root /tmp/f/daemon/unsafe-symlink
88chown -R --no-dereference daemon:daemon /tmp/f/daemon
89
8b3d4ff0 90systemd-tmpfiles --create - <<EOF && { echo 'unexpected success'; exit 1; }
6e866b33
MB
91f /tmp/f/daemon/unsafe-symlink/exploit 0644 daemon daemon - -
92EOF
8b3d4ff0 93test ! -e /tmp/f/daemon/unsafe-symlink/exploit
6e866b33
MB
94
95#
96# 'F'
97#
98echo "This should be truncated" >/tmp/F/truncated
99echo "This should be truncated" >/tmp/F/truncated-with-content
100
101systemd-tmpfiles --create - <<EOF
102F /tmp/F/created 0644 - - - -
103F /tmp/F/created-with-content 0644 - - - new content
104F /tmp/F/truncated 0666 daemon daemon - -
105F /tmp/F/truncated-with-content 0666 daemon daemon - new content
106EOF
107
8b3d4ff0 108test -f /tmp/F/created; test ! -s /tmp/F/created
6e866b33
MB
109test -f /tmp/F/created-with-content
110test "$(< /tmp/F/created-with-content)" = "new content"
8b3d4ff0
MB
111test -f /tmp/F/truncated; test ! -s /tmp/F/truncated
112test "$(stat -c %U:%G:%a /tmp/F/truncated)" = "daemon:daemon:666"
6e866b33 113test -s /tmp/F/truncated-with-content
8b3d4ff0 114test "$(stat -c %U:%G:%a /tmp/F/truncated-with-content)" = "daemon:daemon:666"
6e866b33
MB
115
116### We shouldn't try to truncate anything but regular files since the behavior is
117### unspecified in the other cases.
118mkfifo /tmp/F/fifo
119
8b3d4ff0 120systemd-tmpfiles --create - <<EOF && { echo 'unexpected success'; exit 1; }
6e866b33
MB
121F /tmp/F/fifo 0644 - - - -
122EOF
123
124test -p /tmp/F/fifo
125
126### 'F' should not follow symlinks.
127ln -s missing /tmp/F/dangling
128ln -s /tmp/file-owned-by-root /tmp/F/symlink
129
8b3d4ff0 130systemd-tmpfiles --create - <<EOF && { echo 'unexpected success'; exit 1; }
6e866b33
MB
131f /tmp/F/dangling 0644 daemon daemon - -
132f /tmp/F/symlink 0644 daemon daemon - -
133EOF
8b3d4ff0
MB
134test ! -e /tmp/F/missing
135test "$(stat -c %U:%G:%a /tmp/file-owned-by-root)" = "root:root:644"
6e866b33
MB
136
137### Handle read-only filesystem gracefully: we shouldn't fail if the target
138### already exists and is empty.
139mkdir /tmp/F/rw-fs
140mkdir /tmp/F/ro-fs
141
142touch /tmp/F/rw-fs/foo
143chmod 644 /tmp/F/rw-fs/foo
144
145mount -o bind,ro /tmp/F/rw-fs /tmp/F/ro-fs
146
147systemd-tmpfiles --create - <<EOF
148F /tmp/F/ro-fs/foo 0644 - - - -
149EOF
8b3d4ff0 150test -f /tmp/F/ro-fs/foo; test ! -s /tmp/F/ro-fs/foo
6e866b33
MB
151
152echo "truncating is not allowed anymore" >/tmp/F/rw-fs/foo
8b3d4ff0 153systemd-tmpfiles --create - <<EOF && { echo 'unexpected success'; exit 1; }
6e866b33
MB
154F /tmp/F/ro-fs/foo 0644 - - - -
155EOF
156
8b3d4ff0 157systemd-tmpfiles --create - <<EOF && { echo 'unexpected success'; exit 1; }
6e866b33
MB
158F /tmp/F/ro-fs/foo 0644 - - - - This string should not be written
159EOF
8b3d4ff0
MB
160test -f /tmp/F/ro-fs/foo
161grep -q 'truncating is not allowed' /tmp/F/ro-fs/foo
6e866b33
MB
162
163# Trying to change the perms should fail.
8b3d4ff0
MB
164: >/tmp/F/rw-fs/foo
165systemd-tmpfiles --create - <<EOF && { echo 'unexpected success'; exit 1; }
6e866b33
MB
166F /tmp/F/ro-fs/foo 0666 - - - -
167EOF
8b3d4ff0 168test "$(stat -c %U:%G:%a /tmp/F/ro-fs/foo)" = "root:root:644"
6e866b33
MB
169
170### Try to create a new file.
8b3d4ff0 171systemd-tmpfiles --create - <<EOF && { echo 'unexpected success'; exit 1; }
6e866b33
MB
172F /tmp/F/ro-fs/bar 0644 - - - -
173EOF
8b3d4ff0 174test ! -e /tmp/F/ro-fs/bar
6e866b33
MB
175
176### 'F' shouldn't follow unsafe paths.
177mkdir /tmp/F/daemon
178ln -s /root /tmp/F/daemon/unsafe-symlink
179chown -R --no-dereference daemon:daemon /tmp/F/daemon
180
8b3d4ff0 181systemd-tmpfiles --create - <<EOF && { echo 'unexpected success'; exit 1; }
6e866b33
MB
182F /tmp/F/daemon/unsafe-symlink/exploit 0644 daemon daemon - -
183EOF
8b3d4ff0 184test ! -e /tmp/F/daemon/unsafe-symlink/exploit
6e866b33
MB
185
186#
187# 'w'
188#
189touch /tmp/w/overwritten
190
191### nop if the target does not exist.
192systemd-tmpfiles --create - <<EOF
193w /tmp/w/unexistent 0644 - - - new content
194EOF
8b3d4ff0 195test ! -e /tmp/w/unexistent
6e866b33
MB
196
197### no argument given -> fails.
8b3d4ff0 198systemd-tmpfiles --create - <<EOF && { echo 'unexpected success'; exit 1; }
6e866b33
MB
199w /tmp/w/unexistent 0644 - - - -
200EOF
201
202### write into an empty file.
203systemd-tmpfiles --create - <<EOF
204w /tmp/w/overwritten 0644 - - - old content
205EOF
206test -f /tmp/w/overwritten
207test "$(< /tmp/w/overwritten)" = "old content"
208
209### new content is overwritten
210systemd-tmpfiles --create - <<EOF
211w /tmp/w/overwritten 0644 - - - new content
212EOF
213test -f /tmp/w/overwritten
214test "$(< /tmp/w/overwritten)" = "new content"
215
f2dec872 216### writing into an 'exotic' file should be allowed.
6e866b33
MB
217systemd-tmpfiles --create - <<EOF
218w /dev/null - - - - new content
219EOF
220
221### 'w' follows symlinks
222ln -s ./overwritten /tmp/w/symlink
223systemd-tmpfiles --create - <<EOF
224w /tmp/w/symlink - - - - $(readlink -e /tmp/w/symlink)
225EOF
226readlink -e /tmp/w/symlink
227test "$(< /tmp/w/overwritten)" = "/tmp/w/overwritten"
228
229### 'w' shouldn't follow unsafe paths.
230mkdir /tmp/w/daemon
231ln -s /root /tmp/w/daemon/unsafe-symlink
232chown -R --no-dereference daemon:daemon /tmp/w/daemon
233
8b3d4ff0 234systemd-tmpfiles --create - <<EOF && { echo 'unexpected success'; exit 1; }
6e866b33
MB
235f /tmp/w/daemon/unsafe-symlink/exploit 0644 daemon daemon - -
236EOF
8b3d4ff0 237test ! -e /tmp/w/daemon/unsafe-symlink/exploit