make efivarfs files immutable by default (for stable)
Subsystems (5)
cover make efivarfs files immutable by default (for stable)
Stable folks,
This is a backport of the efivarfs anti-bricking changes [1] for stable. Some fixing up was required because the series doesn't include the ucs2 cleanups that are in Linus' tree since they're not really stable material.
[1] - https://lkml.kernel.org/r/[email protected]
Matt Fleming (1):
efi: Add pstore variables to the deletion whitelist
Peter Jones (2):
efi: Make our variable validation list include the guid
efi: Make efivarfs entries immutable by default.
Documentation/filesystems/efivarfs.txt | 7 ++ drivers/firmware/efi/efivars.c | 4 +- drivers/firmware/efi/vars.c | 161 +++++++++++++++++++------ fs/efivarfs/file.c | 70 +++++++++++ fs/efivarfs/inode.c | 30 +++-- fs/efivarfs/internal.h | 3 +- fs/efivarfs/super.c | 9 +- include/linux/efi.h | 5 +- tools/testing/selftests/efivarfs/efivarfs.sh | 19 ++- tools/testing/selftests/efivarfs/open-unlink.c | 72 ++++++++++- 10 files changed, 320 insertions(+), 60 deletions(-)
Signature · 1 line
2.6.2
PATCH 1/3 efi: Make our variable validation list include the guid
From: Peter Jones <[email protected]>
commit 8282f5d9c17fe15a9e658c06e3f343efae1a2a2f upstream.
All the variables in this list so far are defined to be in the global namespace in the UEFI spec, so this just further ensures we're validating the variables we think we are.
Including the guid for entries will become more important in future patches when we decide whether or not to allow deletion of variables based on presence in this list.
--- drivers/firmware/efi/efivars.c | 4 ++-- drivers/firmware/efi/vars.c | 52 +++++++++++++++++++++++++++--------------- include/linux/efi.h | 3 ++- 3 files changed, 37 insertions(+), 22 deletions(-)
drivers/firmware/efi/efivars.c +2 -2
@@ -221,7 +221,7 @@ sanity_check(struct efi_variable *var, efi_char16_t *name, efi_guid_t vendor, }if ((attributes & ~EFI_VARIABLE_MASK ) !=0 || -efivar_validate (name, data, size) == false) { +efivar_validate (vendor, name, data, size) == false) {printk (KERN_ERR "efivars: Malformed variable content\n" );return -EINVAL ; }
@@ -447,7 +447,7 @@ static ssize_t efivar_create(struct file *filp, struct kobject *kobj, }if ((attributes & ~EFI_VARIABLE_MASK ) !=0 || -efivar_validate (name, data, size) == false) { +efivar_validate (new_var->VendorGuid, name, data, size) == false) {printk (KERN_ERR "efivars: Malformed variable content\n" );return -EINVAL ; }
drivers/firmware/efi/vars.c +33 -19
78 lines not on this page · raw message
include/linux/efi.h +2 -1
@@ -1199,7 +1199,8 @@ int efivar_entry_iter(int (*func)(struct efivar_entry *, void *),struct efivar_entry *efivar_entry_find (efi_char16_t *name,efi_guid_t guid,struct list_head *head,bool remove); -bool efivar_validate (efi_char16_t *var_name,u8 *data,unsigned long len); +bool efivar_validate (efi_guid_t vendor,efi_char16_t *var_name,u8 *data, +unsigned long len);extern struct work_struct efivar_work;void efivar_run_worker (void );
Signature · 1 line
2.6.2
PATCH 2/3 efi: Make efivarfs entries immutable by default.
In reply to Matt FlemingFrom: Peter Jones <[email protected]>
commit ed8b0de5a33d2a2557dce7f9429dca8cb5bc5879 upstream.
"rm -rf" is bricking some peoples' laptops because of variables being used to store non-reinitializable firmware driver data that's required to POST the hardware.
These are 100% bugs, and they need to be fixed, but in the mean time it shouldn't be easy to *accidentally* brick machines.
We have to have delete working, and picking which variables do and don't work for deletion is quite intractable, so instead make everything immutable by default (except for a whitelist), and make tools that aren't quite so broad-spectrum unset the immutable flag.
--- Documentation/filesystems/efivarfs.txt | 7 ++ drivers/firmware/efi/vars.c | 114 ++++++++++++++++++++----- fs/efivarfs/file.c | 70 +++++++++++++++ fs/efivarfs/inode.c | 30 ++++--- fs/efivarfs/internal.h | 3 +- fs/efivarfs/super.c | 9 +- include/linux/efi.h | 4 +- tools/testing/selftests/efivarfs/efivarfs.sh | 19 ++++- tools/testing/selftests/efivarfs/open-unlink.c | 72 +++++++++++++++- 9 files changed, 286 insertions(+), 42 deletions(-)
Documentation/filesystems/efivarfs.txt +7 -0
@@ -14,3 +14,10 @@ filesystem. efivarfs is typically mounted like this, mount -t efivarfs none /sys/firmware/efi/efivars + +Due to the presence of numerous firmware bugs where removing non-standard +UEFI variables causes the system firmware to fail to POST, efivarfs +files that are not well-known standardized variables are created +as immutable files. This doesn't prevent removal - "chattr -i" will work - +but it does prevent this kind of failure from being accomplished +accidentally.
drivers/firmware/efi/vars.c +91 -23
151 lines not on this page · raw message
fs/efivarfs/file.c +70 -0
85 lines not on this page · raw message
fs/efivarfs/inode.c +19 -11
@@ -15,7 +15,8 @@#include "internal.h" struct inode *efivarfs_get_inode (struct super_block *sb, -const struct inode *dir,int mode,dev_t dev) +const struct inode *dir,int mode, +dev_t dev,bool is_removable) {struct inode *inode =new_inode (sb);
@@ -23,6 +24,7 @@ struct inode *efivarfs_get_inode(struct super_block *sb, inode->i_ino =get_next_ino (); inode->i_mode = mode; inode->i_atime = inode->i_mtime = inode->i_ctime =CURRENT_TIME ; + inode->i_flags = is_removable ?0 :S_IMMUTABLE ;switch (mode &S_IFMT ) {case S_IFREG : inode->i_fop = &efivarfs_file_operations;
@@ -102,22 +104,17 @@ static void efivarfs_hex_to_guid(const char *str, efi_guid_t *guid)static int efivarfs_create (struct inode *dir,struct dentry *dentry,umode_t mode,bool excl) { -struct inode *inode; +struct inode *inode =NULL ;struct efivar_entry *var;int namelen, i =0 , err =0 ; +bool is_removable = false;if (!efivarfs_valid_name (dentry->d_name.name, dentry->d_name.len))return -EINVAL ; - inode =efivarfs_get_inode (dir->i_sb, dir, mode,0 ); -if (!inode) -return -ENOMEM ; - var =kzalloc (sizeof (struct efivar_entry ),GFP_KERNEL ); -if (!var) { - err = -ENOMEM ; - goto out; - } +if (!var) +return -ENOMEM ;/* length of the variable name itself: remove GUID and separator */ namelen = dentry->d_name.len -EFI_VARIABLE_GUID_LEN -1 ;
@@ -125,6 +122,16 @@ static int efivarfs_create(struct inode *dir, struct dentry *dentry,efivarfs_hex_to_guid (dentry->d_name.name + namelen +1 , &var->var.VendorGuid); +if (efivar_variable_is_removable (var->var.VendorGuid, + dentry->d_name.name, namelen)) + is_removable = true; + + inode =efivarfs_get_inode (dir->i_sb, dir, mode,0 , is_removable); +if (!inode) { + err = -ENOMEM ; + goto out; + } +for (i =0 ; i < namelen; i++) var->var.VariableName[i] = dentry->d_name.name[i];
@@ -138,7 +145,8 @@ static int efivarfs_create(struct inode *dir, struct dentry *dentry, out:if (err) {kfree (var); -iput (inode); +if (inode) +iput (inode); }return err; }
fs/efivarfs/internal.h +2 -1
@@ -15,7 +15,8 @@ extern const struct file_operations efivarfs_file_operations;extern const struct inode_operations efivarfs_dir_inode_operations;extern bool efivarfs_valid_name (const char *str,int len);extern struct inode *efivarfs_get_inode (struct super_block *sb, -const struct inode *dir,int mode,dev_t dev); +const struct inode *dir,int mode,dev_t dev, +bool is_removable);extern struct list_head efivarfs_list;
fs/efivarfs/super.c +7 -2
@@ -120,6 +120,7 @@ static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,char *name;int len, i;int err = -ENOMEM ; +bool is_removable = false; entry =kzalloc (sizeof (*entry),GFP_KERNEL );if (!entry)
@@ -138,13 +139,17 @@ static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,for (i =0 ; i < len; i++) name[i] = entry->var.VariableName[i] &0xFF ; +if (efivar_variable_is_removable (entry->var.VendorGuid, name, len)) + is_removable = true; + name[len] ='-' ;efi_guid_to_str (&entry->var.VendorGuid, name + len +1 ); name[len +EFI_VARIABLE_GUID_LEN +1 ] ='\0' ; - inode =efivarfs_get_inode (sb,d_inode (root),S_IFREG |0644 ,0 ); + inode =efivarfs_get_inode (sb,d_inode (root),S_IFREG |0644 ,0 , + is_removable);if (!inode) goto fail_name;
@@ -200,7 +205,7 @@ static int efivarfs_fill_super(struct super_block *sb, void *data, int silent) sb->s_d_op = &efivarfs_d_ops; sb->s_time_gran =1 ; - inode =efivarfs_get_inode (sb,NULL ,S_IFDIR |0755 ,0 ); + inode =efivarfs_get_inode (sb,NULL ,S_IFDIR |0755 ,0 , true);if (!inode)return -ENOMEM ; inode->i_op = &efivarfs_dir_inode_operations;
include/linux/efi.h +3 -1
@@ -1200,7 +1200,9 @@ struct efivar_entry *efivar_entry_find(efi_char16_t *name, efi_guid_t guid, struct list_head *head, bool remove);bool efivar_validate (efi_guid_t vendor,efi_char16_t *var_name,u8 *data, -unsigned long len); +unsigned long data_size); +bool efivar_variable_is_removable (efi_guid_t vendor,const char *name, +size_t len);extern struct work_struct efivar_work;void efivar_run_worker (void );
tools/testing/selftests/efivarfs/efivarfs.sh +16 -3
@@ -88,7 +88,11 @@ test_delete() exit 1 fi - rm $file + rm $file 2>/dev/null + if [ $? -ne 0 ]; then + chattr -i $file + rm $file + fi if [ -e $file ]; then echo "$file couldn't be deleted" >&2
@@ -111,6 +115,7 @@ test_zero_size_delete() exit 1 fi + chattr -i $file printf "$attrs" > $file if [ -e $file ]; then
@@ -141,7 +146,11 @@ test_valid_filenames() echo "$file could not be created" >&2 ret=1 else - rm $file + rm $file 2>/dev/null + if [ $? -ne 0 ]; then + chattr -i $file + rm $file + fi fi done
@@ -174,7 +183,11 @@ test_invalid_filenames() if [ -e $file ]; then echo "Creating $file should have failed" >&2 - rm $file + rm $file 2>/dev/null + if [ $? -ne 0 ]; then + chattr -i $file + rm $file + fi ret=1 fi done
tools/testing/selftests/efivarfs/open-unlink.c +71 -1
94 lines not on this page · raw message
Signature · 1 line
2.6.2
PATCH 3/3 efi: Add pstore variables to the deletion whitelist
In reply to Matt Flemingcommit e246eb568bc4cbbdd8a30a3c11151ff9b7ca7312 upstream.
Laszlo explains why this is a good idea,
'This is because the pstore filesystem can be backed by UEFI variables,
and (for example) a crash might dump the last kilobytes of the dmesg into a number of pstore entries, each entry backed by a separate UEFI variable in the above GUID namespace, and with a variable name according to the above pattern.
Please see "drivers/firmware/efi/efi-pstore.c".
While this patch series will not prevent the user from deleting those UEFI variables via the pstore filesystem (i.e., deleting a pstore fs entry will continue to delete the backing UEFI variable), I think it would be nice to preserve the possibility for the sysadmin to delete Linux-created UEFI variables that carry portions of the crash log, *without* having to mount the pstore filesystem.'
There's also no chance of causing machines to become bricked by deleting these variables, which is the whole purpose of excluding things from the whitelist.
Use the LINUX_EFI_CRASH_GUID guid and a wildcard '*' for the match so that we don't have to update the string in the future if new variable name formats are created for crash dump variables.
--- drivers/firmware/efi/vars.c | 1 + 1 file changed, 1 insertion(+)
drivers/firmware/efi/vars.c +1 -0
@@ -198,6 +198,7 @@ static const struct variable_validate variable_validate[] = { { EFI_GLOBAL_VARIABLE_GUID, "OsIndications", NULL }, { EFI_GLOBAL_VARIABLE_GUID, "PlatformLang", validate_ascii_string }, { EFI_GLOBAL_VARIABLE_GUID, "Timeout", validate_uint16 }, + { LINUX_EFI_CRASH_GUID, "*", NULL }, { NULL_GUID, "", NULL }, };
Signature · 1 line
2.6.2
Greg KH
maintainerQuoted 6 lines · Matt Fleming
Stable folks,
This is a backport of the efivarfs anti-bricking changes [1] for stable. Some fixing up was required because the series doesn't include the ucs2 cleanups that are in Linus' tree since they're not really stable material.
What stable tree(s) do you want these applied to?
And if at all possible, I'd really prefer to take the original patches, additional things and all, for stable kernels, as 95%[1] of the time that we take "different" ones, there are bugs.
It also makes things easier for us to take additional changes later on for other commits in the same area. So can we just take the originals please?
thanks,
greg k-h
[1] Totally made up number, I think the true number is 100%, but maybe one or two patches have actually worked over the years. Most relevant example of this is some crypto patches that were "made simpler for stable" last week but were completely broken.
Quoted 9 lines · Greg KH
Quoted 6 lines · Matt Fleming
Stable folks,
This is a backport of the efivarfs anti-bricking changes [1] for stable. Some fixing up was required because the series doesn't include the ucs2 cleanups that are in Linus' tree since they're not really stable material.
What stable tree(s) do you want these applied to?
Probably as far as back v3.10, if possible. That was when efivarfs support was merged and had we known about this issue then, we would have had this immutable feature from day one.
Quoted 3 lines
And if at all possible, I'd really prefer to take the original patches, additional things and all, for stable kernels, as 95%[1] of the time that we take "different" ones, there are bugs.
OK, gotcha.
Quoted 3 lines
It also makes things easier for us to take additional changes later on for other commits in the same area. So can we just take the originals please?
Sure, if that's what you'd prefer that's no problem. Would you like me to send a v2 series containing all the patches?
Quoted 8 lines
thanks,
greg k-h
[1] Totally made up number, I think the true number is 100%, but maybe one or two patches have actually worked over the years. Most relevant example of this is some crypto patches that were "made simpler for stable" last week but were completely broken.
Greg KH
maintainerQuoted 19 lines · Matt Fleming
Quoted 9 lines · Greg KH
Quoted 6 lines · Matt Fleming
Stable folks,
This is a backport of the efivarfs anti-bricking changes [1] for stable. Some fixing up was required because the series doesn't include the ucs2 cleanups that are in Linus' tree since they're not really stable material.
What stable tree(s) do you want these applied to?
Probably as far as back v3.10, if possible. That was when efivarfs support was merged and had we known about this issue then, we would have had this immutable feature from day one.
Quoted 3 lines
And if at all possible, I'd really prefer to take the original patches, additional things and all, for stable kernels, as 95%[1] of the time that we take "different" ones, there are bugs.
Quoted 8 lines
OK, gotcha.
Quoted 3 lines
It also makes things easier for us to take additional changes later on for other commits in the same area. So can we just take the originals please?
Sure, if that's what you'd prefer that's no problem. Would you like me to send a v2 series containing all the patches?
Yes, that would be great, or just a list of git commit ids if they all apply just fine without any changes needed.
thanks,
greg k-h
Quoted 3 lines · Greg KH
Yes, that would be great, or just a list of git commit ids if they all apply just fine without any changes needed.
Unfortunately one of the patches does requiring fixing up to apply against v4.4 and earlier. I'll send out a new series.