Skip to content

Commit e3d37a6

Browse files
Marc Dionneakpm00
authored andcommitted
tools/mm: fix slabinfo crash when MAX_SLABS is exceeded
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. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Marc Dionne <[email protected]> Acked-by: Vlastimil Babka <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 431e106 commit e3d37a6

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

tools/mm/slabinfo.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include <regex.h>
2222
#include <errno.h>
2323

24-
#define MAX_SLABS 500
24+
#define MAX_SLABS 2000
2525
#define MAX_ALIASES 500
2626
#define MAX_NODES 1024
2727

@@ -1228,6 +1228,8 @@ static void read_slab_dir(void)
12281228
continue;
12291229
switch (de->d_type) {
12301230
case DT_LNK:
1231+
if (alias - aliasinfo == MAX_ALIASES)
1232+
fatal("Too many aliases\n");
12311233
alias->name = strdup(de->d_name);
12321234
count = readlink(de->d_name, buffer, sizeof(buffer)-1);
12331235

@@ -1242,6 +1244,8 @@ static void read_slab_dir(void)
12421244
alias++;
12431245
break;
12441246
case DT_DIR:
1247+
if (slab - slabinfo == MAX_SLABS)
1248+
fatal("Too many slabs\n");
12451249
if (chdir(de->d_name))
12461250
fatal("Unable to access slab %s\n", slab->name);
12471251
slab->name = strdup(de->d_name);
@@ -1312,10 +1316,6 @@ static void read_slab_dir(void)
13121316
slabs = slab - slabinfo;
13131317
actual_slabs = slabs;
13141318
aliases = alias - aliasinfo;
1315-
if (slabs > MAX_SLABS)
1316-
fatal("Too many slabs\n");
1317-
if (aliases > MAX_ALIASES)
1318-
fatal("Too many aliases\n");
13191319
}
13201320

13211321
static void output_slabs(void)

0 commit comments

Comments
 (0)