Skip to contentSkip to conversation

tools/mm: Fix slabinfo crash when MAX_SLABS is exceeded

PATCH Superseded Marc Dionne 2 participants 2 messages Oct 29, 2024

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/mm: Fix slabinfo crash when MAX_SLABS is exceeded

From: Marc Dionne <[email protected]>

The number of slabs can easily exceed the hard coded MAX_SLABS in the slabinfo tool, causing it to overwrite memory and crash.

Increase the value of MAX_SLABS, and check if that has been exceeded for each new slab, instead of at the end when it's already too late. Also move the check for MAX_ALIASES into the loop body.

---
 tools/mm/slabinfo.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
tools/mm/slabinfo.c +5 -5
index cfaeaea71042..2c9c86d677ed 100644
@@ -21,7 +21,7 @@
 #include <regex.h>
 #include <errno.h>
 
-#define MAX_SLABS 500
+#define MAX_SLABS 1000
 #define MAX_ALIASES 500
 #define MAX_NODES 1024
 
@@ -1240,6 +1240,8 @@ static void read_slab_dir(void)
 				p--;
 			alias->ref = strdup(p);
 			alias++;
+			if (aliases > MAX_ALIASES)
+				fatal("Too many aliases\n");
 			break;
 		   case DT_DIR:
 			if (chdir(de->d_name))
@@ -1301,6 +1303,8 @@ static void read_slab_dir(void)
 			if (slab->name[0] == ':')
 				alias_targets++;
 			slab++;
+			if (slab - slabinfo > MAX_SLABS)
+				fatal("Too many slabs\n");
 			break;
 		   default :
 			fatal("Unknown file type %lx\n", de->d_type);
@@ -1310,10 +1314,6 @@ static void read_slab_dir(void)
 	slabs = slab - slabinfo;
 	actual_slabs = slabs;
 	aliases = alias - aliasinfo;
-	if (slabs > MAX_SLABS)
-		fatal("Too many slabs\n");
-	if (aliases > MAX_ALIASES)
-		fatal("Too many aliases\n");
 }
 
 static void output_slabs(void)
Signature · 1 line
2.47.0
#1 lore raw
Quoted 29 lines · Marc Dionne

From: Marc Dionne <[email protected]>

The number of slabs can easily exceed the hard coded MAX_SLABS in the slabinfo tool, causing it to overwrite memory and crash.

Increase the value of MAX_SLABS, and check if that has been exceeded for each new slab, instead of at the end when it's already too late. Also move the check for MAX_ALIASES into the loop body.

---
 tools/mm/slabinfo.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
tools/mm/slabinfo.c +1 -1
index cfaeaea71042..2c9c86d677ed 100644
@@ -21,7 +21,7 @@
 #include <regex.h>
 #include <errno.h>
 
-#define MAX_SLABS 500
+#define MAX_SLABS 1000
 #define MAX_ALIASES 500
 #define MAX_NODES 1024
 

tools/mm/slabinfo.c static void read_slab_dir(void) · line 1243

Quoted 4 lines
tools/mm/slabinfo.c +1 -0
@@ -1240,6 +1240,8 @@ static void read_slab_dir(void)
                                p--;
                        alias->ref = strdup(p);
                        alias++;
+                       if (aliases > MAX_ALIASES)

This is not quite right, needs to be alias - aliasinfo. Will send a V2.

Quoted 27 lines
tools/mm/slabinfo.c +3 -5
+                               fatal("Too many aliases\n");
                        break;
                   case DT_DIR:
                        if (chdir(de->d_name))
@@ -1301,6 +1303,8 @@ static void read_slab_dir(void)
                        if (slab->name[0] == ':')
                                alias_targets++;
                        slab++;
+                       if (slab - slabinfo > MAX_SLABS)
+                               fatal("Too many slabs\n");
                        break;
                   default :
                        fatal("Unknown file type %lx\n", de->d_type);
@@ -1310,10 +1314,6 @@ static void read_slab_dir(void)
        slabs = slab - slabinfo;
        actual_slabs = slabs;
        aliases = alias - aliasinfo;
-       if (slabs > MAX_SLABS)
-               fatal("Too many slabs\n");
-       if (aliases > MAX_ALIASES)
-               fatal("Too many aliases\n");
 }
 
 static void output_slabs(void)
--

2.47.0