Skip to content

Commit 1e84529

Browse files
keesgregkh
authored andcommitted
lkdtm: Avoid more compiler optimizations for bad writes
[ Upstream commit 464e86b ] It seems at least Clang is able to throw away writes it knows are destined for read-only memory, which makes things like the WRITE_RO test fail, as the write gets elided. Instead, force the variable to be volatile, and make similar changes through-out other tests in an effort to avoid needing to repeat fixing these kinds of problems. Also includes pr_err() calls in failure paths so that kernel logs are more clear in the failure case. Reported-by: Prasad Sodagudi <psodagud@codeaurora.org> Suggested-by: Sami Tolvanen <samitolvanen@google.com> Fixes: 9ae113c ("lkdtm: add tests for additional page permissions") Signed-off-by: Kees Cook <keescook@chromium.org> Link: https://lore.kernel.org/r/20200625203704.317097-2-keescook@chromium.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 014b2f2 commit 1e84529

3 files changed

Lines changed: 25 additions & 15 deletions

File tree

‎drivers/misc/lkdtm/bugs.c‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,8 @@ noinline void lkdtm_CORRUPT_STACK(void)
118118
/* Use default char array length that triggers stack protection. */
119119
char data[8] __aligned(sizeof(void *));
120120

121-
__lkdtm_CORRUPT_STACK(&data);
122-
123-
pr_info("Corrupted stack containing char array ...\n");
121+
pr_info("Corrupting stack containing char array ...\n");
122+
__lkdtm_CORRUPT_STACK((void *)&data);
124123
}
125124

126125
/* Same as above but will only get a canary with -fstack-protector-strong */
@@ -131,9 +130,8 @@ noinline void lkdtm_CORRUPT_STACK_STRONG(void)
131130
unsigned long *ptr;
132131
} data __aligned(sizeof(void *));
133132

134-
__lkdtm_CORRUPT_STACK(&data);
135-
136-
pr_info("Corrupted stack containing union ...\n");
133+
pr_info("Corrupting stack containing union ...\n");
134+
__lkdtm_CORRUPT_STACK((void *)&data);
137135
}
138136

139137
void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
@@ -248,6 +246,7 @@ void lkdtm_ARRAY_BOUNDS(void)
248246

249247
kfree(not_checked);
250248
kfree(checked);
249+
pr_err("FAIL: survived array bounds overflow!\n");
251250
}
252251

253252
void lkdtm_CORRUPT_LIST_ADD(void)

‎drivers/misc/lkdtm/perms.c‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ static noinline void execute_location(void *dst, bool write)
5757
}
5858
pr_info("attempting bad execution at %px\n", func);
5959
func();
60+
pr_err("FAIL: func returned\n");
6061
}
6162

6263
static void execute_user_location(void *dst)
@@ -75,20 +76,22 @@ static void execute_user_location(void *dst)
7576
return;
7677
pr_info("attempting bad execution at %px\n", func);
7778
func();
79+
pr_err("FAIL: func returned\n");
7880
}
7981

8082
void lkdtm_WRITE_RO(void)
8183
{
82-
/* Explicitly cast away "const" for the test. */
83-
unsigned long *ptr = (unsigned long *)&rodata;
84+
/* Explicitly cast away "const" for the test and make volatile. */
85+
volatile unsigned long *ptr = (unsigned long *)&rodata;
8486

8587
pr_info("attempting bad rodata write at %px\n", ptr);
8688
*ptr ^= 0xabcd1234;
89+
pr_err("FAIL: survived bad write\n");
8790
}
8891

8992
void lkdtm_WRITE_RO_AFTER_INIT(void)
9093
{
91-
unsigned long *ptr = &ro_after_init;
94+
volatile unsigned long *ptr = &ro_after_init;
9295

9396
/*
9497
* Verify we were written to during init. Since an Oops
@@ -102,19 +105,21 @@ void lkdtm_WRITE_RO_AFTER_INIT(void)
102105

103106
pr_info("attempting bad ro_after_init write at %px\n", ptr);
104107
*ptr ^= 0xabcd1234;
108+
pr_err("FAIL: survived bad write\n");
105109
}
106110

107111
void lkdtm_WRITE_KERN(void)
108112
{
109113
size_t size;
110-
unsigned char *ptr;
114+
volatile unsigned char *ptr;
111115

112116
size = (unsigned long)do_overwritten - (unsigned long)do_nothing;
113117
ptr = (unsigned char *)do_overwritten;
114118

115119
pr_info("attempting bad %zu byte write at %px\n", size, ptr);
116-
memcpy(ptr, (unsigned char *)do_nothing, size);
120+
memcpy((void *)ptr, (unsigned char *)do_nothing, size);
117121
flush_icache_range((unsigned long)ptr, (unsigned long)(ptr + size));
122+
pr_err("FAIL: survived bad write\n");
118123

119124
do_overwritten();
120125
}
@@ -193,29 +198,32 @@ void lkdtm_ACCESS_USERSPACE(void)
193198
pr_info("attempting bad read at %px\n", ptr);
194199
tmp = *ptr;
195200
tmp += 0xc0dec0de;
201+
pr_err("FAIL: survived bad read\n");
196202

197203
pr_info("attempting bad write at %px\n", ptr);
198204
*ptr = tmp;
205+
pr_err("FAIL: survived bad write\n");
199206

200207
vm_munmap(user_addr, PAGE_SIZE);
201208
}
202209

203210
void lkdtm_ACCESS_NULL(void)
204211
{
205212
unsigned long tmp;
206-
unsigned long *ptr = (unsigned long *)NULL;
213+
volatile unsigned long *ptr = (unsigned long *)NULL;
207214

208215
pr_info("attempting bad read at %px\n", ptr);
209216
tmp = *ptr;
210217
tmp += 0xc0dec0de;
218+
pr_err("FAIL: survived bad read\n");
211219

212220
pr_info("attempting bad write at %px\n", ptr);
213221
*ptr = tmp;
222+
pr_err("FAIL: survived bad write\n");
214223
}
215224

216225
void __init lkdtm_perms_init(void)
217226
{
218227
/* Make sure we can write to __ro_after_init values during __init */
219228
ro_after_init |= 0xAA;
220-
221229
}

‎drivers/misc/lkdtm/usercopy.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,19 +304,22 @@ void lkdtm_USERCOPY_KERNEL(void)
304304
return;
305305
}
306306

307-
pr_info("attempting good copy_to_user from kernel rodata\n");
307+
pr_info("attempting good copy_to_user from kernel rodata: %px\n",
308+
test_text);
308309
if (copy_to_user((void __user *)user_addr, test_text,
309310
unconst + sizeof(test_text))) {
310311
pr_warn("copy_to_user failed unexpectedly?!\n");
311312
goto free_user;
312313
}
313314

314-
pr_info("attempting bad copy_to_user from kernel text\n");
315+
pr_info("attempting bad copy_to_user from kernel text: %px\n",
316+
vm_mmap);
315317
if (copy_to_user((void __user *)user_addr, vm_mmap,
316318
unconst + PAGE_SIZE)) {
317319
pr_warn("copy_to_user failed, but lacked Oops\n");
318320
goto free_user;
319321
}
322+
pr_err("FAIL: survived bad copy_to_user()\n");
320323

321324
free_user:
322325
vm_munmap(user_addr, PAGE_SIZE);

0 commit comments

Comments
 (0)