]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
execmem: introduce execmem_alloc_rw()
authorMike Rapoport (Microsoft) <rppt@kernel.org>
Sun, 13 Jul 2025 07:17:24 +0000 (10:17 +0300)
committerAndrew Morton <akpm@linux-foundation.org>
Sat, 2 Aug 2025 19:06:11 +0000 (12:06 -0700)
Some callers of execmem_alloc() require the memory to be temporarily
writable even when it is allocated from ROX cache.  These callers use
execemem_make_temp_rw() right after the call to execmem_alloc().

Wrap this sequence in execmem_alloc_rw() API.

Link: https://lkml.kernel.org/r/20250713071730.4117334-3-rppt@kernel.org
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Reviewed-by: Daniel Gomez <da.gomez@samsung.com>
Reviewed-by: Petr Pavlu <petr.pavlu@suse.com>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: Steven Rostedt (Google) <rostedt@goodmis.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
arch/x86/kernel/alternative.c
include/linux/execmem.h
kernel/module/main.c
mm/execmem.c

index ea1d984166cdd404014a7896009eced253d47a14..526a5fef93abcf21e757b8dc597c7367dc58d5ac 100644 (file)
@@ -120,7 +120,7 @@ struct its_array its_pages;
 
 static void *__its_alloc(struct its_array *pages)
 {
-       void *page __free(execmem) = execmem_alloc(EXECMEM_MODULE_TEXT, PAGE_SIZE);
+       void *page __free(execmem) = execmem_alloc_rw(EXECMEM_MODULE_TEXT, PAGE_SIZE);
        if (!page)
                return NULL;
 
@@ -237,7 +237,6 @@ static void *its_alloc(void)
        if (!page)
                return NULL;
 
-       execmem_make_temp_rw(page, PAGE_SIZE);
        if (pages == &its_pages)
                set_memory_x((unsigned long)page, 1);
 
index 734fbe83d98e6948d5e1c2feb1619bcfbf4f2a49..8b61b05da7d57a1e824227a26241a0966ee8970a 100644 (file)
@@ -67,21 +67,6 @@ enum execmem_range_flags {
  */
 void execmem_fill_trapping_insns(void *ptr, size_t size, bool writable);
 
-/**
- * execmem_make_temp_rw - temporarily remap region with read-write
- *                       permissions
- * @ptr:       address of the region to remap
- * @size:      size of the region to remap
- *
- * Remaps a part of the cached large page in the ROX cache in the range
- * [@ptr, @ptr + @size) as writable and not executable. The caller must
- * have exclusive ownership of this range and ensure nothing will try to
- * execute code in this range.
- *
- * Return: 0 on success or negative error code on failure.
- */
-int execmem_make_temp_rw(void *ptr, size_t size);
-
 /**
  * execmem_restore_rox - restore read-only-execute permissions
  * @ptr:       address of the region to remap
@@ -95,7 +80,6 @@ int execmem_make_temp_rw(void *ptr, size_t size);
  */
 int execmem_restore_rox(void *ptr, size_t size);
 #else
-static inline int execmem_make_temp_rw(void *ptr, size_t size) { return 0; }
 static inline int execmem_restore_rox(void *ptr, size_t size) { return 0; }
 #endif
 
@@ -165,6 +149,28 @@ struct execmem_info *execmem_arch_setup(void);
  */
 void *execmem_alloc(enum execmem_type type, size_t size);
 
+/**
+ * execmem_alloc_rw - allocate writable executable memory
+ * @type: type of the allocation
+ * @size: how many bytes of memory are required
+ *
+ * Allocates memory that will contain executable code, either generated or
+ * loaded from kernel modules.
+ *
+ * Allocates memory that will contain data coupled with executable code,
+ * like data sections in kernel modules.
+ *
+ * Forces writable permissions on the allocated memory and the caller is
+ * responsible to manage the permissions afterwards.
+ *
+ * For architectures that use ROX cache the permissions will be set to R+W.
+ * For architectures that don't use ROX cache the default permissions for @type
+ * will be used as they must be writable.
+ *
+ * Return: a pointer to the allocated memory or %NULL
+ */
+void *execmem_alloc_rw(enum execmem_type type, size_t size);
+
 /**
  * execmem_free - free executable memory
  * @ptr: pointer to the memory that should be freed
index 413ac6ea37021bc8ae260f624ca2745ed85333fc..d009326ef7bb813cb6e8cf0d20230ee15eb16064 100644 (file)
@@ -1292,20 +1292,11 @@ static int module_memory_alloc(struct module *mod, enum mod_mem_type type)
        else
                execmem_type = EXECMEM_MODULE_TEXT;
 
-       ptr = execmem_alloc(execmem_type, size);
+       ptr = execmem_alloc_rw(execmem_type, size);
        if (!ptr)
                return -ENOMEM;
 
-       if (execmem_is_rox(execmem_type)) {
-               int err = execmem_make_temp_rw(ptr, size);
-
-               if (err) {
-                       execmem_free(ptr);
-                       return -ENOMEM;
-               }
-
-               mod->mem[type].is_rox = true;
-       }
+       mod->mem[type].is_rox = execmem_is_rox(execmem_type);
 
        /*
         * The pointer to these blocks of memory are stored on the module
index aac211bc88c570398cb242e18e6728337ab5a3e7..d0bf0123bce4c40c1b91dc1804b0cf2d13d05104 100644 (file)
@@ -336,7 +336,7 @@ static bool execmem_cache_free(void *ptr)
        return true;
 }
 
-int execmem_make_temp_rw(void *ptr, size_t size)
+static int execmem_force_rw(void *ptr, size_t size)
 {
        unsigned int nr = PAGE_ALIGN(size) >> PAGE_SHIFT;
        unsigned long addr = (unsigned long)ptr;
@@ -358,6 +358,16 @@ int execmem_restore_rox(void *ptr, size_t size)
 }
 
 #else /* CONFIG_ARCH_HAS_EXECMEM_ROX */
+/*
+ * when ROX cache is not used the permissions defined by architectures for
+ * execmem ranges that are updated before use (e.g. EXECMEM_MODULE_TEXT) must
+ * be writable anyway
+ */
+static inline int execmem_force_rw(void *ptr, size_t size)
+{
+       return 0;
+}
+
 static void *execmem_cache_alloc(struct execmem_range *range, size_t size)
 {
        return NULL;
@@ -387,6 +397,21 @@ void *execmem_alloc(enum execmem_type type, size_t size)
        return kasan_reset_tag(p);
 }
 
+void *execmem_alloc_rw(enum execmem_type type, size_t size)
+{
+       void *p __free(execmem) = execmem_alloc(type, size);
+       int err;
+
+       if (!p)
+               return NULL;
+
+       err = execmem_force_rw(p, size);
+       if (err)
+               return NULL;
+
+       return no_free_ptr(p);
+}
+
 void execmem_free(void *ptr)
 {
        /*