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