exec: Set file unwritable before LSM check
authorKees Cook <keescook@chromium.org>
Fri, 9 Mar 2018 19:30:20 +0000 (11:30 -0800)
committerJames Morris <james.morris@microsoft.com>
Mon, 19 Mar 2018 04:49:32 +0000 (15:49 +1100)
The LSM check should happen after the file has been confirmed to be
unchanging. Without this, we could have a race between the Time of Check
(the call to security_kernel_read_file() which could read the file and
make access policy decisions) and the Time of Use (starting with
kernel_read_file()'s reading of the file contents). In theory, file
contents could change between the two.

Signed-off-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
Signed-off-by: James Morris <james.morris@microsoft.com>
fs/exec.c

index 7eb8d21bcab94b51905071d12a5205e7ac2ef194..a919a827d1811ebc6306205c957991b541cafe4e 100644 (file)
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -895,13 +895,13 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,
        if (!S_ISREG(file_inode(file)->i_mode) || max_size < 0)
                return -EINVAL;
 
-       ret = security_kernel_read_file(file, id);
+       ret = deny_write_access(file);
        if (ret)
                return ret;
 
-       ret = deny_write_access(file);
+       ret = security_kernel_read_file(file, id);
        if (ret)
-               return ret;
+               goto out;
 
        i_size = i_size_read(file_inode(file));
        if (max_size > 0 && i_size > max_size) {