Skip to content

Commit fdff613

Browse files
mdouchametan-ucw
authored andcommitted
Add test for CVE-2019-8912
Fixes #504 Signed-off-by: Martin Doucha <[email protected]> Acked-by: Jan Stancek <[email protected]> Reviewed-by: Cyril Hrubis <[email protected]>
1 parent 8c12483 commit fdff613

File tree

5 files changed

+122
-2
lines changed

5 files changed

+122
-2
lines changed

runtest/crypto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ af_alg03 af_alg03
44
af_alg04 af_alg04
55
af_alg05 af_alg05
66
af_alg06 af_alg06
7+
af_alg07 af_alg07
78
pcrypt_aead01 pcrypt_aead01
89
crypto_user01 crypto_user01
910
crypto_user02 crypto_user02

runtest/cve

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,6 @@ cve-2018-1000199 ptrace08
5454
cve-2018-1000204 ioctl_sg01
5555
cve-2018-18559 bind06
5656
cve-2018-19854 crypto_user01
57+
cve-2019-8912 af_alg07
5758
cve-2020-11494 pty04
5859
cve-2020-14416 pty03

testcases/kernel/crypto/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ af_alg03
44
af_alg04
55
af_alg05
66
af_alg06
7+
af_alg07
78
pcrypt_aead01
89
crypto_user01
910
crypto_user02

testcases/kernel/crypto/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ CFLAGS += -D_GNU_SOURCE
2121

2222
include $(top_srcdir)/include/mk/generic_leaf_target.mk
2323

24-
af_alg02: CFLAGS += -pthread
24+
af_alg02 af_alg07: CFLAGS += -pthread
2525

26-
crypto_user02: LDLIBS += -lrt
26+
af_alg07 crypto_user02: LDLIBS += -lrt

testcases/kernel/crypto/af_alg07.c

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Copyright (c) 2020 SUSE LLC <[email protected]>
4+
*/
5+
6+
/*
7+
* CVE-2019-8912
8+
*
9+
* Check for possible use-after-free in sockfs_setattr() on AF_ALG socket
10+
* closed by dup2() or dup3(). Unlike regular close(), dup*() syscalls don't
11+
* set sock->sk = NULL after closing the socket. Racing fchownat() against
12+
* dup2() may then result in sockfs_setattr() using the stale pointer and
13+
* writing into a block of released memory that may have been reused in the
14+
* mean time.
15+
*
16+
* The race window is small and it's hard to trigger a kernel crash but
17+
* fchownat() will return ENOENT as it should only when the bug is not
18+
* present. Race fixed in:
19+
*
20+
* commit 9060cb719e61b685ec0102574e10337fa5f445ea
21+
* Author: Mao Wenan <[email protected]>
22+
* Date: Mon Feb 18 10:44:44 2019 +0800
23+
*
24+
* net: crypto set sk to NULL when af_alg_release.
25+
*/
26+
27+
#include <sys/types.h>
28+
#include <sys/stat.h>
29+
#include <unistd.h>
30+
#include <pwd.h>
31+
32+
#include "tst_test.h"
33+
#include "tst_af_alg.h"
34+
#include "tst_fuzzy_sync.h"
35+
#include "tst_taint.h"
36+
37+
static int fd = -1, sock = -1;
38+
static int uid, gid;
39+
static struct tst_fzsync_pair fzsync_pair;
40+
41+
static void setup(void)
42+
{
43+
uid = getuid();
44+
gid = getgid();
45+
tst_taint_init(TST_TAINT_W | TST_TAINT_D);
46+
47+
fd = SAFE_OPEN("tmpfile", O_RDWR | O_CREAT, 0644);
48+
49+
tst_fzsync_pair_init(&fzsync_pair);
50+
}
51+
52+
static void *thread_run(void *arg)
53+
{
54+
while (tst_fzsync_run_b(&fzsync_pair)) {
55+
tst_fzsync_start_race_b(&fzsync_pair);
56+
dup2(fd, sock);
57+
tst_fzsync_end_race_b(&fzsync_pair);
58+
}
59+
60+
return arg;
61+
}
62+
63+
static void run(void)
64+
{
65+
tst_fzsync_pair_reset(&fzsync_pair, thread_run);
66+
67+
while (tst_fzsync_run_a(&fzsync_pair)) {
68+
sock = tst_alg_setup_reqfd("hash", "sha1", NULL, 0);
69+
tst_fzsync_start_race_a(&fzsync_pair);
70+
TEST(fchownat(sock, "", uid, gid, AT_EMPTY_PATH));
71+
tst_fzsync_end_race_a(&fzsync_pair);
72+
SAFE_CLOSE(sock);
73+
74+
if (tst_taint_check()) {
75+
tst_res(TFAIL, "Kernel is vulnerable");
76+
return;
77+
}
78+
79+
if (TST_RET == -1 && TST_ERR == ENOENT) {
80+
tst_res(TPASS | TTERRNO,
81+
"fchownat() failed successfully");
82+
return;
83+
}
84+
85+
if (TST_RET == -1) {
86+
tst_brk(TBROK | TTERRNO,
87+
"fchownat() failed unexpectedly");
88+
}
89+
90+
if (TST_RET) {
91+
tst_brk(TBROK | TTERRNO,
92+
"Invalid fchownat() return value");
93+
}
94+
}
95+
96+
tst_res(TFAIL, "fchownat() failed to fail, kernel may be vulnerable");
97+
}
98+
99+
static void cleanup(void)
100+
{
101+
tst_fzsync_pair_cleanup(&fzsync_pair);
102+
103+
if (fd >= 0)
104+
SAFE_CLOSE(fd);
105+
}
106+
107+
static struct tst_test test = {
108+
.needs_tmpdir = 1,
109+
.test_all = run,
110+
.setup = setup,
111+
.cleanup = cleanup,
112+
.tags = (const struct tst_tag[]) {
113+
{"linux-git", "9060cb719e61"},
114+
{"CVE", "2019-8912"},
115+
{}
116+
}
117+
};

0 commit comments

Comments
 (0)