vsock: validate buffer min/max size in setsockopt
Subsystems (4)
PATCH 1/1 vsock: validate buffer min/max size in setsockopt
From: Rongguang Wei <[email protected]>
SO_VM_SOCKETS_BUFFER_MIN_SIZE and SO_VM_SOCKETS_BUFFER_MAX_SIZE do not cross-validate against each other, allowing userspace to set buffer_min_size > buffer_max_size.
When min > max, buffer_size is silently clamped to an incorrect value. For example, setting min=512KB then max=128 results in buffer_size=128 despite the user requesting much larger buffers via SO_VM_SOCKETS_BUFFER_SIZE.
Reproduced with a test program:
setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MIN_SIZE,
512 * 1024, sizeof(int)); setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE, 128, sizeof(int)); // User asked for 1MB but got 128 bytes silently setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE, 1024 * 1024, sizeof(int));
After that use getsockopt to get the buffer_size = 128 and buffer_min_size = 524288, buffer_max_size = 128. The buffer_min_size > buffer_max_size and the kernel accepted contradictory values without error.
Add value check to fix this issue. Return -EINVAL to userspace when setting MAX_SIZE to a value smaller than the current MIN_SIZE or setting MIN_SIZE to a value larger than the current MAX_SIZE.
--- net/vmw_vsock/af_vsock.c | 8 ++++++++ 1 file changed, 8 insertions(+)
net/vmw_vsock/af_vsock.c +8 -0
@@ -2050,12 +2050,20 @@ static int vsock_connectible_setsockopt(struct socket *sock,case SO_VM_SOCKETS_BUFFER_MAX_SIZE :COPY_IN (val); +if (val < vsk->buffer_min_size) { + err = -EINVAL ; + goto exit; + } vsk->buffer_max_size = val;vsock_update_buffer_size (vsk, transport, vsk->buffer_size);break ;case SO_VM_SOCKETS_BUFFER_MIN_SIZE :COPY_IN (val); +if (val > vsk->buffer_max_size) { + err = -EINVAL ; + goto exit; + } vsk->buffer_min_size = val;vsock_update_buffer_size (vsk, transport, vsk->buffer_size);break ;
Signature · 5 lines
2.25.1 No virus found Checked by Hillstone Network AntiVirus
Quoted 19 lines · On Mon, 24 Aug 2026 17:12:57 +0800 Rongguang Wei <[email protected]> wrote:
From: Rongguang Wei <[email protected]>
SO_VM_SOCKETS_BUFFER_MIN_SIZEandSO_VM_SOCKETS_BUFFER_MAX_SIZEdo not cross-validate against each other, allowing userspace to setbuffer_min_size>buffer_max_size.
When min > max,buffer_sizeis silently clamped to an incorrect value. For example, setting min=512KB then max=128 results in buffer_size=128 despite the user requesting much larger buffers viaSO_VM_SOCKETS_BUFFER_SIZE.Reproduced with a test program:
setsockopt(fd,AF_VSOCK,SO_VM_SOCKETS_BUFFER_MIN_SIZE,512 * 1024, sizeof(int)); setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE, 128, sizeof(int)); // User asked for 1MB but got 128 bytes silently setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE, 1024 * 1024, sizeof(int));
Quoted 8 lines
After that use getsockopt to get the
buffer_size= 128 andbuffer_min_size= 524288,buffer_max_size= 128. Thebuffer_min_size>buffer_max_sizeand the kernel accepted contradictory values without error.Add value check to fix this issue. Return
-EINVALto userspace when settingMAX_SIZEto a value smaller than the currentMIN_SIZEor settingMIN_SIZEto a value larger than the currentMAX_SIZE.
That is going to break userspace that sets the minimum before the maximum when the new minimum is larger than the old maximum.
David
Quoted 32 lines
Fixes b9f2b0ffde0c ("vsock: handle buffer_size sockopts in the core")Signed-off-by Rongguang Wei <[email protected]>--- net/vmw_vsock/af_vsock.c | 8 ++++++++ 1 file changed, 8 insertions(+)net/vmw_vsock/af_vsock.c +8
-0@@ -2050,12 +2050,20 @@ static int vsock_connectible_setsockopt(struct socket *sock,case SO_VM_SOCKETS_BUFFER_MAX_SIZE :COPY_IN (val); +if (val < vsk->buffer_min_size) { + err = -EINVAL ; + goto exit; + } vsk->buffer_max_size = val;vsock_update_buffer_size (vsk, transport, vsk->buffer_size);break ;case SO_VM_SOCKETS_BUFFER_MIN_SIZE :COPY_IN (val); +if (val > vsk->buffer_max_size) { + err = -EINVAL ; + goto exit; + } vsk->buffer_min_size = val;vsock_update_buffer_size (vsk, transport, vsk->buffer_size);break ;
Quoted 27 lines · David Laight
Quoted 24 lines · On Mon, 24 Aug 2026 17:12:57 +0800 Rongguang Wei <[email protected]> wrote:
From: Rongguang Wei <[email protected]>
SO_VM_SOCKETS_BUFFER_MIN_SIZEandSO_VM_SOCKETS_BUFFER_MAX_SIZEdo not cross-validate against each other, allowing userspace to setbuffer_min_size>buffer_max_size.
When min > max,buffer_sizeis silently clamped to an incorrect value. For example, setting min=512KB then max=128 results in buffer_size=128 despite the user requesting much larger buffers viaSO_VM_SOCKETS_BUFFER_SIZE.Reproduced with a test program:
setsockopt(fd,AF_VSOCK,SO_VM_SOCKETS_BUFFER_MIN_SIZE,512 * 1024, sizeof(int)); setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE, 128, sizeof(int)); // User asked for 1MB but got 128 bytes silently setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE, 1024 * 1024, sizeof(int));After that use getsockopt to get the
buffer_size= 128 andbuffer_min_size= 524288,buffer_max_size= 128. Thebuffer_min_size>buffer_max_sizeand the kernel accepted contradictory values without error.
Quoted 9 lines
Quoted 3 lines
Add value check to fix this issue. Return
-EINVALto userspace when settingMAX_SIZEto a value smaller than the currentMIN_SIZEor settingMIN_SIZEto a value larger than the currentMAX_SIZE.That is going to break userspace that sets the minimum before the maximum when the new minimum is larger than the old maximum.
David
Hi, David.
Thanks for pointing out the issue.
Here is an alternative approach:
instead of returning -EINVAL, automatically adjust the other value to preserve buffer_min_size <= buffer_max_size. The trade-off is that setting one parameter may implicitly adjust the other, but this is preferable to breaking existing applications. Does this approach look reasonable?
---
net/vmw_vsock/af_vsock.c +6 -0
@@ -2050,12 +2050,18 @@ static int vsock_connectible_setsockopt(struct socket *sock,case SO_VM_SOCKETS_BUFFER_MAX_SIZE :COPY_IN (val); +/* If the new max is less than current min, shrink min to match. */ +if (val < vsk->buffer_min_size) + vsk->buffer_min_size = val; vsk->buffer_max_size = val;vsock_update_buffer_size (vsk, transport, vsk->buffer_size);break ;case SO_VM_SOCKETS_BUFFER_MIN_SIZE :COPY_IN (val); +/* If the new min is greater than current max, expand max to match. */ +if (val > vsk->buffer_max_size) + vsk->buffer_max_size = val; vsk->buffer_min_size = val;vsock_update_buffer_size (vsk, transport, vsk->buffer_size);break ;
Quoted 32 lines
Quoted 32 lines
Fixes b9f2b0ffde0c ("vsock: handle buffer_size sockopts in the core")Signed-off-by Rongguang Wei <[email protected]>--- net/vmw_vsock/af_vsock.c | 8 ++++++++ 1 file changed, 8 insertions(+)net/vmw_vsock/af_vsock.c +8
-0@@ -2050,12 +2050,20 @@ static int vsock_connectible_setsockopt(struct socket *sock,case SO_VM_SOCKETS_BUFFER_MAX_SIZE :COPY_IN (val); +if (val < vsk->buffer_min_size) { + err = -EINVAL ; + goto exit; + } vsk->buffer_max_size = val;vsock_update_buffer_size (vsk, transport, vsk->buffer_size);break ;case SO_VM_SOCKETS_BUFFER_MIN_SIZE :COPY_IN (val); +if (val > vsk->buffer_max_size) { + err = -EINVAL ; + goto exit; + } vsk->buffer_min_size = val;vsock_update_buffer_size (vsk, transport, vsk->buffer_size);break ;
Quoted 27 lines · David Laight
Quoted 24 lines · On Mon, 24 Aug 2026 17:12:57 +0800 Rongguang Wei <[email protected]> wrote:
From: Rongguang Wei <[email protected]>
SO_VM_SOCKETS_BUFFER_MIN_SIZEandSO_VM_SOCKETS_BUFFER_MAX_SIZEdo not cross-validate against each other, allowing userspace to setbuffer_min_size>buffer_max_size.
When min > max,buffer_sizeis silently clamped to an incorrect value. For example, setting min=512KB then max=128 results in buffer_size=128 despite the user requesting much larger buffers viaSO_VM_SOCKETS_BUFFER_SIZE.Reproduced with a test program:
setsockopt(fd,AF_VSOCK,SO_VM_SOCKETS_BUFFER_MIN_SIZE,512 * 1024, sizeof(int)); setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE, 128, sizeof(int)); // User asked for 1MB but got 128 bytes silently setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE, 1024 * 1024, sizeof(int));After that use getsockopt to get the
buffer_size= 128 andbuffer_min_size= 524288,buffer_max_size= 128. Thebuffer_min_size>buffer_max_sizeand the kernel accepted contradictory values without error.
Quoted 9 lines
Quoted 3 lines
Add value check to fix this issue. Return
-EINVALto userspace when settingMAX_SIZEto a value smaller than the currentMIN_SIZEor settingMIN_SIZEto a value larger than the currentMAX_SIZE.That is going to break userspace that sets the minimum before the maximum when the new minimum is larger than the old maximum.
David
Hi, David.
Thanks for pointing out the issue.
Here is an alternative approach:
instead of returning -EINVAL, automatically adjust the other value to preserve buffer_min_size <= buffer_max_size. The trade-off is that setting one parameter may implicitly adjust the other, but this is preferable to breaking existing applications. Does this approach look reasonable?
---
net/vmw_vsock/af_vsock.c +6 -0
@@ -2050,12 +2050,18 @@ static int vsock_connectible_setsockopt(struct socket *sock,case SO_VM_SOCKETS_BUFFER_MAX_SIZE :COPY_IN (val); +/* If the new max is less than current min, shrink min to match. */ +if (val < vsk->buffer_min_size) + vsk->buffer_min_size = val; vsk->buffer_max_size = val;vsock_update_buffer_size (vsk, transport, vsk->buffer_size);break ;case SO_VM_SOCKETS_BUFFER_MIN_SIZE :COPY_IN (val); +/* If the new min is greater than current max, expand max to match. */ +if (val > vsk->buffer_max_size) + vsk->buffer_max_size = val; vsk->buffer_min_size = val;vsock_update_buffer_size (vsk, transport, vsk->buffer_size);break ;
Quoted 32 lines
Quoted 32 lines
Fixes b9f2b0ffde0c ("vsock: handle buffer_size sockopts in the core")Signed-off-by Rongguang Wei <[email protected]>--- net/vmw_vsock/af_vsock.c | 8 ++++++++ 1 file changed, 8 insertions(+)net/vmw_vsock/af_vsock.c +8
-0@@ -2050,12 +2050,20 @@ static int vsock_connectible_setsockopt(struct socket *sock,case SO_VM_SOCKETS_BUFFER_MAX_SIZE :COPY_IN (val); +if (val < vsk->buffer_min_size) { + err = -EINVAL ; + goto exit; + } vsk->buffer_max_size = val;vsock_update_buffer_size (vsk, transport, vsk->buffer_size);break ;case SO_VM_SOCKETS_BUFFER_MIN_SIZE :COPY_IN (val); +if (val > vsk->buffer_max_size) { + err = -EINVAL ; + goto exit; + } vsk->buffer_min_size = val;vsock_update_buffer_size (vsk, transport, vsk->buffer_size);break ;
Stefano Garzarella
maintainerQuoted 16 lines · weirongguang
Quoted 13 lines · David Laight
Quoted 10 lines · On Mon, 24 Aug 2026 17:12:57 +0800 Rongguang Wei <[email protected]> wrote:
From: Rongguang Wei <[email protected]>
SO_VM_SOCKETS_BUFFER_MIN_SIZEandSO_VM_SOCKETS_BUFFER_MAX_SIZEdo not cross-validate against each other, allowing userspace to setbuffer_min_size>buffer_max_size.
When min > max,buffer_sizeis silently clamped to an incorrect value. For example, setting min=512KB then max=128 results in buffer_size=128 despite the user requesting much larger buffers viaSO_VM_SOCKETS_BUFFER_SIZE.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Reproduced with a test program:
setsockopt(fd,AF_VSOCK,SO_VM_SOCKETS_BUFFER_MIN_SIZE,512 * 1024, sizeof(int)); setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE, 128, sizeof(int)); // User asked for 1MB but got 128 bytes silently setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE, 1024 * 1024, sizeof(int));
The user set MAX_SIZE to 128, from commit d114bfdc9b76 ("vsock: fix buffer size clamping order") this is the behavior we wanted, to ensure a rational limit on memory usage.
Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
After that use getsockopt to get the
buffer_size= 128 andbuffer_min_size= 524288,buffer_max_size= 128. Thebuffer_min_size>buffer_max_sizeand the kernel accepted contradictory values without error.Quoted 9 lines
Quoted 3 lines
Add value check to fix this issue. Return
-EINVALto userspace when settingMAX_SIZEto a value smaller than the currentMIN_SIZEor settingMIN_SIZEto a value larger than the currentMAX_SIZE.That is going to break userspace that sets the minimum before the maximum when the new minimum is larger than the old maximum.
David
Quoted 7 lines
Hi, David.
Thanks for pointing out the issue.Here is an alternative approach:
instead of returning-EINVAL, automatically adjust the other value to preservebuffer_min_size<=buffer_max_size. The trade-off is that setting one parameter may implicitly adjust the other, but this is preferable to breaking existing applications. Does this approach look reasonable?
With this fix, how would that change the results of the test program you included in the commit description?
Honestly, I don't understand what we're trying to fix.
Stefano