Skip to contentSkip to conversation

EFI urgent fixes

GIT PULL In review Matt Fleming 5 participants 14 messages Feb 12, 2016 – Feb 18, 2016 2 messages not in the archive

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

Folks,

Please pull the following EFI patches from Peter that prevent accidental deletion of EFI variables through efivarfs which can lead to bricked machines.

These obviously need backporting to stable, so I'll take care of sending the backports separately because we don't need to send the entire 5 patch series.

The following changes since commit 59fd1214561921343305a0e9dc218bf3d40068f3:

x86/mm/numa: Fix 32-bit memblock range truncation bug on 32-bit NUMA kernels (2016-02-08 12:10:03 +0100)

are available in the git repository at:

git://git.kernel.org/pub/scm/linux/kernel/git/mfleming/efi.git tags/efi-urgent

for you to fetch changes up to ed8b0de5a33d2a2557dce7f9429dca8cb5bc5879:

efi: Make efivarfs entries immutable by default (2016-02-10 16:25:52 +0000)

----------------------------------------------------------------
* Prevent accidental deletion of EFI variables through efivarfs that may brick machines. We use a whitelist of known-safe variables to allow things like installing distributions to work out of the box, and instead restrict vendor-specific variable deletion by making non-whitelist variables immutable - Peter Jones

----------------------------------------------------------------
Peter Jones (5):
lib/ucs2_string: Add ucs2 -> utf8 helper functions
efi: Use ucs2_as_utf8 in efivarfs instead of open coding a bad version
efi: Do variable name validation tests in utf8
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                 |  35 +++---
 drivers/firmware/efi/vars.c                    | 143 ++++++++++++++++++-------
 fs/efivarfs/file.c                             |  70 ++++++++++++
 fs/efivarfs/inode.c                            |  30 ++++--
 fs/efivarfs/internal.h                         |   3 +-
 fs/efivarfs/super.c                            |  16 +--
 include/linux/efi.h                            |   5 +-
 include/linux/ucs2_string.h                    |   4 +
 lib/ucs2_string.c                              |  62 +++++++++++
 tools/testing/selftests/efivarfs/efivarfs.sh   |  19 +++-
 tools/testing/selftests/efivarfs/open-unlink.c |  72 ++++++++++++-
 12 files changed, 383 insertions(+), 83 deletions(-)
#1 lore raw

PATCH 1/5 lib/ucs2_string: Add ucs2 -> utf8 helper functions

From: Peter Jones <[email protected]>

This adds ucs2_utf8size(), which tells us how big our ucs2 string is in bytes, and ucs2_as_utf8, which translates from ucs2 to utf8..

---
 include/linux/ucs2_string.h |  4 +++
 lib/ucs2_string.c           | 62 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+)
include/linux/ucs2_string.h +4 -0
index cbb20afdbc01..bb679b48f408 100644
@@ -11,4 +11,8 @@ unsigned long ucs2_strlen(const ucs2_char_t *s);
 unsigned long ucs2_strsize(const ucs2_char_t *data, unsigned long maxlength);
 int ucs2_strncmp(const ucs2_char_t *a, const ucs2_char_t *b, size_t len);
 
+unsigned long ucs2_utf8size(const ucs2_char_t *src);
+unsigned long ucs2_as_utf8(u8 *dest, const ucs2_char_t *src,
+			   unsigned long maxlength);
+
 #endif /* _LINUX_UCS2_STRING_H_ */
lib/ucs2_string.c +62 -0

65 lines not on this page · raw message

Signature · 1 line
2.6.2
#2 lore raw

PATCH 2/5 efi: Use ucs2_as_utf8 in efivarfs instead of open coding a bad version

In reply to Matt Fleming

From: Peter Jones <[email protected]>

Translate EFI's UCS-2 variable names to UTF-8 instead of just assuming all variable names fit in ASCII.

---
 drivers/firmware/efi/efivars.c | 30 +++++++++++-------------------
 fs/efivarfs/super.c            |  7 +++----
 2 files changed, 14 insertions(+), 23 deletions(-)
drivers/firmware/efi/efivars.c +11 -19
index 756eca8c4cf8..f4ff8abc5f3e 100644
@@ -540,38 +540,30 @@ static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,
 static int
 efivar_create_sysfs_entry(struct efivar_entry *new_var)
 {
-	int i, short_name_size;
+	int short_name_size;
 	char *short_name;
-	unsigned long variable_name_size;
-	efi_char16_t *variable_name;
+	unsigned long utf8_name_size;
+	efi_char16_t *variable_name = new_var->var.VariableName;
 	int ret;
 
-	variable_name = new_var->var.VariableName;
-	variable_name_size = ucs2_strlen(variable_name) * sizeof(efi_char16_t);
-
 	/*
-	 * Length of the variable bytes in ASCII, plus the '-' separator,
+	 * Length of the variable bytes in UTF8, plus the '-' separator,
 	 * plus the GUID, plus trailing NUL
 	 */
-	short_name_size = variable_name_size / sizeof(efi_char16_t)
-				+ 1 + EFI_VARIABLE_GUID_LEN + 1;
-
-	short_name = kzalloc(short_name_size, GFP_KERNEL);
+	utf8_name_size = ucs2_utf8size(variable_name);
+	short_name_size = utf8_name_size + 1 + EFI_VARIABLE_GUID_LEN + 1;
 
+	short_name = kmalloc(short_name_size, GFP_KERNEL);
 	if (!short_name)
 		return -ENOMEM;
 
-	/* Convert Unicode to normal chars (assume top bits are 0),
-	   ala UTF-8 */
-	for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++) {
-		short_name[i] = variable_name[i] & 0xFF;
-	}
+	ucs2_as_utf8(short_name, variable_name, short_name_size);
+
 	/* This is ugly, but necessary to separate one vendor's
 	   private variables from another's.         */
-
-	*(short_name + strlen(short_name)) = '-';
+	short_name[utf8_name_size] = '-';
 	efi_guid_to_str(&new_var->var.VendorGuid,
-			 short_name + strlen(short_name));
+			 short_name + utf8_name_size + 1);
 
 	new_var->kobj.kset = efivars_kset;
 
fs/efivarfs/super.c +3 -4
index b8a564f29107..8651ac28ec0d 100644
@@ -118,7 +118,7 @@ static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
 	struct dentry *dentry, *root = sb->s_root;
 	unsigned long size = 0;
 	char *name;
-	int len, i;
+	int len;
 	int err = -ENOMEM;
 
 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
@@ -128,15 +128,14 @@ static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
 	memcpy(entry->var.VariableName, name16, name_size);
 	memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
 
-	len = ucs2_strlen(entry->var.VariableName);
+	len = ucs2_utf8size(entry->var.VariableName);
 
 	/* name, plus '-', plus GUID, plus NUL*/
 	name = kmalloc(len + 1 + EFI_VARIABLE_GUID_LEN + 1, GFP_KERNEL);
 	if (!name)
 		goto fail;
 
-	for (i = 0; i < len; i++)
-		name[i] = entry->var.VariableName[i] & 0xFF;
+	ucs2_as_utf8(name, entry->var.VariableName, len);
 
 	name[len] = '-';
 
Signature · 1 line
2.6.2
#8 lore raw

PATCH 3/5 efi: Do variable name validation tests in utf8

In reply to Matt Fleming

From: Peter Jones <[email protected]>

Actually translate from ucs2 to utf8 before doing the test, and then test against our other utf8 data, instead of fudging it.

---
 drivers/firmware/efi/vars.c | 32 +++++++++++++++++++++-----------
 1 file changed, 21 insertions(+), 11 deletions(-)
drivers/firmware/efi/vars.c +21 -11
index 70a0fb10517f..5c5fde3e6c37 100644
@@ -189,10 +189,19 @@ static const struct variable_validate variable_validate[] = {
 };
 
 bool
-efivar_validate(efi_char16_t *var_name, u8 *data, unsigned long len)
+efivar_validate(efi_char16_t *var_name, u8 *data, unsigned long data_size)
 {
 	int i;
-	u16 *unicode_name = var_name;
+	unsigned long utf8_size;
+	u8 *utf8_name;
+
+	utf8_size = ucs2_utf8size(var_name);
+	utf8_name = kmalloc(utf8_size + 1, GFP_KERNEL);
+	if (!utf8_name)
+		return false;
+
+	ucs2_as_utf8(utf8_name, var_name, utf8_size);
+	utf8_name[utf8_size] = '\0';
 
 	for (i = 0; variable_validate[i].validate != NULL; i++) {
 		const char *name = variable_validate[i].name;
@@ -200,28 +209,29 @@ efivar_validate(efi_char16_t *var_name, u8 *data, unsigned long len)
 
 		for (match = 0; ; match++) {
 			char c = name[match];
-			u16 u = unicode_name[match];
-
-			/* All special variables are plain ascii */
-			if (u > 127)
-				return true;
+			char u = utf8_name[match];
 
 			/* Wildcard in the matching name means we've matched */
-			if (c == '*')
+			if (c == '*') {
+				kfree(utf8_name);
 				return variable_validate[i].validate(var_name,
-							     match, data, len);
+							match, data, data_size);
+			}
 
 			/* Case sensitive match */
 			if (c != u)
 				break;
 
 			/* Reached the end of the string while matching */
-			if (!c)
+			if (!c) {
+				kfree(utf8_name);
 				return variable_validate[i].validate(var_name,
-							     match, data, len);
+							match, data, data_size);
+			}
 		}
 	}
 
+	kfree(utf8_name);
 	return true;
 }
 EXPORT_SYMBOL_GPL(efivar_validate);
Signature · 1 line
2.6.2
#9 lore raw

PATCH 4/5 efi: Make our variable validation list include the guid

In reply to Matt Fleming

From: Peter Jones <[email protected]>

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 |  5 ++--
 drivers/firmware/efi/vars.c    | 52 +++++++++++++++++++++++++++---------------
 include/linux/efi.h            |  3 ++-
 3 files changed, 38 insertions(+), 22 deletions(-)
drivers/firmware/efi/efivars.c +3 -2
index f4ff8abc5f3e..10e6774ab2a2 100644
@@ -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,8 @@ 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

80 lines not on this page · raw message

include/linux/efi.h +2 -1
index 569b5a866bb1..16ca611aabc8 100644
@@ -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 data_size);
 
 extern struct work_struct efivar_work;
 void efivar_run_worker(void);
Signature · 1 line
2.6.2
#10 lore raw

PATCH 5/5 efi: Make efivarfs entries immutable by default

In reply to Matt Fleming

From: Peter Jones <[email protected]>

"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                    | 87 +++++++++++++++++++-------
 fs/efivarfs/file.c                             | 70 +++++++++++++++++++++
 fs/efivarfs/inode.c                            | 30 +++++----
 fs/efivarfs/internal.h                         |  3 +-
 fs/efivarfs/super.c                            |  9 ++-
 include/linux/efi.h                            |  2 +
 tools/testing/selftests/efivarfs/efivarfs.sh   | 19 +++++-
 tools/testing/selftests/efivarfs/open-unlink.c | 72 ++++++++++++++++++++-
 9 files changed, 258 insertions(+), 41 deletions(-)
Documentation/filesystems/efivarfs.txt +7 -0
index c477af086e65..686a64bba775 100644
@@ -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 +64 -23

120 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
index 3381b9da9ee6..e2ab6d0497f2 100644
@@ -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
index b5ff16addb7c..b4505188e799 100644
@@ -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
index 8651ac28ec0d..dd029d13ea61 100644
@@ -120,6 +120,7 @@ static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
 	char *name;
 	int len;
 	int err = -ENOMEM;
+	bool is_removable = false;
 
 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
 	if (!entry)
@@ -137,13 +138,17 @@ static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,
 
 	ucs2_as_utf8(name, entry->var.VariableName, len);
 
+	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;
 
@@ -199,7 +204,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 +2 -0
index 16ca611aabc8..47be3ad7d3e5 100644
@@ -1201,6 +1201,8 @@ struct efivar_entry *efivar_entry_find(efi_char16_t *name, efi_guid_t guid,
 
 bool efivar_validate(efi_guid_t vendor, efi_char16_t *var_name, u8 *data,
 		     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
index 77edcdcc016b..057278448515 100755
@@ -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
#11 lore raw

Re: [PATCH 5/5] efi: Make efivarfs entries immutable by default

Quoted 18 lines · Matt Fleming

From: Peter Jones <[email protected]>

"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.

Quoted 11 lines
---
 Documentation/filesystems/efivarfs.txt         |  7 +++
 drivers/firmware/efi/vars.c                    | 87 +++++++++++++++++++-------
 fs/efivarfs/file.c                             | 70 +++++++++++++++++++++
 fs/efivarfs/inode.c                            | 30 +++++----
 fs/efivarfs/internal.h                         |  3 +-
 fs/efivarfs/super.c                            |  9 ++-
 include/linux/efi.h                            |  2 +
 tools/testing/selftests/efivarfs/efivarfs.sh   | 19 +++++-
 tools/testing/selftests/efivarfs/open-unlink.c | 72 ++++++++++++++++++++-
 9 files changed, 258 insertions(+), 41 deletions(-)

Folks, please hold off on merging this patch as Laszlo has raised a good point about including the efi-pstore variables in the whitelist.

#12 lore raw
In reply to Matt Fleming
Quoted 37 lines · Matt Fleming

Folks,

Please pull the following EFI patches from Peter that prevent accidental deletion of EFI variables through efivarfs which can lead to bricked machines.

These obviously need backporting to stable, so I'll take care of sending the backports separately because we don't need to send the entire 5 patch series.

The following changes since commit 59fd1214561921343305a0e9dc218bf3d40068f3:

x86/mm/numa: Fix 32-bit memblock range truncation bug on 32-bit NUMA kernels (2016-02-08 12:10:03 +0100)

are available in the git repository at:

git://git.kernel.org/pub/scm/linux/kernel/git/mfleming/efi.git tags/efi-urgent

for you to fetch changes up to ed8b0de5a33d2a2557dce7f9429dca8cb5bc5879:

efi: Make efivarfs entries immutable by default (2016-02-10 16:25:52 +0000)

----------------------------------------------------------------
* Prevent accidental deletion of EFI variables through efivarfs that may brick machines. We use a whitelist of known-safe variables to allow things like installing distributions to work out of the box, and instead restrict vendor-specific variable deletion by making non-whitelist variables immutable - Peter Jones

----------------------------------------------------------------
Peter Jones (5):
lib/ucs2_string: Add ucs2 -> utf8 helper functions
efi: Use ucs2_as_utf8 in efivarfs instead of open coding a bad version
efi: Do variable name validation tests in utf8
efi: Make our variable validation list include the guid
efi: Make efivarfs entries immutable by default

Quoted 13 lines
 Documentation/filesystems/efivarfs.txt         |   7 ++
 drivers/firmware/efi/efivars.c                 |  35 +++---
 drivers/firmware/efi/vars.c                    | 143 ++++++++++++++++++-------
 fs/efivarfs/file.c                             |  70 ++++++++++++
 fs/efivarfs/inode.c                            |  30 ++++--
 fs/efivarfs/internal.h                         |   3 +-
 fs/efivarfs/super.c                            |  16 +--
 include/linux/efi.h                            |   5 +-
 include/linux/ucs2_string.h                    |   4 +
 lib/ucs2_string.c                              |  62 +++++++++++
 tools/testing/selftests/efivarfs/efivarfs.sh   |  19 +++-
 tools/testing/selftests/efivarfs/open-unlink.c |  72 ++++++++++++-
 12 files changed, 383 insertions(+), 83 deletions(-)

Pulled, thanks Matt!

Ingo

#13 lore raw
Quoted 2 lines · Ingo Molnar

Pulled, thanks Matt!

Thanks a lot Ingo.

I've actually got a couple of stragglers that came before the weekend that fix bugs. It would be good to pick them up before you send a pull request to Linus.

I'll send out a second pull request momentarily.

#14 lore raw
Quoted 11 lines · Matt Fleming
Quoted 2 lines · Ingo Molnar

Pulled, thanks Matt!

Thanks a lot Ingo.

I've actually got a couple of stragglers that came before the weekend that fix bugs. It would be good to pick them up before you send a pull request to Linus.

I'll send out a second pull request momentarily.

I've pulled it all, so tip:x86/urgent should have everything needed. Please let me know if anything is amiss - I intend to send the fixes to Linus tomorrow-ish.

Thanks,

Ingo

#15 lore raw
Quoted 3 lines · Ingo Molnar

I've pulled it all, so tip:x86/urgent should have everything needed. Please let me know if anything is amiss - I intend to send the fixes to Linus tomorrow-ish.

Looks good. Thanks Ingo.

#3 lore raw

Re: [PATCH 2/5] efi: Use ucs2_as_utf8 in efivarfs instead of open coding a bad version

In reply to Matt Fleming
Quoted 95 lines · Matt Fleming

From: Peter Jones <[email protected]>

Translate EFI's UCS-2 variable names to UTF-8 instead of just assuming all variable names fit in ASCII.

---
drivers/firmware/efi/efivars.c | 30 +++++++++++-------------------
fs/efivarfs/super.c | 7 +++----

2 files changed, 14 insertions(+), 23 deletions(-)
drivers/firmware/efi/efivars.c +0 -0
index 756eca8c4cf8..f4ff8abc5f3e 100644
@@ -540,38 +540,30 @@ static ssize_t efivar_delete(struct file *filp,
 struct kobject *kobj,

static int
efivar_create_sysfs_entry(struct efivar_entry *new_var) {

drivers/firmware/efi/efivars.c +1 -1
-	int i, short_name_size;
+	int short_name_size;
	char *short_name;
drivers/firmware/efi/efivars.c +2 -2
-	unsigned long variable_name_size;
-	efi_char16_t *variable_name;
+	unsigned long utf8_name_size;
+	efi_char16_t *variable_name = new_var->var.VariableName;
	int ret;
drivers/firmware/efi/efivars.c +0 -2
-	variable_name = new_var->var.VariableName;
-	variable_name_size = ucs2_strlen(variable_name) *

sizeof(efi_char16_t);
-

	/*
drivers/firmware/efi/efivars.c +1 -1
-	 * Length of the variable bytes in ASCII, plus the '-' separator,
+	 * Length of the variable bytes in UTF8, plus the '-' separator,
	 * plus the GUID, plus trailing NUL
	 */
drivers/firmware/efi/efivars.c +3 -4
-	short_name_size = variable_name_size / sizeof(efi_char16_t)
-				+ 1 + EFI_VARIABLE_GUID_LEN + 1;
-
-	short_name = kzalloc(short_name_size, GFP_KERNEL);
+	utf8_name_size = ucs2_utf8size(variable_name);
+	short_name_size = utf8_name_size + 1 + EFI_VARIABLE_GUID_LEN + 1;
 
+	short_name = kmalloc(short_name_size, GFP_KERNEL);
	if (!short_name)
		return -ENOMEM;
drivers/firmware/efi/efivars.c +0 -3
-	/* Convert Unicode to normal chars (assume top bits are 0),
-	   ala UTF-8 */
-	for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++)

{

drivers/firmware/efi/efivars.c +2 -2
-		short_name[i] = variable_name[i] & 0xFF;
-	}
+	ucs2_as_utf8(short_name, variable_name, short_name_size);
+
	/* This is ugly, but necessary to separate one vendor's
	   private variables from another's.         */
drivers/firmware/efi/efivars.c +1 -2
-
-	*(short_name + strlen(short_name)) = '-';
+	short_name[utf8_name_size] = '-';
	efi_guid_to_str(&new_var->var.VendorGuid,
drivers/firmware/efi/efivars.c +1 -1
-			 short_name + strlen(short_name));
+			 short_name + utf8_name_size + 1);
	new_var->kobj.kset = efivars_kset;
fs/efivarfs/super.c +1 -1
index b8a564f29107..8651ac28ec0d 100644
@@ -118,7 +118,7 @@ static int efivarfs_callback(efi_char16_t *name16,
 efi_guid_t vendor,
 	struct dentry *dentry, *root = sb->s_root;
 	unsigned long size = 0;
 	char *name;
-	int len, i;
+	int len;
 	int err = -ENOMEM;
 
	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
fs/efivarfs/super.c +1 -1
@@ -128,15 +128,14 @@ static int efivarfs_callback(efi_char16_t
 *name16, efi_guid_t vendor,
 	memcpy(entry->var.VariableName, name16, name_size);
 	memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
 
-	len = ucs2_strlen(entry->var.VariableName);
+	len = ucs2_utf8size(entry->var.VariableName);
 

fs/efivarfs/super.c static int efivarfs_callback(efi_char16_t · line 141

Quoted 10 lines
fs/efivarfs/super.c +1 -2
 	/* name, plus '-', plus GUID, plus NUL*/
 	name = kmalloc(len + 1 + EFI_VARIABLE_GUID_LEN + 1, GFP_KERNEL);
 	if (!name)
 		goto fail;
 
-	for (i = 0; i < len; i++)
-		name[i] = entry->var.VariableName[i] & 0xFF;
+	ucs2_as_utf8(name, entry->var.VariableName, len);
 
 	name[len] = '-';

However, I think we should treat this "ucs2" as utf16, because sooner or later someone will enter utf16 characters.

Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse brevity and formatting.
#5 lore raw

Re: [PATCH 2/5] efi: Use ucs2_as_utf8 in efivarfs instead of open coding a bad version

In reply to a message not in the archive

If we're worried about UTF-16, the appropriate thing for us to do is error on seeing a surrogate pair.

Quoted 110 lines · H. Peter Anvin
Quoted 95 lines · Matt Fleming

From: Peter Jones <[email protected]>

Translate EFI's UCS-2 variable names to UTF-8 instead of just assuming all variable names fit in ASCII.

---
drivers/firmware/efi/efivars.c | 30 +++++++++++-------------------
fs/efivarfs/super.c | 7 +++----

2 files changed, 14 insertions(+), 23 deletions(-)
drivers/firmware/efi/efivars.c +0 -0
index 756eca8c4cf8..f4ff8abc5f3e 100644
@@ -540,38 +540,30 @@ static ssize_t efivar_delete(struct file *filp,
 struct kobject *kobj,

static int
efivar_create_sysfs_entry(struct efivar_entry *new_var) {

drivers/firmware/efi/efivars.c +3 -5
-      int i, short_name_size;
+      int short_name_size;
      char *short_name;
-      unsigned long variable_name_size;
-      efi_char16_t *variable_name;
+      unsigned long utf8_name_size;
+      efi_char16_t *variable_name = new_var->var.VariableName;
      int ret;
 
-      variable_name = new_var->var.VariableName;
-      variable_name_size = ucs2_strlen(variable_name) *

sizeof(efi_char16_t);

drivers/firmware/efi/efivars.c +4 -9
-
      /*
-       * Length of the variable bytes in ASCII, plus the '-' separator,
+       * Length of the variable bytes in UTF8, plus the '-' separator,
       * plus the GUID, plus trailing NUL
       */
-      short_name_size = variable_name_size / sizeof(efi_char16_t)
-                              + 1 + EFI_VARIABLE_GUID_LEN + 1;
-
-      short_name = kzalloc(short_name_size, GFP_KERNEL);
+      utf8_name_size = ucs2_utf8size(variable_name);
+      short_name_size = utf8_name_size + 1 + EFI_VARIABLE_GUID_LEN + 1;
 
+      short_name = kmalloc(short_name_size, GFP_KERNEL);
      if (!short_name)
              return -ENOMEM;
 
-      /* Convert Unicode to normal chars (assume top bits are 0),
-         ala UTF-8 */
-      for (i=0; i < (int)(variable_name_size / sizeof(efi_char16_t)); i++)

{

drivers/firmware/efi/efivars.c +4 -5
-              short_name[i] = variable_name[i] & 0xFF;
-      }
+      ucs2_as_utf8(short_name, variable_name, short_name_size);
+
      /* This is ugly, but necessary to separate one vendor's
         private variables from another's.         */
-
-      *(short_name + strlen(short_name)) = '-';
+      short_name[utf8_name_size] = '-';
      efi_guid_to_str(&new_var->var.VendorGuid,
-                       short_name + strlen(short_name));
+                       short_name + utf8_name_size + 1);
 
      new_var->kobj.kset = efivars_kset;
fs/efivarfs/super.c +1 -1
index b8a564f29107..8651ac28ec0d 100644
@@ -118,7 +118,7 @@ static int efivarfs_callback(efi_char16_t *name16,
 efi_guid_t vendor,
      struct dentry *dentry, *root = sb->s_root;
      unsigned long size = 0;
      char *name;
-      int len, i;
+      int len;
      int err = -ENOMEM;
 
      entry = kzalloc(sizeof(*entry), GFP_KERNEL);
fs/efivarfs/super.c +1 -1
@@ -128,15 +128,14 @@ static int efivarfs_callback(efi_char16_t
 *name16, efi_guid_t vendor,
      memcpy(entry->var.VariableName, name16, name_size);
      memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
 
-      len = ucs2_strlen(entry->var.VariableName);
+      len = ucs2_utf8size(entry->var.VariableName);
 

fs/efivarfs/super.c static int efivarfs_callback(efi_char16_t · line 141

Quoted 10 lines
fs/efivarfs/super.c +1 -2
      /* name, plus '-', plus GUID, plus NUL*/
      name = kmalloc(len + 1 + EFI_VARIABLE_GUID_LEN + 1, GFP_KERNEL);
      if (!name)
              goto fail;
 
-      for (i = 0; i < len; i++)
-              name[i] = entry->var.VariableName[i] & 0xFF;
+      ucs2_as_utf8(name, entry->var.VariableName, len);
 
      name[len] = '-';

However, I think we should treat this "ucs2" as utf16, because sooner or later someone will enter utf16 characters. --
Sent from my Android device with K-9 Mail. Please excuse brevity and formatting.

#7 lore raw

Re: [PATCH 2/5] efi: Use ucs2_as_utf8 in efivarfs instead of open coding a bad version

In reply to a message not in the archive
Quoted 116 lines · Matthew Garrett

If we're worried about UTF-16, the appropriate thing for us to do is error on seeing a surrogate pair.

Quoted 1 line · H. Peter Anvin

On February 12, 2016 3:27:09 AM PST, Matt Fleming

Quoted 3 lines · <matt-mF/unelCI9GS6iBeEJttW/[email protected]> wrote:
Quoted 3 lines

From: Peter Jones <[email protected]>

Translate EFI's UCS-2 variable names to UTF-8 instead of just

assuming

Quoted 30 lines
Quoted 30 lines

all variable names fit in ASCII.

---
drivers/firmware/efi/efivars.c | 30 +++++++++++-------------------
fs/efivarfs/super.c | 7 +++----

2 files changed, 14 insertions(+), 23 deletions(-)
drivers/firmware/efi/efivars.c +0 -0
index 756eca8c4cf8..f4ff8abc5f3e 100644
@@ -540,38 +540,30 @@ static ssize_t efivar_delete(struct file *filp,
 struct kobject *kobj,

static int
efivar_create_sysfs_entry(struct efivar_entry *new_var) {

drivers/firmware/efi/efivars.c +3 -3
-      int i, short_name_size;
+      int short_name_size;
      char *short_name;
-      unsigned long variable_name_size;
-      efi_char16_t *variable_name;
+      unsigned long utf8_name_size;
+      efi_char16_t *variable_name = new_var->var.VariableName;
      int ret;
Quoted 6 lines

drivers/firmware/efi/efivars.c static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,

Quoted 6 lines
drivers/firmware/efi/efivars.c +0 -2
-      variable_name = new_var->var.VariableName;
-      variable_name_size = ucs2_strlen(variable_name) *

sizeof(efi_char16_t);

drivers/firmware/efi/efivars.c +0 -2
-
      /*
-       * Length of the variable bytes in ASCII, plus the '-'

separator,

Quoted 1 line
Quoted 1 line

+ * Length of the variable bytes in UTF8, plus the '-'

separator,

Quoted 8 lines

drivers/firmware/efi/efivars.c static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,

Quoted 8 lines

* plus the GUID, plus trailing NUL
*/

drivers/firmware/efi/efivars.c +2 -4
-      short_name_size = variable_name_size / sizeof(efi_char16_t)
-                              + 1 + EFI_VARIABLE_GUID_LEN + 1;
-
-      short_name = kzalloc(short_name_size, GFP_KERNEL);
+      utf8_name_size = ucs2_utf8size(variable_name);
+      short_name_size = utf8_name_size + 1 + EFI_VARIABLE_GUID_LEN +

1;

Quoted 8 lines

drivers/firmware/efi/efivars.c static ssize_t efivar_delete(struct file *filp, struct kobject *kobj,

Quoted 8 lines
drivers/firmware/efi/efivars.c +1 -3
+      short_name = kmalloc(short_name_size, GFP_KERNEL);
      if (!short_name)
              return -ENOMEM;
 
-      /* Convert Unicode to normal chars (assume top bits are 0),
-         ala UTF-8 */
-      for (i=0; i < (int)(variable_name_size /

sizeof(efi_char16_t)); i++)

Quoted 14 lines
Quoted 14 lines

{

drivers/firmware/efi/efivars.c +4 -5
-              short_name[i] = variable_name[i] & 0xFF;
-      }
+      ucs2_as_utf8(short_name, variable_name, short_name_size);
+
      /* This is ugly, but necessary to separate one vendor's
         private variables from another's.         */
-
-      *(short_name + strlen(short_name)) = '-';
+      short_name[utf8_name_size] = '-';
      efi_guid_to_str(&new_var->var.VendorGuid,
-                       short_name + strlen(short_name));
+                       short_name + utf8_name_size + 1);
Quoted 7 lines

fs/efivarfs/super.c static int efivarfs_callback(efi_char16_t

Quoted 7 lines
      new_var->kobj.kset = efivars_kset;
fs/efivarfs/super.c +0 -0
index b8a564f29107..8651ac28ec0d 100644
@@ -118,7 +118,7 @@ static int efivarfs_callback(efi_char16_t

*name16,

Quoted 14 lines
Quoted 14 lines
fs/efivarfs/super.c +1 -1
 efi_guid_t vendor,
      struct dentry *dentry, *root = sb->s_root;
      unsigned long size = 0;
      char *name;
-      int len, i;
+      int len;
      int err = -ENOMEM;
 
      entry = kzalloc(sizeof(*entry), GFP_KERNEL);
fs/efivarfs/super.c +0 -0
@@ -128,15 +128,14 @@ static int efivarfs_callback(efi_char16_t
 *name16, efi_guid_t vendor,
      memcpy(entry->var.VariableName, name16, name_size);
      memcpy(&(entry->var.VendorGuid), &vendor, sizeof(efi_guid_t));
 
Quoted 5 lines

fs/efivarfs/super.c static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor,

Quoted 5 lines
fs/efivarfs/super.c +1 -1
-      len = ucs2_strlen(entry->var.VariableName);
+      len = ucs2_utf8size(entry->var.VariableName);
 
      /* name, plus '-', plus GUID, plus NUL*/
      name = kmalloc(len + 1 + EFI_VARIABLE_GUID_LEN + 1,

GFP_KERNEL);

Quoted 7 lines
Quoted 7 lines
fs/efivarfs/super.c +1 -2
      if (!name)
              goto fail;
 
-      for (i = 0; i < len; i++)
-              name[i] = entry->var.VariableName[i] & 0xFF;
+      ucs2_as_utf8(name, entry->var.VariableName, len);
 
Quoted 8 lines
Quoted 4 lines

fs/efivarfs/super.c static int efivarfs_callback(efi_char16_t *name16, efi_guid_t vendor, · line 140

Quoted 2 lines
fs/efivarfs/super.c +0 -0
      name[len] = '-';

However, I think we should treat this "ucs2" as utf16, because sooner

or later someone will enter utf16 characters.

Quoted 2 lines

--
Sent from my Android device with K-9 Mail. Please excuse brevity and

formatting.

Error how? Now you're making something in EFI memory inaccessible for no good reason. Most likely utf16 works just fine except when being displayed on the EFI console.

Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse brevity and formatting.