Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 613418 - net-libs/nettle-3.3 corruption during inplace operations
Summary: net-libs/nettle-3.3 corruption during inplace operations
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All Linux
: Normal normal
Assignee: Crypto team [DISABLED]
URL: http://lists.lysator.liu.se/pipermail...
Whiteboard:
Keywords:
Depends on:
Blocks: 612340 626010
  Show dependency tree
 
Reported: 2017-03-21 18:10 UTC by Matt Turner
Modified: 2017-07-23 18:14 UTC (History)
6 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
build.log.gz (sparc-build.log.gz,78.60 KB, application/gzip)
2017-03-21 18:10 UTC, Matt Turner
Details
logs.tar.gz (logs.tar.gz,14.50 KB, application/gzip)
2017-03-21 18:37 UTC, Matt Turner
Details
net-libs:gnutls-3.5.11:20170418-072740.log.xz (net-libs:gnutls-3.5.11:20170418-072740.log.xz,89.70 KB, application/x-xz)
2017-05-13 12:05 UTC, Jeroen Roovers (RETIRED)
Details
dev-libs:nettle-3.3-r1:20170513-120438.log (dev-libs:nettle-3.3-r1:20170513-120438.log,223.08 KB, text/plain)
2017-05-13 12:47 UTC, Jeroen Roovers (RETIRED)
Details
dev-libs:nettle-3.3-r1:20170715-211953.log-qemu-sparc32plus-user (dev-libs:nettle-3.3-r1:20170715-211953.log,225.91 KB, text/x-log)
2017-07-15 22:08 UTC, Sergei Trofimovich (RETIRED)
Details
nettle-3.3-noclobber.patch (nettle-3.3-noclobber.patch,2.65 KB, patch)
2017-07-16 22:20 UTC, Sergei Trofimovich (RETIRED)
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Matt Turner gentoo-dev 2017-03-21 18:10:23 UTC
Created attachment 467802 [details]
build.log.gz

% egrep  '(PASS|FAIL): .*-cert-key-exchange' sparc-build.log 
FAIL: tls1.2-cert-key-exchange
FAIL: tls1.1-cert-key-exchange
FAIL: tls1.0-cert-key-exchange
PASS: ssl3.0-cert-key-exchange
FAIL: dtls1.2-cert-key-exchange
FAIL: dtls1.0-cert-key-exchange
Comment 1 Alon Bar-Lev (RETIRED) gentoo-dev 2017-03-21 18:28:19 UTC
Please attach *.log from build directory.
Thanks!
Comment 2 Matt Turner gentoo-dev 2017-03-21 18:37:47 UTC
Created attachment 467804 [details]
logs.tar.gz
Comment 3 Alon Bar-Lev (RETIRED) gentoo-dev 2017-03-25 19:01:16 UTC
I do not like the "resources temporary unavailable", however, since it is always at same algo, something else may be related. I will ask help.
Comment 4 Alon Bar-Lev (RETIRED) gentoo-dev 2017-03-25 19:09:38 UTC
Always breaks when attempt to use: ecdhe x25519 rsa
Comment 5 Alon Bar-Lev (RETIRED) gentoo-dev 2017-03-25 22:03:31 UTC
@mattst88: can you please try to use latest non stable dev-libs/nettle?
Comment 6 Alon Bar-Lev (RETIRED) gentoo-dev 2017-03-27 10:34:01 UTC
On 27 March 2017 at 12:32, Nikos Mavrogiannopoulos <nmav@gnutls.org> wrote:
> Does nettle's test suite succeed? Which version of nettle is it being used with?
Comment 7 Matt Turner gentoo-dev 2017-03-27 16:33:45 UTC
Sorry, bender, our sparc development system lost a disk while I was testing. I won't be able to do anything else until it's fixed.
Comment 8 Alon Bar-Lev (RETIRED) gentoo-dev 2017-04-08 18:22:34 UTC
Hi,
Any news about the disk?
Anyway to test this?
Thanks!
Comment 9 Matt Turner gentoo-dev 2017-04-08 19:22:49 UTC
As far as I'm aware, no. I've pinged infra again.
Comment 10 Alon Bar-Lev (RETIRED) gentoo-dev 2017-04-28 07:22:27 UTC
Any news? I see sparc stabilizing other packages...
Comment 11 Matt Turner gentoo-dev 2017-04-29 20:58:46 UTC
ago is using some private box. bender is still down.
Comment 12 Jeroen Roovers (RETIRED) gentoo-dev 2017-05-13 11:17:43 UTC
HPPA fails on the same tests:

xzgrep '^FAIL:' net-libs:gnutls-3.5.11:20170418-072740.log.xz
FAIL: handshake-false-start
FAIL: tls1.2-cert-key-exchange
FAIL: tls1.1-cert-key-exchange
FAIL: tls1.0-cert-key-exchange
FAIL: dtls1.2-cert-key-exchange
FAIL: dtls1.0-cert-key-exchange

So that's a bigendian problem, perhaps? If only my PPC64 system still worked.
Comment 13 Alon Bar-Lev (RETIRED) gentoo-dev 2017-05-13 11:44:30 UTC
(In reply to Jeroen Roovers from comment #12)
> HPPA fails on the same tests:

Good to know!

Can you please checkout the latest non stable nettle? And see if tests passes there?

Can you please attach the log so I can confirm that problem is "ecdhe x25519 rsa"?
Comment 14 Jeroen Roovers (RETIRED) gentoo-dev 2017-05-13 12:05:25 UTC
Created attachment 472492 [details]
net-libs:gnutls-3.5.11:20170418-072740.log.xz

(In reply to Alon Bar-Lev from comment #13)
> (In reply to Jeroen Roovers from comment #12)
> > HPPA fails on the same tests:
> 
> Good to know!
> 
> Can you please checkout the latest non stable nettle? And see if tests
> passes there?

=dev-libs/nettle-3.3-r1 is stable for all but IA64.
Comment 15 Jeroen Roovers (RETIRED) gentoo-dev 2017-05-13 12:47:35 UTC
Created attachment 472496 [details]
dev-libs:nettle-3.3-r1:20170513-120438.log

[ebuild   R    ] dev-libs/nettle-3.3-r1:0/6.1::gentoo  USE="doc gmp {test} (-neon) -static-libs" 0 KiB
Comment 16 Alon Bar-Lev (RETIRED) gentoo-dev 2017-05-14 18:27:44 UTC
(In reply to Jeroen Roovers from comment #15)
> Created attachment 472496 [details]
> dev-libs:nettle-3.3-r1:20170513-120438.log
> 
> [ebuild   R    ] dev-libs/nettle-3.3-r1:0/6.1::gentoo  USE="doc gmp {test}
> (-neon) -static-libs" 0 KiB

---
=====================
11 of 89 tests failed
=====================
make[1]: *** [Makefile:126: check] Error 1
make[1]: Leaving directory '/var/tmp/portage/dev-libs/nettle-3.3-r1/work/nettle-3.3-.hppa/testsuite'
make: *** [Makefile:50: check] Error 1
---

interesting how it is installed...
can you please attach *.log so I can have visibility?
maybe I will install ppc64 in qemu so I can reproduce.
Comment 17 Alon Bar-Lev (RETIRED) gentoo-dev 2017-05-19 15:45:57 UTC
Hi,
I tried to boot install cd in qemu for powerpc and sparc (hppa is unavailable), and failed to have a working environment, so I truly need your help.
It seems like the nettle tests are failing, I am unsure.
It may be common to all big-endian or not...
If you have an environment I can access, it would be best.
Thanks!
Comment 18 Jeroen Roovers (RETIRED) gentoo-dev 2017-05-19 18:50:36 UTC
(In reply to Alon Bar-Lev from comment #17)
> If you have an environment I can access, it would be best.

My PPC64 workhorse is broken. I can probably transplant its disk and some of its memory in a spare, but I hardly have the time. I think xmw has a working 64-bit PowerPC system (as well as a 32-bit, or he's cheating and using a 32-bit chroot or something) as he's been keywording for both in the past few weeks. You might ask him.
Comment 19 Michael Weber (RETIRED) gentoo-dev 2017-05-22 21:03:03 UTC
> ... I think xmw has a working
> 64-bit PowerPC system (as well as a 32-bit, or he's cheating and using a
> 32-bit chroot or something) as he's been keywording for both in the past few
> weeks. You might ask him.

I have a working PowerMac G4 Silver and a DP PowerMac G5. My older Xserve G4 has a broken psu. The Nvidia Tegra TK1, Odroid C2 and Pine64 on [1] aren't mine but setup for arch testing as well.

[0] http://xmw.de/tmp/IMG_20170306_142340.jpg

Alon asked me to run FEATURE=test =dev-libs/nettle-3.3-r1::gentoo and =net-libs/gnutls-3.5.10, I report later.
Comment 20 Michael Weber (RETIRED) gentoo-dev 2017-05-23 19:24:48 UTC
(In reply to Michael Weber from comment #19)

It failed on both ppc and ppc64, see uploads of the PORTAGE_TMPDIR at
http://xmw.de/tmp/build/
Comment 21 Alon Bar-Lev (RETIRED) gentoo-dev 2017-05-23 19:44:52 UTC
(In reply to Michael Weber from comment #20)
> (In reply to Michael Weber from comment #19)
> 
> It failed on both ppc and ppc64, see uploads of the PORTAGE_TMPDIR at
> http://xmw.de/tmp/build/

thanks!
you have seccomp USE enabled while no support in kernel.
please disable.

I would like also to see nettle build with FEATURES=test, we suspect problem bight be there.
Comment 22 Michael Weber (RETIRED) gentoo-dev 2017-05-24 22:59:13 UTC
(In reply to Alon Bar-Lev from comment #21)
> (In reply to Michael Weber from comment #20)
> > (In reply to Michael Weber from comment #19)
> > 
> > It failed on both ppc and ppc64, see uploads of the PORTAGE_TMPDIR at
> > http://xmw.de/tmp/build/
> 
> thanks!
> you have seccomp USE enabled while no support in kernel.
> please disable.
> 
> I would like also to see nettle build with FEATURES=test, we suspect problem
> bight be there.

I rerun with gnutls[-seccomp], and both compiled fine?!

http://xmw.de/tmp/build/bug-613418/ppc64-2017-05-25/
http://xmw.de/tmp/build/bug-613418/ppc-2017-05-25/
Comment 23 Alon Bar-Lev (RETIRED) gentoo-dev 2017-05-25 06:09:04 UTC
(In reply to Michael Weber from comment #22)
> (In reply to Alon Bar-Lev from comment #21)
> > (In reply to Michael Weber from comment #20)
> > > (In reply to Michael Weber from comment #19)
> > > 
> > > It failed on both ppc and ppc64, see uploads of the PORTAGE_TMPDIR at
> > > http://xmw.de/tmp/build/
> > 
> > thanks!
> > you have seccomp USE enabled while no support in kernel.
> > please disable.
> > 
> > I would like also to see nettle build with FEATURES=test, we suspect problem
> > bight be there.
> 
> I rerun with gnutls[-seccomp], and both compiled fine?!
> 
> http://xmw.de/tmp/build/bug-613418/ppc64-2017-05-25/
> http://xmw.de/tmp/build/bug-613418/ppc-2017-05-25/

Good!
Thanks!

So nettle is ok.
However, many tests were skipped in gnutls.

Can you please run with additional USE:

USE="-guile -seccomp pkcs11 openpgp openssl idn tools zlib" FEATURES="test" emerge --oneshot =net-misc/gnutls-3.5.12
Comment 24 Alon Bar-Lev (RETIRED) gentoo-dev 2017-06-13 13:05:51 UTC
Hi,
This is not big-endian issue... both ppc, ppc64 are stable.
Please help me to resolve the issue on hppa and solaris. If I can access a machine to reproduce it will be great.
Thanks!
Comment 25 Alon Bar-Lev (RETIRED) gentoo-dev 2017-07-15 18:28:36 UTC
hppa/sparc,
I would appreciate if someone can work with me on this, these are the only arch that had issue, it is not bigendian issue as ppc/ppc64 is stable, so we need to find something else.
Can you please help me?
Moving to gnugls-3.5 is required before gnutls-3.3 becomes obsolete.
Thanks!
Comment 26 Sergei Trofimovich (RETIRED) gentoo-dev 2017-07-15 20:10:04 UTC
I was able to reproduce the same failure locally using qemu-sparc32plus-user:

...
PASS: crt_apis
PASS: set_x509_pkcs12_key
FAIL: tls1.2-cert-key-exchange
PASS: rehandshake-switch-srp-id
FAIL: tls1.1-cert-key-exchange
FAIL: tls1.0-cert-key-exchange
PASS: ssl3.0-cert-key-exchange
FAIL: dtls1.2-cert-key-exchange
PASS: x509-cert-callback-legacy
...

Superficially looks the same as in original report.

sf /tmp/portage-tmpdir/portage/net-libs/gnutls-3.5.13/work/gnutls-3.5.13-.sparc32/tests # cat dtls1.2-cert-key-exchange.log
ERROR: ld.so: object 'libsandbox.so' from LD_PRELOAD cannot be preloaded (wrong ELF class: ELFCLASS64): ignored.
negotiating DTLS 1.2 with anon-ecdh
negotiating DTLS 1.2 with anon-dh
negotiating DTLS 1.2 with dhe-rsa no cert
negotiating DTLS 1.2 with ecdhe x25519 rsa no cert
server|<1>| Discarded message[0] due to invalid decryption
server|<1>| Discarded message[3] due to invalid decryption
server|<1>| Discarded message[4] due to invalid decryption
server|<1>| Discarded message[1] due to invalid decryption
server|<1>| Discarded message[5] due to invalid decryption
server|<1>| Discarded message[6] due to invalid decryption
server|<1>| Discarded message[2] due to invalid decryption
server|<1>| Discarded message[7] due to invalid decryption
server|<1>| Discarded message[8] due to invalid decryption
server|<1>| Discarded message[3] due to invalid decryption
server|<1>| Discarded message[9] due to invalid decryption
server|<1>| Discarded message[10] due to invalid decryption
server|<1>| Discarded message[4] due to invalid decryption
server|<1>| Discarded message[11] due to invalid decryption
server|<1>| Discarded message[12] due to invalid decryption
server|<1>| Discarded message[5] due to invalid decryption
client: Resource temporarily unavailable, try again.
server: The operation timed out
dtls_try:313: dtls_try:313: Handshake failed
FAIL dtls1.2-cert-key-exchange (exit status: 1)

How I created the environment (rough steps):

Tl;DR: 1. setup sparc crossdev; 2. install qemu and refister binfmt loader; 3. run emerge

- 1. crossdev:
# CFLAGS=-mcpu=v9 crossdev sparc-unknown-linux-gnu
# PORTAGE_CONFIGROOT=/usr/sparc-unknown-linux-gnu eselect profile set default/linux/sparc/13.0

- 2. qemu:
# QEMU_USER_TARGETS=sparc32plus emerge app-emulation/qemu
# cat /qemu-sparc32plus 
    #!/bin/bash

    # I have it slightly patched, vanilla qemu should also do
    exec /home/slyfox/dev/git/qemu-sparc32plus/sparc32plus-linux-user/qemu-sparc32plus -L /usr/sparc-unknown-linux-gnu/ "$@"

# echo  ':sparc32plus:M::\x7fELF\x01\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x12:\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff:/qemu-sparc32plus:'  > /proc/sys/fs/binfmt_misc/register

- 3. test itself:
# FEATURES="test" emerge-sparc-unknown-linux-gnu -1 =gnutls-3.5.13 --quiet-build=n
Comment 27 Alon Bar-Lev (RETIRED) gentoo-dev 2017-07-15 20:45:44 UTC
Thank you Sergei!
Interesting setup, I will check this out, didn't think of using user target as I need lots of dependencies in order to full test it.

Can you please confirm that nettle tests are a pass?
Comment 28 Sergei Trofimovich (RETIRED) gentoo-dev 2017-07-15 22:07:52 UTC
(In reply to Alon Bar-Lev from comment #27)
> Thank you Sergei!
> Interesting setup, I will check this out, didn't think of using user target
> as I need lots of dependencies in order to full test it.
> 
> Can you please confirm that nettle tests are a pass?

Ran test as

    FEATURES="test" emerge-sparc-unknown-linux-gnu -1 nettle --quiet-build=n

Not all tests are passing (some are working thus things are not completely fried):

PASS: rsa-encrypt
PASS: rsa-keygen
PASS: dsa
PASS: dsa-keygen
...

curve25519_mul_g failure:
s = 
77076d0a7318a57d 3c16c17251b26645
df4c2f87ebc0992a b177fba51db92c2a

p = 
64c0a74d12813fe7 e7d4656aed95c515
088ad23749174865 7dc7df325859d114
 (bad)
r = 
8520f0098930a754 748b7ddcb43ef75a
0dbf3a0d26381af4 eba4a98eaa9b4e6a
 (expected)

FAIL: curve25519-dh

(same cipher used at least in one of the tests)
Comment 29 Sergei Trofimovich (RETIRED) gentoo-dev 2017-07-15 22:08:48 UTC
Created attachment 484932 [details]
dev-libs:nettle-3.3-r1:20170715-211953.log-qemu-sparc32plus-user
Comment 30 Sergei Trofimovich (RETIRED) gentoo-dev 2017-07-16 11:48:41 UTC
I've found the bug in nettle.

Tl;DR: =dev-libs/nettle-3.3-r1 uses gmp API incorrectly, namely mpn_mul_n:

  https://gmplib.org/manual/Low_002dlevel-Functions.html#Low_002dlevel-Functions

  Function: void mpn_mul_n (mp_limb_t *rp, const mp_limb_t *s1p, const mp_limb_t *s2p, mp_size_t n)

    Multiply {s1p, n} and {s2p, n}, and write the 2*n-limb result to rp.

    The destination has to have space for 2*n limbs, even if the product’s most significant
    limb is zero. No overlap is permitted between the destination and either source.

The easiest way to get overlapping call on x86 is to add the following assert:

nettle-3.3 $ git diff ecc-mod-arith.c
diff --git a/nettle-3.3/ecc-mod-arith.c b/nettle-3.3/ecc-mod-arith.c
index f2e47f6..7789aca 100644
--- a/nettle-3.3/ecc-mod-arith.c
+++ b/nettle-3.3/ecc-mod-arith.c
@@ -109,11 +109,21 @@ ecc_mod_submul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
   assert (hi == 0);
 }
 
+static int does_not_overlap (const mp_limb_t *a, unsigned long la,
+                             const mp_limb_t *b, unsigned long lb)
+{
+    if (a < b)
+        return a + la < b;
+    return b + lb < a;
+}
+
 /* NOTE: mul and sqr needs 2*m->size limbs at rp */
 void
 ecc_mod_mul (const struct ecc_modulo *m, mp_limb_t *rp,
             const mp_limb_t *ap, const mp_limb_t *bp)
 {
+  assert (does_not_overlap (rp, 2*m->size, ap, m->size));
+  assert (does_not_overlap (rp, 2*m->size, bp, m->size));
   mpn_mul_n (rp, ap, bp, m->size);
   m->reduce (m, rp);
 }

Where overlap happens:

https://git.lysator.liu.se/nettle/nettle/blob/master/ecc-add-eh.c#L42

In ecc-add-eh.c there is a complex layout of local variables:

|x1|y1|z1|........|B|
|x3|y3|z3|

x1/x3,y1/y3,z1/z3 pairs both point to the same memory region. Overlap happens at a call of

     ecc_modp_mul (ecc, y3, B, z1);

which is basically
     mpn_mul_n (y3, B, z1, m->size),
see https://git.lysator.liu.se/nettle/nettle/blob/master/ecc-mod-arith.c#L114

Note how y3 overwrites z1 when result is stored. Scary type of aliasing.

I've found the bug by adding printf() and comparing exact output of scratch space
on x86 versus sparc. mpn_mul_n on the same sources works differently in this case.

My guess is the difference is caused by the way how registers are spilled
when loop unrolling happens.
Comment 31 Sergei Trofimovich (RETIRED) gentoo-dev 2017-07-16 11:55:13 UTC
> +static int does_not_overlap (const mp_limb_t *a, unsigned long la,
> +                             const mp_limb_t *b, unsigned long lb)
> +{
> +    if (a < b)
> +        return a + la < b;
> +    return b + lb < a;
> +}

All above stands but this code is wrong (should be '<=' comparison and maybe more).
Comment 32 Brendan Horan 2017-07-16 14:17:25 UTC
(In reply to Alon Bar-Lev from comment #25)
> hppa/sparc,
> I would appreciate if someone can work with me on this, these are the only
> arch that had issue, it is not bigendian issue as ppc/ppc64 is stable, so we
> need to find something else.
> Can you please help me?
> Moving to gnugls-3.5 is required before gnutls-3.3 becomes obsolete.
> Thanks!

I have SPARC and HPPA machines.
I do have a little bit of time, I'd be happy to test things for you if that helps?
Comment 33 Sergei Trofimovich (RETIRED) gentoo-dev 2017-07-16 17:21:37 UTC
Sent a bug report upstream in form of added assert() as: http://lists.lysator.liu.se/pipermail/nettle-bugs/2017/003287.html
Comment 34 Alon Bar-Lev (RETIRED) gentoo-dev 2017-07-16 17:29:22 UTC
(In reply to Sergei Trofimovich from comment #33)
> Sent a bug report upstream in form of added assert() as:
> http://lists.lysator.liu.se/pipermail/nettle-bugs/2017/003287.html

Thank you  Sergei!!!
Just to confirm... with this patch all gnutls tests are passing? Please try to use the test-full USE if you can.
Comment 35 James McMechan 2017-07-16 20:46:21 UTC
Alas no the tests are still failing in dev-libs/nettle

I just ran the test set again with Sergei's patch.
It just causes the tests to fail with an assert rather than failing mysteriously.

Since this does not fix the problem, I am hoping upstream will come up with a fixed var layout that will prevent the assert from triggering, I am guessing it was making a assumption that depends on structure layout, endianess, and/or padding.


So hopefully, upstream will have a fix in the not too distant future.
Comment 36 Sergei Trofimovich (RETIRED) gentoo-dev 2017-07-16 22:20:11 UTC
Created attachment 485160 [details, diff]
nettle-3.3-noclobber.patch

nettle-3.3-noclobber.patch is a real fix for at least sparc.

Please give it a try on hppa and sparc with FEATURES=test on both nettle and gnutls.

I've superficially ran nettle test suite and things don't crash anymore.
Will run gnutls testsuite overnight.
Comment 37 James McMechan 2017-07-17 05:02:00 UTC
nettle-3.3-noclobber.patch has nettle passing all 89 tests on sparc without failure.

USE="-guile -seccomp pkcs11 openpgp openssl idn tools zlib" FEATURES="test" emerge --oneshot =net-libs/gnutls-3.5.12

Is being compiled now.
Comment 38 James McMechan 2017-07-17 19:33:05 UTC
With the nettle-3.3-r1 built with the nettle-3.3-noclobber.patch

USE="-guile -seccomp pkcs11 openpgp openssl idn tools zlib" FEATURES="test" emerge --oneshot =net-libs/gnutls-3.5.12

Ran to completion.

Oddly I had to turn off FEATURE test in order to build dev-scheme/guile-1.8.8-r3 even though "-guile" was in the USE var
apparently sys-devel/autogen-5.18.4 unconditionally requires >=dev-scheme/guile-1.8 per --depclean
Comment 39 Sergei Trofimovich (RETIRED) gentoo-dev 2017-07-19 07:43:58 UTC
Upstream committed less invasive patch that also works in qemu-user as:
    https://git.lysator.liu.se/nettle/nettle/commit/dcda81d796de2f4a16fd7e9e7a5d07baa288f147
Comment 40 Alon Bar-Lev (RETIRED) gentoo-dev 2017-07-23 18:10:09 UTC
Thanks for everyone!
Fixed in dev-libs/nettle-3.3-r2