super.c 10 KB

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