ksmbd: Annotate struct copychunk_ioctl_req with __counted_by_le()
Subsystems (2)
PATCH 1/1 ksmbd: Annotate struct copychunk_ioctl_req with __counted_by_le()
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.
--- fs/smb/server/smb2pdu.c | 2 +- fs/smb/server/smb2pdu.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
fs/smb/server/smb2pdu.c +1 -1
@@ -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 ())
fs/smb/server/smb2pdu.h +1 -1
@@ -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 {
Signature · 1 line
2.46.1
Tom Talpey
reviewerQuoted 39 lines · Thorsten Blum
Add the
__counted_by_lecompiler attribute to the flexible array member Chunks to improve access bounds-checking viaCONFIG_UBSAN_BOUNDSandCONFIG_FORTIFY_SOURCE.Read Chunks[0] after checking that ChunkCount is not 0.
Compile-tested only.
Signed-off-by Thorsten Blum <[email protected]>--- fs/smb/server/smb2pdu.c | 2 +- fs/smb/server/smb2pdu.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)fs/smb/server/smb2pdu.c +1
-1@@ -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 ())fs/smb/server/smb2pdu.h +0
-0
fs/smb/server/smb2pdu.h struct copychunk_ioctl_req { · line 199
Quoted 7 lines
fs/smb/server/smb2pdu.h +1
-1@@ -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;
This isn't correct. The u8 is just a raw buffer, copychunk structs are marshaled into it, and they're 24 bytes each.
The better fix would be to type the buffer contents properly, but that will have protocol implications and should be thoroughly tested.
NAK on this change for now.
Tom.
Quoted 1 line
fs/smb/server/smb2pdu.h +0
-0struct srv_copychunk {
Hi Tom,
Quoted 37 lines · Tom Talpey
Quoted 35 lines · Thorsten Blum
Add the
__counted_by_lecompiler attribute to the flexible array member Chunks to improve access bounds-checking viaCONFIG_UBSAN_BOUNDSandCONFIG_FORTIFY_SOURCE.
Read Chunks[0] after checking that ChunkCount is not 0. Compile-tested only.Signed-off-by Thorsten Blum <[email protected]>--- fs/smb/server/smb2pdu.c | 2 +- fs/smb/server/smb2pdu.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)fs/smb/server/smb2pdu.c +0
-0@@ -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 ())fs/smb/server/smb2pdu.h +0
-0@@ -194,7 +194,7 @@ struct copychunk_ioctl_req {__le64 ResumeKey[3 ];__le32 ChunkCount;
Quoted 8 lines
fs/smb/server/smb2pdu.h struct copychunk_ioctl_req { · line 199
Quoted 5 lines
fs/smb/server/smb2pdu.h +1
-1__le32 Reserved; -__u8 Chunks[];/* array of srv_copychunk */ + __u8 Chunks[] __counted_by_le(ChunkCount); /* array of srv_copychunk */ } __packed;This isn't correct. The u8 is just a raw buffer, copychunk structs are marshaled into it, and they're 24 bytes each.
Hm, I see.
How does this for-loop work then? It iterates over ci_req->ChunkCount and expects a srv_copychunk at each ci_req->Chunks[i]?
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())
break;
total_size_written += le32_to_cpu(chunks[i].Length);
}Thanks,
Thorsten
Quoted 51 lines · Thorsten Blum
Quoted 37 lines · Tom Talpey
Quoted 35 lines · Thorsten Blum
Add the
__counted_by_lecompiler attribute to the flexible array member Chunks to improve access bounds-checking viaCONFIG_UBSAN_BOUNDSandCONFIG_FORTIFY_SOURCE.
Read Chunks[0] after checking that ChunkCount is not 0. Compile-tested only.Signed-off-by Thorsten Blum <[email protected]>---
fs/smb/server/smb2pdu.c| 2 +-fs/smb/server/smb2pdu.h| 2 +-2 files changed, 2 insertions(+), 2 deletions(-)fs/smb/server/smb2pdu.c +1
-1@@ -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 ())fs/smb/server/smb2pdu.h +0
-0@@ -194,7 +194,7 @@ struct copychunk_ioctl_req {__le64 ResumeKey[3 ];__le32 ChunkCount;Quoted 8 lines
fs/smb/server/smb2pdu.h struct copychunk_ioctl_req { · line 199
Quoted 5 lines
fs/smb/server/smb2pdu.h +1
-1__le32 Reserved; -__u8 Chunks[];/* array of srv_copychunk */ + __u8 Chunks[] __counted_by_le(ChunkCount); /* array of srv_copychunk */ } __packed;This isn't correct. The u8 is just a raw buffer, copychunk structs are marshaled into it, and they're 24 bytes each.
Hm, I see.
How does this for-loop work then? It iterates over
ci_req->ChunkCountand expects asrv_copychunkat each ci_req->Chunks[i]?
Quoted 6 lines
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())break;
total_size_written+=le32_to_cpu(chunks[i].Length); }
Never mind, I just realized that the pointer arithmetic takes the array offset into account.
A srv_copychunk takes up 24 bytes and therefore 24 __u8[] slots. The __counted_by annotation is essentially off by a factor of 24.
Thanks,
Thorsten
Tom Talpey
reviewerQuoted 59 lines · Thorsten Blum
Quoted 51 lines · Thorsten Blum
Quoted 37 lines · Tom Talpey
Quoted 35 lines · Thorsten Blum
Add the
__counted_by_lecompiler attribute to the flexible array member Chunks to improve access bounds-checking viaCONFIG_UBSAN_BOUNDSandCONFIG_FORTIFY_SOURCE.
Read Chunks[0] after checking that ChunkCount is not 0. Compile-tested only.Signed-off-by Thorsten Blum <[email protected]>---
fs/smb/server/smb2pdu.c| 2 +-fs/smb/server/smb2pdu.h| 2 +-2 files changed, 2 insertions(+), 2 deletions(-)fs/smb/server/smb2pdu.c +1
-1@@ -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 ())fs/smb/server/smb2pdu.h +0
-0@@ -194,7 +194,7 @@ struct copychunk_ioctl_req {__le64 ResumeKey[3 ];__le32 ChunkCount;Quoted 8 lines
fs/smb/server/smb2pdu.h struct copychunk_ioctl_req { · line 199
Quoted 5 lines
fs/smb/server/smb2pdu.h +1
-1__le32 Reserved; -__u8 Chunks[];/* array of srv_copychunk */ + __u8 Chunks[] __counted_by_le(ChunkCount); /* array of srv_copychunk */ } __packed;This isn't correct. The u8 is just a raw buffer, copychunk structs are marshaled into it, and they're 24 bytes each.
Hm, I see.
How does this for-loop work then? It iterates over
ci_req->ChunkCountand expects asrv_copychunkat each ci_req->Chunks[i]?Quoted 6 lines
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())break;
total_size_written+=le32_to_cpu(chunks[i].Length); }
Quoted 5 lines
Never mind, I just realized that the pointer arithmetic takes the array offset into account.
A
srv_copychunktakes up 24 bytes and therefore 24 __u8[] slots. The__counted_byannotation is essentially off by a factor of 24.
Right, and the fix is probably obvious, but the copychunk code has yielded a few surprises in the past, so I'd really like to see any change tested... Maybe Steve has a ready-to-go section in his set.
Tom.