base = kmalloc(len, GFP_KERNEL); if (!base) return;
pr_info("Allocated memory %p-%p\n", base, &base[offset * 2]);
pr_info("Attempting bad write to freed memory at %p\n",
&base[offset]);
kfree(base);
base[offset] = 0x0abcdef0; /* Attempt to notice the overwrite. */
again = kmalloc(len, GFP_KERNEL);
kfree(again); if (again != base)
pr_info("Hmm, didn't get the same memory range.\n");
}
/* *100xthesampleintervalshouldbemorethanenoughtoensureweget *aKFENCEallocationeventually.
*/
timeout = jiffies + msecs_to_jiffies(100 * kfence_sample_interval); /* *Especiallyfornon-preemptionkernels,ensuretheallocation-gate *timercancatchup:after@resched_after,everyfailedallocation *attemptyields,toensuretheallocation-gatetimerisscheduled.
*/
resched_after = jiffies + msecs_to_jiffies(kfence_sample_interval); do {
base = kmalloc(len, GFP_KERNEL); if (!base) {
pr_err("FAIL: Unable to allocate kfence memory!\n"); return;
}
if (is_kfence_address(base)) {
val = 0x12345678;
base[offset] = val;
pr_info("Value in memory before free: %x\n", base[offset]);
kfree(base);
pr_info("Attempting bad read from freed memory\n");
saw = base[offset]; if (saw != val) { /* Good! Poisoning happened, so declare a win. */
pr_info("Memory correctly poisoned (%x)\n", saw);
} else {
pr_err("FAIL: Memory was not poisoned!\n");
pr_expected_config_param(CONFIG_INIT_ON_FREE_DEFAULT_ON, "init_on_free");
} return;
}
kfree(base); if (time_after(jiffies, resched_after))
cond_resched();
} while (time_before(jiffies, timeout));
pr_err("FAIL: kfence memory never allocated!\n");
}
staticvoid lkdtm_WRITE_BUDDY_AFTER_FREE(void)
{ unsignedlong p = __get_free_page(GFP_KERNEL); if (!p) {
pr_info("Unable to allocate free page\n"); return;
}
pr_info("Writing to the buddy page before free\n");
memset((void *)p, 0x3, PAGE_SIZE);
free_page(p);
schedule();
pr_info("Attempting bad write to the buddy page after free\n");
memset((void *)p, 0x78, PAGE_SIZE); /* Attempt to notice the overwrite. */
p = __get_free_page(GFP_KERNEL);
free_page(p);
schedule();
}
staticvoid lkdtm_READ_BUDDY_AFTER_FREE(void)
{ unsignedlong p = __get_free_page(GFP_KERNEL); int saw, *val; int *base;
if (!p) {
pr_info("Unable to allocate free page\n"); return;
}
val = kmalloc(1024, GFP_KERNEL); if (!val) {
pr_info("Unable to allocate val memory.\n");
free_page(p); return;
}
base = (int *)p;
*val = 0x12345678;
base[0] = *val;
pr_info("Value in memory before free: %x\n", base[0]);
free_page(p);
pr_info("Attempting to read from freed memory\n");
saw = base[0]; if (saw != *val) { /* Good! Poisoning happened, so declare a win. */
pr_info("Memory correctly poisoned (%x)\n", saw);
} else {
pr_err("FAIL: Buddy page was not poisoned!\n");
pr_expected_config_param(CONFIG_INIT_ON_FREE_DEFAULT_ON, "init_on_free");
}
first = kmalloc(512, GFP_KERNEL); if (!first) {
pr_info("Unable to allocate 512 bytes the first time.\n"); return;
}
memset(first, 0xAB, 512);
kfree(first);
val = kmalloc(512, GFP_KERNEL); if (!val) {
pr_info("Unable to allocate 512 bytes the second time.\n"); return;
} if (val != first) {
pr_warn("Reallocation missed clobbered memory.\n");
}
if (memchr(val, 0xAB, 512) == NULL) {
pr_info("Memory appears initialized (%x, no earlier values)\n", *val);
} else {
pr_err("FAIL: Slab was not initialized\n");
pr_expected_config_param(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, "init_on_alloc");
}
kfree(val);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.