Skip to contentSkip to conversation

ksmbd: Use unsafe_memcpy() for ntlm_negotiate

PATCH Applied Namjae Jeon 3 participants 6 messages Aug 14, 2024 – Sep 23, 2024

Subsystems (2)
lore
Share & tools
lore

Namjae Jeon

author maintainer
#0 lore raw

PATCH 1/1 ksmbd: Use unsafe_memcpy() for ntlm_negotiate

rsp buffer is allocatged larger than spnego_blob from smb2_allocate_rsp_buf().

---
 fs/smb/server/smb2pdu.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/smb/server/smb2pdu.c +5 -2
index 2df1354288e6..3f4c56a10a86 100644
@@ -1370,7 +1370,8 @@ static int ntlm_negotiate(struct ksmbd_work *work,
 	}
 
 	sz = le16_to_cpu(rsp->SecurityBufferOffset);
-	memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
+	unsafe_memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len,
+			/* alloc is larger than blob, see smb2_allocate_rsp_buf() */);
 	rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
 
 out:
@@ -1453,7 +1454,9 @@ static int ntlm_authenticate(struct ksmbd_work *work,
 			return -ENOMEM;
 
 		sz = le16_to_cpu(rsp->SecurityBufferOffset);
-		memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
+		unsafe_memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob,
+				spnego_blob_len,
+				/* alloc is larger than blob, see smb2_allocate_rsp_buf() */);
 		rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
 		kfree(spnego_blob);
 	}
Signature · 1 line
2.25.1
Applied
#1 lore raw

This fixed the 'field spanning write' warnings for me.

merged into ksmbd-for-next

Quoted 37 lines · Namjae Jeon

rsp buffer is allocatged larger than spnego_blob from smb2_allocate_rsp_buf().

---
 fs/smb/server/smb2pdu.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/smb/server/smb2pdu.c +5 -3
index 2df1354288e6..3f4c56a10a86 100644
@@ -1370,7 +1370,8 @@ static int ntlm_negotiate(struct ksmbd_work *work,
        }
 
        sz = le16_to_cpu(rsp->SecurityBufferOffset);
-       memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
+       unsafe_memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len,
+                       /* alloc is larger than blob, see smb2_allocate_rsp_buf() */);
        rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
 
 out:
@@ -1453,7 +1454,9 @@ static int ntlm_authenticate(struct ksmbd_work *work,
                        return -ENOMEM;
 
                sz = le16_to_cpu(rsp->SecurityBufferOffset);
-               memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
+               unsafe_memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob,
+                               spnego_blob_len,
+                               /* alloc is larger than blob, see smb2_allocate_rsp_buf() */);
                rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
                kfree(spnego_blob);
        }
--

2.25.1

Signature · 3 lines
Thanks,

Steve
#2 lore raw
In reply to Namjae Jeon
Quoted 23 lines · Namjae Jeon

rsp buffer is allocatged larger than spnego_blob from smb2_allocate_rsp_buf().

---
 fs/smb/server/smb2pdu.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/smb/server/smb2pdu.c +2 -1
index 2df1354288e6..3f4c56a10a86 100644
@@ -1370,7 +1370,8 @@ static int ntlm_negotiate(struct ksmbd_work *work,
 	}
 
 	sz = le16_to_cpu(rsp->SecurityBufferOffset);
-	memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
+	unsafe_memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len,
+			/* alloc is larger than blob, see smb2_allocate_rsp_buf() */);
 	rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
 
 out:

fs/smb/server/smb2pdu.c static int ntlm_authenticate(struct ksmbd_work *work, · line 1462

Quoted 10 lines
fs/smb/server/smb2pdu.c +3 -1
@@ -1453,7 +1454,9 @@ static int ntlm_authenticate(struct ksmbd_work *work,
 			return -ENOMEM;
 
 		sz = le16_to_cpu(rsp->SecurityBufferOffset);
-		memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
+		unsafe_memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob,
+				spnego_blob_len,
+				/* alloc is larger than blob, see smb2_allocate_rsp_buf() */);
 		rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
 		kfree(spnego_blob);
 	}

Ew, please fix these properly instead of continuing to lie to the compiler about the struct member sizes. :P

The above, &rsp->hdr.ProtocolId, addresses a single __le32 member, and then tries to go past the end of it (i.e. "sz" is larger than 4). The struct layout therefore indicates to memcpy that you have no bytes left to write ("size 0" in the warning).

It looks to me like you want to be calculating an offset into rsp->Buffer instead? Try:

	sz = le16_to_cpu(rsp->SecurityBufferOffset) -
		offsetof(*typeof(rsp), Buffer);
	memcpy(&rsp->Buffer[sz], ...)

BTW, what is validating that this:

        sz = le16_to_cpu(rsp->SecurityBufferOffset);
        chgblob =
                (struct challenge_message *)((char *)&rsp->hdr.ProtocolId + sz);

is within the original data buffer? Shouldn't something check that:
sz > offsetof(*typeof(rsp), Buffer)
and
sz <= ...size of the buffer... (maybe that happened already earlier)

Signature · 1 line
Kees Cook
author maintainer
#3 lore raw
Quoted 27 lines · Kees Cook
Quoted 25 lines · Namjae Jeon

rsp buffer is allocatged larger than spnego_blob from smb2_allocate_rsp_buf().

---
 fs/smb/server/smb2pdu.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/smb/server/smb2pdu.c +2 -1
index 2df1354288e6..3f4c56a10a86 100644
@@ -1370,7 +1370,8 @@ static int ntlm_negotiate(struct ksmbd_work *work,
      }
 
      sz = le16_to_cpu(rsp->SecurityBufferOffset);
-     memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
+     unsafe_memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len,
+                     /* alloc is larger than blob, see smb2_allocate_rsp_buf() */);
      rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
 
 out:
@@ -1453,7 +1454,9 @@ static int ntlm_authenticate(struct ksmbd_work *work,
                      return -ENOMEM;
 
Quoted 11 lines

fs/smb/server/smb2pdu.c static int ntlm_authenticate(struct ksmbd_work *work, · line 1462

Quoted 8 lines
fs/smb/server/smb2pdu.c +3 -1
              sz = le16_to_cpu(rsp->SecurityBufferOffset);
-             memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
+             unsafe_memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob,
+                             spnego_blob_len,
+                             /* alloc is larger than blob, see smb2_allocate_rsp_buf() */);
              rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
              kfree(spnego_blob);
      }

Ew, please fix these properly instead of continuing to lie to the compiler about the struct member sizes. :P

I have fully checked that this warning is a false alarm.

Quoted 6 lines

The above, &rsp->hdr.ProtocolId, addresses a single __le32 member, and then tries to go past the end of it (i.e. "sz" is larger than 4). The struct layout therefore indicates to memcpy that you have no bytes left to write ("size 0" in the warning).

Quoted 6 lines

It looks to me like you want to be calculating an offset into rsp->Buffer instead? Try:

        sz = le16_to_cpu(rsp->SecurityBufferOffset) -
                offsetof(*typeof(rsp), Buffer);
        memcpy(&rsp->Buffer[sz], ...)

SecurityBufferOffset is fixed to the value 72 and it points to ->Buffer. So memcpy(&rsp->Buffer[0], ...)

Quoted 11 lines

BTW, what is validating that this:

        sz = le16_to_cpu(rsp->SecurityBufferOffset);
        chgblob =
                (struct challenge_message *)((char *)&rsp->hdr.ProtocolId + sz);

is within the original data buffer? Shouldn't something check that:
sz > offsetof(*typeof(rsp), Buffer)
and
sz <= ...size of the buffer... (maybe that happened already earlier)

SecurityBufferOffset is fixed to the value 72. It is set in smb2_sess_setup().

int smb2_sess_setup(struct ksmbd_work *work)
...

        rsp->StructureSize = cpu_to_le16(9);
        rsp->SessionFlags = 0;
        rsp->SecurityBufferOffset = cpu_to_le16(72);
        rsp->SecurityBufferLength = 0;

So sz and offsetof(*typeof(rsp), Buffer) will be same. and rsp buffer size is at least 448 bytes, That's enough space to contain a chgblob(48) or spnego_blob(249).

Thanks.

Quoted 3 lines

--
Kees Cook

#4 lore raw
Quoted 69 lines · Namjae Jeon
Quoted 27 lines · Kees Cook
Quoted 25 lines · Namjae Jeon

rsp buffer is allocatged larger than spnego_blob from smb2_allocate_rsp_buf().

---
 fs/smb/server/smb2pdu.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/smb/server/smb2pdu.c +2 -1
index 2df1354288e6..3f4c56a10a86 100644
@@ -1370,7 +1370,8 @@ static int ntlm_negotiate(struct ksmbd_work *work,
      }
 
      sz = le16_to_cpu(rsp->SecurityBufferOffset);
-     memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
+     unsafe_memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len,
+                     /* alloc is larger than blob, see smb2_allocate_rsp_buf() */);
      rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
 
 out:
@@ -1453,7 +1454,9 @@ static int ntlm_authenticate(struct ksmbd_work *work,
                      return -ENOMEM;
 
Quoted 11 lines

fs/smb/server/smb2pdu.c static int ntlm_authenticate(struct ksmbd_work *work, · line 1462

Quoted 8 lines
fs/smb/server/smb2pdu.c +3 -1
              sz = le16_to_cpu(rsp->SecurityBufferOffset);
-             memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
+             unsafe_memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob,
+                             spnego_blob_len,
+                             /* alloc is larger than blob, see smb2_allocate_rsp_buf() */);
              rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
              kfree(spnego_blob);
      }

Ew, please fix these properly instead of continuing to lie to the compiler about the struct member sizes. :P

I have fully checked that this warning is a false alarm.

Quoted 6 lines

The above, &rsp->hdr.ProtocolId, addresses a single __le32 member, and then tries to go past the end of it (i.e. "sz" is larger than 4). The struct layout therefore indicates to memcpy that you have no bytes left to write ("size 0" in the warning).

Quoted 6 lines

It looks to me like you want to be calculating an offset into rsp->Buffer instead? Try:

        sz = le16_to_cpu(rsp->SecurityBufferOffset) -
                offsetof(*typeof(rsp), Buffer);
        memcpy(&rsp->Buffer[sz], ...)

SecurityBufferOffset is fixed to the value 72 and it points to ->Buffer. So memcpy(&rsp->Buffer[0], ...)

Quoted 11 lines

BTW, what is validating that this:

        sz = le16_to_cpu(rsp->SecurityBufferOffset);
        chgblob =
                (struct challenge_message *)((char *)&rsp->hdr.ProtocolId + sz);

is within the original data buffer? Shouldn't something check that:
sz > offsetof(*typeof(rsp), Buffer)
and
sz <= ...size of the buffer... (maybe that happened already earlier)

SecurityBufferOffset is fixed to the value 72. It is set in smb2_sess_setup().

int smb2_sess_setup(struct ksmbd_work *work)
...

Quoted 8 lines
        rsp->StructureSize = cpu_to_le16(9);
        rsp->SessionFlags = 0;
        rsp->SecurityBufferOffset = cpu_to_le16(72);
        rsp->SecurityBufferLength = 0;

So sz and offsetof(*typeof(rsp), Buffer) will be same. and rsp buffer size is at least 448 bytes, That's enough space to contain a chgblob(48) or spnego_blob(249).

Okay, in that case, please just use:

	memcpy(rsp->Buffer, ...)

and the "unsafe" usage can be removed. :)

Signature · 1 line
Kees Cook
author maintainer
#5 lore raw
Quoted 80 lines · Kees Cook
Quoted 69 lines · Namjae Jeon
Quoted 27 lines · Kees Cook
Quoted 25 lines · Namjae Jeon

rsp buffer is allocatged larger than spnego_blob from smb2_allocate_rsp_buf().

---
 fs/smb/server/smb2pdu.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/smb/server/smb2pdu.c +2 -1
index 2df1354288e6..3f4c56a10a86 100644
@@ -1370,7 +1370,8 @@ static int ntlm_negotiate(struct ksmbd_work *work,
      }
 
      sz = le16_to_cpu(rsp->SecurityBufferOffset);
-     memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
+     unsafe_memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len,
+                     /* alloc is larger than blob, see smb2_allocate_rsp_buf() */);
      rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
 
 out:
@@ -1453,7 +1454,9 @@ static int ntlm_authenticate(struct ksmbd_work *work,
                      return -ENOMEM;
 
Quoted 11 lines

fs/smb/server/smb2pdu.c static int ntlm_authenticate(struct ksmbd_work *work, · line 1462

Quoted 8 lines
fs/smb/server/smb2pdu.c +3 -1
              sz = le16_to_cpu(rsp->SecurityBufferOffset);
-             memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
+             unsafe_memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob,
+                             spnego_blob_len,
+                             /* alloc is larger than blob, see smb2_allocate_rsp_buf() */);
              rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
              kfree(spnego_blob);
      }

Ew, please fix these properly instead of continuing to lie to the compiler about the struct member sizes. :P

I have fully checked that this warning is a false alarm.

Quoted 6 lines

The above, &rsp->hdr.ProtocolId, addresses a single __le32 member, and then tries to go past the end of it (i.e. "sz" is larger than 4). The struct layout therefore indicates to memcpy that you have no bytes left to write ("size 0" in the warning).

Quoted 6 lines

It looks to me like you want to be calculating an offset into rsp->Buffer instead? Try:

        sz = le16_to_cpu(rsp->SecurityBufferOffset) -
                offsetof(*typeof(rsp), Buffer);
        memcpy(&rsp->Buffer[sz], ...)

SecurityBufferOffset is fixed to the value 72 and it points to ->Buffer. So memcpy(&rsp->Buffer[0], ...)

Quoted 11 lines

BTW, what is validating that this:

        sz = le16_to_cpu(rsp->SecurityBufferOffset);
        chgblob =
                (struct challenge_message *)((char *)&rsp->hdr.ProtocolId + sz);

is within the original data buffer? Shouldn't something check that:
sz > offsetof(*typeof(rsp), Buffer)
and
sz <= ...size of the buffer... (maybe that happened already earlier)

SecurityBufferOffset is fixed to the value 72. It is set in smb2_sess_setup().

int smb2_sess_setup(struct ksmbd_work *work)
...

Quoted 8 lines
        rsp->StructureSize = cpu_to_le16(9);
        rsp->SessionFlags = 0;
        rsp->SecurityBufferOffset = cpu_to_le16(72);
        rsp->SecurityBufferLength = 0;

So sz and offsetof(*typeof(rsp), Buffer) will be same. and rsp buffer size is at least 448 bytes, That's enough space to contain a chgblob(48) or spnego_blob(249).

Quoted 5 lines

Okay, in that case, please just use:

        memcpy(rsp->Buffer, ...)

and the "unsafe" usage can be removed. :)

Okay, I will update it:)

Thanks!

Quoted 3 lines

--
Kees Cook