From patchwork Thu Jul 8 01:08:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Morton X-Patchwork-Id: 12364389 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=-13.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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 3997FC07E9C for ; Thu, 8 Jul 2021 01:08:11 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E670261CBE for ; Thu, 8 Jul 2021 01:08:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E670261CBE Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=linux-foundation.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id D03676B007D; Wed, 7 Jul 2021 21:08:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CB2776B007E; Wed, 7 Jul 2021 21:08:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B53B96B0080; Wed, 7 Jul 2021 21:08:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0215.hostedemail.com [216.40.44.215]) by kanga.kvack.org (Postfix) with ESMTP id 95C756B007D for ; Wed, 7 Jul 2021 21:08:10 -0400 (EDT) Received: from smtpin25.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id E984C274B1 for ; Thu, 8 Jul 2021 01:08:09 +0000 (UTC) X-FDA: 78337634298.25.30768E8 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf08.hostedemail.com (Postfix) with ESMTP id 4B89D30000A8 for ; Thu, 8 Jul 2021 01:08:09 +0000 (UTC) Received: by mail.kernel.org (Postfix) with ESMTPSA id 9119861CCF; Thu, 8 Jul 2021 01:08:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1625706488; bh=UwC1xKQ66RJjSS8Lm5HR4sog8UZ4rbFsKgiRsF29CTE=; h=Date:From:To:Subject:In-Reply-To:From; b=wzbeP+o1fiFEWSsU1NTumoplIXCQixQblfscc/FjNZezbwPeTzGThJIGq5rMkgQ3b BPWvQvAFV6k5E4/iNEarroCm8+ee98xxUFxs5Dtj1NgCxrMGmGdOccMKVpKUzanmrR I7jWi0OhTBiSMID3tBE7I26O8yf+p7o91dvJrRL0= Date: Wed, 07 Jul 2021 18:08:07 -0700 From: Andrew Morton To: akpm@linux-foundation.org, arnd@arndb.de, bp@alien8.de, catalin.marinas@arm.com, cl@linux.com, dan.j.williams@intel.com, dave.hansen@linux.intel.com, david@redhat.com, elena.reshetova@intel.com, guro@fb.com, hagen@jauu.net, hpa@zytor.com, James.Bottomley@HansenPartnership.com, jejb@linux.ibm.com, kirill@shutemov.name, linux-mm@kvack.org, lkp@intel.com, luto@kernel.org, mark.rutland@arm.com, mingo@redhat.com, mm-commits@vger.kernel.org, mtk.manpages@gmail.com, palmer@dabbelt.com, palmerdabbelt@google.com, paul.walmsley@sifive.com, peterz@infradead.org, rick.p.edgecombe@intel.com, rppt@linux.ibm.com, shakeelb@google.com, shuah@kernel.org, tglx@linutronix.de, torvalds@linux-foundation.org, tycho@tycho.ws, viro@zeniv.linux.org.uk, will@kernel.org, willy@infradead.org Subject: [patch 12/54] PM: hibernate: disable when there are active secretmem users Message-ID: <20210708010807.hHILhs811%akpm@linux-foundation.org> In-Reply-To: <20210707175950.eceddb86c6c555555d4730e2@linux-foundation.org> User-Agent: s-nail v14.8.16 Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=wzbeP+o1; dmarc=none; spf=pass (imf08.hostedemail.com: domain of akpm@linux-foundation.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org X-Rspamd-Server: rspam02 X-Rspam-User: nil X-Rspamd-Queue-Id: 4B89D30000A8 X-Stat-Signature: 6hgrr4dnj3gq7rrd1wazc8srewj8oyei X-HE-Tag: 1625706489-639039 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: From: Mike Rapoport Subject: PM: hibernate: disable when there are active secretmem users It is unsafe to allow saving of secretmem areas to the hibernation snapshot as they would be visible after the resume and this essentially will defeat the purpose of secret memory mappings. Prevent hibernation whenever there are active secret memory users. Link: https://lkml.kernel.org/r/20210518072034.31572-6-rppt@kernel.org Signed-off-by: Mike Rapoport Acked-by: David Hildenbrand Acked-by: James Bottomley Cc: Alexander Viro Cc: Andy Lutomirski Cc: Arnd Bergmann Cc: Borislav Petkov Cc: Catalin Marinas Cc: Christopher Lameter Cc: Dan Williams Cc: Dave Hansen Cc: David Hildenbrand Cc: Elena Reshetova Cc: Hagen Paul Pfeifer Cc: "H. Peter Anvin" Cc: Ingo Molnar Cc: James Bottomley Cc: "Kirill A. Shutemov" Cc: Mark Rutland Cc: Matthew Wilcox Cc: Michael Kerrisk Cc: Palmer Dabbelt Cc: Palmer Dabbelt Cc: Paul Walmsley Cc: Peter Zijlstra Cc: Rick Edgecombe Cc: Roman Gushchin Cc: Shakeel Butt Cc: Shuah Khan Cc: Thomas Gleixner Cc: Tycho Andersen Cc: Will Deacon Cc: kernel test robot Signed-off-by: Andrew Morton --- include/linux/secretmem.h | 6 ++++++ kernel/power/hibernate.c | 5 ++++- mm/secretmem.c | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) --- a/include/linux/secretmem.h~pm-hibernate-disable-when-there-are-active-secretmem-users +++ a/include/linux/secretmem.h @@ -30,6 +30,7 @@ static inline bool page_is_secretmem(str } bool vma_is_secretmem(struct vm_area_struct *vma); +bool secretmem_active(void); #else @@ -42,6 +43,11 @@ static inline bool page_is_secretmem(str { return false; } + +static inline bool secretmem_active(void) +{ + return false; +} #endif /* CONFIG_SECRETMEM */ --- a/kernel/power/hibernate.c~pm-hibernate-disable-when-there-are-active-secretmem-users +++ a/kernel/power/hibernate.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include "power.h" @@ -81,7 +82,9 @@ void hibernate_release(void) bool hibernation_available(void) { - return nohibernate == 0 && !security_locked_down(LOCKDOWN_HIBERNATION); + return nohibernate == 0 && + !security_locked_down(LOCKDOWN_HIBERNATION) && + !secretmem_active(); } /** --- a/mm/secretmem.c~pm-hibernate-disable-when-there-are-active-secretmem-users +++ a/mm/secretmem.c @@ -40,6 +40,13 @@ module_param_named(enable, secretmem_ena MODULE_PARM_DESC(secretmem_enable, "Enable secretmem and memfd_secret(2) system call"); +static atomic_t secretmem_users; + +bool secretmem_active(void) +{ + return !!atomic_read(&secretmem_users); +} + static vm_fault_t secretmem_fault(struct vm_fault *vmf) { struct address_space *mapping = vmf->vma->vm_file->f_mapping; @@ -94,6 +101,12 @@ static const struct vm_operations_struct .fault = secretmem_fault, }; +static int secretmem_release(struct inode *inode, struct file *file) +{ + atomic_dec(&secretmem_users); + return 0; +} + static int secretmem_mmap(struct file *file, struct vm_area_struct *vma) { unsigned long len = vma->vm_end - vma->vm_start; @@ -116,6 +129,7 @@ bool vma_is_secretmem(struct vm_area_str } static const struct file_operations secretmem_fops = { + .release = secretmem_release, .mmap = secretmem_mmap, }; @@ -202,6 +216,7 @@ SYSCALL_DEFINE1(memfd_secret, unsigned i file->f_flags |= O_LARGEFILE; fd_install(fd, file); + atomic_inc(&secretmem_users); return fd; err_put_fd: