Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3577065pxb; Mon, 24 Jan 2022 12:38:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJzj0JHtIbow5PE6MxjmoxPEnIOqiXMpRMg2GGlujzQQh/FjuxYjHy/LNcJuETtXXC2tu4F+ X-Received: by 2002:a05:6a00:23c1:b0:4c9:f220:2379 with SMTP id g1-20020a056a0023c100b004c9f2202379mr3157330pfc.65.1643056710577; Mon, 24 Jan 2022 12:38:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643056710; cv=none; d=google.com; s=arc-20160816; b=vg9F60tQZA13Euq8/JSnYy0BW90AlN9B8Nn59C/pm3RFOJBDce1u4RjOXzjw8svz7r sb7wGwXCkwdGrqy/lL/34bIht84BVYd2zeJuvS/HkY6eIfaCOFk0v4Eua6bGfiW0/00d JurRrMv265Pn7ziRKRNyuCJqibwvTzJIT2PID7NqlhPxUVyyABZEuzcIVTaX6eq3BA3y WhFFAlDFeMDoIYm1uHzNiRhbOLaA8XDbExlaFI+1RFDamuZIhgRxyLqPplgAPuZOq1Zj EUKCdPyr64aF+IhOgQwBfmxQuT5X5wJom7DiS9R5/CDBNjy1Ay87pO3CwxCSJ0Ws6JD/ RXXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=4Etwnkcs/F/JXA5yMtJdkLiBeS7j4JGxiL719SNpuNw=; b=KNkPL7KxKcr6CPPkAugAVfb8x1N9xRbNJke5KbZoUoM0wAe5/Hu2uqEvlbla2jUCsr ydaKR6BuIDUWgx4bOWHz4chki6Yy2Nl/y9w20mvBakuchHllTLveV2utUd2ksxizpk/9 51gWfNrBQwReYxcHm9GYcHN6MIgPam7m+u1KvJw1ASPCoteO6aOsKJdu8YUZmvAToHou /kMLFBH+7I7VjyQOVh95tBoEEjowpe9vLt3d/fOFx/mNpD6GuuOSuJsWM4l7NXt9WrvS +iC0qLRIWE7pSCPrLR7925sITJhHGBoL3f2jG/Zpevg+TszNehAxRTnCU5K9En0juy2G 7usw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=yIlFtQZT; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k8si14848452pgc.268.2022.01.24.12.38.11; Mon, 24 Jan 2022 12:38:30 -0800 (PST) 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=@linuxfoundation.org header.s=korg header.b=yIlFtQZT; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357354AbiAXTtw (ORCPT + 99 others); Mon, 24 Jan 2022 14:49:52 -0500 Received: from dfw.source.kernel.org ([139.178.84.217]:34990 "EHLO dfw.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354870AbiAXTjc (ORCPT ); Mon, 24 Jan 2022 14:39:32 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 141506141C; Mon, 24 Jan 2022 19:39:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EEAFEC340E5; Mon, 24 Jan 2022 19:39:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643053171; bh=4KubBFmDsVrKB9scUEaiucazOFxFpJPrDKsfzlgOojM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=yIlFtQZTnd4p1ZIlrUGQg+W0f584gekzdSAC5+nzzL9WPoO4zN7X/k+kRsvJcyRfK 0w+n9gkMqDEQtzEhO2hXMQ49p6gNvZPAbevCBp3/ktx3pdSYEbbST828dB0OLuWslx eM2H84d0qhtpuOTQ6kZ5aPLluSMqVutkBS5Fmv8M= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Dumazet , "David S. Miller" Subject: [PATCH 5.4 299/320] inet: frags: annotate races around fqdir->dead and fqdir->high_thresh Date: Mon, 24 Jan 2022 19:44:43 +0100 Message-Id: <20220124184004.126011562@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124183953.750177707@linuxfoundation.org> References: <20220124183953.750177707@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eric Dumazet commit 91341fa0003befd097e190ec2a4bf63ad957c49a upstream. Both fields can be read/written without synchronization, add proper accessors and documentation. Fixes: d5dd88794a13 ("inet: fix various use-after-free in defrags units") Signed-off-by: Eric Dumazet Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- include/net/inet_frag.h | 11 +++++++++-- include/net/ipv6_frag.h | 3 ++- net/ipv4/inet_fragment.c | 8 +++++--- net/ipv4/ip_fragment.c | 3 ++- 4 files changed, 18 insertions(+), 7 deletions(-) --- a/include/net/inet_frag.h +++ b/include/net/inet_frag.h @@ -116,8 +116,15 @@ int fqdir_init(struct fqdir **fqdirp, st static inline void fqdir_pre_exit(struct fqdir *fqdir) { - fqdir->high_thresh = 0; /* prevent creation of new frags */ - fqdir->dead = true; + /* Prevent creation of new frags. + * Pairs with READ_ONCE() in inet_frag_find(). + */ + WRITE_ONCE(fqdir->high_thresh, 0); + + /* Pairs with READ_ONCE() in inet_frag_kill(), ip_expire() + * and ip6frag_expire_frag_queue(). + */ + WRITE_ONCE(fqdir->dead, true); } void fqdir_exit(struct fqdir *fqdir); --- a/include/net/ipv6_frag.h +++ b/include/net/ipv6_frag.h @@ -67,7 +67,8 @@ ip6frag_expire_frag_queue(struct net *ne struct sk_buff *head; rcu_read_lock(); - if (fq->q.fqdir->dead) + /* Paired with the WRITE_ONCE() in fqdir_pre_exit(). */ + if (READ_ONCE(fq->q.fqdir->dead)) goto out_rcu_unlock; spin_lock(&fq->q.lock); --- a/net/ipv4/inet_fragment.c +++ b/net/ipv4/inet_fragment.c @@ -204,9 +204,9 @@ void inet_frag_kill(struct inet_frag_que /* The RCU read lock provides a memory barrier * guaranteeing that if fqdir->dead is false then * the hash table destruction will not start until - * after we unlock. Paired with inet_frags_exit_net(). + * after we unlock. Paired with fqdir_pre_exit(). */ - if (!fqdir->dead) { + if (!READ_ONCE(fqdir->dead)) { rhashtable_remove_fast(&fqdir->rhashtable, &fq->node, fqdir->f->rhash_params); refcount_dec(&fq->refcnt); @@ -321,9 +321,11 @@ static struct inet_frag_queue *inet_frag /* TODO : call from rcu_read_lock() and no longer use refcount_inc_not_zero() */ struct inet_frag_queue *inet_frag_find(struct fqdir *fqdir, void *key) { + /* This pairs with WRITE_ONCE() in fqdir_pre_exit(). */ + long high_thresh = READ_ONCE(fqdir->high_thresh); struct inet_frag_queue *fq = NULL, *prev; - if (!fqdir->high_thresh || frag_mem_limit(fqdir) > fqdir->high_thresh) + if (!high_thresh || frag_mem_limit(fqdir) > high_thresh) return NULL; rcu_read_lock(); --- a/net/ipv4/ip_fragment.c +++ b/net/ipv4/ip_fragment.c @@ -144,7 +144,8 @@ static void ip_expire(struct timer_list rcu_read_lock(); - if (qp->q.fqdir->dead) + /* Paired with WRITE_ONCE() in fqdir_pre_exit(). */ + if (READ_ONCE(qp->q.fqdir->dead)) goto out_rcu_unlock; spin_lock(&qp->q.lock);