Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp2340463pxb; Sat, 25 Sep 2021 04:45:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJygt88EQR6hjeNOMzJjVdjSxWkZLeJQkwtTFai7hsqARvMTe2NIyvF7QYJ40hY9ZYDJWUmT X-Received: by 2002:a05:6e02:1d06:: with SMTP id i6mr11317482ila.299.1632570352698; Sat, 25 Sep 2021 04:45:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632570352; cv=none; d=google.com; s=arc-20160816; b=jzPBkrBwMgva46I9CxZpodBTR/TLw+aTQeLvR25aCrm4W0XVNoO0+yjTDecVGqPT76 eHFjGyFhmKRl3VsPcTOmyxpx/tEmmpt3lQ5Ia00GGI2gfWqdZF5a0ctUTk7q5wy13dbZ eDsWtG9DTZc1GLCPrG20quM67bmHSC5iWhsMhReBk9KBy/Qd7Yh2xuIv6VsVlpBU5Bdw B5V2MHX/LWWyMBO8Ox8FuSw+c0MrTemIyyLGaJYmU03DWbvCKyj9Xpw54/Jlil/KztQf 21EcaRj0vx90YfWgAcI89XYxAgmhVRYT4dd+m2uu9CWXM1SNhsTOmTBZV7UPX9c3WggQ eoOg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=tAo/f0EABIpWm2eM/0vAmxqpAUPvbS2Z1VOYgweOxzE=; b=DhSq3RYd0vszpxTCQZBLQfOonNTso+gZgFds4RmtTLMuyJG2DvaooAVtVOOBi70n/a qAahmwcL+KzDFixq5p3e75/Dwx080OW2sUV6aAF3Wo3rLR6vWbPi3tCL4rJefwDOY8lg XTMRLdaYNxZp1A25H6qtFZGueYf+F4eutf6jg7Wm1BpY+B3xpSTnJPkHcuqklXJWfGqz Gs1F5QxSN47k+MdTkqW28bU6uZKoDlQaGXQC6e7d1EGIWjjUOogBxBeQFuU16LAo0J6W 5H0Hp6xGVRbPCQ4FmwZpLxYTI+x5BbfT8Lqm7ME2FeBgbzBiSAtIfFtztdvjJ0ZEYbcH ayPg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmx.net header.s=badeba3b8450 header.b=bAWI5mFi; 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=fail (p=NONE sp=NONE dis=NONE) header.from=gmx.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r195si13676336ior.17.2021.09.25.04.45.40; Sat, 25 Sep 2021 04:45:52 -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=@gmx.net header.s=badeba3b8450 header.b=bAWI5mFi; 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=fail (p=NONE sp=NONE dis=NONE) header.from=gmx.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244585AbhIYLpJ (ORCPT + 99 others); Sat, 25 Sep 2021 07:45:09 -0400 Received: from mout.gmx.net ([212.227.17.21]:59933 "EHLO mout.gmx.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232363AbhIYLpJ (ORCPT ); Sat, 25 Sep 2021 07:45:09 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1632570210; bh=/zVNgQmryVA4k3e45CiCaw6IxcHSDeffj98fMqgaE6A=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date; b=bAWI5mFiHaXY8FMRckvJtbeD/t4hKQBNsSI0ng2+mv5DITKHzOGobD1F10x5iEoj6 J5fuBM8VQbaNOX9ftNwPKBQFXt7IJ0DcfaGSjKb/XxVttiY2nS1614Lk8L+JXL8lHt xpQ/uaUWmGqxBzzP2fhMXRx5Bqi4k90Rvj4FUm9g= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from localhost.localdomain ([79.150.72.99]) by mail.gmx.net (mrgmx105 [212.227.17.174]) with ESMTPSA (Nemesis) id 1N79yG-1mvqiD3jQK-017SQE; Sat, 25 Sep 2021 13:43:30 +0200 From: Len Baker To: Alexander Viro Cc: Len Baker , Kees Cook , "Gustavo A. R. Silva" , linux-fsdevel@vger.kernel.org, linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] writeback: prefer struct_size over open coded arithmetic Date: Sat, 25 Sep 2021 13:43:08 +0200 Message-Id: <20210925114308.11455-1-len.baker@gmx.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:UgePny2MsPa0UaX6WMhndso+fvJ4dp1sHEVaBNCXxtmvCRKpZMJ /8NJZk16YV7cFI3tVYkMOyGRBLF5Se1TCudVnP6WHW/j3LyMxDY3mUFdGNEa1YemeUTfsg5 JfIauXX6YbI2A3xKkIx68RV5IUvvs5YtFet0jQEoNzLmNNlf1lzg69qBu18WQWJ3Xz4YOBS ITalQTrRowDwolyNuBn3A== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:yBHF46CKjB8=:bVIKrlHJ3G7BlOYhcfFjqq pKEP1pkMHiXCUjYqgc2UTGighUmZfJ31xCho1Ntft6+7Xm03Ktbj4tjmWj2YqaBvJNQbERgHm Fw++sfk9D5CzTsChvDj5lmOo0EptkYQSKNU6FYnosetszltdgWWb//aBmph1FxfHuYn8VkgFf XBE9M5y6VO8+Z+5ulQl5J01NjIbjPEMYzr72e7Bk0g6mlOJB970qSdq0r3i7rIKjSRQu1gtdK sgdqcIzf5b2HWH098x++F2y9tABS9KUl8998vNrs6PVe9lMuxT5B6k1DGVCKc3nFU9xDYi8XE fKgocq32w0nSwieeAU9RWdNJNLoXQyLGXgb1346lNO5ZCJMXzkkHeMxAvvR765aGBnutgDInp 36OoiPTACd5kFw5IqUmixJwnrxUkaoclYLpHqo7s2vyw3PMF5x3rUi7rf9AhGSTjif73ja3Bk sTH/kCBA3u/fz6pEsi3Mcy3TZZQPj2vr9Vxt6FGGyyxngi+ikef2i5lkrID+54WZWodhVUVr+ xYLRqR/2KCscSSF8vxP/3FgDP+fV+Aof5DRzJiI+FiPL/ei3QKjjJNyrQnmUHDaVZbMCHRra4 /VGMX5lFm17+NUuSD7K2zai3ZllP2hKunw4Kh13QtmxQZp/3WLuJQ36boyMF7ycTArw0PQljI SZpUr4q3W//fSrj17UmLT695BXcpWMscQGzGu9iWem86rmLIxx+7z+wpro751cQyLfn50ZmVF 1FNlePLmdkBCAqO3+xVZClTt4v5Y+t5Rr995obXKMw6uHd0KeOBOVBFki1cTgEjlnmvff5x/6 X6RAN8k7dTczYaMwwGVV9+Fsa2NX/EHo5hRl5tAxg4I8YtJdZAWnDz9VWrl/l1meZlXdCGnOH n2JbT3vnGiaP4syVY1mB76TzXWCOu2gPhj0ubranoYidvBCe34h0yoN58UVOnvG2GpRGrG+rg 0snraO+ILTkG8b7sOAxVoLgHHwaHu49ih1ULoYwiTiZVoXOBRXRRfXvQfv8Z6l8OKR/f4TBxd 5j2VojiPHUAa8ANEXk3PBFlA6Q8SccA6M+X8+XooH8skVSD8mRD8w3BxOpJzgKIpuIibxD4io 3MjIRILnmlCfOA= Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org As noted in the "Deprecated Interfaces, Language Features, Attributes, and Conventions" documentation [1], size calculations (especially multiplication) should not be performed in memory allocator (or similar) function arguments due to the risk of them overflowing. This could lead to values wrapping around and a smaller allocation being made than the caller was expecting. Using those allocations could lead to linear overflows of heap memory and other misbehaviors. In this case these are not actually dynamic sizes: all the operands involved in the calculation are constant values. However it is better to refactor them anyway, just to keep the open-coded math idiom out of code. So, use the struct_size() helper to do the arithmetic instead of the argument "size + count * size" in the kzalloc() functions. This code was detected with the help of Coccinelle and audited and fixed manually. [1] https://www.kernel.org/doc/html/latest/process/deprecated.html#open-co= ded-arithmetic-in-allocator-arguments Signed-off-by: Len Baker =2D-- Changelog v1 -> v2 - Rebase against v5.15-rc2 - Refactor another instance in the same file (Gustavo A. R. Silva). - Update the commit changelog to inform that this code was detected using a Coccinelle script (Gustavo A. R. Silva). fs/fs-writeback.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c index 81ec192ce067..5eb0ada7468c 100644 =2D-- a/fs/fs-writeback.c +++ b/fs/fs-writeback.c @@ -566,7 +566,7 @@ static void inode_switch_wbs(struct inode *inode, int = new_wb_id) if (atomic_read(&isw_nr_in_flight) > WB_FRN_MAX_IN_FLIGHT) return; - isw =3D kzalloc(sizeof(*isw) + 2 * sizeof(struct inode *), GFP_ATOMIC); + isw =3D kzalloc(struct_size(isw, inodes, 2), GFP_ATOMIC); if (!isw) return; @@ -624,8 +624,8 @@ bool cleanup_offline_cgwb(struct bdi_writeback *wb) int nr; bool restart =3D false; - isw =3D kzalloc(sizeof(*isw) + WB_MAX_INODES_PER_ISW * - sizeof(struct inode *), GFP_KERNEL); + isw =3D kzalloc(struct_size(isw, inodes, WB_MAX_INODES_PER_ISW), + GFP_KERNEL); if (!isw) return restart; =2D- 2.25.1