Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp143564ybt; Tue, 30 Jun 2020 16:45:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyknNQdvmcsJLDkADpMruGV5YzjQxn6tyzze8W/v1AN/ROw8xwkdUHcBwwaLGXkyRKAOGwS X-Received: by 2002:a17:907:9495:: with SMTP id dm21mr20230519ejc.357.1593560729899; Tue, 30 Jun 2020 16:45:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593560729; cv=none; d=google.com; s=arc-20160816; b=psgBaFkfLnRVFakeGM/Jkaea54zhgOFMxjGqAJbKTaQ5zaygwEQkx9t2VD7QvR6vyh CJ2Y8s3zVWpwhQIf2TjnP+l8B3W/kwl0NSYj41GyVc7lkUY20NEN12OPNyN4QKfrWTkn VWLKBSZSbBhbgdKb4WjpMpUYekMaXcV4tx8rumR6nhM5Zr0dyAWDuXutF5OXZhVH5+wH MBuRMYlA/Hta0EQxp1hMO6OD1GnXcOIWAgBbrYY3OSVhofZra4l0XowdxnhJI03soJjE gh++rO94yG0j/S6mXC2gEaNJl+5BALQd77Ey9x6FlnOaB7xTXlso8lGA+3pcFdGnvxZ8 wrCg== 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=xwV0C8vTsPSlnmGbQy2hbyNyodmOGB/RFnq+mV8bBdg=; b=CNjPyGqE1KxDignbtKplU+yWm68rOtslTM2CiZXyaJkjwEcm+rrXu98/++kh1pY3nP gGgdV75EHU1Pv3j1mG3p+Ol+dVoQcbt9ElTJQZXp4Nu03oqJmSjnJTX4yhfBj/xBO68j owMBLuQuDro4znaKXMvhLCBRG00j57/5d3UodlWTRzNM8Oi3wGxKU8G/o3bMjbJZwDv8 lMcXGsEYFx0NQaOkgPg1vlkQRe3KqkjxNm5TJlDSOy6FmaaShloDXiK/f1BHhP5AHzt5 aiptCfRB+1cRabitxb/gQxmJEIlCNoSvCtNgh+Bby+To0PqbuJu4xsFEn8Gc366bNk1U PO+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@efficios.com header.s=default header.b=T+fNjcpV; 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 f12si2753438ejf.650.2020.06.30.16.45.06; Tue, 30 Jun 2020 16:45:29 -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=T+fNjcpV; 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 S1726765AbgF3XoY (ORCPT + 99 others); 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