Thomas Gleixner | 68252eb | 2019-05-20 19:08:00 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Phillip Lougher | f41d207cb | 2010-05-17 03:17:04 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Squashfs - a compressed read only filesystem for Linux |
| 4 | * |
| 5 | * Copyright (c) 2010 |
Phillip Lougher | d7f2ff6 | 2011-05-26 10:39:56 +0100 | [diff] [blame] | 6 | * Phillip Lougher <phillip@squashfs.org.uk> |
Phillip Lougher | f41d207cb | 2010-05-17 03:17:04 +0100 | [diff] [blame] | 7 | * |
Phillip Lougher | 4690148 | 2010-05-31 18:50:22 +0100 | [diff] [blame] | 8 | * xattr.c |
Phillip Lougher | f41d207cb | 2010-05-17 03:17:04 +0100 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/string.h> |
| 14 | #include <linux/fs.h> |
| 15 | #include <linux/vfs.h> |
| 16 | #include <linux/xattr.h> |
| 17 | #include <linux/slab.h> |
| 18 | |
| 19 | #include "squashfs_fs.h" |
| 20 | #include "squashfs_fs_sb.h" |
| 21 | #include "squashfs_fs_i.h" |
| 22 | #include "squashfs.h" |
| 23 | |
Phillip Lougher | f6db25a | 2010-05-23 03:29:26 +0100 | [diff] [blame] | 24 | static const struct xattr_handler *squashfs_xattr_handler(int); |
Phillip Lougher | f41d207cb | 2010-05-17 03:17:04 +0100 | [diff] [blame] | 25 | |
| 26 | ssize_t squashfs_listxattr(struct dentry *d, char *buffer, |
| 27 | size_t buffer_size) |
| 28 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 29 | struct inode *inode = d_inode(d); |
Phillip Lougher | f41d207cb | 2010-05-17 03:17:04 +0100 | [diff] [blame] | 30 | struct super_block *sb = inode->i_sb; |
| 31 | struct squashfs_sb_info *msblk = sb->s_fs_info; |
| 32 | u64 start = SQUASHFS_XATTR_BLK(squashfs_i(inode)->xattr) |
| 33 | + msblk->xattr_table; |
| 34 | int offset = SQUASHFS_XATTR_OFFSET(squashfs_i(inode)->xattr); |
| 35 | int count = squashfs_i(inode)->xattr_count; |
| 36 | size_t rest = buffer_size; |
| 37 | int err; |
| 38 | |
| 39 | /* check that the file system has xattrs */ |
| 40 | if (msblk->xattr_id_table == NULL) |
| 41 | return -EOPNOTSUPP; |
| 42 | |
| 43 | /* loop reading each xattr name */ |
| 44 | while (count--) { |
| 45 | struct squashfs_xattr_entry entry; |
| 46 | struct squashfs_xattr_val val; |
Phillip Lougher | f6db25a | 2010-05-23 03:29:26 +0100 | [diff] [blame] | 47 | const struct xattr_handler *handler; |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 48 | int name_size; |
Phillip Lougher | f41d207cb | 2010-05-17 03:17:04 +0100 | [diff] [blame] | 49 | |
| 50 | err = squashfs_read_metadata(sb, &entry, &start, &offset, |
| 51 | sizeof(entry)); |
| 52 | if (err < 0) |
| 53 | goto failed; |
| 54 | |
| 55 | name_size = le16_to_cpu(entry.size); |
| 56 | handler = squashfs_xattr_handler(le16_to_cpu(entry.type)); |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 57 | if (handler && (!handler->list || handler->list(d))) { |
| 58 | const char *prefix = handler->prefix ?: handler->name; |
| 59 | size_t prefix_size = strlen(prefix); |
| 60 | |
Phillip Lougher | f41d207cb | 2010-05-17 03:17:04 +0100 | [diff] [blame] | 61 | if (buffer) { |
| 62 | if (prefix_size + name_size + 1 > rest) { |
| 63 | err = -ERANGE; |
| 64 | goto failed; |
| 65 | } |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 66 | memcpy(buffer, prefix, prefix_size); |
Phillip Lougher | f41d207cb | 2010-05-17 03:17:04 +0100 | [diff] [blame] | 67 | buffer += prefix_size; |
| 68 | } |
| 69 | err = squashfs_read_metadata(sb, buffer, &start, |
| 70 | &offset, name_size); |
| 71 | if (err < 0) |
| 72 | goto failed; |
| 73 | if (buffer) { |
| 74 | buffer[name_size] = '\0'; |
| 75 | buffer += name_size + 1; |
| 76 | } |
| 77 | rest -= prefix_size + name_size + 1; |
| 78 | } else { |
| 79 | /* no handler or insuffficient privileges, so skip */ |
| 80 | err = squashfs_read_metadata(sb, NULL, &start, |
| 81 | &offset, name_size); |
| 82 | if (err < 0) |
| 83 | goto failed; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | /* skip remaining xattr entry */ |
| 88 | err = squashfs_read_metadata(sb, &val, &start, &offset, |
| 89 | sizeof(val)); |
| 90 | if (err < 0) |
| 91 | goto failed; |
| 92 | |
| 93 | err = squashfs_read_metadata(sb, NULL, &start, &offset, |
| 94 | le32_to_cpu(val.vsize)); |
| 95 | if (err < 0) |
| 96 | goto failed; |
| 97 | } |
| 98 | err = buffer_size - rest; |
| 99 | |
| 100 | failed: |
| 101 | return err; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | static int squashfs_xattr_get(struct inode *inode, int name_index, |
| 106 | const char *name, void *buffer, size_t buffer_size) |
| 107 | { |
| 108 | struct super_block *sb = inode->i_sb; |
| 109 | struct squashfs_sb_info *msblk = sb->s_fs_info; |
| 110 | u64 start = SQUASHFS_XATTR_BLK(squashfs_i(inode)->xattr) |
| 111 | + msblk->xattr_table; |
| 112 | int offset = SQUASHFS_XATTR_OFFSET(squashfs_i(inode)->xattr); |
| 113 | int count = squashfs_i(inode)->xattr_count; |
| 114 | int name_len = strlen(name); |
| 115 | int err, vsize; |
| 116 | char *target = kmalloc(name_len, GFP_KERNEL); |
| 117 | |
| 118 | if (target == NULL) |
| 119 | return -ENOMEM; |
| 120 | |
| 121 | /* loop reading each xattr name */ |
| 122 | for (; count; count--) { |
| 123 | struct squashfs_xattr_entry entry; |
| 124 | struct squashfs_xattr_val val; |
| 125 | int type, prefix, name_size; |
| 126 | |
| 127 | err = squashfs_read_metadata(sb, &entry, &start, &offset, |
| 128 | sizeof(entry)); |
| 129 | if (err < 0) |
| 130 | goto failed; |
| 131 | |
| 132 | name_size = le16_to_cpu(entry.size); |
| 133 | type = le16_to_cpu(entry.type); |
| 134 | prefix = type & SQUASHFS_XATTR_PREFIX_MASK; |
| 135 | |
Phillip Lougher | 5c80f5a | 2010-05-23 08:27:42 +0100 | [diff] [blame] | 136 | if (prefix == name_index && name_size == name_len) |
| 137 | err = squashfs_read_metadata(sb, target, &start, |
| 138 | &offset, name_size); |
| 139 | else |
| 140 | err = squashfs_read_metadata(sb, NULL, &start, |
| 141 | &offset, name_size); |
Phillip Lougher | f41d207cb | 2010-05-17 03:17:04 +0100 | [diff] [blame] | 142 | if (err < 0) |
| 143 | goto failed; |
| 144 | |
| 145 | if (prefix == name_index && name_size == name_len && |
| 146 | strncmp(target, name, name_size) == 0) { |
| 147 | /* found xattr */ |
| 148 | if (type & SQUASHFS_XATTR_VALUE_OOL) { |
Phillip Lougher | 0772458 | 2010-10-24 23:13:57 +0100 | [diff] [blame] | 149 | __le64 xattr_val; |
| 150 | u64 xattr; |
Phillip Lougher | f41d207cb | 2010-05-17 03:17:04 +0100 | [diff] [blame] | 151 | /* val is a reference to the real location */ |
| 152 | err = squashfs_read_metadata(sb, &val, &start, |
| 153 | &offset, sizeof(val)); |
| 154 | if (err < 0) |
| 155 | goto failed; |
Phillip Lougher | 0772458 | 2010-10-24 23:13:57 +0100 | [diff] [blame] | 156 | err = squashfs_read_metadata(sb, &xattr_val, |
| 157 | &start, &offset, sizeof(xattr_val)); |
Phillip Lougher | f41d207cb | 2010-05-17 03:17:04 +0100 | [diff] [blame] | 158 | if (err < 0) |
| 159 | goto failed; |
Phillip Lougher | 0772458 | 2010-10-24 23:13:57 +0100 | [diff] [blame] | 160 | xattr = le64_to_cpu(xattr_val); |
Phillip Lougher | f41d207cb | 2010-05-17 03:17:04 +0100 | [diff] [blame] | 161 | start = SQUASHFS_XATTR_BLK(xattr) + |
| 162 | msblk->xattr_table; |
| 163 | offset = SQUASHFS_XATTR_OFFSET(xattr); |
| 164 | } |
| 165 | /* read xattr value */ |
| 166 | err = squashfs_read_metadata(sb, &val, &start, &offset, |
| 167 | sizeof(val)); |
| 168 | if (err < 0) |
| 169 | goto failed; |
| 170 | |
| 171 | vsize = le32_to_cpu(val.vsize); |
| 172 | if (buffer) { |
| 173 | if (vsize > buffer_size) { |
| 174 | err = -ERANGE; |
| 175 | goto failed; |
| 176 | } |
| 177 | err = squashfs_read_metadata(sb, buffer, &start, |
| 178 | &offset, vsize); |
| 179 | if (err < 0) |
| 180 | goto failed; |
| 181 | } |
| 182 | break; |
| 183 | } |
| 184 | |
| 185 | /* no match, skip remaining xattr entry */ |
| 186 | err = squashfs_read_metadata(sb, &val, &start, &offset, |
| 187 | sizeof(val)); |
| 188 | if (err < 0) |
| 189 | goto failed; |
| 190 | err = squashfs_read_metadata(sb, NULL, &start, &offset, |
| 191 | le32_to_cpu(val.vsize)); |
| 192 | if (err < 0) |
| 193 | goto failed; |
| 194 | } |
| 195 | err = count ? vsize : -ENODATA; |
| 196 | |
| 197 | failed: |
| 198 | kfree(target); |
| 199 | return err; |
| 200 | } |
| 201 | |
| 202 | |
Andreas Gruenbacher | 0ddaf72 | 2015-10-04 19:18:53 +0200 | [diff] [blame] | 203 | static int squashfs_xattr_handler_get(const struct xattr_handler *handler, |
Al Viro | b296821 | 2016-04-10 20:48:24 -0400 | [diff] [blame] | 204 | struct dentry *unused, |
| 205 | struct inode *inode, |
| 206 | const char *name, |
Andreas Gruenbacher | 0ddaf72 | 2015-10-04 19:18:53 +0200 | [diff] [blame] | 207 | void *buffer, size_t size) |
Phillip Lougher | f41d207cb | 2010-05-17 03:17:04 +0100 | [diff] [blame] | 208 | { |
Al Viro | b296821 | 2016-04-10 20:48:24 -0400 | [diff] [blame] | 209 | return squashfs_xattr_get(inode, handler->flags, name, |
Phillip Lougher | f41d207cb | 2010-05-17 03:17:04 +0100 | [diff] [blame] | 210 | buffer, size); |
| 211 | } |
| 212 | |
Andreas Gruenbacher | 0ddaf72 | 2015-10-04 19:18:53 +0200 | [diff] [blame] | 213 | /* |
| 214 | * User namespace support |
| 215 | */ |
Phillip Lougher | f6db25a | 2010-05-23 03:29:26 +0100 | [diff] [blame] | 216 | static const struct xattr_handler squashfs_xattr_user_handler = { |
Phillip Lougher | f41d207cb | 2010-05-17 03:17:04 +0100 | [diff] [blame] | 217 | .prefix = XATTR_USER_PREFIX, |
Andreas Gruenbacher | 0ddaf72 | 2015-10-04 19:18:53 +0200 | [diff] [blame] | 218 | .flags = SQUASHFS_XATTR_USER, |
Andreas Gruenbacher | 0ddaf72 | 2015-10-04 19:18:53 +0200 | [diff] [blame] | 219 | .get = squashfs_xattr_handler_get |
Phillip Lougher | f41d207cb | 2010-05-17 03:17:04 +0100 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | /* |
| 223 | * Trusted namespace support |
| 224 | */ |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 225 | static bool squashfs_trusted_xattr_handler_list(struct dentry *d) |
Phillip Lougher | f41d207cb | 2010-05-17 03:17:04 +0100 | [diff] [blame] | 226 | { |
Andreas Gruenbacher | 764a5c6 | 2015-12-02 14:44:43 +0100 | [diff] [blame] | 227 | return capable(CAP_SYS_ADMIN); |
Phillip Lougher | f41d207cb | 2010-05-17 03:17:04 +0100 | [diff] [blame] | 228 | } |
| 229 | |
Phillip Lougher | f6db25a | 2010-05-23 03:29:26 +0100 | [diff] [blame] | 230 | static const struct xattr_handler squashfs_xattr_trusted_handler = { |
Phillip Lougher | f41d207cb | 2010-05-17 03:17:04 +0100 | [diff] [blame] | 231 | .prefix = XATTR_TRUSTED_PREFIX, |
Andreas Gruenbacher | 0ddaf72 | 2015-10-04 19:18:53 +0200 | [diff] [blame] | 232 | .flags = SQUASHFS_XATTR_TRUSTED, |
| 233 | .list = squashfs_trusted_xattr_handler_list, |
| 234 | .get = squashfs_xattr_handler_get |
Phillip Lougher | f41d207cb | 2010-05-17 03:17:04 +0100 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | /* |
| 238 | * Security namespace support |
| 239 | */ |
Phillip Lougher | f6db25a | 2010-05-23 03:29:26 +0100 | [diff] [blame] | 240 | static const struct xattr_handler squashfs_xattr_security_handler = { |
Phillip Lougher | f41d207cb | 2010-05-17 03:17:04 +0100 | [diff] [blame] | 241 | .prefix = XATTR_SECURITY_PREFIX, |
Andreas Gruenbacher | 0ddaf72 | 2015-10-04 19:18:53 +0200 | [diff] [blame] | 242 | .flags = SQUASHFS_XATTR_SECURITY, |
Andreas Gruenbacher | 0ddaf72 | 2015-10-04 19:18:53 +0200 | [diff] [blame] | 243 | .get = squashfs_xattr_handler_get |
Phillip Lougher | f41d207cb | 2010-05-17 03:17:04 +0100 | [diff] [blame] | 244 | }; |
| 245 | |
Phillip Lougher | a02956e | 2010-05-31 18:32:17 +0100 | [diff] [blame] | 246 | static const struct xattr_handler *squashfs_xattr_handler(int type) |
Phillip Lougher | f41d207cb | 2010-05-17 03:17:04 +0100 | [diff] [blame] | 247 | { |
| 248 | if (type & ~(SQUASHFS_XATTR_PREFIX_MASK | SQUASHFS_XATTR_VALUE_OOL)) |
| 249 | /* ignore unrecognised type */ |
| 250 | return NULL; |
| 251 | |
| 252 | switch (type & SQUASHFS_XATTR_PREFIX_MASK) { |
| 253 | case SQUASHFS_XATTR_USER: |
| 254 | return &squashfs_xattr_user_handler; |
| 255 | case SQUASHFS_XATTR_TRUSTED: |
| 256 | return &squashfs_xattr_trusted_handler; |
| 257 | case SQUASHFS_XATTR_SECURITY: |
| 258 | return &squashfs_xattr_security_handler; |
| 259 | default: |
| 260 | /* ignore unrecognised type */ |
| 261 | return NULL; |
| 262 | } |
| 263 | } |
| 264 | |
Phillip Lougher | f6db25a | 2010-05-23 03:29:26 +0100 | [diff] [blame] | 265 | const struct xattr_handler *squashfs_xattr_handlers[] = { |
Phillip Lougher | f41d207cb | 2010-05-17 03:17:04 +0100 | [diff] [blame] | 266 | &squashfs_xattr_user_handler, |
| 267 | &squashfs_xattr_trusted_handler, |
| 268 | &squashfs_xattr_security_handler, |
| 269 | NULL |
| 270 | }; |
| 271 | |