#include #include #include #include static long lib_addr; module_param(lib_addr, long, 0); MODULE_PARM_DESC(lib_addr, "lib_addr"); static unsigned long parse_elf64(unsigned long start_loc) { Elf64_Ehdr * ehdr; int ret = 0; ehdr = kmalloc(sizeof(Elf64_Ehdr), GFP_KERNEL); if (copy_from_user((void *)ehdr, (void *) start_loc, sizeof(Elf64_Ehdr))) { printk("cannot get Elf64_Ehdr from " "start_loc %lx\n", start_loc); goto out; } if (ehdr->e_ident[EI_CLASS] != ELFCLASS64) { printk("EI_CLASS of Elf64_Hdr is incorrect! %d\n", ehdr->e_ident[EI_CLASS]); goto out; } if (ehdr->e_type != ET_DYN) { printk(KERN_INFO "LPA: " "%s, line %d: Unexpected e_type %u parsing ELF\n", __FUNCTION__, __LINE__, ehdr->e_type); goto out; } ret = ehdr->e_ident[EI_CLASS]; printk(KERN_INFO "Elf class from Ehdr is %d\n", ret); out: return ret; } static unsigned long parse_elf32(unsigned long start_loc) { Elf32_Ehdr ehdr; int ret = 0; if (copy_from_user(&ehdr, (void *) start_loc, sizeof (ehdr))) goto out; if (ehdr.e_ident[EI_CLASS] != ELFCLASS32) goto out; if (ehdr.e_type != ET_DYN) { printk(KERN_INFO "%s, line %d: Unexpected e_type %u parsing ELF\n", __FUNCTION__, __LINE__, ehdr.e_type); goto out; } ret = ehdr.e_ident[EI_CLASS]; printk(KERN_INFO "Elf class from Ehdr is %d\n", ret); out: return ret; } int find_ehdr(unsigned long start_loc) { int ret = 0; if (!(ret = parse_elf32(start_loc))) ret = parse_elf64(start_loc); return ret; } int __init init_module(void) { if (!(find_ehdr(lib_addr))) { printk(KERN_INFO "uaccess test failed\n"); return -1; } printk(KERN_INFO "uaccess test succeeded\n"); return 0; } void __exit cleanup_module(void) { } MODULE_LICENSE("GPL");