Vyacheslav Dubeyko | 127e5f5 | 2013-02-27 17:03:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/hfsplus/xattr_trusted.c |
| 3 | * |
| 4 | * Vyacheslav Dubeyko <slava@dubeyko.com> |
| 5 | * |
| 6 | * Handler for storing security labels as extended attributes. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/security.h> |
Hin-Tak Leung | bf29e88 | 2014-06-06 14:36:22 -0700 | [diff] [blame] | 10 | #include <linux/nls.h> |
| 11 | |
Vyacheslav Dubeyko | 127e5f5 | 2013-02-27 17:03:03 -0800 | [diff] [blame] | 12 | #include "hfsplus_fs.h" |
| 13 | #include "xattr.h" |
Vyacheslav Dubeyko | b4c1107 | 2013-09-11 14:24:30 -0700 | [diff] [blame] | 14 | #include "acl.h" |
Vyacheslav Dubeyko | 127e5f5 | 2013-02-27 17:03:03 -0800 | [diff] [blame] | 15 | |
| 16 | static int hfsplus_security_getxattr(struct dentry *dentry, const char *name, |
| 17 | void *buffer, size_t size, int type) |
| 18 | { |
Hin-Tak Leung | bf29e88 | 2014-06-06 14:36:22 -0700 | [diff] [blame] | 19 | char *xattr_name; |
| 20 | int res; |
Vyacheslav Dubeyko | 127e5f5 | 2013-02-27 17:03:03 -0800 | [diff] [blame] | 21 | |
| 22 | if (!strcmp(name, "")) |
| 23 | return -EINVAL; |
| 24 | |
Hin-Tak Leung | bf29e88 | 2014-06-06 14:36:22 -0700 | [diff] [blame] | 25 | xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN + 1, |
| 26 | GFP_KERNEL); |
| 27 | if (!xattr_name) |
| 28 | return -ENOMEM; |
Vyacheslav Dubeyko | 127e5f5 | 2013-02-27 17:03:03 -0800 | [diff] [blame] | 29 | strcpy(xattr_name, XATTR_SECURITY_PREFIX); |
| 30 | strcpy(xattr_name + XATTR_SECURITY_PREFIX_LEN, name); |
| 31 | |
Hin-Tak Leung | bf29e88 | 2014-06-06 14:36:22 -0700 | [diff] [blame] | 32 | res = hfsplus_getxattr(dentry, xattr_name, buffer, size); |
| 33 | kfree(xattr_name); |
| 34 | return res; |
Vyacheslav Dubeyko | 127e5f5 | 2013-02-27 17:03:03 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | static int hfsplus_security_setxattr(struct dentry *dentry, const char *name, |
| 38 | const void *buffer, size_t size, int flags, int type) |
| 39 | { |
Hin-Tak Leung | bf29e88 | 2014-06-06 14:36:22 -0700 | [diff] [blame] | 40 | char *xattr_name; |
| 41 | int res; |
Vyacheslav Dubeyko | 127e5f5 | 2013-02-27 17:03:03 -0800 | [diff] [blame] | 42 | |
| 43 | if (!strcmp(name, "")) |
| 44 | return -EINVAL; |
| 45 | |
Hin-Tak Leung | bf29e88 | 2014-06-06 14:36:22 -0700 | [diff] [blame] | 46 | xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN + 1, |
| 47 | GFP_KERNEL); |
| 48 | if (!xattr_name) |
| 49 | return -ENOMEM; |
Vyacheslav Dubeyko | 127e5f5 | 2013-02-27 17:03:03 -0800 | [diff] [blame] | 50 | strcpy(xattr_name, XATTR_SECURITY_PREFIX); |
| 51 | strcpy(xattr_name + XATTR_SECURITY_PREFIX_LEN, name); |
| 52 | |
Hin-Tak Leung | bf29e88 | 2014-06-06 14:36:22 -0700 | [diff] [blame] | 53 | res = hfsplus_setxattr(dentry, xattr_name, buffer, size, flags); |
| 54 | kfree(xattr_name); |
| 55 | return res; |
Vyacheslav Dubeyko | 127e5f5 | 2013-02-27 17:03:03 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static size_t hfsplus_security_listxattr(struct dentry *dentry, char *list, |
| 59 | size_t list_size, const char *name, size_t name_len, int type) |
| 60 | { |
| 61 | /* |
| 62 | * This method is not used. |
| 63 | * It is used hfsplus_listxattr() instead of generic_listxattr(). |
| 64 | */ |
| 65 | return -EOPNOTSUPP; |
| 66 | } |
| 67 | |
| 68 | static int hfsplus_initxattrs(struct inode *inode, |
| 69 | const struct xattr *xattr_array, |
| 70 | void *fs_info) |
| 71 | { |
| 72 | const struct xattr *xattr; |
Hin-Tak Leung | bf29e88 | 2014-06-06 14:36:22 -0700 | [diff] [blame] | 73 | char *xattr_name; |
Vyacheslav Dubeyko | 127e5f5 | 2013-02-27 17:03:03 -0800 | [diff] [blame] | 74 | int err = 0; |
| 75 | |
Hin-Tak Leung | bf29e88 | 2014-06-06 14:36:22 -0700 | [diff] [blame] | 76 | xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN + 1, |
| 77 | GFP_KERNEL); |
| 78 | if (!xattr_name) |
| 79 | return -ENOMEM; |
Vyacheslav Dubeyko | 127e5f5 | 2013-02-27 17:03:03 -0800 | [diff] [blame] | 80 | for (xattr = xattr_array; xattr->name != NULL; xattr++) { |
Vyacheslav Dubeyko | 127e5f5 | 2013-02-27 17:03:03 -0800 | [diff] [blame] | 81 | |
Hin-Tak Leung | bf29e88 | 2014-06-06 14:36:22 -0700 | [diff] [blame] | 82 | if (!strcmp(xattr->name, "")) |
Vyacheslav Dubeyko | 127e5f5 | 2013-02-27 17:03:03 -0800 | [diff] [blame] | 83 | continue; |
| 84 | |
Vyacheslav Dubeyko | 127e5f5 | 2013-02-27 17:03:03 -0800 | [diff] [blame] | 85 | strcpy(xattr_name, XATTR_SECURITY_PREFIX); |
| 86 | strcpy(xattr_name + |
| 87 | XATTR_SECURITY_PREFIX_LEN, xattr->name); |
| 88 | memset(xattr_name + |
Hin-Tak Leung | bf29e88 | 2014-06-06 14:36:22 -0700 | [diff] [blame] | 89 | XATTR_SECURITY_PREFIX_LEN + strlen(xattr->name), 0, 1); |
Vyacheslav Dubeyko | 127e5f5 | 2013-02-27 17:03:03 -0800 | [diff] [blame] | 90 | |
| 91 | err = __hfsplus_setxattr(inode, xattr_name, |
| 92 | xattr->value, xattr->value_len, 0); |
| 93 | if (err) |
| 94 | break; |
| 95 | } |
Hin-Tak Leung | bf29e88 | 2014-06-06 14:36:22 -0700 | [diff] [blame] | 96 | kfree(xattr_name); |
Vyacheslav Dubeyko | 127e5f5 | 2013-02-27 17:03:03 -0800 | [diff] [blame] | 97 | return err; |
| 98 | } |
| 99 | |
| 100 | int hfsplus_init_security(struct inode *inode, struct inode *dir, |
| 101 | const struct qstr *qstr) |
| 102 | { |
| 103 | return security_inode_init_security(inode, dir, qstr, |
| 104 | &hfsplus_initxattrs, NULL); |
| 105 | } |
| 106 | |
Vyacheslav Dubeyko | b4c1107 | 2013-09-11 14:24:30 -0700 | [diff] [blame] | 107 | int hfsplus_init_inode_security(struct inode *inode, |
| 108 | struct inode *dir, |
| 109 | const struct qstr *qstr) |
| 110 | { |
| 111 | int err; |
| 112 | |
| 113 | err = hfsplus_init_posix_acl(inode, dir); |
| 114 | if (!err) |
| 115 | err = hfsplus_init_security(inode, dir, qstr); |
| 116 | return err; |
| 117 | } |
| 118 | |
Vyacheslav Dubeyko | 127e5f5 | 2013-02-27 17:03:03 -0800 | [diff] [blame] | 119 | const struct xattr_handler hfsplus_xattr_security_handler = { |
| 120 | .prefix = XATTR_SECURITY_PREFIX, |
| 121 | .list = hfsplus_security_listxattr, |
| 122 | .get = hfsplus_security_getxattr, |
| 123 | .set = hfsplus_security_setxattr, |
| 124 | }; |