blob: e1e3f3dd5a06b9e06869381edfb9e633222b1286 [file] [log] [blame]
Thomas Gleixner68252eb2019-05-20 19:08:00 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Phillip Lougherf41d207cb2010-05-17 03:17:04 +01002/*
3 * Squashfs - a compressed read only filesystem for Linux
4 *
5 * Copyright (c) 2010
Phillip Lougherd7f2ff62011-05-26 10:39:56 +01006 * Phillip Lougher <phillip@squashfs.org.uk>
Phillip Lougherf41d207cb2010-05-17 03:17:04 +01007 *
Phillip Lougher46901482010-05-31 18:50:22 +01008 * xattr.c
Phillip Lougherf41d207cb2010-05-17 03:17:04 +01009 */
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 Lougherf6db25a2010-05-23 03:29:26 +010024static const struct xattr_handler *squashfs_xattr_handler(int);
Phillip Lougherf41d207cb2010-05-17 03:17:04 +010025
26ssize_t squashfs_listxattr(struct dentry *d, char *buffer,
27 size_t buffer_size)
28{
David Howells2b0143b2015-03-17 22:25:59 +000029 struct inode *inode = d_inode(d);
Phillip Lougherf41d207cb2010-05-17 03:17:04 +010030 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 Lougherf6db25a2010-05-23 03:29:26 +010047 const struct xattr_handler *handler;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +010048 int name_size;
Phillip Lougherf41d207cb2010-05-17 03:17:04 +010049
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 Gruenbacher764a5c62015-12-02 14:44:43 +010057 if (handler && (!handler->list || handler->list(d))) {
58 const char *prefix = handler->prefix ?: handler->name;
59 size_t prefix_size = strlen(prefix);
60
Phillip Lougherf41d207cb2010-05-17 03:17:04 +010061 if (buffer) {
62 if (prefix_size + name_size + 1 > rest) {
63 err = -ERANGE;
64 goto failed;
65 }
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +010066 memcpy(buffer, prefix, prefix_size);
Phillip Lougherf41d207cb2010-05-17 03:17:04 +010067 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
100failed:
101 return err;
102}
103
104
105static 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 Lougher5c80f5a2010-05-23 08:27:42 +0100136 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 Lougherf41d207cb2010-05-17 03:17:04 +0100142 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 Lougher07724582010-10-24 23:13:57 +0100149 __le64 xattr_val;
150 u64 xattr;
Phillip Lougherf41d207cb2010-05-17 03:17:04 +0100151 /* 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 Lougher07724582010-10-24 23:13:57 +0100156 err = squashfs_read_metadata(sb, &xattr_val,
157 &start, &offset, sizeof(xattr_val));
Phillip Lougherf41d207cb2010-05-17 03:17:04 +0100158 if (err < 0)
159 goto failed;
Phillip Lougher07724582010-10-24 23:13:57 +0100160 xattr = le64_to_cpu(xattr_val);
Phillip Lougherf41d207cb2010-05-17 03:17:04 +0100161 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
197failed:
198 kfree(target);
199 return err;
200}
201
202
Andreas Gruenbacher0ddaf722015-10-04 19:18:53 +0200203static int squashfs_xattr_handler_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -0400204 struct dentry *unused,
205 struct inode *inode,
206 const char *name,
Andreas Gruenbacher0ddaf722015-10-04 19:18:53 +0200207 void *buffer, size_t size)
Phillip Lougherf41d207cb2010-05-17 03:17:04 +0100208{
Al Virob2968212016-04-10 20:48:24 -0400209 return squashfs_xattr_get(inode, handler->flags, name,
Phillip Lougherf41d207cb2010-05-17 03:17:04 +0100210 buffer, size);
211}
212
Andreas Gruenbacher0ddaf722015-10-04 19:18:53 +0200213/*
214 * User namespace support
215 */
Phillip Lougherf6db25a2010-05-23 03:29:26 +0100216static const struct xattr_handler squashfs_xattr_user_handler = {
Phillip Lougherf41d207cb2010-05-17 03:17:04 +0100217 .prefix = XATTR_USER_PREFIX,
Andreas Gruenbacher0ddaf722015-10-04 19:18:53 +0200218 .flags = SQUASHFS_XATTR_USER,
Andreas Gruenbacher0ddaf722015-10-04 19:18:53 +0200219 .get = squashfs_xattr_handler_get
Phillip Lougherf41d207cb2010-05-17 03:17:04 +0100220};
221
222/*
223 * Trusted namespace support
224 */
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100225static bool squashfs_trusted_xattr_handler_list(struct dentry *d)
Phillip Lougherf41d207cb2010-05-17 03:17:04 +0100226{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100227 return capable(CAP_SYS_ADMIN);
Phillip Lougherf41d207cb2010-05-17 03:17:04 +0100228}
229
Phillip Lougherf6db25a2010-05-23 03:29:26 +0100230static const struct xattr_handler squashfs_xattr_trusted_handler = {
Phillip Lougherf41d207cb2010-05-17 03:17:04 +0100231 .prefix = XATTR_TRUSTED_PREFIX,
Andreas Gruenbacher0ddaf722015-10-04 19:18:53 +0200232 .flags = SQUASHFS_XATTR_TRUSTED,
233 .list = squashfs_trusted_xattr_handler_list,
234 .get = squashfs_xattr_handler_get
Phillip Lougherf41d207cb2010-05-17 03:17:04 +0100235};
236
237/*
238 * Security namespace support
239 */
Phillip Lougherf6db25a2010-05-23 03:29:26 +0100240static const struct xattr_handler squashfs_xattr_security_handler = {
Phillip Lougherf41d207cb2010-05-17 03:17:04 +0100241 .prefix = XATTR_SECURITY_PREFIX,
Andreas Gruenbacher0ddaf722015-10-04 19:18:53 +0200242 .flags = SQUASHFS_XATTR_SECURITY,
Andreas Gruenbacher0ddaf722015-10-04 19:18:53 +0200243 .get = squashfs_xattr_handler_get
Phillip Lougherf41d207cb2010-05-17 03:17:04 +0100244};
245
Phillip Loughera02956e2010-05-31 18:32:17 +0100246static const struct xattr_handler *squashfs_xattr_handler(int type)
Phillip Lougherf41d207cb2010-05-17 03:17:04 +0100247{
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 Lougherf6db25a2010-05-23 03:29:26 +0100265const struct xattr_handler *squashfs_xattr_handlers[] = {
Phillip Lougherf41d207cb2010-05-17 03:17:04 +0100266 &squashfs_xattr_user_handler,
267 &squashfs_xattr_trusted_handler,
268 &squashfs_xattr_security_handler,
269 NULL
270};
271