Add the __counted_by_le compiler attribute to the flexible array member
Chunks to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and
CONFIG_FORTIFY_SOURCE.

Read Chunks[0] after checking that ChunkCount is not 0.

Compile-tested only.

Signed-off-by: Thorsten Blum <thorsten.b...@linux.dev>
---
 fs/smb/server/smb2pdu.c | 2 +-
 fs/smb/server/smb2pdu.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index 461c4fc682ac..0670bdf3e167 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -7565,7 +7565,6 @@ static int fsctl_copychunk(struct ksmbd_work *work,
        ci_rsp->TotalBytesWritten =
                cpu_to_le32(ksmbd_server_side_copy_max_total_size());
 
-       chunks = (struct srv_copychunk *)&ci_req->Chunks[0];
        chunk_count = le32_to_cpu(ci_req->ChunkCount);
        if (chunk_count == 0)
                goto out;
@@ -7579,6 +7578,7 @@ static int fsctl_copychunk(struct ksmbd_work *work,
                return -EINVAL;
        }
 
+       chunks = (struct srv_copychunk *)&ci_req->Chunks[0];
        for (i = 0; i < chunk_count; i++) {
                if (le32_to_cpu(chunks[i].Length) == 0 ||
                    le32_to_cpu(chunks[i].Length) > 
ksmbd_server_side_copy_max_chunk_size())
diff --git a/fs/smb/server/smb2pdu.h b/fs/smb/server/smb2pdu.h
index 73aff20e22d0..f01121dbf358 100644
--- a/fs/smb/server/smb2pdu.h
+++ b/fs/smb/server/smb2pdu.h
@@ -194,7 +194,7 @@ struct copychunk_ioctl_req {
        __le64 ResumeKey[3];
        __le32 ChunkCount;
        __le32 Reserved;
-       __u8 Chunks[]; /* array of srv_copychunk */
+       __u8 Chunks[] __counted_by_le(ChunkCount); /* array of srv_copychunk */
 } __packed;
 
 struct srv_copychunk {
-- 
2.46.1


Reply via email to