On Mon, Sep 15, 2025 at 09:36:32AM -0700, Kalesh Singh wrote:
> The VMA count limit check in do_mmap() and do_brk_flags() uses a
> strict inequality (>), which allows a process's VMA count to exceed
> the configured sysctl_max_map_count limit by one.
>
> A process with mm->map_count == sysctl_max_map_count will incorrectly
> pass this check and then exceed the limit upon allocation of a new VMA
> when its map_count is incremented.
>
> Other VMA allocation paths, such as split_vma(), already use the
> correct, inclusive (>=) comparison.

Nice spot :)

And also 'doh!'

>
> Fix this bug by changing the comparison to be inclusive in do_mmap()
> and do_brk_flags(), bringing them in line with the correct behavior
> of other allocation paths.
>
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Cc: <sta...@vger.kernel.org>
> Cc: Andrew Morton <a...@linux-foundation.org>
> Cc: David Hildenbrand <da...@redhat.com>
> Cc: "Liam R. Howlett" <liam.howl...@oracle.com>
> Cc: Lorenzo Stoakes <lorenzo.stoa...@oracle.com>
> Cc: Mike Rapoport <r...@kernel.org>
> Cc: Minchan Kim <minc...@kernel.org>
> Cc: Pedro Falcato <pfalc...@suse.de>
> Signed-off-by: Kalesh Singh <kaleshsi...@google.com>

LGTM, so:

Reviewed-by: Lorenzo Stoakes <lorenzo.stoa...@oracle.com>

> ---
>
> Chnages in v2:
>  - Fix mmap check, per Pedro
>
>  mm/mmap.c | 2 +-
>  mm/vma.c  | 2 +-
>  2 files changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/mm/mmap.c b/mm/mmap.c
> index 7306253cc3b5..e5370e7fcd8f 100644
> --- a/mm/mmap.c
> +++ b/mm/mmap.c
> @@ -374,7 +374,7 @@ unsigned long do_mmap(struct file *file, unsigned long 
> addr,
>               return -EOVERFLOW;
>
>       /* Too many mappings? */
> -     if (mm->map_count > sysctl_max_map_count)
> +     if (mm->map_count >= sysctl_max_map_count)
>               return -ENOMEM;
>
>       /*
> diff --git a/mm/vma.c b/mm/vma.c
> index 3b12c7579831..033a388bc4b1 100644
> --- a/mm/vma.c
> +++ b/mm/vma.c
> @@ -2772,7 +2772,7 @@ int do_brk_flags(struct vma_iterator *vmi, struct 
> vm_area_struct *vma,
>       if (!may_expand_vm(mm, vm_flags, len >> PAGE_SHIFT))
>               return -ENOMEM;
>
> -     if (mm->map_count > sysctl_max_map_count)
> +     if (mm->map_count >= sysctl_max_map_count)
>               return -ENOMEM;
>
>       if (security_vm_enough_memory_mm(mm, len >> PAGE_SHIFT))
> --
> 2.51.0.384.g4c02a37b29-goog
>

Reply via email to