Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1661251

[PATCH 07/23] KEYS: encrypted: avoid encrypting/decrypting stack buffers

From David Howells <dhowells@redhat.com>
Newsgroups linux.kernel
Subject [PATCH 07/23] KEYS: encrypted: avoid encrypting/decrypting stack buffers
Date 2017-06-08 16:00 +0200
Message-ID <tQ68F-50i-1@gated-at.bofh.it> (permalink)
References <tQ5Z0-4WL-11@gated-at.bofh.it>
Organization Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903

Show all headers | View raw


From: Eric Biggers <ebiggers@google.com>

Since v4.9, the crypto API cannot (normally) be used to encrypt/decrypt
stack buffers because the stack may be virtually mapped.  Fix this for
the padding buffers in encrypted-keys by using ZERO_PAGE for the
encryption padding and by allocating a temporary heap buffer for the
decryption padding.

Tested with CONFIG_DEBUG_SG=y:
	keyctl new_session
	keyctl add user master "abcdefghijklmnop" @s
	keyid=$(keyctl add encrypted desc "new user:master 25" @s)
	datablob="$(keyctl pipe $keyid)"
	keyctl unlink $keyid
	keyid=$(keyctl add encrypted desc "load $datablob" @s)
	datablob2="$(keyctl pipe $keyid)"
	[ "$datablob" = "$datablob2" ] && echo "Success!"

Cc: Andy Lutomirski <luto@kernel.org>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Mimi Zohar <zohar@linux.vnet.ibm.com>
Cc: stable@vger.kernel.org # 4.9+
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: David Howells <dhowells@redhat.com>
---

 security/keys/encrypted-keys/encrypted.c |   17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/security/keys/encrypted-keys/encrypted.c b/security/keys/encrypted-keys/encrypted.c
index 2ab48eab29a1..d14f1a47a130 100644
--- a/security/keys/encrypted-keys/encrypted.c
+++ b/security/keys/encrypted-keys/encrypted.c
@@ -479,12 +479,9 @@ static int derived_key_encrypt(struct encrypted_key_payload *epayload,
 	struct skcipher_request *req;
 	unsigned int encrypted_datalen;
 	u8 iv[AES_BLOCK_SIZE];
-	unsigned int padlen;
-	char pad[16];
 	int ret;
 
 	encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
-	padlen = encrypted_datalen - epayload->decrypted_datalen;
 
 	req = init_skcipher_req(derived_key, derived_keylen);
 	ret = PTR_ERR(req);
@@ -492,11 +489,10 @@ static int derived_key_encrypt(struct encrypted_key_payload *epayload,
 		goto out;
 	dump_decrypted_data(epayload);
 
-	memset(pad, 0, sizeof pad);
 	sg_init_table(sg_in, 2);
 	sg_set_buf(&sg_in[0], epayload->decrypted_data,
 		   epayload->decrypted_datalen);
-	sg_set_buf(&sg_in[1], pad, padlen);
+	sg_set_page(&sg_in[1], ZERO_PAGE(0), AES_BLOCK_SIZE, 0);
 
 	sg_init_table(sg_out, 1);
 	sg_set_buf(sg_out, epayload->encrypted_data, encrypted_datalen);
@@ -583,9 +579,14 @@ static int derived_key_decrypt(struct encrypted_key_payload *epayload,
 	struct skcipher_request *req;
 	unsigned int encrypted_datalen;
 	u8 iv[AES_BLOCK_SIZE];
-	char pad[16];
+	u8 *pad;
 	int ret;
 
+	/* Throwaway buffer to hold the unused zero padding at the end */
+	pad = kmalloc(AES_BLOCK_SIZE, GFP_KERNEL);
+	if (!pad)
+		return -ENOMEM;
+
 	encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
 	req = init_skcipher_req(derived_key, derived_keylen);
 	ret = PTR_ERR(req);
@@ -593,13 +594,12 @@ static int derived_key_decrypt(struct encrypted_key_payload *epayload,
 		goto out;
 	dump_encrypted_data(epayload, encrypted_datalen);
 
-	memset(pad, 0, sizeof pad);
 	sg_init_table(sg_in, 1);
 	sg_init_table(sg_out, 2);
 	sg_set_buf(sg_in, epayload->encrypted_data, encrypted_datalen);
 	sg_set_buf(&sg_out[0], epayload->decrypted_data,
 		   epayload->decrypted_datalen);
-	sg_set_buf(&sg_out[1], pad, sizeof pad);
+	sg_set_buf(&sg_out[1], pad, AES_BLOCK_SIZE);
 
 	memcpy(iv, epayload->iv, sizeof(iv));
 	skcipher_request_set_crypt(req, sg_in, sg_out, encrypted_datalen, iv);
@@ -611,6 +611,7 @@ static int derived_key_decrypt(struct encrypted_key_payload *epayload,
 		goto out;
 	dump_decrypted_data(epayload);
 out:
+	kfree(pad);
 	return ret;
 }
 

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH 00/23] KEYS: Fixes David Howells <dhowells@redhat.com> - 2017-06-08 15:50 +0200
  [PATCH 05/23] KEYS: Delete an error message for a failed memory  allocation in get_derived_key() David Howells <dhowells@redhat.com> - 2017-06-08 15:50 +0200
  [PATCH 18/23] KEYS: DH: forbid using digest_null as the KDF hash David Howells <dhowells@redhat.com> - 2017-06-08 15:50 +0200
  [PATCH 10/23] KEYS: encrypted: use constant-time HMAC comparison David Howells <dhowells@redhat.com> - 2017-06-08 15:50 +0200
  [PATCH 04/23] X.509: Fix error code in x509_cert_parse() David Howells <dhowells@redhat.com> - 2017-06-08 15:50 +0200
  [PATCH 07/23] KEYS: encrypted: avoid encrypting/decrypting stack  buffers David Howells <dhowells@redhat.com> - 2017-06-08 16:00 +0200
  [PATCH 20/23] KEYS: DH: ensure the KDF counter is properly aligned David Howells <dhowells@redhat.com> - 2017-06-08 16:00 +0200
  [PATCH 19/23] KEYS: DH: don't feed uninitialized "otherinfo" into  KDF David Howells <dhowells@redhat.com> - 2017-06-08 16:00 +0200
  [PATCH 16/23] KEYS: trusted: sanitize all key material David Howells <dhowells@redhat.com> - 2017-06-08 16:00 +0200
  [PATCH 21/23] KEYS: DH: add __user annotations to keyctl_kdf_params David Howells <dhowells@redhat.com> - 2017-06-08 16:00 +0200
  [PATCH 13/23] KEYS: sanitize add_key() and keyctl() key payloads David Howells <dhowells@redhat.com> - 2017-06-08 16:00 +0200
  [PATCH 14/23] KEYS: user_defined: sanitize key payloads David Howells <dhowells@redhat.com> - 2017-06-08 16:00 +0200
  [PATCH 22/23] crypto : asymmetric_keys : verify_pefile:zero memory  content before freeing David Howells <dhowells@redhat.com> - 2017-06-08 16:00 +0200
  [PATCH 23/23] KEYS: Convert KEYCTL_DH_COMPUTE to use the crypto KPP  API David Howells <dhowells@redhat.com> - 2017-06-08 16:00 +0200
  [PATCH 11/23] KEYS: fix dereferencing NULL payload with nonzero  length David Howells <dhowells@redhat.com> - 2017-06-08 16:00 +0200
  [PATCH 09/23] KEYS: encrypted: fix race causing incorrect HMAC  calculations David Howells <dhowells@redhat.com> - 2017-06-08 16:00 +0200
  [PATCH 08/23] KEYS: encrypted: fix buffer overread in  valid_master_desc() David Howells <dhowells@redhat.com> - 2017-06-08 16:10 +0200
  [PATCH 02/23] security: use READ_ONCE instead of deprecated  ACCESS_ONCE David Howells <dhowells@redhat.com> - 2017-06-08 16:10 +0200
  Re: [PATCH 00/23] KEYS: Fixes James Morris <jmorris@namei.org> - 2017-06-08 16:40 +0200
    Re: [PATCH 00/23] KEYS: Fixes David Howells <dhowells@redhat.com> - 2017-06-08 16:50 +0200

csiph-web