Ard Biesheuvel | 4febfb8 | 2019-02-02 10:41:15 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Originally from efivars.c, |
| 4 | * |
| 5 | * Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com> |
| 6 | * Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com> |
| 7 | * |
| 8 | * This code takes all variables accessible from EFI runtime and |
| 9 | * exports them via sysfs |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/efi.h> |
| 13 | #include <linux/module.h> |
Linus Torvalds | 20b4fb4 | 2013-05-01 17:51:54 -0700 | [diff] [blame] | 14 | #include <linux/slab.h> |
Matt Fleming | a614e19 | 2013-04-30 11:30:24 +0100 | [diff] [blame] | 15 | #include <linux/ucs2_string.h> |
Matt Fleming | e33655a | 2014-03-17 15:36:37 +0000 | [diff] [blame] | 16 | #include <linux/compat.h> |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 17 | |
| 18 | #define EFIVARS_VERSION "0.08" |
| 19 | #define EFIVARS_DATE "2004-May-17" |
| 20 | |
| 21 | MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>"); |
| 22 | MODULE_DESCRIPTION("sysfs interface to EFI Variables"); |
| 23 | MODULE_LICENSE("GPL"); |
| 24 | MODULE_VERSION(EFIVARS_VERSION); |
| 25 | |
Ard Biesheuvel | 232f4eb | 2020-09-23 09:56:14 +0200 | [diff] [blame] | 26 | static LIST_HEAD(efivar_sysfs_list); |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 27 | |
| 28 | static struct kset *efivars_kset; |
| 29 | |
| 30 | static struct bin_attribute *efivars_new_var; |
| 31 | static struct bin_attribute *efivars_del_var; |
| 32 | |
Matt Fleming | e33655a | 2014-03-17 15:36:37 +0000 | [diff] [blame] | 33 | struct compat_efi_variable { |
| 34 | efi_char16_t VariableName[EFI_VAR_NAME_LEN/sizeof(efi_char16_t)]; |
| 35 | efi_guid_t VendorGuid; |
| 36 | __u32 DataSize; |
| 37 | __u8 Data[1024]; |
| 38 | __u32 Status; |
| 39 | __u32 Attributes; |
| 40 | } __packed; |
| 41 | |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 42 | struct efivar_attribute { |
| 43 | struct attribute attr; |
| 44 | ssize_t (*show) (struct efivar_entry *entry, char *buf); |
| 45 | ssize_t (*store)(struct efivar_entry *entry, const char *buf, size_t count); |
| 46 | }; |
| 47 | |
| 48 | #define EFIVAR_ATTR(_name, _mode, _show, _store) \ |
| 49 | struct efivar_attribute efivar_attr_##_name = { \ |
| 50 | .attr = {.name = __stringify(_name), .mode = _mode}, \ |
| 51 | .show = _show, \ |
| 52 | .store = _store, \ |
| 53 | }; |
| 54 | |
| 55 | #define to_efivar_attr(_attr) container_of(_attr, struct efivar_attribute, attr) |
| 56 | #define to_efivar_entry(obj) container_of(obj, struct efivar_entry, kobj) |
| 57 | |
| 58 | /* |
| 59 | * Prototype for sysfs creation function |
| 60 | */ |
| 61 | static int |
| 62 | efivar_create_sysfs_entry(struct efivar_entry *new_var); |
| 63 | |
| 64 | static ssize_t |
| 65 | efivar_guid_read(struct efivar_entry *entry, char *buf) |
| 66 | { |
| 67 | struct efi_variable *var = &entry->var; |
| 68 | char *str = buf; |
| 69 | |
| 70 | if (!entry || !buf) |
| 71 | return 0; |
| 72 | |
Borislav Petkov | 26e0227 | 2014-12-18 16:02:17 +0100 | [diff] [blame] | 73 | efi_guid_to_str(&var->VendorGuid, str); |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 74 | str += strlen(str); |
| 75 | str += sprintf(str, "\n"); |
| 76 | |
| 77 | return str - buf; |
| 78 | } |
| 79 | |
| 80 | static ssize_t |
| 81 | efivar_attr_read(struct efivar_entry *entry, char *buf) |
| 82 | { |
| 83 | struct efi_variable *var = &entry->var; |
Vladis Dronov | 286d325 | 2020-03-08 09:08:54 +0100 | [diff] [blame] | 84 | unsigned long size = sizeof(var->Data); |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 85 | char *str = buf; |
Vladis Dronov | 286d325 | 2020-03-08 09:08:54 +0100 | [diff] [blame] | 86 | int ret; |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 87 | |
| 88 | if (!entry || !buf) |
| 89 | return -EINVAL; |
| 90 | |
Vladis Dronov | 286d325 | 2020-03-08 09:08:54 +0100 | [diff] [blame] | 91 | ret = efivar_entry_get(entry, &var->Attributes, &size, var->Data); |
| 92 | var->DataSize = size; |
| 93 | if (ret) |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 94 | return -EIO; |
| 95 | |
| 96 | if (var->Attributes & EFI_VARIABLE_NON_VOLATILE) |
| 97 | str += sprintf(str, "EFI_VARIABLE_NON_VOLATILE\n"); |
| 98 | if (var->Attributes & EFI_VARIABLE_BOOTSERVICE_ACCESS) |
| 99 | str += sprintf(str, "EFI_VARIABLE_BOOTSERVICE_ACCESS\n"); |
| 100 | if (var->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) |
| 101 | str += sprintf(str, "EFI_VARIABLE_RUNTIME_ACCESS\n"); |
| 102 | if (var->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) |
| 103 | str += sprintf(str, "EFI_VARIABLE_HARDWARE_ERROR_RECORD\n"); |
| 104 | if (var->Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) |
| 105 | str += sprintf(str, |
| 106 | "EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS\n"); |
| 107 | if (var->Attributes & |
| 108 | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) |
| 109 | str += sprintf(str, |
| 110 | "EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\n"); |
| 111 | if (var->Attributes & EFI_VARIABLE_APPEND_WRITE) |
| 112 | str += sprintf(str, "EFI_VARIABLE_APPEND_WRITE\n"); |
| 113 | return str - buf; |
| 114 | } |
| 115 | |
| 116 | static ssize_t |
| 117 | efivar_size_read(struct efivar_entry *entry, char *buf) |
| 118 | { |
| 119 | struct efi_variable *var = &entry->var; |
Vladis Dronov | 286d325 | 2020-03-08 09:08:54 +0100 | [diff] [blame] | 120 | unsigned long size = sizeof(var->Data); |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 121 | char *str = buf; |
Vladis Dronov | 286d325 | 2020-03-08 09:08:54 +0100 | [diff] [blame] | 122 | int ret; |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 123 | |
| 124 | if (!entry || !buf) |
| 125 | return -EINVAL; |
| 126 | |
Vladis Dronov | 286d325 | 2020-03-08 09:08:54 +0100 | [diff] [blame] | 127 | ret = efivar_entry_get(entry, &var->Attributes, &size, var->Data); |
| 128 | var->DataSize = size; |
| 129 | if (ret) |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 130 | return -EIO; |
| 131 | |
| 132 | str += sprintf(str, "0x%lx\n", var->DataSize); |
| 133 | return str - buf; |
| 134 | } |
| 135 | |
| 136 | static ssize_t |
| 137 | efivar_data_read(struct efivar_entry *entry, char *buf) |
| 138 | { |
| 139 | struct efi_variable *var = &entry->var; |
Vladis Dronov | 286d325 | 2020-03-08 09:08:54 +0100 | [diff] [blame] | 140 | unsigned long size = sizeof(var->Data); |
| 141 | int ret; |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 142 | |
| 143 | if (!entry || !buf) |
| 144 | return -EINVAL; |
| 145 | |
Vladis Dronov | 286d325 | 2020-03-08 09:08:54 +0100 | [diff] [blame] | 146 | ret = efivar_entry_get(entry, &var->Attributes, &size, var->Data); |
| 147 | var->DataSize = size; |
| 148 | if (ret) |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 149 | return -EIO; |
| 150 | |
| 151 | memcpy(buf, var->Data, var->DataSize); |
| 152 | return var->DataSize; |
| 153 | } |
Matt Fleming | 54d2fbf | 2014-03-17 15:08:34 +0000 | [diff] [blame] | 154 | |
| 155 | static inline int |
| 156 | sanity_check(struct efi_variable *var, efi_char16_t *name, efi_guid_t vendor, |
| 157 | unsigned long size, u32 attributes, u8 *data) |
| 158 | { |
| 159 | /* |
| 160 | * If only updating the variable data, then the name |
| 161 | * and guid should remain the same |
| 162 | */ |
| 163 | if (memcmp(name, var->VariableName, sizeof(var->VariableName)) || |
| 164 | efi_guidcmp(vendor, var->VendorGuid)) { |
| 165 | printk(KERN_ERR "efivars: Cannot edit the wrong variable!\n"); |
| 166 | return -EINVAL; |
| 167 | } |
| 168 | |
| 169 | if ((size <= 0) || (attributes == 0)){ |
| 170 | printk(KERN_ERR "efivars: DataSize & Attributes must be valid!\n"); |
| 171 | return -EINVAL; |
| 172 | } |
| 173 | |
| 174 | if ((attributes & ~EFI_VARIABLE_MASK) != 0 || |
Peter Jones | 8282f5d | 2016-02-08 14:48:14 -0500 | [diff] [blame] | 175 | efivar_validate(vendor, name, data, size) == false) { |
Matt Fleming | 54d2fbf | 2014-03-17 15:08:34 +0000 | [diff] [blame] | 176 | printk(KERN_ERR "efivars: Malformed variable content\n"); |
| 177 | return -EINVAL; |
| 178 | } |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
Matt Fleming | e33655a | 2014-03-17 15:36:37 +0000 | [diff] [blame] | 183 | static void |
| 184 | copy_out_compat(struct efi_variable *dst, struct compat_efi_variable *src) |
| 185 | { |
| 186 | memcpy(dst->VariableName, src->VariableName, EFI_VAR_NAME_LEN); |
| 187 | memcpy(dst->Data, src->Data, sizeof(src->Data)); |
| 188 | |
| 189 | dst->VendorGuid = src->VendorGuid; |
| 190 | dst->DataSize = src->DataSize; |
| 191 | dst->Attributes = src->Attributes; |
| 192 | } |
| 193 | |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 194 | /* |
| 195 | * We allow each variable to be edited via rewriting the |
| 196 | * entire efi variable structure. |
| 197 | */ |
| 198 | static ssize_t |
| 199 | efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count) |
| 200 | { |
| 201 | struct efi_variable *new_var, *var = &entry->var; |
Matt Fleming | bafc84d | 2014-03-16 12:14:49 +0000 | [diff] [blame] | 202 | efi_char16_t *name; |
| 203 | unsigned long size; |
| 204 | efi_guid_t vendor; |
| 205 | u32 attributes; |
| 206 | u8 *data; |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 207 | int err; |
| 208 | |
Vladis Dronov | d6c066f | 2020-03-08 09:08:55 +0100 | [diff] [blame] | 209 | if (!entry || !buf) |
| 210 | return -EINVAL; |
| 211 | |
Dmitry Safonov | 98f7620 | 2018-10-12 14:42:53 +0100 | [diff] [blame] | 212 | if (in_compat_syscall()) { |
Matt Fleming | e33655a | 2014-03-17 15:36:37 +0000 | [diff] [blame] | 213 | struct compat_efi_variable *compat; |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 214 | |
Matt Fleming | e33655a | 2014-03-17 15:36:37 +0000 | [diff] [blame] | 215 | if (count != sizeof(*compat)) |
| 216 | return -EINVAL; |
Matt Fleming | bafc84d | 2014-03-16 12:14:49 +0000 | [diff] [blame] | 217 | |
Matt Fleming | e33655a | 2014-03-17 15:36:37 +0000 | [diff] [blame] | 218 | compat = (struct compat_efi_variable *)buf; |
| 219 | attributes = compat->Attributes; |
| 220 | vendor = compat->VendorGuid; |
| 221 | name = compat->VariableName; |
| 222 | size = compat->DataSize; |
| 223 | data = compat->Data; |
Matt Fleming | bafc84d | 2014-03-16 12:14:49 +0000 | [diff] [blame] | 224 | |
Matt Fleming | e33655a | 2014-03-17 15:36:37 +0000 | [diff] [blame] | 225 | err = sanity_check(var, name, vendor, size, attributes, data); |
| 226 | if (err) |
| 227 | return err; |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 228 | |
Matt Fleming | e33655a | 2014-03-17 15:36:37 +0000 | [diff] [blame] | 229 | copy_out_compat(&entry->var, compat); |
| 230 | } else { |
| 231 | if (count != sizeof(struct efi_variable)) |
| 232 | return -EINVAL; |
| 233 | |
| 234 | new_var = (struct efi_variable *)buf; |
| 235 | |
| 236 | attributes = new_var->Attributes; |
| 237 | vendor = new_var->VendorGuid; |
| 238 | name = new_var->VariableName; |
| 239 | size = new_var->DataSize; |
| 240 | data = new_var->Data; |
| 241 | |
| 242 | err = sanity_check(var, name, vendor, size, attributes, data); |
| 243 | if (err) |
| 244 | return err; |
| 245 | |
| 246 | memcpy(&entry->var, new_var, count); |
| 247 | } |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 248 | |
Matt Fleming | bafc84d | 2014-03-16 12:14:49 +0000 | [diff] [blame] | 249 | err = efivar_entry_set(entry, attributes, size, data, NULL); |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 250 | if (err) { |
| 251 | printk(KERN_WARNING "efivars: set_variable() failed: status=%d\n", err); |
| 252 | return -EIO; |
| 253 | } |
| 254 | |
| 255 | return count; |
| 256 | } |
| 257 | |
| 258 | static ssize_t |
| 259 | efivar_show_raw(struct efivar_entry *entry, char *buf) |
| 260 | { |
| 261 | struct efi_variable *var = &entry->var; |
Matt Fleming | e33655a | 2014-03-17 15:36:37 +0000 | [diff] [blame] | 262 | struct compat_efi_variable *compat; |
Vladis Dronov | 286d325 | 2020-03-08 09:08:54 +0100 | [diff] [blame] | 263 | unsigned long datasize = sizeof(var->Data); |
Matt Fleming | e33655a | 2014-03-17 15:36:37 +0000 | [diff] [blame] | 264 | size_t size; |
Vladis Dronov | 286d325 | 2020-03-08 09:08:54 +0100 | [diff] [blame] | 265 | int ret; |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 266 | |
| 267 | if (!entry || !buf) |
| 268 | return 0; |
| 269 | |
Vladis Dronov | 286d325 | 2020-03-08 09:08:54 +0100 | [diff] [blame] | 270 | ret = efivar_entry_get(entry, &var->Attributes, &datasize, var->Data); |
| 271 | var->DataSize = datasize; |
| 272 | if (ret) |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 273 | return -EIO; |
| 274 | |
Dmitry Safonov | 98f7620 | 2018-10-12 14:42:53 +0100 | [diff] [blame] | 275 | if (in_compat_syscall()) { |
Matt Fleming | e33655a | 2014-03-17 15:36:37 +0000 | [diff] [blame] | 276 | compat = (struct compat_efi_variable *)buf; |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 277 | |
Matt Fleming | e33655a | 2014-03-17 15:36:37 +0000 | [diff] [blame] | 278 | size = sizeof(*compat); |
| 279 | memcpy(compat->VariableName, var->VariableName, |
| 280 | EFI_VAR_NAME_LEN); |
| 281 | memcpy(compat->Data, var->Data, sizeof(compat->Data)); |
| 282 | |
| 283 | compat->VendorGuid = var->VendorGuid; |
| 284 | compat->DataSize = var->DataSize; |
| 285 | compat->Attributes = var->Attributes; |
| 286 | } else { |
| 287 | size = sizeof(*var); |
| 288 | memcpy(buf, var, size); |
| 289 | } |
| 290 | |
| 291 | return size; |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /* |
| 295 | * Generic read/write functions that call the specific functions of |
| 296 | * the attributes... |
| 297 | */ |
| 298 | static ssize_t efivar_attr_show(struct kobject *kobj, struct attribute *attr, |
| 299 | char *buf) |
| 300 | { |
| 301 | struct efivar_entry *var = to_efivar_entry(kobj); |
| 302 | struct efivar_attribute *efivar_attr = to_efivar_attr(attr); |
| 303 | ssize_t ret = -EIO; |
| 304 | |
| 305 | if (!capable(CAP_SYS_ADMIN)) |
| 306 | return -EACCES; |
| 307 | |
| 308 | if (efivar_attr->show) { |
| 309 | ret = efivar_attr->show(var, buf); |
| 310 | } |
| 311 | return ret; |
| 312 | } |
| 313 | |
| 314 | static ssize_t efivar_attr_store(struct kobject *kobj, struct attribute *attr, |
| 315 | const char *buf, size_t count) |
| 316 | { |
| 317 | struct efivar_entry *var = to_efivar_entry(kobj); |
| 318 | struct efivar_attribute *efivar_attr = to_efivar_attr(attr); |
| 319 | ssize_t ret = -EIO; |
| 320 | |
| 321 | if (!capable(CAP_SYS_ADMIN)) |
| 322 | return -EACCES; |
| 323 | |
| 324 | if (efivar_attr->store) |
| 325 | ret = efivar_attr->store(var, buf, count); |
| 326 | |
| 327 | return ret; |
| 328 | } |
| 329 | |
| 330 | static const struct sysfs_ops efivar_attr_ops = { |
| 331 | .show = efivar_attr_show, |
| 332 | .store = efivar_attr_store, |
| 333 | }; |
| 334 | |
| 335 | static void efivar_release(struct kobject *kobj) |
| 336 | { |
Geliang Tang | 9c09a34 | 2016-02-01 22:07:02 +0000 | [diff] [blame] | 337 | struct efivar_entry *var = to_efivar_entry(kobj); |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 338 | kfree(var); |
| 339 | } |
| 340 | |
| 341 | static EFIVAR_ATTR(guid, 0400, efivar_guid_read, NULL); |
| 342 | static EFIVAR_ATTR(attributes, 0400, efivar_attr_read, NULL); |
| 343 | static EFIVAR_ATTR(size, 0400, efivar_size_read, NULL); |
| 344 | static EFIVAR_ATTR(data, 0400, efivar_data_read, NULL); |
| 345 | static EFIVAR_ATTR(raw_var, 0600, efivar_show_raw, efivar_store_raw); |
| 346 | |
| 347 | static struct attribute *def_attrs[] = { |
| 348 | &efivar_attr_guid.attr, |
| 349 | &efivar_attr_size.attr, |
| 350 | &efivar_attr_attributes.attr, |
| 351 | &efivar_attr_data.attr, |
| 352 | &efivar_attr_raw_var.attr, |
| 353 | NULL, |
| 354 | }; |
| 355 | |
| 356 | static struct kobj_type efivar_ktype = { |
| 357 | .release = efivar_release, |
| 358 | .sysfs_ops = &efivar_attr_ops, |
| 359 | .default_attrs = def_attrs, |
| 360 | }; |
| 361 | |
| 362 | static ssize_t efivar_create(struct file *filp, struct kobject *kobj, |
| 363 | struct bin_attribute *bin_attr, |
| 364 | char *buf, loff_t pos, size_t count) |
| 365 | { |
Matt Fleming | e33655a | 2014-03-17 15:36:37 +0000 | [diff] [blame] | 366 | struct compat_efi_variable *compat = (struct compat_efi_variable *)buf; |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 367 | struct efi_variable *new_var = (struct efi_variable *)buf; |
| 368 | struct efivar_entry *new_entry; |
Dmitry Safonov | 98f7620 | 2018-10-12 14:42:53 +0100 | [diff] [blame] | 369 | bool need_compat = in_compat_syscall(); |
Matt Fleming | a5d92ad | 2014-03-17 10:57:00 +0000 | [diff] [blame] | 370 | efi_char16_t *name; |
Matt Fleming | bafc84d | 2014-03-16 12:14:49 +0000 | [diff] [blame] | 371 | unsigned long size; |
| 372 | u32 attributes; |
| 373 | u8 *data; |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 374 | int err; |
| 375 | |
| 376 | if (!capable(CAP_SYS_ADMIN)) |
| 377 | return -EACCES; |
| 378 | |
Matt Fleming | e33655a | 2014-03-17 15:36:37 +0000 | [diff] [blame] | 379 | if (need_compat) { |
| 380 | if (count != sizeof(*compat)) |
| 381 | return -EINVAL; |
Matt Fleming | e003bbe | 2014-03-17 09:17:28 +0000 | [diff] [blame] | 382 | |
Matt Fleming | e33655a | 2014-03-17 15:36:37 +0000 | [diff] [blame] | 383 | attributes = compat->Attributes; |
| 384 | name = compat->VariableName; |
| 385 | size = compat->DataSize; |
| 386 | data = compat->Data; |
| 387 | } else { |
| 388 | if (count != sizeof(*new_var)) |
| 389 | return -EINVAL; |
| 390 | |
| 391 | attributes = new_var->Attributes; |
| 392 | name = new_var->VariableName; |
| 393 | size = new_var->DataSize; |
| 394 | data = new_var->Data; |
| 395 | } |
Matt Fleming | bafc84d | 2014-03-16 12:14:49 +0000 | [diff] [blame] | 396 | |
| 397 | if ((attributes & ~EFI_VARIABLE_MASK) != 0 || |
Peter Jones | 8282f5d | 2016-02-08 14:48:14 -0500 | [diff] [blame] | 398 | efivar_validate(new_var->VendorGuid, name, data, |
| 399 | size) == false) { |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 400 | printk(KERN_ERR "efivars: Malformed variable content\n"); |
| 401 | return -EINVAL; |
| 402 | } |
| 403 | |
| 404 | new_entry = kzalloc(sizeof(*new_entry), GFP_KERNEL); |
| 405 | if (!new_entry) |
| 406 | return -ENOMEM; |
| 407 | |
Matt Fleming | e33655a | 2014-03-17 15:36:37 +0000 | [diff] [blame] | 408 | if (need_compat) |
| 409 | copy_out_compat(&new_entry->var, compat); |
| 410 | else |
| 411 | memcpy(&new_entry->var, new_var, sizeof(*new_var)); |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 412 | |
Matt Fleming | bafc84d | 2014-03-16 12:14:49 +0000 | [diff] [blame] | 413 | err = efivar_entry_set(new_entry, attributes, size, |
| 414 | data, &efivar_sysfs_list); |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 415 | if (err) { |
| 416 | if (err == -EEXIST) |
| 417 | err = -EINVAL; |
| 418 | goto out; |
| 419 | } |
| 420 | |
| 421 | if (efivar_create_sysfs_entry(new_entry)) { |
| 422 | printk(KERN_WARNING "efivars: failed to create sysfs entry.\n"); |
| 423 | kfree(new_entry); |
| 424 | } |
| 425 | return count; |
| 426 | |
| 427 | out: |
| 428 | kfree(new_entry); |
| 429 | return err; |
| 430 | } |
| 431 | |
| 432 | static ssize_t efivar_delete(struct file *filp, struct kobject *kobj, |
| 433 | struct bin_attribute *bin_attr, |
| 434 | char *buf, loff_t pos, size_t count) |
| 435 | { |
| 436 | struct efi_variable *del_var = (struct efi_variable *)buf; |
Matt Fleming | e33655a | 2014-03-17 15:36:37 +0000 | [diff] [blame] | 437 | struct compat_efi_variable *compat; |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 438 | struct efivar_entry *entry; |
Matt Fleming | bafc84d | 2014-03-16 12:14:49 +0000 | [diff] [blame] | 439 | efi_char16_t *name; |
| 440 | efi_guid_t vendor; |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 441 | int err = 0; |
| 442 | |
| 443 | if (!capable(CAP_SYS_ADMIN)) |
| 444 | return -EACCES; |
| 445 | |
Dmitry Safonov | 98f7620 | 2018-10-12 14:42:53 +0100 | [diff] [blame] | 446 | if (in_compat_syscall()) { |
Matt Fleming | e33655a | 2014-03-17 15:36:37 +0000 | [diff] [blame] | 447 | if (count != sizeof(*compat)) |
| 448 | return -EINVAL; |
Matt Fleming | e003bbe | 2014-03-17 09:17:28 +0000 | [diff] [blame] | 449 | |
Matt Fleming | e33655a | 2014-03-17 15:36:37 +0000 | [diff] [blame] | 450 | compat = (struct compat_efi_variable *)buf; |
| 451 | name = compat->VariableName; |
| 452 | vendor = compat->VendorGuid; |
| 453 | } else { |
| 454 | if (count != sizeof(*del_var)) |
| 455 | return -EINVAL; |
| 456 | |
| 457 | name = del_var->VariableName; |
| 458 | vendor = del_var->VendorGuid; |
| 459 | } |
Matt Fleming | bafc84d | 2014-03-16 12:14:49 +0000 | [diff] [blame] | 460 | |
Sylvain Chouleur | 21b3ddd | 2016-07-15 21:36:30 +0200 | [diff] [blame] | 461 | if (efivar_entry_iter_begin()) |
| 462 | return -EINTR; |
Matt Fleming | bafc84d | 2014-03-16 12:14:49 +0000 | [diff] [blame] | 463 | entry = efivar_entry_find(name, vendor, &efivar_sysfs_list, true); |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 464 | if (!entry) |
| 465 | err = -EINVAL; |
| 466 | else if (__efivar_entry_delete(entry)) |
| 467 | err = -EIO; |
| 468 | |
Seiji Aguchi | e0d5973 | 2013-10-30 15:27:26 -0400 | [diff] [blame] | 469 | if (err) { |
| 470 | efivar_entry_iter_end(); |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 471 | return err; |
Seiji Aguchi | e0d5973 | 2013-10-30 15:27:26 -0400 | [diff] [blame] | 472 | } |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 473 | |
Seiji Aguchi | e0d5973 | 2013-10-30 15:27:26 -0400 | [diff] [blame] | 474 | if (!entry->scanning) { |
| 475 | efivar_entry_iter_end(); |
| 476 | efivar_unregister(entry); |
| 477 | } else |
| 478 | efivar_entry_iter_end(); |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 479 | |
| 480 | /* It's dead Jim.... */ |
| 481 | return count; |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * efivar_create_sysfs_entry - create a new entry in sysfs |
| 486 | * @new_var: efivar entry to create |
| 487 | * |
Dan Carpenter | f7ef7e3 | 2015-04-21 12:21:53 +0300 | [diff] [blame] | 488 | * Returns 0 on success, negative error code on failure |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 489 | */ |
| 490 | static int |
| 491 | efivar_create_sysfs_entry(struct efivar_entry *new_var) |
| 492 | { |
Peter Jones | e0d64e6 | 2016-02-08 14:48:12 -0500 | [diff] [blame] | 493 | int short_name_size; |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 494 | char *short_name; |
Peter Jones | e0d64e6 | 2016-02-08 14:48:12 -0500 | [diff] [blame] | 495 | unsigned long utf8_name_size; |
| 496 | efi_char16_t *variable_name = new_var->var.VariableName; |
Dan Carpenter | f7ef7e3 | 2015-04-21 12:21:53 +0300 | [diff] [blame] | 497 | int ret; |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 498 | |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 499 | /* |
Peter Jones | e0d64e6 | 2016-02-08 14:48:12 -0500 | [diff] [blame] | 500 | * Length of the variable bytes in UTF8, plus the '-' separator, |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 501 | * plus the GUID, plus trailing NUL |
| 502 | */ |
Peter Jones | e0d64e6 | 2016-02-08 14:48:12 -0500 | [diff] [blame] | 503 | utf8_name_size = ucs2_utf8size(variable_name); |
| 504 | short_name_size = utf8_name_size + 1 + EFI_VARIABLE_GUID_LEN + 1; |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 505 | |
Peter Jones | e0d64e6 | 2016-02-08 14:48:12 -0500 | [diff] [blame] | 506 | short_name = kmalloc(short_name_size, GFP_KERNEL); |
Dan Carpenter | 7b2dd6d | 2013-04-30 10:43:44 +0300 | [diff] [blame] | 507 | if (!short_name) |
Dan Carpenter | f7ef7e3 | 2015-04-21 12:21:53 +0300 | [diff] [blame] | 508 | return -ENOMEM; |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 509 | |
Peter Jones | e0d64e6 | 2016-02-08 14:48:12 -0500 | [diff] [blame] | 510 | ucs2_as_utf8(short_name, variable_name, short_name_size); |
| 511 | |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 512 | /* This is ugly, but necessary to separate one vendor's |
| 513 | private variables from another's. */ |
Peter Jones | e0d64e6 | 2016-02-08 14:48:12 -0500 | [diff] [blame] | 514 | short_name[utf8_name_size] = '-'; |
Borislav Petkov | 26e0227 | 2014-12-18 16:02:17 +0100 | [diff] [blame] | 515 | efi_guid_to_str(&new_var->var.VendorGuid, |
Peter Jones | e0d64e6 | 2016-02-08 14:48:12 -0500 | [diff] [blame] | 516 | short_name + utf8_name_size + 1); |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 517 | |
| 518 | new_var->kobj.kset = efivars_kset; |
| 519 | |
Dan Carpenter | f7ef7e3 | 2015-04-21 12:21:53 +0300 | [diff] [blame] | 520 | ret = kobject_init_and_add(&new_var->kobj, &efivar_ktype, |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 521 | NULL, "%s", short_name); |
| 522 | kfree(short_name); |
Ard Biesheuvel | d8bd8c6 | 2020-05-22 18:15:49 +0200 | [diff] [blame] | 523 | if (ret) { |
| 524 | kobject_put(&new_var->kobj); |
Dan Carpenter | f7ef7e3 | 2015-04-21 12:21:53 +0300 | [diff] [blame] | 525 | return ret; |
Ard Biesheuvel | d8bd8c6 | 2020-05-22 18:15:49 +0200 | [diff] [blame] | 526 | } |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 527 | |
| 528 | kobject_uevent(&new_var->kobj, KOBJ_ADD); |
Sylvain Chouleur | 21b3ddd | 2016-07-15 21:36:30 +0200 | [diff] [blame] | 529 | if (efivar_entry_add(new_var, &efivar_sysfs_list)) { |
| 530 | efivar_unregister(new_var); |
| 531 | return -EINTR; |
| 532 | } |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 533 | |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | static int |
| 538 | create_efivars_bin_attributes(void) |
| 539 | { |
| 540 | struct bin_attribute *attr; |
| 541 | int error; |
| 542 | |
| 543 | /* new_var */ |
| 544 | attr = kzalloc(sizeof(*attr), GFP_KERNEL); |
| 545 | if (!attr) |
| 546 | return -ENOMEM; |
| 547 | |
| 548 | attr->attr.name = "new_var"; |
| 549 | attr->attr.mode = 0200; |
| 550 | attr->write = efivar_create; |
| 551 | efivars_new_var = attr; |
| 552 | |
| 553 | /* del_var */ |
| 554 | attr = kzalloc(sizeof(*attr), GFP_KERNEL); |
| 555 | if (!attr) { |
| 556 | error = -ENOMEM; |
| 557 | goto out_free; |
| 558 | } |
| 559 | attr->attr.name = "del_var"; |
| 560 | attr->attr.mode = 0200; |
| 561 | attr->write = efivar_delete; |
| 562 | efivars_del_var = attr; |
| 563 | |
| 564 | sysfs_bin_attr_init(efivars_new_var); |
| 565 | sysfs_bin_attr_init(efivars_del_var); |
| 566 | |
| 567 | /* Register */ |
| 568 | error = sysfs_create_bin_file(&efivars_kset->kobj, efivars_new_var); |
| 569 | if (error) { |
| 570 | printk(KERN_ERR "efivars: unable to create new_var sysfs file" |
| 571 | " due to error %d\n", error); |
| 572 | goto out_free; |
| 573 | } |
| 574 | |
| 575 | error = sysfs_create_bin_file(&efivars_kset->kobj, efivars_del_var); |
| 576 | if (error) { |
| 577 | printk(KERN_ERR "efivars: unable to create del_var sysfs file" |
| 578 | " due to error %d\n", error); |
| 579 | sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var); |
| 580 | goto out_free; |
| 581 | } |
| 582 | |
| 583 | return 0; |
| 584 | out_free: |
| 585 | kfree(efivars_del_var); |
| 586 | efivars_del_var = NULL; |
| 587 | kfree(efivars_new_var); |
| 588 | efivars_new_var = NULL; |
| 589 | return error; |
| 590 | } |
| 591 | |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 592 | static int efivars_sysfs_callback(efi_char16_t *name, efi_guid_t vendor, |
| 593 | unsigned long name_size, void *data) |
| 594 | { |
| 595 | struct efivar_entry *entry; |
| 596 | |
| 597 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 598 | if (!entry) |
| 599 | return -ENOMEM; |
| 600 | |
| 601 | memcpy(entry->var.VariableName, name, name_size); |
| 602 | memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t)); |
| 603 | |
| 604 | efivar_create_sysfs_entry(entry); |
| 605 | |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | static int efivar_sysfs_destroy(struct efivar_entry *entry, void *data) |
| 610 | { |
Sylvain Chouleur | 21b3ddd | 2016-07-15 21:36:30 +0200 | [diff] [blame] | 611 | int err = efivar_entry_remove(entry); |
| 612 | |
| 613 | if (err) |
| 614 | return err; |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 615 | efivar_unregister(entry); |
| 616 | return 0; |
| 617 | } |
| 618 | |
Bojan Prtvar | 6f9dd30 | 2013-09-03 08:56:20 +0200 | [diff] [blame] | 619 | static void efivars_sysfs_exit(void) |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 620 | { |
| 621 | /* Remove all entries and destroy */ |
Sylvain Chouleur | 21b3ddd | 2016-07-15 21:36:30 +0200 | [diff] [blame] | 622 | int err; |
| 623 | |
| 624 | err = __efivar_entry_iter(efivar_sysfs_destroy, &efivar_sysfs_list, |
| 625 | NULL, NULL); |
| 626 | if (err) { |
| 627 | pr_err("efivars: Failed to destroy sysfs entries\n"); |
| 628 | return; |
| 629 | } |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 630 | |
| 631 | if (efivars_new_var) |
| 632 | sysfs_remove_bin_file(&efivars_kset->kobj, efivars_new_var); |
| 633 | if (efivars_del_var) |
| 634 | sysfs_remove_bin_file(&efivars_kset->kobj, efivars_del_var); |
| 635 | kfree(efivars_new_var); |
| 636 | kfree(efivars_del_var); |
| 637 | kset_unregister(efivars_kset); |
| 638 | } |
| 639 | |
Ard Biesheuvel | 5d3c8617 | 2020-09-23 10:13:07 +0200 | [diff] [blame] | 640 | static int efivars_sysfs_init(void) |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 641 | { |
| 642 | struct kobject *parent_kobj = efivars_kobject(); |
| 643 | int error = 0; |
| 644 | |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 645 | /* No efivars has been registered yet */ |
Ard Biesheuvel | f88814c | 2020-07-08 13:01:57 +0300 | [diff] [blame] | 646 | if (!parent_kobj || !efivar_supports_writes()) |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 647 | return 0; |
| 648 | |
| 649 | printk(KERN_INFO "EFI Variables Facility v%s %s\n", EFIVARS_VERSION, |
| 650 | EFIVARS_DATE); |
| 651 | |
| 652 | efivars_kset = kset_create_and_add("vars", NULL, parent_kobj); |
| 653 | if (!efivars_kset) { |
| 654 | printk(KERN_ERR "efivars: Subsystem registration failed.\n"); |
| 655 | return -ENOMEM; |
| 656 | } |
| 657 | |
Julia Lawall | 1cfd631 | 2016-05-06 22:39:30 +0100 | [diff] [blame] | 658 | efivar_init(efivars_sysfs_callback, NULL, true, &efivar_sysfs_list); |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 659 | |
| 660 | error = create_efivars_bin_attributes(); |
| 661 | if (error) { |
| 662 | efivars_sysfs_exit(); |
| 663 | return error; |
| 664 | } |
| 665 | |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 666 | return 0; |
| 667 | } |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 668 | |
| 669 | module_init(efivars_sysfs_init); |
| 670 | module_exit(efivars_sysfs_exit); |