]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blame - releases/4.4.36/mpi-fix-null-ptr-dereference-in-mpi_powm.patch
4.9-stable patches
[thirdparty/kernel/stable-queue.git] / releases / 4.4.36 / mpi-fix-null-ptr-dereference-in-mpi_powm.patch
CommitLineData
9c6fd700
GKH
1From f5527fffff3f002b0a6b376163613b82f69de073 Mon Sep 17 00:00:00 2001
2From: Andrey Ryabinin <aryabinin@virtuozzo.com>
3Date: Thu, 24 Nov 2016 13:23:10 +0000
4Subject: mpi: Fix NULL ptr dereference in mpi_powm() [ver #3]
5
6From: Andrey Ryabinin <aryabinin@virtuozzo.com>
7
8commit f5527fffff3f002b0a6b376163613b82f69de073 upstream.
9
10This fixes CVE-2016-8650.
11
12If mpi_powm() is given a zero exponent, it wants to immediately return
13either 1 or 0, depending on the modulus. However, if the result was
14initalised with zero limb space, no limbs space is allocated and a
15NULL-pointer exception ensues.
16
17Fix this by allocating a minimal amount of limb space for the result when
18the 0-exponent case when the result is 1 and not touching the limb space
19when the result is 0.
20
21This affects the use of RSA keys and X.509 certificates that carry them.
22
23BUG: unable to handle kernel NULL pointer dereference at (null)
24IP: [<ffffffff8138ce5d>] mpi_powm+0x32/0x7e6
25PGD 0
26Oops: 0002 [#1] SMP
27Modules linked in:
28CPU: 3 PID: 3014 Comm: keyctl Not tainted 4.9.0-rc6-fscache+ #278
29Hardware name: ASUS All Series/H97-PLUS, BIOS 2306 10/09/2014
30task: ffff8804011944c0 task.stack: ffff880401294000
31RIP: 0010:[<ffffffff8138ce5d>] [<ffffffff8138ce5d>] mpi_powm+0x32/0x7e6
32RSP: 0018:ffff880401297ad8 EFLAGS: 00010212
33RAX: 0000000000000000 RBX: ffff88040868bec0 RCX: ffff88040868bba0
34RDX: ffff88040868b260 RSI: ffff88040868bec0 RDI: ffff88040868bee0
35RBP: ffff880401297ba8 R08: 0000000000000000 R09: 0000000000000000
36R10: 0000000000000047 R11: ffffffff8183b210 R12: 0000000000000000
37R13: ffff8804087c7600 R14: 000000000000001f R15: ffff880401297c50
38FS: 00007f7a7918c700(0000) GS:ffff88041fb80000(0000) knlGS:0000000000000000
39CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
40CR2: 0000000000000000 CR3: 0000000401250000 CR4: 00000000001406e0
41Stack:
42 ffff88040868bec0 0000000000000020 ffff880401297b00 ffffffff81376cd4
43 0000000000000100 ffff880401297b10 ffffffff81376d12 ffff880401297b30
44 ffffffff81376f37 0000000000000100 0000000000000000 ffff880401297ba8
45Call Trace:
46 [<ffffffff81376cd4>] ? __sg_page_iter_next+0x43/0x66
47 [<ffffffff81376d12>] ? sg_miter_get_next_page+0x1b/0x5d
48 [<ffffffff81376f37>] ? sg_miter_next+0x17/0xbd
49 [<ffffffff8138ba3a>] ? mpi_read_raw_from_sgl+0xf2/0x146
50 [<ffffffff8132a95c>] rsa_verify+0x9d/0xee
51 [<ffffffff8132acca>] ? pkcs1pad_sg_set_buf+0x2e/0xbb
52 [<ffffffff8132af40>] pkcs1pad_verify+0xc0/0xe1
53 [<ffffffff8133cb5e>] public_key_verify_signature+0x1b0/0x228
54 [<ffffffff8133d974>] x509_check_for_self_signed+0xa1/0xc4
55 [<ffffffff8133cdde>] x509_cert_parse+0x167/0x1a1
56 [<ffffffff8133d609>] x509_key_preparse+0x21/0x1a1
57 [<ffffffff8133c3d7>] asymmetric_key_preparse+0x34/0x61
58 [<ffffffff812fc9f3>] key_create_or_update+0x145/0x399
59 [<ffffffff812fe227>] SyS_add_key+0x154/0x19e
60 [<ffffffff81001c2b>] do_syscall_64+0x80/0x191
61 [<ffffffff816825e4>] entry_SYSCALL64_slow_path+0x25/0x25
62Code: 56 41 55 41 54 53 48 81 ec a8 00 00 00 44 8b 71 04 8b 42 04 4c 8b 67 18 45 85 f6 89 45 80 0f 84 b4 06 00 00 85 c0 75 2f 41 ff ce <49> c7 04 24 01 00 00 00 b0 01 75 0b 48 8b 41 18 48 83 38 01 0f
63RIP [<ffffffff8138ce5d>] mpi_powm+0x32/0x7e6
64 RSP <ffff880401297ad8>
65CR2: 0000000000000000
66---[ end trace d82015255d4a5d8d ]---
67
68Basically, this is a backport of a libgcrypt patch:
69
70 http://git.gnupg.org/cgi-bin/gitweb.cgi?p=libgcrypt.git;a=patch;h=6e1adb05d290aeeb1c230c763970695f4a538526
71
72Fixes: cdec9cb5167a ("crypto: GnuPG based MPI lib - source files (part 1)")
73Signed-off-by: Andrey Ryabinin <aryabinin@virtuozzo.com>
74Signed-off-by: David Howells <dhowells@redhat.com>
75cc: Dmitry Kasatkin <dmitry.kasatkin@gmail.com>
76cc: linux-ima-devel@lists.sourceforge.net
77Signed-off-by: James Morris <james.l.morris@oracle.com>
78Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
79
80---
81 lib/mpi/mpi-pow.c | 7 ++++++-
82 1 file changed, 6 insertions(+), 1 deletion(-)
83
84--- a/lib/mpi/mpi-pow.c
85+++ b/lib/mpi/mpi-pow.c
86@@ -64,8 +64,13 @@ int mpi_powm(MPI res, MPI base, MPI exp,
87 if (!esize) {
88 /* Exponent is zero, result is 1 mod MOD, i.e., 1 or 0
89 * depending on if MOD equals 1. */
90- rp[0] = 1;
91 res->nlimbs = (msize == 1 && mod->d[0] == 1) ? 0 : 1;
92+ if (res->nlimbs) {
93+ if (mpi_resize(res, 1) < 0)
94+ goto enomem;
95+ rp = res->d;
96+ rp[0] = 1;
97+ }
98 res->sign = 0;
99 goto leave;
100 }