Skip to contentSkip to conversation

fs: fix possible Spectre V1 indexing in __close_fd()

PATCH In review Greg Hackmann 2 participants 2 messages Dec 24, 2018

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

PATCH v2 1/1 fs: fix possible Spectre V1 indexing in __close_fd()

Omer Tripp's analysis of a Spectre V1 gadget in __close_fd():

"1. __close_fd() is reachable via the close() syscall with a
user-controlled fd.

2. If said bounds check is mispredicted, then a user-controlled address fdt->fd[fd] is obtained then dereferenced, and the value of a user-controlled address is loaded into the local variable file.

3. file is then passed as an argument to filp_close, where the cache lines secret + offsetof(f_op) and secret + offsetof(f_mode) are hot and vulnerable to a timing channel attack."

Address this by using array_index_nospec() to prevent speculation past the end of current->fdt.

---
v2: include Omer Tripp's analysis in commit message, and update my email
address

 fs/file.c | 2 ++
 1 file changed, 2 insertions(+)
fs/file.c +2 -0
index 7ffd6e9d103d..a80cf82be96b 100644
@@ -18,6 +18,7 @@
 #include <linux/bitops.h>
 #include <linux/spinlock.h>
 #include <linux/rcupdate.h>
+#include <linux/nospec.h>
 
 unsigned int sysctl_nr_open __read_mostly = 1024*1024;
 unsigned int sysctl_nr_open_min = BITS_PER_LONG;
@@ -626,6 +627,7 @@ int __close_fd(struct files_struct *files, unsigned fd)
 	fdt = files_fdtable(files);
 	if (fd >= fdt->max_fds)
 		goto out_unlock;
+	fd = array_index_nospec(fd, fdt->max_fds);
 	file = fdt->fd[fd];
 	if (!file)
 		goto out_unlock;
Signature · 1 line
2.19.1
#1 lore raw
Quoted 10 lines · Greg Hackmann

+++ b/fs/file.c

fs/file.c +1 -0
@@ -18,6 +18,7 @@
 #include <linux/bitops.h>
 #include <linux/spinlock.h>
 #include <linux/rcupdate.h>
+#include <linux/nospec.h>
 
 unsigned int sysctl_nr_open __read_mostly = 1024*1024;
 unsigned int sysctl_nr_open_min = BITS_PER_LONG;

fs/file.c int __close_fd(struct files_struct *files, unsigned fd) · line 633

Quoted 7 lines
fs/file.c +1 -0
@@ -626,6 +627,7 @@ int __close_fd(struct files_struct *files, unsigned fd)
 	fdt = files_fdtable(files);
 	if (fd >= fdt->max_fds)
 		goto out_unlock;
+	fd = array_index_nospec(fd, fdt->max_fds);
 	file = fdt->fd[fd];
 	if (!file)
 		goto out_unlock;

This is insufficient. do_dup2() has a similar problem.