Skip to contentSkip to conversation

Fix BUG() in calc_seckey()

PATCH In review Sachin Prabhu 2 participants 3 messages Oct 17, 2016 – Oct 18, 2016 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix BUG() in calc_seckey()

Andy Lutromirski's new virtually mapped kernel stack allocations moves kernel stacks the vmalloc area. This triggers the bug
kernel BUG at ./include/linux/scatterlist.h:140!
at calc_seckey()->sg_init()

---
 fs/cifs/cifsencrypt.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
fs/cifs/cifsencrypt.c +8 -3
index 8347c90..5eb0412 100644
@@ -808,7 +808,11 @@ calc_seckey(struct cifs_ses *ses)
 	struct crypto_skcipher *tfm_arc4;
 	struct scatterlist sgin, sgout;
 	struct skcipher_request *req;
-	unsigned char sec_key[CIFS_SESS_KEY_SIZE]; /* a nonce */
+	unsigned char *sec_key;
+
+	sec_key = kmalloc(CIFS_SESS_KEY_SIZE, GFP_KERNEL);
+	if (sec_key == NULL)
+		return -ENOMEM;
 
 	get_random_bytes(sec_key, CIFS_SESS_KEY_SIZE);
 
@@ -816,7 +820,7 @@ calc_seckey(struct cifs_ses *ses)
 	if (IS_ERR(tfm_arc4)) {
 		rc = PTR_ERR(tfm_arc4);
 		cifs_dbg(VFS, "could not allocate crypto API arc4\n");
-		return rc;
+		goto out;
 	}
 
 	rc = crypto_skcipher_setkey(tfm_arc4, ses->auth_key.response,
@@ -854,7 +858,8 @@ calc_seckey(struct cifs_ses *ses)
 
 out_free_cipher:
 	crypto_free_skcipher(tfm_arc4);
-
+out:
+	kfree(sec_key);
 	return rc;
 }
 
Signature · 1 line
2.7.4
#2 lore raw
In reply to a message not in the archive

A bit more reference for this patch

Information about the patch series which seems to have caused this problem is available at

http://lwn.net/Articles/692208/

For the reasons described in the article, the kernel now allocates the stack from the vmalloc region. The buffers used in calc_seckey() are therefore now situated in the vmalloc-ed region. This triggers the check in

static inline void sg_set_buf(struct scatterlist *sg, const void *buf, unsigned int buflen)
{
#ifdef CONFIG_DEBUG_SG
        BUG_ON(!virt_addr_valid(buf));
#endif
..
}

This issue was also addressed in Linus's release notes at
http://lwn.net/Articles/703664/

"
The virtual stack mapping also happens to mean that people who try to do DMA from temporary buffers on the stack ("Don't do it!") now really need to change their evil ways. So there is some fallout from this, and I expect a couple of drivers to need minor fixes. But it's all for a good cause, really (and it isn't all that common, because doing DMA from the stack really has never been a good idea, and is generally not even workable in most situations).
"

Sachin Prabhu

Quoted 49 lines · Sachin Prabhu

Andy Lutromirski's new virtually mapped kernel stack allocations moves
kernel stacks the vmalloc area. This triggers the bug kernel BUG at ./include/linux/scatterlist.h:140!
at calc_seckey()->sg_init()

---
 fs/cifs/cifsencrypt.c | 11 ++++++++---

 1 file changed, 8 insertions(+), 3 deletions(-)
fs/cifs/cifsencrypt.c +5 -1
index 8347c90..5eb0412 100644
@@ -808,7 +808,11 @@ calc_seckey(struct cifs_ses *ses)
  	struct crypto_skcipher *tfm_arc4;
  	struct scatterlist sgin, sgout;
  	struct skcipher_request *req;
-	unsigned char sec_key[CIFS_SESS_KEY_SIZE]; /* a nonce */
+	unsigned char *sec_key;
+
+	sec_key = kmalloc(CIFS_SESS_KEY_SIZE, GFP_KERNEL);
+	if (sec_key == NULL)
+		return -ENOMEM;


  get_random_bytes(sec_key, CIFS_SESS_KEY_SIZE);

fs/cifs/cifsencrypt.c +0 -0
@@ -816,7 +820,7 @@ calc_seckey(struct cifs_ses *ses)
  	if (IS_ERR(tfm_arc4)) {
  		rc = PTR_ERR(tfm_arc4);

  cifs_dbg(VFS, "could not allocate crypto API arc4\n");

fs/cifs/cifsencrypt.c +1 -1
-		return rc;
+		goto out;

  }

  rc = crypto_skcipher_setkey(tfm_arc4, ses-

Quoted 1 line

auth_key.response,

fs/cifs/cifsencrypt.c +0 -0
@@ -854,7 +858,8 @@ calc_seckey(struct cifs_ses *ses)


 out_free_cipher:
  crypto_free_skcipher(tfm_arc4);

fs/cifs/cifsencrypt.c +2 -1
-
+out:
+	kfree(sec_key);

  return rc;
 }

Reviewed-by
#3 lore raw
In reply to a message not in the archive
Quoted 37 lines · Sachin Prabhu

Andy Lutromirski's new virtually mapped kernel stack allocations moves kernel stacks the vmalloc area. This triggers the bug
kernel BUG at ./include/linux/scatterlist.h:140!
at calc_seckey()->sg_init()

---
 fs/cifs/cifsencrypt.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
fs/cifs/cifsencrypt.c +6 -2
index 8347c90..5eb0412 100644
@@ -808,7 +808,11 @@ calc_seckey(struct cifs_ses *ses)
 	struct crypto_skcipher *tfm_arc4;
 	struct scatterlist sgin, sgout;
 	struct skcipher_request *req;
-	unsigned char sec_key[CIFS_SESS_KEY_SIZE]; /* a nonce */
+	unsigned char *sec_key;
+
+	sec_key = kmalloc(CIFS_SESS_KEY_SIZE, GFP_KERNEL);
+	if (sec_key == NULL)
+		return -ENOMEM;
 
 	get_random_bytes(sec_key, CIFS_SESS_KEY_SIZE);
 
@@ -816,7 +820,7 @@ calc_seckey(struct cifs_ses *ses)
 	if (IS_ERR(tfm_arc4)) {
 		rc = PTR_ERR(tfm_arc4);
 		cifs_dbg(VFS, "could not allocate crypto API arc4\n");
-		return rc;
+		goto out;
 	}
 
 	rc = crypto_skcipher_setkey(tfm_arc4, ses->auth_key.response,

fs/cifs/cifsencrypt.c calc_seckey(struct cifs_ses *ses) · line 865

Quoted 9 lines
fs/cifs/cifsencrypt.c +2 -1
@@ -854,7 +858,8 @@ calc_seckey(struct cifs_ses *ses)
 
 out_free_cipher:
 	crypto_free_skcipher(tfm_arc4);
-
+out:
+	kfree(sec_key);
 	return rc;
 }