Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp1793003lqo; Sun, 12 May 2024 20:03:51 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUFjgSYUc+HCmWz1/0Kym8qvNSK6uRpNy8dseQrAb3lyMNONmNhGQhF0qhqDJg9MjCghz/5TxC0eFG4Bg5EY1qM2+clRBYxjyxM1GLGBg== X-Google-Smtp-Source: AGHT+IGIUzeNgdHDVPfzVQfv3E27P9DwYM2JvM4xR/RFh1/1lWKwTeIev9VYcLTROuBAa+ul/6X9 X-Received: by 2002:a17:90a:9281:b0:2b2:b99f:4d5b with SMTP id 98e67ed59e1d1-2b6cc454828mr7243999a91.18.1715569431276; Sun, 12 May 2024 20:03:51 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715569431; cv=pass; d=google.com; s=arc-20160816; b=GeD7r2zzAE4/oVxRHSTg+U2NOTeBDw1XExaGlyIeZ1l+iOTqF1SZK+MYDXSN4nHI0V yGu9tUB3nuAxkwyRUlBmiVRhkuOY87Gm8zkC/ZmMgDz2UBFy+v45ON0RH6Qipd7fNSZY zT7ul1iaGbhoSdUT8jZ1xsHaZqFyfXj+zJKeKb4lIpNZZne9Lp4o5WktTSc/PCYu40jR L6kt/glE9wZa7ul30bkYbf9EJdVLnkoPc5g2m2HdiP9gvmS49U+1YF8WZMGHLgi6xXkv 9R40Y71ONQSCHja35t+kdToPw5FJjpTMN9aJVEwXmXff3YX1HvQ6uCKLl98Ys99odYLL 6qlA== 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=yzBJEORmly2uweAvFf1l8zuxXIyjPhUuwee5HZnj+NQ=; fh=h92w3n3WAq9C3vOiQJU15jGUA8giv5q5fB9VCNI99Ao=; b=ROGjqwSv7tM1iFGuTKM2ZvzdkJW7OxhDquJBeEg9+ZNOor0xvBC0b3SmEZNVMZcwfF bsjsnf/HIYeE8iD9J8itCgCohe07mZvfSJ5JEuy0+vf56C7eI8bjYoM63o1oowBjOt+a LdMd5x11TNm6kT1ZJpL1ZdHDvDs9nQJnyXqPm/1yEJalRLdKZRmB1lsHQkfD1jW1eNCL DoIF2Ra+6Gnow0P5jEsz9EIE7Ru+Ise5CxlDJcl8HkF4OQLA4m/IXnfI6LDItqcjnbse gSdFmHby+SWfgAwt2EqFnPCGdu1vI90cvbT7dpZcyNx9ZRhF75UMKEnAAdI9a89D9fE9 9xzg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="TaAz/ENJ"; 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-bluetooth+bounces-4548-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-4548-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id 41be03b00d2f7-63f14c541b1si2444716a12.289.2024.05.12.20.03.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 May 2024 20:03:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth+bounces-4548-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="TaAz/ENJ"; 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-bluetooth+bounces-4548-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-4548-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id D5EA92812B0 for ; Mon, 13 May 2024 03:03:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8E607145B2F; Mon, 13 May 2024 03:03:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="TaAz/ENJ" X-Original-To: linux-bluetooth@vger.kernel.org Received: from mail-ot1-f52.google.com (mail-ot1-f52.google.com [209.85.210.52]) (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 93458145B03 for ; Mon, 13 May 2024 03:03:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.52 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715569422; cv=none; b=KPAdU9VQkfK6CmLbwSetC8ZQJjQyXiJoZhNRoGSWvGd2bLZiLpd9573PsMOCr2ql1tp0rqc2S41A0obLU7JAqV8Y/mALLVcZY4Jh5XVcLhz9vRNLxFqCepTAeFRCApD8mw6MAw3ta2U8cRSCucKa4KwTwcwQ2yyllVkyBHUMemA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715569422; c=relaxed/simple; bh=z4NlVMZtR0r+DtTYPkbCuUFcxQnmejuMc8+xSsKlqFA=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=S2W+0czXDO/axQa2JA+YkOwAVf2VCNBa5Soptowe9/bhKRvzwBAESx9aeLiyXl33aHPmF35KMlC3cxSTByV4inIvLkcaqAN3svewGFADPL2DtxFsxTnEkycrKxWbz9uGKYFCX3Bn2bYSrTT2F/7COwg5Zp+NYmnh9+nGg2ZkNb4= 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=TaAz/ENJ; arc=none smtp.client-ip=209.85.210.52 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-ot1-f52.google.com with SMTP id 46e09a7af769-6f0ea2c726aso1660994a34.3 for ; Sun, 12 May 2024 20:03:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715569419; x=1716174219; 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=yzBJEORmly2uweAvFf1l8zuxXIyjPhUuwee5HZnj+NQ=; b=TaAz/ENJRmk2nXOWjPkrKvqSHiD+1amGYqc9jImroruKtcSZhetmkp/p8WRydH4e1c qRkmec0KhfxkupyvIKJR7oc0GGOjy7YTPKTkNHKaXNvjVI5gjtXPSJ8Zz+5uxdTrEOM7 Hr7F5R3d8A3z72lkUJnCe4iN4gYcXs0+KhOtE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715569419; x=1716174219; 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=yzBJEORmly2uweAvFf1l8zuxXIyjPhUuwee5HZnj+NQ=; b=hRwaxfuOdtnEcwmnVJy107tfgHrwrnOvfz+uTSIAI8plOsu+2dqVX+FfaLEYg2gXA+ XIQwjhAerJXpuIlgO2KQI09NmV+jwep68uAzwEpH85odoQKI/mur+aoAzmj2ZWJSEdma pH+xAryWU0ltmkCxz7KWoq0aZw+P4VYWzlgNeFONpVxxjXOcrbdaW+sKP5oKOsIhl8Q+ ljPRvj8stv1BW32lK/fXsmV2B8dNXni97VvtHJerKaW5v91UeMlz4MChJYjqK2GZiQPb A7JJy7AZFhO8X9N0+5eIjByyVPLnLuHPN9CUAF1he/5KyNNLzW1Uc79zJFRer3KtOSGe O41A== X-Forwarded-Encrypted: i=1; AJvYcCUft58F2+6CtwAVYkHisK4qcrP2JnJ8tWWArTk+uKcp9uFrl5zsvFhtN1JkrVr2TPilbQRBMEy0htVqEb2d/e8VlJstXmBmZ/I35qoFWRP1 X-Gm-Message-State: AOJu0YzemNDjybFAkFIoe6x516pROy+qLwvZHKrVfpDC5uERd7Y/oaY+ 1w5oPPMbjnOwwVyLfeQ6ZjUMnqski2LRQAuV8DoZB2rhZ5hhScXI458VlabnaQ== X-Received: by 2002:a05:6870:b6a7:b0:23e:6d9a:8f45 with SMTP id 586e51a60fabf-24172f79503mr10911205fac.48.1715569419606; Sun, 12 May 2024 20:03:39 -0700 (PDT) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f4d2b30341sm6317982b3a.208.2024.05.12.20.03.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 May 2024 20:03:39 -0700 (PDT) Date: Sun, 12 May 2024 20:03:38 -0700 From: Kees Cook To: Erick Archer Cc: Marcel Holtmann , Johan Hedberg , Luiz Augusto von Dentz , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , "Gustavo A. R. Silva" , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , "Jiri Slaby (SUSE)" , Greg Kroah-Hartman , Geert Uytterhoeven , linux-bluetooth@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, llvm@lists.linux.dev Subject: Re: [PATCH v2] tty: rfcomm: prefer struct_size over open coded arithmetic Message-ID: <202405121959.50F6DDA@keescook> References: Precedence: bulk X-Mailing-List: linux-bluetooth@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 Sun, May 12, 2024 at 01:17:24PM +0200, Erick Archer wrote: > This is an effort to get rid of all multiplications from allocation > functions in order to prevent integer overflows [1][2]. > > As the "dl" variable is a pointer to "struct rfcomm_dev_list_req" and > this structure ends in a flexible array: > > struct rfcomm_dev_list_req { > [...] > struct rfcomm_dev_info dev_info[]; > }; > > the preferred way in the kernel is to use the struct_size() helper to > do the arithmetic instead of the calculation "size + count * size" in > the kzalloc() and copy_to_user() functions. > > At the same time, prepare for the coming implementation by GCC and Clang > of the __counted_by attribute. Flexible array members annotated with > __counted_by can have their accesses bounds-checked at run-time via > CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for > strcpy/memcpy-family functions). > > In this case, it is important to note that the logic needs a little > refactoring to ensure that the "dev_num" member is initialized before > the first access to the flex array. Specifically, add the assignment > before the list_for_each_entry() loop. > > Also remove the "size" variable as it is no longer needed and refactor > the list_for_each_entry() loop to use di[n] instead of (di + n). > > This way, the code is more readable, idiomatic and safer. > > This code was detected with the help of Coccinelle, and audited and > modified manually. > > Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1] > Link: https://github.com/KSPP/linux/issues/160 [2] > Signed-off-by: Erick Archer Looks good! Reviewed-by: Kees Cook > [...] > - bacpy(&(di + n)->src, &dev->src); > - bacpy(&(di + n)->dst, &dev->dst); > + bacpy(&di[n].src, &dev->src); > + bacpy(&di[n].dst, &dev->dst); Not an issue with your patch, but this helper is really pointless in the Bluetooth tree: static inline void bacpy(bdaddr_t *dst, const bdaddr_t *src) { memcpy(dst, src, sizeof(bdaddr_t)); } So the above could just be: di[n].src = dev->src; di[n].dst = dev->dst; :P -Kees -- Kees Cook