Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp205102pxb; Tue, 17 Aug 2021 23:50:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwOm8XCFIXf48GNXtDLrOrITz3EP6nvBFKSeUdB9lQ30E+tymP0tZ3AwCMt3KiV/kdDkO9M X-Received: by 2002:a05:6402:1add:: with SMTP id ba29mr8534363edb.145.1629269423777; Tue, 17 Aug 2021 23:50:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629269423; cv=none; d=google.com; s=arc-20160816; b=pwueFXvJum7yYLYq3emP4WE4CLnRsY+Ye+WPAxe+zKSOtz9zdUD4ZUiYNzDbnMoQ/2 lQHzwqoJpRIb0osu/UVY4NSE80OdlTwF/bDcvDnJ+PfVDIxFl7p5H964+Fijbqmh8bco DqPOcVPjr0+UU0sONSNNhWEhyZelzh4mX3j5cisOcCfqyc3HA61inOvScS+6zQPexj9f wuVqXMd8qQSY8jT52WJEg9oX18/MENawAp1f/C3efCe3WqCXnuTWOgauM3JeX16CwIdx d15CAvSRZjXO7689qLCVlqZpZMTN1RgWgYksDdtlGWQMAh2zfqKNZslffo6DMM5hWU+Q HH8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=ydwImPka+33pK3zFh/3ZRhWUJUFUKLKTzYRKuwQUsxw=; b=idiVAHI7FwaqE2OFivsX+zEXQhJtlW3IpBFl3INXUAX5ILcxTeC39bhOwDWi14A6rK SqcmmdR88NwycLGNUVmYAgliwLK86JL+CbesOCtwOtKeAUmIoY08QXsEUIeavMS/IJig i1vASJbdF+ZJdPNUPTeI1ZG/tnLPJd/0qZoOEiYdZ1c7plrST6i5e1zldq1rnR8RP6OW 703YWmrUs7qzZ4+znm87Jrb4krIzsP+05GgULp+23OD67AocqR5X9xEZTWsH81JBxHK9 1kA9gCV1ZHT15VLD0FRDzxCflIs1322mVRduftOGf/TxujSunu0t9MXF6EOThBnRvebp n4fw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=cGjVLJJd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h4si5422176edt.426.2021.08.17.23.50.01; Tue, 17 Aug 2021 23:50:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=cGjVLJJd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238559AbhHRGtR (ORCPT + 99 others); Wed, 18 Aug 2021 02:49:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238103AbhHRGtQ (ORCPT ); Wed, 18 Aug 2021 02:49:16 -0400 Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1DE93C061764 for ; Tue, 17 Aug 2021 23:48:42 -0700 (PDT) Received: by mail-pg1-x535.google.com with SMTP id k24so1240163pgh.8 for ; Tue, 17 Aug 2021 23:48:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=ydwImPka+33pK3zFh/3ZRhWUJUFUKLKTzYRKuwQUsxw=; b=cGjVLJJdziTcmvJ27JU5rTQ8yCeDMcn5cVnyGLyKhLTIfFt1+UogEWL2TR600L0YNa 2ooQ2QARhAFf0vpH9wd2/xNtxYtH/TARe4IIayGOo1ZKruM4m488v+jeFjPTdR1DQCVO +vukV7cOhOLhxKJWKPtV2YOFJB/OfXWuW9pwQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=ydwImPka+33pK3zFh/3ZRhWUJUFUKLKTzYRKuwQUsxw=; b=ps7UcMWabRuzbrFPt8LnClVmcf+hwucyU3y4s0N4w4wMYvWSHujEq+b4Bkec9Aef4M /azTs6V2/AystdAfafctn22QbEDF1dhEimBJNxpHjuhKO3i1vE2rvyKit0KLceLfLFNj A5Y1kU+Q+UkCu73dIOB4hWPamiZ2g1fGS5eVeqj7ELQipd5f3n+wruL4BcZCzHAbDXks 7VZp5a9gqr0pWIpH8HWhw74N+QXp68td9QsdfAN3O/gGo04cdr14gYqBqR/hdRbC4OH7 TntD7V3LL39E1/7UGNnqAc5ej/kgwgIj2mapy7N9luVyU7eXgN+Oc9Z0/86jpf9wzhp/ wP4w== X-Gm-Message-State: AOAM532s/rieeIkd5XTeWBN9MBYKcTZ2PFlcJjr/rHB4AxSaCHl1qzBl Hd5jP8+1FqzK168lgVEt48+jeA== X-Received: by 2002:a62:878a:0:b029:3e0:7810:ec36 with SMTP id i132-20020a62878a0000b02903e07810ec36mr7701759pfe.4.1629269321704; Tue, 17 Aug 2021 23:48:41 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id d5sm4039400pju.28.2021.08.17.23.48.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Aug 2021 23:48:41 -0700 (PDT) Date: Tue, 17 Aug 2021 23:48:40 -0700 From: Kees Cook To: Vincent MAILHOL Cc: Wolfgang Grandegger , Marc Kleine-Budde , "David S. Miller" , Jakub Kicinski , Arunachalam Santhanam , linux-can , netdev , open list , linux-hardening@vger.kernel.org Subject: Re: [PATCH] can: etas_es58x: Replace 0-element raw_msg array Message-ID: <202108172320.1540EC10C@keescook> References: <20210818034010.800652-1-keescook@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Aug 18, 2021 at 02:13:51PM +0900, Vincent MAILHOL wrote: > On Wed. 18 Aug 2021 at 12:40, Kees Cook wrote: > > While raw_msg isn't a fixed size, it does have a maximum size. Adjust the > > struct to represent this and avoid the following warning when building > > with -Wzero-length-bounds: > > > > drivers/net/can/usb/etas_es58x/es58x_fd.c: In function 'es58x_fd_tx_can_msg': > > drivers/net/can/usb/etas_es58x/es58x_fd.c:360:35: warning: array subscript 65535 is outside the bounds of an interior zero-length array 'u8[0]' {aka 'unsigned char[]'} [-Wzero-length-bounds] > > 360 | tx_can_msg = (typeof(tx_can_msg))&es58x_fd_urb_cmd->raw_msg[msg_len]; > > | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > In file included from drivers/net/can/usb/etas_es58x/es58x_core.h:22, > > from drivers/net/can/usb/etas_es58x/es58x_fd.c:17: > > drivers/net/can/usb/etas_es58x/es58x_fd.h:231:6: note: while referencing 'raw_msg' > > 231 | u8 raw_msg[0]; > > | ^~~~~~~ > > > > Cc: Wolfgang Grandegger > > Cc: Marc Kleine-Budde > > Cc: "David S. Miller" > > Cc: Jakub Kicinski > > Cc: Arunachalam Santhanam > > Cc: Vincent Mailhol > > Cc: linux-can@vger.kernel.org > > Cc: netdev@vger.kernel.org > > Signed-off-by: Kees Cook > > --- > > drivers/net/can/usb/etas_es58x/es581_4.h | 2 +- > > drivers/net/can/usb/etas_es58x/es58x_fd.h | 2 +- > > 2 files changed, 2 insertions(+), 2 deletions(-) > > > > diff --git a/drivers/net/can/usb/etas_es58x/es581_4.h b/drivers/net/can/usb/etas_es58x/es581_4.h > > index 4bc60a6df697..af38c4938859 100644 > > --- a/drivers/net/can/usb/etas_es58x/es581_4.h > > +++ b/drivers/net/can/usb/etas_es58x/es581_4.h > > @@ -192,7 +192,7 @@ struct es581_4_urb_cmd { > > struct es581_4_rx_cmd_ret rx_cmd_ret; > > __le64 timestamp; > > u8 rx_cmd_ret_u8; > > - u8 raw_msg[0]; > > + u8 raw_msg[USHRT_MAX]; > > } __packed; > > > > __le16 reserved_for_crc16_do_not_use; > > diff --git a/drivers/net/can/usb/etas_es58x/es58x_fd.h b/drivers/net/can/usb/etas_es58x/es58x_fd.h > > index ee18a87e40c0..e0319b8358ef 100644 > > --- a/drivers/net/can/usb/etas_es58x/es58x_fd.h > > +++ b/drivers/net/can/usb/etas_es58x/es58x_fd.h > > @@ -228,7 +228,7 @@ struct es58x_fd_urb_cmd { > > struct es58x_fd_tx_ack_msg tx_ack_msg; > > __le64 timestamp; > > __le32 rx_cmd_ret_le32; > > - u8 raw_msg[0]; > > + u8 raw_msg[USHRT_MAX]; > > } __packed; > > > > __le16 reserved_for_crc16_do_not_use; > > -- > > 2.30.2 > > raw_msg is part of a union so its maximum size is implicitly the > biggest size of the other member of that union: Yup, understood. See below... > > | struct es58x_fd_urb_cmd { > | __le16 SOF; > | u8 cmd_type; > | u8 cmd_id; > | u8 channel_idx; > | __le16 msg_len; > | > | union { > | struct es58x_fd_tx_conf_msg tx_conf_msg; > | u8 tx_can_msg_buf[ES58X_FD_TX_BULK_MAX * ES58X_FD_CANFD_TX_LEN]; > | u8 rx_can_msg_buf[ES58X_FD_RX_BULK_MAX * ES58X_FD_CANFD_RX_LEN]; > | struct es58x_fd_echo_msg echo_msg[ES58X_FD_ECHO_BULK_MAX]; > | struct es58x_fd_rx_event_msg rx_event_msg; > | struct es58x_fd_tx_ack_msg tx_ack_msg; > | __le64 timestamp; > | __le32 rx_cmd_ret_le32; > | u8 raw_msg[0]; > | } __packed; > | > | __le16 reserved_for_crc16_do_not_use; > | } __packed; > > ram_msg can then be used to manipulate the other fields at the byte level. > I am sorry but I fail to understand why this is an issue. The issue is with using a 0-element array (these are being removed from the kernel[1] so we can add -Warray-bounds). Normally in this situation I would replace the 0-element array with a flexible array, but this case is unusual in several ways: - There is a trailing struct member (reserved_for_crc16_do_not_use), which is never accessed (good), and documented as "please never access this". - struct es58x_fd_urb_cmd is statically allocated (it is written into from the URB handler). - The message lengths coming from the USB device are stored in a u16, which looked like it was possible to overflow the buffer. In taking a closer look, I see that the URB command length is checked, and the in-data length is checked as well, so the overflow concern appears to be addressed. > Also, the proposed fix drastically increases the size of the structure. Indeed. I will send a v2, now that I see that the overflow concern isn't an issue. Thanks! -Kees [1] https://www.kernel.org/doc/html/latest/process/deprecated.html#zero-length-and-one-element-arrays -- Kees Cook