drivers/misc/lkdtm/perms.c
Source file repositories/reference/linux-study-clean/drivers/misc/lkdtm/perms.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/misc/lkdtm/perms.c- Extension
.c- Size
- 7624 bytes
- Lines
- 307
- Domain
- Driver Families
- Bucket
- drivers/misc
- Inferred role
- Driver Families: implementation source
- Status
- source implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
lkdtm.hlinux/slab.hlinux/vmalloc.hlinux/mman.hlinux/uaccess.hlinux/objtool.hasm/cacheflush.hasm/sections.h
Detected Declarations
function do_nothingfunction do_overwrittenfunction do_almost_nothingfunction execute_locationfunction execute_user_locationfunction lkdtm_WRITE_ROfunction lkdtm_WRITE_RO_AFTER_INITfunction lkdtm_WRITE_KERNfunction lkdtm_WRITE_OPDfunction lkdtm_EXEC_DATAfunction lkdtm_EXEC_STACKfunction lkdtm_EXEC_KMALLOCfunction lkdtm_EXEC_VMALLOCfunction lkdtm_EXEC_RODATAfunction lkdtm_EXEC_USERSPACEfunction lkdtm_EXEC_NULLfunction lkdtm_ACCESS_USERSPACEfunction lkdtm_ACCESS_NULLfunction lkdtm_perms_init
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/*
* This is for all the tests related to validating kernel memory
* permissions: non-executable regions, non-writable regions, and
* even non-readable regions.
*/
#include "lkdtm.h"
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/mman.h>
#include <linux/uaccess.h>
#include <linux/objtool.h>
#include <asm/cacheflush.h>
#include <asm/sections.h>
/* Whether or not to fill the target memory area with do_nothing(). */
#define CODE_WRITE true
#define CODE_AS_IS false
/* How many bytes to copy to be sure we've copied enough of do_nothing(). */
#define EXEC_SIZE 64
/* This is non-const, so it will end up in the .data section. */
static u8 data_area[EXEC_SIZE];
/* This is const, so it will end up in the .rodata section. */
static const unsigned long rodata = 0xAA55AA55;
/* This is marked __ro_after_init, so it should ultimately be .rodata. */
static unsigned long ro_after_init __ro_after_init = 0x55AA5500;
/*
* This is a pointer to do_nothing() which is initialized at runtime rather
* than build time to avoid objtool IBT validation warnings caused by an
* inlined unrolled memcpy() in execute_location().
*/
static void __ro_after_init *do_nothing_ptr;
/*
* This just returns to the caller. It is designed to be copied into
* non-executable memory regions.
*/
static noinline void do_nothing(void)
{
return;
}
/* Must immediately follow do_nothing for size calculuations to work out. */
static noinline void do_overwritten(void)
{
pr_info("do_overwritten wasn't overwritten!\n");
return;
}
static noinline void do_almost_nothing(void)
{
pr_info("do_nothing was hijacked!\n");
}
static void *setup_function_descriptor(func_desc_t *fdesc, void *dst)
{
if (!have_function_descriptors())
return dst;
memcpy(fdesc, do_nothing, sizeof(*fdesc));
fdesc->addr = (unsigned long)dst;
barrier();
return fdesc;
}
static noinline __nocfi void execute_location(void *dst, bool write)
{
void (*func)(void);
func_desc_t fdesc;
pr_info("attempting ok execution at %px\n", do_nothing_ptr);
do_nothing();
if (write == CODE_WRITE) {
memcpy(dst, do_nothing_ptr, EXEC_SIZE);
flush_icache_range((unsigned long)dst,
(unsigned long)dst + EXEC_SIZE);
}
pr_info("attempting bad execution at %px\n", dst);
func = setup_function_descriptor(&fdesc, dst);
func();
pr_err("FAIL: func returned\n");
}
/*
Annotation
- Immediate include surface: `lkdtm.h`, `linux/slab.h`, `linux/vmalloc.h`, `linux/mman.h`, `linux/uaccess.h`, `linux/objtool.h`, `asm/cacheflush.h`, `asm/sections.h`.
- Detected declarations: `function do_nothing`, `function do_overwritten`, `function do_almost_nothing`, `function execute_location`, `function execute_user_location`, `function lkdtm_WRITE_RO`, `function lkdtm_WRITE_RO_AFTER_INIT`, `function lkdtm_WRITE_KERN`, `function lkdtm_WRITE_OPD`, `function lkdtm_EXEC_DATA`.
- Atlas domain: Driver Families / drivers/misc.
- Implementation status: source implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.