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/20250305021711.3867874-8-jeffxu@google.com Signed-off-by: Jeff Xu <jeffxu@chromium.org> Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Reviewed-by: Kees Cook <kees@kernel.org> Cc: Adhemerval Zanella <adhemerval.zanella@linaro.org> Cc: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com> Cc: Alexey Dobriyan <adobriyan@gmail.com> Cc: Andrei Vagin <avagin@gmail.com> Cc: Anna-Maria Behnsen <anna-maria@linutronix.de> Cc: Ard Biesheuvel <ardb@kernel.org> Cc: Benjamin Berg <benjamin@sipsolutions.net> Cc: Christoph Hellwig <hch@lst.de> Cc: Dave Hansen <dave.hansen@linux.intel.com> Cc: David Rientjes <rientjes@google.com> Cc: David S. Miller <davem@davemloft.net> Cc: Elliot Hughes <enh@google.com> Cc: Florian Faineli <f.fainelli@gmail.com> Cc: Greg Ungerer <gerg@kernel.org> Cc: Guenter Roeck <groeck@chromium.org> Cc: Heiko Carstens <hca@linux.ibm.com> Cc: Helge Deller <deller@gmx.de> Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com> Cc: Ingo Molnar <mingo@kernel.org> Cc: Jann Horn <jannh@google.com> Cc: Jason A. Donenfeld <jason@zx2c4.com> Cc: Johannes Berg <johannes@sipsolutions.net> Cc: Jorge Lucangeli Obes <jorgelo@chromium.org> Cc: Liam R. Howlett <Liam.Howlett@oracle.com> Cc: Linus Waleij <linus.walleij@linaro.org> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Matthew Wilcow (Oracle) <willy@infradead.org> Cc: Michael Ellerman <mpe@ellerman.id.au> Cc: Michal Hocko <mhocko@suse.com> Cc: Miguel Ojeda <ojeda@kernel.org> Cc: Mike Rapoport <mike.rapoport@gmail.com> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Pedro Falcato <pedro.falcato@gmail.com> Cc: Peter Xu <peterx@redhat.com> Cc: Randy Dunlap <rdunlap@infradead.org> Cc: Stephen Röttger <sroettger@google.com> Cc: Thomas Weißschuh <thomas.weissschuh@linutronix.de> Cc: Vlastimil Babka <vbabka@suse.cz> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
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)