Skip to contentSkip to conversation

iommu/omap: fix buffer overflow in debugfs

PATCH Applied Dan Carpenter 4 participants 5 messages Aug 4, 2022 – Sep 7, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iommu/omap: fix buffer overflow in debugfs

There are two issues here:

1) The "len" variable needs to be checked before the very first write. Otherwise if omap2_iommu_dump_ctx() with "bytes" less than 32 it is a buffer overflow.
2) The snprintf() function returns the number of bytes that *would* have been copied if there were enough space. But we want to know the number of bytes which were *actually* copied so use scnprintf() instead.

Fixes bd4396f09a4a ("iommu/omap: Consolidate OMAP IOMMU modules")
---
 drivers/iommu/omap-iommu-debug.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/iommu/omap-iommu-debug.c +3 -3
index a99afb5d9011..259f65291d90 100644
@@ -32,12 +32,12 @@ static inline bool is_omap_iommu_detached(struct omap_iommu *obj)
 		ssize_t bytes;						\
 		const char *str = "%20s: %08x\n";			\
 		const int maxcol = 32;					\
-		bytes = snprintf(p, maxcol, str, __stringify(name),	\
+		if (len < maxcol)					\
+			goto out;					\
+		bytes = scnprintf(p, maxcol, str, __stringify(name),	\
 				 iommu_read_reg(obj, MMU_##name));	\
 		p += bytes;						\
 		len -= bytes;						\
-		if (len < maxcol)					\
-			goto out;					\
 	} while (0)
 
 static ssize_t
Signature · 1 line
2.35.1
Reviewed-by on v1 1/1
#1 lore raw

Hi Dan,

Thank you for the patch.

Quoted 24 lines · Dan Carpenter

There are two issues here:

1) The "len" variable needs to be checked before the very first write. Otherwise if omap2_iommu_dump_ctx() with "bytes" less than 32 it is a buffer overflow.
2) The snprintf() function returns the number of bytes that *would* have been copied if there were enough space. But we want to know the number of bytes which were *actually* copied so use scnprintf() instead.

Fixes bd4396f09a4a ("iommu/omap: Consolidate OMAP IOMMU modules")
---
 drivers/iommu/omap-iommu-debug.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/iommu/omap-iommu-debug.c +0 -0
index a99afb5d9011..259f65291d90 100644
@@ -32,12 +32,12 @@ static inline bool is_omap_iommu_detached(struct omap_iommu *obj)
 		ssize_t bytes;						\
 		const char *str = "%20s: %08x\n";			\
 		const int maxcol = 32;					\

drivers/iommu/omap-iommu-debug.c static inline bool is_omap_iommu_detached(struct omap_iommu *obj) · line 41

Quoted 10 lines
drivers/iommu/omap-iommu-debug.c +3 -3
-		bytes = snprintf(p, maxcol, str, __stringify(name),	\
+		if (len < maxcol)					\
+			goto out;					\
+		bytes = scnprintf(p, maxcol, str, __stringify(name),	\
 				 iommu_read_reg(obj, MMU_##name));	\
 		p += bytes;						\
 		len -= bytes;						\
-		if (len < maxcol)					\
-			goto out;					\
 	} while (0)

This could would really benefit from being rewritten using a static table of register name and offset and a loop that iterates over it. Still, this is a valid bug fix, so

Quoted 1 line
drivers/iommu/omap-iommu-debug.c +0 -0
 static ssize_t
Signature · 3 lines
Regards,

Laurent Pinchart
reviewer Reviewed-by on v1 1/1
#2 lore raw
In reply to Dan Carpenter
Quoted 11 lines · Dan Carpenter

There are two issues here:

1) The "len" variable needs to be checked before the very first write. Otherwise if omap2_iommu_dump_ctx() with "bytes" less than 32 it is a buffer overflow.
2) The snprintf() function returns the number of bytes that *would* have been copied if there were enough space. But we want to know the number of bytes which were *actually* copied so use scnprintf() instead.

Fixes bd4396f09a4a ("iommu/omap: Consolidate OMAP IOMMU modules")

FWIW I think this has actually been broken since day one back in 14e0e6796a0d, but I'm also not inclined to care that much - 2014 is already plenty long ago.

Quoted 10 lines
---
  drivers/iommu/omap-iommu-debug.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/iommu/omap-iommu-debug.c +0 -0
index a99afb5d9011..259f65291d90 100644

drivers/iommu/omap-iommu-debug.c static inline bool is_omap_iommu_detached(struct omap_iommu *obj) · line 38

Quoted 8 lines
drivers/iommu/omap-iommu-debug.c +3 -1
@@ -32,12 +32,12 @@ static inline bool is_omap_iommu_detached(struct omap_iommu *obj)
  		ssize_t bytes;						\
  		const char *str = "%20s: %08x\n";			\
  		const int maxcol = 32;					\
-		bytes = snprintf(p, maxcol, str, __stringify(name),	\
+		if (len < maxcol)					\
+			goto out;					\
+		bytes = scnprintf(p, maxcol, str, __stringify(name),	\
  				 iommu_read_reg(obj, MMU_##name));	\

I suppose snprintf is OK in practice since none of the names are anywhere near 20 characters long anyway, but I agree it's better to be obviously foolproof. Frankly this code is all kinds of horrible anyway, and deserves ripping out and replacing with a simple seq_file, but for this fix as a fix,

Cheers,
Robin.

Quoted 7 lines
drivers/iommu/omap-iommu-debug.c +0 -2
  		p += bytes;						\
  		len -= bytes;						\
-		if (len < maxcol)					\
-			goto out;					\
  	} while (0)
 
  static ssize_t
#3 lore raw
Quoted 11 lines · Robin Murphy
Quoted 10 lines · Dan Carpenter

There are two issues here:

1) The "len" variable needs to be checked before the very first write. Otherwise if omap2_iommu_dump_ctx() with "bytes" less than 32 it is a buffer overflow.
2) The snprintf() function returns the number of bytes that *would* have been copied if there were enough space. But we want to know the number of bytes which were *actually* copied so use scnprintf() instead.

Quoted 6 lines
Quoted 1 line
Fixes bd4396f09a4a ("iommu/omap: Consolidate OMAP IOMMU modules")

FWIW I think this has actually been broken since day one back in 14e0e6796a0d, but I'm also not inclined to care that much - 2014 is already plenty long ago.

I don't know how I didn't see that. It's like my eyeballs are broken sometimes. Possibly it should be:

Fixes 14e0e6796a0d ("OMAP: iommu: add initial debugfs support")

Althought that's debatable as well...

Quoted 14 lines
Quoted 14 lines
---
  drivers/iommu/omap-iommu-debug.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/iommu/omap-iommu-debug.c +0 -1
index a99afb5d9011..259f65291d90 100644
@@ -32,12 +32,12 @@ static inline bool is_omap_iommu_detached(struct omap_iommu *obj)
  		ssize_t bytes;						\
  		const char *str = "%20s: %08x\n";			\
  		const int maxcol = 32;					\
-		bytes = snprintf(p, maxcol, str, __stringify(name),	\
Quoted 8 lines

drivers/iommu/omap-iommu-debug.c static inline bool is_omap_iommu_detached(struct omap_iommu *obj) · line 38

Quoted 4 lines
drivers/iommu/omap-iommu-debug.c +3 -0
+		if (len < maxcol)					\
+			goto out;					\
+		bytes = scnprintf(p, maxcol, str, __stringify(name),	\
  				 iommu_read_reg(obj, MMU_##name));	\

I suppose snprintf is OK in practice since none of the names are anywhere near 20 characters long anyway, but I agree it's better to be obviously foolproof.

The issue with scnprintf() vs snprintf() is not the 32 character limit. The issue is the "p - buf" math in omap2_iommu_dump_ctx(). The "p" variable is past the end of the buffer. The user decides the size of the buffer so it's a real bug.

regards,
dan carpenter

maintainer Applied
#4 lore raw
In reply to Dan Carpenter
Quoted 2 lines · Dan Carpenter
 drivers/iommu/omap-iommu-debug.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Applied, thanks.