Skip to contentSkip to conversation

dma-debug: Avoid uninitialized hash traversal in mapping dumps

PATCH Superseded Avi Weiss 4 participants 5 messages Nov 15 – Nov 19 DMA mapping helpers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Avi Weiss

author
#0 lore raw

PATCH 1/1 dma-debug: Avoid uninitialized hash traversal in mapping dumps

Booting with dma_debug=off causes dma_debug_init() to return before initializing the hash buckets. The debugfs dump file is registered independently, so reading it traverses an uninitialized list and triggers a NULL pointer dereference in dump_show(). The debug_dma_dump_mappings() helper has the same unchecked traversal.

Skip both dump paths until dma_debug_initialized is set. Check the initialization state rather than global_disable so that mappings recorded before DMA debugging disables itself at runtime remain available.

With CONFIG_DMA_API_DEBUG=y and CONFIG_DEBUG_FS=y, reading the dump with dma_debug=off triggers an oops in the unpatched kernel. The patched kernel returns an empty dump, while dumping with DMA debugging enabled still returns mapping information.

Fixes 173735c346c4 ("dma-debug: fix debugfs initialization order")
---
 kernel/dma/debug.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/dma/debug.c +6 -0
index 36c1f7cbb2ae..2f487246d8ce 100644
@@ -543,6 +543,9 @@ void debug_dma_dump_mappings(struct device *dev)
 	int idx;
 	phys_addr_t cln;
 
+	if (!dma_debug_initialized)
+		return;
+
 	for (idx = 0; idx < HASH_SIZE; idx++) {
 		struct hash_bucket *bucket = &dma_entry_hash[idx];
 		struct dma_debug_entry *entry;
@@ -576,6 +579,9 @@ static int dump_show(struct seq_file *seq, void *v)
 	int idx;
 	phys_addr_t cln;
 
+	if (!dma_debug_initialized)
+		return 0;
+
 	for (idx = 0; idx < HASH_SIZE; idx++) {
 		struct hash_bucket *bucket = &dma_entry_hash[idx];
 		struct dma_debug_entry *entry;

base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa

Signature · 1 line
2.43.0
#1 lore raw
Quoted 5 lines · Avi Weiss

Booting with dma_debug=off causes dma_debug_init() to return before initializing the hash buckets. The debugfs dump file is registered independently, so reading it traverses an uninitialized list and triggers a NULL pointer dereference in dump_show(). The debug_dma_dump_mappings() helper has the same unchecked traversal.

So the debug_dma_dump_mappings() issue seems preexisting (the risk was there since its introduction in commit ac26c18bd35d, given that it doesn't check if dma_debug has been initialized), and it is not related to decoupling the debugfs init.

Quoted 4 lines

Skip both dump paths until dma_debug_initialized is set. Check the initialization state rather than global_disable so that mappings recorded before DMA debugging disables itself at runtime remain available.

I suppose this makes sense for debug_dma_dump_mappings, but I am not sure we need this for dump_show. See my next comment.

Quoted 26 lines

With CONFIG_DMA_API_DEBUG=y and CONFIG_DEBUG_FS=y, reading the dump with dma_debug=off triggers an oops in the unpatched kernel. The patched kernel returns an empty dump, while dumping with DMA debugging enabled still returns mapping information.

Fixes 173735c346c4 ("dma-debug: fix debugfs initialization order")
---
 kernel/dma/debug.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/dma/debug.c +3 -0
index 36c1f7cbb2ae..2f487246d8ce 100644
@@ -543,6 +543,9 @@ void debug_dma_dump_mappings(struct device *dev)
 	int idx;
 	phys_addr_t cln;
 
+	if (!dma_debug_initialized)
+		return;
+
 	for (idx = 0; idx < HASH_SIZE; idx++) {
 		struct hash_bucket *bucket = &dma_entry_hash[idx];
 		struct dma_debug_entry *entry;

kernel/dma/debug.c static int dump_show(struct seq_file *seq, void *v) · line 584

Quoted 6 lines
kernel/dma/debug.c +3 -0
@@ -576,6 +579,9 @@ static int dump_show(struct seq_file *seq, void *v)
 	int idx;
 	phys_addr_t cln;
 
+	if (!dma_debug_initialized)
+		return 0;
+

I think it makes better sense to skip creating debugfs files altogether when debugging is disabled, which was also the case before 15b28bbcd567.

Quoted 8 lines
kernel/dma/debug.c +0 -0
 	for (idx = 0; idx < HASH_SIZE; idx++) {
 		struct hash_bucket *bucket = &dma_entry_hash[idx];
 		struct dma_debug_entry *entry;

base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa

Signature · 1 line
2.43.0

Regards,
Anthony

reviewer
#2 lore raw
Quoted 49 lines · Anthony Iliopoulos
Quoted 5 lines · Avi Weiss

Booting with dma_debug=off causes dma_debug_init() to return before initializing the hash buckets. The debugfs dump file is registered independently, so reading it traverses an uninitialized list and triggers a NULL pointer dereference in dump_show(). The debug_dma_dump_mappings() helper has the same unchecked traversal.

So the debug_dma_dump_mappings() issue seems preexisting (the risk was there since its introduction in commit ac26c18bd35d, given that it doesn't check if dma_debug has been initialized), and it is not related to decoupling the debugfs init.

Quoted 4 lines

Skip both dump paths until dma_debug_initialized is set. Check the initialization state rather than global_disable so that mappings recorded before DMA debugging disables itself at runtime remain available.

I suppose this makes sense for debug_dma_dump_mappings, but I am not sure we need this for dump_show. See my next comment.

Quoted 29 lines

With CONFIG_DMA_API_DEBUG=y and CONFIG_DEBUG_FS=y, reading the dump with dma_debug=off triggers an oops in the unpatched kernel. The patched kernel returns an empty dump, while dumping with DMA debugging enabled still returns mapping information.

Fixes 173735c346c4 ("dma-debug: fix debugfs initialization order")
---
  kernel/dma/debug.c | 6 ++++++
  1 file changed, 6 insertions(+)
kernel/dma/debug.c +3 -0
index 36c1f7cbb2ae..2f487246d8ce 100644
@@ -543,6 +543,9 @@ void debug_dma_dump_mappings(struct device *dev)
  	int idx;
  	phys_addr_t cln;
 
+	if (!dma_debug_initialized)
+		return;
+
  	for (idx = 0; idx < HASH_SIZE; idx++) {
  		struct hash_bucket *bucket = &dma_entry_hash[idx];
  		struct dma_debug_entry *entry;
@@ -576,6 +579,9 @@ static int dump_show(struct seq_file *seq, void *v)
  	int idx;
  	phys_addr_t cln;
 
Quoted 6 lines

kernel/dma/debug.c static int dump_show(struct seq_file *seq, void *v) · line 584

Quoted 3 lines
kernel/dma/debug.c +3 -0
+	if (!dma_debug_initialized)
+		return 0;
+

I think it makes better sense to skip creating debugfs files altogether when debugging is disabled, which was also the case before 15b28bbcd567.

TBH there's probably an argument for just getting rid of debug_dma_dump_mappings() now too... Hands up anyone who's using a 20-year-old AMD GART as any kind of development machine at all, let alone for DMA API debugging of new drivers?

Anyone?

Cheers,
Robin.

Quoted 12 lines
Quoted 9 lines
kernel/dma/debug.c +0 -0
  	for (idx = 0; idx < HASH_SIZE; idx++) {
  		struct hash_bucket *bucket = &dma_entry_hash[idx];
  		struct dma_debug_entry *entry;

base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa

Signature · 1 line
2.43.0

Regards,
Anthony

maintainer
#3 lore raw
Quoted 55 lines · Robin Murphy
Quoted 45 lines · Anthony Iliopoulos
Quoted 5 lines · Avi Weiss

Booting with dma_debug=off causes dma_debug_init() to return before initializing the hash buckets. The debugfs dump file is registered independently, so reading it traverses an uninitialized list and triggers a NULL pointer dereference in dump_show(). The debug_dma_dump_mappings() helper has the same unchecked traversal.

So the debug_dma_dump_mappings() issue seems preexisting (the risk was there since its introduction in commit ac26c18bd35d, given that it doesn't check if dma_debug has been initialized), and it is not related to decoupling the debugfs init.

Quoted 4 lines

Skip both dump paths until dma_debug_initialized is set. Check the initialization state rather than global_disable so that mappings recorded before DMA debugging disables itself at runtime remain available.

I suppose this makes sense for debug_dma_dump_mappings, but I am not sure we need this for dump_show. See my next comment.

Quoted 25 lines

With CONFIG_DMA_API_DEBUG=y and CONFIG_DEBUG_FS=y, reading the dump with dma_debug=off triggers an oops in the unpatched kernel. The patched kernel returns an empty dump, while dumping with DMA debugging enabled still returns mapping information.

Fixes 173735c346c4 ("dma-debug: fix debugfs initialization order")

---
  kernel/dma/debug.c | 6 ++++++

  1 file changed, 6 insertions(+)
kernel/dma/debug.c +2 -0
index 36c1f7cbb2ae..2f487246d8ce 100644
@@ -543,6 +543,9 @@ void debug_dma_dump_mappings(struct device *dev)
       int idx;
       phys_addr_t cln;
   +    if (!dma_debug_initialized)
+        return;
+
       for (idx = 0; idx < HASH_SIZE; idx++) {
           struct hash_bucket *bucket = &dma_entry_hash[idx];
           struct dma_debug_entry *entry;
@@ -576,6 +579,9 @@ static int dump_show(struct seq_file *seq, void *v)
Quoted 8 lines

kernel/dma/debug.c static int dump_show(struct seq_file *seq, void *v) · line 584

Quoted 5 lines
kernel/dma/debug.c +2 -0
       int idx;
       phys_addr_t cln;
   +    if (!dma_debug_initialized)
+        return 0;
+

I think it makes better sense to skip creating debugfs files altogether when debugging is disabled, which was also the case before 15b28bbcd567.

Quoted 1 line

TBH there's probably an argument for just getting rid of debug_dma_dump_mappings() now too...

Agreed, remove it completely and register the debugfs entries only if (dma_debug_initialized).

Best regards

Signature · 2 lines
Marek Szyprowski, PhD
Samsung R&D Institute Poland
author
#4 lore raw

OK.
Will do.

Quoted 67 lines · Marek Szyprowski
Quoted 55 lines · Robin Murphy
Quoted 45 lines · Anthony Iliopoulos
Quoted 5 lines · Avi Weiss

Booting with dma_debug=off causes dma_debug_init() to return before initializing the hash buckets. The debugfs dump file is registered independently, so reading it traverses an uninitialized list and triggers a NULL pointer dereference in dump_show(). The debug_dma_dump_mappings() helper has the same unchecked traversal.

So the debug_dma_dump_mappings() issue seems preexisting (the risk was there since its introduction in commit ac26c18bd35d, given that it doesn't check if dma_debug has been initialized), and it is not related to decoupling the debugfs init.

Quoted 4 lines

Skip both dump paths until dma_debug_initialized is set. Check the initialization state rather than global_disable so that mappings recorded before DMA debugging disables itself at runtime remain available.

I suppose this makes sense for debug_dma_dump_mappings, but I am not sure we need this for dump_show. See my next comment.

Quoted 25 lines

With CONFIG_DMA_API_DEBUG=y and CONFIG_DEBUG_FS=y, reading the dump with dma_debug=off triggers an oops in the unpatched kernel. The patched kernel returns an empty dump, while dumping with DMA debugging enabled still returns mapping information.

Fixes 173735c346c4 ("dma-debug: fix debugfs initialization order")
---
  kernel/dma/debug.c | 6 ++++++
  1 file changed, 6 insertions(+)
kernel/dma/debug.c +2 -0
index 36c1f7cbb2ae..2f487246d8ce 100644
@@ -543,6 +543,9 @@ void debug_dma_dump_mappings(struct device *dev)
      int idx;
      phys_addr_t cln;
  +    if (!dma_debug_initialized)
+        return;
+
      for (idx = 0; idx < HASH_SIZE; idx++) {
          struct hash_bucket *bucket = &dma_entry_hash[idx];
          struct dma_debug_entry *entry;
@@ -576,6 +579,9 @@ static int dump_show(struct seq_file *seq, void *v)
Quoted 8 lines

kernel/dma/debug.c static int dump_show(struct seq_file *seq, void *v) · line 584

Quoted 5 lines
kernel/dma/debug.c +2 -0
      int idx;
      phys_addr_t cln;
  +    if (!dma_debug_initialized)
+        return 0;
+

I think it makes better sense to skip creating debugfs files altogether when debugging is disabled, which was also the case before 15b28bbcd567.

Quoted 1 line

TBH there's probably an argument for just getting rid of debug_dma_dump_mappings() now too...

Agreed, remove it completely and register the debugfs entries only if (dma_debug_initialized).

Best regards
--
Marek Szyprowski, PhD
Samsung R&D Institute Poland