xref: /linux/fs/efivarfs/inode.c (revision 5ff328836dfde0cef9f28c8b8791a90a36d7a183)
1 /*
2  * Copyright (C) 2012 Red Hat, Inc.
3  * Copyright (C) 2012 Jeremy Kerr <jeremy.kerr@canonical.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9 
10 #include <linux/efi.h>
11 #include <linux/fs.h>
12 #include <linux/ctype.h>
13 #include <linux/slab.h>
14 #include <linux/uuid.h>
15 
16 #include "internal.h"
17 
18 struct inode *efivarfs_get_inode(struct super_block *sb,
19 				const struct inode *dir, int mode,
20 				dev_t dev, bool is_removable)
21 {
22 	struct inode *inode = new_inode(sb);
23 
24 	if (inode) {
25 		inode->i_ino = get_next_ino();
26 		inode->i_mode = mode;
27 		inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
28 		inode->i_flags = is_removable ? 0 : S_IMMUTABLE;
29 		switch (mode & S_IFMT) {
30 		case S_IFREG:
31 			inode->i_fop = &efivarfs_file_operations;
32 			break;
33 		case S_IFDIR:
34 			inode->i_op = &efivarfs_dir_inode_operations;
35 			inode->i_fop = &simple_dir_operations;
36 			inc_nlink(inode);
37 			break;
38 		}
39 	}
40 	return inode;
41 }
42 
43 /*
44  * Return true if 'str' is a valid efivarfs filename of the form,
45  *
46  *	VariableName-12345678-1234-1234-1234-1234567891bc
47  */
48 bool efivarfs_valid_name(const char *str, int len)
49 {
50 	const char *s = str + len - EFI_VARIABLE_GUID_LEN;
51 
52 	/*
53 	 * We need a GUID, plus at least one letter for the variable name,
54 	 * plus the '-' separator
55 	 */
56 	if (len < EFI_VARIABLE_GUID_LEN + 2)
57 		return false;
58 
59 	/* GUID must be preceded by a '-' */
60 	if (*(s - 1) != '-')
61 		return false;
62 
63 	/*
64 	 * Validate that 's' is of the correct format, e.g.
65 	 *
66 	 *	12345678-1234-1234-1234-123456789abc
67 	 */
68 	return uuid_is_valid(s);
69 }
70 
71 static int efivarfs_create(struct inode *dir, struct dentry *dentry,
72 			  umode_t mode, bool excl)
73 {
74 	struct inode *inode = NULL;
75 	struct efivar_entry *var;
76 	int namelen, i = 0, err = 0;
77 	bool is_removable = false;
78 
79 	if (!efivarfs_valid_name(dentry->d_name.name, dentry->d_name.len))
80 		return -EINVAL;
81 
82 	var = kzalloc(sizeof(struct efivar_entry), GFP_KERNEL);
83 	if (!var)
84 		return -ENOMEM;
85 
86 	/* length of the variable name itself: remove GUID and separator */
87 	namelen = dentry->d_name.len - EFI_VARIABLE_GUID_LEN - 1;
88 
89 	err = guid_parse(dentry->d_name.name + namelen + 1, &var->var.VendorGuid);
90 	if (err)
91 		goto out;
92 
93 	if (efivar_variable_is_removable(var->var.VendorGuid,
94 					 dentry->d_name.name, namelen))
95 		is_removable = true;
96 
97 	inode = efivarfs_get_inode(dir->i_sb, dir, mode, 0, is_removable);
98 	if (!inode) {
99 		err = -ENOMEM;
100 		goto out;
101 	}
102 
103 	for (i = 0; i < namelen; i++)
104 		var->var.VariableName[i] = dentry->d_name.name[i];
105 
106 	var->var.VariableName[i] = '\0';
107 
108 	inode->i_private = var;
109 
110 	err = efivar_entry_add(var, &efivarfs_list);
111 	if (err)
112 		goto out;
113 
114 	d_instantiate(dentry, inode);
115 	dget(dentry);
116 out:
117 	if (err) {
118 		kfree(var);
119 		if (inode)
120 			iput(inode);
121 	}
122 	return err;
123 }
124 
125 static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
126 {
127 	struct efivar_entry *var = d_inode(dentry)->i_private;
128 
129 	if (efivar_entry_delete(var))
130 		return -EINVAL;
131 
132 	drop_nlink(d_inode(dentry));
133 	dput(dentry);
134 	return 0;
135 };
136 
137 const struct inode_operations efivarfs_dir_inode_operations = {
138 	.lookup = simple_lookup,
139 	.unlink = efivarfs_unlink,
140 	.create = efivarfs_create,
141 };
142