Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * This is for all the tests related to validating kernel memory
0004  * permissions: non-executable regions, non-writable regions, and
0005  * even non-readable regions.
0006  */
0007 #include "lkdtm.h"
0008 #include <linux/slab.h>
0009 #include <linux/vmalloc.h>
0010 #include <linux/mman.h>
0011 #include <linux/uaccess.h>
0012 #include <asm/cacheflush.h>
0013 #include <asm/sections.h>
0014 
0015 /* Whether or not to fill the target memory area with do_nothing(). */
0016 #define CODE_WRITE  true
0017 #define CODE_AS_IS  false
0018 
0019 /* How many bytes to copy to be sure we've copied enough of do_nothing(). */
0020 #define EXEC_SIZE 64
0021 
0022 /* This is non-const, so it will end up in the .data section. */
0023 static u8 data_area[EXEC_SIZE];
0024 
0025 /* This is const, so it will end up in the .rodata section. */
0026 static const unsigned long rodata = 0xAA55AA55;
0027 
0028 /* This is marked __ro_after_init, so it should ultimately be .rodata. */
0029 static unsigned long ro_after_init __ro_after_init = 0x55AA5500;
0030 
0031 /*
0032  * This just returns to the caller. It is designed to be copied into
0033  * non-executable memory regions.
0034  */
0035 static noinline void do_nothing(void)
0036 {
0037     return;
0038 }
0039 
0040 /* Must immediately follow do_nothing for size calculuations to work out. */
0041 static noinline void do_overwritten(void)
0042 {
0043     pr_info("do_overwritten wasn't overwritten!\n");
0044     return;
0045 }
0046 
0047 static noinline void do_almost_nothing(void)
0048 {
0049     pr_info("do_nothing was hijacked!\n");
0050 }
0051 
0052 static void *setup_function_descriptor(func_desc_t *fdesc, void *dst)
0053 {
0054     if (!have_function_descriptors())
0055         return dst;
0056 
0057     memcpy(fdesc, do_nothing, sizeof(*fdesc));
0058     fdesc->addr = (unsigned long)dst;
0059     barrier();
0060 
0061     return fdesc;
0062 }
0063 
0064 static noinline void execute_location(void *dst, bool write)
0065 {
0066     void (*func)(void);
0067     func_desc_t fdesc;
0068     void *do_nothing_text = dereference_function_descriptor(do_nothing);
0069 
0070     pr_info("attempting ok execution at %px\n", do_nothing_text);
0071     do_nothing();
0072 
0073     if (write == CODE_WRITE) {
0074         memcpy(dst, do_nothing_text, EXEC_SIZE);
0075         flush_icache_range((unsigned long)dst,
0076                    (unsigned long)dst + EXEC_SIZE);
0077     }
0078     pr_info("attempting bad execution at %px\n", dst);
0079     func = setup_function_descriptor(&fdesc, dst);
0080     func();
0081     pr_err("FAIL: func returned\n");
0082 }
0083 
0084 static void execute_user_location(void *dst)
0085 {
0086     int copied;
0087 
0088     /* Intentionally crossing kernel/user memory boundary. */
0089     void (*func)(void);
0090     func_desc_t fdesc;
0091     void *do_nothing_text = dereference_function_descriptor(do_nothing);
0092 
0093     pr_info("attempting ok execution at %px\n", do_nothing_text);
0094     do_nothing();
0095 
0096     copied = access_process_vm(current, (unsigned long)dst, do_nothing_text,
0097                    EXEC_SIZE, FOLL_WRITE);
0098     if (copied < EXEC_SIZE)
0099         return;
0100     pr_info("attempting bad execution at %px\n", dst);
0101     func = setup_function_descriptor(&fdesc, dst);
0102     func();
0103     pr_err("FAIL: func returned\n");
0104 }
0105 
0106 static void lkdtm_WRITE_RO(void)
0107 {
0108     /* Explicitly cast away "const" for the test and make volatile. */
0109     volatile unsigned long *ptr = (unsigned long *)&rodata;
0110 
0111     pr_info("attempting bad rodata write at %px\n", ptr);
0112     *ptr ^= 0xabcd1234;
0113     pr_err("FAIL: survived bad write\n");
0114 }
0115 
0116 static void lkdtm_WRITE_RO_AFTER_INIT(void)
0117 {
0118     volatile unsigned long *ptr = &ro_after_init;
0119 
0120     /*
0121      * Verify we were written to during init. Since an Oops
0122      * is considered a "success", a failure is to just skip the
0123      * real test.
0124      */
0125     if ((*ptr & 0xAA) != 0xAA) {
0126         pr_info("%p was NOT written during init!?\n", ptr);
0127         return;
0128     }
0129 
0130     pr_info("attempting bad ro_after_init write at %px\n", ptr);
0131     *ptr ^= 0xabcd1234;
0132     pr_err("FAIL: survived bad write\n");
0133 }
0134 
0135 static void lkdtm_WRITE_KERN(void)
0136 {
0137     size_t size;
0138     volatile unsigned char *ptr;
0139 
0140     size = (unsigned long)dereference_function_descriptor(do_overwritten) -
0141            (unsigned long)dereference_function_descriptor(do_nothing);
0142     ptr = dereference_function_descriptor(do_overwritten);
0143 
0144     pr_info("attempting bad %zu byte write at %px\n", size, ptr);
0145     memcpy((void *)ptr, (unsigned char *)do_nothing, size);
0146     flush_icache_range((unsigned long)ptr, (unsigned long)(ptr + size));
0147     pr_err("FAIL: survived bad write\n");
0148 
0149     do_overwritten();
0150 }
0151 
0152 static void lkdtm_WRITE_OPD(void)
0153 {
0154     size_t size = sizeof(func_desc_t);
0155     void (*func)(void) = do_nothing;
0156 
0157     if (!have_function_descriptors()) {
0158         pr_info("XFAIL: Platform doesn't use function descriptors.\n");
0159         return;
0160     }
0161     pr_info("attempting bad %zu bytes write at %px\n", size, do_nothing);
0162     memcpy(do_nothing, do_almost_nothing, size);
0163     pr_err("FAIL: survived bad write\n");
0164 
0165     asm("" : "=m"(func));
0166     func();
0167 }
0168 
0169 static void lkdtm_EXEC_DATA(void)
0170 {
0171     execute_location(data_area, CODE_WRITE);
0172 }
0173 
0174 static void lkdtm_EXEC_STACK(void)
0175 {
0176     u8 stack_area[EXEC_SIZE];
0177     execute_location(stack_area, CODE_WRITE);
0178 }
0179 
0180 static void lkdtm_EXEC_KMALLOC(void)
0181 {
0182     u32 *kmalloc_area = kmalloc(EXEC_SIZE, GFP_KERNEL);
0183     execute_location(kmalloc_area, CODE_WRITE);
0184     kfree(kmalloc_area);
0185 }
0186 
0187 static void lkdtm_EXEC_VMALLOC(void)
0188 {
0189     u32 *vmalloc_area = vmalloc(EXEC_SIZE);
0190     execute_location(vmalloc_area, CODE_WRITE);
0191     vfree(vmalloc_area);
0192 }
0193 
0194 static void lkdtm_EXEC_RODATA(void)
0195 {
0196     execute_location(dereference_function_descriptor(lkdtm_rodata_do_nothing),
0197              CODE_AS_IS);
0198 }
0199 
0200 static void lkdtm_EXEC_USERSPACE(void)
0201 {
0202     unsigned long user_addr;
0203 
0204     user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
0205                 PROT_READ | PROT_WRITE | PROT_EXEC,
0206                 MAP_ANONYMOUS | MAP_PRIVATE, 0);
0207     if (user_addr >= TASK_SIZE) {
0208         pr_warn("Failed to allocate user memory\n");
0209         return;
0210     }
0211     execute_user_location((void *)user_addr);
0212     vm_munmap(user_addr, PAGE_SIZE);
0213 }
0214 
0215 static void lkdtm_EXEC_NULL(void)
0216 {
0217     execute_location(NULL, CODE_AS_IS);
0218 }
0219 
0220 static void lkdtm_ACCESS_USERSPACE(void)
0221 {
0222     unsigned long user_addr, tmp = 0;
0223     unsigned long *ptr;
0224 
0225     user_addr = vm_mmap(NULL, 0, PAGE_SIZE,
0226                 PROT_READ | PROT_WRITE | PROT_EXEC,
0227                 MAP_ANONYMOUS | MAP_PRIVATE, 0);
0228     if (user_addr >= TASK_SIZE) {
0229         pr_warn("Failed to allocate user memory\n");
0230         return;
0231     }
0232 
0233     if (copy_to_user((void __user *)user_addr, &tmp, sizeof(tmp))) {
0234         pr_warn("copy_to_user failed\n");
0235         vm_munmap(user_addr, PAGE_SIZE);
0236         return;
0237     }
0238 
0239     ptr = (unsigned long *)user_addr;
0240 
0241     pr_info("attempting bad read at %px\n", ptr);
0242     tmp = *ptr;
0243     tmp += 0xc0dec0de;
0244     pr_err("FAIL: survived bad read\n");
0245 
0246     pr_info("attempting bad write at %px\n", ptr);
0247     *ptr = tmp;
0248     pr_err("FAIL: survived bad write\n");
0249 
0250     vm_munmap(user_addr, PAGE_SIZE);
0251 }
0252 
0253 static void lkdtm_ACCESS_NULL(void)
0254 {
0255     unsigned long tmp;
0256     volatile unsigned long *ptr = (unsigned long *)NULL;
0257 
0258     pr_info("attempting bad read at %px\n", ptr);
0259     tmp = *ptr;
0260     tmp += 0xc0dec0de;
0261     pr_err("FAIL: survived bad read\n");
0262 
0263     pr_info("attempting bad write at %px\n", ptr);
0264     *ptr = tmp;
0265     pr_err("FAIL: survived bad write\n");
0266 }
0267 
0268 void __init lkdtm_perms_init(void)
0269 {
0270     /* Make sure we can write to __ro_after_init values during __init */
0271     ro_after_init |= 0xAA;
0272 }
0273 
0274 static struct crashtype crashtypes[] = {
0275     CRASHTYPE(WRITE_RO),
0276     CRASHTYPE(WRITE_RO_AFTER_INIT),
0277     CRASHTYPE(WRITE_KERN),
0278     CRASHTYPE(WRITE_OPD),
0279     CRASHTYPE(EXEC_DATA),
0280     CRASHTYPE(EXEC_STACK),
0281     CRASHTYPE(EXEC_KMALLOC),
0282     CRASHTYPE(EXEC_VMALLOC),
0283     CRASHTYPE(EXEC_RODATA),
0284     CRASHTYPE(EXEC_USERSPACE),
0285     CRASHTYPE(EXEC_NULL),
0286     CRASHTYPE(ACCESS_USERSPACE),
0287     CRASHTYPE(ACCESS_NULL),
0288 };
0289 
0290 struct crashtype_category perms_crashtypes = {
0291     .crashtypes = crashtypes,
0292     .len        = ARRAY_SIZE(crashtypes),
0293 };