Merge tag 'efi-next-for-v6.7' of git://git.kernel.org/pub/scm/linux/kernel/git/efi/efi
[linux-2.6-microblaze.git] / fs / efivarfs / super.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 Red Hat, Inc.
4  * Copyright (C) 2012 Jeremy Kerr <jeremy.kerr@canonical.com>
5  */
6
7 #include <linux/ctype.h>
8 #include <linux/efi.h>
9 #include <linux/fs.h>
10 #include <linux/fs_context.h>
11 #include <linux/fs_parser.h>
12 #include <linux/module.h>
13 #include <linux/pagemap.h>
14 #include <linux/ucs2_string.h>
15 #include <linux/slab.h>
16 #include <linux/magic.h>
17 #include <linux/statfs.h>
18
19 #include "internal.h"
20
21 LIST_HEAD(efivarfs_list);
22
23 static void efivarfs_evict_inode(struct inode *inode)
24 {
25         clear_inode(inode);
26 }
27
28 static int efivarfs_show_options(struct seq_file *m, struct dentry *root)
29 {
30         struct super_block *sb = root->d_sb;
31         struct efivarfs_fs_info *sbi = sb->s_fs_info;
32         struct efivarfs_mount_opts *opts = &sbi->mount_opts;
33
34         if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
35                 seq_printf(m, ",uid=%u",
36                                 from_kuid_munged(&init_user_ns, opts->uid));
37         if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
38                 seq_printf(m, ",gid=%u",
39                                 from_kgid_munged(&init_user_ns, opts->gid));
40         return 0;
41 }
42
43 static int efivarfs_statfs(struct dentry *dentry, struct kstatfs *buf)
44 {
45         const u32 attr = EFI_VARIABLE_NON_VOLATILE |
46                          EFI_VARIABLE_BOOTSERVICE_ACCESS |
47                          EFI_VARIABLE_RUNTIME_ACCESS;
48         u64 storage_space, remaining_space, max_variable_size;
49         efi_status_t status;
50
51         /* Some UEFI firmware does not implement QueryVariableInfo() */
52         storage_space = remaining_space = 0;
53         if (efi_rt_services_supported(EFI_RT_SUPPORTED_QUERY_VARIABLE_INFO)) {
54                 status = efivar_query_variable_info(attr, &storage_space,
55                                                     &remaining_space,
56                                                     &max_variable_size);
57                 if (status != EFI_SUCCESS && status != EFI_UNSUPPORTED)
58                         pr_warn_ratelimited("query_variable_info() failed: 0x%lx\n",
59                                             status);
60         }
61
62         /*
63          * This is not a normal filesystem, so no point in pretending it has a block
64          * size; we declare f_bsize to 1, so that we can then report the exact value
65          * sent by EFI QueryVariableInfo in f_blocks and f_bfree
66          */
67         buf->f_bsize    = 1;
68         buf->f_namelen  = NAME_MAX;
69         buf->f_blocks   = storage_space;
70         buf->f_bfree    = remaining_space;
71         buf->f_type     = dentry->d_sb->s_magic;
72
73         /*
74          * In f_bavail we declare the free space that the kernel will allow writing
75          * when the storage_paranoia x86 quirk is active. To use more, users
76          * should boot the kernel with efi_no_storage_paranoia.
77          */
78         if (remaining_space > efivar_reserved_space())
79                 buf->f_bavail = remaining_space - efivar_reserved_space();
80         else
81                 buf->f_bavail = 0;
82
83         return 0;
84 }
85 static const struct super_operations efivarfs_ops = {
86         .statfs = efivarfs_statfs,
87         .drop_inode = generic_delete_inode,
88         .evict_inode = efivarfs_evict_inode,
89         .show_options = efivarfs_show_options,
90 };
91
92 /*
93  * Compare two efivarfs file names.
94  *
95  * An efivarfs filename is composed of two parts,
96  *
97  *      1. A case-sensitive variable name
98  *      2. A case-insensitive GUID
99  *
100  * So we need to perform a case-sensitive match on part 1 and a
101  * case-insensitive match on part 2.
102  */
103 static int efivarfs_d_compare(const struct dentry *dentry,
104                               unsigned int len, const char *str,
105                               const struct qstr *name)
106 {
107         int guid = len - EFI_VARIABLE_GUID_LEN;
108
109         if (name->len != len)
110                 return 1;
111
112         /* Case-sensitive compare for the variable name */
113         if (memcmp(str, name->name, guid))
114                 return 1;
115
116         /* Case-insensitive compare for the GUID */
117         return strncasecmp(name->name + guid, str + guid, EFI_VARIABLE_GUID_LEN);
118 }
119
120 static int efivarfs_d_hash(const struct dentry *dentry, struct qstr *qstr)
121 {
122         unsigned long hash = init_name_hash(dentry);
123         const unsigned char *s = qstr->name;
124         unsigned int len = qstr->len;
125
126         if (!efivarfs_valid_name(s, len))
127                 return -EINVAL;
128
129         while (len-- > EFI_VARIABLE_GUID_LEN)
130                 hash = partial_name_hash(*s++, hash);
131
132         /* GUID is case-insensitive. */
133         while (len--)
134                 hash = partial_name_hash(tolower(*s++), hash);
135
136         qstr->hash = end_name_hash(hash);
137         return 0;
138 }
139
140 static const struct dentry_operations efivarfs_d_ops = {
141         .d_compare = efivarfs_d_compare,
142         .d_hash = efivarfs_d_hash,
143         .d_delete = always_delete_dentry,
144 };
145
146 static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
147 {
148         struct dentry *d;
149         struct qstr q;
150         int err;
151
152         q.name = name;
153         q.len = strlen(name);
154
155         err = efivarfs_d_hash(parent, &q);
156         if (err)
157                 return ERR_PTR(err);
158
159         d = d_alloc(parent, &q);
160         if (d)
161                 return d;
162
163         return ERR_PTR(-ENOMEM);
164 }
165
166 static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
167                              unsigned long name_size, void *data)
168 {
169         struct super_block *sb = (struct super_block *)data;
170         struct efivar_entry *entry;
171         struct inode *inode = NULL;
172         struct dentry *dentry, *root = sb->s_root;
173         unsigned long size = 0;
174         char *name;
175         int len;
176         int err = -ENOMEM;
177         bool is_removable = false;
178
179         if (guid_equal(&vendor, &LINUX_EFI_RANDOM_SEED_TABLE_GUID))
180                 return 0;
181
182         entry = kzalloc(sizeof(*entry), GFP_KERNEL);
183         if (!entry)
184                 return err;
185
186         memcpy(entry->var.VariableName, name16, name_size);
187         memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
188
189         len = ucs2_utf8size(entry->var.VariableName);
190
191         /* name, plus '-', plus GUID, plus NUL*/
192         name = kmalloc(len + 1 + EFI_VARIABLE_GUID_LEN + 1, GFP_KERNEL);
193         if (!name)
194                 goto fail;
195
196         ucs2_as_utf8(name, entry->var.VariableName, len);
197
198         if (efivar_variable_is_removable(entry->var.VendorGuid, name, len))
199                 is_removable = true;
200
201         name[len] = '-';
202
203         efi_guid_to_str(&entry->var.VendorGuid, name + len + 1);
204
205         name[len + EFI_VARIABLE_GUID_LEN+1] = '\0';
206
207         /* replace invalid slashes like kobject_set_name_vargs does for /sys/firmware/efi/vars. */
208         strreplace(name, '/', '!');
209
210         inode = efivarfs_get_inode(sb, d_inode(root), S_IFREG | 0644, 0,
211                                    is_removable);
212         if (!inode)
213                 goto fail_name;
214
215         dentry = efivarfs_alloc_dentry(root, name);
216         if (IS_ERR(dentry)) {
217                 err = PTR_ERR(dentry);
218                 goto fail_inode;
219         }
220
221         __efivar_entry_get(entry, NULL, &size, NULL);
222         __efivar_entry_add(entry, &efivarfs_list);
223
224         /* copied by the above to local storage in the dentry. */
225         kfree(name);
226
227         inode_lock(inode);
228         inode->i_private = entry;
229         i_size_write(inode, size + sizeof(entry->var.Attributes));
230         inode_unlock(inode);
231         d_add(dentry, inode);
232
233         return 0;
234
235 fail_inode:
236         iput(inode);
237 fail_name:
238         kfree(name);
239 fail:
240         kfree(entry);
241         return err;
242 }
243
244 static int efivarfs_destroy(struct efivar_entry *entry, void *data)
245 {
246         efivar_entry_remove(entry);
247         kfree(entry);
248         return 0;
249 }
250
251 enum {
252         Opt_uid, Opt_gid,
253 };
254
255 static const struct fs_parameter_spec efivarfs_parameters[] = {
256         fsparam_u32("uid", Opt_uid),
257         fsparam_u32("gid", Opt_gid),
258         {},
259 };
260
261 static int efivarfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
262 {
263         struct efivarfs_fs_info *sbi = fc->s_fs_info;
264         struct efivarfs_mount_opts *opts = &sbi->mount_opts;
265         struct fs_parse_result result;
266         int opt;
267
268         opt = fs_parse(fc, efivarfs_parameters, param, &result);
269         if (opt < 0)
270                 return opt;
271
272         switch (opt) {
273         case Opt_uid:
274                 opts->uid = make_kuid(current_user_ns(), result.uint_32);
275                 if (!uid_valid(opts->uid))
276                         return -EINVAL;
277                 break;
278         case Opt_gid:
279                 opts->gid = make_kgid(current_user_ns(), result.uint_32);
280                 if (!gid_valid(opts->gid))
281                         return -EINVAL;
282                 break;
283         default:
284                 return -EINVAL;
285         }
286
287         return 0;
288 }
289
290 static int efivarfs_fill_super(struct super_block *sb, struct fs_context *fc)
291 {
292         struct inode *inode = NULL;
293         struct dentry *root;
294         int err;
295
296         if (!efivar_is_available())
297                 return -EOPNOTSUPP;
298
299         sb->s_maxbytes          = MAX_LFS_FILESIZE;
300         sb->s_blocksize         = PAGE_SIZE;
301         sb->s_blocksize_bits    = PAGE_SHIFT;
302         sb->s_magic             = EFIVARFS_MAGIC;
303         sb->s_op                = &efivarfs_ops;
304         sb->s_d_op              = &efivarfs_d_ops;
305         sb->s_time_gran         = 1;
306
307         if (!efivar_supports_writes())
308                 sb->s_flags |= SB_RDONLY;
309
310         inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0, true);
311         if (!inode)
312                 return -ENOMEM;
313         inode->i_op = &efivarfs_dir_inode_operations;
314
315         root = d_make_root(inode);
316         sb->s_root = root;
317         if (!root)
318                 return -ENOMEM;
319
320         INIT_LIST_HEAD(&efivarfs_list);
321
322         err = efivar_init(efivarfs_callback, (void *)sb, true, &efivarfs_list);
323         if (err)
324                 efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL);
325
326         return err;
327 }
328
329 static int efivarfs_get_tree(struct fs_context *fc)
330 {
331         return get_tree_single(fc, efivarfs_fill_super);
332 }
333
334 static const struct fs_context_operations efivarfs_context_ops = {
335         .get_tree       = efivarfs_get_tree,
336         .parse_param    = efivarfs_parse_param,
337 };
338
339 static int efivarfs_init_fs_context(struct fs_context *fc)
340 {
341         struct efivarfs_fs_info *sfi;
342
343         sfi = kzalloc(sizeof(*sfi), GFP_KERNEL);
344         if (!sfi)
345                 return -ENOMEM;
346
347         sfi->mount_opts.uid = GLOBAL_ROOT_UID;
348         sfi->mount_opts.gid = GLOBAL_ROOT_GID;
349
350         fc->s_fs_info = sfi;
351         fc->ops = &efivarfs_context_ops;
352         return 0;
353 }
354
355 static void efivarfs_kill_sb(struct super_block *sb)
356 {
357         kill_litter_super(sb);
358
359         if (!efivar_is_available())
360                 return;
361
362         /* Remove all entries and destroy */
363         efivar_entry_iter(efivarfs_destroy, &efivarfs_list, NULL);
364 }
365
366 static struct file_system_type efivarfs_type = {
367         .owner   = THIS_MODULE,
368         .name    = "efivarfs",
369         .init_fs_context = efivarfs_init_fs_context,
370         .kill_sb = efivarfs_kill_sb,
371         .parameters = efivarfs_parameters,
372 };
373
374 static __init int efivarfs_init(void)
375 {
376         return register_filesystem(&efivarfs_type);
377 }
378
379 static __exit void efivarfs_exit(void)
380 {
381         unregister_filesystem(&efivarfs_type);
382 }
383
384 MODULE_AUTHOR("Matthew Garrett, Jeremy Kerr");
385 MODULE_DESCRIPTION("EFI Variable Filesystem");
386 MODULE_LICENSE("GPL");
387 MODULE_ALIAS_FS("efivarfs");
388
389 module_init(efivarfs_init);
390 module_exit(efivarfs_exit);