Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp576255img; Fri, 22 Mar 2019 04:22:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqz0rAuI8J22TQgJnh+j3coSWjEqsK41UpBPZZ4cmDzm/YOK1eTeWLGxw/KHq5iq3rWsuCiO X-Received: by 2002:a17:902:2a89:: with SMTP id j9mr8719815plb.272.1553253778818; Fri, 22 Mar 2019 04:22:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553253778; cv=none; d=google.com; s=arc-20160816; b=CKwWz93uMDUPOV/dOb+8qnGnl3k9Imbh5DqSNpzuYU46YGrF+nn/nIj6CotajSO8ba 8IE8tG1cv2Jr5IqVW7I59xcxmfIRgrycOKyE6xn+vFcu1BjJe8P6+m+VtZISiZ6QqOUe L1zrut+hePDOc+RbRWZtaurd1qaKfpwPr8XGQycu9IVJoc7qe598RvfbHYbvZYmg4TE+ kUUCUVOjn3bRFQ0aZFICHDJ2MUdhbjjqTF2QCxbELsyN0T0y9oPunSq8tru+/HvWcg4Q 73gm4A15qXxMIygvLBMNsERy/cWVQ6LI8XGMtZEgbYw7GYBjnr+TNx4mHBf3mB8R9kVI h+xQ== 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=TcscHpOdNj3qlQmyvgIz0ICWIn7+oyyBM6xfcCnYOzU=; b=HHeBxZo6BdcjIusFx5oXq4vHZ2f0VnfmKaZ3iSpGBc1exc7zwYm1rBpukzLzMFVDtS Spy2fzFftbFcioykVxgcgH8xiTY9h6ps88qev/wJM2DXd1f+WR/tPZCDjpIT6sGc16IP v5fVFZC4XYwvzncJcttyMri7hHun4GlII2MVX1GFM34m1zvs7IIF324OXe2lxj9cuD+n hOWdTafy+szVEUJlcS1cQNsn1PZGwZEHep9uOOH+i0nwAKeSnO209SvKQQUdILPDlJv6 WJnm7dPeJpm4IHn8DnLJROzkcb2W4B6Y7BXbJF79N9rziHRFUgoYmWfgBH7Lv8f5vr9n sixQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Ki17kg3F; 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 31si6947407plb.39.2019.03.22.04.22.44; Fri, 22 Mar 2019 04:22: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=Ki17kg3F; 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 S1728620AbfCVLVd (ORCPT + 99 others); Fri, 22 Mar 2019 07:21:33 -0400 Received: from mail.kernel.org ([198.145.29.99]:48044 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728605AbfCVLVc (ORCPT ); Fri, 22 Mar 2019 07:21:32 -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 C88F1218D4; Fri, 22 Mar 2019 11:21:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553253691; bh=3Sgdnf3koeXeTMaPS3J74eXibG0SHxcWUNDSWmsQVkE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ki17kg3FxDhdL9rz3mg4q4CbtEXuySJ8XAybN1QduiReGTbCSyZq9v0W1kOqKF8HH YnmQ2IWJHu9g9fM02g0/RnQdw+zsrcoPuKFcaBVSJSD+nBZrwHfZBa3f2WcQnGpDi4 sHPdaYH1NYs51yhZLacbUGxw15JG73uKDHuKOe7U= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ronen Arad , "David S. Miller" , Mark Salyzyn Subject: [PATCH 3.18 024/134] netlink: Trim skb to alloc size to avoid MSG_TRUNC Date: Fri, 22 Mar 2019 12:13:57 +0100 Message-Id: <20190322111211.573883178@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190322111210.465931067@linuxfoundation.org> References: <20190322111210.465931067@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore 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 3.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Arad, Ronen commit db65a3aaf29ecce2e34271d52e8d2336b97bd9fe upstream. netlink_dump() allocates skb based on the calculated min_dump_alloc or a per socket max_recvmsg_len. min_alloc_size is maximum space required for any single netdev attributes as calculated by rtnl_calcit(). max_recvmsg_len tracks the user provided buffer to netlink_recvmsg. It is capped at 16KiB. The intention is to avoid small allocations and to minimize the number of calls required to obtain dump information for all net devices. netlink_dump packs as many small messages as could fit within an skb that was sized for the largest single netdev information. The actual space available within an skb is larger than what is requested. It could be much larger and up to near 2x with align to next power of 2 approach. Allowing netlink_dump to use all the space available within the allocated skb increases the buffer size a user has to provide to avoid truncaion (i.e. MSG_TRUNG flag set). It was observed that with many VLANs configured on at least one netdev, a larger buffer of near 64KiB was necessary to avoid "Message truncated" error in "ip link" or "bridge [-c[ompressvlans]] vlan show" when min_alloc_size was only little over 32KiB. This patch trims skb to allocated size in order to allow the user to avoid truncation with more reasonable buffer size. Signed-off-by: Ronen Arad Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman Signed-off-by: Mark Salyzyn --- net/netlink/af_netlink.c | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) --- a/net/netlink/af_netlink.c +++ b/net/netlink/af_netlink.c @@ -1977,6 +1977,7 @@ static int netlink_dump(struct sock *sk) struct nlmsghdr *nlh; struct module *module; int err = -ENOBUFS; + int alloc_min_size; int alloc_size; mutex_lock(nlk->cb_mutex); @@ -1985,9 +1986,6 @@ static int netlink_dump(struct sock *sk) goto errout_skb; } - cb = &nlk->cb; - alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE); - if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf) goto errout_skb; @@ -1996,22 +1994,34 @@ static int netlink_dump(struct sock *sk) * to reduce number of system calls on dump operations, if user * ever provided a big enough buffer. */ - if (alloc_size < nlk->max_recvmsg_len) { - skb = netlink_alloc_skb(sk, - nlk->max_recvmsg_len, - nlk->portid, + cb = &nlk->cb; + alloc_min_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE); + + if (alloc_min_size < nlk->max_recvmsg_len) { + alloc_size = nlk->max_recvmsg_len; + skb = netlink_alloc_skb(sk, alloc_size, nlk->portid, (GFP_KERNEL & ~__GFP_WAIT) | __GFP_NOWARN | __GFP_NORETRY); - /* available room should be exact amount to avoid MSG_TRUNC */ - if (skb) - skb_reserve(skb, skb_tailroom(skb) - - nlk->max_recvmsg_len); } - if (!skb) + if (!skb) { + alloc_size = alloc_min_size; skb = netlink_alloc_skb(sk, alloc_size, nlk->portid, (GFP_KERNEL & ~__GFP_WAIT)); + } if (!skb) goto errout_skb; + + /* Trim skb to allocated size. User is expected to provide buffer as + * large as max(min_dump_alloc, 16KiB (mac_recvmsg_len capped at + * netlink_recvmsg())). dump will pack as many smaller messages as + * could fit within the allocated skb. skb is typically allocated + * with larger space than required (could be as much as near 2x the + * requested size with align to next power of 2 approach). Allowing + * dump to use the excess space makes it difficult for a user to have a + * reasonable static buffer based on the expected largest dump of a + * single netdev. The outcome is MSG_TRUNC error. + */ + skb_reserve(skb, skb_tailroom(skb) - alloc_size); netlink_skb_set_owner_r(skb, sk); if (nlk->dump_done_errno > 0)