Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp3835586pxb; Mon, 8 Feb 2021 01:03:23 -0800 (PST) X-Google-Smtp-Source: ABdhPJwFfxudVfG6b3QoGyUFOPbykFgyWKGG3/TMwcDdjGNxKpW+jYrV/2kGzJ+k/5XEhMGMps7O X-Received: by 2002:aa7:d452:: with SMTP id q18mr16303019edr.350.1612775003526; Mon, 08 Feb 2021 01:03:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612775003; cv=none; d=google.com; s=arc-20160816; b=moyrM8xk8RLXX0bsf6XSrN9phiR67Cp15JeIEbJJlwWnPhzKtUWddn4i2xNOIBTUC9 JDFSP8Xwk8BO3YONfeZOR3SRkft5MtDp+g1KTBYDaf3YwdgX13OPktpQHinm8LLBCcd0 TuJ86l78MRdfvhjSahxOhSpGy5MwQ6PL27gyIdqTtkbkX0M967h4sKY77oIfu3ln7oc8 cNjOXkzTrh3ntyZiQ6nX10da0nnrsoGIQNEbBLROSmRzH/0ohzs2HMgh+tPeKwXlzy2z o03DnB8estYGUbxSZ6jCtqDGRknZ1PQvg+0/LKWRaJKvcBSgX2AvJRwip1dC55IwiTN+ fkHg== 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 :dkim-signature; bh=0O7XokeNqoX+xH6ZdVKnwuxuz5pDlohuJAUKqUU7oMg=; b=WgtjidHCayMYRP/t5P36j3h/5BVSvJVezRbwKK0HpGQ1rtZqezfw6w5yb4IXrzgIq1 sIa5e10Se/icqQK79NnAHeusa9pSwPib0T2hsc90tr0q1I5ahGAG3SIMH2T9z7ec5Zyn +O12FuOiuXGICyLyaHU9u5AtjmEgIdEZqmtIFAu7VVBFkzZUa3rs+81NMhSg3il86xky PqIU4z8cPRp+sU6ms8BmSiX7OyZTL98zSBGzG8JRNVff7xOK4mJVQYr/ipTNbquLGNpO CyA1kN+G/eiBRv0xbdouoPuU0mCczvDvJeTZHXr7Ktwy3XnXiu5U+GA/HXGEbwCK+TKw TnGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="A0/LBZ7B"; 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 kj10si11050871ejc.166.2021.02.08.01.02.59; Mon, 08 Feb 2021 01:03:23 -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; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="A0/LBZ7B"; 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 S230400AbhBHJB6 (ORCPT + 99 others); Mon, 8 Feb 2021 04:01:58 -0500 Received: from mail.kernel.org ([198.145.29.99]:43392 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230364AbhBHIwm (ORCPT ); Mon, 8 Feb 2021 03:52:42 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 03AAC64E99; Mon, 8 Feb 2021 08:50:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1612774271; bh=xidrDhecGP6CNp1WbvEb+2by3BdO1w+m3Yqw9X4ZQKU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=A0/LBZ7BQf0jNALWaG80nd5zVj9fcSwP8saiC3x50DDP82kPSZY/A0v955jf5aFJ5 hz2nafz7mJFu9lFsiuczlq87NFtpkUSj4zutEbvS2ofRoS2L5eVd6bdpXuhL7FxQYE zVIM15Rdd2pkPsJsy5uZSgCcNeO/RtRLyT8X/hGRUt7uBpbDGVT8zLwijF7vxy7VRz skKkjmBx0CXQPXwmRH5r8kiLB/Jl0xK87y9pvlSEknsmG4LXwAZOiWDo+G+5DrSnI6 Rkm1RP0qmkIuZRxKOEV2UCav2AbqG5KbM8/fISz/y5PG+0a4m8b/7K+UP3AFVpEzpN RXtkRj3m0XfXg== From: Mike Rapoport 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 , Michal Hocko , 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, Hagen Paul Pfeifer , Palmer Dabbelt Subject: [PATCH v17 08/10] PM: hibernate: disable when there are active secretmem users Date: Mon, 8 Feb 2021 10:49:18 +0200 Message-Id: <20210208084920.2884-9-rppt@kernel.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20210208084920.2884-1-rppt@kernel.org> References: <20210208084920.2884-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 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 --- 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 fa6738e860c2..f2ae3f32a193 100644 --- a/mm/secretmem.c +++ b/mm/secretmem.c @@ -40,6 +40,13 @@ module_param_named(enable, secretmem_enable, bool, 0400); 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 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; @@ -116,6 +129,7 @@ bool vma_is_secretmem(struct vm_area_struct *vma) } static const struct file_operations secretmem_fops = { + .release = secretmem_release, .mmap = secretmem_mmap, }; @@ -212,6 +226,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