Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp899658yba; Thu, 18 Apr 2019 11:26:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqzj1JjKbQKn8n9ai/P8eeSkwAJCALBqWeiM9hgIXfly0dzKJVDEljSSUoR+8IdN3c9mOuAg X-Received: by 2002:a17:902:441:: with SMTP id 59mr22999301ple.242.1555612018222; Thu, 18 Apr 2019 11:26:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555612018; cv=none; d=google.com; s=arc-20160816; b=d2zjjn9CM1SIsBJDyIVBS4UE8UsiV2Mrivt7FoC0wYnavI/5gzfVPDpoFoR9UKqz/e f1Wl31DQ4EpgG1G/3tUG1bIpDxxrkn0BzBcQJkjDfAZ0yvE52QlVv2CM0M5SgV8BoWuW edf4aLb8q6echYNmk8j/71VYwHIJ179lmnSXEnHhCSG9puhL69M8NAY8pU+zoR9mhmNa mh63uEugwTS4bWHrCbQWCmrdnK4zfUtEpGPb88n3XotCjlgvEt/lnIPaXRQbm89W4brh 3CI1OuEenW5hJuD9IPl8dvvxnmCrmGPPl7qmQLINDahgeeskDEH4jVbMRx0J3dV2eL9t jGzA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=w4fIH8kRnA+9WZxXBnhst1wW0p4jcPafXFCqkRJCSkE=; b=bBiAun1VCKhkM1TEmVaMc07ZWXVrG98yNUk24pkucvkRXye2ESOsdwe5bPVoDKpfuJ sSrrPPOJeHTf3SvFRsmE57T5qi/wCUvPKyBu841Uhx5ejOcohr4eRRE0vBGb+XUsyaxs 9l5nPiThI5tOh7dar5WN/SPzUY/QHRsYrRnc8fqD3xRJ/nnboG0V1xSyJp01pjuEEC0N XPlPnR4gsmfD/fpk1SAUQrAcFVSHaEzE7MYVkwOEKabEVklaTO9VcCteKwV0kJvO6eSB 6a18KVjGnAXWBx6+dNf/2rfkRskJT5tOQGDZFtE3FGoBsHXkZxPiqmfD45mW75OIqLPV bDnw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=C9SxBNir; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 125si2362616pgc.220.2019.04.18.11.26.43; Thu, 18 Apr 2019 11:26:58 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=C9SxBNir; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391350AbfDRSYE (ORCPT + 99 others); Thu, 18 Apr 2019 14:24:04 -0400 Received: from mail.kernel.org ([198.145.29.99]:35790 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390029AbfDRSGI (ORCPT ); Thu, 18 Apr 2019 14:06:08 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 5DB4E218A6; Thu, 18 Apr 2019 18:06:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1555610766; bh=GJi2JNKo/h+hbFSyFs6tMdNJopHV2QNqFz8bhKeIfQ4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=C9SxBNirl5Q0Dwo9jSboqI/cHoe/acxn0HzH2Z5R4bPUJ5MOCKOTlHdZWFDw9MH+Q TH+Ic1WYrhKN3sKxuKuiO8cda7BxWZhSCltXWuWUc8xExnzuYDL1SYyToBx60tlLuZ G+QepixsSYRtSm+z/MoLch4mswA+hO8dqk5IlftM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Tejun Heo , Pablo Neira Ayuso , Sasha Levin Subject: [PATCH 4.14 43/92] netfilter: xt_cgroup: shrink size of v2 path Date: Thu, 18 Apr 2019 19:57:01 +0200 Message-Id: <20190418160433.958865210@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190418160430.325165109@linuxfoundation.org> References: <20190418160430.325165109@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [ Upstream commit 0d704967f4a49cc2212350b3e4a8231f8b4283ed ] cgroup v2 path field is PATH_MAX which is too large, this is placing too much pressure on memory allocation for people with many rules doing cgroup v1 classid matching, side effects of this are bug reports like: https://bugzilla.kernel.org/show_bug.cgi?id=200639 This patch registers a new revision that shrinks the cgroup path to 512 bytes, which is the same approach we follow in similar extensions that have a path field. Cc: Tejun Heo Signed-off-by: Pablo Neira Ayuso Acked-by: Tejun Heo Signed-off-by: Sasha Levin --- include/uapi/linux/netfilter/xt_cgroup.h | 16 ++++++ net/netfilter/xt_cgroup.c | 72 ++++++++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/include/uapi/linux/netfilter/xt_cgroup.h b/include/uapi/linux/netfilter/xt_cgroup.h index e96dfa1b34f7..b74e370d6133 100644 --- a/include/uapi/linux/netfilter/xt_cgroup.h +++ b/include/uapi/linux/netfilter/xt_cgroup.h @@ -22,4 +22,20 @@ struct xt_cgroup_info_v1 { void *priv __attribute__((aligned(8))); }; +#define XT_CGROUP_PATH_MAX 512 + +struct xt_cgroup_info_v2 { + __u8 has_path; + __u8 has_classid; + __u8 invert_path; + __u8 invert_classid; + union { + char path[XT_CGROUP_PATH_MAX]; + __u32 classid; + }; + + /* kernel internal data */ + void *priv __attribute__((aligned(8))); +}; + #endif /* _UAPI_XT_CGROUP_H */ diff --git a/net/netfilter/xt_cgroup.c b/net/netfilter/xt_cgroup.c index 891f4e7e8ea7..db18c0177b0f 100644 --- a/net/netfilter/xt_cgroup.c +++ b/net/netfilter/xt_cgroup.c @@ -66,6 +66,38 @@ static int cgroup_mt_check_v1(const struct xt_mtchk_param *par) return 0; } +static int cgroup_mt_check_v2(const struct xt_mtchk_param *par) +{ + struct xt_cgroup_info_v2 *info = par->matchinfo; + struct cgroup *cgrp; + + if ((info->invert_path & ~1) || (info->invert_classid & ~1)) + return -EINVAL; + + if (!info->has_path && !info->has_classid) { + pr_info("xt_cgroup: no path or classid specified\n"); + return -EINVAL; + } + + if (info->has_path && info->has_classid) { + pr_info_ratelimited("path and classid specified\n"); + return -EINVAL; + } + + info->priv = NULL; + if (info->has_path) { + cgrp = cgroup_get_from_path(info->path); + if (IS_ERR(cgrp)) { + pr_info_ratelimited("invalid path, errno=%ld\n", + PTR_ERR(cgrp)); + return -EINVAL; + } + info->priv = cgrp; + } + + return 0; +} + static bool cgroup_mt_v0(const struct sk_buff *skb, struct xt_action_param *par) { @@ -95,6 +127,24 @@ static bool cgroup_mt_v1(const struct sk_buff *skb, struct xt_action_param *par) info->invert_classid; } +static bool cgroup_mt_v2(const struct sk_buff *skb, struct xt_action_param *par) +{ + const struct xt_cgroup_info_v2 *info = par->matchinfo; + struct sock_cgroup_data *skcd = &skb->sk->sk_cgrp_data; + struct cgroup *ancestor = info->priv; + struct sock *sk = skb->sk; + + if (!sk || !sk_fullsock(sk) || !net_eq(xt_net(par), sock_net(sk))) + return false; + + if (ancestor) + return cgroup_is_descendant(sock_cgroup_ptr(skcd), ancestor) ^ + info->invert_path; + else + return (info->classid == sock_cgroup_classid(skcd)) ^ + info->invert_classid; +} + static void cgroup_mt_destroy_v1(const struct xt_mtdtor_param *par) { struct xt_cgroup_info_v1 *info = par->matchinfo; @@ -103,6 +153,14 @@ static void cgroup_mt_destroy_v1(const struct xt_mtdtor_param *par) cgroup_put(info->priv); } +static void cgroup_mt_destroy_v2(const struct xt_mtdtor_param *par) +{ + struct xt_cgroup_info_v2 *info = par->matchinfo; + + if (info->priv) + cgroup_put(info->priv); +} + static struct xt_match cgroup_mt_reg[] __read_mostly = { { .name = "cgroup", @@ -130,6 +188,20 @@ static struct xt_match cgroup_mt_reg[] __read_mostly = { (1 << NF_INET_POST_ROUTING) | (1 << NF_INET_LOCAL_IN), }, + { + .name = "cgroup", + .revision = 2, + .family = NFPROTO_UNSPEC, + .checkentry = cgroup_mt_check_v2, + .match = cgroup_mt_v2, + .matchsize = sizeof(struct xt_cgroup_info_v2), + .usersize = offsetof(struct xt_cgroup_info_v2, priv), + .destroy = cgroup_mt_destroy_v2, + .me = THIS_MODULE, + .hooks = (1 << NF_INET_LOCAL_OUT) | + (1 << NF_INET_POST_ROUTING) | + (1 << NF_INET_LOCAL_IN), + }, }; static int __init cgroup_mt_init(void) -- 2.19.1