Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp619014pxb; Fri, 29 Oct 2021 16:55:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJweKwEU7pWafT4OeOqN4F9wmmBpnel5Wvip6SCRBS2zdZNoB5EsDqS+gW46YEwL/zcHInmx X-Received: by 2002:a17:907:2d8e:: with SMTP id gt14mr16788762ejc.335.1635551712471; Fri, 29 Oct 2021 16:55:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635551712; cv=none; d=google.com; s=arc-20160816; b=UuqfP56qcxauUJydOmH3Knxy0EwKZdSHgsm0VcyQwUbLrDSJCC1o8eNv7xJ+vGcCCW lwJ2uqPeJtTfPfQO+jOzFHchmnpRCX195Kj6eCfJX4S0U9IhBkmU7s1yM3Gz4Jav/buj p9h8QzPaSJ6WZYS16yVn6zKSFxbY9RrA4n+Ig95IorUF4dsF8ugwOdP51mdwcibIpdAA SW4qHN0dboH8Ukahy+RsfPKz5xBWDl+18jBhlgAcnrolkZVONXBX0BEAWr+Jiuv1Kqn6 E/oowgDSqV1AeIHOK/pMeGs9PCpQ/8PAjDCwYcQBiZcVtcUsO6Fitvn+9h1u9+nvJdhw QjIA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=ut6pn5cliGqKJyxhLk39cGyPq9pAH/Drj266MOylggY=; b=Sf7ujeJB8teUFfsESXR20y3dGn274t1zsuG+yGLwQIYc6VWKTDCs5WHve1PSZ/Gpmb HkzCF2bAYDUvwv8Q56CP3MzduxO+xHJUX8lWPll3MY2nnaF/s/fWdhCF+yV0Cx6kJk7V rACcVxWOIOBP8hrIZy8IvbsEDn6yWpOlpvK0l7FFnPgEWAJc6K5AA1gFXPs/QjQgUsoi uQx7f3UZSMcaHak2WXfkn0wFZoE40rOD3+7Busd4Nz6Pu4BTCFTJoMdE/BOBY2H74gbH XgBws36H8QuAam20Nq1/8ElHgPBYBKiosZAJ1Dg31FpnnFRWUwT1gfs2VsP/aOcqKeSO iVJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=QaB65rFy; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w7si10534978ejy.356.2021.10.29.16.54.48; Fri, 29 Oct 2021 16:55:12 -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=@gmail.com header.s=20210112 header.b=QaB65rFy; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231592AbhJ2Xzh (ORCPT + 99 others); Fri, 29 Oct 2021 19:55:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231154AbhJ2Xzg (ORCPT ); Fri, 29 Oct 2021 19:55:36 -0400 Received: from mail-oi1-x22c.google.com (mail-oi1-x22c.google.com [IPv6:2607:f8b0:4864:20::22c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 250A4C061570; Fri, 29 Oct 2021 16:53:07 -0700 (PDT) Received: by mail-oi1-x22c.google.com with SMTP id g125so15604180oif.9; Fri, 29 Oct 2021 16:53:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :cc:references:from:in-reply-to:content-transfer-encoding; bh=ut6pn5cliGqKJyxhLk39cGyPq9pAH/Drj266MOylggY=; b=QaB65rFyylww/otFLw4EZbF7r6sgxqFL1f9BsRxQVff5RCyCrDpCuO4ulCuDxw31ej piM95O/fr1PFSsz6yzMwtHX04tBY9gk+EiBjUOWGRBwgH3uIlEgfNtL20oBaXs3LtBrv fN5FWr5HqTVReG7b8+WaUpmBNPGmVZPBdNKZa2MVJiKwD3h+RtgKu9f1u3x986YF7yCy z0wkqzr4JJRb6y6vS7vXfjvkpYoH3oJk0ToM7RC1hyaR5q8NWdEmzRrKMpDzLfTRDF7s BxOpdBSn6uv9iW//u2iDPYbPdFVHqvVKG7S2dUus9z0BnU0kjzoZFUgyy2VvUIT5ZBBM Oyig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=ut6pn5cliGqKJyxhLk39cGyPq9pAH/Drj266MOylggY=; b=AC4LWqCkvt/2rcqMr2IBcpBPnAi24G4SmcO4JnBqfqMqV0pelTz4oSYVtw5wkjUPtM 32KCni7xPovAoi5uYGZy53Y9OnUbnhwIxolpfNYa9qNkMpWcSCG9Qk8/FcrVu0e+td3U QDhMm9Uq4HIqKpwC5/U0oH+5pwua8u9jxmvEGH4Dx+fP+7WGubuXTBSCoK6nomRsmCxw YansCgJ1m3LLoA0/nPiRlJrZIWF72sljB/l2WjxZ+e6w3EsBm0WrhUk2DKsgW+QYoJ0K Okt4yvq2SersInUN0E6sKXl59AZ1c3mV1opFSXjB4vyu0hK24my6JeZtXrNodSiK2vDz dMTQ== X-Gm-Message-State: AOAM532IA03LuugxvhhnqvH/FY53XuV7BuYIXG/tqcMSgfINaxxAQ8yH 04SC/23nQwRYxlMvXNdybyA= X-Received: by 2002:aca:31c9:: with SMTP id x192mr15849915oix.173.1635551586095; Fri, 29 Oct 2021 16:53:06 -0700 (PDT) Received: from [172.16.0.2] ([8.48.134.30]) by smtp.googlemail.com with ESMTPSA id r131sm2386537oib.27.2021.10.29.16.53.04 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 29 Oct 2021 16:53:05 -0700 (PDT) Message-ID: <9015da81-689a-5ff6-c5ca-55c28dec1867@gmail.com> Date: Fri, 29 Oct 2021 17:53:03 -0600 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.2.1 Subject: Re: Kernel leaks memory in ip6_dst_cache when suppress_prefix is present in ipv6 routing rules and a `fib` rule is present in ipv6 nftables rules Content-Language: en-US To: msizanoen , davem@davemloft.net, yoshfuji@linux-ipv6.org, dsahern@kernel.org, kuba@kernel.org Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org References: From: David Ahern In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 10/26/21 8:24 AM, msizanoen wrote: > The kernel leaks memory when a `fib` rule is present in ipv6 nftables > firewall rules and a suppress_prefix rule > is present in the IPv6 routing rules (used by certain tools such as > wg-quick). In such scenarios, every incoming > packet will leak an allocation in ip6_dst_cache slab cache. > > After some hours of `bpftrace`-ing and source code reading, I tracked > down the issue to this commit: >     https://github.com/torvalds/linux/commit/ca7a03c4175366a92cee0ccc4fec0038c3266e26 > > > The problem with that patch is that the generic args->flags always have > FIB_LOOKUP_NOREF set[1][2] but the > ip6-specific flag RT6_LOOKUP_F_DST_NOREF might not be specified, leading > to fib6_rule_suppress not > decreasing the refcount when needed. This can be fixed by exposing the > protocol-specific flags to the > protocol specific `suppress` function, and check the protocol-specific > `flags` argument for > RT6_LOOKUP_F_DST_NOREF instead of the generic FIB_LOOKUP_NOREF when > decreasing the refcount. > > How to reproduce: > - Add the following nftables rule to a prerouting chain: `meta nfproto > ipv6 fib saddr . mark . iif oif missing drop` exact command? I have not played with nftables. Do you have a stack trace of where the dst reference is getting taken? > - Run `sudo ip -6 rule add table main suppress_prefixlength 0` > - Watch `sudo slabtop -o | grep ip6_dst_cache` memory usage increase > with every incoming ipv6 packet > > Example > patch:https://gist.github.com/msizanoen1/36a2853467a9bd34fadc5bb3783fde0f > > [1]:https://github.com/torvalds/linux/blob/ca7a03c4175366a92cee0ccc4fec0038c3266e26/net/ipv6/fib6_rules.c#L71 > > [2]:https://github.com/torvalds/linux/blob/ca7a03c4175366a92cee0ccc4fec0038c3266e26/net/ipv6/fib6_rules.c#L99 > > >