Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp266166pxu; Wed, 2 Dec 2020 22:34:49 -0800 (PST) X-Google-Smtp-Source: ABdhPJyJi4JQmn6+W+DIudhc/PPr7Y6QdHd1H8202AwA+cXkjh66oilM1kUdI6pS18TibyjVptCM X-Received: by 2002:a50:fe0e:: with SMTP id f14mr1513387edt.159.1606977289427; Wed, 02 Dec 2020 22:34:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606977289; cv=none; d=google.com; s=arc-20160816; b=wWaax0tlmjC2m7Zy9ZwGZfp++T5DzicBaH1IsarxipxLlLq4Y0F298Xsi80ttOXcD2 29EzhBCy7hCDSOX8wwxJssW7vvX53wMhsVT87zoXR7CXlFAo/eBmxFGK5nIFcpfgUfCW kBqajVRXm8dKxJH5gK7iMlYPRQR0bNdWmqGotbxV1TouAVp8vJbDrlNGLWqyjCQQ/IYW JM3TP5DPgobxFT5WvjgH9z7wd+Wb61ycXAAXadR94bAIsGNTpwLFnK/AWzriHa5Admkw eZc8qedWBjxrIWljyp32HdQPIk9+HucJfUc8L36xfIPqyUqGk9z2d/GpsB0/XgX2232s VM8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=K7ujTuXAbunV1s4I7PzHxYN3AKZnACs15E7rQivHvbA=; b=k//LpOn/xH8nAXL5Www8y3MYv9mNTzpvgXBsKdmq3hTCEM2ht4mMoh5muVYhwfPmaf BEltzaO/mraARC6N175hrb4S1MC5eD5FqWN+sdJcO4I3fhLxGu1V6pqRuzsgkxNK3p6K +10zkY6Bqa0C9OxoRxRabCbwACq0Oyv93tc1C7Lvt248QQZy9eFRmQ1CN9pGbFc/ZYk7 k0o7+VAw4CSV+p9lmKkINfsiWlHvYCG0nE2R0sCx2F9RDyyFTI+bTVooeYWqBd3EYa1/ tj2kZ+uYp4An9Daa9umgt6DMRKGV2A4q6FG6vchTHfvGnz9FEMnO4yyYkYXbt2/u1MRB RJDg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s30si396671edi.465.2020.12.02.22.34.27; Wed, 02 Dec 2020 22:34:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387474AbgLCGcT (ORCPT + 99 others); Thu, 3 Dec 2020 01:32:19 -0500 Received: from mail.kernel.org ([198.145.29.99]:48864 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726140AbgLCGcT (ORCPT ); Thu, 3 Dec 2020 01:32:19 -0500 From: Mike Rapoport Authentication-Results: mail.kernel.org; dkim=permerror (bad message/signature format) To: Andrew Morton Cc: Alexander Viro , Andy Lutomirski , Arnd Bergmann , Borislav Petkov , Catalin Marinas , Christopher Lameter , Dan Williams , Dave Hansen , David Hildenbrand , Elena Reshetova , "H. Peter Anvin" , Ingo Molnar , James Bottomley , "Kirill A. Shutemov" , Matthew Wilcox , Mark Rutland , Mike Rapoport , Mike Rapoport , Michael Kerrisk , Palmer Dabbelt , Paul Walmsley , Peter Zijlstra , Rick Edgecombe , Roman Gushchin , Shakeel Butt , Shuah Khan , Thomas Gleixner , Tycho Andersen , Will Deacon , linux-api@vger.kernel.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-nvdimm@lists.01.org, linux-riscv@lists.infradead.org, x86@kernel.org Subject: [PATCH v14 08/10] PM: hibernate: disable when there are active secretmem users Date: Thu, 3 Dec 2020 08:29:47 +0200 Message-Id: <20201203062949.5484-9-rppt@kernel.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201203062949.5484-1-rppt@kernel.org> References: <20201203062949.5484-1-rppt@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Mike Rapoport 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. Signed-off-by: Mike Rapoport --- include/linux/secretmem.h | 6 ++++++ kernel/power/hibernate.c | 5 ++++- mm/secretmem.c | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/include/linux/secretmem.h b/include/linux/secretmem.h index 70e7db9f94fe..907a6734059c 100644 --- a/include/linux/secretmem.h +++ b/include/linux/secretmem.h @@ -6,6 +6,7 @@ bool vma_is_secretmem(struct vm_area_struct *vma); bool page_is_secretmem(struct page *page); +bool secretmem_active(void); #else @@ -19,6 +20,11 @@ static inline bool page_is_secretmem(struct page *page) return false; } +static inline bool secretmem_active(void) +{ + return false; +} + #endif /* CONFIG_SECRETMEM */ #endif /* _LINUX_SECRETMEM_H */ diff --git a/kernel/power/hibernate.c b/kernel/power/hibernate.c index da0b41914177..559acef3fddb 100644 --- a/kernel/power/hibernate.c +++ b/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(); } /** diff --git a/mm/secretmem.c b/mm/secretmem.c index 2390901d3ff7..7236f4d9458a 100644 --- a/mm/secretmem.c +++ b/mm/secretmem.c @@ -45,6 +45,13 @@ struct secretmem_ctx { static struct cma *secretmem_cma; +static atomic_t secretmem_users; + +bool secretmem_active(void) +{ + return !!atomic_read(&secretmem_users); +} + static int secretmem_account_pages(struct page *page, gfp_t gfp, int order) { int err; @@ -179,6 +186,12 @@ static const struct vm_operations_struct secretmem_vm_ops = { .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; @@ -201,6 +214,7 @@ bool vma_is_secretmem(struct vm_area_struct *vma) } static const struct file_operations secretmem_fops = { + .release = secretmem_release, .mmap = secretmem_mmap, }; @@ -318,6 +332,7 @@ SYSCALL_DEFINE1(memfd_secret, unsigned long, flags) file->f_flags |= O_LARGEFILE; fd_install(fd, file); + atomic_inc(&secretmem_users); return fd; err_put_fd: -- 2.28.0