Skip to content

Commit 7fe74d7

Browse files
WOnder93stephensmalley
authored andcommitted
tests/overlay: add tests for mmap(2) and mprotect(2)
Add asserts that verify the expected behavior of mmap(2) and mprotect(2) on overlayfs files. Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com> Tested-by: Paul Moore <paul@paul-moore.com>
1 parent 03577f5 commit 7fe74d7

5 files changed

Lines changed: 233 additions & 17 deletions

File tree

policy/test_overlayfs.te

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ files_type(test_overlay_files_ro_t)
2121
type test_overlay_files_rwx_t;
2222
files_type(test_overlay_files_rwx_t)
2323

24+
type test_overlay_files_map_ro_t;
25+
files_type(test_overlay_files_map_ro_t)
26+
27+
type test_overlay_files_map_rwx_t;
28+
files_type(test_overlay_files_map_rwx_t)
29+
2430
type test_overlay_mounter_files_t;
2531
files_type(test_overlay_mounter_files_t)
2632

@@ -60,7 +66,11 @@ allow test_overlay_mounter_t test_overlay_files_ro_t:file mounton;
6066
allow test_overlay_mounter_t test_overlay_files_ro_t:filesystem { mount relabelfrom relabelto };
6167
allow test_overlay_mounter_t test_overlay_files_rwx_t:{dir file} mounton;
6268
allow test_overlay_mounter_t test_overlay_files_rwx_t:filesystem { mount relabelfrom relabelto };
63-
can_exec(test_overlay_mounter_t, test_overlay_files_ro_t)
69+
allow test_overlay_mounter_t test_overlay_files_map_ro_t:file mounton;
70+
allow test_overlay_mounter_t test_overlay_files_map_ro_t:filesystem { mount relabelfrom relabelto };
71+
allow test_overlay_mounter_t test_overlay_files_map_rwx_t:{dir file} mounton;
72+
allow test_overlay_mounter_t test_overlay_files_map_rwx_t:filesystem { mount relabelfrom relabelto };
73+
can_exec(test_overlay_mounter_t, test_overlay_files_map_ro_t)
6474

6575
#
6676
# Mounter should be allowed to search/read r/o directories and files
@@ -70,10 +80,14 @@ read_files_pattern(test_overlay_mounter_t, test_overlay_files_ro_t, test_overlay
7080
read_chr_files_pattern(test_overlay_mounter_t, test_overlay_files_ro_t, test_overlay_files_ro_t)
7181
read_blk_files_pattern(test_overlay_mounter_t, test_overlay_files_ro_t, test_overlay_files_ro_t)
7282
read_lnk_files_pattern(test_overlay_mounter_t, test_overlay_files_ro_t, test_overlay_files_ro_t)
83+
list_dirs_pattern(test_overlay_mounter_t, test_overlay_files_map_ro_t, test_overlay_files_map_ro_t)
84+
read_files_pattern(test_overlay_mounter_t, test_overlay_files_map_ro_t, test_overlay_files_map_ro_t)
85+
allow_map(test_overlay_mounter_t, test_overlay_files_map_ro_t, file)
7386
allow test_overlay_mounter_t test_overlay_files_ro_t:dir mounton;
7487
allow test_overlay_mounter_t test_overlay_mounter_files_t:dir mounton;
75-
allow test_overlay_mounter_t test_overlay_files_rwx_t:filesystem unmount;
88+
allow test_overlay_mounter_t test_overlay_files_map_rwx_t:filesystem unmount;
7689
manage_files_pattern(test_overlay_mounter_t, test_overlay_mounter_files_t, test_overlay_mounter_files_t)
90+
allow_map(test_overlay_mounter_t, test_overlay_mounter_files_t, file)
7791
# Needed for transition
7892
manage_dirs_pattern(test_overlay_mounter_t, test_overlay_mounter_files_t, test_overlay_mounter_files_t)
7993
#
@@ -90,6 +104,10 @@ manage_dirs_pattern(test_overlay_mounter_t, test_overlay_files_rwx_t, test_overl
90104
manage_lnk_files_pattern(test_overlay_mounter_t, test_overlay_files_rwx_t, test_overlay_files_rwx_t)
91105
manage_sock_files_pattern(test_overlay_mounter_t, test_overlay_files_rwx_t, test_overlay_files_rwx_t)
92106

107+
manage_files_pattern(test_overlay_mounter_t, test_overlay_files_map_rwx_t, test_overlay_files_map_rwx_t)
108+
manage_dirs_pattern(test_overlay_mounter_t, test_overlay_files_map_rwx_t, test_overlay_files_map_rwx_t)
109+
allow_map(test_overlay_mounter_t, test_overlay_files_map_rwx_t, file)
110+
93111
manage_files_pattern(test_overlay_mounter_t, test_overlay_transition_files_t, test_overlay_transition_files_t)
94112

95113
#
@@ -121,7 +139,11 @@ list_dirs_pattern(test_overlay_client_t, test_overlay_files_ro_t, test_overlay_f
121139
read_lnk_files_pattern(test_overlay_client_t, test_overlay_files_ro_t, test_overlay_files_ro_t)
122140
read_chr_files_pattern(test_overlay_client_t, test_overlay_files_ro_t, test_overlay_files_ro_t)
123141
read_sock_files_pattern(test_overlay_client_t, test_overlay_files_ro_t, test_overlay_files_ro_t)
124-
can_exec(test_overlay_client_t, test_overlay_files_ro_t)
142+
143+
read_files_pattern(test_overlay_client_t, test_overlay_files_map_ro_t, test_overlay_files_map_ro_t)
144+
allow_map(test_overlay_client_t, test_overlay_files_map_ro_t, file)
145+
can_exec(test_overlay_client_t, test_overlay_files_map_ro_t)
146+
allow test_overlay_client_t test_overlay_files_map_ro_t:file entrypoint;
125147

126148
#
127149
# Client should be allowed to execute/write/search/read r/w/x directories and files
@@ -131,15 +153,22 @@ manage_dirs_pattern(test_overlay_client_t, test_overlay_files_rwx_t, test_overla
131153
manage_lnk_files_pattern(test_overlay_client_t, test_overlay_files_rwx_t, test_overlay_files_rwx_t)
132154
rw_chr_files_pattern(test_overlay_client_t, test_overlay_files_rwx_t, test_overlay_files_rwx_t)
133155
manage_sock_files_pattern(test_overlay_client_t, test_overlay_files_rwx_t, test_overlay_files_rwx_t)
134-
can_exec(test_overlay_client_t, test_overlay_files_rwx_t)
135-
allow test_overlay_client_t test_overlay_files_rwx_t:file entrypoint;
156+
157+
manage_files_pattern(test_overlay_client_t, test_overlay_files_map_rwx_t, test_overlay_files_map_rwx_t)
158+
manage_dirs_pattern(test_overlay_client_t, test_overlay_files_map_rwx_t, test_overlay_files_map_rwx_t)
159+
manage_lnk_files_pattern(test_overlay_client_t, test_overlay_files_map_rwx_t, test_overlay_files_map_rwx_t)
160+
rw_chr_files_pattern(test_overlay_client_t, test_overlay_files_map_rwx_t, test_overlay_files_map_rwx_t)
161+
manage_sock_files_pattern(test_overlay_client_t, test_overlay_files_map_rwx_t, test_overlay_files_map_rwx_t)
162+
allow_map(test_overlay_client_t, test_overlay_files_map_rwx_t, file)
163+
can_exec(test_overlay_client_t, test_overlay_files_map_rwx_t)
164+
allow test_overlay_client_t test_overlay_files_map_rwx_t:file entrypoint;
136165

137166
#
138167
# Client needs to manage test_overlay_transition_files_t and transition on file named "transition"
139168
#
140169
manage_files_pattern(test_overlay_client_t, test_overlay_transition_files_t, test_overlay_transition_files_t)
141170
manage_dirs_pattern(test_overlay_client_t, test_overlay_transition_files_t, test_overlay_transition_files_t)
142-
filetrans_pattern(test_overlay_client_t, test_overlay_files_rwx_t, test_overlay_transition_files_t, file, "transition")
171+
filetrans_pattern(test_overlay_client_t, test_overlay_files_map_rwx_t, test_overlay_transition_files_t, file, "transition")
143172

144173
#
145174
# Client needs to search test_file_t directory and execute tests
@@ -153,7 +182,9 @@ can_exec(test_overlay_client_t,test_file_t)
153182
#
154183
allow test_overlay_client_t test_overlay_files_rwx_t:file { relabelto relabelfrom };
155184
allow test_overlay_mounter_t test_overlay_files_rwx_t:file { relabelto relabelfrom };
156-
can_exec(test_overlay_mounter_t, test_overlay_files_rwx_t)
185+
allow test_overlay_client_t test_overlay_files_map_rwx_t:file { relabelto relabelfrom };
186+
allow test_overlay_mounter_t test_overlay_files_map_rwx_t:file { relabelto relabelfrom };
187+
can_exec(test_overlay_mounter_t, test_overlay_files_map_rwx_t)
157188

158189
#
159190
# Server needs to manage socket files

tests/overlay/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
access
2+
map_access

tests/overlay/map_access.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <unistd.h>
2+
#include <fcntl.h>
3+
4+
#include <sys/mman.h>
5+
6+
#include <stdio.h>
7+
#include <string.h>
8+
#include <errno.h>
9+
10+
int main(int argc, const char **argv)
11+
{
12+
const char *file;
13+
void *ptr;
14+
int rdonly, fd, ret;
15+
16+
if (argc != 3 || (strcmp(argv[2], "R_OK") && strcmp(argv[2], "W_OK"))) {
17+
fprintf(stderr, "Usage %s <file> R_OK|W_OK\n", argv[0]);
18+
return EINVAL;
19+
}
20+
21+
file = argv[1];
22+
rdonly = strcmp(argv[2], "R_OK") == 0;
23+
24+
fd = open(file, rdonly ? O_RDONLY : O_RDWR);
25+
if (fd == -1) {
26+
perror("open");
27+
return 2;
28+
}
29+
30+
/* try direct mmap */
31+
ptr = mmap(NULL, 1, rdonly ? PROT_READ : PROT_READ | PROT_WRITE,
32+
MAP_SHARED, fd, 0);
33+
if (ptr == MAP_FAILED) {
34+
perror("mmap");
35+
return 3;
36+
}
37+
munmap(ptr, 1);
38+
39+
/* try mmap with PROT_NONE followed by mprotect with full access */
40+
ptr = mmap(NULL, 1, PROT_NONE, MAP_SHARED, fd, 0);
41+
if (ptr == MAP_FAILED) {
42+
perror("mmap PROT_NONE");
43+
return 4;
44+
}
45+
46+
ret = mprotect(ptr, 1, rdonly ? PROT_READ : PROT_READ | PROT_WRITE);
47+
if (ret == -1) {
48+
perror("mprotect");
49+
return 5;
50+
}
51+
52+
munmap(ptr, 1);
53+
close(fd);
54+
return 0;
55+
}

tests/overlay/setup-overlay

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ setup () {
1212
mkdir -p $BASEDIR/lower/readdir $BASEDIR/lower/writedir $BASEDIR/lower/noaccessdir $BASEDIR/lower/mounterdir
1313
echo read > $BASEDIR/lower/readfile
1414
echo write > $BASEDIR/lower/writefile
15+
echo mapread > $BASEDIR/lower/mapreadfile
16+
echo mapwrite > $BASEDIR/lower/mapwritefile
1517
echo noaccess > $BASEDIR/lower/noaccessfile
1618
echo client_nounlink > $BASEDIR/lower/client_nounlinkfile
1719
echo mounter > $BASEDIR/lower/mounterfile
@@ -35,7 +37,11 @@ setup () {
3537
chcon -t test_overlay_mounter_files_t $BASEDIR/lower/mounterfile $BASEDIR/lower/mounterdir $BASEDIR/lower/null_mounter
3638
# Label writefile on lower with a type that is writable by the client which
3739
# should allow copy up and write.
38-
chcon -t test_overlay_files_rwx_t $BASEDIR/lower/writefile $BASEDIR/lower/writedir $BASEDIR/lower/null_write $BASEDIR/lower/transition $BASEDIR/lower/goodentrypoint
40+
chcon -t test_overlay_files_rwx_t $BASEDIR/lower/writefile $BASEDIR/lower/writedir $BASEDIR/lower/null_write $BASEDIR/lower/transition
41+
# Label mapreadfile and mapwritefile with the appropriate types that
42+
# allow map access
43+
chcon -t test_overlay_files_map_ro_t $BASEDIR/lower/mapreadfile $BASEDIR/lower/goodentrypoint
44+
chcon -t test_overlay_files_map_rwx_t $BASEDIR/lower/mapwritefile
3945
# Create a directory per container.
4046
mkdir -p $BASEDIR/container1
4147

@@ -48,7 +54,7 @@ setup () {
4854
# creation and to prevent permission problems when overlayfs
4955
# internally creates files on behalf of the current process.
5056
chcon -R -t test_overlay_mounter_files_t $BASEDIR/container1/work $BASEDIR/container1/merged
51-
chcon -R -t test_overlay_files_rwx_t $BASEDIR/container1/upper
57+
chcon -R -t test_overlay_files_map_rwx_t $BASEDIR/container1/upper
5258
}
5359

5460
# Mount the overlays.

0 commit comments

Comments
 (0)