Skip to contentSkip to conversation

CRYPTO: Fix initialize 'psp_ret' to avoid uninitialized usage in error paths

PATCH NAK Haiwei Li 2 participants 2 messages Nov 28, 2019 – Dec 2, 2019

Subsystems (2)
lore
Share & tools
lore

Haiwei Li

author
#0 lore raw

PATCH 1/1 CRYPTO: Fix initialize 'psp_ret' to avoid uninitialized usage in error paths

From 842cac9822aafd3cfe2da154b92b033fa1ed0d2d Mon Sep 17 00:00:00 2001
From: Haiwei Li <[email protected]>
Date: Thu, 28 Nov 2019 08:25:16 +0800
Subject: [PATCH] fix: initialize @psp_ret to avoid uninitialized usage in error paths

Initialize @psp_ret to -1 to avoid uninitialized usage in error paths. Such as the function 'sev_flush_asides' in file 'arch/x86/kvm/svm.c'.

---
 drivers/crypto/ccp/psp-dev.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/crypto/ccp/psp-dev.c +3 -0
index 39fdd06..3501562 100644
@@ -155,6 +155,9 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)
 	unsigned int phys_lsb, phys_msb;
 	unsigned int reg, ret = 0;
 
+	if (psp_ret)
+		*psp_ret = -1;
+
 	if (!psp)
 		return -ENODEV;
 

--
1.8.3.1

Nacked-by on v1 1/1 NAK
#1 lore raw
Quoted 7 lines · Haiwei Li

From 842cac9822aafd3cfe2da154b92b033fa1ed0d2d Mon Sep 17 00:00:00 2001
From: Haiwei Li <[email protected]>
Date: Thu, 28 Nov 2019 08:25:16 +0800
Subject: [PATCH] fix: initialize @psp_ret to avoid uninitialized usage in error paths

Initialize @psp_ret to -1 to avoid uninitialized usage in error paths. Such as the function 'sev_flush_asides' in file 'arch/x86/kvm/svm.c'.

There is no uninitialized usage in error paths.

Quoted 12 lines
---
  drivers/crypto/ccp/psp-dev.c | 3 +++
  1 file changed, 3 insertions(+)
drivers/crypto/ccp/psp-dev.c +0 -0
index 39fdd06..3501562 100644

drivers/crypto/ccp/psp-dev.c static int __sev_do_cmd_locked(int cmd, void *data, · line 161

Quoted 7 lines
drivers/crypto/ccp/psp-dev.c +3 -0
@@ -155,6 +155,9 @@ static int __sev_do_cmd_locked(int cmd, void *data, 
 int *psp_ret)
      unsigned int phys_lsb, phys_msb;
      unsigned int reg, ret = 0;
 
+    if (psp_ret)
+        *psp_ret = -1;
+

This function is not responsible for initializing memory that comes from elsewhere. Much like the use of errno, we should not modify memory if an error path causes __sev_do_cmd_locked() to return before any work is done.

Since this function can return two values (the return code, and the psp_ret argument), it has been defined to use the return value of the function to first indicate success or failure. Only in the case of a failure should the memory pointed to by psp_ret contain any useful information. In every other case, that memory should remain unmodified.

The return value that is stored in *psp_ret only represents information from the PSP. Therefore, it should only be modified when the PSP is called. Additionally, there is no "-1" return value from the PSP, and we will not be defining an default value at this time.

While I am somewhat sympathetic to the static checker's complaints, the proper solution for that problem is to initialize memory when it is allocated. Not here.

Therefore:

Quoted 5 lines
drivers/crypto/ccp/psp-dev.c +0 -0
      if (!psp)
          return -ENODEV;
Signature · 2 lines
-- 
1.8.3.1