From nobody Sun Feb 8 17:04:13 2026 Received: from exchange.fintech.ru (exchange.fintech.ru [195.54.195.159]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E17EB1BD003; Thu, 28 Nov 2024 15:33:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.54.195.159 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732808031; cv=none; b=POHaaQAhc6hWmGyFHjkzzMHHlEw5wdsDsojg0ENzNBxKQVuEdPRfMTBhCVa2fLIx5pBP0JUlFfhJnYlPWZJvyBasC4MyVoebhIDWAxNUkOpL+NGbRpkdl/GHmyIzFkTmUjBQjzm2uzIIQI5dqYYWIq3XqqbQD719Fa3+FcifTto= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732808031; c=relaxed/simple; bh=7+2fvqHmSSiZr8FzvsxvGsUCIxuqZwRAFJF1lweIaDI=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=UdlLsVMSkdPE7aG6dOSYo1f6KxAyrvgp5NoXV7+dDuFsBBtZYkASHimscdyPimja5GRlSpwKf4/QTQslJbyNbCIKLl340jrzP/1McmB2O3waVwYMr3UUjiTtn3oIRfs4OF/nQnPT68Iy+XN8ifc1Ky0AWIxH3zdwWS6k/TeK4lE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=fintech.ru; spf=pass smtp.mailfrom=fintech.ru; arc=none smtp.client-ip=195.54.195.159 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=fintech.ru Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=fintech.ru Received: from Ex16-01.fintech.ru (10.0.10.18) by exchange.fintech.ru (195.54.195.159) with Microsoft SMTP Server (TLS) id 14.3.498.0; Thu, 28 Nov 2024 18:33:45 +0300 Received: from localhost (10.0.253.138) by Ex16-01.fintech.ru (10.0.10.18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2242.4; Thu, 28 Nov 2024 18:33:45 +0300 From: Nikita Zhandarovich To: Greg Kroah-Hartman , Sasha Levin , CC: Nikita Zhandarovich , Harald Freudenberger , Heiko Carstens , Vasily Gorbik , Christian Borntraeger , "Holger Dengler" , Alexander Gordeev , , , Subject: [PATCH 6.1 1/3] s390/pkey: Use kfree_sensitive() to fix Coccinelle warnings Date: Thu, 28 Nov 2024 07:33:35 -0800 Message-ID: <20241128153337.19666-2-n.zhandarovich@fintech.ru> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20241128153337.19666-1-n.zhandarovich@fintech.ru> References: <20241128153337.19666-1-n.zhandarovich@fintech.ru> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: Ex16-02.fintech.ru (10.0.10.19) To Ex16-01.fintech.ru (10.0.10.18) Content-Type: text/plain; charset="utf-8" From: Jules Irenge commit 22e6824622e8a8889df0f8fc4ed5aea0e702a694 upstream. Replace memzero_explicit() and kfree() with kfree_sensitive() to fix warnings reported by Coccinelle: WARNING opportunity for kfree_sensitive/kvfree_sensitive (line 1506) WARNING opportunity for kfree_sensitive/kvfree_sensitive (line 1643) WARNING opportunity for kfree_sensitive/kvfree_sensitive (line 1770) Signed-off-by: Jules Irenge Reviewed-by: Holger Dengler Link: https://lore.kernel.org/r/ZjqZkNi_JUJu73Rg@octinomon.home Signed-off-by: Heiko Carstens Signed-off-by: Alexander Gordeev [Nikita: small changes were made during cherry-picking due to different debug macro use and similar discrepancies between branches] Signed-off-by: Nikita Zhandarovich --- P.S. As no Fixes: tag was present, I decided against adding it myself and leaving commit body intact. drivers/s390/crypto/pkey_api.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/drivers/s390/crypto/pkey_api.c b/drivers/s390/crypto/pkey_api.c index 17885c9f55cb..d1429622036f 100644 --- a/drivers/s390/crypto/pkey_api.c +++ b/drivers/s390/crypto/pkey_api.c @@ -1307,8 +1307,7 @@ static long pkey_unlocked_ioctl(struct file *filp, un= signed int cmd, return PTR_ERR(kkey); rc =3D pkey_keyblob2pkey(kkey, ktp.keylen, &ktp.protkey); DEBUG_DBG("%s pkey_keyblob2pkey()=3D%d\n", __func__, rc); - memzero_explicit(kkey, ktp.keylen); - kfree(kkey); + kfree_sensitive(kkey); if (rc) break; if (copy_to_user(utp, &ktp, sizeof(ktp))) @@ -1441,8 +1440,7 @@ static long pkey_unlocked_ioctl(struct file *filp, un= signed int cmd, kkey, ktp.keylen, &ktp.protkey); DEBUG_DBG("%s pkey_keyblob2pkey2()=3D%d\n", __func__, rc); kfree(apqns); - memzero_explicit(kkey, ktp.keylen); - kfree(kkey); + kfree_sensitive(kkey); if (rc) break; if (copy_to_user(utp, &ktp, sizeof(ktp))) @@ -1568,8 +1566,7 @@ static long pkey_unlocked_ioctl(struct file *filp, un= signed int cmd, protkey, &protkeylen); DEBUG_DBG("%s pkey_keyblob2pkey3()=3D%d\n", __func__, rc); kfree(apqns); - memzero_explicit(kkey, ktp.keylen); - kfree(kkey); + kfree_sensitive(kkey); if (rc) { kfree(protkey); break; --=20 2.25.1 From nobody Sun Feb 8 17:04:13 2026 Received: from exchange.fintech.ru (exchange.fintech.ru [195.54.195.159]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 274C51BD9CF; Thu, 28 Nov 2024 15:33:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.54.195.159 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732808035; cv=none; b=EkPLv1Mg34vft7YQAZWi/JEIwnJPnfZAtIwQhNxvcVrBS+BJRA8ejOAC6F7ZBd0kA8VqC7QhH8U3ot0BfW5PC5EBj/Ym/3nBoNjgCytytolnF9bgfrUMyOxQZOnXEaIEv/ZCyS4SNAJNFf2O5+YpfQlY8Jd0dzgsGLDG5sffAdE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732808035; c=relaxed/simple; bh=cfNwAVvyvS73eiE+Io8/KaU80ZlBUJkSxih1TuRbtBY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Nr1sC8/x5uhxi+btAvxQBxsCg7Hrit42DGErpRS0+YHJ3OxCpU7CUjTRtPNx+6nm12jkaXmNcVFsAaCu11fF/XUPFJPIDld+tdkYHHUbEkUxLn4CKTV0v5UV2IV64N5nePeN3+ioBdjbcIvzWV7Tqj9ijyt/vOoOlBGD7KbmUx0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=fintech.ru; spf=pass smtp.mailfrom=fintech.ru; arc=none smtp.client-ip=195.54.195.159 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=fintech.ru Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=fintech.ru Received: from Ex16-01.fintech.ru (10.0.10.18) by exchange.fintech.ru (195.54.195.159) with Microsoft SMTP Server (TLS) id 14.3.498.0; Thu, 28 Nov 2024 18:33:49 +0300 Received: from localhost (10.0.253.138) by Ex16-01.fintech.ru (10.0.10.18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2242.4; Thu, 28 Nov 2024 18:33:49 +0300 From: Nikita Zhandarovich To: Greg Kroah-Hartman , Sasha Levin , CC: Nikita Zhandarovich , Harald Freudenberger , Heiko Carstens , Vasily Gorbik , Christian Borntraeger , "Holger Dengler" , Alexander Gordeev , , , Subject: [PATCH 6.1 2/3] s390/pkey: Wipe copies of clear-key structures on failure Date: Thu, 28 Nov 2024 07:33:36 -0800 Message-ID: <20241128153337.19666-3-n.zhandarovich@fintech.ru> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20241128153337.19666-1-n.zhandarovich@fintech.ru> References: <20241128153337.19666-1-n.zhandarovich@fintech.ru> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: Ex16-02.fintech.ru (10.0.10.19) To Ex16-01.fintech.ru (10.0.10.18) Content-Type: text/plain; charset="utf-8" From: Holger Dengler commit d65d76a44ffe74c73298ada25b0f578680576073 upstream. Wipe all sensitive data from stack for all IOCTLs, which convert a clear-key into a protected- or secure-key. Reviewed-by: Harald Freudenberger Reviewed-by: Ingo Franzki Acked-by: Heiko Carstens Signed-off-by: Holger Dengler Signed-off-by: Alexander Gordeev [Nikita: small changes were made during cherry-picking due to different debug macro use and similar discrepancies between branches] Signed-off-by: Nikita Zhandarovich --- P.S. As no Fixes: tag was present, I decided against adding it myself and leaving commit body intact. drivers/s390/crypto/pkey_api.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/drivers/s390/crypto/pkey_api.c b/drivers/s390/crypto/pkey_api.c index d1429622036f..0aaa8686a0b2 100644 --- a/drivers/s390/crypto/pkey_api.c +++ b/drivers/s390/crypto/pkey_api.c @@ -1188,9 +1188,7 @@ static long pkey_unlocked_ioctl(struct file *filp, un= signed int cmd, rc =3D cca_clr2seckey(kcs.cardnr, kcs.domain, kcs.keytype, kcs.clrkey.clrkey, kcs.seckey.seckey); DEBUG_DBG("%s cca_clr2seckey()=3D%d\n", __func__, rc); - if (rc) - break; - if (copy_to_user(ucs, &kcs, sizeof(kcs))) + if (!rc && copy_to_user(ucs, &kcs, sizeof(kcs))) rc =3D -EFAULT; memzero_explicit(&kcs, sizeof(kcs)); break; @@ -1220,9 +1218,7 @@ static long pkey_unlocked_ioctl(struct file *filp, un= signed int cmd, rc =3D pkey_clr2protkey(kcp.keytype, &kcp.clrkey, &kcp.protkey); DEBUG_DBG("%s pkey_clr2protkey()=3D%d\n", __func__, rc); - if (rc) - break; - if (copy_to_user(ucp, &kcp, sizeof(kcp))) + if (!rc && copy_to_user(ucp, &kcp, sizeof(kcp))) rc =3D -EFAULT; memzero_explicit(&kcp, sizeof(kcp)); break; @@ -1366,11 +1362,14 @@ static long pkey_unlocked_ioctl(struct file *filp, = unsigned int cmd, if (copy_from_user(&kcs, ucs, sizeof(kcs))) return -EFAULT; apqns =3D _copy_apqns_from_user(kcs.apqns, kcs.apqn_entries); - if (IS_ERR(apqns)) + if (IS_ERR(apqns)) { + memzero_explicit(&kcs, sizeof(kcs)); return PTR_ERR(apqns); + } kkey =3D kzalloc(klen, GFP_KERNEL); if (!kkey) { kfree(apqns); + memzero_explicit(&kcs, sizeof(kcs)); return -ENOMEM; } rc =3D pkey_clr2seckey2(apqns, kcs.apqn_entries, @@ -1380,15 +1379,18 @@ static long pkey_unlocked_ioctl(struct file *filp, = unsigned int cmd, kfree(apqns); if (rc) { kfree(kkey); + memzero_explicit(&kcs, sizeof(kcs)); break; } if (kcs.key) { if (kcs.keylen < klen) { kfree(kkey); + memzero_explicit(&kcs, sizeof(kcs)); return -EINVAL; } if (copy_to_user(kcs.key, kkey, klen)) { kfree(kkey); + memzero_explicit(&kcs, sizeof(kcs)); return -EFAULT; } } --=20 2.25.1 From nobody Sun Feb 8 17:04:13 2026 Received: from exchange.fintech.ru (exchange.fintech.ru [195.54.195.159]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BF0241BD004; Thu, 28 Nov 2024 15:33:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.54.195.159 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732808038; cv=none; b=SxGzouXX9LO29Cl5akpOfeTBJtx1phDyveWyGT4wmFSheEB2+dVO8/q3dSHUp5SZgaJSdZ5vu2fDvNMNzSUWCpyYprDX9LJbYh3TEidS0Gc87p9Gj3Mwcg5rCiga5rcO46ZJRFUg32bxXKOS7cj0Vnn7+Jlcba3925s5j6wcnH4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732808038; c=relaxed/simple; bh=6HolMuGOVTVGvk2Y2Pjahg0UiS6VGNbXp4B2h9+ljK0=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=YY8BVhUdDFw+DKAY4qRSib91f4qNbKn7cAzGwLth24SU+JiEjM+aKOuRjs9MfmeRQ8wfRzFzuMewVplU167I1TJ07gxAIr7EE+UtB5FecyMnwnRBagF7Yuqn+L3hkEXKcK+Pmdcm08nP1uMLq8y1ncrdCQXpQ/6WzGV3NJslkA0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=fintech.ru; spf=pass smtp.mailfrom=fintech.ru; arc=none smtp.client-ip=195.54.195.159 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=fintech.ru Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=fintech.ru Received: from Ex16-01.fintech.ru (10.0.10.18) by exchange.fintech.ru (195.54.195.159) with Microsoft SMTP Server (TLS) id 14.3.498.0; Thu, 28 Nov 2024 18:33:53 +0300 Received: from localhost (10.0.253.138) by Ex16-01.fintech.ru (10.0.10.18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2242.4; Thu, 28 Nov 2024 18:33:53 +0300 From: Nikita Zhandarovich To: Greg Kroah-Hartman , Sasha Levin , CC: Nikita Zhandarovich , Harald Freudenberger , Heiko Carstens , Vasily Gorbik , Christian Borntraeger , "Holger Dengler" , Alexander Gordeev , , , Subject: [PATCH 6.1 3/3] s390/pkey: Wipe copies of protected- and secure-keys Date: Thu, 28 Nov 2024 07:33:37 -0800 Message-ID: <20241128153337.19666-4-n.zhandarovich@fintech.ru> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20241128153337.19666-1-n.zhandarovich@fintech.ru> References: <20241128153337.19666-1-n.zhandarovich@fintech.ru> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: Ex16-02.fintech.ru (10.0.10.19) To Ex16-01.fintech.ru (10.0.10.18) Content-Type: text/plain; charset="utf-8" From: Holger Dengler commit f2ebdadd85af4f4d0cae1e5d009c70eccc78c207 upstream. Although the clear-key of neither protected- nor secure-keys is accessible, this key material should only be visible to the calling process. So wipe all copies of protected- or secure-keys from stack, even in case of an error. Reviewed-by: Harald Freudenberger Reviewed-by: Ingo Franzki Acked-by: Heiko Carstens Signed-off-by: Holger Dengler Signed-off-by: Alexander Gordeev [Nikita: small changes were made during cherry-picking due to different debug macro use and similar discrepancies between branches] Signed-off-by: Nikita Zhandarovich --- P.S. As no Fixes: tag was present, I decided against adding it myself and leaving commit body intact. drivers/s390/crypto/pkey_api.c | 80 ++++++++++++++++------------------ 1 file changed, 37 insertions(+), 43 deletions(-) diff --git a/drivers/s390/crypto/pkey_api.c b/drivers/s390/crypto/pkey_api.c index 0aaa8686a0b2..4b7ca7473123 100644 --- a/drivers/s390/crypto/pkey_api.c +++ b/drivers/s390/crypto/pkey_api.c @@ -1173,10 +1173,9 @@ static long pkey_unlocked_ioctl(struct file *filp, u= nsigned int cmd, rc =3D cca_genseckey(kgs.cardnr, kgs.domain, kgs.keytype, kgs.seckey.seckey); DEBUG_DBG("%s cca_genseckey()=3D%d\n", __func__, rc); - if (rc) - break; - if (copy_to_user(ugs, &kgs, sizeof(kgs))) - return -EFAULT; + if (!rc && copy_to_user(ugs, &kgs, sizeof(kgs))) + rc =3D -EFAULT; + memzero_explicit(&kgs, sizeof(kgs)); break; } case PKEY_CLR2SECK: { @@ -1203,10 +1202,9 @@ static long pkey_unlocked_ioctl(struct file *filp, u= nsigned int cmd, ksp.seckey.seckey, ksp.protkey.protkey, &ksp.protkey.len, &ksp.protkey.type); DEBUG_DBG("%s cca_sec2protkey()=3D%d\n", __func__, rc); - if (rc) - break; - if (copy_to_user(usp, &ksp, sizeof(ksp))) - return -EFAULT; + if (!rc && copy_to_user(usp, &ksp, sizeof(ksp))) + rc =3D -EFAULT; + memzero_explicit(&ksp, sizeof(ksp)); break; } case PKEY_CLR2PROTK: { @@ -1246,10 +1244,9 @@ static long pkey_unlocked_ioctl(struct file *filp, u= nsigned int cmd, return -EFAULT; rc =3D pkey_skey2pkey(ksp.seckey.seckey, &ksp.protkey); DEBUG_DBG("%s pkey_skey2pkey()=3D%d\n", __func__, rc); - if (rc) - break; - if (copy_to_user(usp, &ksp, sizeof(ksp))) - return -EFAULT; + if (!rc && copy_to_user(usp, &ksp, sizeof(ksp))) + rc =3D -EFAULT; + memzero_explicit(&ksp, sizeof(ksp)); break; } case PKEY_VERIFYKEY: { @@ -1261,10 +1258,9 @@ static long pkey_unlocked_ioctl(struct file *filp, u= nsigned int cmd, rc =3D pkey_verifykey(&kvk.seckey, &kvk.cardnr, &kvk.domain, &kvk.keysize, &kvk.attributes); DEBUG_DBG("%s pkey_verifykey()=3D%d\n", __func__, rc); - if (rc) - break; - if (copy_to_user(uvk, &kvk, sizeof(kvk))) - return -EFAULT; + if (!rc && copy_to_user(uvk, &kvk, sizeof(kvk))) + rc =3D -EFAULT; + memzero_explicit(&kvk, sizeof(kvk)); break; } case PKEY_GENPROTK: { @@ -1275,10 +1271,9 @@ static long pkey_unlocked_ioctl(struct file *filp, u= nsigned int cmd, return -EFAULT; rc =3D pkey_genprotkey(kgp.keytype, &kgp.protkey); DEBUG_DBG("%s pkey_genprotkey()=3D%d\n", __func__, rc); - if (rc) - break; - if (copy_to_user(ugp, &kgp, sizeof(kgp))) - return -EFAULT; + if (!rc && copy_to_user(ugp, &kgp, sizeof(kgp))) + rc =3D -EFAULT; + memzero_explicit(&kgp, sizeof(kgp)); break; } case PKEY_VERIFYPROTK: { @@ -1289,6 +1284,7 @@ static long pkey_unlocked_ioctl(struct file *filp, un= signed int cmd, return -EFAULT; rc =3D pkey_verifyprotkey(&kvp.protkey); DEBUG_DBG("%s pkey_verifyprotkey()=3D%d\n", __func__, rc); + memzero_explicit(&kvp, sizeof(kvp)); break; } case PKEY_KBLOB2PROTK: { @@ -1304,10 +1300,9 @@ static long pkey_unlocked_ioctl(struct file *filp, u= nsigned int cmd, rc =3D pkey_keyblob2pkey(kkey, ktp.keylen, &ktp.protkey); DEBUG_DBG("%s pkey_keyblob2pkey()=3D%d\n", __func__, rc); kfree_sensitive(kkey); - if (rc) - break; - if (copy_to_user(utp, &ktp, sizeof(ktp))) - return -EFAULT; + if (!rc && copy_to_user(utp, &ktp, sizeof(ktp))) + rc =3D -EFAULT; + memzero_explicit(&ktp, sizeof(ktp)); break; } case PKEY_GENSECK2: { @@ -1333,23 +1328,23 @@ static long pkey_unlocked_ioctl(struct file *filp, = unsigned int cmd, DEBUG_DBG("%s pkey_genseckey2()=3D%d\n", __func__, rc); kfree(apqns); if (rc) { - kfree(kkey); + kfree_sensitive(kkey); break; } if (kgs.key) { if (kgs.keylen < klen) { - kfree(kkey); + kfree_sensitive(kkey); return -EINVAL; } if (copy_to_user(kgs.key, kkey, klen)) { - kfree(kkey); + kfree_sensitive(kkey); return -EFAULT; } } kgs.keylen =3D klen; if (copy_to_user(ugs, &kgs, sizeof(kgs))) rc =3D -EFAULT; - kfree(kkey); + kfree_sensitive(kkey); break; } case PKEY_CLR2SECK2: { @@ -1378,18 +1373,18 @@ static long pkey_unlocked_ioctl(struct file *filp, = unsigned int cmd, DEBUG_DBG("%s pkey_clr2seckey2()=3D%d\n", __func__, rc); kfree(apqns); if (rc) { - kfree(kkey); + kfree_sensitive(kkey); memzero_explicit(&kcs, sizeof(kcs)); break; } if (kcs.key) { if (kcs.keylen < klen) { - kfree(kkey); + kfree_sensitive(kkey); memzero_explicit(&kcs, sizeof(kcs)); return -EINVAL; } if (copy_to_user(kcs.key, kkey, klen)) { - kfree(kkey); + kfree_sensitive(kkey); memzero_explicit(&kcs, sizeof(kcs)); return -EFAULT; } @@ -1398,7 +1393,7 @@ static long pkey_unlocked_ioctl(struct file *filp, un= signed int cmd, if (copy_to_user(ucs, &kcs, sizeof(kcs))) rc =3D -EFAULT; memzero_explicit(&kcs, sizeof(kcs)); - kfree(kkey); + kfree_sensitive(kkey); break; } case PKEY_VERIFYKEY2: { @@ -1415,7 +1410,7 @@ static long pkey_unlocked_ioctl(struct file *filp, un= signed int cmd, &kvk.cardnr, &kvk.domain, &kvk.type, &kvk.size, &kvk.flags); DEBUG_DBG("%s pkey_verifykey2()=3D%d\n", __func__, rc); - kfree(kkey); + kfree_sensitive(kkey); if (rc) break; if (copy_to_user(uvk, &kvk, sizeof(kvk))) @@ -1443,10 +1438,9 @@ static long pkey_unlocked_ioctl(struct file *filp, u= nsigned int cmd, DEBUG_DBG("%s pkey_keyblob2pkey2()=3D%d\n", __func__, rc); kfree(apqns); kfree_sensitive(kkey); - if (rc) - break; - if (copy_to_user(utp, &ktp, sizeof(ktp))) - return -EFAULT; + if (!rc && copy_to_user(utp, &ktp, sizeof(ktp))) + rc =3D -EFAULT; + memzero_explicit(&ktp, sizeof(ktp)); break; } case PKEY_APQNS4K: { @@ -1474,7 +1468,7 @@ static long pkey_unlocked_ioctl(struct file *filp, un= signed int cmd, rc =3D pkey_apqns4key(kkey, kak.keylen, kak.flags, apqns, &nr_apqns); DEBUG_DBG("%s pkey_apqns4key()=3D%d\n", __func__, rc); - kfree(kkey); + kfree_sensitive(kkey); if (rc && rc !=3D -ENOSPC) { kfree(apqns); break; @@ -1560,7 +1554,7 @@ static long pkey_unlocked_ioctl(struct file *filp, un= signed int cmd, protkey =3D kmalloc(protkeylen, GFP_KERNEL); if (!protkey) { kfree(apqns); - kfree(kkey); + kfree_sensitive(kkey); return -ENOMEM; } rc =3D pkey_keyblob2pkey3(apqns, ktp.apqn_entries, kkey, @@ -1570,20 +1564,20 @@ static long pkey_unlocked_ioctl(struct file *filp, = unsigned int cmd, kfree(apqns); kfree_sensitive(kkey); if (rc) { - kfree(protkey); + kfree_sensitive(protkey); break; } if (ktp.pkey && ktp.pkeylen) { if (protkeylen > ktp.pkeylen) { - kfree(protkey); + kfree_sensitive(protkey); return -EINVAL; } if (copy_to_user(ktp.pkey, protkey, protkeylen)) { - kfree(protkey); + kfree_sensitive(protkey); return -EFAULT; } } - kfree(protkey); + kfree_sensitive(protkey); ktp.pkeylen =3D protkeylen; if (copy_to_user(utp, &ktp, sizeof(ktp))) return -EFAULT; --=20 2.25.1