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> |
Miklos Szeredi | d701ea2 | 2021-04-07 14:36:44 +0200 | [diff] [blame] | 13 | #include <linux/fileattr.h> |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 14 | |
| 15 | #include "internal.h" |
| 16 | |
Miklos Szeredi | d701ea2 | 2021-04-07 14:36:44 +0200 | [diff] [blame] | 17 | static const struct inode_operations efivarfs_file_inode_operations; |
| 18 | |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 19 | struct inode *efivarfs_get_inode(struct super_block *sb, |
Peter Jones | ed8b0de | 2016-02-08 14:48:15 -0500 | [diff] [blame] | 20 | const struct inode *dir, int mode, |
| 21 | dev_t dev, bool is_removable) |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 22 | { |
| 23 | struct inode *inode = new_inode(sb); |
| 24 | |
| 25 | if (inode) { |
| 26 | inode->i_ino = get_next_ino(); |
| 27 | inode->i_mode = mode; |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 28 | inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); |
Peter Jones | ed8b0de | 2016-02-08 14:48:15 -0500 | [diff] [blame] | 29 | inode->i_flags = is_removable ? 0 : S_IMMUTABLE; |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 30 | switch (mode & S_IFMT) { |
| 31 | case S_IFREG: |
Miklos Szeredi | d701ea2 | 2021-04-07 14:36:44 +0200 | [diff] [blame] | 32 | inode->i_op = &efivarfs_file_inode_operations; |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 33 | inode->i_fop = &efivarfs_file_operations; |
| 34 | break; |
| 35 | case S_IFDIR: |
| 36 | inode->i_op = &efivarfs_dir_inode_operations; |
| 37 | inode->i_fop = &simple_dir_operations; |
| 38 | inc_nlink(inode); |
| 39 | break; |
| 40 | } |
| 41 | } |
| 42 | return inode; |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * Return true if 'str' is a valid efivarfs filename of the form, |
| 47 | * |
| 48 | * VariableName-12345678-1234-1234-1234-1234567891bc |
| 49 | */ |
| 50 | bool efivarfs_valid_name(const char *str, int len) |
| 51 | { |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 52 | const char *s = str + len - EFI_VARIABLE_GUID_LEN; |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 53 | |
| 54 | /* |
| 55 | * We need a GUID, plus at least one letter for the variable name, |
| 56 | * plus the '-' separator |
| 57 | */ |
| 58 | if (len < EFI_VARIABLE_GUID_LEN + 2) |
| 59 | return false; |
| 60 | |
| 61 | /* GUID must be preceded by a '-' */ |
| 62 | if (*(s - 1) != '-') |
| 63 | return false; |
| 64 | |
| 65 | /* |
| 66 | * Validate that 's' is of the correct format, e.g. |
| 67 | * |
| 68 | * 12345678-1234-1234-1234-123456789abc |
| 69 | */ |
Andy Shevchenko | 8236431 | 2016-05-20 17:01:21 -0700 | [diff] [blame] | 70 | return uuid_is_valid(s); |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 73 | static int efivarfs_create(struct user_namespace *mnt_userns, struct inode *dir, |
| 74 | struct dentry *dentry, umode_t mode, bool excl) |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 75 | { |
Peter Jones | ed8b0de | 2016-02-08 14:48:15 -0500 | [diff] [blame] | 76 | struct inode *inode = NULL; |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 77 | struct efivar_entry *var; |
| 78 | int namelen, i = 0, err = 0; |
Peter Jones | ed8b0de | 2016-02-08 14:48:15 -0500 | [diff] [blame] | 79 | bool is_removable = false; |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 80 | |
| 81 | if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len)) |
| 82 | return -EINVAL; |
| 83 | |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 84 | var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL); |
Peter Jones | ed8b0de | 2016-02-08 14:48:15 -0500 | [diff] [blame] | 85 | if (!var) |
| 86 | return -ENOMEM; |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 87 | |
| 88 | /* length of the variable name itself: remove GUID and separator */ |
| 89 | namelen = dentry->d_name.len - EFI_VARIABLE_GUID_LEN - 1; |
| 90 | |
Andy Shevchenko | c432656 | 2018-07-20 10:47:26 +0900 | [diff] [blame] | 91 | err = guid_parse(dentry->d_name.name + namelen + 1, &var->var.VendorGuid); |
| 92 | if (err) |
| 93 | goto out; |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 94 | |
Peter Jones | ed8b0de | 2016-02-08 14:48:15 -0500 | [diff] [blame] | 95 | if (efivar_variable_is_removable(var->var.VendorGuid, |
| 96 | dentry->d_name.name, namelen)) |
| 97 | is_removable = true; |
| 98 | |
| 99 | inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0, is_removable); |
| 100 | if (!inode) { |
| 101 | err = -ENOMEM; |
| 102 | goto out; |
| 103 | } |
| 104 | |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 105 | for (i = 0; i < namelen; i++) |
| 106 | var->var.VariableName[i] = dentry->d_name.name[i]; |
| 107 | |
| 108 | var->var.VariableName[i] = '\0'; |
| 109 | |
| 110 | inode->i_private = var; |
Ard Biesheuvel | ff04f3b | 2020-11-25 08:45:55 +0100 | [diff] [blame] | 111 | kmemleak_ignore(var); |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 112 | |
Sylvain Chouleur | 21b3ddd | 2016-07-15 21:36:30 +0200 | [diff] [blame] | 113 | err = efivar_entry_add(var, &efivarfs_list); |
| 114 | if (err) |
| 115 | goto out; |
| 116 | |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 117 | d_instantiate(dentry, inode); |
| 118 | dget(dentry); |
| 119 | out: |
| 120 | if (err) { |
| 121 | kfree(var); |
Peter Jones | ed8b0de | 2016-02-08 14:48:15 -0500 | [diff] [blame] | 122 | if (inode) |
| 123 | iput(inode); |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 124 | } |
| 125 | return err; |
| 126 | } |
| 127 | |
| 128 | static int efivarfs_unlink(struct inode *dir, struct dentry *dentry) |
| 129 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 130 | struct efivar_entry *var = d_inode(dentry)->i_private; |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 131 | |
| 132 | if (efivar_entry_delete(var)) |
| 133 | return -EINVAL; |
| 134 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 135 | drop_nlink(d_inode(dentry)); |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 136 | dput(dentry); |
| 137 | return 0; |
| 138 | }; |
| 139 | |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 140 | const struct inode_operations efivarfs_dir_inode_operations = { |
Al Viro | 6e8cd2c | 2013-07-14 17:48:35 +0400 | [diff] [blame] | 141 | .lookup = simple_lookup, |
Matt Fleming | d68772b | 2013-02-08 16:27:24 +0000 | [diff] [blame] | 142 | .unlink = efivarfs_unlink, |
| 143 | .create = efivarfs_create, |
| 144 | }; |
Miklos Szeredi | d701ea2 | 2021-04-07 14:36:44 +0200 | [diff] [blame] | 145 | |
| 146 | static int |
| 147 | efivarfs_fileattr_get(struct dentry *dentry, struct fileattr *fa) |
| 148 | { |
| 149 | unsigned int i_flags; |
| 150 | unsigned int flags = 0; |
| 151 | |
| 152 | i_flags = d_inode(dentry)->i_flags; |
| 153 | if (i_flags & S_IMMUTABLE) |
| 154 | flags |= FS_IMMUTABLE_FL; |
| 155 | |
| 156 | fileattr_fill_flags(fa, flags); |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static int |
| 162 | efivarfs_fileattr_set(struct user_namespace *mnt_userns, |
| 163 | struct dentry *dentry, struct fileattr *fa) |
| 164 | { |
| 165 | unsigned int i_flags = 0; |
| 166 | |
| 167 | if (fileattr_has_fsx(fa)) |
| 168 | return -EOPNOTSUPP; |
| 169 | |
| 170 | if (fa->flags & ~FS_IMMUTABLE_FL) |
| 171 | return -EOPNOTSUPP; |
| 172 | |
| 173 | if (fa->flags & FS_IMMUTABLE_FL) |
| 174 | i_flags |= S_IMMUTABLE; |
| 175 | |
| 176 | inode_set_flags(d_inode(dentry), i_flags, S_IMMUTABLE); |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static const struct inode_operations efivarfs_file_inode_operations = { |
| 182 | .fileattr_get = efivarfs_fileattr_get, |
| 183 | .fileattr_set = efivarfs_fileattr_set, |
| 184 | }; |