Skip to content

Commit 34cfb10

Browse files
dgerlachgregkh
authored andcommitted
misc: sram-exec: Use aligned fncpy instead of memcpy
Currently the sram-exec functionality, which allows allocation of executable memory and provides an API to move code to it, is only selected in configs for the ARM architecture. Based on commit 5756e9d ("ARM: 6640/1: Thumb-2: Symbol manipulation macros for function body copying") simply copying a C function pointer address using memcpy without consideration of alignment and Thumb is unsafe on ARM platforms. The aforementioned patch introduces the fncpy macro which is a safe way to copy executable code on ARM platforms, so let's make use of that here rather than the unsafe plain memcpy that was previously used by sram_exec_copy. Now sram_exec_copy will move the code to "dst" and return an address that is guaranteed to be safely callable. In the future, architectures hoping to make use of the sram-exec functionality must define an fncpy macro just as ARM has done to guarantee or check for safe copying to executable memory before allowing the arch to select CONFIG_SRAM_EXEC. Acked-by: Tony Lindgren <tony@atomide.com> Acked-by: Russell King <rmk+kernel@armlinux.org.uk> Reviewed-by: Alexandre Belloni <alexandre.belloni@free-electrons.com> Signed-off-by: Dave Gerlach <d-gerlach@ti.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent a5061d0 commit 34cfb10

2 files changed

Lines changed: 24 additions & 11 deletions

File tree

‎drivers/misc/sram-exec.c‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/mm.h>
2020
#include <linux/sram.h>
2121

22+
#include <asm/fncpy.h>
2223
#include <asm/set_memory.h>
2324

2425
#include "sram.h"
@@ -58,20 +59,32 @@ int sram_add_protect_exec(struct sram_partition *part)
5859
* @src: Source address for the data to copy
5960
* @size: Size of copy to perform, which starting from dst, must reside in pool
6061
*
62+
* Return: Address for copied data that can safely be called through function
63+
* pointer, or NULL if problem.
64+
*
6165
* This helper function allows sram driver to act as central control location
6266
* of 'protect-exec' pools which are normal sram pools but are always set
6367
* read-only and executable except when copying data to them, at which point
6468
* they are set to read-write non-executable, to make sure no memory is
6569
* writeable and executable at the same time. This region must be page-aligned
6670
* and is checked during probe, otherwise page attribute manipulation would
67-
* not be possible.
71+
* not be possible. Care must be taken to only call the returned address as
72+
* dst address is not guaranteed to be safely callable.
73+
*
74+
* NOTE: This function uses the fncpy macro to move code to the executable
75+
* region. Some architectures have strict requirements for relocating
76+
* executable code, so fncpy is a macro that must be defined by any arch
77+
* making use of this functionality that guarantees a safe copy of exec
78+
* data and returns a safe address that can be called as a C function
79+
* pointer.
6880
*/
69-
int sram_exec_copy(struct gen_pool *pool, void *dst, void *src,
70-
size_t size)
81+
void *sram_exec_copy(struct gen_pool *pool, void *dst, void *src,
82+
size_t size)
7183
{
7284
struct sram_partition *part = NULL, *p;
7385
unsigned long base;
7486
int pages;
87+
void *dst_cpy;
7588

7689
mutex_lock(&exec_pool_list_mutex);
7790
list_for_each_entry(p, &exec_pool_list, list) {
@@ -81,10 +94,10 @@ int sram_exec_copy(struct gen_pool *pool, void *dst, void *src,
8194
mutex_unlock(&exec_pool_list_mutex);
8295

8396
if (!part)
84-
return -EINVAL;
97+
return NULL;
8598

8699
if (!addr_in_gen_pool(pool, (unsigned long)dst, size))
87-
return -EINVAL;
100+
return NULL;
88101

89102
base = (unsigned long)part->base;
90103
pages = PAGE_ALIGN(size) / PAGE_SIZE;
@@ -94,13 +107,13 @@ int sram_exec_copy(struct gen_pool *pool, void *dst, void *src,
94107
set_memory_nx((unsigned long)base, pages);
95108
set_memory_rw((unsigned long)base, pages);
96109

97-
memcpy(dst, src, size);
110+
dst_cpy = fncpy(dst, src, size);
98111

99112
set_memory_ro((unsigned long)base, pages);
100113
set_memory_x((unsigned long)base, pages);
101114

102115
mutex_unlock(&part->lock);
103116

104-
return 0;
117+
return dst_cpy;
105118
}
106119
EXPORT_SYMBOL_GPL(sram_exec_copy);

‎include/linux/sram.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
struct gen_pool;
1717

1818
#ifdef CONFIG_SRAM_EXEC
19-
int sram_exec_copy(struct gen_pool *pool, void *dst, void *src, size_t size);
19+
void *sram_exec_copy(struct gen_pool *pool, void *dst, void *src, size_t size);
2020
#else
21-
static inline int sram_exec_copy(struct gen_pool *pool, void *dst, void *src,
22-
size_t size)
21+
static inline void *sram_exec_copy(struct gen_pool *pool, void *dst, void *src,
22+
size_t size)
2323
{
24-
return -ENODEV;
24+
return NULL;
2525
}
2626
#endif /* CONFIG_SRAM_EXEC */
2727
#endif /* __LINUX_SRAM_H__ */

0 commit comments

Comments
 (0)