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