[PATCH v4 7/7] x86/crash: make the page that stores the dm crypt keys inaccessible

Coiby Xu posted 7 patches 1 year, 8 months ago
There is a newer version of this series
[PATCH v4 7/7] x86/crash: make the page that stores the dm crypt keys inaccessible
Posted by Coiby Xu 1 year, 8 months ago
This adds an addition layer of protection for the saved copy of dm
crypt key. Trying to access the saved copy will cause page fault.

Suggested-by: Pingfan Liu <kernelfans@gmail.com>
Signed-off-by: Coiby Xu <coxu@redhat.com>
---
 arch/x86/kernel/machine_kexec_64.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_kexec_64.c
index b180d8e497c3..fc0a80f4254e 100644
--- a/arch/x86/kernel/machine_kexec_64.c
+++ b/arch/x86/kernel/machine_kexec_64.c
@@ -545,13 +545,34 @@ static void kexec_mark_crashkres(bool protect)
 	kexec_mark_range(control, crashk_res.end, protect);
 }
 
+static void kexec_mark_dm_crypt_keys(bool protect)
+{
+	unsigned long start_paddr, end_paddr;
+	unsigned int nr_pages;
+
+	if (kexec_crash_image->dm_crypt_keys_addr) {
+		start_paddr = kexec_crash_image->dm_crypt_keys_addr;
+		end_paddr = start_paddr + kexec_crash_image->dm_crypt_keys_sz - 1;
+		nr_pages = (PAGE_ALIGN(end_paddr) - PAGE_ALIGN_DOWN(start_paddr))/PAGE_SIZE;
+		if (protect)
+			set_memory_np((unsigned long)phys_to_virt(start_paddr), nr_pages);
+		else
+			__set_memory_prot(
+				(unsigned long)phys_to_virt(start_paddr),
+				nr_pages,
+				__pgprot(_PAGE_PRESENT | _PAGE_NX | _PAGE_RW));
+	}
+}
+
 void arch_kexec_protect_crashkres(void)
 {
 	kexec_mark_crashkres(true);
+	kexec_mark_dm_crypt_keys(true);
 }
 
 void arch_kexec_unprotect_crashkres(void)
 {
+	kexec_mark_dm_crypt_keys(false);
 	kexec_mark_crashkres(false);
 }
 #endif
-- 
2.45.0
Re: [PATCH v4 7/7] x86/crash: make the page that stores the dm crypt keys inaccessible
Posted by Baoquan He 1 year, 8 months ago
On 05/23/24 at 01:04pm, Coiby Xu wrote:
> This adds an addition layer of protection for the saved copy of dm
> crypt key. Trying to access the saved copy will cause page fault.
> 
> Suggested-by: Pingfan Liu <kernelfans@gmail.com>
> Signed-off-by: Coiby Xu <coxu@redhat.com>
> ---
>  arch/x86/kernel/machine_kexec_64.c | 21 +++++++++++++++++++++
>  1 file changed, 21 insertions(+)
> 
> diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_kexec_64.c
> index b180d8e497c3..fc0a80f4254e 100644
> --- a/arch/x86/kernel/machine_kexec_64.c
> +++ b/arch/x86/kernel/machine_kexec_64.c
> @@ -545,13 +545,34 @@ static void kexec_mark_crashkres(bool protect)
>  	kexec_mark_range(control, crashk_res.end, protect);
>  }
>  
> +static void kexec_mark_dm_crypt_keys(bool protect)
> +{
> +	unsigned long start_paddr, end_paddr;
> +	unsigned int nr_pages;
> +
> +	if (kexec_crash_image->dm_crypt_keys_addr) {
> +		start_paddr = kexec_crash_image->dm_crypt_keys_addr;
> +		end_paddr = start_paddr + kexec_crash_image->dm_crypt_keys_sz - 1;
> +		nr_pages = (PAGE_ALIGN(end_paddr) - PAGE_ALIGN_DOWN(start_paddr))/PAGE_SIZE;
> +		if (protect)
> +			set_memory_np((unsigned long)phys_to_virt(start_paddr), nr_pages);
> +		else
> +			__set_memory_prot(
> +				(unsigned long)phys_to_virt(start_paddr),
> +				nr_pages,
> +				__pgprot(_PAGE_PRESENT | _PAGE_NX | _PAGE_RW));
> +	}
> +}
> +
>  void arch_kexec_protect_crashkres(void)
>  {
>  	kexec_mark_crashkres(true);
> +	kexec_mark_dm_crypt_keys(true);

Isn't crashkernel region covering crypt keys' storing region? Do we need
mark it again specifically? Not sure if I miss anything.

>  }
>  
>  void arch_kexec_unprotect_crashkres(void)
>  {
> +	kexec_mark_dm_crypt_keys(false);
>  	kexec_mark_crashkres(false);
>  }
>  #endif
> -- 
> 2.45.0
>
Re: [PATCH v4 7/7] x86/crash: make the page that stores the dm crypt keys inaccessible
Posted by Coiby Xu 1 year, 8 months ago
On Fri, Jun 07, 2024 at 06:00:44PM +0800, Baoquan He wrote:
>On 05/23/24 at 01:04pm, Coiby Xu wrote:
>> This adds an addition layer of protection for the saved copy of dm
>> crypt key. Trying to access the saved copy will cause page fault.
>>
>> Suggested-by: Pingfan Liu <kernelfans@gmail.com>
>> Signed-off-by: Coiby Xu <coxu@redhat.com>
>> ---
>>  arch/x86/kernel/machine_kexec_64.c | 21 +++++++++++++++++++++
>>  1 file changed, 21 insertions(+)
>>
>> diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_kexec_64.c
>> index b180d8e497c3..fc0a80f4254e 100644
>> --- a/arch/x86/kernel/machine_kexec_64.c
>> +++ b/arch/x86/kernel/machine_kexec_64.c
>> @@ -545,13 +545,34 @@ static void kexec_mark_crashkres(bool protect)
>>  	kexec_mark_range(control, crashk_res.end, protect);
>>  }
>>
>> +static void kexec_mark_dm_crypt_keys(bool protect)
>> +{
>> +	unsigned long start_paddr, end_paddr;
>> +	unsigned int nr_pages;
>> +
>> +	if (kexec_crash_image->dm_crypt_keys_addr) {
>> +		start_paddr = kexec_crash_image->dm_crypt_keys_addr;
>> +		end_paddr = start_paddr + kexec_crash_image->dm_crypt_keys_sz - 1;
>> +		nr_pages = (PAGE_ALIGN(end_paddr) - PAGE_ALIGN_DOWN(start_paddr))/PAGE_SIZE;
>> +		if (protect)
>> +			set_memory_np((unsigned long)phys_to_virt(start_paddr), nr_pages);
>> +		else
>> +			__set_memory_prot(
>> +				(unsigned long)phys_to_virt(start_paddr),
>> +				nr_pages,
>> +				__pgprot(_PAGE_PRESENT | _PAGE_NX | _PAGE_RW));
>> +	}
>> +}
>> +
>>  void arch_kexec_protect_crashkres(void)
>>  {
>>  	kexec_mark_crashkres(true);
>> +	kexec_mark_dm_crypt_keys(true);
>
>Isn't crashkernel region covering crypt keys' storing region? Do we need
>mark it again specifically? Not sure if I miss anything.

kexec_mark_crashkres only makes the page read-only whereas
kexec_mark_dm_crypt_keys makes the memory inaccessible. I've added a
comment for this function in v5 and hopefully no one will be confused by
it.

-- 
Best regards,
Coiby