xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb2_negotiate.c (revision 1160dcf7283d2485f2b9c32da573db0275558d9b)
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  * highest version we support may be limited by the
67  * _cfg.skc_max_protocol setting.
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 		return (B_FALSE);
84 	for (i = 0; i < smb2_nversions; i++)
85 		if (version == smb2_versions[i])
86 			return (B_TRUE);
87 	return (B_FALSE);
88 }
89 
90 /*
91  * Helper for the (SMB1) smb_com_negotiate().  This is the
92  * very unusual protocol interaction where an SMB1 negotiate
93  * gets an SMB2 negotiate response.  This is the normal way
94  * clients first find out if the server supports SMB2.
95  *
96  * Note: This sends an SMB2 reply _itself_ and then returns
97  * SDRC_NO_REPLY so the caller will not send an SMB1 reply.
98  * Also, this is called directly from the reader thread, so
99  * we know this is the only thread using this session.
100  *
101  * The caller frees this request.
102  */
103 smb_sdrc_t
104 smb1_negotiate_smb2(smb_request_t *sr)
105 {
106 	smb_session_t *s = sr->session;
107 	smb_arg_negotiate_t *negprot = sr->sr_negprot;
108 	uint16_t smb2_version;
109 	int rc;
110 
111 	/*
112 	 * Note: In the SMB1 negotiate command handler, we
113 	 * agreed with one of the SMB2 dialects.  If that
114 	 * dialect was "SMB 2.002", we'll respond here with
115 	 * version 0x202 and negotiation is done.  If that
116 	 * dialect was "SMB 2.???", we'll respond here with
117 	 * the "wildcard" version 0x2FF, and the client will
118 	 * come back with an SMB2 negotiate.
119 	 */
120 	switch (negprot->ni_dialect) {
121 	case DIALECT_SMB2002:	/* SMB 2.002 (a.k.a. SMB2.0) */
122 		smb2_version = 0x202;
123 		s->dialect = smb2_version;
124 		s->s_state = SMB_SESSION_STATE_NEGOTIATED;
125 		/* Allow normal SMB2 requests now. */
126 		s->newrq_func = smb2sr_newrq;
127 		break;
128 	case DIALECT_SMB2XXX:	/* SMB 2.??? (wildcard vers) */
129 		/*
130 		 * Expecting an SMB2 negotiate next, so keep the
131 		 * initial s->newrq_func.
132 		 */
133 		smb2_version = 0x2FF;
134 		break;
135 	default:
136 		return (SDRC_DROP_VC);
137 	}
138 
139 	/*
140 	 * Clients that negotiate SMB2 from SMB1 have not yet had the
141 	 * opportunity to provide us with a secmode. However, any
142 	 * client that negotiates SMB2 should support signing, so
143 	 * this should be fiction good enough to pass the signing
144 	 * check in smb2_negotiate_common(). Even if the client
145 	 * doesn't support signing and we require it, we'll fail them
146 	 * later when they fail to sign the packet. For 2.???,
147 	 * we'll check the real secmode when the 2nd negotiate comes.
148 	 */
149 	s->cli_secmode = SMB2_NEGOTIATE_SIGNING_ENABLED;
150 
151 	/*
152 	 * We did not decode an SMB2 header, so make sure
153 	 * the SMB2 header fields are initialized.
154 	 * (Most are zero from smb_request_alloc.)
155 	 * Also, the SMB1 common dispatch code reserved space
156 	 * for an SMB1 header, which we need to undo here.
157 	 */
158 	sr->smb2_reply_hdr = sr->reply.chain_offset = 0;
159 	sr->smb2_cmd_code = SMB2_NEGOTIATE;
160 
161 	rc = smb2_negotiate_common(sr, smb2_version);
162 	smb2_send_reply(sr);
163 	if (rc != 0)
164 		return (SDRC_DROP_VC);
165 
166 	/*
167 	 * We sent the reply, so tell the SMB1 dispatch
168 	 * it should NOT (also) send a reply.
169 	 */
170 	return (SDRC_NO_REPLY);
171 }
172 
173 static uint16_t
174 smb2_find_best_dialect(smb_session_t *s, uint16_t cl_versions[],
175     uint16_t version_cnt)
176 {
177 	uint16_t best_version = 0;
178 	int i;
179 
180 	for (i = 0; i < version_cnt; i++)
181 		if (smb2_supported_version(s, cl_versions[i]) &&
182 		    best_version < cl_versions[i])
183 			best_version = cl_versions[i];
184 
185 	return (best_version);
186 }
187 
188 /*
189  * SMB2 Negotiate gets special handling.  This is called directly by
190  * the reader thread (see smbsr_newrq_initial) with what _should_ be
191  * an SMB2 Negotiate.  Only the "\feSMB" header has been checked
192  * when this is called, so this needs to check the SMB command,
193  * if it's Negotiate execute it, then send the reply, etc.
194  *
195  * Since this is called directly from the reader thread, we
196  * know this is the only thread currently using this session.
197  * This has to duplicate some of what smb2sr_work does as a
198  * result of bypassing the normal dispatch mechanism.
199  *
200  * The caller always frees this request.
201  *
202  * Return value is 0 for success, and anything else will
203  * terminate the reader thread (drop the connection).
204  */
205 int
206 smb2_newrq_negotiate(smb_request_t *sr)
207 {
208 	smb_session_t *s = sr->session;
209 	int rc;
210 	uint16_t struct_size;
211 	uint16_t best_version;
212 	uint16_t version_cnt;
213 	uint16_t cl_versions[8];
214 
215 	sr->smb2_cmd_hdr = sr->command.chain_offset;
216 	rc = smb2_decode_header(sr);
217 	if (rc != 0)
218 		return (rc);
219 
220 	if ((sr->smb2_cmd_code != SMB2_NEGOTIATE) ||
221 	    (sr->smb2_next_command != 0))
222 		return (-1);
223 
224 	/*
225 	 * Decode SMB2 Negotiate (fixed-size part)
226 	 */
227 	rc = smb_mbc_decodef(
228 	    &sr->command, "www..l16c8.",
229 	    &struct_size,	/* w */
230 	    &version_cnt,	/* w */
231 	    &s->cli_secmode,	/* w */
232 	    /* reserved		(..) */
233 	    &s->capabilities,	/* l */
234 	    s->clnt_uuid);	/* 16c */
235 	    /* start_time	  8. */
236 	if (rc != 0)
237 		return (rc);
238 	if (struct_size != 36 || version_cnt > 8)
239 		return (-1);
240 
241 	/*
242 	 * Decode SMB2 Negotiate (variable part)
243 	 */
244 	rc = smb_mbc_decodef(&sr->command,
245 	    "#w", version_cnt, cl_versions);
246 	if (rc != 0)
247 		return (rc);
248 
249 	DTRACE_SMB2_START(op__Negotiate, smb_request_t *, sr);
250 
251 	/*
252 	 * The client offers an array of protocol versions it
253 	 * supports, which we have decoded into cl_versions[].
254 	 * We walk the array and pick the highest supported.
255 	 */
256 	best_version = smb2_find_best_dialect(s, cl_versions, version_cnt);
257 	if (best_version == 0)
258 		return (SDRC_DROP_VC);
259 	s->dialect = best_version;
260 
261 	/* Allow normal SMB2 requests now. */
262 	s->s_state = SMB_SESSION_STATE_NEGOTIATED;
263 	s->newrq_func = smb2sr_newrq;
264 
265 	rc = smb2_negotiate_common(sr, best_version);
266 
267 	/* sr->smb2_status was set */
268 	DTRACE_SMB2_DONE(op__Negotiate, smb_request_t *, sr);
269 
270 	smb2_send_reply(sr);
271 
272 	return (rc);
273 }
274 
275 /*
276  * Common parts of SMB2 Negotiate, used for both the
277  * SMB1-to-SMB2 style, and straight SMB2 style.
278  * Do negotiation decisions and encode the reply.
279  * The caller does the network send.
280  *
281  * Return value is 0 for success, and anything else will
282  * terminate the reader thread (drop the connection).
283  */
284 static int
285 smb2_negotiate_common(smb_request_t *sr, uint16_t version)
286 {
287 	timestruc_t boot_tv, now_tv;
288 	smb_session_t *s = sr->session;
289 	int rc;
290 	uint32_t max_rwsize;
291 	uint16_t secmode;
292 
293 	sr->smb2_status = 0;
294 
295 	/*
296 	 * Negotiation itself.  First the Security Mode.
297 	 */
298 	secmode = SMB2_NEGOTIATE_SIGNING_ENABLED;
299 	if (sr->sr_cfg->skc_signing_required) {
300 		secmode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
301 		/* Make sure client at least enables signing. */
302 		if ((s->cli_secmode & secmode) == 0) {
303 			sr->smb2_status = NT_STATUS_INVALID_PARAMETER;
304 		}
305 	}
306 	s->srv_secmode = secmode;
307 
308 	s->cmd_max_bytes = smb2_tcp_bufsize;
309 	s->reply_max_bytes = smb2_tcp_bufsize;
310 
311 	/*
312 	 * "The number of credits held by the client MUST be considered
313 	 * as 1 when the connection is established." [MS-SMB2]
314 	 * We leave credits at 1 until the first successful
315 	 * session setup is completed.
316 	 */
317 	s->s_cur_credits = s->s_max_credits = 1;
318 	sr->smb2_credit_response = 1;
319 
320 	boot_tv.tv_sec = smb_get_boottime();
321 	boot_tv.tv_nsec = 0;
322 	now_tv.tv_sec = gethrestime_sec();
323 	now_tv.tv_nsec = 0;
324 
325 	/*
326 	 * SMB2 negotiate reply
327 	 */
328 	sr->smb2_hdr_flags = SMB2_FLAGS_SERVER_TO_REDIR;
329 	(void) smb2_encode_header(sr, B_FALSE);
330 	if (sr->smb2_status != 0) {
331 		smb2sr_put_error(sr, sr->smb2_status);
332 		/* smb2_send_reply(sr); in caller */
333 		return (-1); /* will drop */
334 	}
335 
336 	/*
337 	 * If the version is 0x2FF, we haven't completed negotiate.
338 	 * Don't initialize until we have our final request.
339 	 */
340 	if (version != 0x2FF)
341 		smb2_sign_init_mech(s);
342 
343 	/*
344 	 * [MS-SMB2] 3.3.5.4 Receiving an SMB2 NEGOTIATE Request
345 	 *
346 	 * Only set CAP_ENCRYPTION if this is 3.0 or 3.0.2 and
347 	 * the client has it set.
348 	 */
349 
350 	if (s->dialect < SMB_VERS_3_0 ||
351 	    !SMB3_CLIENT_ENCRYPTS(sr) ||
352 	    smb3_encrypt_init_mech(s) != 0)
353 		s->srv_cap = smb2srv_capabilities & ~SMB2_CAP_ENCRYPTION;
354 	else
355 		s->srv_cap = smb2srv_capabilities;
356 
357 	/*
358 	 * See notes above smb2_max_rwsize, smb2_old_rwsize
359 	 */
360 	if (s->capabilities & SMB2_CAP_LARGE_MTU)
361 		max_rwsize = smb2_max_rwsize;
362 	else
363 		max_rwsize = smb2_old_rwsize;
364 
365 	rc = smb_mbc_encodef(
366 	    &sr->reply,
367 	    "wwww#cllllTTwwl#c",
368 	    65,	/* StructSize */	/* w */
369 	    s->srv_secmode,		/* w */
370 	    version,			/* w */
371 	    0, /* reserved */		/* w */
372 	    UUID_LEN,			/* # */
373 	    &s->s_cfg.skc_machine_uuid, /* c */
374 	    s->srv_cap,			/* l */
375 	    smb2_max_trans,		/* l */
376 	    max_rwsize,			/* l */
377 	    max_rwsize,			/* l */
378 	    &now_tv,			/* T */
379 	    &boot_tv,			/* T */
380 	    128, /* SecBufOff */	/* w */
381 	    sr->sr_cfg->skc_negtok_len,	/* w */
382 	    0,	/* reserved */		/* l */
383 	    sr->sr_cfg->skc_negtok_len,	/* # */
384 	    sr->sr_cfg->skc_negtok);	/* c */
385 
386 	/* smb2_send_reply(sr); in caller */
387 
388 	(void) ksocket_setsockopt(s->sock, SOL_SOCKET,
389 	    SO_SNDBUF, (const void *)&smb2_tcp_bufsize,
390 	    sizeof (smb2_tcp_bufsize), CRED());
391 	(void) ksocket_setsockopt(s->sock, SOL_SOCKET,
392 	    SO_RCVBUF, (const void *)&smb2_tcp_bufsize,
393 	    sizeof (smb2_tcp_bufsize), CRED());
394 
395 	return (rc);
396 }
397 
398 /*
399  * SMB2 Dispatch table handler, which will run if we see an
400  * SMB2_NEGOTIATE after the initial negotiation is done.
401  * That would be a protocol error.
402  */
403 smb_sdrc_t
404 smb2_negotiate(smb_request_t *sr)
405 {
406 	sr->smb2_status = NT_STATUS_INVALID_PARAMETER;
407 	return (SDRC_ERROR);
408 }
409 
410 /*
411  * VALIDATE_NEGOTIATE_INFO [MS-SMB2] 2.2.32.6
412  */
413 uint32_t
414 smb2_fsctl_vneginfo(smb_request_t *sr, smb_fsctl_t *fsctl)
415 {
416 	smb_session_t *s = sr->session;
417 	int rc;
418 
419 	/*
420 	 * The spec. says to parse the VALIDATE_NEGOTIATE_INFO here
421 	 * and verify that the original negotiate was not modified.
422 	 * The only tampering we need worry about is secmode, and
423 	 * we're not taking that from the client, so don't bother.
424 	 *
425 	 * One interesting requirement here is that we MUST reply
426 	 * with exactly the same information as we returned in our
427 	 * original reply to the SMB2 negotiate on this session.
428 	 * If we don't the client closes the connection.
429 	 */
430 
431 	/* dialects[8] taken from cl_versions[8] in smb2_newrq_negotiate */
432 	uint32_t capabilities;
433 	uint16_t secmode, num_dialects, dialects[8];
434 	uint8_t clnt_guid[16];
435 
436 	if (fsctl->InputCount < 24)
437 		goto drop;
438 
439 	(void) smb_mbc_decodef(fsctl->in_mbc, "l16cww",
440 	    &capabilities, /* l */
441 	    &clnt_guid, /* 16c */
442 	    &secmode, /* w */
443 	    &num_dialects); /* w */
444 
445 	if (num_dialects == 0 || num_dialects > 8)
446 		goto drop;
447 	if (secmode != s->cli_secmode)
448 		goto drop;
449 	if (capabilities != s->capabilities)
450 		goto drop;
451 	if (memcmp(clnt_guid, s->clnt_uuid, sizeof (clnt_guid)) != 0)
452 		goto drop;
453 
454 	if (fsctl->InputCount < (24 + num_dialects * sizeof (*dialects)))
455 		goto drop;
456 
457 	rc = smb_mbc_decodef(fsctl->in_mbc, "#w", num_dialects, dialects);
458 	if (rc != 0)
459 		goto drop;
460 
461 	if (smb2_find_best_dialect(s, dialects, num_dialects) != s->dialect)
462 		goto drop;
463 
464 	rc = smb_mbc_encodef(
465 	    fsctl->out_mbc, "l#cww",
466 	    s->srv_cap,			/* l */
467 	    UUID_LEN,			/* # */
468 	    &s->s_cfg.skc_machine_uuid, /* c */
469 	    s->srv_secmode,		/* w */
470 	    s->dialect);		/* w */
471 	if (rc == 0)
472 		return (rc);
473 
474 drop:
475 	smb_session_disconnect(s);
476 	return (NT_STATUS_ACCESS_DENIED);
477 }
478