Skip to content

SMB1 fixes#399

Open
mmakassikis wants to merge 5 commits intonamjaejeon:masterfrom
mmakassikis:marios/smb1-fixes
Open

SMB1 fixes#399
mmakassikis wants to merge 5 commits intonamjaejeon:masterfrom
mmakassikis:marios/smb1-fixes

Conversation

@mmakassikis
Copy link
Copy Markdown

A few fixes SMB1 fixes:

  • ksmbd: smb1: remove unused parameter in find_next()
  • ksmbd: smb1: use kasprintf() instead of kzalloc+snprintf+strncat
  • ksmbd: smb1: Fix potential memleak in smb_nt_create_andx
  • ksmbd: smb1: add buffer validation

The first 3 ones small fixes. The last one is big, but it is essentially the same pattern repeated over and over again.

I tried sending to the linux-cifsd-devel list, but it looks like the project has been deleted from sourceforge.

auth.c Outdated
* @pw_buf: NTLM challenge response
* @passkey: user password
* @pw_len: buffer length
* @cryptkey: buffer length
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- * @cryptkey:	buffer length
+ * @cryptkey: server challenge

@namjaejeon
Copy link
Copy Markdown
Owner

I have applied 3 patches first. I need more time to review "ksmbd: smb1: add buffer validation" patch.

@namjaejeon namjaejeon force-pushed the master branch 2 times, most recently from 80f76a4 to cdd2c68 Compare May 16, 2022 07:35
@namjaejeon namjaejeon force-pushed the master branch 2 times, most recently from 598c4b4 to 9ef42e9 Compare June 19, 2022 00:10
@namjaejeon
Copy link
Copy Markdown
Owner

@mmakassikis Sorry for checking "ksmbd: smb1: add buffer validation" patch. I will check it on weekend.
@hclee Hyunchul, please join to review it together.

@hclee
Copy link
Copy Markdown
Contributor

hclee commented Jun 24, 2022

@mmakassikis Sorry for checking "ksmbd: smb1: add buffer validation" patch. I will check it on weekend. @hclee Hyunchul, please join to review it together.

Okay, I will review this patch on weekend.

smb1pdu.c Outdated
pr_err("Unable to strdup() treename or devtype uid %d\n",
rsp_hdr->Uid);
offset += offsetof(struct smb_com_tconx_req, Password);
offset += le16_to_cpu(req->PasswordLength);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tested if req->PasswordLength is zero ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If req->PasswordLength is zero, offset is unchanged (incidentally, the current code works because req->PasswordLength == le16_to_cpu(req->PasswordLength)).

I don't see what problems can arise with req->PasswordLength.

smb1pdu.c Outdated
sz = le16_to_cpu(req->SecurityBlobLength);

if (offsetof(struct smb_com_session_setup_req, SecurityBlob) + sz >
get_rfc1002_len(req)) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check can be moved to smb1misc.c ?

@namjaejeon
Copy link
Copy Markdown
Owner

@mmakassikis When I checked smb1 buffer validation patch, You need to move some of validation codes to smb1misc.c. I think that you can check codes in smb2misc.c

struct andx_block *next;

/* AndXOffset does not include 4 byte RFC1002 header */
len -= 4;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the "len" include 4-byte RFC1002 header?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buf is the full request (RFC1002 header, followed by SMB2 header and payload). len should those 4 bytes.

The code here is correct, but the caller code is wrong as it uses get_rfc1002_len() which is the buffer length minus 4.
This mistake is present in most if not all changes in this patch.

smb1pdu.c Outdated
}

offset += oldname_len + 2;
if (offset > maxlen) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of offset > maxlen, offset >= maxlen?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Actually, I thought it was ok as we passed a calculated length to smb_get_name(), but if the request is malformed, it's possible to have offset > maxlen, in which case the negative "maxlen - offset" will cause an infinite loop in smb_utf16_bytes().

I will update the patch to check offset >= maxlen before calling smb_get_name().

smb1pdu.c Outdated
setup_bytes_count = 2 * req->SetupCount;

maxlen = get_rfc1002_len(req);
offset = offsetof(struct smb_com_trans_req, Data);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't 1-byte, smb_com_trans_req.Data[1] need to be considered?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand.

Currently, the code parses up to 256 bytes starting from req->Data + setup_bytes_count. My change initializes the offset to req->Data. Whether the Data field is Data[0] or Data[1] doesn't matter, no ?

@namjaejeon namjaejeon force-pushed the master branch 7 times, most recently from d055654 to 7202068 Compare July 25, 2022 04:06
@namjaejeon namjaejeon force-pushed the master branch 9 times, most recently from c683c7d to 04bf112 Compare July 29, 2022 00:31
Marios Makassikis added 2 commits November 23, 2022 18:45
buf_len is never used anywhere.

Signed-off-by: Marios Makassikis <mmakassikis@freebox.fr>
- Unset spnego bit in SMB negotiate response.
- Include NativeOS / Native LAN Manager / Primary Domain fields in
session setup response. Without these, smbclient fails with
NT_STATUS_BUFFER_TOO_SMALL.

Tested with smbclient:
  smbclient //127.0.0.1/testshare -U user%password \
	--option "client min protocol = NT1" \
	--option "client use spnego = no" \
	-m NT1

Signed-off-by: Marios Makassikis <mmakassikis@freebox.fr>
Marios Makassikis added 2 commits November 29, 2022 13:16
flock is leaked in an error happens before smb_lock_init().

Signed-off-by: Marios Makassikis <mmakassikis@freebox.fr>
Many functions assume the client sends well formed packets, or are
optimistic about the buffer length (using PATH_MAX or a fixed '256'
value) instead of the actual length.

The pattern followed to fix this boils down to extracting the total
buffer length and calculate how much data has been consumed.

Signed-off-by: Marios Makassikis <mmakassikis@freebox.fr>
@mmakassikis
Copy link
Copy Markdown
Author

Updated "ksmbd: smb1: Fix resource leak in smb_locking_andx()" as there were other paths were flock was leaked

@namjaejeon
Copy link
Copy Markdown
Owner

@mmakassikis I have applied 4 patches first. Could you please tell me how you test "ksmbd: smb1: add buffer validation" patch ? and this patch was already applied to your product ?

@mmakassikis
Copy link
Copy Markdown
Author

The buffer validation patch has not yet been applied for our product. The changes themselves are quite repetitive but it's easy to make mistakes / forget a check (as shown by the previous version of the patch that you and Hyunchul reviewed). An extra pair of eyes is always welcome.

I tested the patchset manually for the most part (which is how I saw non-spnego session setup was broken). I tested the following operations with smbclient / cifs.ko:

  • create dir/file
  • list directory contents
  • read/write/rename file
  • delete dir

@namjaejeon
Copy link
Copy Markdown
Owner

@mmakassikis Okay, Let me check it:) Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants