Skip to content

Commit 9cf11ce

Browse files
Mikulas PatockaMike Snitzer
authored andcommitted
dm stats: limit the number of entries
The kvmalloc function fails with a warning if the size is larger than INT_MAX. Linus said that there should be limits that prevent this warning from being hit. This commit adds the limits to the dm-stats subsystem in DM core. Signed-off-by: Mikulas Patocka <mpatocka@redhat.com> Signed-off-by: Mike Snitzer <snitzer@kernel.org>
1 parent bd504bc commit 9cf11ce

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

drivers/md/dm-stats.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ struct dm_stats_last_position {
6666
unsigned int last_rw;
6767
};
6868

69+
#define DM_STAT_MAX_ENTRIES 8388608
70+
#define DM_STAT_MAX_HISTOGRAM_ENTRIES 134217728
71+
6972
/*
7073
* A typo on the command line could possibly make the kernel run out of memory
7174
* and crash. To prevent the crash we account all used memory. We fail if we
@@ -285,6 +288,9 @@ static int dm_stats_create(struct dm_stats *stats, sector_t start, sector_t end,
285288
if (n_entries != (size_t)n_entries || !(size_t)(n_entries + 1))
286289
return -EOVERFLOW;
287290

291+
if (n_entries > DM_STAT_MAX_ENTRIES)
292+
return -EOVERFLOW;
293+
288294
shared_alloc_size = struct_size(s, stat_shared, n_entries);
289295
if ((shared_alloc_size - sizeof(struct dm_stat)) / sizeof(struct dm_stat_shared) != n_entries)
290296
return -EOVERFLOW;
@@ -297,6 +303,9 @@ static int dm_stats_create(struct dm_stats *stats, sector_t start, sector_t end,
297303
if (histogram_alloc_size / (n_histogram_entries + 1) != (size_t)n_entries * sizeof(unsigned long long))
298304
return -EOVERFLOW;
299305

306+
if ((n_histogram_entries + 1) * (size_t)n_entries > DM_STAT_MAX_HISTOGRAM_ENTRIES)
307+
return -EOVERFLOW;
308+
300309
if (!check_shared_memory(shared_alloc_size + histogram_alloc_size +
301310
num_possible_cpus() * (percpu_alloc_size + histogram_alloc_size)))
302311
return -ENOMEM;

0 commit comments

Comments
 (0)