Skip to content

Commit 838955f

Browse files
rpptakpm00
authored andcommitted
execmem: introduce execmem_alloc_rw()
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>
1 parent fcd90ad commit 838955f

4 files changed

Lines changed: 51 additions & 30 deletions

File tree

arch/x86/kernel/alternative.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ struct its_array its_pages;
120120

121121
static void *__its_alloc(struct its_array *pages)
122122
{
123-
void *page __free(execmem) = execmem_alloc(EXECMEM_MODULE_TEXT, PAGE_SIZE);
123+
void *page __free(execmem) = execmem_alloc_rw(EXECMEM_MODULE_TEXT, PAGE_SIZE);
124124
if (!page)
125125
return NULL;
126126

@@ -237,7 +237,6 @@ static void *its_alloc(void)
237237
if (!page)
238238
return NULL;
239239

240-
execmem_make_temp_rw(page, PAGE_SIZE);
241240
if (pages == &its_pages)
242241
set_memory_x((unsigned long)page, 1);
243242

include/linux/execmem.h

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,6 @@ enum execmem_range_flags {
6767
*/
6868
void execmem_fill_trapping_insns(void *ptr, size_t size, bool writable);
6969

70-
/**
71-
* execmem_make_temp_rw - temporarily remap region with read-write
72-
* permissions
73-
* @ptr: address of the region to remap
74-
* @size: size of the region to remap
75-
*
76-
* Remaps a part of the cached large page in the ROX cache in the range
77-
* [@ptr, @ptr + @size) as writable and not executable. The caller must
78-
* have exclusive ownership of this range and ensure nothing will try to
79-
* execute code in this range.
80-
*
81-
* Return: 0 on success or negative error code on failure.
82-
*/
83-
int execmem_make_temp_rw(void *ptr, size_t size);
84-
8570
/**
8671
* execmem_restore_rox - restore read-only-execute permissions
8772
* @ptr: address of the region to remap
@@ -95,7 +80,6 @@ int execmem_make_temp_rw(void *ptr, size_t size);
9580
*/
9681
int execmem_restore_rox(void *ptr, size_t size);
9782
#else
98-
static inline int execmem_make_temp_rw(void *ptr, size_t size) { return 0; }
9983
static inline int execmem_restore_rox(void *ptr, size_t size) { return 0; }
10084
#endif
10185

@@ -165,6 +149,28 @@ struct execmem_info *execmem_arch_setup(void);
165149
*/
166150
void *execmem_alloc(enum execmem_type type, size_t size);
167151

152+
/**
153+
* execmem_alloc_rw - allocate writable executable memory
154+
* @type: type of the allocation
155+
* @size: how many bytes of memory are required
156+
*
157+
* Allocates memory that will contain executable code, either generated or
158+
* loaded from kernel modules.
159+
*
160+
* Allocates memory that will contain data coupled with executable code,
161+
* like data sections in kernel modules.
162+
*
163+
* Forces writable permissions on the allocated memory and the caller is
164+
* responsible to manage the permissions afterwards.
165+
*
166+
* For architectures that use ROX cache the permissions will be set to R+W.
167+
* For architectures that don't use ROX cache the default permissions for @type
168+
* will be used as they must be writable.
169+
*
170+
* Return: a pointer to the allocated memory or %NULL
171+
*/
172+
void *execmem_alloc_rw(enum execmem_type type, size_t size);
173+
168174
/**
169175
* execmem_free - free executable memory
170176
* @ptr: pointer to the memory that should be freed

kernel/module/main.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,20 +1292,11 @@ static int module_memory_alloc(struct module *mod, enum mod_mem_type type)
12921292
else
12931293
execmem_type = EXECMEM_MODULE_TEXT;
12941294

1295-
ptr = execmem_alloc(execmem_type, size);
1295+
ptr = execmem_alloc_rw(execmem_type, size);
12961296
if (!ptr)
12971297
return -ENOMEM;
12981298

1299-
if (execmem_is_rox(execmem_type)) {
1300-
int err = execmem_make_temp_rw(ptr, size);
1301-
1302-
if (err) {
1303-
execmem_free(ptr);
1304-
return -ENOMEM;
1305-
}
1306-
1307-
mod->mem[type].is_rox = true;
1308-
}
1299+
mod->mem[type].is_rox = execmem_is_rox(execmem_type);
13091300

13101301
/*
13111302
* The pointer to these blocks of memory are stored on the module

mm/execmem.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ static bool execmem_cache_free(void *ptr)
336336
return true;
337337
}
338338

339-
int execmem_make_temp_rw(void *ptr, size_t size)
339+
static int execmem_force_rw(void *ptr, size_t size)
340340
{
341341
unsigned int nr = PAGE_ALIGN(size) >> PAGE_SHIFT;
342342
unsigned long addr = (unsigned long)ptr;
@@ -358,6 +358,16 @@ int execmem_restore_rox(void *ptr, size_t size)
358358
}
359359

360360
#else /* CONFIG_ARCH_HAS_EXECMEM_ROX */
361+
/*
362+
* when ROX cache is not used the permissions defined by architectures for
363+
* execmem ranges that are updated before use (e.g. EXECMEM_MODULE_TEXT) must
364+
* be writable anyway
365+
*/
366+
static inline int execmem_force_rw(void *ptr, size_t size)
367+
{
368+
return 0;
369+
}
370+
361371
static void *execmem_cache_alloc(struct execmem_range *range, size_t size)
362372
{
363373
return NULL;
@@ -387,6 +397,21 @@ void *execmem_alloc(enum execmem_type type, size_t size)
387397
return kasan_reset_tag(p);
388398
}
389399

400+
void *execmem_alloc_rw(enum execmem_type type, size_t size)
401+
{
402+
void *p __free(execmem) = execmem_alloc(type, size);
403+
int err;
404+
405+
if (!p)
406+
return NULL;
407+
408+
err = execmem_force_rw(p, size);
409+
if (err)
410+
return NULL;
411+
412+
return no_free_ptr(p);
413+
}
414+
390415
void execmem_free(void *ptr)
391416
{
392417
/*

0 commit comments

Comments
 (0)