Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp25915rdb; Fri, 16 Feb 2024 19:03:33 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXo5Z3nyJ+gfW2jVTHFpAhpTJ/2ANt2KaDh2jmEf24tgqZIeYmtkHy/wN8I226R5Ecb4syEK3rrlxki1Fh+TzuQt3RRurSXE5v9+Ov8iQ== X-Google-Smtp-Source: AGHT+IH8Ft9oz88tp/HuJzRORSlgMsM5oQf45kx50uF8G7rTVf3MIXGzGhHbViqBhWWy5Hy4USoh X-Received: by 2002:a17:906:480d:b0:a3c:c772:7b43 with SMTP id w13-20020a170906480d00b00a3cc7727b43mr3809083ejq.50.1708139012910; Fri, 16 Feb 2024 19:03:32 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708139012; cv=pass; d=google.com; s=arc-20160816; b=sAX42a71p4Xtb58P3gJo9a1lkBTq3b28YreyI0sqgDswIR9O9gnhjimvKePgChQrJH /v09o2s9crYOYdqudJq26WAUM+3MNhV7X5gwui6YcmEdI4eFlh2cijPV/Spiwszys/Zf TCrIqJ3beJGVatCx7Es9eYhxsQOkbwn28/sFDJeufvW0Ea337vdt18m65/l/9KJqsl66 8CblyGcclXOs3/46JPCmLOn28Zmlx7I0A0CKtEO9ZtnSi5bJfCyGarhsakJ3eD1tv0UV WS2/57ZxcgrDJzsIGVOYZbD5eA9swNWrr9b2n8PbIasENRQ6bWw5nsp7MvMQCHyzidcb VcRQ== 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=9E8QSOafC61clD8Eej3Har3OU/+oWCjszElG8x74Hj8=; fh=QjHfdU3Jc//M3vlPD+yK900DePsaSqFxiM7i+bd7iP0=; b=vosCSxAXK2MMsbZLqoc2ghPtnqAmxNcGeEBbKa1X4AcZ9fDrpnYH5bh6vFxUPvh8+C id8fFJRyqknMdY/WzwcAJcg/8wxVLjsx5y129iS1k8PBstA/vEht+IWspFhzZnVc7ChF p9eNb9/RheGW3TsXOt77YFQhOijiN3N99pcoUze6BAcRgpW1aRvREpXGU+SjPvHtdA7X ysiLoJGxeViQ4D6+BFJmTNqU34ANobMJbJ083MUYg2h5p2aVq5O1J+ZFVHsmkVsSnXLo EFR+ZPhjVliaCJt68FUTE0OAikTG/bAS0UyZRplmG5rBQ8hW7qcKOH/GOfK1UhBI5BOd kJtg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=McLXvosS; 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-69665-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69665-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id n4-20020a1709061d0400b00a3d9a81a912si490883ejh.272.2024.02.16.19.03.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 19:03:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-69665-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=McLXvosS; 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-69665-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69665-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 7752F1F235C7 for ; Sat, 17 Feb 2024 03:03:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6AD081BDC4; Sat, 17 Feb 2024 03:03:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="McLXvosS" Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) (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 B6F7918E25 for ; Sat, 17 Feb 2024 03:03:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708139002; cv=none; b=LseCm2wESxbTfFe65AwCoeSJIZBgL7mEP2NorX4ntp+T3oKV6gMGrcfKS9GPHYRtrjchOhN2PN3U0syoQjNLvWYboWGysaaA3YmlB7nFmtqvGfGKnJ5voejHoB9ij/q5lj/i/8sadVUYTUQfV6HeRD3IMOm62/qWRC2jCfNsVIE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708139002; c=relaxed/simple; bh=xpvnUQORiMZ/oAeyo85a9kbOqGl2fDmjOk2texpVn8I=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=X7fU6IuCNz6mbKDIbCuQuvi99HKHCjDuuXWDZzKDVlBXuWvTkvQGF2ujNNFVglIIdDs5gMLsKqltUfUskrERSamn/PBZgE6A4rIKmJWfWQg4ZAZCtp7K3eZj0qWL1XxWRz6Nf+LuANJpXKWUjPdlWpwAZKq3KJRvUDfk6A2vGzc= 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=McLXvosS; arc=none smtp.client-ip=209.85.215.179 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-f179.google.com with SMTP id 41be03b00d2f7-5c229dabbb6so1551159a12.0 for ; Fri, 16 Feb 2024 19:03:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1708139000; x=1708743800; 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=9E8QSOafC61clD8Eej3Har3OU/+oWCjszElG8x74Hj8=; b=McLXvosSYm2MWVUEK/eacWgTkwXq1xzXKmxqORZBZPBuc3Ea19jdw3dDdgY4rnRJc+ Lg8YS/ydevC1hc4f3uEOa4b87WlgZw4yJO/MSqSdFhnjwhspJEgP/FkxIIXw+iicBmkf QKnB14duiBoR5bPq8YbixNjN9g6iGJXuq0h1I= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708139000; x=1708743800; 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=9E8QSOafC61clD8Eej3Har3OU/+oWCjszElG8x74Hj8=; b=cAnu4zX6GF3CB142xqUz6oRs2Cu31LwcbvfsJWXJQOCvdR806aIO5kak/IoB7d1TRT n2BWfUcvBD7ndrzXtx/Af5lWV8OPLZL0+kiqJEYjkaWoDzF6eHyNFDOpMEvtkc+ENLgV j77EZHOlFGvlm+Gs/mbefCc32EkkI7Cwtnst8tO35ME5vayTZY4IXdZhT8eDV6c8O5fg NGrs7VXOAM9z8BN2rK0s+cjXuDaiayFCncGo9TnpYZOcVfFy1baBz46fSAe91hii4GDV KybLybfu9WjjK9otvc1B4lcoUcqw3v6ANvcmLy10A5ZifZptGgDnST3oDNFZVDMoOyHd zoGg== X-Forwarded-Encrypted: i=1; AJvYcCUkba/B9yS/Mtq5FFUqOjMWwLmtloL7QDBYddgIz8RbHgQR4ubynjiL05YCbeey2PHhthwnCxum1QLXuPtUxC8jdHlYMIhJvygBf1T1 X-Gm-Message-State: AOJu0YxbEZKwt4iApAk7qeZc9BVC1lJeN4/QBKAnavQAvYt9gdKA7k0J tGEyTodHXeLYiiZblfhV229ir/wMeIn/ruPpdrLzKwRwFcPGHF0rfjPNg64DTw== X-Received: by 2002:a17:902:f54a:b0:1db:55cc:d222 with SMTP id h10-20020a170902f54a00b001db55ccd222mr9000388plf.4.1708139000004; Fri, 16 Feb 2024 19:03:20 -0800 (PST) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id kq13-20020a170903284d00b001db5c8202a4sm525497plb.59.2024.02.16.19.03.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 19:03:19 -0800 (PST) Date: Fri, 16 Feb 2024 19:03:18 -0800 From: Kees Cook To: "Gustavo A. R. Silva" Cc: Alexei Starovoitov , Mark Rutland , Daniel Borkmann , 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: <202402161902.FCFFEC322@keescook> References: <20240216235536.it.234-kees@kernel.org> 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 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. :) -- Kees Cook