1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
14  */
15 
16 /*
17  * Dispatch function for SMB2_NEGOTIATE
18  */
19 
20 #include <smbsrv/smb2_kproto.h>
21 #include <smbsrv/smb2.h>
22 
23 static int smb2_negotiate_common(smb_request_t *, uint16_t);
24 
25 uint32_t smb2srv_capabilities =
26 	SMB2_CAP_DFS |
27 	SMB2_CAP_LEASING |
28 	SMB2_CAP_LARGE_MTU |
29 	SMB2_CAP_ENCRYPTION;
30 
31 /*
32  * These are not intended as customer tunables, but dev. & test folks
33  * might want to adjust them (with caution).
34  *
35  * smb2_tcp_bufsize is the TCP buffer size, applied to the network socket
36  * with setsockopt SO_SNDBUF, SO_RCVBUF.  These set the TCP window size.
37  * This is also used as a "sanity limit" for internal send/reply message
38  * allocations.  Note that with compounding SMB2 messages may contain
39  * multiple requests/responses.  This size should be large enough for
40  * at least a few SMB2 requests, and at least 2X smb2_max_rwsize.
41  *
42  * smb2_max_rwsize is what we put in the SMB2 negotiate response to tell
43  * the client the largest read and write request size we'll support.
44  * For now, we're using contiguous allocations, so keep this at 64KB
45  * so that (even with message overhead) allocations stay below 128KB,
46  * avoiding kmem_alloc -> page_create_va thrashing.
47  *
48  * smb2_max_trans is the largest "transact" send or receive, which is
49  * used for directory listings and info set/get operations.
50  */
51 uint32_t smb2_tcp_bufsize = (1<<22);	/* 4MB */
52 uint32_t smb2_max_rwsize = (1<<16);	/* 64KB */
53 uint32_t smb2_max_trans  = (1<<16);	/* 64KB */
54 
55 /*
56  * With clients (e.g. HP scanners) that don't advertise SMB2_CAP_LARGE_MTU
57  * (including all clients using dialect < SMB 2.1), use a "conservative" value
58  * for max r/w size because some older clients misbehave with larger values.
59  * 64KB is recommended in the [MS-SMB2] spec.  (3.3.5.3.1 SMB 2.1 or SMB 3.x
60  * Support) as the minimum so we'll use that.
61  */
62 uint32_t smb2_old_rwsize = (1<<16);	/* 64KB */
63 
64 /*
65  * List of all SMB2 versions we implement.  Note that the
66  * versions we support may be limited by the
67  * _cfg.skc_max_protocol and min_protocol settings.
68  */
69 static uint16_t smb2_versions[] = {
70 	0x202,	/* SMB 2.002 */
71 	0x210,	/* SMB 2.1 */
72 	0x300,	/* SMB 3.0 */
73 };
74 static uint16_t smb2_nversions =
75     sizeof (smb2_versions) / sizeof (smb2_versions[0]);
76 
77 static boolean_t
78 smb2_supported_version(smb_session_t *s, uint16_t version)
79 {
80 	int i;
81 
82 	if (version > s->s_cfg.skc_max_protocol ||
83 	    version < s->s_cfg.skc_min_protocol)
84 		return (B_FALSE);
85 	for (i = 0; i < smb2_nversions; i++)
86 		if (version == smb2_versions[i])
87 			return (B_TRUE);
88 	return (B_FALSE);
89 }
90 
91 /*
92  * Helper for the (SMB1) smb_com_negotiate().  This is the
93  * very unusual protocol interaction where an SMB1 negotiate
94  * gets an SMB2 negotiate response.  This is the normal way
95  * clients first find out if the server supports SMB2.
96  *
97  * Note: This sends an SMB2 reply _itself_ and then returns
98  * SDRC_NO_REPLY so the caller will not send an SMB1 reply.
99  * Also, this is called directly from the reader thread, so
100  * we know this is the only thread using this session.
101  *
102  * The caller frees this request.
103  */
104 smb_sdrc_t
105 smb1_negotiate_smb2(smb_request_t *sr)
106 {
107 	smb_session_t *s = sr->session;
108 	smb_arg_negotiate_t *negprot = sr->sr_negprot;
109 	uint16_t smb2_version;
110 	int rc;
111 
112 	/*
113 	 * Note: In the SMB1 negotiate command handler, we
114 	 * agreed with one of the SMB2 dialects.  If that
115 	 * dialect was "SMB 2.002", we'll respond here with
116 	 * version 0x202 and negotiation is done.  If that
117 	 * dialect was "SMB 2.???", we'll respond here with
118 	 * the "wildcard" version 0x2FF, and the client will
119 	 * come back with an SMB2 negotiate.
120 	 */
121 	switch (negprot->ni_dialect) {
122 	case DIALECT_SMB2002:	/* SMB 2.002 (a.k.a. SMB2.0) */
123 		smb2_version = SMB_VERS_2_002;
124 		s->dialect = smb2_version;
125 		s->s_state = SMB_SESSION_STATE_NEGOTIATED;
126 		/* Allow normal SMB2 requests now. */
127 		s->newrq_func = smb2sr_newrq;
128 		break;
129 	case DIALECT_SMB2XXX:	/* SMB 2.??? (wildcard vers) */
130 		/*
131 		 * Expecting an SMB2 negotiate next, so keep the
132 		 * initial s->newrq_func.
133 		 */
134 		smb2_version = 0x2FF;
135 		break;
136 	default:
137 		return (SDRC_DROP_VC);
138 	}
139 
140 	/*
141 	 * Clients that negotiate SMB2 from SMB1 have not yet had the
142 	 * opportunity to provide us with a secmode. However, any
143 	 * client that negotiates SMB2 should support signing, so
144 	 * this should be fiction good enough to pass the signing
145 	 * check in smb2_negotiate_common(). Even if the client
146 	 * doesn't support signing and we require it, we'll fail them
147 	 * later when they fail to sign the packet. For 2.???,
148 	 * we'll check the real secmode when the 2nd negotiate comes.
149 	 */
150 	s->cli_secmode = SMB2_NEGOTIATE_SIGNING_ENABLED;
151 
152 	/*
153 	 * We did not decode an SMB2 header, so make sure
154 	 * the SMB2 header fields are initialized.
155 	 * (Most are zero from smb_request_alloc.)
156 	 * Also, the SMB1 common dispatch code reserved space
157 	 * for an SMB1 header, which we need to undo here.
158 	 */
159 	sr->smb2_reply_hdr = sr->reply.chain_offset = 0;
160 	sr->smb2_cmd_code = SMB2_NEGOTIATE;
161 
162 	rc = smb2_negotiate_common(sr, smb2_version);
163 	smb2_send_reply(sr);
164 	if (rc != 0)
165 		return (SDRC_DROP_VC);
166 
167 	/*
168 	 * We sent the reply, so tell the SMB1 dispatch
169 	 * it should NOT (also) send a reply.
170 	 */
171 	return (SDRC_NO_REPLY);
172 }
173 
174 static uint16_t
175 smb2_find_best_dialect(smb_session_t *s, uint16_t cl_versions[],
176     uint16_t version_cnt)
177 {
178 	uint16_t best_version = 0;
179 	int i;
180 
181 	for (i = 0; i < version_cnt; i++)
182 		if (smb2_supported_version(s, cl_versions[i]) &&
183 		    best_version < cl_versions[i])
184 			best_version = cl_versions[i];
185 
186 	return (best_version);
187 }
188 
189 /*
190  * SMB2 Negotiate gets special handling.  This is called directly by
191  * the reader thread (see smbsr_newrq_initial) with what _should_ be
192  * an SMB2 Negotiate.  Only the "\feSMB" header has been checked
193  * when this is called, so this needs to check the SMB command,
194  * if it's Negotiate execute it, then send the reply, etc.
195  *
196  * Since this is called directly from the reader thread, we
197  * know this is the only thread currently using this session.
198  * This has to duplicate some of what smb2sr_work does as a
199  * result of bypassing the normal dispatch mechanism.
200  *
201  * The caller always frees this request.
202  *
203  * Return value is 0 for success, and anything else will
204  * terminate the reader thread (drop the connection).
205  */
206 int
207 smb2_newrq_negotiate(smb_request_t *sr)
208 {
209 	smb_session_t *s = sr->session;
210 	int rc;
211 	uint16_t struct_size;
212 	uint16_t best_version;
213 	uint16_t version_cnt;
214 	uint16_t cl_versions[8];
215 
216 	sr->smb2_cmd_hdr = sr->command.chain_offset;
217 	rc = smb2_decode_header(sr);
218 	if (rc != 0)
219 		return (rc);
220 
221 	if ((sr->smb2_cmd_code != SMB2_NEGOTIATE) ||
222 	    (sr->smb2_next_command != 0))
223 		return (-1);
224 
225 	/*
226 	 * Decode SMB2 Negotiate (fixed-size part)
227 	 */
228 	rc = smb_mbc_decodef(
229 	    &sr->command, "www..l16c8.",
230 	    &struct_size,	/* w */
231 	    &version_cnt,	/* w */
232 	    &s->cli_secmode,	/* w */
233 	    /* reserved		(..) */
234 	    &s->capabilities,	/* l */
235 	    s->clnt_uuid);	/* 16c */
236 	    /* start_time	  8. */
237 	if (rc != 0)
238 		return (rc);
239 	if (struct_size != 36 || version_cnt > 8)
240 		return (-1);
241 
242 	/*
243 	 * Decode SMB2 Negotiate (variable part)
244 	 */
245 	rc = smb_mbc_decodef(&sr->command,
246 	    "#w", version_cnt, cl_versions);
247 	if (rc != 0)
248 		return (rc);
249 
250 	DTRACE_SMB2_START(op__Negotiate, smb_request_t *, sr);
251 
252 	/*
253 	 * The client offers an array of protocol versions it
254 	 * supports, which we have decoded into cl_versions[].
255 	 * We walk the array and pick the highest supported.
256 	 */
257 	best_version = smb2_find_best_dialect(s, cl_versions, version_cnt);
258 	if (best_version == 0) {
259 		cmn_err(CE_NOTE, "clnt %s no supported dialect",
260 		    sr->session->ip_addr_str);
261 		sr->smb2_status = NT_STATUS_INVALID_PARAMETER;
262 		rc = -1;
263 		goto errout;
264 	}
265 	s->dialect = best_version;
266 
267 	/* Allow normal SMB2 requests now. */
268 	s->s_state = SMB_SESSION_STATE_NEGOTIATED;
269 	s->newrq_func = smb2sr_newrq;
270 
271 	rc = smb2_negotiate_common(sr, best_version);
272 
273 errout:
274 	/* sr->smb2_status was set */
275 	DTRACE_SMB2_DONE(op__Negotiate, smb_request_t *, sr);
276 
277 	smb2_send_reply(sr);
278 
279 	return (rc);
280 }
281 
282 /*
283  * Common parts of SMB2 Negotiate, used for both the
284  * SMB1-to-SMB2 style, and straight SMB2 style.
285  * Do negotiation decisions and encode the reply.
286  * The caller does the network send.
287  *
288  * Return value is 0 for success, and anything else will
289  * terminate the reader thread (drop the connection).
290  */
291 static int
292 smb2_negotiate_common(smb_request_t *sr, uint16_t version)
293 {
294 	timestruc_t boot_tv, now_tv;
295 	smb_session_t *s = sr->session;
296 	int rc;
297 	uint32_t max_rwsize;
298 	uint16_t secmode;
299 
300 	sr->smb2_status = 0;
301 
302 	/*
303 	 * Negotiation itself.  First the Security Mode.
304 	 */
305 	secmode = SMB2_NEGOTIATE_SIGNING_ENABLED;
306 	if (sr->sr_cfg->skc_signing_required) {
307 		secmode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
308 		/* Make sure client at least enables signing. */
309 		if ((s->cli_secmode & secmode) == 0) {
310 			sr->smb2_status = NT_STATUS_INVALID_PARAMETER;
311 		}
312 	}
313 	s->srv_secmode = secmode;
314 
315 	s->cmd_max_bytes = smb2_tcp_bufsize;
316 	s->reply_max_bytes = smb2_tcp_bufsize;
317 
318 	/*
319 	 * "The number of credits held by the client MUST be considered
320 	 * as 1 when the connection is established." [MS-SMB2]
321 	 * We leave credits at 1 until the first successful
322 	 * session setup is completed.
323 	 */
324 	s->s_cur_credits = s->s_max_credits = 1;
325 	sr->smb2_credit_response = 1;
326 
327 	boot_tv.tv_sec = smb_get_boottime();
328 	boot_tv.tv_nsec = 0;
329 	now_tv.tv_sec = gethrestime_sec();
330 	now_tv.tv_nsec = 0;
331 
332 	/*
333 	 * SMB2 negotiate reply
334 	 */
335 	sr->smb2_hdr_flags = SMB2_FLAGS_SERVER_TO_REDIR;
336 	(void) smb2_encode_header(sr, B_FALSE);
337 	if (sr->smb2_status != 0) {
338 		smb2sr_put_error(sr, sr->smb2_status);
339 		/* smb2_send_reply(sr); in caller */
340 		return (-1); /* will drop */
341 	}
342 
343 	/*
344 	 * If the version is 0x2FF, we haven't completed negotiate.
345 	 * Don't initialize until we have our final request.
346 	 */
347 	if (version != 0x2FF)
348 		smb2_sign_init_mech(s);
349 
350 	/*
351 	 * [MS-SMB2] 3.3.5.4 Receiving an SMB2 NEGOTIATE Request
352 	 *
353 	 * Only set CAP_ENCRYPTION if this is 3.0 or 3.0.2 and
354 	 * the client has it set.
355 	 */
356 
357 	if (s->dialect < SMB_VERS_3_0 ||
358 	    !SMB3_CLIENT_ENCRYPTS(sr) ||
359 	    smb3_encrypt_init_mech(s) != 0)
360 		s->srv_cap = smb2srv_capabilities & ~SMB2_CAP_ENCRYPTION;
361 	else
362 		s->srv_cap = smb2srv_capabilities;
363 
364 	/*
365 	 * See notes above smb2_max_rwsize, smb2_old_rwsize
366 	 */
367 	if (s->capabilities & SMB2_CAP_LARGE_MTU)
368 		max_rwsize = smb2_max_rwsize;
369 	else
370 		max_rwsize = smb2_old_rwsize;
371 
372 	rc = smb_mbc_encodef(
373 	    &sr->reply,
374 	    "wwww#cllllTTwwl#c",
375 	    65,	/* StructSize */	/* w */
376 	    s->srv_secmode,		/* w */
377 	    version,			/* w */
378 	    0, /* reserved */		/* w */
379 	    UUID_LEN,			/* # */
380 	    &s->s_cfg.skc_machine_uuid, /* c */
381 	    s->srv_cap,			/* l */
382 	    smb2_max_trans,		/* l */
383 	    max_rwsize,			/* l */
384 	    max_rwsize,			/* l */
385 	    &now_tv,			/* T */
386 	    &boot_tv,			/* T */
387 	    128, /* SecBufOff */	/* w */
388 	    sr->sr_cfg->skc_negtok_len,	/* w */
389 	    0,	/* reserved */		/* l */
390 	    sr->sr_cfg->skc_negtok_len,	/* # */
391 	    sr->sr_cfg->skc_negtok);	/* c */
392 
393 	/* smb2_send_reply(sr); in caller */
394 
395 	(void) ksocket_setsockopt(s->sock, SOL_SOCKET,
396 	    SO_SNDBUF, (const void *)&smb2_tcp_bufsize,
397 	    sizeof (smb2_tcp_bufsize), CRED());
398 	(void) ksocket_setsockopt(s->sock, SOL_SOCKET,
399 	    SO_RCVBUF, (const void *)&smb2_tcp_bufsize,
400 	    sizeof (smb2_tcp_bufsize), CRED());
401 
402 	return (rc);
403 }
404 
405 /*
406  * SMB2 Dispatch table handler, which will run if we see an
407  * SMB2_NEGOTIATE after the initial negotiation is done.
408  * That would be a protocol error.
409  */
410 smb_sdrc_t
411 smb2_negotiate(smb_request_t *sr)
412 {
413 	sr->smb2_status = NT_STATUS_INVALID_PARAMETER;
414 	return (SDRC_ERROR);
415 }
416 
417 /*
418  * VALIDATE_NEGOTIATE_INFO [MS-SMB2] 2.2.32.6
419  */
420 uint32_t
421 smb2_nego_validate(smb_request_t *sr, smb_fsctl_t *fsctl)
422 {
423 	smb_session_t *s = sr->session;
424 	int rc;
425 
426 	/*
427 	 * The spec. says to parse the VALIDATE_NEGOTIATE_INFO here
428 	 * and verify that the original negotiate was not modified.
429 	 * The only tampering we need worry about is secmode, and
430 	 * we're not taking that from the client, so don't bother.
431 	 *
432 	 * One interesting requirement here is that we MUST reply
433 	 * with exactly the same information as we returned in our
434 	 * original reply to the SMB2 negotiate on this session.
435 	 * If we don't the client closes the connection.
436 	 */
437 
438 	/* dialects[8] taken from cl_versions[8] in smb2_newrq_negotiate */
439 	uint32_t capabilities;
440 	uint16_t secmode, num_dialects, dialects[8];
441 	uint8_t clnt_guid[16];
442 
443 	if (fsctl->InputCount < 24)
444 		goto drop;
445 
446 	(void) smb_mbc_decodef(fsctl->in_mbc, "l16cww",
447 	    &capabilities, /* l */
448 	    &clnt_guid, /* 16c */
449 	    &secmode, /* w */
450 	    &num_dialects); /* w */
451 
452 	if (num_dialects == 0 || num_dialects > 8)
453 		goto drop;
454 	if (secmode != s->cli_secmode)
455 		goto drop;
456 	if (capabilities != s->capabilities)
457 		goto drop;
458 	if (memcmp(clnt_guid, s->clnt_uuid, sizeof (clnt_guid)) != 0)
459 		goto drop;
460 
461 	if (fsctl->InputCount < (24 + num_dialects * sizeof (*dialects)))
462 		goto drop;
463 
464 	rc = smb_mbc_decodef(fsctl->in_mbc, "#w", num_dialects, dialects);
465 	if (rc != 0)
466 		goto drop;
467 
468 	if (smb2_find_best_dialect(s, dialects, num_dialects) != s->dialect)
469 		goto drop;
470 
471 	rc = smb_mbc_encodef(
472 	    fsctl->out_mbc, "l#cww",
473 	    s->srv_cap,			/* l */
474 	    UUID_LEN,			/* # */
475 	    &s->s_cfg.skc_machine_uuid, /* c */
476 	    s->srv_secmode,		/* w */
477 	    s->dialect);		/* w */
478 	if (rc == 0)
479 		return (rc);
480 
481 drop:
482 	smb_session_disconnect(s);
483 	return (NT_STATUS_ACCESS_DENIED);
484 }
485