lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20220222110312.q7eaepfph2tyjq3e@black.fi.intel.com>
Date:   Tue, 22 Feb 2022 14:03:12 +0300
From:   "Kirill A. Shutemov" <kirill.shutemov@...ux.intel.com>
To:     Dave Hansen <dave.hansen@...el.com>
Cc:     tglx@...utronix.de, mingo@...hat.com, bp@...en8.de,
        luto@...nel.org, peterz@...radead.org,
        sathyanarayanan.kuppuswamy@...ux.intel.com, aarcange@...hat.com,
        ak@...ux.intel.com, dan.j.williams@...el.com, david@...hat.com,
        hpa@...or.com, jgross@...e.com, jmattson@...gle.com,
        joro@...tes.org, jpoimboe@...hat.com, knsathya@...nel.org,
        pbonzini@...hat.com, sdeep@...are.com, seanjc@...gle.com,
        tony.luck@...el.com, vkuznets@...hat.com, wanpengli@...cent.com,
        x86@...nel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCHv3 02/32] x86/coco: Add API to handle encryption mask

On Tue, Feb 22, 2022 at 01:28:56AM +0300, Kirill A. Shutemov wrote:
> On Mon, Feb 21, 2022 at 11:28:16AM -0800, Dave Hansen wrote:
> > I'm just a bit confused why *this* was chosen as the cc_whatever() hook.
> >  Just like the mask function, it has one spot where it gets used:
> > 
> > +#define pgprot_encrypted(prot)	__pgprot(cc_mkenc(pgprot_val(prot)))
> > +#define pgprot_decrypted(prot)	__pgprot(cc_mkdec(pgprot_val(prot)))
> > 
> > So, why bother having another level of abstraction?
> > 
> > Why don't we just have:
> > 
> > 	pgprot_t cc_mkenc(pgprot prot)
> > 	pgprot_t cc_mkenc(pgprot prot)
> > 
> > and *no* pgprot_{en,de}crypted()?
> 
> Okay. Let me try this.

Below it how it would look like.

I don't like it, especially, tdx_enc_status_changed() case. There are more
cases like this in attestation code.

I would rather make cc_mkenc()/cc_mkdec() to operate on u64 (or
phys_addr_t?) while pgprot_encrypted()/pgprot_decrypted() cover pgprot_t.
It also makes set_memory cleaner:

	cpa.mask_set = __pgprot(enc ? cc_mkenc(0) : cc_mkdec(0));
	cpa.mask_clr = __pgprot(enc ? cc_mkdec(0) : cc_mkenc(0));

Opinions?

diff --git a/arch/x86/coco/tdx.c b/arch/x86/coco/tdx.c
index fb32c271fe34..bf5ba7d5b736 100644
--- a/arch/x86/coco/tdx.c
+++ b/arch/x86/coco/tdx.c
@@ -514,8 +514,8 @@ static int tdx_enc_status_changed(unsigned long vaddr, int numpages, bool enc)
 		return -EINVAL;
 
 	if (!enc) {
-		start = cc_mkenc(start);
-		end = cc_mkenc(end);
+		start |= pgprot_val(cc_mkenc(__pgprot(0)));
+		end |= pgprot_val(cc_mkenc(__pgprot(0)));
 	}
 
 	/*
diff --git a/arch/x86/include/asm/coco.h b/arch/x86/include/asm/coco.h
index 802d87d08e31..fba1dd2ef319 100644
--- a/arch/x86/include/asm/coco.h
+++ b/arch/x86/include/asm/coco.h
@@ -14,13 +14,18 @@ enum cc_vendor {
 void cc_init(enum cc_vendor, u64 mask);
 
 #ifdef CONFIG_ARCH_HAS_CC_PLATFORM
-u64 cc_get_mask(bool enc);
-u64 cc_mkenc(u64 val);
-u64 cc_mkdec(u64 val);
+pgprot_t cc_mkenc(pgprot_t prot);
+pgprot_t cc_mkdec(pgprot_t prot);
 #else
-#define cc_get_mask(enc)	0
-#define cc_mkenc(val)		(val)
-#define cc_mkdec(val)		(val)
+static inline pgprot_t cc_mkenc(pgprot_t prot)
+{
+	return prot;
+}
+
+static inline pgprot_t cc_mkdec(pgprot_t prot)
+{
+	return prot;
+}
 #endif
 
 #endif /* _ASM_X86_COCO_H */
diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h
index 62ab07e24aef..8cba0d258e49 100644
--- a/arch/x86/include/asm/pgtable.h
+++ b/arch/x86/include/asm/pgtable.h
@@ -36,8 +36,8 @@ void ptdump_walk_user_pgd_level_checkwx(void);
 /*
  * Macros to add or remove encryption attribute
  */
-#define pgprot_encrypted(prot)	__pgprot(cc_mkenc(pgprot_val(prot)))
-#define pgprot_decrypted(prot)	__pgprot(cc_mkdec(pgprot_val(prot)))
+#define pgprot_encrypted(prot)	cc_mkenc(prot)
+#define pgprot_decrypted(prot)	cc_mkdec(prot)
 
 #ifdef CONFIG_DEBUG_WX
 #define debug_checkwx()		ptdump_walk_pgd_level_checkwx()
diff --git a/arch/x86/kernel/cc_platform.c b/arch/x86/kernel/cc_platform.c
index 12a34e80ef50..d13a034e9a21 100644
--- a/arch/x86/kernel/cc_platform.c
+++ b/arch/x86/kernel/cc_platform.c
@@ -12,6 +12,7 @@
 
 #include <asm/coco.h>
 #include <asm/processor.h>
+#include <asm/pgtable_types.h>
 
 static u64 cc_mask;
 static enum cc_vendor cc_vendor;
@@ -93,39 +94,27 @@ bool cc_platform_has(enum cc_attr attr)
 }
 EXPORT_SYMBOL_GPL(cc_platform_has);
 
-u64 cc_get_mask(bool enc)
+pgprot_t cc_mkenc(pgprot_t prot)
 {
 	switch (cc_vendor) {
 	case CC_VENDOR_AMD:
-		return enc ? cc_mask : 0;
+		return __pgprot(pgprot_val(prot) | cc_mask);
 	case CC_VENDOR_INTEL:
-		return enc ? 0 : cc_mask;
+		return __pgprot(pgprot_val(prot) & ~cc_mask);
 	default:
-		return 0;
+		return prot;
 	}
 }
 
-u64 cc_mkenc(u64 val)
+pgprot_t cc_mkdec(pgprot_t prot)
 {
 	switch (cc_vendor) {
 	case CC_VENDOR_AMD:
-		return val | cc_mask;
+		return __pgprot(pgprot_val(prot) & ~cc_mask);
 	case CC_VENDOR_INTEL:
-		return val & ~cc_mask;
+		return __pgprot(pgprot_val(prot) | cc_mask);
 	default:
-		return val;
-	}
-}
-
-u64 cc_mkdec(u64 val)
-{
-	switch (cc_vendor) {
-	case CC_VENDOR_AMD:
-		return val & ~cc_mask;
-	case CC_VENDOR_INTEL:
-		return val | cc_mask;
-	default:
-		return val;
+		return prot;
 	}
 }
 EXPORT_SYMBOL_GPL(cc_mkdec);
diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c
index 65a27dec095f..1ef63ea377dd 100644
--- a/arch/x86/mm/pat/set_memory.c
+++ b/arch/x86/mm/pat/set_memory.c
@@ -1999,8 +1999,8 @@ static int __set_memory_enc_pgtable(unsigned long addr, int numpages, bool enc)
 	memset(&cpa, 0, sizeof(cpa));
 	cpa.vaddr = &addr;
 	cpa.numpages = numpages;
-	cpa.mask_set = __pgprot(cc_get_mask(enc));
-	cpa.mask_clr = __pgprot(cc_get_mask(!enc));
+	cpa.mask_set = enc ? cc_mkenc(__pgprot(0)) : cc_mkdec(__pgprot(0));
+	cpa.mask_clr = enc ? cc_mkdec(__pgprot(0)) : cc_mkenc(__pgprot(0));
 	cpa.pgd = init_mm.pgd;
 
 	/* Must avoid aliasing mappings in the highmem code */
-- 
 Kirill A. Shutemov

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ