Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp210535pxb; Wed, 20 Jan 2021 05:18:49 -0800 (PST) X-Google-Smtp-Source: ABdhPJwpvK9W6wXjON/S0VB8kHydNC5txHPm3JrYCZfdLCS47/SPy7lCiz//jsKBV712aEKDpzr3 X-Received: by 2002:aa7:d4c8:: with SMTP id t8mr7713060edr.199.1611148729100; Wed, 20 Jan 2021 05:18:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611148729; cv=none; d=google.com; s=arc-20160816; b=0Usm8TnzA8CWoCbH7Mi+1LIFCyUI9ZcluBw1yQzz9uTWV4kdHC7Oz+3BYU9OAZRGTL kcwve8WrQF/ZlXW/c6qnrarACzhTE+wIsSox5VxtJAvA7yx6+i06DPFSyVw9bhGmkzT+ C/Z49IjALWSVVsh3pVxGAcTOTXbd4BFBAdGQyXulLcxDOJRMeclEuuAeAfxXscz7PLy3 XZJwWM5UE+uPU+bs+hIhvHLr/e5OMRMlZF+LHy326CtRUcNNI1HhL6a1Flxs3AVDzDKG 8NlW6Qi9uImEvoQW3A2K+kSepwlUD2AkhmdCA0APQsKWi1aMzZ+GjCg1ZJ5bEG1XOune s/6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=TxVlKu1fTmmCpX79Lm0VHrpC5j04Mcyz1Kh6xWJDeqo=; b=Bp4fTXo/RmIsjT24E0aVOWA8OdDECMGhjX6WRx8eL6Czn1Vw9yTf2XtXsyIbG2wOTI ZzzoZBi7RpDX7mAFk1GT3RiygpXTe7zJZ+PrqcJSlWDrYA9M9nGUR2rI7KNpIDvME2fO dYGDXgjDVccNE3Cws1eXrpdVPt95esgOPhWnjZ/pRqopPIv0EhS20U6rvrCVNrD3w+Vg KgUmFalGEd32WgJl+i1JzpaIC9rWaabxSM+DuhI3D7asEgsnkVHTepOgx7uueoTwsnFr qZaBbz9B36Q+KHEn/lXvvVrfo0XghC5B6PvJDHVGBAhyiGQoplKJvSkdzmQf/V0bsuvb DVxw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=J1YZxyHV; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h14si637345ejt.198.2021.01.20.05.18.24; Wed, 20 Jan 2021 05:18:49 -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=@google.com header.s=20161025 header.b=J1YZxyHV; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731521AbhATNRe (ORCPT + 99 others); Wed, 20 Jan 2021 08:17:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732605AbhATNI2 (ORCPT ); Wed, 20 Jan 2021 08:08:28 -0500 Received: from mail-io1-xd36.google.com (mail-io1-xd36.google.com [IPv6:2607:f8b0:4864:20::d36]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46A63C0613C1 for ; Wed, 20 Jan 2021 05:07:48 -0800 (PST) Received: by mail-io1-xd36.google.com with SMTP id h11so6793456ioh.11 for ; Wed, 20 Jan 2021 05:07:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=TxVlKu1fTmmCpX79Lm0VHrpC5j04Mcyz1Kh6xWJDeqo=; b=J1YZxyHV2scGRU7022bnASDjf5zU28bwzCuezhWalOeXKl4ayTLsUok6SRLnNDJSg5 Y1B6qIzL6cdTZPUcadl6OGtiA+BpTnpFfwMtw/iTjtI8BDpAiDHX5/NhFRHEHKBMQDfz G04raxx8XqxUzrrORUO6/mLzUW8adETQCbCfOviT4/z8slA6Jp6KyZ5XhEWCIL3RhPyi v+NE/OOvRzzgBNBu75VKWvbiyKNO8HGLIqQRDFC5va+0di2TtJUEpuahSuWcWsuQxyTM FyEiuhYG7w1bGRTCKNA9Nvr30BH5dNq89obTwc8BI/EU1iXC+6OcQkOhWRbgEKWx5p1p Wunw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=TxVlKu1fTmmCpX79Lm0VHrpC5j04Mcyz1Kh6xWJDeqo=; b=MxLMgDXwhDaMHhRQlEfIyw+uE+HSx7QDHpfedENrhr3kgpOq6okiCANR4mijE/K/gj i9jeyM0fslWcfTjS6PEMYfpOeO+021GcbVac+feNA40+7NqMQDrOhvG/PKdI6+LYoo1J T9RA0IMyH2QqtrxYJZQy4sfLnPADIBe59oBmYV5FoB6eT56AsMnHUX57LUdda4rH3NVH fjSu8qEDQi+qgi4yElt/F4AY7k54VsnhG9/9fZ2g63Y1BuHSlDlH6eL/besVoeikLfQY +ER+2TMWx/kqgjhU8CK4Jl/nMmZuMkJ2t7P9ADIKmQSN2cWs27IZLTquIn4rnIqpHe44 iAFA== X-Gm-Message-State: AOAM530d8PtHaQO70b1Lfz5PPbBQdBWiJBeIyN+//0rmJsJyMneP5C8u PjFbK0vMvdc3FjlylX2HXBhmTFfNmRlNVRvu6YMEeg== X-Received: by 2002:a5d:8a02:: with SMTP id w2mr6955472iod.157.1611148067266; Wed, 20 Jan 2021 05:07:47 -0800 (PST) MIME-Version: 1.0 References: <20210118055920.82516-1-kuniyu@amazon.co.jp> <20210119171745.6840e3a5@kicinski-fedora-pc1c0hjn.dhcp.thefacebook.com> In-Reply-To: <20210119171745.6840e3a5@kicinski-fedora-pc1c0hjn.dhcp.thefacebook.com> From: Eric Dumazet Date: Wed, 20 Jan 2021 14:07:35 +0100 Message-ID: Subject: Re: [PATCH net] tcp: Fix potential use-after-free due to double kfree(). To: Jakub Kicinski Cc: Kuniyuki Iwashima , "David S . Miller" , Hideaki YOSHIFUJI , netdev , LKML , Kuniyuki Iwashima , Benjamin Herrenschmidt , Ricardo Dias Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jan 20, 2021 at 2:17 AM Jakub Kicinski wrote: > > On Mon, 18 Jan 2021 14:59:20 +0900 Kuniyuki Iwashima wrote: > > Receiving ACK with a valid SYN cookie, cookie_v4_check() allocates struct > > request_sock and then can allocate inet_rsk(req)->ireq_opt. After that, > > tcp_v4_syn_recv_sock() allocates struct sock and copies ireq_opt to > > inet_sk(sk)->inet_opt. Normally, tcp_v4_syn_recv_sock() inserts the full > > socket into ehash and sets NULL to ireq_opt. Otherwise, > > tcp_v4_syn_recv_sock() has to reset inet_opt by NULL and free the full > > socket. > > > > The commit 01770a1661657 ("tcp: fix race condition when creating child > > sockets from syncookies") added a new path, in which more than one cores > > create full sockets for the same SYN cookie. Currently, the core which > > loses the race frees the full socket without resetting inet_opt, resulting > > in that both sock_put() and reqsk_put() call kfree() for the same memory: > > > > sock_put > > sk_free > > __sk_free > > sk_destruct > > __sk_destruct > > sk->sk_destruct/inet_sock_destruct > > kfree(rcu_dereference_protected(inet->inet_opt, 1)); > > > > reqsk_put > > reqsk_free > > __reqsk_free > > req->rsk_ops->destructor/tcp_v4_reqsk_destructor > > kfree(rcu_dereference_protected(inet_rsk(req)->ireq_opt, 1)); > > > > Calling kmalloc() between the double kfree() can lead to use-after-free, so > > this patch fixes it by setting NULL to inet_opt before sock_put(). > > > > As a side note, this kind of issue does not happen for IPv6. This is > > because tcp_v6_syn_recv_sock() clones both ipv6_opt and pktopts which > > correspond to ireq_opt in IPv4. > > > > Fixes: 01770a166165 ("tcp: fix race condition when creating child sockets from syncookies") > > CC: Ricardo Dias > > Signed-off-by: Kuniyuki Iwashima > > Reviewed-by: Benjamin Herrenschmidt > > Ricardo, Eric, any reason this was written this way? Well, I guess that was a plain bug. IPv4 options are not used often I think. Reviewed-by: Eric Dumazet