diff options
-rw-r--r-- | patches/cve/CVE-2018-12232-socket-close-race-condition-between-sock_close-and-s.patch | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/patches/cve/CVE-2018-12232-socket-close-race-condition-between-sock_close-and-s.patch b/patches/cve/CVE-2018-12232-socket-close-race-condition-between-sock_close-and-s.patch new file mode 100644 index 0000000..2dabfe5 --- /dev/null +++ b/patches/cve/CVE-2018-12232-socket-close-race-condition-between-sock_close-and-s.patch | |||
@@ -0,0 +1,98 @@ | |||
1 | From 91717ffc9057f38a0203a40ef36ae2e482fd7cbe Mon Sep 17 00:00:00 2001 | ||
2 | From: Cong Wang <xiyou.wangcong@gmail.com> | ||
3 | Date: Thu, 7 Jun 2018 13:39:49 -0700 | ||
4 | Subject: [PATCH] socket: close race condition between sock_close() and | ||
5 | sockfs_setattr() | ||
6 | |||
7 | [ Upstream commit 6d8c50dcb029872b298eea68cc6209c866fd3e14 ] | ||
8 | |||
9 | fchownat() doesn't even hold refcnt of fd until it figures out | ||
10 | fd is really needed (otherwise is ignored) and releases it after | ||
11 | it resolves the path. This means sock_close() could race with | ||
12 | sockfs_setattr(), which leads to a NULL pointer dereference | ||
13 | since typically we set sock->sk to NULL in ->release(). | ||
14 | |||
15 | As pointed out by Al, this is unique to sockfs. So we can fix this | ||
16 | in socket layer by acquiring inode_lock in sock_close() and | ||
17 | checking against NULL in sockfs_setattr(). | ||
18 | |||
19 | sock_release() is called in many places, only the sock_close() | ||
20 | path matters here. And fortunately, this should not affect normal | ||
21 | sock_close() as it is only called when the last fd refcnt is gone. | ||
22 | It only affects sock_close() with a parallel sockfs_setattr() in | ||
23 | progress, which is not common. | ||
24 | |||
25 | CVE: CVE-2018-12232 | ||
26 | Upstream-Status: Backport [https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?h=linux-4.14.y&id=91717ffc9057f38a0203a40ef36ae2e482fd7cbe] | ||
27 | |||
28 | Fixes: 86741ec25462 ("net: core: Add a UID field to struct sock.") | ||
29 | Reported-by: shankarapailoor <shankarapailoor@gmail.com> | ||
30 | Cc: Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp> | ||
31 | Cc: Lorenzo Colitti <lorenzo@google.com> | ||
32 | Cc: Al Viro <viro@zeniv.linux.org.uk> | ||
33 | Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com> | ||
34 | Signed-off-by: David S. Miller <davem@davemloft.net> | ||
35 | Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> | ||
36 | Signed-off-by: Andreas Wellving <andreas.wellving@enea.com> | ||
37 | --- | ||
38 | net/socket.c | 18 +++++++++++++++--- | ||
39 | 1 file changed, 15 insertions(+), 3 deletions(-) | ||
40 | |||
41 | diff --git a/net/socket.c b/net/socket.c | ||
42 | index 43d2f17f5eea..8b2bef6cfe42 100644 | ||
43 | --- a/net/socket.c | ||
44 | +++ b/net/socket.c | ||
45 | @@ -538,7 +538,10 @@ static int sockfs_setattr(struct dentry *dentry, struct iattr *iattr) | ||
46 | if (!err && (iattr->ia_valid & ATTR_UID)) { | ||
47 | struct socket *sock = SOCKET_I(d_inode(dentry)); | ||
48 | |||
49 | - sock->sk->sk_uid = iattr->ia_uid; | ||
50 | + if (sock->sk) | ||
51 | + sock->sk->sk_uid = iattr->ia_uid; | ||
52 | + else | ||
53 | + err = -ENOENT; | ||
54 | } | ||
55 | |||
56 | return err; | ||
57 | @@ -588,12 +591,16 @@ EXPORT_SYMBOL(sock_alloc); | ||
58 | * an inode not a file. | ||
59 | */ | ||
60 | |||
61 | -void sock_release(struct socket *sock) | ||
62 | +static void __sock_release(struct socket *sock, struct inode *inode) | ||
63 | { | ||
64 | if (sock->ops) { | ||
65 | struct module *owner = sock->ops->owner; | ||
66 | |||
67 | + if (inode) | ||
68 | + inode_lock(inode); | ||
69 | sock->ops->release(sock); | ||
70 | + if (inode) | ||
71 | + inode_unlock(inode); | ||
72 | sock->ops = NULL; | ||
73 | module_put(owner); | ||
74 | } | ||
75 | @@ -608,6 +615,11 @@ void sock_release(struct socket *sock) | ||
76 | } | ||
77 | sock->file = NULL; | ||
78 | } | ||
79 | + | ||
80 | +void sock_release(struct socket *sock) | ||
81 | +{ | ||
82 | + __sock_release(sock, NULL); | ||
83 | +} | ||
84 | EXPORT_SYMBOL(sock_release); | ||
85 | |||
86 | void __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags) | ||
87 | @@ -1122,7 +1134,7 @@ static int sock_mmap(struct file *file, struct vm_area_struct *vma) | ||
88 | |||
89 | static int sock_close(struct inode *inode, struct file *filp) | ||
90 | { | ||
91 | - sock_release(SOCKET_I(inode)); | ||
92 | + __sock_release(SOCKET_I(inode), inode); | ||
93 | return 0; | ||
94 | } | ||
95 | |||
96 | -- | ||
97 | 2.20.1 | ||
98 | |||