kernel-hardening.lists.openwall.com archive mirror
 help / color / mirror / Atom feed
* [PATCH] drivers/misc: have the callers of set_memory_*() check the return value
@ 2019-12-17 19:45 Tianlin Li
  2019-12-17 23:57 ` Kees Cook
  0 siblings, 1 reply; 2+ messages in thread
From: Tianlin Li @ 2019-12-17 19:45 UTC (permalink / raw)
  To: kernel-hardening, keescook
  Cc: Arnd Bergmann, Greg Kroah-Hartman, linux-kernel, Tianlin Li

Right now several architectures allow their set_memory_*() family of  
functions to fail, but callers may not be checking the return values.
If set_memory_*() returns with an error, call-site assumptions may be
infact wrong to assume that it would either succeed or not succeed at  
all. Ideally, the failure of set_memory_*() should be passed up the 
call stack, and callers should examine the failure and deal with it. 

Need to fix the callers and add the __must_check attribute. They also 
may not provide any level of atomicity, in the sense that the memory 
protections may be left incomplete on failure. This issue likely has a 
few steps on effects architectures:
1)Have all callers of set_memory_*() helpers check the return value.
2)Add __must_check to all set_memory_*() helpers so that new uses do 
not ignore the return value.
3)Add atomicity to the calls so that the memory protections aren't left 
in a partial state.

This series is part of step 1. Make sram driver check the return value of  
set_memory_*().

Signed-off-by: Tianlin Li <tli@digitalocean.com>
---
 drivers/misc/sram-exec.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

diff --git a/drivers/misc/sram-exec.c b/drivers/misc/sram-exec.c
index d054e2842a5f..cb57ac6ab4c3 100644
--- a/drivers/misc/sram-exec.c
+++ b/drivers/misc/sram-exec.c
@@ -85,6 +85,7 @@ void *sram_exec_copy(struct gen_pool *pool, void *dst, void *src,
 	unsigned long base;
 	int pages;
 	void *dst_cpy;
+	int ret;
 
 	mutex_lock(&exec_pool_list_mutex);
 	list_for_each_entry(p, &exec_pool_list, list) {
@@ -104,16 +105,28 @@ void *sram_exec_copy(struct gen_pool *pool, void *dst, void *src,
 
 	mutex_lock(&part->lock);
 
-	set_memory_nx((unsigned long)base, pages);
-	set_memory_rw((unsigned long)base, pages);
+	ret = set_memory_nx((unsigned long)base, pages);
+	if (ret)
+		goto error_out;
+	ret = set_memory_rw((unsigned long)base, pages);
+	if (ret)
+		goto error_out;
 
 	dst_cpy = fncpy(dst, src, size);
 
-	set_memory_ro((unsigned long)base, pages);
-	set_memory_x((unsigned long)base, pages);
+	ret = set_memory_ro((unsigned long)base, pages);
+	if (ret)
+		goto error_out;
+	ret = set_memory_x((unsigned long)base, pages);
+	if (ret)
+		goto error_out;
 
 	mutex_unlock(&part->lock);
 
 	return dst_cpy;
+
+error_out:
+	mutex_unlock(&part->lock);
+	return NULL;
 }
 EXPORT_SYMBOL_GPL(sram_exec_copy);
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] drivers/misc: have the callers of set_memory_*() check the return value
  2019-12-17 19:45 [PATCH] drivers/misc: have the callers of set_memory_*() check the return value Tianlin Li
@ 2019-12-17 23:57 ` Kees Cook
  0 siblings, 0 replies; 2+ messages in thread
From: Kees Cook @ 2019-12-17 23:57 UTC (permalink / raw)
  To: Tianlin Li
  Cc: kernel-hardening, Arnd Bergmann, Greg Kroah-Hartman, linux-kernel

On Tue, Dec 17, 2019 at 01:45:28PM -0600, Tianlin Li wrote:
> Right now several architectures allow their set_memory_*() family of  
> functions to fail, but callers may not be checking the return values.
> If set_memory_*() returns with an error, call-site assumptions may be
> infact wrong to assume that it would either succeed or not succeed at  
> all. Ideally, the failure of set_memory_*() should be passed up the 
> call stack, and callers should examine the failure and deal with it. 
> 
> Need to fix the callers and add the __must_check attribute. They also 
> may not provide any level of atomicity, in the sense that the memory 
> protections may be left incomplete on failure. This issue likely has a 
> few steps on effects architectures:
> 1)Have all callers of set_memory_*() helpers check the return value.
> 2)Add __must_check to all set_memory_*() helpers so that new uses do 
> not ignore the return value.
> 3)Add atomicity to the calls so that the memory protections aren't left 
> in a partial state.
> 
> This series is part of step 1. Make sram driver check the return value of  
> set_memory_*().
> 
> Signed-off-by: Tianlin Li <tli@digitalocean.com>

Reviewed-by: Kees Cook <keescook@chromium.org>

-Kees

> ---
>  drivers/misc/sram-exec.c | 21 +++++++++++++++++----
>  1 file changed, 17 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/misc/sram-exec.c b/drivers/misc/sram-exec.c
> index d054e2842a5f..cb57ac6ab4c3 100644
> --- a/drivers/misc/sram-exec.c
> +++ b/drivers/misc/sram-exec.c
> @@ -85,6 +85,7 @@ void *sram_exec_copy(struct gen_pool *pool, void *dst, void *src,
>  	unsigned long base;
>  	int pages;
>  	void *dst_cpy;
> +	int ret;
>  
>  	mutex_lock(&exec_pool_list_mutex);
>  	list_for_each_entry(p, &exec_pool_list, list) {
> @@ -104,16 +105,28 @@ void *sram_exec_copy(struct gen_pool *pool, void *dst, void *src,
>  
>  	mutex_lock(&part->lock);
>  
> -	set_memory_nx((unsigned long)base, pages);
> -	set_memory_rw((unsigned long)base, pages);
> +	ret = set_memory_nx((unsigned long)base, pages);
> +	if (ret)
> +		goto error_out;
> +	ret = set_memory_rw((unsigned long)base, pages);
> +	if (ret)
> +		goto error_out;
>  
>  	dst_cpy = fncpy(dst, src, size);
>  
> -	set_memory_ro((unsigned long)base, pages);
> -	set_memory_x((unsigned long)base, pages);
> +	ret = set_memory_ro((unsigned long)base, pages);
> +	if (ret)
> +		goto error_out;
> +	ret = set_memory_x((unsigned long)base, pages);
> +	if (ret)
> +		goto error_out;
>  
>  	mutex_unlock(&part->lock);
>  
>  	return dst_cpy;
> +
> +error_out:
> +	mutex_unlock(&part->lock);
> +	return NULL;
>  }
>  EXPORT_SYMBOL_GPL(sram_exec_copy);
> -- 
> 2.17.1
> 

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-12-17 23:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-17 19:45 [PATCH] drivers/misc: have the callers of set_memory_*() check the return value Tianlin Li
2019-12-17 23:57 ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).