Skip to contentSkip to conversation

vfs: forbid write access when reading a file into memory

PATCH In review Mimi Zohar 3 participants 4 messages Feb 16, 2016 – Feb 19, 2016

Subsystems (2)
lore
Share & tools
lore

Mimi Zohar

author
#0 lore raw

PATCH 1/1 vfs: forbid write access when reading a file into memory

From: Dmitry Kasatkin <[email protected]>

This patch is based on top of the "vfs: support for a common kernel file loader" patch set. In general when the kernel is reading a file into memory it does not want anything else writing to it.

The kernel currently only forbids write access to a file being executed. This patch extends this locking to files being read by the kernel.

Changelog:
- moved function to kernel_read_file() - Mimi
- updated patch description - Mimi

---
 fs/exec.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)
fs/exec.c +21 -8
index 604f669..1b7d617 100644
@@ -846,15 +846,25 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,
 	if (ret)
 		return ret;
 
+	ret = deny_write_access(file);
+	if (ret)
+		return ret;
+
 	i_size = i_size_read(file_inode(file));
-	if (max_size > 0 && i_size > max_size)
-		return -EFBIG;
-	if (i_size <= 0)
-		return -EINVAL;
+	if (max_size > 0 && i_size > max_size) {
+		ret = -EFBIG;
+		goto out;
+	}
+	if (i_size <= 0) {
+		ret = -EINVAL;
+		goto out;
+	}
 
 	*buf = vmalloc(i_size);
-	if (!*buf)
-		return -ENOMEM;
+	if (!*buf) {
+		ret = -ENOMEM;
+		goto out;
+	}
 
 	pos = 0;
 	while (pos < i_size) {
@@ -872,18 +882,21 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,
 
 	if (pos != i_size) {
 		ret = -EIO;
-		goto out;
+		goto out_free;
 	}
 
 	ret = security_kernel_post_read_file(file, *buf, i_size, id);
 	if (!ret)
 		*size = pos;
 
-out:
+out_free:
 	if (ret < 0) {
 		vfree(*buf);
 		*buf = NULL;
 	}
+
+out:
+	allow_write_access(file);
 	return ret;
 }
 EXPORT_SYMBOL_GPL(kernel_read_file);
Signature · 1 line
2.1.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Mimi Zohar

From: Dmitry Kasatkin <[email protected]>

This patch is based on top of the "vfs: support for a common kernel file loader" patch set. In general when the kernel is reading a file into memory it does not want anything else writing to it.

The kernel currently only forbids write access to a file being executed. This patch extends this locking to files being read by the kernel.

Quoted 7 lines

Changelog:
- moved function to kernel_read_file() - Mimi
- updated patch description - Mimi

BTW I don't see your common read work on linux-next yet, would you resend this later after that queue of patches get merged?

Luis

author Reviewed-by Luis R. Rodriguez Acked-by Kees Cook
#2 lore raw

This patch is based on top of the "vfs: support for a common kernel file loader" patch set. In general when the kernel is reading a file into memory it does not want anything else writing to it.

The kernel currently only forbids write access to a file being executed. This patch extends this locking to files being read by the kernel.

Changelog:
- moved function to kernel_read_file() - Mimi
- updated patch description - Mimi

---
 fs/exec.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)
fs/exec.c +21 -8
index 604f669..1b7d617 100644
@@ -846,15 +846,25 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,
 	if (ret)
 		return ret;
 
+	ret = deny_write_access(file);
+	if (ret)
+		return ret;
+
 	i_size = i_size_read(file_inode(file));
-	if (max_size > 0 && i_size > max_size)
-		return -EFBIG;
-	if (i_size <= 0)
-		return -EINVAL;
+	if (max_size > 0 && i_size > max_size) {
+		ret = -EFBIG;
+		goto out;
+	}
+	if (i_size <= 0) {
+		ret = -EINVAL;
+		goto out;
+	}
 
 	*buf = vmalloc(i_size);
-	if (!*buf)
-		return -ENOMEM;
+	if (!*buf) {
+		ret = -ENOMEM;
+		goto out;
+	}
 
 	pos = 0;
 	while (pos < i_size) {
@@ -872,18 +882,21 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,
 
 	if (pos != i_size) {
 		ret = -EIO;
-		goto out;
+		goto out_free;
 	}
 
 	ret = security_kernel_post_read_file(file, *buf, i_size, id);
 	if (!ret)
 		*size = pos;
 
-out:
+out_free:
 	if (ret < 0) {
 		vfree(*buf);
 		*buf = NULL;
 	}
+
+out:
+	allow_write_access(file);
 	return ret;
 }
 EXPORT_SYMBOL_GPL(kernel_read_file);
Signature · 1 line
2.1.0
Acked-by on v1 1/1
#3 lore raw
In reply to Mimi Zohar
Quoted 5 lines · Mimi Zohar

From: Dmitry Kasatkin <[email protected]>

This patch is based on top of the "vfs: support for a common kernel file loader" patch set. In general when the kernel is reading a file into memory it does not want anything else writing to it.

Oh yes please. Thanks for this! That had bothered me for a long time. :)

-Kees

Quoted 78 lines

The kernel currently only forbids write access to a file being executed. This patch extends this locking to files being read by the kernel.

Changelog:
- moved function to kernel_read_file() - Mimi
- updated patch description - Mimi

---
 fs/exec.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)
fs/exec.c +21 -9
index 604f669..1b7d617 100644
@@ -846,15 +846,25 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,
        if (ret)
                return ret;
 
+       ret = deny_write_access(file);
+       if (ret)
+               return ret;
+
        i_size = i_size_read(file_inode(file));
-       if (max_size > 0 && i_size > max_size)
-               return -EFBIG;
-       if (i_size <= 0)
-               return -EINVAL;
+       if (max_size > 0 && i_size > max_size) {
+               ret = -EFBIG;
+               goto out;
+       }
+       if (i_size <= 0) {
+               ret = -EINVAL;
+               goto out;
+       }
 
        *buf = vmalloc(i_size);
-       if (!*buf)
-               return -ENOMEM;
+       if (!*buf) {
+               ret = -ENOMEM;
+               goto out;
+       }
 
        pos = 0;
        while (pos < i_size) {
@@ -872,18 +882,21 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,
 
        if (pos != i_size) {
                ret = -EIO;
-               goto out;
+               goto out_free;
        }
 
        ret = security_kernel_post_read_file(file, *buf, i_size, id);
        if (!ret)
                *size = pos;
 
-out:
+out_free:
        if (ret < 0) {
                vfree(*buf);
                *buf = NULL;
        }
+
+out:
+       allow_write_access(file);
        return ret;
 }
 EXPORT_SYMBOL_GPL(kernel_read_file);
--

2.1.0

Signature · 2 lines
Kees Cook
Chrome OS & Brillo Security