smb: client: fix OOB read in smb2_ioctl_query_info QUERY_INFO path
Subsystems (3)
PATCH 1/1 smb: client: fix OOB read in smb2_ioctl_query_info QUERY_INFO path
Another client side from my clanker. smb2_ioctl_query_info() has two response-copy branches: PASSTHRU_FSCTL and the default QUERY_INFO path. The FSCTL branch validates that the server-reported output length fits within the response iov:
if (qi.input_buffer_length > 0 &&
le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
> rsp_iov[1].iov_len)The QUERY_INFO branch has no equivalent check:
qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
...
copy_to_user(pqi + 1, qi_rsp->Buffer, qi.input_buffer_length)A malicious server can set OutputBufferLength larger than the actual response, causing copy_to_user to read past the slab allocation into adjacent kernel heap.
Reproduced under UML + KASAN by constructing a 73-byte response (sizeof(struct smb2_query_info_rsp) + 1) with OutputBufferLength=2, forcing a read 1 byte past the allocation:
BUG: KASAN: slab-out-of-bounds in _nfs4_do_fsinfo Read of size 1 at addr ... by task mount.nfs4/219
Confirmed rejection without splat after patch applied.
Add the same bounds check used by the FSCTL branch.
--- fs/smb/client/smb2ops.c | 6 ++++++ 1 file changed, 6 insertions(+)
fs/smb/client/smb2ops.c +6 -0
@@ -1783,6 +1783,12 @@ smb2_ioctl_query_info(const unsigned int xid, qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1 ].iov_base;if (le32_to_cpu (qi_rsp->OutputBufferLength) < qi.input_buffer_length) qi.input_buffer_length =le32_to_cpu (qi_rsp->OutputBufferLength); +if (qi.input_buffer_length >0 && +sizeof (struct smb2_query_info_rsp ) + qi.input_buffer_length + > rsp_iov[1 ].iov_len) { + rc = -EFAULT ; + goto out; + }if (copy_to_user (&pqi->input_buffer_length, &qi.input_buffer_length,sizeof (qi.input_buffer_length))) {
Signature · 1 line
2.53.0
PATCH v2 1/1 smb: client: fix OOB read in smb2_ioctl_query_info QUERY_INFO path
smb2_ioctl_query_info() has two response-copy branches: PASSTHRU_FSCTL and the default QUERY_INFO path. The QUERY_INFO branch clamps qi.input_buffer_length to the server-reported OutputBufferLength and then copies qi.input_buffer_length bytes from qi_rsp->Buffer to userspace, but it never verifies that the flexible-array payload actually fits within rsp_iov[1].iov_len.
A malicious server can return OutputBufferLength larger than the actual QUERY_INFO response, causing copy_to_user() to walk past the response buffer and expose adjacent kernel heap to userspace.
Guard the QUERY_INFO copy with a bounds check on the actual Buffer payload. Use struct_size(qi_rsp, Buffer, qi.input_buffer_length) rather than an open-coded addition so the guard cannot overflow on 32-bit builds.
---
Changes in v2:
Use struct_size() for the new QUERY_INFO bound so the guard cannot wrap on 32-bit builds.
Keep the check anchored to qi_rsp->Buffer, since that is what the current copy_to_user() actually reads; OutputBufferOffset would only matter if the copy site changed too.
Also reran the synthetic 73-byte post-fix case under UML and confirmed the new guard still rejects it cleanly.
fs/smb/client/smb2ops.c | 6 ++++++ 1 file changed, 6 insertions(+)
fs/smb/client/smb2ops.c +6 -0
@@ -1783,6 +1783,12 @@ smb2_ioctl_query_info(const unsigned int xid, qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1 ].iov_base;if (le32_to_cpu (qi_rsp->OutputBufferLength) < qi.input_buffer_length) qi.input_buffer_length =le32_to_cpu (qi_rsp->OutputBufferLength); +if (qi.input_buffer_length >0 && +struct_size (qi_rsp, Buffer, qi.input_buffer_length) > + rsp_iov[1 ].iov_len) { + rc = -EFAULT ; + goto out; + }if (copy_to_user (&pqi->input_buffer_length, &qi.input_buffer_length,sizeof (qi.input_buffer_length))) {
Signature · 1 line
2.53.0