Skip to contentSkip to conversation

vfs: read file_handle only once in handle_to_path

PATCH In review Sasha Levin 2 participants 4 messages Jan 28, 2015 – Jun 2, 2015

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

PATCH v2 1/1 vfs: read file_handle only once in handle_to_path

We used to read file_handle twice. Once to get the amount of extra bytes, and once to fetch the entire structure.

This may be problematic since we do size verifications only after the first read, so if the number of extra bytes changes in userspace between the first and second calls, we'll have an incoherent view of file_handle.

Instead, read the constant size once, and copy that over to the final structure without having to re-read it again.

---
Change in v2:
- Use the f_handle pointer rather than size of struct

 fs/fhandle.c |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/fhandle.c +3 -2
index 999ff5c..d59712d 100644
@@ -195,8 +195,9 @@ static int handle_to_path(int mountdirfd, struct file_handle __user *ufh,
 		goto out_err;
 	}
 	/* copy the full handle */
-	if (copy_from_user(handle, ufh,
-			   sizeof(struct file_handle) +
+	*handle = f_handle;
+	if (copy_from_user(&handle->f_handle,
+			   &ufh->f_handle,
 			   f_handle.handle_bytes)) {
 		retval = -EFAULT;
 		goto out_handle;
Signature · 1 line
1.7.10.4
#1 lore raw

ping?

Quoted 35 lines · Sasha Levin

We used to read file_handle twice. Once to get the amount of extra bytes, and once to fetch the entire structure.

This may be problematic since we do size verifications only after the first read, so if the number of extra bytes changes in userspace between the first and second calls, we'll have an incoherent view of file_handle.

Instead, read the constant size once, and copy that over to the final structure without having to re-read it again.

---
Change in v2:
- Use the f_handle pointer rather than size of struct

 fs/fhandle.c |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/fhandle.c +3 -2
index 999ff5c..d59712d 100644
@@ -195,8 +195,9 @@ static int handle_to_path(int mountdirfd, struct file_handle __user *ufh,
 		goto out_err;
 	}
 	/* copy the full handle */
-	if (copy_from_user(handle, ufh,
-			   sizeof(struct file_handle) +
+	*handle = f_handle;
+	if (copy_from_user(&handle->f_handle,
+			   &ufh->f_handle,
 			   f_handle.handle_bytes)) {
 		retval = -EFAULT;
 		goto out_handle;
#2 lore raw

Hi,

This commit fixes a race condition an unprivileged user can exploit to corrupt kernel memory. I can't seem to get anyone to grab it for the past 4+ months.

Help please?

Thanks,
Sasha

Quoted 39 lines · Sasha Levin

ping?

Quoted 35 lines · Sasha Levin

We used to read file_handle twice. Once to get the amount of extra bytes, and once to fetch the entire structure.

This may be problematic since we do size verifications only after the first read, so if the number of extra bytes changes in userspace between the first and second calls, we'll have an incoherent view of file_handle.

Instead, read the constant size once, and copy that over to the final structure without having to re-read it again.

---
Change in v2:
- Use the f_handle pointer rather than size of struct

 fs/fhandle.c |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/fhandle.c +3 -2
index 999ff5c..d59712d 100644
@@ -195,8 +195,9 @@ static int handle_to_path(int mountdirfd, struct file_handle __user *ufh,
 		goto out_err;
 	}
 	/* copy the full handle */
-	if (copy_from_user(handle, ufh,
-			   sizeof(struct file_handle) +
+	*handle = f_handle;
+	if (copy_from_user(&handle->f_handle,
+			   &ufh->f_handle,
 			   f_handle.handle_bytes)) {
 		retval = -EFAULT;
 		goto out_handle;
#3 lore raw
Quoted 3 lines · Sasha Levin

This commit fixes a race condition an unprivileged user can exploit to corrupt kernel memory. I can't seem to get anyone to grab it for the past 4+ months.

Will take it directly. Feel free to just cc me directly earlier if things take this long.

Thanks,
Linus