From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 083CAC433E0 for ; Tue, 30 Jun 2020 23:44:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D494D206CB for ; Tue, 30 Jun 2020 23:44:25 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=efficios.com header.i=@efficios.com header.b="T+fNjcpV" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726800AbgF3XoY (ORCPT ); Tue, 30 Jun 2020 19:44:24 -0400 Received: from mail.efficios.com ([167.114.26.124]:39406 "EHLO mail.efficios.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725930AbgF3XoX (ORCPT ); Tue, 30 Jun 2020 19:44:23 -0400 Received: from localhost (localhost [127.0.0.1]) by mail.efficios.com (Postfix) with ESMTP id CF6BF2C8628; Tue, 30 Jun 2020 19:44:21 -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 0vJOA2UfVbAo; Tue, 30 Jun 2020 19:44:21 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by mail.efficios.com (Postfix) with ESMTP id 787472C8354; Tue, 30 Jun 2020 19:44:21 -0400 (EDT) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.efficios.com 787472C8354 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=default; t=1593560661; bh=xwV0C8vTsPSlnmGbQy2hbyNyodmOGB/RFnq+mV8bBdg=; h=Date:From:To:Message-ID:MIME-Version; b=T+fNjcpVMrKbACA4plRKj2I5ve8mu9T+5e/l8MBCCFygcxdfisFxkQx2WbYgSW29N yuiLHpu2GnTeqJKCduT8rkJxFyPL3pWHy5gkyZ/9tDMSy7NL/nCpy10GIbkwN+EoEI 3dhiP3KFFeysI6Uy0czTdUeuBcxbWmYM7oeK37NzQd3RKUq7VYiRlkdj9OlQnh+pqt +RNSFFpQGonHhg9vzBRrFh+O5f5GnWOSr4HAGdEmqZzmCEC71cSurY9vgWL7NvQO4G wbYWacIw6RoZT6TA5R+9VO5EM+sesb2hX2OrKb0HaOifUM9S0h/3yG6SdCuDrTyJrD eW5WPd2/AZhGg== 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 w9K2b6F0Itho; Tue, 30 Jun 2020 19:44:21 -0400 (EDT) Received: from mail03.efficios.com (mail03.efficios.com [167.114.26.124]) by mail.efficios.com (Postfix) with ESMTP id 665DD2C85B5; Tue, 30 Jun 2020 19:44:21 -0400 (EDT) Date: Tue, 30 Jun 2020 19:44:21 -0400 (EDT) From: Mathieu Desnoyers To: Eric Dumazet , paulmck Cc: "David S. Miller" , Linus Torvalds , linux-kernel , netdev , Yuchung Cheng , Jonathan Rajotte-Julien Message-ID: <416125262.18159.1593560661355.JavaMail.zimbra@efficios.com> In-Reply-To: References: <20200630.134429.1590957032456466647.davem@davemloft.net> <474095696.17969.1593551866537.JavaMail.zimbra@efficios.com> 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: B06Xl8CKUYbxp+VQ6LJs8ecBAejacQ== Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ----- On Jun 30, 2020, at 6:38 PM, Eric Dumazet edumazet@google.com wrote: [...] > > For updates of keys, it seems existing code lacks some RCU care. > > MD5 keys use RCU for lookups/hashes, but the replacement of a key does > not allocate a new piece of memory. How is that RCU-safe ? Based on what I see here: tcp_md5_do_add() has a comment stating: "/* This can be called on a newly created socket, from other files */" which appears to be untrue if this can indeed be called on a live socket. The path for pre-existing keys does: key = tcp_md5_do_lookup_exact(sk, addr, family, prefixlen, l3index); if (key) { /* Pre-existing entry - just update that one. */ memcpy(key->key, newkey, newkeylen); key->keylen = newkeylen; return 0; } AFAIU, this works only if you assume there are no concurrent readers accessing key->key, else they can see a corrupted key. The change you are proposing adds smp_wmb/smp_rmb to pair stores to key before key_len with loads of key_len before key. I'm not sure what this is trying to achieve, and how it prevents the readers from observing a corrupted state if the key is updated on a live socket ? Based on my understanding, this path which deals with pre-existing keys in-place should only ever be used when there are no concurrent readers, else a new memory allocation would be needed to guarantee that readers always observe a valid copy. Thanks, Mathieu > > diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c > index > 810cc164f795f8e1e8ca747ed5df51bb20fec8a2..ecc0e3fabce8b03bef823cbfc5c1b0a9e24df124 > 100644 > --- a/net/ipv4/tcp.c > +++ b/net/ipv4/tcp.c > @@ -4034,9 +4034,12 @@ EXPORT_SYMBOL(tcp_md5_hash_skb_data); > int tcp_md5_hash_key(struct tcp_md5sig_pool *hp, const struct > tcp_md5sig_key *key) > { > struct scatterlist sg; > + u8 keylen = key->keylen; > > - sg_init_one(&sg, key->key, key->keylen); > - ahash_request_set_crypt(hp->md5_req, &sg, NULL, key->keylen); > + 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); > } > EXPORT_SYMBOL(tcp_md5_hash_key); > diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c > index > ad6435ba6d72ffd8caf783bb25cad7ec151d6909..99916fcc15ca0be12c2c133ff40516f79e6fdf7f > 100644 > --- a/net/ipv4/tcp_ipv4.c > +++ b/net/ipv4/tcp_ipv4.c > @@ -1113,6 +1113,9 @@ int tcp_md5_do_add(struct sock *sk, const union > tcp_md5_addr *addr, > if (key) { > /* Pre-existing entry - just update that one. */ > memcpy(key->key, newkey, newkeylen); > + > + smp_wmb(); /* pairs with smp_rmb() in tcp_md5_hash_key() */ > + > key->keylen = newkeylen; > return 0; > } -- Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com