From patchwork Thu Feb 24 23:18:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 12759414 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E6E11C433FE for ; Thu, 24 Feb 2022 23:18:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 17BCE8D0003; Thu, 24 Feb 2022 18:18:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 104658D0001; Thu, 24 Feb 2022 18:18:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC0018D0003; Thu, 24 Feb 2022 18:18:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0138.hostedemail.com [216.40.44.138]) by kanga.kvack.org (Postfix) with ESMTP id DB9528D0001 for ; Thu, 24 Feb 2022 18:18:42 -0500 (EST) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 8222218037C6F for ; Thu, 24 Feb 2022 23:18:42 +0000 (UTC) X-FDA: 79179240084.26.2952E42 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf14.hostedemail.com (Postfix) with ESMTP id F19AB10000F for ; Thu, 24 Feb 2022 23:18:41 +0000 (UTC) Received: by mail-yb1-f201.google.com with SMTP id i6-20020a255406000000b006241e97e420so1401417ybb.5 for ; Thu, 24 Feb 2022 15:18:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=WcRk+VkLxrTwbDR3tjLPtnh/8wAdzloMGXwNgmUa7nc=; b=Xwxd9XwNKK0NF4cvAU2R8vhvu0md7aSBa2B55PIhY89XephZkblHj+PSAVvnEPIM4j TJIvC7duVYI+j1l6KOTqznhCYAbQUDtp8EpNbXjUqsUR5K9jmjjJEz1SkUFUjRw8YwCK bnYV2bSlT8fQ+WgyynH1cHM7qxDGM1Sm39XZoK8HQyPnb1f0yMWhTvxbrVXSuh3F2Xwv 3M3L85H9AkkLnx4241smzJynhC1b5AwbqMkVvRB3UeCPVW7FDOP0wdB4SAYnkqg0UKiQ UOhtxnzRUP3nOBNtj+VucN6ANz2W8hgC/+uweejp8adTZwWCRHPbq8zaE+zE6kR4B1GZ sxmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=WcRk+VkLxrTwbDR3tjLPtnh/8wAdzloMGXwNgmUa7nc=; b=a8ysoNoDk+ykww/zT5EDNYO+YB2C8kb8BPhfj4ITZ/U11YgqWOaHhhDcE3L6c5uOaF 8qnwTXP/BtOOTJKZ2wvVu13Herl/DzY3Aqmc57eHGmPasS5br323Do0zr8+oib3Yksr1 XhhGnuFxi33WVuQ3eVEPohJNIU6WhF7EuWnj9jTJsJbXGhBVI5KnLeGGpbeSkgxsIERg EjI3KOlG2bmKRUAKO15yy4TRApRXisQJfJi+/heIJc3nDcWOhfyLZ6mMe3vvrNsfq4nv Wf/DtSy41MRcFEnP8bvmsNBIPZXh6dsQ1mD7Dm/hkeMXIGNOQa9pkOinmphoAJNHc1GE RWLw== X-Gm-Message-State: AOAM532hGmr32zbZww64wof/fc/1R2Zdo/3G7uH8w3z6KcCxLfUhuMVB Xjx9Muv74cmwFi7i/33m0f39v2eEExc= X-Google-Smtp-Source: ABdhPJyJCBq67He+0PoQK/vX8Mc19UBZmel2zFsmVtxgnReH+P7sQUqvJV9mTsvwW3i/cmtEnDhi4N6BT1Q= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:200:1c5c:1806:c1bc:a422]) (user=surenb job=sendgmr) by 2002:a81:8d1:0:b0:2d0:ebfe:210d with SMTP id 200-20020a8108d1000000b002d0ebfe210dmr4834089ywi.486.1645744721199; Thu, 24 Feb 2022 15:18:41 -0800 (PST) Date: Thu, 24 Feb 2022 15:18:33 -0800 In-Reply-To: <20220224231834.1481408-1-surenb@google.com> Message-Id: <20220224231834.1481408-2-surenb@google.com> Mime-Version: 1.0 References: <20220224231834.1481408-1-surenb@google.com> X-Mailer: git-send-email 2.35.1.574.g5d30c73bfb-goog Subject: [PATCH v3 2/3] mm: prevent vm_area_struct::anon_name refcount saturation From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: ccross@google.com, sumit.semwal@linaro.org, mhocko@suse.com, dave.hansen@intel.com, keescook@chromium.org, willy@infradead.org, kirill.shutemov@linux.intel.com, vbabka@suse.cz, hannes@cmpxchg.org, ebiederm@xmission.com, brauner@kernel.org, legion@kernel.org, ran.xiaokai@zte.com.cn, sashal@kernel.org, chris.hyser@oracle.com, dave@stgolabs.net, pcc@google.com, caoxiaofeng@yulong.com, david@redhat.com, gorcunov@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@android.com, surenb@google.com X-Stat-Signature: mj9dk3jc1zb4i6te8bsw6biz48anqasf X-Rspam-User: Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Xwxd9XwN; spf=pass (imf14.hostedemail.com: domain of 3URIYYgYKCOwgifSbPUccUZS.QcaZWbil-aaYjOQY.cfU@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3URIYYgYKCOwgifSbPUccUZS.QcaZWbil-aaYjOQY.cfU@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: F19AB10000F X-HE-Tag: 1645744721-329263 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: A deep process chain with many vmas could grow really high. With default sysctl_max_map_count (64k) and default pid_max (32k) the max number of vmas in the system is 2147450880 and the refcounter has headroom of 1073774592 before it reaches REFCOUNT_SATURATED (3221225472). Therefore it's unlikely that an anonymous name refcounter will overflow with these defaults. Currently the max for pid_max is PID_MAX_LIMIT (4194304) and for sysctl_max_map_count it's INT_MAX (2147483647). In this configuration anon_vma_name refcount overflow becomes theoretically possible (that still require heavy sharing of that anon_vma_name between processes). kref refcounting interface used in anon_vma_name structure will detect a counter overflow when it reaches REFCOUNT_SATURATED value but will only generate a warning and freeze the ref counter. This would lead to the refcounted object never being freed. A determined attacker could leak memory like that but it would be rather expensive and inefficient way to do so. To ensure anon_vma_name refcount does not overflow, stop anon_vma_name sharing when the refcount reaches REFCOUNT_MAX (2147483647), which still leaves INT_MAX/2 (1073741823) values before the counter reaches REFCOUNT_SATURATED. This should provide enough headroom for raising the refcounts temporarily. Link: https://lkml.kernel.org/r/20220223153613.835563-2-surenb@google.com Signed-off-by: Suren Baghdasaryan Suggested-by: Michal Hocko Acked-by: Michal Hocko --- changes in v3: - Amended description with what happens when refcount saturates, per Michal Hocko - Added Ack, per Michal Hocko include/linux/mm_inline.h | 18 ++++++++++++++---- mm/madvise.c | 3 +-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index dd3accaa4e6d..cf90b1fa2c60 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -161,15 +161,25 @@ static inline void anon_vma_name_put(struct anon_vma_name *anon_name) kref_put(&anon_name->kref, anon_vma_name_free); } +static inline +struct anon_vma_name *anon_vma_name_reuse(struct anon_vma_name *anon_name) +{ + /* Prevent anon_name refcount saturation early on */ + if (kref_read(&anon_name->kref) < REFCOUNT_MAX) { + anon_vma_name_get(anon_name); + return anon_name; + + } + return anon_vma_name_alloc(anon_name->name); +} + static inline void dup_anon_vma_name(struct vm_area_struct *orig_vma, struct vm_area_struct *new_vma) { struct anon_vma_name *anon_name = anon_vma_name(orig_vma); - if (anon_name) { - anon_vma_name_get(anon_name); - new_vma->anon_name = anon_name; - } + if (anon_name) + new_vma->anon_name = anon_vma_name_reuse(anon_name); } static inline void free_anon_vma_name(struct vm_area_struct *vma) diff --git a/mm/madvise.c b/mm/madvise.c index 081b1cded21e..1f2693dccf7b 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -113,8 +113,7 @@ static int replace_anon_vma_name(struct vm_area_struct *vma, if (anon_vma_name_eq(orig_name, anon_name)) return 0; - anon_vma_name_get(anon_name); - vma->anon_name = anon_name; + vma->anon_name = anon_vma_name_reuse(anon_name); anon_vma_name_put(orig_name); return 0;