Linux Audio

Check our new training course

Buildroot integration, development and maintenance

Need a Buildroot system for your embedded project?
Loading...
Note: File does not exist in v3.5.6.
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 *   Copyright (C) 2018 Samsung Electronics Co., Ltd.
  4 *   Copyright (C) 2018 Namjae Jeon <linkinjeon@kernel.org>
  5 */
  6
  7#include <linux/user_namespace.h>
  8
  9#include "smb_common.h"
 10#include "server.h"
 11#include "misc.h"
 12#include "smbstatus.h"
 13#include "connection.h"
 14#include "ksmbd_work.h"
 15#include "mgmt/user_session.h"
 16#include "mgmt/user_config.h"
 17#include "mgmt/tree_connect.h"
 18#include "mgmt/share_config.h"
 19
 20/*for shortname implementation */
 21static const char basechars[43] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_-!@#$%";
 22#define MANGLE_BASE (sizeof(basechars) / sizeof(char) - 1)
 23#define MAGIC_CHAR '~'
 24#define PERIOD '.'
 25#define mangle(V) ((char)(basechars[(V) % MANGLE_BASE]))
 26
 27struct smb_protocol {
 28	int		index;
 29	char		*name;
 30	char		*prot;
 31	__u16		prot_id;
 32};
 33
 34static struct smb_protocol smb1_protos[] = {
 35	{
 36		SMB21_PROT,
 37		"\2SMB 2.1",
 38		"SMB2_10",
 39		SMB21_PROT_ID
 40	},
 41	{
 42		SMB2X_PROT,
 43		"\2SMB 2.???",
 44		"SMB2_22",
 45		SMB2X_PROT_ID
 46	},
 47};
 48
 49static struct smb_protocol smb2_protos[] = {
 50	{
 51		SMB21_PROT,
 52		"\2SMB 2.1",
 53		"SMB2_10",
 54		SMB21_PROT_ID
 55	},
 56	{
 57		SMB30_PROT,
 58		"\2SMB 3.0",
 59		"SMB3_00",
 60		SMB30_PROT_ID
 61	},
 62	{
 63		SMB302_PROT,
 64		"\2SMB 3.02",
 65		"SMB3_02",
 66		SMB302_PROT_ID
 67	},
 68	{
 69		SMB311_PROT,
 70		"\2SMB 3.1.1",
 71		"SMB3_11",
 72		SMB311_PROT_ID
 73	},
 74};
 75
 76unsigned int ksmbd_server_side_copy_max_chunk_count(void)
 77{
 78	return 256;
 79}
 80
 81unsigned int ksmbd_server_side_copy_max_chunk_size(void)
 82{
 83	return (2U << 30) - 1;
 84}
 85
 86unsigned int ksmbd_server_side_copy_max_total_size(void)
 87{
 88	return (2U << 30) - 1;
 89}
 90
 91inline int ksmbd_min_protocol(void)
 92{
 93	return SMB21_PROT;
 94}
 95
 96inline int ksmbd_max_protocol(void)
 97{
 98	return SMB311_PROT;
 99}
100
101int ksmbd_lookup_protocol_idx(char *str)
102{
103	int offt = ARRAY_SIZE(smb1_protos) - 1;
104	int len = strlen(str);
105
106	while (offt >= 0) {
107		if (!strncmp(str, smb1_protos[offt].prot, len)) {
108			ksmbd_debug(SMB, "selected %s dialect idx = %d\n",
109				    smb1_protos[offt].prot, offt);
110			return smb1_protos[offt].index;
111		}
112		offt--;
113	}
114
115	offt = ARRAY_SIZE(smb2_protos) - 1;
116	while (offt >= 0) {
117		if (!strncmp(str, smb2_protos[offt].prot, len)) {
118			ksmbd_debug(SMB, "selected %s dialect idx = %d\n",
119				    smb2_protos[offt].prot, offt);
120			return smb2_protos[offt].index;
121		}
122		offt--;
123	}
124	return -1;
125}
126
127/**
128 * ksmbd_verify_smb_message() - check for valid smb2 request header
129 * @work:	smb work
130 *
131 * check for valid smb signature and packet direction(request/response)
132 *
133 * Return:      0 on success, otherwise -EINVAL
134 */
135int ksmbd_verify_smb_message(struct ksmbd_work *work)
136{
137	struct smb2_hdr *smb2_hdr = ksmbd_req_buf_next(work);
138	struct smb_hdr *hdr;
139
140	if (smb2_hdr->ProtocolId == SMB2_PROTO_NUMBER)
141		return ksmbd_smb2_check_message(work);
142
143	hdr = work->request_buf;
144	if (*(__le32 *)hdr->Protocol == SMB1_PROTO_NUMBER &&
145	    hdr->Command == SMB_COM_NEGOTIATE) {
146		work->conn->outstanding_credits++;
147		return 0;
148	}
149
150	return -EINVAL;
151}
152
153/**
154 * ksmbd_smb_request() - check for valid smb request type
155 * @conn:	connection instance
156 *
157 * Return:      true on success, otherwise false
158 */
159bool ksmbd_smb_request(struct ksmbd_conn *conn)
160{
161	return conn->request_buf[0] == 0;
162}
163
164static bool supported_protocol(int idx)
165{
166	if (idx == SMB2X_PROT &&
167	    (server_conf.min_protocol >= SMB21_PROT ||
168	     server_conf.max_protocol <= SMB311_PROT))
169		return true;
170
171	return (server_conf.min_protocol <= idx &&
172		idx <= server_conf.max_protocol);
173}
174
175static char *next_dialect(char *dialect, int *next_off, int bcount)
176{
177	dialect = dialect + *next_off;
178	*next_off = strnlen(dialect, bcount);
179	if (dialect[*next_off] != '\0')
180		return NULL;
181	return dialect;
182}
183
184static int ksmbd_lookup_dialect_by_name(char *cli_dialects, __le16 byte_count)
185{
186	int i, seq_num, bcount, next;
187	char *dialect;
188
189	for (i = ARRAY_SIZE(smb1_protos) - 1; i >= 0; i--) {
190		seq_num = 0;
191		next = 0;
192		dialect = cli_dialects;
193		bcount = le16_to_cpu(byte_count);
194		do {
195			dialect = next_dialect(dialect, &next, bcount);
196			if (!dialect)
197				break;
198			ksmbd_debug(SMB, "client requested dialect %s\n",
199				    dialect);
200			if (!strcmp(dialect, smb1_protos[i].name)) {
201				if (supported_protocol(smb1_protos[i].index)) {
202					ksmbd_debug(SMB,
203						    "selected %s dialect\n",
204						    smb1_protos[i].name);
205					if (smb1_protos[i].index == SMB1_PROT)
206						return seq_num;
207					return smb1_protos[i].prot_id;
208				}
209			}
210			seq_num++;
211			bcount -= (++next);
212		} while (bcount > 0);
213	}
214
215	return BAD_PROT_ID;
216}
217
218int ksmbd_lookup_dialect_by_id(__le16 *cli_dialects, __le16 dialects_count)
219{
220	int i;
221	int count;
222
223	for (i = ARRAY_SIZE(smb2_protos) - 1; i >= 0; i--) {
224		count = le16_to_cpu(dialects_count);
225		while (--count >= 0) {
226			ksmbd_debug(SMB, "client requested dialect 0x%x\n",
227				    le16_to_cpu(cli_dialects[count]));
228			if (le16_to_cpu(cli_dialects[count]) !=
229					smb2_protos[i].prot_id)
230				continue;
231
232			if (supported_protocol(smb2_protos[i].index)) {
233				ksmbd_debug(SMB, "selected %s dialect\n",
234					    smb2_protos[i].name);
235				return smb2_protos[i].prot_id;
236			}
237		}
238	}
239
240	return BAD_PROT_ID;
241}
242
243static int ksmbd_negotiate_smb_dialect(void *buf)
244{
245	int smb_buf_length = get_rfc1002_len(buf);
246	__le32 proto = ((struct smb2_hdr *)smb2_get_msg(buf))->ProtocolId;
247
248	if (proto == SMB2_PROTO_NUMBER) {
249		struct smb2_negotiate_req *req;
250		int smb2_neg_size =
251			offsetof(struct smb2_negotiate_req, Dialects);
252
253		req = (struct smb2_negotiate_req *)smb2_get_msg(buf);
254		if (smb2_neg_size > smb_buf_length)
255			goto err_out;
256
257		if (smb2_neg_size + le16_to_cpu(req->DialectCount) * sizeof(__le16) >
258		    smb_buf_length)
259			goto err_out;
260
261		return ksmbd_lookup_dialect_by_id(req->Dialects,
262						  req->DialectCount);
263	}
264
265	proto = *(__le32 *)((struct smb_hdr *)buf)->Protocol;
266	if (proto == SMB1_PROTO_NUMBER) {
267		struct smb_negotiate_req *req;
268
269		req = (struct smb_negotiate_req *)buf;
270		if (le16_to_cpu(req->ByteCount) < 2)
271			goto err_out;
272
273		if (offsetof(struct smb_negotiate_req, DialectsArray) - 4 +
274			le16_to_cpu(req->ByteCount) > smb_buf_length) {
275			goto err_out;
276		}
277
278		return ksmbd_lookup_dialect_by_name(req->DialectsArray,
279						    req->ByteCount);
280	}
281
282err_out:
283	return BAD_PROT_ID;
284}
285
286int ksmbd_init_smb_server(struct ksmbd_work *work)
287{
288	struct ksmbd_conn *conn = work->conn;
289
290	if (conn->need_neg == false)
291		return 0;
292
293	init_smb3_11_server(conn);
294
295	if (conn->ops->get_cmd_val(work) != SMB_COM_NEGOTIATE)
296		conn->need_neg = false;
297	return 0;
298}
299
300int ksmbd_populate_dot_dotdot_entries(struct ksmbd_work *work, int info_level,
301				      struct ksmbd_file *dir,
302				      struct ksmbd_dir_info *d_info,
303				      char *search_pattern,
304				      int (*fn)(struct ksmbd_conn *, int,
305						struct ksmbd_dir_info *,
306						struct ksmbd_kstat *))
307{
308	int i, rc = 0;
309	struct ksmbd_conn *conn = work->conn;
310	struct user_namespace *user_ns = file_mnt_user_ns(dir->filp);
311
312	for (i = 0; i < 2; i++) {
313		struct kstat kstat;
314		struct ksmbd_kstat ksmbd_kstat;
315		struct dentry *dentry;
316
317		if (!dir->dot_dotdot[i]) { /* fill dot entry info */
318			if (i == 0) {
319				d_info->name = ".";
320				d_info->name_len = 1;
321				dentry = dir->filp->f_path.dentry;
322			} else {
323				d_info->name = "..";
324				d_info->name_len = 2;
325				dentry = dir->filp->f_path.dentry->d_parent;
326			}
327
328			if (!match_pattern(d_info->name, d_info->name_len,
329					   search_pattern)) {
330				dir->dot_dotdot[i] = 1;
331				continue;
332			}
333
334			ksmbd_kstat.kstat = &kstat;
335			ksmbd_vfs_fill_dentry_attrs(work,
336						    user_ns,
337						    dentry,
338						    &ksmbd_kstat);
339			rc = fn(conn, info_level, d_info, &ksmbd_kstat);
340			if (rc)
341				break;
342			if (d_info->out_buf_len <= 0)
343				break;
344
345			dir->dot_dotdot[i] = 1;
346			if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) {
347				d_info->out_buf_len = 0;
348				break;
349			}
350		}
351	}
352
353	return rc;
354}
355
356/**
357 * ksmbd_extract_shortname() - get shortname from long filename
358 * @conn:	connection instance
359 * @longname:	source long filename
360 * @shortname:	destination short filename
361 *
362 * Return:	shortname length or 0 when source long name is '.' or '..'
363 * TODO: Though this function comforms the restriction of 8.3 Filename spec,
364 * but the result is different with Windows 7's one. need to check.
365 */
366int ksmbd_extract_shortname(struct ksmbd_conn *conn, const char *longname,
367			    char *shortname)
368{
369	const char *p;
370	char base[9], extension[4];
371	char out[13] = {0};
372	int baselen = 0;
373	int extlen = 0, len = 0;
374	unsigned int csum = 0;
375	const unsigned char *ptr;
376	bool dot_present = true;
377
378	p = longname;
379	if ((*p == '.') || (!(strcmp(p, "..")))) {
380		/*no mangling required */
381		return 0;
382	}
383
384	p = strrchr(longname, '.');
385	if (p == longname) { /*name starts with a dot*/
386		strscpy(extension, "___", strlen("___"));
387	} else {
388		if (p) {
389			p++;
390			while (*p && extlen < 3) {
391				if (*p != '.')
392					extension[extlen++] = toupper(*p);
393				p++;
394			}
395			extension[extlen] = '\0';
396		} else {
397			dot_present = false;
398		}
399	}
400
401	p = longname;
402	if (*p == '.') {
403		p++;
404		longname++;
405	}
406	while (*p && (baselen < 5)) {
407		if (*p != '.')
408			base[baselen++] = toupper(*p);
409		p++;
410	}
411
412	base[baselen] = MAGIC_CHAR;
413	memcpy(out, base, baselen + 1);
414
415	ptr = longname;
416	len = strlen(longname);
417	for (; len > 0; len--, ptr++)
418		csum += *ptr;
419
420	csum = csum % (MANGLE_BASE * MANGLE_BASE);
421	out[baselen + 1] = mangle(csum / MANGLE_BASE);
422	out[baselen + 2] = mangle(csum);
423	out[baselen + 3] = PERIOD;
424
425	if (dot_present)
426		memcpy(&out[baselen + 4], extension, 4);
427	else
428		out[baselen + 4] = '\0';
429	smbConvertToUTF16((__le16 *)shortname, out, PATH_MAX,
430			  conn->local_nls, 0);
431	len = strlen(out) * 2;
432	return len;
433}
434
435static int __smb2_negotiate(struct ksmbd_conn *conn)
436{
437	return (conn->dialect >= SMB21_PROT_ID &&
438		conn->dialect <= SMB311_PROT_ID);
439}
440
441static int smb_handle_negotiate(struct ksmbd_work *work)
442{
443	struct smb_negotiate_rsp *neg_rsp = work->response_buf;
444
445	ksmbd_debug(SMB, "Unsupported SMB protocol\n");
446	neg_rsp->hdr.Status.CifsError = STATUS_INVALID_LOGON_TYPE;
447	return -EINVAL;
448}
449
450int ksmbd_smb_negotiate_common(struct ksmbd_work *work, unsigned int command)
451{
452	struct ksmbd_conn *conn = work->conn;
453	int ret;
454
455	conn->dialect =
456		ksmbd_negotiate_smb_dialect(work->request_buf);
457	ksmbd_debug(SMB, "conn->dialect 0x%x\n", conn->dialect);
458
459	if (command == SMB2_NEGOTIATE_HE) {
460		struct smb2_hdr *smb2_hdr = smb2_get_msg(work->request_buf);
461
462		if (smb2_hdr->ProtocolId != SMB2_PROTO_NUMBER) {
463			ksmbd_debug(SMB, "Downgrade to SMB1 negotiation\n");
464			command = SMB_COM_NEGOTIATE;
465		}
466	}
467
468	if (command == SMB2_NEGOTIATE_HE && __smb2_negotiate(conn)) {
469		ret = smb2_handle_negotiate(work);
470		init_smb2_neg_rsp(work);
471		return ret;
472	}
473
474	if (command == SMB_COM_NEGOTIATE) {
475		if (__smb2_negotiate(conn)) {
476			conn->need_neg = true;
477			init_smb3_11_server(conn);
478			init_smb2_neg_rsp(work);
479			ksmbd_debug(SMB, "Upgrade to SMB2 negotiation\n");
480			return 0;
481		}
482		return smb_handle_negotiate(work);
483	}
484
485	pr_err("Unknown SMB negotiation command: %u\n", command);
486	return -EINVAL;
487}
488
489enum SHARED_MODE_ERRORS {
490	SHARE_DELETE_ERROR,
491	SHARE_READ_ERROR,
492	SHARE_WRITE_ERROR,
493	FILE_READ_ERROR,
494	FILE_WRITE_ERROR,
495	FILE_DELETE_ERROR,
496};
497
498static const char * const shared_mode_errors[] = {
499	"Current access mode does not permit SHARE_DELETE",
500	"Current access mode does not permit SHARE_READ",
501	"Current access mode does not permit SHARE_WRITE",
502	"Desired access mode does not permit FILE_READ",
503	"Desired access mode does not permit FILE_WRITE",
504	"Desired access mode does not permit FILE_DELETE",
505};
506
507static void smb_shared_mode_error(int error, struct ksmbd_file *prev_fp,
508				  struct ksmbd_file *curr_fp)
509{
510	ksmbd_debug(SMB, "%s\n", shared_mode_errors[error]);
511	ksmbd_debug(SMB, "Current mode: 0x%x Desired mode: 0x%x\n",
512		    prev_fp->saccess, curr_fp->daccess);
513}
514
515int ksmbd_smb_check_shared_mode(struct file *filp, struct ksmbd_file *curr_fp)
516{
517	int rc = 0;
518	struct ksmbd_file *prev_fp;
519
520	/*
521	 * Lookup fp in master fp list, and check desired access and
522	 * shared mode between previous open and current open.
523	 */
524	read_lock(&curr_fp->f_ci->m_lock);
525	list_for_each_entry(prev_fp, &curr_fp->f_ci->m_fp_list, node) {
526		if (file_inode(filp) != file_inode(prev_fp->filp))
527			continue;
528
529		if (filp == prev_fp->filp)
530			continue;
531
532		if (ksmbd_stream_fd(prev_fp) && ksmbd_stream_fd(curr_fp))
533			if (strcmp(prev_fp->stream.name, curr_fp->stream.name))
534				continue;
535
536		if (prev_fp->attrib_only != curr_fp->attrib_only)
537			continue;
538
539		if (!(prev_fp->saccess & FILE_SHARE_DELETE_LE) &&
540		    curr_fp->daccess & FILE_DELETE_LE) {
541			smb_shared_mode_error(SHARE_DELETE_ERROR,
542					      prev_fp,
543					      curr_fp);
544			rc = -EPERM;
545			break;
546		}
547
548		/*
549		 * Only check FILE_SHARE_DELETE if stream opened and
550		 * normal file opened.
551		 */
552		if (ksmbd_stream_fd(prev_fp) && !ksmbd_stream_fd(curr_fp))
553			continue;
554
555		if (!(prev_fp->saccess & FILE_SHARE_READ_LE) &&
556		    curr_fp->daccess & (FILE_EXECUTE_LE | FILE_READ_DATA_LE)) {
557			smb_shared_mode_error(SHARE_READ_ERROR,
558					      prev_fp,
559					      curr_fp);
560			rc = -EPERM;
561			break;
562		}
563
564		if (!(prev_fp->saccess & FILE_SHARE_WRITE_LE) &&
565		    curr_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE)) {
566			smb_shared_mode_error(SHARE_WRITE_ERROR,
567					      prev_fp,
568					      curr_fp);
569			rc = -EPERM;
570			break;
571		}
572
573		if (prev_fp->daccess & (FILE_EXECUTE_LE | FILE_READ_DATA_LE) &&
574		    !(curr_fp->saccess & FILE_SHARE_READ_LE)) {
575			smb_shared_mode_error(FILE_READ_ERROR,
576					      prev_fp,
577					      curr_fp);
578			rc = -EPERM;
579			break;
580		}
581
582		if (prev_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE) &&
583		    !(curr_fp->saccess & FILE_SHARE_WRITE_LE)) {
584			smb_shared_mode_error(FILE_WRITE_ERROR,
585					      prev_fp,
586					      curr_fp);
587			rc = -EPERM;
588			break;
589		}
590
591		if (prev_fp->daccess & FILE_DELETE_LE &&
592		    !(curr_fp->saccess & FILE_SHARE_DELETE_LE)) {
593			smb_shared_mode_error(FILE_DELETE_ERROR,
594					      prev_fp,
595					      curr_fp);
596			rc = -EPERM;
597			break;
598		}
599	}
600	read_unlock(&curr_fp->f_ci->m_lock);
601
602	return rc;
603}
604
605bool is_asterisk(char *p)
606{
607	return p && p[0] == '*';
608}
609
610int ksmbd_override_fsids(struct ksmbd_work *work)
611{
612	struct ksmbd_session *sess = work->sess;
613	struct ksmbd_share_config *share = work->tcon->share_conf;
614	struct cred *cred;
615	struct group_info *gi;
616	unsigned int uid;
617	unsigned int gid;
618
619	uid = user_uid(sess->user);
620	gid = user_gid(sess->user);
621	if (share->force_uid != KSMBD_SHARE_INVALID_UID)
622		uid = share->force_uid;
623	if (share->force_gid != KSMBD_SHARE_INVALID_GID)
624		gid = share->force_gid;
625
626	cred = prepare_kernel_cred(&init_task);
627	if (!cred)
628		return -ENOMEM;
629
630	cred->fsuid = make_kuid(&init_user_ns, uid);
631	cred->fsgid = make_kgid(&init_user_ns, gid);
632
633	gi = groups_alloc(0);
634	if (!gi) {
635		abort_creds(cred);
636		return -ENOMEM;
637	}
638	set_groups(cred, gi);
639	put_group_info(gi);
640
641	if (!uid_eq(cred->fsuid, GLOBAL_ROOT_UID))
642		cred->cap_effective = cap_drop_fs_set(cred->cap_effective);
643
644	WARN_ON(work->saved_cred);
645	work->saved_cred = override_creds(cred);
646	if (!work->saved_cred) {
647		abort_creds(cred);
648		return -EINVAL;
649	}
650	return 0;
651}
652
653void ksmbd_revert_fsids(struct ksmbd_work *work)
654{
655	const struct cred *cred;
656
657	WARN_ON(!work->saved_cred);
658
659	cred = current_cred();
660	revert_creds(work->saved_cred);
661	put_cred(cred);
662	work->saved_cred = NULL;
663}
664
665__le32 smb_map_generic_desired_access(__le32 daccess)
666{
667	if (daccess & FILE_GENERIC_READ_LE) {
668		daccess |= cpu_to_le32(GENERIC_READ_FLAGS);
669		daccess &= ~FILE_GENERIC_READ_LE;
670	}
671
672	if (daccess & FILE_GENERIC_WRITE_LE) {
673		daccess |= cpu_to_le32(GENERIC_WRITE_FLAGS);
674		daccess &= ~FILE_GENERIC_WRITE_LE;
675	}
676
677	if (daccess & FILE_GENERIC_EXECUTE_LE) {
678		daccess |= cpu_to_le32(GENERIC_EXECUTE_FLAGS);
679		daccess &= ~FILE_GENERIC_EXECUTE_LE;
680	}
681
682	if (daccess & FILE_GENERIC_ALL_LE) {
683		daccess |= cpu_to_le32(GENERIC_ALL_FLAGS);
684		daccess &= ~FILE_GENERIC_ALL_LE;
685	}
686
687	return daccess;
688}