Vulnerability: An issue was discovered in the Linux kernel before 4.19.3. crypto_report_one() and related functions in crypto/crypto_user.c (the crypto user configuration API) do not fully initialize structures that are copied to userspace, potentially leaking sensitive memory to user programs. NOTE: this is a CVE-2013-2547 regression but with easier exploitability because the attacker does not need a capability (however, the system must have the CONFIG_CRYPTO_USER kconfig option).
Vulnerability ID: CVE-2018-19854
CWE: CWE-200
Severity score: 2.9
Project: https://github.com/torvalds/linux
Published: 2018-12-04
File: crypto/crypto_user_base.c
Commit message: crypto: user - fix leaking uninitialized memory to userspace

All bytes of the NETLINK_CRYPTO report structures must be initialized,
since they are copied to userspace.  The change from strncpy() to
strlcpy() broke this.  As a minimal fix, change it back.

Fixes: 4473710df1f8 ("crypto: user - Prepare for CRYPTO_MAX_ALG_NAME expansion")
Cc: <stable@vger.kernel.org> # v4.12+
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Commit author: Eric Biggers

Vulnerable version (before fix):
 static int crypto_report_cipher(struct sk_buff *skb, struct crypto_alg *alg) { 	struct crypto_report_cipher rcipher;  	strlcpy(rcipher.type, "cipher", sizeof(rcipher.type));  	rcipher.blocksize = alg->cra_blocksize; 	rcipher.min_keysize = alg->cra_cipher.cia_min_keysize; static int crypto_report_comp(struct sk_buff *skb, struct crypto_alg *alg) { 	struct crypto_report_comp rcomp;  	strlcpy(rcomp.type, "compression", sizeof(rcomp.type)); 	if (nla_put(skb, CRYPTOCFGA_REPORT_COMPRESS, 		    sizeof(struct crypto_report_comp), &rcomp)) 		goto nla_put_failure; static int crypto_report_acomp(struct sk_buff *skb, struct crypto_alg *alg) { 	struct crypto_report_acomp racomp;  	strlcpy(racomp.type, "acomp", sizeof(racomp.type));  	if (nla_put(skb, CRYPTOCFGA_REPORT_ACOMP, 		    sizeof(struct crypto_report_acomp), &racomp)) static int crypto_report_akcipher(struct sk_buff *skb, struct crypto_alg *alg) { 	struct crypto_report_akcipher rakcipher;  	strlcpy(rakcipher.type, "akcipher", sizeof(rakcipher.type));  	if (nla_put(skb, CRYPTOCFGA_REPORT_AKCIPHER, 		    sizeof(struct crypto_report_akcipher), &rakcipher)) static int crypto_report_kpp(struct sk_buff *skb, struct crypto_alg *alg) { 	struct crypto_report_kpp rkpp;  	strlcpy(rkpp.type, "kpp", sizeof(rkpp.type));  	if (nla_put(skb, CRYPTOCFGA_REPORT_KPP, 		    sizeof(struct crypto_report_kpp), &rkpp)) static int crypto_report_kpp(struct sk_buff *skb, struct crypto_alg *alg) static int crypto_report_one(struct crypto_alg *alg, 			     struct crypto_user_alg *ualg, struct sk_buff *skb) { 	strlcpy(ualg->cru_name, alg->cra_name, sizeof(ualg->cru_name)); 	strlcpy(ualg->cru_driver_name, alg->cra_driver_name, 		sizeof(ualg->cru_driver_name)); 	strlcpy(ualg->cru_module_name, module_name(alg->cra_module), 		sizeof(ualg->cru_module_name));  	ualg->cru_type = 0; static int crypto_report_one(struct crypto_alg *alg, 	if (alg->cra_flags & CRYPTO_ALG_LARVAL) { 		struct crypto_report_larval rl;  		strlcpy(rl.type, "larval", sizeof(rl.type)); 		if (nla_put(skb, CRYPTOCFGA_REPORT_LARVAL, 			    sizeof(struct crypto_report_larval), &rl)) 			goto nla_put_failure;

Fixed version (after fix):
 static int crypto_report_cipher(struct sk_buff *skb, struct crypto_alg *alg) { 	struct crypto_report_cipher rcipher;  	strncpy(rcipher.type, "cipher", sizeof(rcipher.type));  	rcipher.blocksize = alg->cra_blocksize; 	rcipher.min_keysize = alg->cra_cipher.cia_min_keysize; static int crypto_report_comp(struct sk_buff *skb, struct crypto_alg *alg) { 	struct crypto_report_comp rcomp;  	strncpy(rcomp.type, "compression", sizeof(rcomp.type)); 	if (nla_put(skb, CRYPTOCFGA_REPORT_COMPRESS, 		    sizeof(struct crypto_report_comp), &rcomp)) 		goto nla_put_failure; static int crypto_report_acomp(struct sk_buff *skb, struct crypto_alg *alg) { 	struct crypto_report_acomp racomp;  	strncpy(racomp.type, "acomp", sizeof(racomp.type));  	if (nla_put(skb, CRYPTOCFGA_REPORT_ACOMP, 		    sizeof(struct crypto_report_acomp), &racomp)) static int crypto_report_akcipher(struct sk_buff *skb, struct crypto_alg *alg) { 	struct crypto_report_akcipher rakcipher;  	strncpy(rakcipher.type, "akcipher", sizeof(rakcipher.type));  	if (nla_put(skb, CRYPTOCFGA_REPORT_AKCIPHER, 		    sizeof(struct crypto_report_akcipher), &rakcipher)) static int crypto_report_kpp(struct sk_buff *skb, struct crypto_alg *alg) { 	struct crypto_report_kpp rkpp;  	strncpy(rkpp.type, "kpp", sizeof(rkpp.type));  	if (nla_put(skb, CRYPTOCFGA_REPORT_KPP, 		    sizeof(struct crypto_report_kpp), &rkpp)) static int crypto_report_kpp(struct sk_buff *skb, struct crypto_alg *alg) static int crypto_report_one(struct crypto_alg *alg, 			     struct crypto_user_alg *ualg, struct sk_buff *skb) { 	strncpy(ualg->cru_name, alg->cra_name, sizeof(ualg->cru_name)); 	strncpy(ualg->cru_driver_name, alg->cra_driver_name, 		sizeof(ualg->cru_driver_name)); 	strncpy(ualg->cru_module_name, module_name(alg->cra_module), 		sizeof(ualg->cru_module_name));  	ualg->cru_type = 0; static int crypto_report_one(struct crypto_alg *alg, 	if (alg->cra_flags & CRYPTO_ALG_LARVAL) { 		struct crypto_report_larval rl;  		strncpy(rl.type, "larval", sizeof(rl.type)); 		if (nla_put(skb, CRYPTOCFGA_REPORT_LARVAL, 			    sizeof(struct crypto_report_larval), &rl)) 			goto nla_put_failure;

Patch:
patch: @@ -84,7 +84,7 @@ static int crypto_report_cipher(struct sk_buff *skb, struct crypto_alg *alg)
 {
 	struct crypto_report_cipher rcipher;
 
-	strlcpy(rcipher.type, "cipher", sizeof(rcipher.type));
+	strncpy(rcipher.type, "cipher", sizeof(rcipher.type));
 
 	rcipher.blocksize = alg->cra_blocksize;
 	rcipher.min_keysize = alg->cra_cipher.cia_min_keysize;
@@ -103,7 +103,7 @@ static int crypto_report_comp(struct sk_buff *skb, struct crypto_alg *alg)
 {
 	struct crypto_report_comp rcomp;
 
-	strlcpy(rcomp.type, "compression", sizeof(rcomp.type));
+	strncpy(rcomp.type, "compression", sizeof(rcomp.type));
 	if (nla_put(skb, CRYPTOCFGA_REPORT_COMPRESS,
 		    sizeof(struct crypto_report_comp), &rcomp))
 		goto nla_put_failure;
@@ -117,7 +117,7 @@ static int crypto_report_acomp(struct sk_buff *skb, struct crypto_alg *alg)
 {
 	struct crypto_report_acomp racomp;
 
-	strlcpy(racomp.type, "acomp", sizeof(racomp.type));
+	strncpy(racomp.type, "acomp", sizeof(racomp.type));
 
 	if (nla_put(skb, CRYPTOCFGA_REPORT_ACOMP,
 		    sizeof(struct crypto_report_acomp), &racomp))
@@ -132,7 +132,7 @@ static int crypto_report_akcipher(struct sk_buff *skb, struct crypto_alg *alg)
 {
 	struct crypto_report_akcipher rakcipher;
 
-	strlcpy(rakcipher.type, "akcipher", sizeof(rakcipher.type));
+	strncpy(rakcipher.type, "akcipher", sizeof(rakcipher.type));
 
 	if (nla_put(skb, CRYPTOCFGA_REPORT_AKCIPHER,
 		    sizeof(struct crypto_report_akcipher), &rakcipher))
@@ -147,7 +147,7 @@ static int crypto_report_kpp(struct sk_buff *skb, struct crypto_alg *alg)
 {
 	struct crypto_report_kpp rkpp;
 
-	strlcpy(rkpp.type, "kpp", sizeof(rkpp.type));
+	strncpy(rkpp.type, "kpp", sizeof(rkpp.type));
 
 	if (nla_put(skb, CRYPTOCFGA_REPORT_KPP,
 		    sizeof(struct crypto_report_kpp), &rkpp))
@@ -161,10 +161,10 @@ static int crypto_report_kpp(struct sk_buff *skb, struct crypto_alg *alg)
 static int crypto_report_one(struct crypto_alg *alg,
 			     struct crypto_user_alg *ualg, struct sk_buff *skb)
 {
-	strlcpy(ualg->cru_name, alg->cra_name, sizeof(ualg->cru_name));
-	strlcpy(ualg->cru_driver_name, alg->cra_driver_name,
+	strncpy(ualg->cru_name, alg->cra_name, sizeof(ualg->cru_name));
+	strncpy(ualg->cru_driver_name, alg->cra_driver_name,
 		sizeof(ualg->cru_driver_name));
-	strlcpy(ualg->cru_module_name, module_name(alg->cra_module),
+	strncpy(ualg->cru_module_name, module_name(alg->cra_module),
 		sizeof(ualg->cru_module_name));
 
 	ualg->cru_type = 0;
@@ -177,7 +177,7 @@ static int crypto_report_one(struct crypto_alg *alg,
 	if (alg->cra_flags & CRYPTO_ALG_LARVAL) {
 		struct crypto_report_larval rl;
 
-		strlcpy(rl.type, "larval", sizeof(rl.type));
+		strncpy(rl.type, "larval", sizeof(rl.type));
 		if (nla_put(skb, CRYPTOCFGA_REPORT_LARVAL,
 			    sizeof(struct crypto_report_larval), &rl))
 			goto nla_put_failure;
path: crypto/crypto_user_base.c