Skip to content

Commit b481341

Browse files
peaktocreekakpm00
authored andcommitted
selftest: test system mappings are sealed
Add sysmap_is_sealed.c to test system mappings are sealed. Note: CONFIG_MSEAL_SYSTEM_MAPPINGS must be set, as indicated in config file. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Jeff Xu <[email protected]> Reviewed-by: Lorenzo Stoakes <[email protected]> Reviewed-by: Kees Cook <[email protected]> Cc: Adhemerval Zanella <[email protected]> Cc: Alexander Mikhalitsyn <[email protected]> Cc: Alexey Dobriyan <[email protected]> Cc: Andrei Vagin <[email protected]> Cc: Anna-Maria Behnsen <[email protected]> Cc: Ard Biesheuvel <[email protected]> Cc: Benjamin Berg <[email protected]> Cc: Christoph Hellwig <[email protected]> Cc: Dave Hansen <[email protected]> Cc: David Rientjes <[email protected]> Cc: David S. Miller <[email protected]> Cc: Elliot Hughes <[email protected]> Cc: Florian Faineli <[email protected]> Cc: Greg Ungerer <[email protected]> Cc: Guenter Roeck <[email protected]> Cc: Heiko Carstens <[email protected]> Cc: Helge Deller <[email protected]> Cc: Hyeonggon Yoo <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jann Horn <[email protected]> Cc: Jason A. Donenfeld <[email protected]> Cc: Johannes Berg <[email protected]> Cc: Jorge Lucangeli Obes <[email protected]> Cc: Liam R. Howlett <[email protected]> Cc: Linus Waleij <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Matthew Wilcow (Oracle) <[email protected]> Cc: Michael Ellerman <[email protected]> Cc: Michal Hocko <[email protected]> Cc: Miguel Ojeda <[email protected]> Cc: Mike Rapoport <[email protected]> Cc: Oleg Nesterov <[email protected]> Cc: Pedro Falcato <[email protected]> Cc: Peter Xu <[email protected]> Cc: Randy Dunlap <[email protected]> Cc: Stephen Röttger <[email protected]> Cc: Thomas Weißschuh <[email protected]> Cc: Vlastimil Babka <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent a8c15bb commit b481341

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed

tools/testing/selftests/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ TARGETS += mount
6262
TARGETS += mount_setattr
6363
TARGETS += move_mount_set_group
6464
TARGETS += mqueue
65+
TARGETS += mseal_system_mappings
6566
TARGETS += nci
6667
TARGETS += net
6768
TARGETS += net/af_unix
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# SPDX-License-Identifier: GPL-2.0-only
2+
sysmap_is_sealed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# SPDX-License-Identifier: GPL-2.0-only
2+
CFLAGS += -std=c99 -pthread -Wall $(KHDR_INCLUDES)
3+
4+
TEST_GEN_PROGS := sysmap_is_sealed
5+
6+
include ../lib.mk
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_MSEAL_SYSTEM_MAPPINGS=y
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* test system mappings are sealed when
4+
* KCONFIG_MSEAL_SYSTEM_MAPPINGS=y
5+
*/
6+
7+
#define _GNU_SOURCE
8+
#include <stdio.h>
9+
#include <errno.h>
10+
#include <unistd.h>
11+
#include <string.h>
12+
#include <stdbool.h>
13+
14+
#include "../kselftest.h"
15+
#include "../kselftest_harness.h"
16+
17+
#define VMFLAGS "VmFlags:"
18+
#define MSEAL_FLAGS "sl"
19+
#define MAX_LINE_LEN 512
20+
21+
bool has_mapping(char *name, FILE *maps)
22+
{
23+
char line[MAX_LINE_LEN];
24+
25+
while (fgets(line, sizeof(line), maps)) {
26+
if (strstr(line, name))
27+
return true;
28+
}
29+
30+
return false;
31+
}
32+
33+
bool mapping_is_sealed(char *name, FILE *maps)
34+
{
35+
char line[MAX_LINE_LEN];
36+
37+
while (fgets(line, sizeof(line), maps)) {
38+
if (!strncmp(line, VMFLAGS, strlen(VMFLAGS))) {
39+
if (strstr(line, MSEAL_FLAGS))
40+
return true;
41+
42+
return false;
43+
}
44+
}
45+
46+
return false;
47+
}
48+
49+
FIXTURE(basic) {
50+
FILE *maps;
51+
};
52+
53+
FIXTURE_SETUP(basic)
54+
{
55+
self->maps = fopen("/proc/self/smaps", "r");
56+
if (!self->maps)
57+
SKIP(return, "Could not open /proc/self/smap, errno=%d",
58+
errno);
59+
};
60+
61+
FIXTURE_TEARDOWN(basic)
62+
{
63+
if (self->maps)
64+
fclose(self->maps);
65+
};
66+
67+
FIXTURE_VARIANT(basic)
68+
{
69+
char *name;
70+
bool sealed;
71+
};
72+
73+
FIXTURE_VARIANT_ADD(basic, vdso) {
74+
.name = "[vdso]",
75+
.sealed = true,
76+
};
77+
78+
FIXTURE_VARIANT_ADD(basic, vvar) {
79+
.name = "[vvar]",
80+
.sealed = true,
81+
};
82+
83+
FIXTURE_VARIANT_ADD(basic, vvar_vclock) {
84+
.name = "[vvar_vclock]",
85+
.sealed = true,
86+
};
87+
88+
FIXTURE_VARIANT_ADD(basic, sigpage) {
89+
.name = "[sigpage]",
90+
.sealed = true,
91+
};
92+
93+
FIXTURE_VARIANT_ADD(basic, vectors) {
94+
.name = "[vectors]",
95+
.sealed = true,
96+
};
97+
98+
FIXTURE_VARIANT_ADD(basic, uprobes) {
99+
.name = "[uprobes]",
100+
.sealed = true,
101+
};
102+
103+
FIXTURE_VARIANT_ADD(basic, stack) {
104+
.name = "[stack]",
105+
.sealed = false,
106+
};
107+
108+
TEST_F(basic, check_sealed)
109+
{
110+
if (!has_mapping(variant->name, self->maps)) {
111+
SKIP(return, "could not find the mapping, %s",
112+
variant->name);
113+
}
114+
115+
EXPECT_EQ(variant->sealed,
116+
mapping_is_sealed(variant->name, self->maps));
117+
};
118+
119+
TEST_HARNESS_MAIN

0 commit comments

Comments
 (0)