rkp_module_support.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <linux/rkp.h>
  2. #include <linux/module.h>
  3. #include <linux/gfp.h>
  4. #include <linux/kasan.h>
  5. #include <linux/mm.h>
  6. #include <linux/vmalloc.h>
  7. #include <linux/moduleloader.h>
  8. void *__vmalloc_node_range_for_module(unsigned long core_layout_size, unsigned long core_text_size,
  9. unsigned long align, unsigned long start, unsigned long end, gfp_t gfp_mask,
  10. pgprot_t prot, unsigned long vm_flags, int node,
  11. const void *caller);
  12. void *module_alloc_by_rkp(unsigned int core_layout_size, unsigned int core_text_size)
  13. {
  14. u64 module_alloc_end = module_alloc_base + MODULES_VSIZE;
  15. gfp_t gfp_mask = GFP_KERNEL;
  16. void *p;
  17. /* Silence the initial allocation */
  18. if (IS_ENABLED(CONFIG_ARM64_MODULE_PLTS))
  19. gfp_mask |= __GFP_NOWARN;
  20. if (IS_ENABLED(CONFIG_KASAN_GENERIC) ||
  21. IS_ENABLED(CONFIG_KASAN_SW_TAGS))
  22. /* don't exceed the static module region - see below */
  23. module_alloc_end = MODULES_END;
  24. p = __vmalloc_node_range_for_module(core_layout_size, core_text_size, MODULE_ALIGN, module_alloc_base,
  25. module_alloc_end, gfp_mask, PAGE_KERNEL, VM_DEFER_KMEMLEAK,
  26. NUMA_NO_NODE, __builtin_return_address(0));
  27. if (!p && IS_ENABLED(CONFIG_ARM64_MODULE_PLTS) &&
  28. (IS_ENABLED(CONFIG_KASAN_VMALLOC) ||
  29. (!IS_ENABLED(CONFIG_KASAN_GENERIC) &&
  30. !IS_ENABLED(CONFIG_KASAN_SW_TAGS))))
  31. /*
  32. * KASAN without KASAN_VMALLOC can only deal with module
  33. * allocations being served from the reserved module region,
  34. * since the remainder of the vmalloc region is already
  35. * backed by zero shadow pages, and punching holes into it
  36. * is non-trivial. Since the module region is not randomized
  37. * when KASAN is enabled without KASAN_VMALLOC, it is even
  38. * less likely that the module region gets exhausted, so we
  39. * can simply omit this fallback in that case.
  40. */
  41. p = __vmalloc_node_range_for_module(core_layout_size, core_text_size, MODULE_ALIGN, module_alloc_base,
  42. module_alloc_base + SZ_2G, GFP_KERNEL,
  43. PAGE_KERNEL, 0, NUMA_NO_NODE,
  44. __builtin_return_address(0));
  45. if (p && (kasan_alloc_module_shadow(p, core_layout_size, gfp_mask) < 0)) {
  46. vfree(p);
  47. return NULL;
  48. }
  49. /* Memory is intended to be executable, reset the pointer tag. */
  50. return kasan_reset_tag(p);
  51. }