Received: by 2002:a05:7412:1e0b:b0:fc:a2b0:25d7 with SMTP id kr11csp1434013rdb; Fri, 16 Feb 2024 16:30:24 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXISleYcm1Dz9DAbbvGqEDQezmk7MXiB5qCc12ptQlGEH4bpwEgbHPJrh1IrtG9mwkZzHhYkzJ0cVVbxPbfdvjass4rMvYlDRnJNYuopg== X-Google-Smtp-Source: AGHT+IH/3FhkV+z1PEY+c2+u67tb5eiRt7FQzaz1VaQ3nDR7WkIOCNVdX/roTaTec7NUWDHiKBq/ X-Received: by 2002:a17:906:abd8:b0:a3e:1205:a02d with SMTP id kq24-20020a170906abd800b00a3e1205a02dmr725756ejb.17.1708129824471; Fri, 16 Feb 2024 16:30:24 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708129824; cv=pass; d=google.com; s=arc-20160816; b=TJmIMLpQfD2DJWSQrX1nrdrT/NCk1wJb0/HD779AL/bT+X6Ha3ILualDnX20/tswi5 EvWxBKgx4tO5KSaBn8A1neIKNPgt6hW3i0cALJBwErIRKkT2VVUkzCKEO1bs2kj/SBzF TvhX+Y1Bwt86sQO5WJXffWTfwFeeHYV50gyKD2+rzrMVfFkONw4x0l1sil5+zY1di8Hh TVB3hXU9pn/ZKqM7clJDFleqZtZ8ZOgDxfxWjezw4HrufZBfoQFVWhL8OXJwnOk0RRvf leSwu1QT2TrqKIz60iIGmkXe84kQe+/udptWAEb9WsJH9xtNd4C1p8FKk9rSVnys9l/p 7q9A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=wByDJQzTAShm7Gkyw4stFr/KXSxVitihfe54G/dVvfE=; fh=Uxv2gf/AtWIILVZEbgdaLqwEwPRcTR9xp9IYEdecnR0=; b=prF4XP4lxBYFFNOaqD1Cis4MxSHuW6HK9oX8+VnJDHMYEp09FqOBSuHOAOgB3mQXEI qFZtxqo010rGdWqnTHp3d2PspVlECGOQfjeyJOBKiCo759cZ2kAy54WLiw3jA0J96MFn br7cm3Ej4qcxXWaF6L/xBy5DGPSlvJzmvAovBp1PEfvuLkDrTcFyvmXw29wuwf4Ntis9 uKZujsnSrAWn5CUb3GxUnST3oKwHjtlRgdK4tBsSlC0dviCPqbQDzJC4YOYX5Ncd8tLx ojTFIuCMGqUVRoFfIDOQFaICiavHkDlUTqyyaLmRPBOcawJKpkDzDujWDEZW2Cqvocr4 65WQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=fail header.i=@embeddedor.com header.s=default header.b=ZvI+7UlQ; arc=pass (i=1 spf=pass spfdomain=embeddedor.com dkim=pass dkdomain=embeddedor.com); spf=pass (google.com: domain of linux-kernel+bounces-69584-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69584-linux.lists.archive=gmail.com@vger.kernel.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 m24-20020a17090607d800b00a3d8ffcbf09si372980ejc.569.2024.02.16.16.30.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 16:30:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-69584-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=fail header.i=@embeddedor.com header.s=default header.b=ZvI+7UlQ; arc=pass (i=1 spf=pass spfdomain=embeddedor.com dkim=pass dkdomain=embeddedor.com); spf=pass (google.com: domain of linux-kernel+bounces-69584-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69584-linux.lists.archive=gmail.com@vger.kernel.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 1477D1F21F47 for ; Sat, 17 Feb 2024 00:30:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 05A61381CE; Sat, 17 Feb 2024 00:27:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=embeddedor.com header.i=@embeddedor.com header.b="ZvI+7UlQ" Received: from omta036.useast.a.cloudfilter.net (omta036.useast.a.cloudfilter.net [44.202.169.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E876B376EC for ; Sat, 17 Feb 2024 00:27:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=44.202.169.35 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708129642; cv=none; b=kLJ9bYTOxb+8xxJqIq+EztgW5Adm580d+yDrs4tTYShi2sIBE/I32V/Or0Eoqv5aPYXCRH9G5NTGIffZjAENhOmADf0Rh+EEcege2iLgS5r5pO21h7xZUGP76DP6V5nKAYCHK8bwi+EqSm14SDm3fhZ53x7td0F/X2Jh12SvNZs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708129642; c=relaxed/simple; bh=6qi5i2HZdurFWg68hISCVmuDEvRQ7/se82kz2njo5qI=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=umRp6C+XhpVH8eiKJaDdSE7gEYs0/77bf/3KOVFqyTOTZaa+vnOjuhmUScXfVfiNmwb9kgMxwM3TE50NbD/UH2/bq8CaWleYzSe8NEu0fEPJIm7Cg35vO1V4zkWoB1J2lZxC62CF+mB5yPD6yfbti/lvhBX5LQpS+RxXql2itSk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=embeddedor.com; spf=pass smtp.mailfrom=embeddedor.com; dkim=pass (2048-bit key) header.d=embeddedor.com header.i=@embeddedor.com header.b=ZvI+7UlQ; arc=none smtp.client-ip=44.202.169.35 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=embeddedor.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=embeddedor.com Received: from eig-obgw-6010a.ext.cloudfilter.net ([10.0.30.248]) by cmsmtp with ESMTPS id b53FroQKN8uLRb8XyrMWQU; Sat, 17 Feb 2024 00:27:14 +0000 Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with ESMTPS id b8XxrC1Bp8ZXdb8XxrgnTV; Sat, 17 Feb 2024 00:27:13 +0000 X-Authority-Analysis: v=2.4 cv=eqR8zZpX c=1 sm=1 tr=0 ts=65cffd61 a=1YbLdUo/zbTtOZ3uB5T3HA==:117 a=VhncohosazJxI00KdYJ/5A==:17 a=IkcTkHD0fZMA:10 a=k7vzHIieQBIA:10 a=wYkD_t78qR0A:10 a=VwQbUJbxAAAA:8 a=7CQSdrXTAAAA:8 a=3_uRt0xjAAAA:8 a=cm27Pg_UAAAA:8 a=hWMQpYRtAAAA:8 a=FOH2dFAWAAAA:8 a=pGLkceISAAAA:8 a=1XWaLZrsAAAA:8 a=yjU-xTemAAAA:8 a=EU4AEG8cihyboLecn_oA:9 a=QEXdDO2ut3YA:10 a=AjGcO6oz07-iQ99wixmX:22 a=a-qgeE7W1pNrGK8U0ZQC:22 a=z1SuboXgGPGzQ8_2mWib:22 a=xmb-EsYY8bH0VWELuYED:22 a=KCsI-UfzjElwHeZNREa_:22 a=i3VuKzQdj-NEYjvDI-p3:22 a=SwQY0DHxSCHDbjv2szoi:22 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=embeddedor.com; s=default; h=Content-Transfer-Encoding:Content-Type: In-Reply-To:From:References:Cc:To:Subject:MIME-Version:Date:Message-ID:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=wByDJQzTAShm7Gkyw4stFr/KXSxVitihfe54G/dVvfE=; b=ZvI+7UlQ3j/mAJ/+A1JIB2m0jL B9lbVrudd/U0viF7X6bD8UkKruJQvjyk+hcvIGQAUNI6MlvbTRREVimfouXeUJRLlvlMFmqpigGrO XibCe5LuLxruX6X8A/RZnkqgTnvIiE2JYnjjPul+PWnUHKQe4wNQsRBZlDqZje7xmHSLMagUhxpW/ NGa18liFeIj8rFEFxmDUJ1sMT1XZMqT7eoaQ8VR0g/DYqB9krGTbUS+1VGgRE3fqV7rukR8ugpMq+ orHqy9oZN8xnQwysXH6XWC0hJww9hcZP33W6ViBtYVAxCXrZrGqKaJr202Hpjp/CuA7b5aFKjN52h pQuh+e3Q==; Received: from [201.172.172.225] (port=39390 helo=[192.168.15.10]) by gator4166.hostgator.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96.2) (envelope-from ) id 1rb8Xv-002wqL-0B; Fri, 16 Feb 2024 18:27:11 -0600 Message-ID: Date: Fri, 16 Feb 2024 18:27:08 -0600 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] bpf: Replace bpf_lpm_trie_key 0-length array with flexible array Content-Language: en-US To: Kees Cook , Alexei Starovoitov Cc: 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 References: <20240216235536.it.234-kees@kernel.org> From: "Gustavo A. R. Silva" In-Reply-To: <20240216235536.it.234-kees@kernel.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator4166.hostgator.com X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - embeddedor.com X-BWhitelist: no X-Source-IP: 201.172.172.225 X-Source-L: No X-Exim-ID: 1rb8Xv-002wqL-0B X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: ([192.168.15.10]) [201.172.172.225]:39390 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 19 X-Org: HG=hgshared;ORG=hostgator; X-Source-Cap: Z3V6aWRpbmU7Z3V6aWRpbmU7Z2F0b3I0MTY2Lmhvc3RnYXRvci5jb20= X-Local-Domain: yes X-CMAE-Envelope: MS4xfJdzqatdsnbltcQ+IgaMteVDtnVxMSqTlR2S/ElivDqLxuZqba2go/jOAsZlj0iU5iNK17oW/WDA9Wh+G+kpjbc1WEy/cpJuYKLvZOQK5oLbNAIlgR0r 8jGSiOG0PNehcZKu15bNf6KcGYfjQ/Se4ZNgx2MNdDEp9LlCA2SbI66BBI8yjMfdDFl4rHEKSNF0ao33MatsmKZ8sOsOUAe7E+5BWfhDe+V69B/DaVaQGdhY 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 > Signed-off-by: Kees Cook Acked-by: Gustavo A. R. Silva Thanks! -- Gustavo > --- > Cc: Alexei Starovoitov > Cc: Daniel Borkmann > Cc: Andrii Nakryiko > Cc: Martin KaFai Lau > Cc: Song Liu > Cc: Yonghong Song > Cc: John Fastabend > Cc: KP Singh > Cc: Stanislav Fomichev > Cc: Hao Luo > Cc: Jiri Olsa > Cc: Mykola Lysenko > Cc: Shuah Khan > Cc: Haowen Bai > Cc: bpf@vger.kernel.org > Cc: linux-kselftest@vger.kernel.org > > v2- clarify commit log, add more failure examples > v1- https://lore.kernel.org/all/63e531e3.170a0220.3a46a.3262@mx.google.com/ > --- > include/uapi/linux/bpf.h | 2 +- > tools/testing/selftests/bpf/progs/map_ptr_kern.c | 2 +- > 2 files changed, 2 insertions(+), 2 deletions(-) > > diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h > index 754e68ca8744..359dd8a429c1 100644 > --- a/include/uapi/linux/bpf.h > +++ b/include/uapi/linux/bpf.h > @@ -80,7 +80,7 @@ struct bpf_insn { > /* Key of an a BPF_MAP_TYPE_LPM_TRIE entry */ > struct bpf_lpm_trie_key { > __u32 prefixlen; /* up to 32 for AF_INET, 128 for AF_INET6 */ > - __u8 data[0]; /* Arbitrary size */ > + __u8 data[]; /* Arbitrary size */ > }; > > struct bpf_cgroup_storage_key { > diff --git a/tools/testing/selftests/bpf/progs/map_ptr_kern.c b/tools/testing/selftests/bpf/progs/map_ptr_kern.c > index 3325da17ec81..1d476c6ae284 100644 > --- a/tools/testing/selftests/bpf/progs/map_ptr_kern.c > +++ b/tools/testing/selftests/bpf/progs/map_ptr_kern.c > @@ -316,7 +316,7 @@ struct lpm_trie { > } __attribute__((preserve_access_index)); > > struct lpm_key { > - struct bpf_lpm_trie_key trie_key; > + __u32 prefixlen; > __u32 data; > }; >