From patchwork Fri Jul 30 08:00:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugh Dickins X-Patchwork-Id: 12410619 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-23.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 67A3AC4338F for ; Fri, 30 Jul 2021 08:00:22 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 1789260F5C for ; Fri, 30 Jul 2021 08:00:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 1789260F5C Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id ACD9C8D0002; Fri, 30 Jul 2021 04:00:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A57118D0001; Fri, 30 Jul 2021 04:00:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 91E078D0002; Fri, 30 Jul 2021 04:00:21 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0063.hostedemail.com [216.40.44.63]) by kanga.kvack.org (Postfix) with ESMTP id 7591C8D0001 for ; Fri, 30 Jul 2021 04:00:21 -0400 (EDT) Received: from smtpin39.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 31DB48249980 for ; Fri, 30 Jul 2021 08:00:21 +0000 (UTC) X-FDA: 78418506642.39.C97C30D Received: from mail-qk1-f173.google.com (mail-qk1-f173.google.com [209.85.222.173]) by imf20.hostedemail.com (Postfix) with ESMTP id E7EE8D005863 for ; Fri, 30 Jul 2021 08:00:20 +0000 (UTC) Received: by mail-qk1-f173.google.com with SMTP id x3so8631179qkl.6 for ; Fri, 30 Jul 2021 01:00:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:in-reply-to:message-id:references :mime-version; bh=Y7HX5YFqqvflWtOAFdHp5v6MXY/OcEeQOaBTkBgwruM=; b=u8YUNlG7+lu5Is9eMpdvivobPs3T+0kbTL+S17t5SgHQK7ZI42wPzwim0p5Qj9MV7u 2bU9YmpRo2bxZv0e7RZC0bKrCIrL2hx3bFOEZ6pFcNGTALYst0tCHVstlt4tlGIRQQdX YBJkVxUcLZYwRKs5+DEOoB5RT6WXu3viAXutpX6d0PengNqn+KpyfxmeUiJktHHuDf9S wz6YT+4+y452/F7EFITeaClRAPZZk6ry5z+Bi8M8uGzUp3dTwWg0eb8/yMP9X5qjqoH0 P/262T7dGLXaXcsIuhno7w4OQvYVMVnVrxPySbDDf0a4bY6qGegqMAGl1aNtMhFP5DZ/ 8ibw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:in-reply-to:message-id :references:mime-version; bh=Y7HX5YFqqvflWtOAFdHp5v6MXY/OcEeQOaBTkBgwruM=; b=q40TV9dGj1qZSyabIwVIiDbJ3COeH5hUoCOcOsrnB7PoOUuoJqYWRUCAe9Aaz9r7aw g3SU4ApnznGhlkNaJa06oudWEQfLYdOUDEFNC1JRslxpGJx5fq5cDS/iiKbhu5FkKheb qoYnPmC8AK0Lo3+8I5G6Iaq+ty5D7mbTyc2g0Bpdboa2mrhFFO0QyHx9CH+RiiTnCL2B 58DX7Zf9k9NLAh6qHcNk8VsQjbAAaF65bEwaRfBO5WrW67OyRyKl63ZdplGzbOvxaFrV MW9pEQFBxehnBPmY1FVMj+NM+bSaL+LZl47m926yGyA8Btx7ow4p4MZtUqaHID1ZSFAY +etA== X-Gm-Message-State: AOAM533/eavc5tWyZNV7mDhT1G+n3tC829GDrJpqepLlLrQwRUkIA9OR g0j8L/k0JKbTucTEqwdMnlM6aw== X-Google-Smtp-Source: ABdhPJxX+FZqmUvFbB6LFhFizDSmok15mmV3G7aae8zfjyp+xlKCEmRsFFggV9F2XDQhme81TddiHg== X-Received: by 2002:a05:620a:13a1:: with SMTP id m1mr1061123qki.91.1627632020050; Fri, 30 Jul 2021 01:00:20 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id m80sm536727qke.98.2021.07.30.01.00.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jul 2021 01:00:18 -0700 (PDT) Date: Fri, 30 Jul 2021 01:00:16 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.anvils To: Andrew Morton cc: Hugh Dickins , Shakeel Butt , "Kirill A. Shutemov" , Yang Shi , Miaohe Lin , Mike Kravetz , Michal Hocko , Rik van Riel , Christoph Hellwig , Matthew Wilcox , "Eric W. Biederman" , Alexey Gladkov , Chris Wilson , Matthew Auld , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 12/16] tmpfs: refuse memlock when fallocated beyond i_size In-Reply-To: <2862852d-badd-7486-3a8e-c5ea9666d6fb@google.com> Message-ID: <3e5b2999-a27d-3590-46d9-80841b9427a9@google.com> References: <2862852d-badd-7486-3a8e-c5ea9666d6fb@google.com> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: E7EE8D005863 Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20161025 header.b=u8YUNlG7; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf20.hostedemail.com: domain of hughd@google.com designates 209.85.222.173 as permitted sender) smtp.mailfrom=hughd@google.com X-Stat-Signature: xxx4aywhs4rdok6esk8uxyu4awdt44ye X-HE-Tag: 1627632020-469041 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: F_MEM_LOCK is accounted by i_size, but fallocate(,FALLOC_FL_KEEP_SIZE,,) could have added many pages beyond i_size, which would also be held as Unevictable from memory. The mlock_ucounts check in shmem_fallocate() is fine, but shmem_memlock_fcntl() needs to check fallocend too. We could change F_MEM_LOCK accounting to use the max of i_size and fallocend, but fallocend is obscure: I think it's better just to refuse the F_MEM_LOCK (with EPERM) if fallocend exceeds (page-rounded) i_size. Signed-off-by: Hugh Dickins --- mm/shmem.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 6e53dabe658b..35c0f5c7120e 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2304,7 +2304,10 @@ static int shmem_memlock_fcntl(struct file *file, unsigned int cmd) inode_lock(inode); if (cmd == F_MEM_LOCK) { - if (!info->mlock_ucounts) { + if (info->fallocend > DIV_ROUND_UP(inode->i_size, PAGE_SIZE)) { + /* locking is accounted by i_size: disallow excess */ + retval = -EPERM; + } else if (!info->mlock_ucounts) { struct ucounts *ucounts = current_ucounts(); /* capability/rlimit check is down in user_shm_lock */ retval = shmem_lock(file, 1, ucounts); @@ -2854,9 +2857,10 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset, spin_unlock(&inode->i_lock); /* - * info->fallocend is only relevant when huge pages might be + * info->fallocend is mostly relevant when huge pages might be * involved: to prevent split_huge_page() freeing fallocated * pages when FALLOC_FL_KEEP_SIZE committed beyond i_size. + * But it is also checked in F_MEM_LOCK validation. */ undo_fallocend = info->fallocend; if (info->fallocend < end)