Commit c75de845 authored by Linus Torvalds's avatar Linus Torvalds
Browse files

Merge tag '5.15-rc4-ksmbd-fixes' of git://git.samba.org/ksmbd

Pull ksmbd fixes from Steve French:
 "Six fixes for the ksmbd kernel server, including two additional
  overflow checks, a fix for oops, and some cleanup (e.g. remove dead
  code for less secure dialects that has been removed)"

* tag '5.15-rc4-ksmbd-fixes' of git://git.samba.org/ksmbd:
  ksmbd: fix oops from fuse driver
  ksmbd: fix version mismatch with out of tree
  ksmbd: use buf_data_size instead of recalculation in smb3_decrypt_req()
  ksmbd: remove the leftover of smb2.0 dialect support
  ksmbd: check strictly data area in ksmbd_smb2_check_message()
  ksmbd: add the check to vaildate if stream protocol length exceeds maximum value
parents 717478d8 64e78755
Loading
Loading
Loading
Loading
+6 −4
Original line number Diff line number Diff line
@@ -296,10 +296,12 @@ int ksmbd_conn_handler_loop(void *p)
		pdu_size = get_rfc1002_len(hdr_buf);
		ksmbd_debug(CONN, "RFC1002 header %u bytes\n", pdu_size);

		/* make sure we have enough to get to SMB header end */
		if (!ksmbd_pdu_size_has_room(pdu_size)) {
			ksmbd_debug(CONN, "SMB request too short (%u bytes)\n",
				    pdu_size);
		/*
		 * Check if pdu size is valid (min : smb header size,
		 * max : 0x00FFFFFF).
		 */
		if (pdu_size < __SMB2_HEADER_STRUCTURE_SIZE ||
		    pdu_size > MAX_STREAM_PROT_LEN) {
			continue;
		}

+1 −1
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@
#include "unicode.h"
#include "vfs_cache.h"

#define KSMBD_VERSION	"3.1.9"
#define KSMBD_VERSION	"3.4.2"

extern int ksmbd_debug_types;

+47 −51
Original line number Diff line number Diff line
@@ -80,18 +80,21 @@ static const bool has_smb2_data_area[NUMBER_OF_SMB2_COMMANDS] = {
};

/*
 * Returns the pointer to the beginning of the data area. Length of the data
 * area and the offset to it (from the beginning of the smb are also returned.
 * Set length of the data area and the offset to arguments.
 * if they are invalid, return error.
 */
static char *smb2_get_data_area_len(int *off, int *len, struct smb2_hdr *hdr)
static int smb2_get_data_area_len(unsigned int *off, unsigned int *len,
				  struct smb2_hdr *hdr)
{
	int ret = 0;

	*off = 0;
	*len = 0;

	/* error reqeusts do not have data area */
	if (hdr->Status && hdr->Status != STATUS_MORE_PROCESSING_REQUIRED &&
	    (((struct smb2_err_rsp *)hdr)->StructureSize) == SMB2_ERROR_STRUCTURE_SIZE2_LE)
		return NULL;
		return ret;

	/*
	 * Following commands have data areas so we have to get the location
@@ -165,69 +168,60 @@ static char *smb2_get_data_area_len(int *off, int *len, struct smb2_hdr *hdr)
	case SMB2_IOCTL:
		*off = le32_to_cpu(((struct smb2_ioctl_req *)hdr)->InputOffset);
		*len = le32_to_cpu(((struct smb2_ioctl_req *)hdr)->InputCount);

		break;
	default:
		ksmbd_debug(SMB, "no length check for command\n");
		break;
	}

	/*
	 * Invalid length or offset probably means data area is invalid, but
	 * we have little choice but to ignore the data area in this case.
	 */
	if (*off > 4096) {
		ksmbd_debug(SMB, "offset %d too large, data area ignored\n",
			    *off);
		*len = 0;
		*off = 0;
	} else if (*off < 0) {
		ksmbd_debug(SMB,
			    "negative offset %d to data invalid ignore data area\n",
			    *off);
		*off = 0;
		*len = 0;
	} else if (*len < 0) {
		ksmbd_debug(SMB,
			    "negative data length %d invalid, data area ignored\n",
			    *len);
		*len = 0;
	} else if (*len > 128 * 1024) {
		ksmbd_debug(SMB, "data area larger than 128K: %d\n", *len);
		*len = 0;
		ksmbd_debug(SMB, "offset %d too large\n", *off);
		ret = -EINVAL;
	} else if ((u64)*off + *len > MAX_STREAM_PROT_LEN) {
		ksmbd_debug(SMB, "Request is larger than maximum stream protocol length(%u): %llu\n",
			    MAX_STREAM_PROT_LEN, (u64)*off + *len);
		ret = -EINVAL;
	}

	/* return pointer to beginning of data area, ie offset from SMB start */
	if ((*off != 0) && (*len != 0))
		return (char *)hdr + *off;
	else
		return NULL;
	return ret;
}

/*
 * Calculate the size of the SMB message based on the fixed header
 * portion, the number of word parameters and the data portion of the message.
 */
static unsigned int smb2_calc_size(void *buf)
static int smb2_calc_size(void *buf, unsigned int *len)
{
	struct smb2_pdu *pdu = (struct smb2_pdu *)buf;
	struct smb2_hdr *hdr = &pdu->hdr;
	int offset; /* the offset from the beginning of SMB to data area */
	int data_length; /* the length of the variable length data area */
	unsigned int offset; /* the offset from the beginning of SMB to data area */
	unsigned int data_length; /* the length of the variable length data area */
	int ret;

	/* Structure Size has already been checked to make sure it is 64 */
	int len = le16_to_cpu(hdr->StructureSize);
	*len = le16_to_cpu(hdr->StructureSize);

	/*
	 * StructureSize2, ie length of fixed parameter area has already
	 * been checked to make sure it is the correct length.
	 */
	len += le16_to_cpu(pdu->StructureSize2);
	*len += le16_to_cpu(pdu->StructureSize2);
	/*
	 * StructureSize2 of smb2_lock pdu is set to 48, indicating
	 * the size of smb2 lock request with single smb2_lock_element
	 * regardless of number of locks. Subtract single
	 * smb2_lock_element for correct buffer size check.
	 */
	if (hdr->Command == SMB2_LOCK)
		*len -= sizeof(struct smb2_lock_element);

	if (has_smb2_data_area[le16_to_cpu(hdr->Command)] == false)
		goto calc_size_exit;

	smb2_get_data_area_len(&offset, &data_length, hdr);
	ksmbd_debug(SMB, "SMB2 data length %d offset %d\n", data_length,
	ret = smb2_get_data_area_len(&offset, &data_length, hdr);
	if (ret)
		return ret;
	ksmbd_debug(SMB, "SMB2 data length %u offset %u\n", data_length,
		    offset);

	if (data_length > 0) {
@@ -237,16 +231,19 @@ static unsigned int smb2_calc_size(void *buf)
		 * for some commands, typically those with odd StructureSize,
		 * so we must add one to the calculation.
		 */
		if (offset + 1 < len)
		if (offset + 1 < *len) {
			ksmbd_debug(SMB,
				    "data area offset %d overlaps SMB2 header %d\n",
				    offset + 1, len);
		else
			len = offset + data_length;
				    "data area offset %d overlaps SMB2 header %u\n",
				    offset + 1, *len);
			return -EINVAL;
		}

		*len = offset + data_length;
	}

calc_size_exit:
	ksmbd_debug(SMB, "SMB2 len %d\n", len);
	return len;
	ksmbd_debug(SMB, "SMB2 len %u\n", *len);
	return 0;
}

static inline int smb2_query_info_req_len(struct smb2_query_info_req *h)
@@ -391,9 +388,11 @@ int ksmbd_smb2_check_message(struct ksmbd_work *work)
		return 1;
	}

	clc_len = smb2_calc_size(hdr);
	if (smb2_calc_size(hdr, &clc_len))
		return 1;

	if (len != clc_len) {
		/* server can return one byte more due to implied bcc[0] */
		/* client can return one byte more due to implied bcc[0] */
		if (clc_len == len + 1)
			return 0;

@@ -418,9 +417,6 @@ int ksmbd_smb2_check_message(struct ksmbd_work *work)
			return 0;
		}

		if (command == SMB2_LOCK_HE && len == 88)
			return 0;

		ksmbd_debug(SMB,
			    "cli req too short, len %d not %d. cmd:%d mid:%llu\n",
			    len, clc_len, command,
+0 −5
Original line number Diff line number Diff line
@@ -187,11 +187,6 @@ static struct smb_version_cmds smb2_0_server_cmds[NUMBER_OF_SMB2_COMMANDS] = {
	[SMB2_CHANGE_NOTIFY_HE]	=	{ .proc = smb2_notify},
};

int init_smb2_0_server(struct ksmbd_conn *conn)
{
	return -EOPNOTSUPP;
}

/**
 * init_smb2_1_server() - initialize a smb server connection with smb2.1
 *			command dispatcher
+18 −41
Original line number Diff line number Diff line
@@ -236,9 +236,6 @@ int init_smb2_neg_rsp(struct ksmbd_work *work)

	if (conn->need_neg == false)
		return -EINVAL;
	if (!(conn->dialect >= SMB20_PROT_ID &&
	      conn->dialect <= SMB311_PROT_ID))
		return -EINVAL;

	rsp_hdr = work->response_buf;

@@ -1166,13 +1163,6 @@ int smb2_handle_negotiate(struct ksmbd_work *work)
	case SMB21_PROT_ID:
		init_smb2_1_server(conn);
		break;
	case SMB20_PROT_ID:
		rc = init_smb2_0_server(conn);
		if (rc) {
			rsp->hdr.Status = STATUS_NOT_SUPPORTED;
			goto err_out;
		}
		break;
	case SMB2X_PROT_ID:
	case BAD_PROT_ID:
	default:
@@ -1191,11 +1181,9 @@ int smb2_handle_negotiate(struct ksmbd_work *work)
	rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size);
	rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size);

	if (conn->dialect > SMB20_PROT_ID) {
	memcpy(conn->ClientGUID, req->ClientGUID,
			SMB2_CLIENT_GUID_SIZE);
	conn->cli_sec_mode = le16_to_cpu(req->SecurityMode);
	}

	rsp->StructureSize = cpu_to_le16(65);
	rsp->DialectRevision = cpu_to_le16(conn->dialect);
@@ -1537,12 +1525,10 @@ static int ntlm_authenticate(struct ksmbd_work *work)
		}
	}

	if (conn->dialect > SMB20_PROT_ID) {
	if (!ksmbd_conn_lookup_dialect(conn)) {
		pr_err("fail to verify the dialect\n");
		return -ENOENT;
	}
	}
	return 0;
}

@@ -1623,12 +1609,10 @@ static int krb5_authenticate(struct ksmbd_work *work)
		}
	}

	if (conn->dialect > SMB20_PROT_ID) {
	if (!ksmbd_conn_lookup_dialect(conn)) {
		pr_err("fail to verify the dialect\n");
		return -ENOENT;
	}
	}
	return 0;
}
#else
@@ -5499,7 +5483,6 @@ static int set_file_basic_info(struct ksmbd_file *fp,
			       struct ksmbd_share_config *share)
{
	struct iattr attrs;
	struct timespec64 ctime;
	struct file *filp;
	struct inode *inode;
	struct user_namespace *user_ns;
@@ -5521,13 +5504,11 @@ static int set_file_basic_info(struct ksmbd_file *fp,
		attrs.ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
	}

	if (file_info->ChangeTime) {
		attrs.ia_ctime = ksmbd_NTtimeToUnix(file_info->ChangeTime);
		ctime = attrs.ia_ctime;
	attrs.ia_valid |= ATTR_CTIME;
	} else {
		ctime = inode->i_ctime;
	}
	if (file_info->ChangeTime)
		attrs.ia_ctime = ksmbd_NTtimeToUnix(file_info->ChangeTime);
	else
		attrs.ia_ctime = inode->i_ctime;

	if (file_info->LastWriteTime) {
		attrs.ia_mtime = ksmbd_NTtimeToUnix(file_info->LastWriteTime);
@@ -5573,11 +5554,9 @@ static int set_file_basic_info(struct ksmbd_file *fp,
			return -EACCES;

		inode_lock(inode);
		inode->i_ctime = attrs.ia_ctime;
		attrs.ia_valid &= ~ATTR_CTIME;
		rc = notify_change(user_ns, dentry, &attrs, NULL);
		if (!rc) {
			inode->i_ctime = ctime;
			mark_inode_dirty(inode);
		}
		inode_unlock(inode);
	}
	return rc;
@@ -8411,20 +8390,18 @@ int smb3_decrypt_req(struct ksmbd_work *work)
	struct smb2_hdr *hdr;
	unsigned int pdu_length = get_rfc1002_len(buf);
	struct kvec iov[2];
	unsigned int buf_data_size = pdu_length + 4 -
	int buf_data_size = pdu_length + 4 -
		sizeof(struct smb2_transform_hdr);
	struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
	int rc = 0;

	if (pdu_length + 4 <
	    sizeof(struct smb2_transform_hdr) + sizeof(struct smb2_hdr)) {
	if (buf_data_size < sizeof(struct smb2_hdr)) {
		pr_err("Transform message is too small (%u)\n",
		       pdu_length);
		return -ECONNABORTED;
	}

	if (pdu_length + 4 <
	    le32_to_cpu(tr_hdr->OriginalMessageSize) + sizeof(struct smb2_transform_hdr)) {
	if (buf_data_size < le32_to_cpu(tr_hdr->OriginalMessageSize)) {
		pr_err("Transform message is broken\n");
		return -ECONNABORTED;
	}
Loading