vhost: zero vhost_vsock memory on allocation
Subsystems (2)
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
@@ -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
Quoted 7 lines · Vitaly Mayatskikh
This fixes OOPS when using under-initialized
vhost_vsockobject.The code had a combo of kzalloc plus vmalloc as a fallback initially, but it has been replaced by plain kvmalloc in
commit6c5ab6511f71("mm: support__GFP_REPEATinkvmalloc_nodefor >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.
Quoted 1 line
Signed-off-by Vitaly Mayatskikh <[email protected]>
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@@ -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
Stefan Hajnoczi
maintainerQuoted 7 lines · Vitaly Mayatskikh
This fixes OOPS when using under-initialized
vhost_vsockobject.The code had a combo of kzalloc plus vmalloc as a fallback initially, but it has been replaced by plain kvmalloc in
commit6c5ab6511f71("mm: support__GFP_REPEATinkvmalloc_nodefor >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
Signed-off-by Vitaly Mayatskikh <[email protected]>--- drivers/vhost/vsock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/vhost/vsock.c +1
-1@@ -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
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).
Stefan Hajnoczi
maintainerQuoted 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.
Quoted 8 lines · Stefan Hajnoczi
vsock->hashis only read ifvsock->guest_cidhas already been set and hencevsock->hashhas been initialized too. I don't see where the problem is.Was your tree missing commit
a72b69dc083a931422cc8a5e33841aff7d5312f2("vhost/vsock: fix uninitializedvhost_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
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
@@ -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
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_vsockfield initialized just for clarity.Signed-off-by Vitaly Mayatskikh <[email protected]>--- drivers/vhost/vsock.c | 1 + 1 file changed, 1 insertion(+)drivers/vhost/vsock.c +1
-0@@ -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