Skip to contentSkip to conversation

efivarfs: bug fixes

PATCH In review James Bottomley 2 participants 9 messages Dec 8, 2024 – Dec 9, 2024

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

cover efivarfs: bug fixes

Patch 1 is stand alone, but 2 depends on 3

Regards,

James

---

James Bottomley (3):
efivarfs: fix error on non-existent file
efivarfs: fix memory leak on variable removal
efivarfs: fix incorrect variable creation

 fs/efivarfs/file.c  | 30 ++++++++++++++++++++++--------
 fs/efivarfs/inode.c |  1 +
 fs/efivarfs/super.c |  3 ---
 3 files changed, 23 insertions(+), 11 deletions(-)
Signature · 1 line
2.35.3
#1 lore raw

PATCH 1/3 efivarfs: fix error on non-existent file

efivarfs returns -EINVAL if the non-existing file .doesn't conform to the NAME-GUID format and -ENOENT if it does. This is caused by efivars_d_hash returning -EINVAL if the name isn't formatted correctly. This error is returned before simple_lookup returns a negative dentry and is what the user sees. Fix by removing this check. If the file doesn't exist, simple_lookup will return a negative dentry leading to -ENOENT and efivarfs_create already has a validity check before it creates an entry (and will correctly return -EINVAL)

---
 fs/efivarfs/super.c | 3 ---
 1 file changed, 3 deletions(-)
fs/efivarfs/super.c +0 -3
index a929f1b613be..beba15673be8 100644
@@ -144,9 +144,6 @@ static int efivarfs_d_hash(const struct dentry *dentry, struct qstr *qstr)
 	const unsigned char *s = qstr->name;
 	unsigned int len = qstr->len;
 
-	if (!efivarfs_valid_name(s, len))
-		return -EINVAL;
-
 	while (len-- > EFI_VARIABLE_GUID_LEN)
 		hash = partial_name_hash(*s++, hash);
 
Signature · 1 line
2.35.3
#2 lore raw

PATCH 2/3 efivarfs: fix memory leak on variable removal

In reply to James Bottomley

efivar_entry_delete() doesn't free the entry, it merely removes it from the list. Neither efivarfs_unlink(), nor efivarfs_file_write() free the variable entry meaning it remains as unreferenced but allocated memory. Fix by adding the correct kfree()'s.

---
 fs/efivarfs/file.c  | 1 +
 fs/efivarfs/inode.c | 1 +
 2 files changed, 2 insertions(+)
fs/efivarfs/file.c +1 -0
index 23c51d62f902..41b2f5a7239c 100644
@@ -48,6 +48,7 @@ static ssize_t efivarfs_file_write(struct file *file,
 		drop_nlink(inode);
 		d_delete(file->f_path.dentry);
 		dput(file->f_path.dentry);
+		kfree(var);
 	} else {
 		inode_lock(inode);
 		i_size_write(inode, datasize + sizeof(attributes));
fs/efivarfs/inode.c +1 -0
index 586446e02ef7..37ff34fab9f7 100644
@@ -143,6 +143,7 @@ static int efivarfs_unlink(struct inode *dir, struct dentry *dentry)
 
 	drop_nlink(d_inode(dentry));
 	dput(dentry);
+	kfree(var);
 	return 0;
 };
 
Signature · 1 line
2.35.3
#3 lore raw

PATCH 3/3 efivarfs: fix incorrect variable creation

In reply to James Bottomley

If an EFI variable is created by an open and write but returns an error in set_variable, it isn't removed but hangs around in efivarfs with invalid inode attributes. This happens because the entry is created in efivarfs_create but the EFI set_variable problem isn't discovered until efivarfs_file_write(). Fix by having set_variable failure in efivarfs_file_write() check if the variable existed before or is newly created and remove it again on the latter. The signal for a newly created variable is that var.Attributes is empty. This cannot happen for a real variable because one of the flags in EFI_VARIABLE_MASK must be set.

---
 fs/efivarfs/file.c | 31 ++++++++++++++++++++++---------
 1 file changed, 22 insertions(+), 9 deletions(-)
fs/efivarfs/file.c +22 -9
index 41b2f5a7239c..5ef88c577e03 100644
@@ -23,18 +23,23 @@ static ssize_t efivarfs_file_write(struct file *file,
 	ssize_t bytes;
 	bool set = false;
 
+	bytes = -EINVAL;
 	if (count < sizeof(attributes))
-		return -EINVAL;
+		goto err;
 
+	bytes = -EFAULT;
 	if (copy_from_user(&attributes, userbuf, sizeof(attributes)))
-		return -EFAULT;
+		goto err;
 
+	bytes = -EINVAL;
 	if (attributes & ~(EFI_VARIABLE_MASK))
-		return -EINVAL;
+		goto err;
 
 	data = memdup_user(userbuf + sizeof(attributes), datasize);
-	if (IS_ERR(data))
-		return PTR_ERR(data);
+	if (IS_ERR(data)) {
+		bytes = PTR_ERR(data);
+		goto err;
+	}
 
 	bytes = efivar_entry_set_get_size(var, attributes, &datasize,
 					  data, &set);
@@ -45,10 +50,7 @@ static ssize_t efivarfs_file_write(struct file *file,
 	}
 
 	if (bytes == -ENOENT) {
-		drop_nlink(inode);
-		d_delete(file->f_path.dentry);
-		dput(file->f_path.dentry);
-		kfree(var);
+		var->var.Attributes = 0;
 	} else {
 		inode_lock(inode);
 		i_size_write(inode, datasize + sizeof(attributes));
@@ -60,6 +62,17 @@ static ssize_t efivarfs_file_write(struct file *file,
 
 out:
 	kfree(data);
+err:
+	if (var->var.Attributes == 0) {
+		/*
+		 * variable got deleted or didn't exist before we
+		 *  tried to set it
+		 */
+		drop_nlink(inode);
+		d_delete(file->f_path.dentry);
+		dput(file->f_path.dentry);
+		kfree(var);
+	}
 
 	return bytes;
 }
Signature · 1 line
2.35.3
maintainer
#4 lore raw
In reply to James Bottomley
Quoted 5 lines · James Bottomley

Patch 1 is stand alone, but 2 depends on 3

Regards,

Quoted 9 lines

James

---

James Bottomley (3):
efivarfs: fix error on non-existent file
efivarfs: fix memory leak on variable removal
efivarfs: fix incorrect variable creation

Thanks James,

I've queued these up now.

#5 lore raw
Quoted 17 lines · Ard Biesheuvel
Quoted 5 lines · James Bottomley

Patch 1 is stand alone, but 2 depends on 3

Regards,

Quoted 9 lines

James

---

James Bottomley (3):
  efivarfs: fix error on non-existent file
  efivarfs: fix memory leak on variable removal
  efivarfs: fix incorrect variable creation

Quoted 3 lines

Thanks James,

I've queued these up now.

Thanks, but I need to redo 3/3: there's a bug where if the variable is created to do a write which fails, it remains on the list even though the entry is freed.

It also begs the question: why does this list of variables exist? All it does is cause management complexity and overhead and its only function seems to be to free the entries when the filesystem is unmounted, which could much more easily be accomplished by implementing a superblock evict_inode() callback that kfree's i_private, which would mean the entry was freed when the inode was and thus wouldn't have to be explicitly freed anywhere.

Regards,

James

maintainer
#6 lore raw
Quoted 19 lines · James Bottomley
Quoted 17 lines · Ard Biesheuvel
Quoted 14 lines · James Bottomley

Patch 1 is stand alone, but 2 depends on 3

Regards,

James

---

James Bottomley (3):
efivarfs: fix error on non-existent file
efivarfs: fix memory leak on variable removal
efivarfs: fix incorrect variable creation

Quoted 8 lines
Quoted 3 lines

Thanks James,

I've queued these up now.

Thanks, but I need to redo 3/3: there's a bug where if the variable is created to do a write which fails, it remains on the list even though the entry is freed.

OK. I'm a bit out of my depth here with the VFS stuff.

So efivarfs_create() creates the file and efivarfs_file_write() writes the contents of the variables, right? So what is the correct behavior here if the write fails? If the file exists as an empty file, but might still be open, surely we cannot just drop it from the list?

Quoted 8 lines

It also begs the question: why does this list of variables exist? All it does is cause management complexity and overhead and its only function seems to be to free the entries when the filesystem is unmounted, which could much more easily be accomplished by implementing a superblock evict_inode() callback that kfree's i_private, which would mean the entry was freed when the inode was and thus wouldn't have to be explicitly freed anywhere.

I have no idea - this code existed before I got involved with EFI.

However, a related issue came up here [0]: the list may get out of sync after a resume from hibernate, and so it needs to be brought up to date. Not having a list in the first place seems like a step in the right direction, as we'd need to synchronize that with the updated varstore as well.

So IIUC, you are suggesting to keep the caching behavior of the name and attributes, but not keep a list at all? Would that work with the needed resync above?

[0] https://github.com/systemd/systemd/pull/35497

#7 lore raw
Quoted 30 lines · Ard Biesheuvel
Quoted 19 lines · James Bottomley
Quoted 17 lines · Ard Biesheuvel
Quoted 14 lines · James Bottomley

Patch 1 is stand alone, but 2 depends on 3

Regards,

James

---

James Bottomley (3):
  efivarfs: fix error on non-existent file
  efivarfs: fix memory leak on variable removal
  efivarfs: fix incorrect variable creation

Quoted 8 lines
Quoted 3 lines

Thanks James,

I've queued these up now.

Thanks, but I need to redo 3/3: there's a bug where if the variable is created to do a write which fails, it remains on the list even though the entry is freed.

Quoted 4 lines

OK. I'm a bit out of my depth here with the VFS stuff.

So efivarfs_create() creates the file and efivarfs_file_write() writes the contents of the variables, right?

Right.

Quoted 3 lines

So what is the correct behavior here if the write fails? If the file
exists as an empty file, but might still be open, surely we cannot just drop it from the list?

I don't think it's a race that can be mediated in the current mechanism, although the efivar lock seems to do some of it. In the ordinary course of filesystems it should be mediated by the dentry objects (any racing open/write with delete can either succeed or get an error).

Quoted 9 lines
Quoted 8 lines

It also begs the question: why does this list of variables exist? All it does is cause management complexity and overhead and its only function seems to be to free the entries when the filesystem is unmounted, which could much more easily be accomplished by implementing a superblock evict_inode() callback that kfree's i_private, which would mean the entry was freed when the inode was and thus wouldn't have to be explicitly freed anywhere.

Quoted 7 lines

I have no idea - this code existed before I got involved with EFI.

However, a related issue came up here [0]: the list may get out of sync after a resume from hibernate, and so it needs to be brought up to date. Not having a list in the first place seems like a step in the right direction, as we'd need to synchronize that with the updated varstore as well.

Yes, that's the same issue I was complaining about and trying to fix in patch 3/3.

Quoted 3 lines

So IIUC, you are suggesting to keep the caching behavior of the name and attributes, but not keep a list at all? Would that work with the needed resync above?

Yes. If we simply let the dentries behave correctly as filesystem objects, they'll mediate the race and if the inode gets evicted then we also free the entry (so we tie efivarfs entry lifetimes to the inode). There would still be a corner case where you could call open(O_CREAT) but *not* write on the file. That would still create a zero length variable entry which would disappear on reboot because it would have no corresponding EFI variable. This is an inevitable consequence of our required semantics of not creating the variable until something is written to it although it may be possible to flag the dentry in create as being not visible to others until it gets written (i.e. effectively open it as a deleted but open file and promote it to not deleted on a successful write).

If you want me to look into doing the above instead of patches 2-3, I can. I think patch 1 can be safely applied.

Regards,

James

maintainer
#8 lore raw
Quoted 86 lines · James Bottomley
Quoted 30 lines · Ard Biesheuvel
Quoted 19 lines · James Bottomley
Quoted 17 lines · Ard Biesheuvel
Quoted 14 lines · James Bottomley

Patch 1 is stand alone, but 2 depends on 3

Regards,

James

---

James Bottomley (3):
efivarfs: fix error on non-existent file
efivarfs: fix memory leak on variable removal
efivarfs: fix incorrect variable creation

Quoted 8 lines
Quoted 3 lines

Thanks James,

I've queued these up now.

Thanks, but I need to redo 3/3: there's a bug where if the variable is created to do a write which fails, it remains on the list even though the entry is freed.

Quoted 4 lines

OK. I'm a bit out of my depth here with the VFS stuff.

So efivarfs_create() creates the file and efivarfs_file_write() writes the contents of the variables, right?

Right.

Quoted 3 lines

So what is the correct behavior here if the write fails? If the file
exists as an empty file, but might still be open, surely we cannot just drop it from the list?

I don't think it's a race that can be mediated in the current mechanism, although the efivar lock seems to do some of it. In the ordinary course of filesystems it should be mediated by the dentry objects (any racing open/write with delete can either succeed or get an error).

Quoted 9 lines
Quoted 8 lines

It also begs the question: why does this list of variables exist? All it does is cause management complexity and overhead and its only function seems to be to free the entries when the filesystem is unmounted, which could much more easily be accomplished by implementing a superblock evict_inode() callback that kfree's i_private, which would mean the entry was freed when the inode was and thus wouldn't have to be explicitly freed anywhere.

Quoted 7 lines

I have no idea - this code existed before I got involved with EFI.

However, a related issue came up here [0]: the list may get out of sync after a resume from hibernate, and so it needs to be brought up to date. Not having a list in the first place seems like a step in the right direction, as we'd need to synchronize that with the updated varstore as well.

Yes, that's the same issue I was complaining about and trying to fix in patch 3/3.

Quoted 3 lines

So IIUC, you are suggesting to keep the caching behavior of the name and attributes, but not keep a list at all? Would that work with the needed resync above?

Yes. If we simply let the dentries behave correctly as filesystem objects, they'll mediate the race and if the inode gets evicted then we also free the entry (so we tie efivarfs entry lifetimes to the inode). There would still be a corner case where you could call open(O_CREAT) but *not* write on the file. That would still create a zero length variable entry which would disappear on reboot because it would have no corresponding EFI variable. This is an inevitable consequence of our required semantics of not creating the variable until something is written to it although it may be possible to flag the dentry in create as being not visible to others until it gets written (i.e. effectively open it as a deleted but open file and promote it to not deleted on a successful write).

Quoted 3 lines

If you want me to look into doing the above instead of patches 2-3, I can. I think patch 1 can be safely applied.

That would be much appreciated. I'll keep only patch #1 for now.