From nobody Sat Feb 7 08:53:18 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 3A1171BDAA2; Thu, 28 Nov 2024 16:42:51 +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=1732812174; cv=none; b=q2cYcuL6zgX9IGsIAU2WW095puNYXBvZxbZU7u0XIrPjm1EnqdYsUOU8B+H/lyZ0B/kMxMV9HwCZLRYmsPF36slBO/dVobSgJMgsynBB66gtfy3EqaEkwqmhFmv3Vdphqg5aKj9BUP6C/gVe0BxSyoXDpN2NgD7SRzuq56RIFNQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732812174; c=relaxed/simple; bh=4JR8n7i8hrMN5nsyIy34oom7vNO6U4C8v8nvZ+yDxao=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=a6IqBWTBaB1tECb63yCDU54PYw9RmhFvoMTGQj1XyxdwtiCM7doMmqxJZcpOhsFq4W4NG+USiwB9TGCYmKSYMCGIEdP8dkisQqeQLNsQdCeI0X+m3IGFyg1fQnrk8zVNhkI3Fx5b4dyjV1uD3r0w4CJl2YBNsxUPRkrGssASSCg= 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.169) with Microsoft SMTP Server (TLS) id 14.3.498.0; Thu, 28 Nov 2024 19:42:47 +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 19:42:46 +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.6 1/3] s390/pkey: Use kfree_sensitive() to fix Coccinelle warnings Date: Thu, 28 Nov 2024 08:42:37 -0800 Message-ID: <20241128164239.21136-2-n.zhandarovich@fintech.ru> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20241128164239.21136-1-n.zhandarovich@fintech.ru> References: <20241128164239.21136-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 d2ffdf2491da..69bb4db78b69 100644 --- a/drivers/s390/crypto/pkey_api.c +++ b/drivers/s390/crypto/pkey_api.c @@ -1495,8 +1495,7 @@ static long pkey_unlocked_ioctl(struct file *filp, un= signed int cmd, rc =3D pkey_keyblob2pkey(kkey, ktp.keylen, ktp.protkey.protkey, &ktp.protkey.len, &ktp.protkey.type); 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))) @@ -1632,8 +1631,7 @@ static long pkey_unlocked_ioctl(struct file *filp, un= signed int cmd, &ktp.protkey.type); 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))) @@ -1759,8 +1757,7 @@ static long pkey_unlocked_ioctl(struct file *filp, un= signed int cmd, protkey, &protkeylen, &ktp.pkeytype); 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 Sat Feb 7 08:53:18 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 A483F1C07E6; Thu, 28 Nov 2024 16:42:54 +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=1732812176; cv=none; b=lpP8YnqnpaYxNLAZQRLFYS/8iAwJbC/pvLCMElZMUMvHsgIiqIDgLWpICfHg0TZHTtp9MlpIKHtYD2MnoQ01l7Xq5nYP391qNxQYn0ziFdw7cB42D7dK+BtcIYh4k9iPaKGzrbhMgdyZvgCbef0HxpZijrrzRtrWGIdFmzgpCAo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732812176; c=relaxed/simple; bh=4k/R3JjpHFvP0iAxCD9mKyN+oHsQKX0ynxCj07nrem8=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=V4y5ST3yey7XGlbJJwRhXikAkdN1N7qgOrQNspo+ZyqZpqOiQNSOiloHLp/7z0l43HQqfxKj8IYsm/NywV4p4UzUjTkQ3isFlseK2CsXdV+UBZJjAk31LZZw4b3dFEqq4Do10n9U8mTFND5hOhHYGcqEfPGRCPWgytkDu3yFDRI= 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.169) with Microsoft SMTP Server (TLS) id 14.3.498.0; Thu, 28 Nov 2024 19:42:50 +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 19:42:50 +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.6 2/3] s390/pkey: Wipe copies of clear-key structures on failure Date: Thu, 28 Nov 2024 08:42:38 -0800 Message-ID: <20241128164239.21136-3-n.zhandarovich@fintech.ru> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20241128164239.21136-1-n.zhandarovich@fintech.ru> References: <20241128164239.21136-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 69bb4db78b69..87df60710ad3 100644 --- a/drivers/s390/crypto/pkey_api.c +++ b/drivers/s390/crypto/pkey_api.c @@ -1366,9 +1366,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; @@ -1401,9 +1399,7 @@ static long pkey_unlocked_ioctl(struct file *filp, un= signed int cmd, kcp.protkey.protkey, &kcp.protkey.len, &kcp.protkey.type); 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; @@ -1554,11 +1550,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, @@ -1568,15 +1567,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 Sat Feb 7 08:53:18 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 A9CFD1C1AAA; Thu, 28 Nov 2024 16:42:56 +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=1732812178; cv=none; b=MsyMgOC+IsVU6crLPgF9zcDpJMPkDlO0WWYWjfynWt16XpkUhCsi1j6l2tsylK/vmULF3C2nbQ9HUdUUD1xpHFEX7A1QX3fVmdCnOMTGo5ZtpBUwHhC0gfuhN0twB8Tc58YnyaZHkPakeK/8/yQrCfxRjk3nndYNMoAgR/bxbdY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732812178; c=relaxed/simple; bh=tKK8ruQDumGOJjndJKLB2AkpnJqV0FD4BSz5B3JLcHg=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Fl4wb6Vcm0na8n34/UzzHUycOgX5GbAozdMlIRgaT5cqwnpICFXSfHu/hP8lflya3QCZ8YkM2dO31iAbPz900Si747q+OxYhCmC4hxRxMihNQLEjINNm2tVTxRXB1j8CavKO07xeS4CbYUTI+IlDhj1tU9YJM3sUUlwjNrSZv+E= 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.169) with Microsoft SMTP Server (TLS) id 14.3.498.0; Thu, 28 Nov 2024 19:42: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 19:42:52 +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.6 3/3] s390/pkey: Wipe copies of protected- and secure-keys Date: Thu, 28 Nov 2024 08:42:39 -0800 Message-ID: <20241128164239.21136-4-n.zhandarovich@fintech.ru> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20241128164239.21136-1-n.zhandarovich@fintech.ru> References: <20241128164239.21136-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 87df60710ad3..1da370bd9fd5 100644 --- a/drivers/s390/crypto/pkey_api.c +++ b/drivers/s390/crypto/pkey_api.c @@ -1351,10 +1351,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: { @@ -1382,10 +1381,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: { @@ -1429,10 +1427,9 @@ static long pkey_unlocked_ioctl(struct file *filp, u= nsigned int cmd, rc =3D pkey_skey2pkey(ksp.seckey.seckey, ksp.protkey.protkey, &ksp.protkey.len, &ksp.protkey.type); 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: { @@ -1444,10 +1441,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: { @@ -1460,10 +1456,9 @@ static long pkey_unlocked_ioctl(struct file *filp, u= nsigned int cmd, rc =3D pkey_genprotkey(kgp.keytype, kgp.protkey.protkey, &kgp.protkey.len, &kgp.protkey.type); 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: { @@ -1475,6 +1470,7 @@ static long pkey_unlocked_ioctl(struct file *filp, un= signed int cmd, rc =3D pkey_verifyprotkey(kvp.protkey.protkey, kvp.protkey.len, kvp.protkey.type); DEBUG_DBG("%s pkey_verifyprotkey()=3D%d\n", __func__, rc); + memzero_explicit(&kvp, sizeof(kvp)); break; } case PKEY_KBLOB2PROTK: { @@ -1492,10 +1488,9 @@ static long pkey_unlocked_ioctl(struct file *filp, u= nsigned int cmd, &ktp.protkey.len, &ktp.protkey.type); 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: { @@ -1521,23 +1516,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: { @@ -1566,18 +1561,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; } @@ -1586,7 +1581,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: { @@ -1603,7 +1598,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))) @@ -1634,10 +1629,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: { @@ -1665,7 +1659,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; @@ -1751,7 +1745,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, @@ -1761,20 +1755,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