Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 Red Hat, Inc. |
| 4 | * Copyright (C) 2012 Jeremy Kerr <jeremy.kerr@canonical.com> |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/efi.h> |
| 8 | #include <linux/fs.h> |
| 9 | #include <linux/ctype.h> |
Ard Biesheuvel | ff04f3b | 2020-11-25 08:45:55 +0100 | [diff] [blame] | 10 | #include <linux/kmemleak.h> |
Linus Torvalds | 20b4fb4 | 2013-05-01 17:51:54 -0700 | [diff] [blame] | 11 | #include <linux/slab.h> |
Andy Shevchenko | 8236431 | 2016-05-20 17:01:21 -0700 | [diff] [blame] | 12 | #include <linux/uuid.h> |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 13 | |
| 14 | #include "internal.h" |
| 15 | |
| 16 | struct inode *efivarfs_get_inode(struct super_block *sb, |
Peter Jones | ed8b0de | 2016-02-08 14:48:15 -0500 | [diff] [blame] | 17 | const struct inode *dir, int mode, |
| 18 | dev_t dev, bool is_removable) |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 19 | { |
| 20 | struct inode *inode = new_inode(sb); |
| 21 | |
| 22 | if (inode) { |
| 23 | inode->i_ino = get_next_ino(); |
| 24 | inode->i_mode = mode; |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 25 | inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); |
Peter Jones | ed8b0de | 2016-02-08 14:48:15 -0500 | [diff] [blame] | 26 | inode->i_flags = is_removable ? 0 : S_IMMUTABLE; |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 27 | switch (mode & S_IFMT) { |
| 28 | case S_IFREG: |
| 29 | inode->i_fop = &efivarfs_file_operations; |
| 30 | break; |
| 31 | case S_IFDIR: |
| 32 | inode->i_op = &efivarfs_dir_inode_operations; |
| 33 | inode->i_fop = &simple_dir_operations; |
| 34 | inc_nlink(inode); |
| 35 | break; |
| 36 | } |
| 37 | } |
| 38 | return inode; |
| 39 | } |
| 40 | |
| 41 | /* |
| 42 | * Return true if 'str' is a valid efivarfs filename of the form, |
| 43 | * |
| 44 | * VariableName-12345678-1234-1234-1234-1234567891bc |
| 45 | */ |
| 46 | bool efivarfs_valid_name(const char *str, int len) |
| 47 | { |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 48 | const char *s = str + len - EFI_VARIABLE_GUID_LEN; |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 49 | |
| 50 | /* |
| 51 | * We need a GUID, plus at least one letter for the variable name, |
| 52 | * plus the '-' separator |
| 53 | */ |
| 54 | if (len < EFI_VARIABLE_GUID_LEN + 2) |
| 55 | return false; |
| 56 | |
| 57 | /* GUID must be preceded by a '-' */ |
| 58 | if (*(s - 1) != '-') |
| 59 | return false; |
| 60 | |
| 61 | /* |
| 62 | * Validate that 's' is of the correct format, e.g. |
| 63 | * |
| 64 | * 12345678-1234-1234-1234-123456789abc |
| 65 | */ |
Andy Shevchenko | 8236431 | 2016-05-20 17:01:21 -0700 | [diff] [blame] | 66 | return uuid_is_valid(s); |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 69 | static int efivarfs_create(struct user_namespace *mnt_userns, struct inode *dir, |
| 70 | struct dentry *dentry, umode_t mode, bool excl) |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 71 | { |
Peter Jones | ed8b0de | 2016-02-08 14:48:15 -0500 | [diff] [blame] | 72 | struct inode *inode = NULL; |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 73 | struct efivar_entry *var; |
| 74 | int namelen, i = 0, err = 0; |
Peter Jones | ed8b0de | 2016-02-08 14:48:15 -0500 | [diff] [blame] | 75 | bool is_removable = false; |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 76 | |
| 77 | if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len)) |
| 78 | return -EINVAL; |
| 79 | |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 80 | var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL); |
Peter Jones | ed8b0de | 2016-02-08 14:48:15 -0500 | [diff] [blame] | 81 | if (!var) |
| 82 | return -ENOMEM; |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 83 | |
| 84 | /* length of the variable name itself: remove GUID and separator */ |
| 85 | namelen = dentry->d_name.len - EFI_VARIABLE_GUID_LEN - 1; |
| 86 | |
Andy Shevchenko | c432656 | 2018-07-20 10:47:26 +0900 | [diff] [blame] | 87 | err = guid_parse(dentry->d_name.name + namelen + 1, &var->var.VendorGuid); |
| 88 | if (err) |
| 89 | goto out; |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 90 | |
Peter Jones | ed8b0de | 2016-02-08 14:48:15 -0500 | [diff] [blame] | 91 | if (efivar_variable_is_removable(var->var.VendorGuid, |
| 92 | dentry->d_name.name, namelen)) |
| 93 | is_removable = true; |
| 94 | |
| 95 | inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0, is_removable); |
| 96 | if (!inode) { |
| 97 | err = -ENOMEM; |
| 98 | goto out; |
| 99 | } |
| 100 | |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 101 | for (i = 0; i < namelen; i++) |
| 102 | var->var.VariableName[i] = dentry->d_name.name[i]; |
| 103 | |
| 104 | var->var.VariableName[i] = '\0'; |
| 105 | |
| 106 | inode->i_private = var; |
Ard Biesheuvel | ff04f3b | 2020-11-25 08:45:55 +0100 | [diff] [blame] | 107 | kmemleak_ignore(var); |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 108 | |
Sylvain Chouleur | 21b3ddd | 2016-07-15 21:36:30 +0200 | [diff] [blame] | 109 | err = efivar_entry_add(var, &efivarfs_list); |
| 110 | if (err) |
| 111 | goto out; |
| 112 | |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 113 | d_instantiate(dentry, inode); |
| 114 | dget(dentry); |
| 115 | out: |
| 116 | if (err) { |
| 117 | kfree(var); |
Peter Jones | ed8b0de | 2016-02-08 14:48:15 -0500 | [diff] [blame] | 118 | if (inode) |
| 119 | iput(inode); |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 120 | } |
| 121 | return err; |
| 122 | } |
| 123 | |
| 124 | static int efivarfs_unlink(struct inode *dir, struct dentry *dentry) |
| 125 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 126 | struct efivar_entry *var = d_inode(dentry)->i_private; |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 127 | |
| 128 | if (efivar_entry_delete(var)) |
| 129 | return -EINVAL; |
| 130 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 131 | drop_nlink(d_inode(dentry)); |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 132 | dput(dentry); |
| 133 | return 0; |
| 134 | }; |
| 135 | |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 136 | const struct inode_operations efivarfs_dir_inode_operations = { |
Al Viro | 6e8cd2c | 2013-07-14 17:48:35 +0400 | [diff] [blame] | 137 | .lookup = simple_lookup, |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 138 | .unlink = efivarfs_unlink, |
| 139 | .create = efivarfs_create, |
| 140 | }; |