Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp571790ybt; Wed, 1 Jul 2020 05:20:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxrtKELyk9TV6Dh3Y0krxhs1Vc67IdIfCXljQH03HnwRcIxg2BZF5HsifxdPHeIjgRRUcIs X-Received: by 2002:a17:906:5c05:: with SMTP id e5mr20892175ejq.203.1593606048288; Wed, 01 Jul 2020 05:20:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593606048; cv=none; d=google.com; s=arc-20160816; b=FS1Y83dNomHASooElHdrGcjenVnp0WqPLRP9w7yqB2kIPcBfrXA7rCnnLPN/XK09Hp uStnqHzwyI4SBMdufrJ87QPPkvuAAXv7zruF0+XBBOqndo1XcnN0qzV9+a+fWUyYKnlA +sj3aus9Bmfn3mcrYckMC1lWy5uMWuJiSCHcJxexB4ObedBFdB5JigwimW5Yl7ihVbDl BkH/SVH2Hi+X9XZQVY90f3wtcgrcQBWDfKUOb0rgRVRjbDJEvzrD6HO9UmJhURdup+/v P7tqFkJFwpuAPBA5CEn6vUIEW9NM8ZcMK++ayxW4XBk054kP8ibyW+QODLUOcIMvE2fP xfLw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:thread-index:thread-topic :content-transfer-encoding:mime-version:subject:references :in-reply-to:message-id:cc:to:from:date:dkim-signature:dkim-filter; bh=YXoF5PUmxWXzp2LmHg4Wgh13FE3P4zYYtwXrHzfqq3Y=; b=RoV9VBL4bqquXEtAt9DhT4xQkFG9J9+0SqmFmiXsXubo3TsATdUErOzf0NDt0CJ3c4 ftTLdVJqtasd/vMvVR6o1fcLzu7nr0TpyidYfuMX6/dQ1AJgLhA3tU7nF5FZLp8gbXl7 bn8dskWNA6LfPNv+tMyG+SIodRQxM0i5u9/XW3MGO9pneKqSO5B4cnPyYFogPlLCws/p YVnVwrNpSWDyda9Eo+5dY8eEeTe8rtMYV+qPVA2ergatCJV54pbd78QeSY3f/oSZPGbd P5iTu17HNo55+bcI6PPWNtLt4NHI+4O3WDsdtiI/32b0uAgpQyO98sa80Mo1mXlpUMTI jvJw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@efficios.com header.s=default header.b=sG39cyUa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=efficios.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l17si4052295ejc.174.2020.07.01.05.20.24; Wed, 01 Jul 2020 05:20:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@efficios.com header.s=default header.b=sG39cyUa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=efficios.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730554AbgGAMTq (ORCPT + 99 others); Wed, 1 Jul 2020 08:19:46 -0400 Received: from mail.efficios.com ([167.114.26.124]:50208 "EHLO mail.efficios.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730063AbgGAMTq (ORCPT ); Wed, 1 Jul 2020 08:19:46 -0400 Received: from localhost (localhost [127.0.0.1]) by mail.efficios.com (Postfix) with ESMTP id A354A2CD6E7; Wed, 1 Jul 2020 08:19:42 -0400 (EDT) Received: from mail.efficios.com ([127.0.0.1]) by localhost (mail03.efficios.com [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id 2FJk1-xrNm4E; Wed, 1 Jul 2020 08:19:42 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by mail.efficios.com (Postfix) with ESMTP id 4A8AD2CD572; Wed, 1 Jul 2020 08:19:42 -0400 (EDT) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.efficios.com 4A8AD2CD572 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=default; t=1593605982; bh=YXoF5PUmxWXzp2LmHg4Wgh13FE3P4zYYtwXrHzfqq3Y=; h=Date:From:To:Message-ID:MIME-Version; b=sG39cyUal96uC6mVyehepRr4pm1czvIHFsUGOd53n/dOX2qwq+HAY1AYOll43FKKK aLuZoGCMngJoRBScUGKntC5NT2sInb2Eh8EG2FT8QXfZVR4/kojqte6y6KA83fzKVL WFgTHC3EULUyItFTiDhFMGZgVgN9jjWwoNoge6dDRMkSxcq/587EsdxMPEJi9RtYul PPSZGqHYpt3+gngE0EmiaxVZr6Gx1mJQeLWvBcW2zWhTOWd/bZ9RyHfuA9YUMzNiNU nW+fcJm7c+MLyrTZaCM/0+juW8g5yPVmeI9N43OiiPOGCGvDyxqG6ceJ2/RfFq/OlS K5lGL6JG5BUow== X-Virus-Scanned: amavisd-new at efficios.com Received: from mail.efficios.com ([127.0.0.1]) by localhost (mail03.efficios.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 8b3X4FPEW-Fj; Wed, 1 Jul 2020 08:19:42 -0400 (EDT) Received: from mail03.efficios.com (mail03.efficios.com [167.114.26.124]) by mail.efficios.com (Postfix) with ESMTP id 3BB862CD987; Wed, 1 Jul 2020 08:19:42 -0400 (EDT) Date: Wed, 1 Jul 2020 08:19:42 -0400 (EDT) From: Mathieu Desnoyers To: Eric Dumazet Cc: Herbert Xu , "David S. Miller" , Linus Torvalds , linux-kernel , netdev , Yuchung Cheng , Jonathan Rajotte-Julien Message-ID: <338284155.18826.1593605982156.JavaMail.zimbra@efficios.com> In-Reply-To: References: <20200701020211.GA6875@gondor.apana.org.au> <20200701022241.GA7167@gondor.apana.org.au> <20200701025843.GA7254@gondor.apana.org.au> Subject: Re: [regression] TCP_MD5SIG on established sockets MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Originating-IP: [167.114.26.124] X-Mailer: Zimbra 8.8.15_GA_3945 (ZimbraWebClient - FF77 (Linux)/8.8.15_GA_3928) Thread-Topic: TCP_MD5SIG on established sockets Thread-Index: DVmdKoIdfg8hNWwm7FgIos17cuNI0g== Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ----- On Jun 30, 2020, at 11:36 PM, Eric Dumazet edumazet@google.com wrote: > On Tue, Jun 30, 2020 at 7:59 PM Herbert Xu wrote: >> >> On Tue, Jun 30, 2020 at 07:30:43PM -0700, Eric Dumazet wrote: >> > >> > I made this clear in the changelog, do we want comments all over the places ? >> > Do not get me wrong, we had this bug for years and suddenly this is a >> > big deal... >> >> I thought you were adding a new pair of smp_rmb/smp_wmb. If they >> already exist in the code then I agree it's not a big deal. But >> adding a new pair of bogus smp_Xmb's is bad for maintenance. >> > > If I knew so many people were excited about TCP / MD5, I would have > posted all my patches on lkml ;) > > Without the smp_wmb() we would still need something to prevent KMSAN > from detecting that we read uninitialized bytes, > if key->keylen is increased. (initial content of key->key[] is garbage) > > Something like this : The approach below looks good to me, but you'll also need to annotate both tcp_md5_hash_key and tcp_md5_do_add with __no_kcsan or use data_race(expr) to let the concurrency sanitizer know that there is a known data race which is there on purpose (triggered by memcpy in tcp_md5_do_add and somewhere within crypto_ahash_update). See Documentation/dev-tools/kcsan.rst for details. Thanks, Mathieu > > diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c > index > f111660453241692a17c881dd6dc2910a1236263..c3af8180c7049d5c4987bf5c67e4aff2ed6967c9 > 100644 > --- a/net/ipv4/tcp.c > +++ b/net/ipv4/tcp.c > @@ -4033,11 +4033,9 @@ EXPORT_SYMBOL(tcp_md5_hash_skb_data); > > int tcp_md5_hash_key(struct tcp_md5sig_pool *hp, const struct > tcp_md5sig_key *key) > { > - u8 keylen = key->keylen; > + u8 keylen = READ_ONCE(key->keylen); /* paired with > WRITE_ONCE() in tcp_md5_do_add */ > struct scatterlist sg; > > - smp_rmb(); /* paired with smp_wmb() in tcp_md5_do_add() */ > - > sg_init_one(&sg, key->key, keylen); > ahash_request_set_crypt(hp->md5_req, &sg, NULL, keylen); > return crypto_ahash_update(hp->md5_req); > diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c > index > 99916fcc15ca0be12c2c133ff40516f79e6fdf7f..0d08e0134335a21d23702e6a5c24a0f2b3c61c6f > 100644 > --- a/net/ipv4/tcp_ipv4.c > +++ b/net/ipv4/tcp_ipv4.c > @@ -1114,9 +1114,13 @@ int tcp_md5_do_add(struct sock *sk, const union > tcp_md5_addr *addr, > /* Pre-existing entry - just update that one. */ > memcpy(key->key, newkey, newkeylen); > > - smp_wmb(); /* pairs with smp_rmb() in tcp_md5_hash_key() */ > + /* Pairs with READ_ONCE() in tcp_md5_hash_key(). > + * Also note that a reader could catch new key->keylen value > + * but old key->key[], this is the reason we use __GFP_ZERO > + * at sock_kmalloc() time below these lines. > + */ > + WRITE_ONCE(key->keylen, newkeylen); > > - key->keylen = newkeylen; > return 0; > } > > @@ -1132,7 +1136,7 @@ int tcp_md5_do_add(struct sock *sk, const union > tcp_md5_addr *addr, > rcu_assign_pointer(tp->md5sig_info, md5sig); > } > > - key = sock_kmalloc(sk, sizeof(*key), gfp); > + key = sock_kmalloc(sk, sizeof(*key), gfp | __GFP_ZERO); > if (!key) > return -ENOMEM; > if (!tcp_alloc_md5sig_pool()) { -- Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com