super.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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/suspend.h>
  16. #include <linux/magic.h>
  17. #include <linux/statfs.h>
  18. #include <linux/notifier.h>
  19. #include <linux/printk.h>
  20. #include <linux/namei.h>
  21. #include "internal.h"
  22. #include "../internal.h"
  23. static int efivarfs_ops_notifier(struct notifier_block *nb, unsigned long event,
  24. void *data)
  25. {
  26. struct efivarfs_fs_info *sfi = container_of(nb, struct efivarfs_fs_info, nb);
  27. switch (event) {
  28. case EFIVAR_OPS_RDONLY:
  29. sfi->sb->s_flags |= SB_RDONLY;
  30. break;
  31. case EFIVAR_OPS_RDWR:
  32. sfi->sb->s_flags &= ~SB_RDONLY;
  33. break;
  34. default:
  35. return NOTIFY_DONE;
  36. }
  37. return NOTIFY_OK;
  38. }
  39. static struct inode *efivarfs_alloc_inode(struct super_block *sb)
  40. {
  41. struct efivar_entry *entry = kzalloc_obj(*entry);
  42. if (!entry)
  43. return NULL;
  44. inode_init_once(&entry->vfs_inode);
  45. entry->removed = false;
  46. return &entry->vfs_inode;
  47. }
  48. static void efivarfs_free_inode(struct inode *inode)
  49. {
  50. struct efivar_entry *entry = efivar_entry(inode);
  51. kfree(entry);
  52. }
  53. static int efivarfs_show_options(struct seq_file *m, struct dentry *root)
  54. {
  55. struct super_block *sb = root->d_sb;
  56. struct efivarfs_fs_info *sbi = sb->s_fs_info;
  57. struct efivarfs_mount_opts *opts = &sbi->mount_opts;
  58. if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
  59. seq_printf(m, ",uid=%u",
  60. from_kuid_munged(&init_user_ns, opts->uid));
  61. if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
  62. seq_printf(m, ",gid=%u",
  63. from_kgid_munged(&init_user_ns, opts->gid));
  64. return 0;
  65. }
  66. static int efivarfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  67. {
  68. const u32 attr = EFI_VARIABLE_NON_VOLATILE |
  69. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  70. EFI_VARIABLE_RUNTIME_ACCESS;
  71. u64 storage_space, remaining_space, max_variable_size;
  72. u64 id = huge_encode_dev(dentry->d_sb->s_dev);
  73. efi_status_t status;
  74. /* Some UEFI firmware does not implement QueryVariableInfo() */
  75. storage_space = remaining_space = 0;
  76. if (efi_rt_services_supported(EFI_RT_SUPPORTED_QUERY_VARIABLE_INFO)) {
  77. status = efivar_query_variable_info(attr, &storage_space,
  78. &remaining_space,
  79. &max_variable_size);
  80. if (status != EFI_SUCCESS && status != EFI_UNSUPPORTED)
  81. pr_warn_ratelimited("query_variable_info() failed: 0x%lx\n",
  82. status);
  83. }
  84. /*
  85. * This is not a normal filesystem, so no point in pretending it has a block
  86. * size; we declare f_bsize to 1, so that we can then report the exact value
  87. * sent by EFI QueryVariableInfo in f_blocks and f_bfree
  88. */
  89. buf->f_bsize = 1;
  90. buf->f_namelen = NAME_MAX;
  91. buf->f_blocks = storage_space;
  92. buf->f_bfree = remaining_space;
  93. buf->f_type = dentry->d_sb->s_magic;
  94. buf->f_fsid = u64_to_fsid(id);
  95. /*
  96. * In f_bavail we declare the free space that the kernel will allow writing
  97. * when the storage_paranoia x86 quirk is active. To use more, users
  98. * should boot the kernel with efi_no_storage_paranoia.
  99. */
  100. if (remaining_space > efivar_reserved_space())
  101. buf->f_bavail = remaining_space - efivar_reserved_space();
  102. else
  103. buf->f_bavail = 0;
  104. return 0;
  105. }
  106. static int efivarfs_freeze_fs(struct super_block *sb);
  107. static int efivarfs_unfreeze_fs(struct super_block *sb);
  108. static const struct super_operations efivarfs_ops = {
  109. .statfs = efivarfs_statfs,
  110. .drop_inode = inode_just_drop,
  111. .alloc_inode = efivarfs_alloc_inode,
  112. .free_inode = efivarfs_free_inode,
  113. .show_options = efivarfs_show_options,
  114. .freeze_fs = efivarfs_freeze_fs,
  115. .unfreeze_fs = efivarfs_unfreeze_fs,
  116. };
  117. /*
  118. * Compare two efivarfs file names.
  119. *
  120. * An efivarfs filename is composed of two parts,
  121. *
  122. * 1. A case-sensitive variable name
  123. * 2. A case-insensitive GUID
  124. *
  125. * So we need to perform a case-sensitive match on part 1 and a
  126. * case-insensitive match on part 2.
  127. */
  128. static int efivarfs_d_compare(const struct dentry *dentry,
  129. unsigned int len, const char *str,
  130. const struct qstr *name)
  131. {
  132. int guid = len - EFI_VARIABLE_GUID_LEN;
  133. /* Parallel lookups may produce a temporary invalid filename */
  134. if (guid <= 0)
  135. return 1;
  136. if (name->len != len)
  137. return 1;
  138. /* Case-sensitive compare for the variable name */
  139. if (memcmp(str, name->name, guid))
  140. return 1;
  141. /* Case-insensitive compare for the GUID */
  142. return strncasecmp(name->name + guid, str + guid, EFI_VARIABLE_GUID_LEN);
  143. }
  144. static int efivarfs_d_hash(const struct dentry *dentry, struct qstr *qstr)
  145. {
  146. unsigned long hash = init_name_hash(dentry);
  147. const unsigned char *s = qstr->name;
  148. unsigned int len = qstr->len;
  149. while (len-- > EFI_VARIABLE_GUID_LEN)
  150. hash = partial_name_hash(*s++, hash);
  151. /* GUID is case-insensitive. */
  152. while (len--)
  153. hash = partial_name_hash(tolower(*s++), hash);
  154. qstr->hash = end_name_hash(hash);
  155. return 0;
  156. }
  157. static const struct dentry_operations efivarfs_d_ops = {
  158. .d_compare = efivarfs_d_compare,
  159. .d_hash = efivarfs_d_hash,
  160. };
  161. static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
  162. {
  163. struct dentry *d;
  164. struct qstr q;
  165. int err;
  166. q.name = name;
  167. q.len = strlen(name);
  168. err = efivarfs_d_hash(parent, &q);
  169. if (err)
  170. return ERR_PTR(err);
  171. d = d_alloc(parent, &q);
  172. if (d)
  173. return d;
  174. return ERR_PTR(-ENOMEM);
  175. }
  176. bool efivarfs_variable_is_present(efi_char16_t *variable_name,
  177. efi_guid_t *vendor, void *data)
  178. {
  179. char *name = efivar_get_utf8name(variable_name, vendor);
  180. struct super_block *sb = data;
  181. struct dentry *dentry;
  182. if (!name)
  183. /*
  184. * If the allocation failed there'll already be an
  185. * error in the log (and likely a huge and growing
  186. * number of them since they system will be under
  187. * extreme memory pressure), so simply assume
  188. * collision for safety but don't add to the log
  189. * flood.
  190. */
  191. return true;
  192. dentry = try_lookup_noperm(&QSTR(name), sb->s_root);
  193. kfree(name);
  194. if (!IS_ERR_OR_NULL(dentry))
  195. dput(dentry);
  196. return dentry != NULL;
  197. }
  198. static int efivarfs_create_dentry(struct super_block *sb, efi_char16_t *name16,
  199. unsigned long name_size, efi_guid_t vendor,
  200. char *name)
  201. {
  202. struct efivar_entry *entry;
  203. struct inode *inode;
  204. struct dentry *dentry, *root = sb->s_root;
  205. unsigned long size = 0;
  206. int len;
  207. int err = -ENOMEM;
  208. bool is_removable = false;
  209. /* length of the variable name itself: remove GUID and separator */
  210. len = strlen(name) - EFI_VARIABLE_GUID_LEN - 1;
  211. if (efivar_variable_is_removable(vendor, name, len))
  212. is_removable = true;
  213. inode = efivarfs_get_inode(sb, d_inode(root), S_IFREG | 0644, 0,
  214. is_removable);
  215. if (!inode)
  216. goto fail_name;
  217. entry = efivar_entry(inode);
  218. memcpy(entry->var.VariableName, name16, name_size);
  219. memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
  220. dentry = efivarfs_alloc_dentry(root, name);
  221. if (IS_ERR(dentry)) {
  222. err = PTR_ERR(dentry);
  223. goto fail_inode;
  224. }
  225. __efivar_entry_get(entry, NULL, &size, NULL);
  226. /* copied by the above to local storage in the dentry. */
  227. kfree(name);
  228. inode_lock(inode);
  229. inode->i_private = entry;
  230. i_size_write(inode, size + sizeof(__u32)); /* attributes + data */
  231. inode_unlock(inode);
  232. d_make_persistent(dentry, inode);
  233. dput(dentry);
  234. return 0;
  235. fail_inode:
  236. iput(inode);
  237. fail_name:
  238. kfree(name);
  239. return err;
  240. }
  241. static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
  242. unsigned long name_size, void *data)
  243. {
  244. struct super_block *sb = (struct super_block *)data;
  245. char *name;
  246. if (guid_equal(&vendor, &LINUX_EFI_RANDOM_SEED_TABLE_GUID))
  247. return 0;
  248. name = efivar_get_utf8name(name16, &vendor);
  249. if (!name)
  250. return -ENOMEM;
  251. return efivarfs_create_dentry(sb, name16, name_size, vendor, name);
  252. }
  253. enum {
  254. Opt_uid, Opt_gid,
  255. };
  256. static const struct fs_parameter_spec efivarfs_parameters[] = {
  257. fsparam_uid("uid", Opt_uid),
  258. fsparam_gid("gid", Opt_gid),
  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. opt = fs_parse(fc, efivarfs_parameters, param, &result);
  268. if (opt < 0)
  269. return opt;
  270. switch (opt) {
  271. case Opt_uid:
  272. opts->uid = result.uid;
  273. break;
  274. case Opt_gid:
  275. opts->gid = result.gid;
  276. break;
  277. default:
  278. return -EINVAL;
  279. }
  280. return 0;
  281. }
  282. static int efivarfs_fill_super(struct super_block *sb, struct fs_context *fc)
  283. {
  284. struct efivarfs_fs_info *sfi = sb->s_fs_info;
  285. struct inode *inode = NULL;
  286. struct dentry *root;
  287. int err;
  288. sb->s_maxbytes = MAX_LFS_FILESIZE;
  289. sb->s_blocksize = PAGE_SIZE;
  290. sb->s_blocksize_bits = PAGE_SHIFT;
  291. sb->s_magic = EFIVARFS_MAGIC;
  292. sb->s_op = &efivarfs_ops;
  293. set_default_d_op(sb, &efivarfs_d_ops);
  294. sb->s_d_flags |= DCACHE_DONTCACHE;
  295. sb->s_time_gran = 1;
  296. if (!efivar_supports_writes())
  297. sb->s_flags |= SB_RDONLY;
  298. inode = efivarfs_get_inode(sb, NULL, S_IFDIR | 0755, 0, true);
  299. if (!inode)
  300. return -ENOMEM;
  301. inode->i_op = &efivarfs_dir_inode_operations;
  302. root = d_make_root(inode);
  303. sb->s_root = root;
  304. if (!root)
  305. return -ENOMEM;
  306. sfi->sb = sb;
  307. sfi->nb.notifier_call = efivarfs_ops_notifier;
  308. err = blocking_notifier_chain_register(&efivar_ops_nh, &sfi->nb);
  309. if (err)
  310. return err;
  311. return efivar_init(efivarfs_callback, sb, true);
  312. }
  313. static int efivarfs_get_tree(struct fs_context *fc)
  314. {
  315. return get_tree_single(fc, efivarfs_fill_super);
  316. }
  317. static int efivarfs_reconfigure(struct fs_context *fc)
  318. {
  319. if (!efivar_supports_writes() && !(fc->sb_flags & SB_RDONLY)) {
  320. pr_err("Firmware does not support SetVariableRT. Can not remount with rw\n");
  321. return -EINVAL;
  322. }
  323. return 0;
  324. }
  325. static void efivarfs_free(struct fs_context *fc)
  326. {
  327. kfree(fc->s_fs_info);
  328. }
  329. static const struct fs_context_operations efivarfs_context_ops = {
  330. .get_tree = efivarfs_get_tree,
  331. .parse_param = efivarfs_parse_param,
  332. .reconfigure = efivarfs_reconfigure,
  333. .free = efivarfs_free,
  334. };
  335. static int efivarfs_check_missing(efi_char16_t *name16, efi_guid_t vendor,
  336. unsigned long name_size, void *data)
  337. {
  338. char *name;
  339. struct super_block *sb = data;
  340. struct dentry *dentry;
  341. int err;
  342. if (guid_equal(&vendor, &LINUX_EFI_RANDOM_SEED_TABLE_GUID))
  343. return 0;
  344. name = efivar_get_utf8name(name16, &vendor);
  345. if (!name)
  346. return -ENOMEM;
  347. dentry = try_lookup_noperm(&QSTR(name), sb->s_root);
  348. if (IS_ERR(dentry)) {
  349. err = PTR_ERR(dentry);
  350. goto out;
  351. }
  352. if (!dentry) {
  353. /* found missing entry */
  354. pr_info("efivarfs: creating variable %s\n", name);
  355. return efivarfs_create_dentry(sb, name16, name_size, vendor, name);
  356. }
  357. dput(dentry);
  358. err = 0;
  359. out:
  360. kfree(name);
  361. return err;
  362. }
  363. static struct file_system_type efivarfs_type;
  364. static int efivarfs_freeze_fs(struct super_block *sb)
  365. {
  366. /* Nothing for us to do. */
  367. return 0;
  368. }
  369. static int efivarfs_unfreeze_fs(struct super_block *sb)
  370. {
  371. struct dentry *child = NULL;
  372. /*
  373. * Unconditionally resync the variable state on a thaw request.
  374. * Given the size of efivarfs it really doesn't matter to simply
  375. * iterate through all of the entries and resync. Freeze/thaw
  376. * requests are rare enough for that to not matter and the
  377. * number of entries is pretty low too. So we really don't care.
  378. */
  379. pr_info("efivarfs: resyncing variable state\n");
  380. for (;;) {
  381. int err;
  382. unsigned long size = 0;
  383. struct inode *inode;
  384. struct efivar_entry *entry;
  385. child = find_next_child(sb->s_root, child);
  386. if (!child)
  387. break;
  388. inode = d_inode(child);
  389. entry = efivar_entry(inode);
  390. err = efivar_entry_size(entry, &size);
  391. if (err)
  392. size = 0;
  393. else
  394. size += sizeof(__u32);
  395. inode_lock(inode);
  396. i_size_write(inode, size);
  397. inode_unlock(inode);
  398. /* The variable doesn't exist anymore, delete it. */
  399. if (!size) {
  400. pr_info("efivarfs: removing variable %pd\n", child);
  401. simple_recursive_removal(child, NULL);
  402. }
  403. }
  404. efivar_init(efivarfs_check_missing, sb, false);
  405. pr_info("efivarfs: finished resyncing variable state\n");
  406. return 0;
  407. }
  408. static int efivarfs_init_fs_context(struct fs_context *fc)
  409. {
  410. struct efivarfs_fs_info *sfi;
  411. if (!efivar_is_available())
  412. return -EOPNOTSUPP;
  413. sfi = kzalloc_obj(*sfi);
  414. if (!sfi)
  415. return -ENOMEM;
  416. sfi->mount_opts.uid = GLOBAL_ROOT_UID;
  417. sfi->mount_opts.gid = GLOBAL_ROOT_GID;
  418. fc->s_fs_info = sfi;
  419. fc->ops = &efivarfs_context_ops;
  420. return 0;
  421. }
  422. static void efivarfs_kill_sb(struct super_block *sb)
  423. {
  424. struct efivarfs_fs_info *sfi = sb->s_fs_info;
  425. blocking_notifier_chain_unregister(&efivar_ops_nh, &sfi->nb);
  426. kill_anon_super(sb);
  427. kfree(sfi);
  428. }
  429. static struct file_system_type efivarfs_type = {
  430. .owner = THIS_MODULE,
  431. .name = "efivarfs",
  432. .init_fs_context = efivarfs_init_fs_context,
  433. .kill_sb = efivarfs_kill_sb,
  434. .parameters = efivarfs_parameters,
  435. .fs_flags = FS_POWER_FREEZE,
  436. };
  437. static __init int efivarfs_init(void)
  438. {
  439. return register_filesystem(&efivarfs_type);
  440. }
  441. static __exit void efivarfs_exit(void)
  442. {
  443. unregister_filesystem(&efivarfs_type);
  444. }
  445. MODULE_AUTHOR("Matthew Garrett, Jeremy Kerr");
  446. MODULE_DESCRIPTION("EFI Variable Filesystem");
  447. MODULE_LICENSE("GPL");
  448. MODULE_ALIAS_FS("efivarfs");
  449. module_init(efivarfs_init);
  450. module_exit(efivarfs_exit);