Skip to contentSkip to conversation

vhost: zero vhost_vsock memory on allocation

PATCH In review Vitaly Mayatskikh 3 participants 8 messages Mar 27, 2019 – Apr 5, 2019

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

PATCH 1/1 vhost: zero vhost_vsock memory on allocation

This fixes OOPS when using under-initialized vhost_vsock object.

The code had a combo of kzalloc plus vmalloc as a fallback initially, but it has been replaced by plain kvmalloc in
commit 6c5ab6511f71 ("mm: support __GFP_REPEAT in kvmalloc_node for >32kB")

OOPS is easy to reproduce with open/ioctl after trashing the RAM.

---
 drivers/vhost/vsock.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/vhost/vsock.c +1 -1
index bb5fc0e..9e7cb13 100644
@@ -512,7 +512,7 @@ static int vhost_vsock_dev_open(struct inode *inode, struct file *file)
 	/* This struct is large and allocation could fail, fall back to vmalloc
 	 * if there is no other way.
 	 */
-	vsock = kvmalloc(sizeof(*vsock), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
+	vsock = kvzalloc(sizeof(*vsock), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!vsock)
 		return -ENOMEM;
 
Signature · 1 line
1.8.3.1
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Vitaly Mayatskikh

This fixes OOPS when using under-initialized vhost_vsock object.

The code had a combo of kzalloc plus vmalloc as a fallback initially, but it has been replaced by plain kvmalloc in
commit 6c5ab6511f71 ("mm: support __GFP_REPEAT in kvmalloc_node for >32kB")

OOPS is easy to reproduce with open/ioctl after trashing the RAM.

Sorry for the screw up, that was certainly not inteded effect of the patch.

Fixes 6c5ab6511f71 ("mm: support __GFP_REPEAT in kvmalloc_node for >32kB")
Cc stable # 4.12+
Quoted 1 line

Thanks for catching that.

Quoted 20 lines
---
 drivers/vhost/vsock.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/vhost/vsock.c +1 -1
index bb5fc0e..9e7cb13 100644
@@ -512,7 +512,7 @@ static int vhost_vsock_dev_open(struct inode *inode, struct file *file)
 	/* This struct is large and allocation could fail, fall back to vmalloc
 	 * if there is no other way.
 	 */
-	vsock = kvmalloc(sizeof(*vsock), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
+	vsock = kvzalloc(sizeof(*vsock), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!vsock)
 		return -ENOMEM;
 
Signature · 1 line
1.8.3.1
Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer
#2 lore raw
In reply to Vitaly Mayatskikh
Quoted 7 lines · Vitaly Mayatskikh

This fixes OOPS when using under-initialized vhost_vsock object.

The code had a combo of kzalloc plus vmalloc as a fallback initially, but it has been replaced by plain kvmalloc in
commit 6c5ab6511f71 ("mm: support __GFP_REPEAT in kvmalloc_node for >32kB")

OOPS is easy to reproduce with open/ioctl after trashing the RAM.

Which field was accessed before initialization?

I ask because the situation is now unclear since code remains that assumes vsock is *not* zero-initialized:

vsock->guest_cid = 0; /* no CID assigned yet */

  atomic_set(&vsock->queued_replies, 0);

If we're going to zalloc, let's get rid of explicit zero initializations. Or let's use kvmalloc() and fix the uninitialized access. Mixing both is confusing.

Quoted 21 lines
---
 drivers/vhost/vsock.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/vhost/vsock.c +1 -1
index bb5fc0e..9e7cb13 100644
@@ -512,7 +512,7 @@ static int vhost_vsock_dev_open(struct inode *inode, struct file *file)
 	/* This struct is large and allocation could fail, fall back to vmalloc
 	 * if there is no other way.
 	 */
-	vsock = kvmalloc(sizeof(*vsock), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
+	vsock = kvzalloc(sizeof(*vsock), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
 	if (!vsock)
 		return -ENOMEM;
 
Signature · 1 line
1.8.3.1
#3 lore raw
Quoted 8 lines · Stefan Hajnoczi

Which field was accessed before initialization?

I ask because the situation is now unclear since code remains that assumes vsock is *not* zero-initialized:

vsock->guest_cid = 0; /* no CID assigned yet */

  atomic_set(&vsock->queued_replies, 0);

It was hash.

Quoted 3 lines

If we're going to zalloc, let's get rid of explicit zero initializations. Or let's use kvmalloc() and fix the uninitialized access. Mixing both is confusing.

I would go with zalloc, since it's easier to read and it prevents further situations like this.
A zalloc was there originally (not in fallback though).

maintainer
#4 lore raw
Quoted 4 lines · Vitaly Mayatskih
Quoted 2 lines · Stefan Hajnoczi

Which field was accessed before initialization?

Quoted 8 lines
Quoted 6 lines

I ask because the situation is now unclear since code remains that assumes vsock is *not* zero-initialized:

vsock->guest_cid = 0; /* no CID assigned yet */

  atomic_set(&vsock->queued_replies, 0);

It was hash.

vsock->hash is only read if vsock->guest_cid has already been set and hence vsock->hash has been initialized too. I don't see where the problem is.

Was your tree missing commit a72b69dc083a931422cc8a5e33841aff7d5312f2 ("vhost/vsock: fix uninitialized vhost_vsock->guest_cid")?

Are you sure the crash can be reproduced on linux.git/master?

Sorry for insisting on so much information but I want to make I fully understand the issue you encountered.

Quoted 7 lines
Quoted 3 lines

If we're going to zalloc, let's get rid of explicit zero initializations. Or let's use kvmalloc() and fix the uninitialized access. Mixing both is confusing.

I would go with zalloc, since it's easier to read and it prevents further situations like this.
A zalloc was there originally (not in fallback though).

Sounds good. Please send a v2 that also removes the explicit zero initialization since it's no longer needed with zalloc.

#5 lore raw
Quoted 8 lines · Stefan Hajnoczi

vsock->hash is only read if vsock->guest_cid has already been set and hence vsock->hash has been initialized too. I don't see where the problem is.

Was your tree missing commit a72b69dc083a931422cc8a5e33841aff7d5312f2 ("vhost/vsock: fix uninitialized vhost_vsock->guest_cid")?

Are you sure the crash can be reproduced on linux.git/master?

You are right: we hit it on 4.14.91 while the guest_cid fix was backported in 4.14.93.
Thus not a real issue. Sorry for confusion, vhost is something still new to me.

Quoted 2 lines

Sounds good. Please send a v2 that also removes the explicit zero initialization since it's no longer needed with zalloc.

I changed my mind, all the rest is initialized explicitly, let's keep it that way.
Will send the patch shortly.

Signature · 1 line
wbr, Vitaly
#6 lore raw

PATCH 1/1 vhost/vsock: initialize vhost_vsock->hash

There's no current valid use case when uninitialized hash can be read before being written, however let's keep every vhost_vsock field initialized just for clarity.

---
 drivers/vhost/vsock.c | 1 +
 1 file changed, 1 insertion(+)
drivers/vhost/vsock.c +1 -0
index bb5fc0e..f43f4ed 100644
@@ -537,6 +537,7 @@ static int vhost_vsock_dev_open(struct inode *inode, struct file *file)
 	spin_lock_init(&vsock->send_pkt_list_lock);
 	INIT_LIST_HEAD(&vsock->send_pkt_list);
 	vhost_work_init(&vsock->send_pkt_work, vhost_transport_send_pkt_work);
+	INIT_HLIST_NODE(&vsock->hash);
 	return 0;
 
 out:
Signature · 1 line
1.8.3.1
maintainer Reviewed-by on v1 1/1
#7 lore raw

Re: [PATCH] vhost/vsock: initialize vhost_vsock->hash

Quoted 23 lines · Vitaly Mayatskikh

There's no current valid use case when uninitialized hash can be read before being written, however let's keep every vhost_vsock field initialized just for clarity.

---
 drivers/vhost/vsock.c | 1 +
 1 file changed, 1 insertion(+)
drivers/vhost/vsock.c +1 -0
index bb5fc0e..f43f4ed 100644
@@ -537,6 +537,7 @@ static int vhost_vsock_dev_open(struct inode *inode, struct file *file)
 	spin_lock_init(&vsock->send_pkt_list_lock);
 	INIT_LIST_HEAD(&vsock->send_pkt_list);
 	vhost_work_init(&vsock->send_pkt_work, vhost_transport_send_pkt_work);
+	INIT_HLIST_NODE(&vsock->hash);
 	return 0;
 
 out:
Signature · 1 line
1.8.3.1