Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp1339801rdb; Mon, 19 Feb 2024 11:58:24 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXzM3xEIfWKGXdZma8WVV1mjU493b3kMmhO+DtXWCiOmw18RbuDgBwhNNp2ZPSc9xtjGEDvxRttwSlF1LYwOsEX/fWaqoq9kSAjivjV7g== X-Google-Smtp-Source: AGHT+IHExa/55OajJ0x7boUawJgP6WZzyyRrgx6nuXRNaHjMHW3gkMUatbEdMnaRmwdV0PzR+RYb X-Received: by 2002:a17:90b:4b4f:b0:299:3ebf:d180 with SMTP id mi15-20020a17090b4b4f00b002993ebfd180mr7252538pjb.6.1708372704450; Mon, 19 Feb 2024 11:58:24 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708372704; cv=pass; d=google.com; s=arc-20160816; b=xwnqUsGgBzUwoMACEGfPQzw5Jqt6Tx2ijchuxZOlxLQ4pifMhAHhEdcQIckYbWIzAQ Id9b0AQOUQk0pRjyX1htxEYX4BCjcPJLXBfp6zjNFEghOauQO5iiyhjRjdWsS4E9OIkh VEAtGNqmdxx6jLwE26q2DGVyY3sPmAmCJ15LI1pPv3Cn5p44f2qQXD1kzmj+gKW46JdR +ZxT3TbpG5JM4lmz9BapkjC8QrINV7nJgAFrjOFqPw9TL+Bn5Qa1gKNWo11x7L3uTTYk 1N6zK8xZP2sqIHcrcehDHefSfJ/BzhkQNka4cKgejZExmjzSFuOnt5OYrN9ARgjsD1/p E3yA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=DOFMy5aAASBYxwEuY26DwtWuV8GmVWpkF2wskH7Kuzg=; fh=Q+bhczi/Tb7GWiUr74Usso8SOSpQOcO4Ut+3YONPKqA=; b=1Bcbz8G2lmEpr0Rs4PylcSkKTOhd15q/SfgglX9VLTEUAHS67oVu6+OS5NCKC5XSJR 47Nkta6HG2CC14tBJN/n6trXVeU3ui+9LtdfV9OQ8DJGSvfAh/PesRBuLwvWAThH2V+n Vv4z5cGfgFcBLjeT6N65VRzPnlAzJiin8OLXvlrnxK1/zwB296AnrglJwMKntnQ8PjV4 Iu1Fkced6GH/zwf4dsPVL+Pd2xnuWwcgnIUOcFRLyaKOcForLHi4N+cdOgPhQ59OQgPN /4m2hR/iRz/VsGZgY1XQod8qp8RUE15Wa33a1Slz7nIGCcHVBd5GaKlnElJYiZ4SqUNU Bdgw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=BsRtgLJs; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-71910-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-71910-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id np17-20020a17090b4c5100b00299c490d298si922279pjb.188.2024.02.19.11.58.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 11:58:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-71910-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=BsRtgLJs; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-71910-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-71910-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id EEB5CB244A5 for ; Mon, 19 Feb 2024 19:51:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AB3A955E54; Mon, 19 Feb 2024 19:48:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="BsRtgLJs" Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E281456453 for ; Mon, 19 Feb 2024 19:48:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708372120; cv=none; b=N7FpxW85jAKOGF3lqKri3ouJNa+pu1B/I9AoRMyR0PSsdXSQ1RStE3H868q8bWYspSRnUnxoMHUOrTgtjUawhLRWKOVZ23HCKEIkQjGs+1wkwKo90dntGy0ERG57eQTtgvNgMwetLl8g95Y1+7ozu5SPWlPKMVk2ZcERkTcFVvE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708372120; c=relaxed/simple; bh=mTwnrWFRVkMmk/C370Z2PfPl2ZzMt3ZnRg8rjXdj5M8=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=sH7rofAwYaUiUVEmovBBy2QVtCiPO5dxivttvdUH//wVT04H3/axNJ0f0lo8jSaESc8nsFheFeDHsAArPpFgrV4HQn5ng+sHtze1S2GSf6EgWaEe5nyXJDtKhnqnP5678aAsL1jk4hjt9wKoXp6GwwB6v8nWrqnSAVHXLpQh2Eo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=BsRtgLJs; arc=none smtp.client-ip=209.85.215.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-5ce942efda5so2741617a12.2 for ; Mon, 19 Feb 2024 11:48:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1708372118; x=1708976918; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=DOFMy5aAASBYxwEuY26DwtWuV8GmVWpkF2wskH7Kuzg=; b=BsRtgLJsQMCLQ16wev0X7ok0toZRzp0KMmRlhfMFBLuARhuBWGzIl1ddg0B+jTFuxw bX3pq1/yM9RamwfpYN0PVgdPZ5W3X9xlsiGPQq+t6dM9+Y120t+uk2oRPHPq6ZzQA8lY JskCbXmzlBznsWKd0WPivfLmfJ0+Gtj7FMo7w= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708372118; x=1708976918; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=DOFMy5aAASBYxwEuY26DwtWuV8GmVWpkF2wskH7Kuzg=; b=nu21g780K8qdE/lLlBCiv51Vq+nfwpB8bMgky1sQWpz3NS0hCxg8WVFMIU8LYG8jrF AUxJgfb1VOyUkUdqCnr6t/4QW7hM3vRlRpK8XsZZce8KsJijgZdJ2BuEfeYhcj3e2qAc JPaoS9UYUBHFqyP42Amtzlj++nZAQyHHj6rrUXPZx51A/h3E6QTmCaQFbifQRevrdb6P 6i4C4w6ip6UFAo34tWV15Rr5qCu3cOU1Feb8YGj/Wl8tXstjXDX22S/wse2/xZma0qXw XBfZoDTrBM/oCGYwjfcUTvc6FVLPEwfgULYGiEhJYRBiH0seC712ZV2W2PVcAJwFAiP+ RVmQ== X-Forwarded-Encrypted: i=1; AJvYcCVTDVBq+KHVfslDMa3E9k9jKMrpgyWJeNsPqzysJnaNL94XzGmoBzW2v9oEFvD3FhGy0MgMgriuPaDPHnVmWEvC813sYzZxQwX28KJi X-Gm-Message-State: AOJu0YwGqQg8WMVu1Gv8lNoOlZg9eEKP5SXHEC4oGWaUstwGrW4U/JHI HNCvgiaFbg3N8zU4e9vjiPLRnn4X/CgFjhzSt5WM0PVisgqsMCktiLwkn6HbwQ== X-Received: by 2002:a17:903:298b:b0:1db:be69:d02d with SMTP id lm11-20020a170903298b00b001dbbe69d02dmr11902487plb.32.1708372118058; Mon, 19 Feb 2024 11:48:38 -0800 (PST) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id a16-20020a170902b59000b001d8dd636705sm4716588pls.190.2024.02.19.11.48.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 11:48:37 -0800 (PST) Date: Mon, 19 Feb 2024 11:48:37 -0800 From: Kees Cook To: Daniel Borkmann Cc: "Gustavo A. R. Silva" , Alexei Starovoitov , Mark Rutland , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , Haowen Bai , bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, Yonghong Song , Anton Protopopov , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH v2] bpf: Replace bpf_lpm_trie_key 0-length array with flexible array Message-ID: <202402191144.C4DB9B7AA@keescook> References: <20240216235536.it.234-kees@kernel.org> <202402161902.FCFFEC322@keescook> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Mon, Feb 19, 2024 at 06:48:41PM +0100, Daniel Borkmann wrote: > On 2/17/24 4:03 AM, Kees Cook wrote: > > On Fri, Feb 16, 2024 at 06:27:08PM -0600, Gustavo A. R. Silva wrote: > > > On 2/16/24 17:55, Kees Cook wrote: > > > > Replace deprecated 0-length array in struct bpf_lpm_trie_key with > > > > flexible array. Found with GCC 13: > > > > > > > > ../kernel/bpf/lpm_trie.c:207:51: warning: array subscript i is outside array bounds of 'const __u8[0]' {aka 'const unsigned char[]'} [-Warray-bounds=] > > > > 207 | *(__be16 *)&key->data[i]); > > > > | ^~~~~~~~~~~~~ > > > > ../include/uapi/linux/swab.h:102:54: note: in definition of macro '__swab16' > > > > 102 | #define __swab16(x) (__u16)__builtin_bswap16((__u16)(x)) > > > > | ^ > > > > ../include/linux/byteorder/generic.h:97:21: note: in expansion of macro '__be16_to_cpu' > > > > 97 | #define be16_to_cpu __be16_to_cpu > > > > | ^~~~~~~~~~~~~ > > > > ../kernel/bpf/lpm_trie.c:206:28: note: in expansion of macro 'be16_to_cpu' > > > > 206 | u16 diff = be16_to_cpu(*(__be16 *)&node->data[i] > > > > ^ > > > > | ^~~~~~~~~~~ > > > > In file included from ../include/linux/bpf.h:7: > > > > ../include/uapi/linux/bpf.h:82:17: note: while referencing 'data' > > > > 82 | __u8 data[0]; /* Arbitrary size */ > > > > | ^~~~ > > > > > > > > And found at run-time under CONFIG_FORTIFY_SOURCE: > > > > > > > > UBSAN: array-index-out-of-bounds in kernel/bpf/lpm_trie.c:218:49 > > > > index 0 is out of range for type '__u8 [*]' > > > > > > > > This includes fixing the selftest which was incorrectly using a > > > > variable length struct as a header, identified earlier[1]. Avoid this > > > > by just explicitly including the prefixlen member instead of struct > > > > bpf_lpm_trie_key. > > > > > > > > Note that it is not possible to simply remove the "data" member, as it > > > > is referenced by userspace > > > > > > > > cilium: > > > > struct egress_gw_policy_key in_key = { > > > > .lpm_key = { 32 + 24, {} }, > > > > .saddr = CLIENT_IP, > > > > .daddr = EXTERNAL_SVC_IP & 0Xffffff, > > > > }; > > > > > > > > systemd: > > > > ipv6_map_fd = bpf_map_new( > > > > BPF_MAP_TYPE_LPM_TRIE, > > > > offsetof(struct bpf_lpm_trie_key, data) + sizeof(uint32_t)*4, > > > > sizeof(uint64_t), > > > > ... > > > > > > > > The only risk to UAPI would be if sizeof() were used directly on the > > > > data member, which it does not seem to be. It is only used as a static > > > > initializer destination and to find its location via offsetof(). > > > > > > > > Link: https://lore.kernel.org/all/202206281009.4332AA33@keescook/ [1] > > > > Reported-by: Mark Rutland > > > > Closes: https://paste.debian.net/hidden/ca500597/ > > > > > > mmh... this URL expires: 2024-05-15 > > > > Yup, but that's why I included the run-time splat above too. :) > > I don't quite follow, this basically undoes 3024d95a4c52 ("bpf: Partially revert > flexible-array member replacement") again with the small change that this 'fixes' > up the BPF selftest to not embed struct bpf_lpm_trie_key. > > Outside of BPF selftests though aren't we readding the same error that we fixed > earlier for BPF programs in the wild which embed struct bpf_lpm_trie_key into their > key structure? Oops, yes, sorry. I see how that cilium does include it in the same fashion. I will adjust this patch again. Thanks for double-checking! struct egress_gw_policy_key { struct bpf_lpm_trie_key lpm_key; __u32 saddr; __u32 daddr; }; -- Kees Cook