x86/sme: Fix memory encryption if enabled by default and not overridden

Message ID 20240126163918.2908990-2-ardb+git@google.com
State New
Headers
Series x86/sme: Fix memory encryption if enabled by default and not overridden |

Commit Message

Ard Biesheuvel Jan. 26, 2024, 4:39 p.m. UTC
  From: Ard Biesheuvel <ardb@kernel.org>

Commit cbebd68f59f0 ("x86/mm: Fix use of uninitialized buffer in
sme_enable()") 'fixed' an issue in sme_enable() detected by static
analysis, and broke the common case in the process.

cmdline_find_option() will return < 0 on an error, or when the command
line argument does not appear at all. In this particular case, the
latter is not an error condition, and so the early exit is wrong.

Instead, without mem_encrypt= on the command line, the compile time
default should be honoured, which could be to enable memory encryption,
and this is currently broken.

Fix it by setting sme_me_mask to a preliminary value based on the
compile time default, and only omitting the command line argument test
when cmdline_find_option() returns an error.

Cc: Borislav Petkov (AMD) <bp@alien8.de>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: Nikita Zhandarovich <n.zhandarovich@fintech.ru>
Cc: Kevin Loughlin <kevinloughlin@google.com>
Fixes: cbebd68f59f0 ("x86/mm: Fix use of uninitialized buffer in sme_enable()")
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 arch/x86/mm/mem_encrypt_identity.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
  

Comments

Tom Lendacky Jan. 26, 2024, 10:26 p.m. UTC | #1
On 1/26/24 10:39, Ard Biesheuvel wrote:
> From: Ard Biesheuvel <ardb@kernel.org>
> 
> Commit cbebd68f59f0 ("x86/mm: Fix use of uninitialized buffer in
> sme_enable()") 'fixed' an issue in sme_enable() detected by static
> analysis, and broke the common case in the process.
> 
> cmdline_find_option() will return < 0 on an error, or when the command
> line argument does not appear at all. In this particular case, the
> latter is not an error condition, and so the early exit is wrong.
> 
> Instead, without mem_encrypt= on the command line, the compile time
> default should be honoured, which could be to enable memory encryption,
> and this is currently broken.
> 
> Fix it by setting sme_me_mask to a preliminary value based on the
> compile time default, and only omitting the command line argument test
> when cmdline_find_option() returns an error.
> 
> Cc: Borislav Petkov (AMD) <bp@alien8.de>
> Cc: Tom Lendacky <thomas.lendacky@amd.com>
> Cc: Nikita Zhandarovich <n.zhandarovich@fintech.ru>
> Cc: Kevin Loughlin <kevinloughlin@google.com>
> Fixes: cbebd68f59f0 ("x86/mm: Fix use of uninitialized buffer in sme_enable()")
> Signed-off-by: Ard Biesheuvel <ardb@kernel.org>

Nice catch.

Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com>

> ---
>   arch/x86/mm/mem_encrypt_identity.c | 5 ++---
>   1 file changed, 2 insertions(+), 3 deletions(-)
> 
> diff --git a/arch/x86/mm/mem_encrypt_identity.c b/arch/x86/mm/mem_encrypt_identity.c
> index d73aeb16417f..30df4f1725f4 100644
> --- a/arch/x86/mm/mem_encrypt_identity.c
> +++ b/arch/x86/mm/mem_encrypt_identity.c
> @@ -600,15 +600,14 @@ void __init sme_enable(struct boot_params *bp)
>   	cmdline_ptr = (const char *)((u64)bp->hdr.cmd_line_ptr |
>   				     ((u64)bp->ext_cmd_line_ptr << 32));
>   
> +	sme_me_mask = active_by_default ? me_mask : 0;
>   	if (cmdline_find_option(cmdline_ptr, cmdline_arg, buffer, sizeof(buffer)) < 0)
> -		return;
> +		goto out;
>   
>   	if (!strncmp(buffer, cmdline_on, sizeof(buffer)))
>   		sme_me_mask = me_mask;
>   	else if (!strncmp(buffer, cmdline_off, sizeof(buffer)))
>   		sme_me_mask = 0;
> -	else
> -		sme_me_mask = active_by_default ? me_mask : 0;
>   out:
>   	if (sme_me_mask) {
>   		physical_mask &= ~sme_me_mask;
  
Borislav Petkov Jan. 27, 2024, 10:52 a.m. UTC | #2
On Fri, Jan 26, 2024 at 05:39:19PM +0100, Ard Biesheuvel wrote:
> From: Ard Biesheuvel <ardb@kernel.org>
> 
> Commit cbebd68f59f0 ("x86/mm: Fix use of uninitialized buffer in
> sme_enable()") 'fixed' an issue in sme_enable() detected by static
> analysis, and broke the common case in the process.
> 
> cmdline_find_option() will return < 0 on an error, or when the command
> line argument does not appear at all.

Is it just me or cmdline_find_option() should be fixed to return 0 when
there's no cmdline argument and < 0 only when there was a real error
parsing?

Hohumm, sounds like a TODO for me or someone who wants to audit all
callers and fix them up accordingly.
  
Ard Biesheuvel Jan. 29, 2024, 11:06 a.m. UTC | #3
On Sat, 27 Jan 2024 at 11:53, Borislav Petkov <bp@alien8.de> wrote:
>
> On Fri, Jan 26, 2024 at 05:39:19PM +0100, Ard Biesheuvel wrote:
> > From: Ard Biesheuvel <ardb@kernel.org>
> >
> > Commit cbebd68f59f0 ("x86/mm: Fix use of uninitialized buffer in
> > sme_enable()") 'fixed' an issue in sme_enable() detected by static
> > analysis, and broke the common case in the process.
> >
> > cmdline_find_option() will return < 0 on an error, or when the command
> > line argument does not appear at all.
>
> Is it just me or cmdline_find_option() should be fixed to return 0 when
> there's no cmdline argument and < 0 only when there was a real error
> parsing?
>
> Hohumm, sounds like a TODO for me or someone who wants to audit all
> callers and fix them up accordingly.
>

I intend to propose removing this occurrence entirely, and move it
into the EFI stub/decompressor.

Parsing external input in security related code when the entire kernel
is still mapped writable is kind of gross, and since I'm cleaning up
the early PIC code anyway, might just as well rip this out.

Another issue here is that it does not honor
CONFIG_CMDLINE_BOOL/CONFIG_CMDLINE_OVERRIDE. I.e., if memory
encryption is enabled by default, and the kernel is configured to
ignore the command line, the current code will still disable memory
encryption when mem_encrypt=off is passed. Probably not a big deal,
but unintuitive nonetheless.

For the other cases, I agree that this would be a good thing to clean
up. Note that someone has been looking into the handling of the
command line recently.

https://lore.kernel.org/all/20231110013817.2378507-6-danielwa@cisco.com/T/#m817c573e25f6f7da237272178a8f6b116192a6ad

but I am not sure what the state of the code is, or whether the
approach is the right one. I was meaning to look at that but haven't
found the time yet.
  

Patch

diff --git a/arch/x86/mm/mem_encrypt_identity.c b/arch/x86/mm/mem_encrypt_identity.c
index d73aeb16417f..30df4f1725f4 100644
--- a/arch/x86/mm/mem_encrypt_identity.c
+++ b/arch/x86/mm/mem_encrypt_identity.c
@@ -600,15 +600,14 @@  void __init sme_enable(struct boot_params *bp)
 	cmdline_ptr = (const char *)((u64)bp->hdr.cmd_line_ptr |
 				     ((u64)bp->ext_cmd_line_ptr << 32));
 
+	sme_me_mask = active_by_default ? me_mask : 0;
 	if (cmdline_find_option(cmdline_ptr, cmdline_arg, buffer, sizeof(buffer)) < 0)
-		return;
+		goto out;
 
 	if (!strncmp(buffer, cmdline_on, sizeof(buffer)))
 		sme_me_mask = me_mask;
 	else if (!strncmp(buffer, cmdline_off, sizeof(buffer)))
 		sme_me_mask = 0;
-	else
-		sme_me_mask = active_by_default ? me_mask : 0;
 out:
 	if (sme_me_mask) {
 		physical_mask &= ~sme_me_mask;