1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3  *
4  *   Copyright (c) International Business Machines  Corp., 2003, 2007
5  *   Author(s): Steve French (sfrench@us.ibm.com)
6  *
7  */
8 
9 #include <linux/fs.h>
10 #include <linux/posix_acl_xattr.h>
11 #include <linux/slab.h>
12 #include <linux/xattr.h>
13 #include "cifsfs.h"
14 #include "cifspdu.h"
15 #include "cifsglob.h"
16 #include "cifsproto.h"
17 #include "cifs_debug.h"
18 #include "cifs_fs_sb.h"
19 #include "cifs_unicode.h"
20 #include "cifs_ioctl.h"
21 
22 #define MAX_EA_VALUE_SIZE CIFSMaxBufSize
23 #define CIFS_XATTR_CIFS_ACL "system.cifs_acl" /* DACL only */
24 #define CIFS_XATTR_CIFS_NTSD "system.cifs_ntsd" /* owner plus DACL */
25 #define CIFS_XATTR_CIFS_NTSD_FULL "system.cifs_ntsd_full" /* owner/DACL/SACL */
26 #define CIFS_XATTR_ATTRIB "cifs.dosattrib"  /* full name: user.cifs.dosattrib */
27 #define CIFS_XATTR_CREATETIME "cifs.creationtime"  /* user.cifs.creationtime */
28 /*
29  * Although these three are just aliases for the above, need to move away from
30  * confusing users and using the 20+ year old term 'cifs' when it is no longer
31  * secure, replaced by SMB2 (then even more highly secure SMB3) many years ago
32  */
33 #define SMB3_XATTR_CIFS_ACL "system.smb3_acl" /* DACL only */
34 #define SMB3_XATTR_CIFS_NTSD "system.smb3_ntsd" /* owner plus DACL */
35 #define SMB3_XATTR_CIFS_NTSD_FULL "system.smb3_ntsd_full" /* owner/DACL/SACL */
36 #define SMB3_XATTR_ATTRIB "smb3.dosattrib"  /* full name: user.smb3.dosattrib */
37 #define SMB3_XATTR_CREATETIME "smb3.creationtime"  /* user.smb3.creationtime */
38 /* BB need to add server (Samba e.g) support for security and trusted prefix */
39 
40 enum { XATTR_USER, XATTR_CIFS_ACL, XATTR_ACL_ACCESS, XATTR_ACL_DEFAULT,
41 	XATTR_CIFS_NTSD, XATTR_CIFS_NTSD_FULL };
42 
cifs_attrib_set(unsigned int xid,struct cifs_tcon * pTcon,struct inode * inode,const char * full_path,const void * value,size_t size)43 static int cifs_attrib_set(unsigned int xid, struct cifs_tcon *pTcon,
44 			   struct inode *inode, const char *full_path,
45 			   const void *value, size_t size)
46 {
47 	ssize_t rc = -EOPNOTSUPP;
48 	__u32 *pattrib = (__u32 *)value;
49 	__u32 attrib;
50 	FILE_BASIC_INFO info_buf;
51 
52 	if ((value == NULL) || (size != sizeof(__u32)))
53 		return -ERANGE;
54 
55 	memset(&info_buf, 0, sizeof(info_buf));
56 	attrib = *pattrib;
57 	info_buf.Attributes = cpu_to_le32(attrib);
58 	if (pTcon->ses->server->ops->set_file_info)
59 		rc = pTcon->ses->server->ops->set_file_info(inode, full_path,
60 				&info_buf, xid);
61 	if (rc == 0)
62 		CIFS_I(inode)->cifsAttrs = attrib;
63 
64 	return rc;
65 }
66 
cifs_creation_time_set(unsigned int xid,struct cifs_tcon * pTcon,struct inode * inode,const char * full_path,const void * value,size_t size)67 static int cifs_creation_time_set(unsigned int xid, struct cifs_tcon *pTcon,
68 				  struct inode *inode, const char *full_path,
69 				  const void *value, size_t size)
70 {
71 	ssize_t rc = -EOPNOTSUPP;
72 	__u64 *pcreation_time = (__u64 *)value;
73 	__u64 creation_time;
74 	FILE_BASIC_INFO info_buf;
75 
76 	if ((value == NULL) || (size != sizeof(__u64)))
77 		return -ERANGE;
78 
79 	memset(&info_buf, 0, sizeof(info_buf));
80 	creation_time = *pcreation_time;
81 	info_buf.CreationTime = cpu_to_le64(creation_time);
82 	if (pTcon->ses->server->ops->set_file_info)
83 		rc = pTcon->ses->server->ops->set_file_info(inode, full_path,
84 				&info_buf, xid);
85 	if (rc == 0)
86 		CIFS_I(inode)->createtime = creation_time;
87 
88 	return rc;
89 }
90 
cifs_xattr_set(const struct xattr_handler * handler,struct user_namespace * mnt_userns,struct dentry * dentry,struct inode * inode,const char * name,const void * value,size_t size,int flags)91 static int cifs_xattr_set(const struct xattr_handler *handler,
92 			  struct user_namespace *mnt_userns,
93 			  struct dentry *dentry, struct inode *inode,
94 			  const char *name, const void *value,
95 			  size_t size, int flags)
96 {
97 	int rc = -EOPNOTSUPP;
98 	unsigned int xid;
99 	struct super_block *sb = dentry->d_sb;
100 	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
101 	struct tcon_link *tlink;
102 	struct cifs_tcon *pTcon;
103 	const char *full_path;
104 	void *page;
105 
106 	tlink = cifs_sb_tlink(cifs_sb);
107 	if (IS_ERR(tlink))
108 		return PTR_ERR(tlink);
109 	pTcon = tlink_tcon(tlink);
110 
111 	xid = get_xid();
112 	page = alloc_dentry_path();
113 
114 	full_path = build_path_from_dentry(dentry, page);
115 	if (IS_ERR(full_path)) {
116 		rc = PTR_ERR(full_path);
117 		goto out;
118 	}
119 	/* return dos attributes as pseudo xattr */
120 	/* return alt name if available as pseudo attr */
121 
122 	/* if proc/fs/cifs/streamstoxattr is set then
123 		search server for EAs or streams to
124 		returns as xattrs */
125 	if (size > MAX_EA_VALUE_SIZE) {
126 		cifs_dbg(FYI, "size of EA value too large\n");
127 		rc = -EOPNOTSUPP;
128 		goto out;
129 	}
130 
131 	switch (handler->flags) {
132 	case XATTR_USER:
133 		cifs_dbg(FYI, "%s:setting user xattr %s\n", __func__, name);
134 		if ((strcmp(name, CIFS_XATTR_ATTRIB) == 0) ||
135 		    (strcmp(name, SMB3_XATTR_ATTRIB) == 0)) {
136 			rc = cifs_attrib_set(xid, pTcon, inode, full_path,
137 					value, size);
138 			if (rc == 0) /* force revalidate of the inode */
139 				CIFS_I(inode)->time = 0;
140 			break;
141 		} else if ((strcmp(name, CIFS_XATTR_CREATETIME) == 0) ||
142 			   (strcmp(name, SMB3_XATTR_CREATETIME) == 0)) {
143 			rc = cifs_creation_time_set(xid, pTcon, inode,
144 					full_path, value, size);
145 			if (rc == 0) /* force revalidate of the inode */
146 				CIFS_I(inode)->time = 0;
147 			break;
148 		}
149 
150 		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
151 			goto out;
152 
153 		if (pTcon->ses->server->ops->set_EA)
154 			rc = pTcon->ses->server->ops->set_EA(xid, pTcon,
155 				full_path, name, value, (__u16)size,
156 				cifs_sb->local_nls, cifs_sb);
157 		break;
158 
159 	case XATTR_CIFS_ACL:
160 	case XATTR_CIFS_NTSD:
161 	case XATTR_CIFS_NTSD_FULL: {
162 		struct cifs_ntsd *pacl;
163 
164 		if (!value)
165 			goto out;
166 		pacl = kmalloc(size, GFP_KERNEL);
167 		if (!pacl) {
168 			rc = -ENOMEM;
169 		} else {
170 			memcpy(pacl, value, size);
171 			if (pTcon->ses->server->ops->set_acl) {
172 				int aclflags = 0;
173 				rc = 0;
174 
175 				switch (handler->flags) {
176 				case XATTR_CIFS_NTSD_FULL:
177 					aclflags = (CIFS_ACL_OWNER |
178 						    CIFS_ACL_GROUP |
179 						    CIFS_ACL_DACL |
180 						    CIFS_ACL_SACL);
181 					break;
182 				case XATTR_CIFS_NTSD:
183 					aclflags = (CIFS_ACL_OWNER |
184 						    CIFS_ACL_GROUP |
185 						    CIFS_ACL_DACL);
186 					break;
187 				case XATTR_CIFS_ACL:
188 				default:
189 					aclflags = CIFS_ACL_DACL;
190 				}
191 
192 				rc = pTcon->ses->server->ops->set_acl(pacl,
193 					size, inode, full_path, aclflags);
194 			} else {
195 				rc = -EOPNOTSUPP;
196 			}
197 			if (rc == 0) /* force revalidate of the inode */
198 				CIFS_I(inode)->time = 0;
199 			kfree(pacl);
200 		}
201 		break;
202 	}
203 
204 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
205 	case XATTR_ACL_ACCESS:
206 #ifdef CONFIG_CIFS_POSIX
207 		if (!value)
208 			goto out;
209 		if (sb->s_flags & SB_POSIXACL)
210 			rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
211 				value, (const int)size,
212 				ACL_TYPE_ACCESS, cifs_sb->local_nls,
213 				cifs_remap(cifs_sb));
214 #endif  /* CONFIG_CIFS_POSIX */
215 		break;
216 
217 	case XATTR_ACL_DEFAULT:
218 #ifdef CONFIG_CIFS_POSIX
219 		if (!value)
220 			goto out;
221 		if (sb->s_flags & SB_POSIXACL)
222 			rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
223 				value, (const int)size,
224 				ACL_TYPE_DEFAULT, cifs_sb->local_nls,
225 				cifs_remap(cifs_sb));
226 #endif  /* CONFIG_CIFS_POSIX */
227 		break;
228 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
229 	}
230 
231 out:
232 	free_dentry_path(page);
233 	free_xid(xid);
234 	cifs_put_tlink(tlink);
235 	return rc;
236 }
237 
cifs_attrib_get(struct dentry * dentry,struct inode * inode,void * value,size_t size)238 static int cifs_attrib_get(struct dentry *dentry,
239 			   struct inode *inode, void *value,
240 			   size_t size)
241 {
242 	ssize_t rc;
243 	__u32 *pattribute;
244 
245 	rc = cifs_revalidate_dentry_attr(dentry);
246 
247 	if (rc)
248 		return rc;
249 
250 	if ((value == NULL) || (size == 0))
251 		return sizeof(__u32);
252 	else if (size < sizeof(__u32))
253 		return -ERANGE;
254 
255 	/* return dos attributes as pseudo xattr */
256 	pattribute = (__u32 *)value;
257 	*pattribute = CIFS_I(inode)->cifsAttrs;
258 
259 	return sizeof(__u32);
260 }
261 
cifs_creation_time_get(struct dentry * dentry,struct inode * inode,void * value,size_t size)262 static int cifs_creation_time_get(struct dentry *dentry, struct inode *inode,
263 				  void *value, size_t size)
264 {
265 	ssize_t rc;
266 	__u64 *pcreatetime;
267 
268 	rc = cifs_revalidate_dentry_attr(dentry);
269 	if (rc)
270 		return rc;
271 
272 	if ((value == NULL) || (size == 0))
273 		return sizeof(__u64);
274 	else if (size < sizeof(__u64))
275 		return -ERANGE;
276 
277 	/* return dos attributes as pseudo xattr */
278 	pcreatetime = (__u64 *)value;
279 	*pcreatetime = CIFS_I(inode)->createtime;
280 	return sizeof(__u64);
281 }
282 
283 
cifs_xattr_get(const struct xattr_handler * handler,struct dentry * dentry,struct inode * inode,const char * name,void * value,size_t size)284 static int cifs_xattr_get(const struct xattr_handler *handler,
285 			  struct dentry *dentry, struct inode *inode,
286 			  const char *name, void *value, size_t size)
287 {
288 	ssize_t rc = -EOPNOTSUPP;
289 	unsigned int xid;
290 	struct super_block *sb = dentry->d_sb;
291 	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
292 	struct tcon_link *tlink;
293 	struct cifs_tcon *pTcon;
294 	const char *full_path;
295 	void *page;
296 
297 	tlink = cifs_sb_tlink(cifs_sb);
298 	if (IS_ERR(tlink))
299 		return PTR_ERR(tlink);
300 	pTcon = tlink_tcon(tlink);
301 
302 	xid = get_xid();
303 	page = alloc_dentry_path();
304 
305 	full_path = build_path_from_dentry(dentry, page);
306 	if (IS_ERR(full_path)) {
307 		rc = PTR_ERR(full_path);
308 		goto out;
309 	}
310 
311 	/* return alt name if available as pseudo attr */
312 	switch (handler->flags) {
313 	case XATTR_USER:
314 		cifs_dbg(FYI, "%s:querying user xattr %s\n", __func__, name);
315 		if ((strcmp(name, CIFS_XATTR_ATTRIB) == 0) ||
316 		    (strcmp(name, SMB3_XATTR_ATTRIB) == 0)) {
317 			rc = cifs_attrib_get(dentry, inode, value, size);
318 			break;
319 		} else if ((strcmp(name, CIFS_XATTR_CREATETIME) == 0) ||
320 		    (strcmp(name, SMB3_XATTR_CREATETIME) == 0)) {
321 			rc = cifs_creation_time_get(dentry, inode, value, size);
322 			break;
323 		}
324 
325 		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
326 			goto out;
327 
328 		if (pTcon->ses->server->ops->query_all_EAs)
329 			rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
330 				full_path, name, value, size, cifs_sb);
331 		break;
332 
333 	case XATTR_CIFS_ACL:
334 	case XATTR_CIFS_NTSD:
335 	case XATTR_CIFS_NTSD_FULL: {
336 		/*
337 		 * fetch owner, DACL, and SACL if asked for full descriptor,
338 		 * fetch owner and DACL otherwise
339 		 */
340 		u32 acllen, extra_info;
341 		struct cifs_ntsd *pacl;
342 
343 		if (pTcon->ses->server->ops->get_acl == NULL)
344 			goto out; /* rc already EOPNOTSUPP */
345 
346 		if (handler->flags == XATTR_CIFS_NTSD_FULL) {
347 			extra_info = SACL_SECINFO;
348 		} else {
349 			extra_info = 0;
350 		}
351 		pacl = pTcon->ses->server->ops->get_acl(cifs_sb,
352 				inode, full_path, &acllen, extra_info);
353 		if (IS_ERR(pacl)) {
354 			rc = PTR_ERR(pacl);
355 			cifs_dbg(VFS, "%s: error %zd getting sec desc\n",
356 				 __func__, rc);
357 		} else {
358 			if (value) {
359 				if (acllen > size)
360 					acllen = -ERANGE;
361 				else
362 					memcpy(value, pacl, acllen);
363 			}
364 			rc = acllen;
365 			kfree(pacl);
366 		}
367 		break;
368 	}
369 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
370 	case XATTR_ACL_ACCESS:
371 #ifdef CONFIG_CIFS_POSIX
372 		if (sb->s_flags & SB_POSIXACL)
373 			rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
374 				value, size, ACL_TYPE_ACCESS,
375 				cifs_sb->local_nls,
376 				cifs_remap(cifs_sb));
377 #endif  /* CONFIG_CIFS_POSIX */
378 		break;
379 
380 	case XATTR_ACL_DEFAULT:
381 #ifdef CONFIG_CIFS_POSIX
382 		if (sb->s_flags & SB_POSIXACL)
383 			rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
384 				value, size, ACL_TYPE_DEFAULT,
385 				cifs_sb->local_nls,
386 				cifs_remap(cifs_sb));
387 #endif  /* CONFIG_CIFS_POSIX */
388 		break;
389 #endif /* ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
390 	}
391 
392 	/* We could add an additional check for streams ie
393 	    if proc/fs/cifs/streamstoxattr is set then
394 		search server for EAs or streams to
395 		returns as xattrs */
396 
397 	if (rc == -EINVAL)
398 		rc = -EOPNOTSUPP;
399 
400 out:
401 	free_dentry_path(page);
402 	free_xid(xid);
403 	cifs_put_tlink(tlink);
404 	return rc;
405 }
406 
cifs_listxattr(struct dentry * direntry,char * data,size_t buf_size)407 ssize_t cifs_listxattr(struct dentry *direntry, char *data, size_t buf_size)
408 {
409 	ssize_t rc = -EOPNOTSUPP;
410 	unsigned int xid;
411 	struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
412 	struct tcon_link *tlink;
413 	struct cifs_tcon *pTcon;
414 	const char *full_path;
415 	void *page;
416 
417 	if (unlikely(cifs_forced_shutdown(cifs_sb)))
418 		return -EIO;
419 
420 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
421 		return -EOPNOTSUPP;
422 
423 	tlink = cifs_sb_tlink(cifs_sb);
424 	if (IS_ERR(tlink))
425 		return PTR_ERR(tlink);
426 	pTcon = tlink_tcon(tlink);
427 
428 	xid = get_xid();
429 	page = alloc_dentry_path();
430 
431 	full_path = build_path_from_dentry(direntry, page);
432 	if (IS_ERR(full_path)) {
433 		rc = PTR_ERR(full_path);
434 		goto list_ea_exit;
435 	}
436 	/* return dos attributes as pseudo xattr */
437 	/* return alt name if available as pseudo attr */
438 
439 	/* if proc/fs/cifs/streamstoxattr is set then
440 		search server for EAs or streams to
441 		returns as xattrs */
442 
443 	if (pTcon->ses->server->ops->query_all_EAs)
444 		rc = pTcon->ses->server->ops->query_all_EAs(xid, pTcon,
445 				full_path, NULL, data, buf_size, cifs_sb);
446 list_ea_exit:
447 	free_dentry_path(page);
448 	free_xid(xid);
449 	cifs_put_tlink(tlink);
450 	return rc;
451 }
452 
453 static const struct xattr_handler cifs_user_xattr_handler = {
454 	.prefix = XATTR_USER_PREFIX,
455 	.flags = XATTR_USER,
456 	.get = cifs_xattr_get,
457 	.set = cifs_xattr_set,
458 };
459 
460 /* os2.* attributes are treated like user.* attributes */
461 static const struct xattr_handler cifs_os2_xattr_handler = {
462 	.prefix = XATTR_OS2_PREFIX,
463 	.flags = XATTR_USER,
464 	.get = cifs_xattr_get,
465 	.set = cifs_xattr_set,
466 };
467 
468 static const struct xattr_handler cifs_cifs_acl_xattr_handler = {
469 	.name = CIFS_XATTR_CIFS_ACL,
470 	.flags = XATTR_CIFS_ACL,
471 	.get = cifs_xattr_get,
472 	.set = cifs_xattr_set,
473 };
474 
475 /*
476  * Although this is just an alias for the above, need to move away from
477  * confusing users and using the 20 year old term 'cifs' when it is no
478  * longer secure and was replaced by SMB2/SMB3 a long time ago, and
479  * SMB3 and later are highly secure.
480  */
481 static const struct xattr_handler smb3_acl_xattr_handler = {
482 	.name = SMB3_XATTR_CIFS_ACL,
483 	.flags = XATTR_CIFS_ACL,
484 	.get = cifs_xattr_get,
485 	.set = cifs_xattr_set,
486 };
487 
488 static const struct xattr_handler cifs_cifs_ntsd_xattr_handler = {
489 	.name = CIFS_XATTR_CIFS_NTSD,
490 	.flags = XATTR_CIFS_NTSD,
491 	.get = cifs_xattr_get,
492 	.set = cifs_xattr_set,
493 };
494 
495 /*
496  * Although this is just an alias for the above, need to move away from
497  * confusing users and using the 20 year old term 'cifs' when it is no
498  * longer secure and was replaced by SMB2/SMB3 a long time ago, and
499  * SMB3 and later are highly secure.
500  */
501 static const struct xattr_handler smb3_ntsd_xattr_handler = {
502 	.name = SMB3_XATTR_CIFS_NTSD,
503 	.flags = XATTR_CIFS_NTSD,
504 	.get = cifs_xattr_get,
505 	.set = cifs_xattr_set,
506 };
507 
508 static const struct xattr_handler cifs_cifs_ntsd_full_xattr_handler = {
509 	.name = CIFS_XATTR_CIFS_NTSD_FULL,
510 	.flags = XATTR_CIFS_NTSD_FULL,
511 	.get = cifs_xattr_get,
512 	.set = cifs_xattr_set,
513 };
514 
515 /*
516  * Although this is just an alias for the above, need to move away from
517  * confusing users and using the 20 year old term 'cifs' when it is no
518  * longer secure and was replaced by SMB2/SMB3 a long time ago, and
519  * SMB3 and later are highly secure.
520  */
521 static const struct xattr_handler smb3_ntsd_full_xattr_handler = {
522 	.name = SMB3_XATTR_CIFS_NTSD_FULL,
523 	.flags = XATTR_CIFS_NTSD_FULL,
524 	.get = cifs_xattr_get,
525 	.set = cifs_xattr_set,
526 };
527 
528 
529 static const struct xattr_handler cifs_posix_acl_access_xattr_handler = {
530 	.name = XATTR_NAME_POSIX_ACL_ACCESS,
531 	.flags = XATTR_ACL_ACCESS,
532 	.get = cifs_xattr_get,
533 	.set = cifs_xattr_set,
534 };
535 
536 static const struct xattr_handler cifs_posix_acl_default_xattr_handler = {
537 	.name = XATTR_NAME_POSIX_ACL_DEFAULT,
538 	.flags = XATTR_ACL_DEFAULT,
539 	.get = cifs_xattr_get,
540 	.set = cifs_xattr_set,
541 };
542 
543 const struct xattr_handler *cifs_xattr_handlers[] = {
544 	&cifs_user_xattr_handler,
545 	&cifs_os2_xattr_handler,
546 	&cifs_cifs_acl_xattr_handler,
547 	&smb3_acl_xattr_handler, /* alias for above since avoiding "cifs" */
548 	&cifs_cifs_ntsd_xattr_handler,
549 	&smb3_ntsd_xattr_handler, /* alias for above since avoiding "cifs" */
550 	&cifs_cifs_ntsd_full_xattr_handler,
551 	&smb3_ntsd_full_xattr_handler, /* alias for above since avoiding "cifs" */
552 	&cifs_posix_acl_access_xattr_handler,
553 	&cifs_posix_acl_default_xattr_handler,
554 	NULL
555 };
556