Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp983468pxb; Wed, 6 Apr 2022 06:02:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJysZSBjcVKSATDSbEZ3QIEkVsYLB8ZAEvelmkVJPkY8dFlSj2Rj8j73iEIvy10xjzyrJo5i X-Received: by 2002:a05:6602:26cd:b0:649:2bae:a63a with SMTP id g13-20020a05660226cd00b006492baea63amr4072844ioo.148.1649250123043; Wed, 06 Apr 2022 06:02:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649250123; cv=none; d=google.com; s=arc-20160816; b=qePUgT70PIR24BUHBS/QNJw0D9c+qtdpIKr6ArfDOAjc3GqjpPtaD+SEB/lWFs+O17 XI86Ccv+KpZKRYmwoIJFU7OaE+Ep+JmPm0aqQAqVikct9mn1HORbTw4zkgoE8rZYM4yA H5Ckv2w4Hvyjark2ZvLyfRQgK2qfhMKp8moiHbD+7hHJQ4+6hg3UetdDe3m0PpFhuRnQ 6EG0bRvbJOfIXXIpAbaalPBkTGlqsD4CH5qa/qFPV4BTO5kK+0bfWmz+QsWYqIroz8ZV VVhbpEzuXoplUNA2OJm0l+tGOZdfJhaFp7cwBFP9XAbwZVJV+Gpq854k+PAj1qRpFin4 pm+g== 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=Ws+1SknuCfvWkGHDvueCGyX5Ay65LXBKlD2SIOC8r9k=; b=fLrnK9f5pIg/g/MLEmxn8wECT8zS7BBdtwspItY5VGZhF+C1d3NYZz+qZGrA9sTLjb BydKlsIfzLw2VoiN89no2kthylCkDIKDhvjDtruxLvIMXgZHFQzE6YS4irruouNyS7F/ Klx7jV0irts5paKo7kW6mHhbKIb5ZzDi7NyO+Txzq7dPp1RC7bmjzKoQhN7zvNmiiboL TP8wfzjO3WDKp2PlTWBBwh06GN4K55fP2PzynptfVt1uNWrMpoYQV2kHhIY9bBBGglu8 ydmttfhgHA6kXVWD1meERTjmWkXykHPmImJjzbt3mPaEtakpIoEU2El/PVqf2LV/pIXo EcpQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=pmTpJlyY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id d8-20020a928748000000b002c7f51ab864si8116983ilm.46.2022.04.06.06.02.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 06:02:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=pmTpJlyY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E907B1B9315; Wed, 6 Apr 2022 03:18:56 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1839055AbiDFA6G (ORCPT + 99 others); Tue, 5 Apr 2022 20:58:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349006AbiDEJsz (ORCPT ); Tue, 5 Apr 2022 05:48:55 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 66459DF41; Tue, 5 Apr 2022 02:39:03 -0700 (PDT) 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 ams.source.kernel.org (Postfix) with ESMTPS id 80DC3B81B7F; Tue, 5 Apr 2022 09:39:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D0254C385A2; Tue, 5 Apr 2022 09:38:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649151540; bh=TIY/MaJG64dJFYhgSREZ7Io2V0kw+HnjbuZ5wtzkwkI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pmTpJlyYHfuOmgFW0VwPlHzwrVNL3zz7ir75eGGDTkrivWiWpdRpzt3K3Ias36kCQ 7aF3RwA2v2bu/LcW20kChCaWORhQe7F1gJodXo6pzc8IER51hNTFKgl5/go10kMDos 5ec0RiaLNlMSR2mGUgtI5DKAd935lepAJ6Jg1QoM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Zhiqian Guan , =?UTF-8?q?Toke=20H=C3=B8iland-J=C3=B8rgensen?= , Andrii Nakryiko , Kumar Kartikeya Dwivedi , Sasha Levin Subject: [PATCH 5.15 450/913] libbpf: Use dynamically allocated buffer when receiving netlink messages Date: Tue, 5 Apr 2022 09:25:12 +0200 Message-Id: <20220405070353.337403206@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070339.801210740@linuxfoundation.org> References: <20220405070339.801210740@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Toke Høiland-Jørgensen [ Upstream commit 9c3de619e13ee6693ec5ac74f50b7aa89056a70e ] When receiving netlink messages, libbpf was using a statically allocated stack buffer of 4k bytes. This happened to work fine on systems with a 4k page size, but on systems with larger page sizes it can lead to truncated messages. The user-visible impact of this was that libbpf would insist no XDP program was attached to some interfaces because that bit of the netlink message got chopped off. Fix this by switching to a dynamically allocated buffer; we borrow the approach from iproute2 of using recvmsg() with MSG_PEEK|MSG_TRUNC to get the actual size of the pending message before receiving it, adjusting the buffer as necessary. While we're at it, also add retries on interrupted system calls around the recvmsg() call. v2: - Move peek logic to libbpf_netlink_recv(), don't double free on ENOMEM. Fixes: 8bbb77b7c7a2 ("libbpf: Add various netlink helpers") Reported-by: Zhiqian Guan Signed-off-by: Toke Høiland-Jørgensen Signed-off-by: Andrii Nakryiko Acked-by: Kumar Kartikeya Dwivedi Link: https://lore.kernel.org/bpf/20220211234819.612288-1-toke@redhat.com Signed-off-by: Sasha Levin --- tools/lib/bpf/netlink.c | 55 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/tools/lib/bpf/netlink.c b/tools/lib/bpf/netlink.c index 39f25e09b51e..69b353d55dbf 100644 --- a/tools/lib/bpf/netlink.c +++ b/tools/lib/bpf/netlink.c @@ -87,29 +87,75 @@ enum { NL_DONE, }; +static int netlink_recvmsg(int sock, struct msghdr *mhdr, int flags) +{ + int len; + + do { + len = recvmsg(sock, mhdr, flags); + } while (len < 0 && (errno == EINTR || errno == EAGAIN)); + + if (len < 0) + return -errno; + return len; +} + +static int alloc_iov(struct iovec *iov, int len) +{ + void *nbuf; + + nbuf = realloc(iov->iov_base, len); + if (!nbuf) + return -ENOMEM; + + iov->iov_base = nbuf; + iov->iov_len = len; + return 0; +} + static int libbpf_netlink_recv(int sock, __u32 nl_pid, int seq, __dump_nlmsg_t _fn, libbpf_dump_nlmsg_t fn, void *cookie) { + struct iovec iov = {}; + struct msghdr mhdr = { + .msg_iov = &iov, + .msg_iovlen = 1, + }; bool multipart = true; struct nlmsgerr *err; struct nlmsghdr *nh; - char buf[4096]; int len, ret; + ret = alloc_iov(&iov, 4096); + if (ret) + goto done; + while (multipart) { start: multipart = false; - len = recv(sock, buf, sizeof(buf), 0); + len = netlink_recvmsg(sock, &mhdr, MSG_PEEK | MSG_TRUNC); + if (len < 0) { + ret = len; + goto done; + } + + if (len > iov.iov_len) { + ret = alloc_iov(&iov, len); + if (ret) + goto done; + } + + len = netlink_recvmsg(sock, &mhdr, 0); if (len < 0) { - ret = -errno; + ret = len; goto done; } if (len == 0) break; - for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); + for (nh = (struct nlmsghdr *)iov.iov_base; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len)) { if (nh->nlmsg_pid != nl_pid) { ret = -LIBBPF_ERRNO__WRNGPID; @@ -151,6 +197,7 @@ static int libbpf_netlink_recv(int sock, __u32 nl_pid, int seq, } ret = 0; done: + free(iov.iov_base); return ret; } -- 2.34.1