mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH master] fs: /dev/mem: handle copy at offset 0 correctly
@ 2021-11-29  7:14 Ahmad Fatoum
  2021-12-07  9:13 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Ahmad Fatoum @ 2021-11-29  7:14 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

Despite that /dev/mem has a size of 0, the check preventing
out-of-bounds access works for /dev/mem as well, because of unsigned
wrap around. Corner case is when offset is zero. There will be no wrap
around and

  count = min(count, 0 - 0) = 0

Leading to unexpected behavior with e.g.

  memcmp -s /dev/mem 0

on systems without MMU. Fix this.

Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
---
 fs/devfs-core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/fs/devfs-core.c b/fs/devfs-core.c
index 3715e543e632..2d016e0e4861 100644
--- a/fs/devfs-core.c
+++ b/fs/devfs-core.c
@@ -573,7 +573,10 @@ ssize_t mem_copy(struct device_d *dev, void *dst, const void *src,
 	if (!dev || dev->num_resources < 1)
 		return -1;
 
-	count = size = min(count, resource_size(&dev->resource[0]) - offset);
+	if (resource_size(&dev->resource[0]) > 0 || offset != 0)
+		count = min(count, resource_size(&dev->resource[0]) - offset);
+
+	size = count;
 
 	/* no rwsize specification given. Do whatever memcpy likes best */
 	if (!rwsize) {
-- 
2.33.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


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

* Re: [PATCH master] fs: /dev/mem: handle copy at offset 0 correctly
  2021-11-29  7:14 [PATCH master] fs: /dev/mem: handle copy at offset 0 correctly Ahmad Fatoum
@ 2021-12-07  9:13 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2021-12-07  9:13 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

On Mon, Nov 29, 2021 at 08:14:54AM +0100, Ahmad Fatoum wrote:
> Despite that /dev/mem has a size of 0, the check preventing
> out-of-bounds access works for /dev/mem as well, because of unsigned
> wrap around. Corner case is when offset is zero. There will be no wrap
> around and
> 
>   count = min(count, 0 - 0) = 0
> 
> Leading to unexpected behavior with e.g.
> 
>   memcmp -s /dev/mem 0
> 
> on systems without MMU. Fix this.
> 
> Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
> ---
>  fs/devfs-core.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)

Applied, thanks

Sascha

> 
> diff --git a/fs/devfs-core.c b/fs/devfs-core.c
> index 3715e543e632..2d016e0e4861 100644
> --- a/fs/devfs-core.c
> +++ b/fs/devfs-core.c
> @@ -573,7 +573,10 @@ ssize_t mem_copy(struct device_d *dev, void *dst, const void *src,
>  	if (!dev || dev->num_resources < 1)
>  		return -1;
>  
> -	count = size = min(count, resource_size(&dev->resource[0]) - offset);
> +	if (resource_size(&dev->resource[0]) > 0 || offset != 0)
> +		count = min(count, resource_size(&dev->resource[0]) - offset);
> +
> +	size = count;
>  
>  	/* no rwsize specification given. Do whatever memcpy likes best */
>  	if (!rwsize) {
> -- 
> 2.33.0
> 
> 
> _______________________________________________
> barebox mailing list
> barebox@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


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

end of thread, other threads:[~2021-12-07  9:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-29  7:14 [PATCH master] fs: /dev/mem: handle copy at offset 0 correctly Ahmad Fatoum
2021-12-07  9:13 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox