Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp2124640pxb; Sun, 5 Sep 2021 09:17:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxgfMUjzFjdNB1qahE+e9h64LXuKWeLaHFBXuMKaQmc9axQYYgucaFtJO3Q5IfvWNEvMzxV X-Received: by 2002:a50:998c:: with SMTP id m12mr9557474edb.327.1630858630649; Sun, 05 Sep 2021 09:17:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630858630; cv=none; d=google.com; s=arc-20160816; b=sJKTTjkgBh8RftO0TTFmylAQnq1pyc1Zzosqz92UX7adkzIFHyazR6pTDxxVnfQOuL LV1ysExnIzy2iHoE9TkFYfuS+5NhHbX/uUh4h3LKtHGcFt3urvmFPn3VVM4BPwuDdzbF J6yPrFNjuk3btAZyUlVZTZ6TeFHSK/iXPuy+4y/xUnfzlGf/fQKmsy2X0d1gyxNV50Ik ol+5eCRW5Ea+J+ppWQ3NqcJeiAhWT0ObRekzMWA4VH/7mSDqmUhFNxUvRfquFbSziqMZ UJbSggyP3qH9n9XSV7pQXvXoS5ITrbe1eANGnju966Al2VARuLFIRVlPLGAdJJ8Hcp6q LABQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=Z1qfnInw6eBkH7VjzzpSAckO42XtWnywfSjVuFVYJ3Q=; b=b+uZw4buntRQG/d7xg7BrN7/DnJQpPcRN/bIdzafEzSwsSZrMXvXi7r/LRKz7Jd4f7 Vzc2F9JW+kuEd1Yep0OomCszvJcoF9qF2fMDv2QQuRMgYCNXnmegbC+goGXgsQIcfJW8 5IGwWpXU+pKiOozwlQnW1qV3LkXTHD0kCn0MOMebyw7XviMVXx9/X+dxzFYmWgRHLmVv j7/848x6KD+kSDEodat8SZ+cTv87SCQBgZTcp744GGqJ+OdhqESxGnOCfSOGyfDdNjz8 JamiJkOAB+ISpR52MP+uD/xgpDRUaws1FtwgjDu0DJJq75NnXOO45/bPPms+g73jdqen cnKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=PU14FLyz; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i11si7656712edb.418.2021.09.05.09.16.45; Sun, 05 Sep 2021 09:17:10 -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=@gmail.com header.s=20210112 header.b=PU14FLyz; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237752AbhIEQPh (ORCPT + 99 others); Sun, 5 Sep 2021 12:15:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45216 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229566AbhIEQPg (ORCPT ); Sun, 5 Sep 2021 12:15:36 -0400 Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E7007C061575; Sun, 5 Sep 2021 09:14:32 -0700 (PDT) Received: by mail-ej1-x635.google.com with SMTP id jg16so8263649ejc.1; Sun, 05 Sep 2021 09:14:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Z1qfnInw6eBkH7VjzzpSAckO42XtWnywfSjVuFVYJ3Q=; b=PU14FLyzP6DjD1Xg8mWVpaBO2svY6s7u4j3xgfY/lmmGPcXclmQGtbrjSBwOBgChCi 8U7E3QMqeWunvrr7r0hXeVFaiyvvN47s0sSC9Bu6Slhfyi1XHFeLvQxzz9QCQeGODFu6 GzBkWImo3OYYrP/A9bu779vxlYlBVhb6LbO0u+wedSDSFzNxtyLlLEhLCXbIJVNaGojQ UKpGG/kQC2Qj4bMWlecREXAMhXXp0zR6yQo1zym6vn89i9m7VS3HOiPnjngTLTjryZNG O93maFPHunXPw9vRhlTcF+NoE6qAziXBHYdhI+PlI2WYjNz5KZAU3DVjAxZ/Yc/0h34/ EntA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Z1qfnInw6eBkH7VjzzpSAckO42XtWnywfSjVuFVYJ3Q=; b=koiZJxTbwRhvgzA4mSPlMHBYO1wQGTKOFpGZEg4fHsnnr3J7zOHjfAag/SzI8ao9SK nJFqXW+8wN4lKZ0F4MVOtpTMYdhjz4RVg+6p2HgCTVYW+liSsX5ifK2jd2qxDUfBYvTK 36xxtmoxFX+SU/RWI0d1ldPR4Si440TiMB+JH7jPytT36IJOetIfaPn9exb9t4hRKnpY Go8/XmF/Q/3FYAJTn4Gt0USgtsq0mGgwRMwzYZYK/0U6LU8bWr4zDcmMF6oekI5gbkj8 xsg9lb3gIDXl9S2kgfc6N4fWPcc6cXMt2HfeSBrVrunrzDswKL5S4MODiWLb83cUH9A1 g9ww== X-Gm-Message-State: AOAM53053aaGwnqdnBUpJIF7qdShuoPEaNeoN7yBqsxRKejnAj/hc3O2 kv0NYIzHD7tNi/oegsbPX4eoABt9v5SKq6uYZU4= X-Received: by 2002:a17:906:52c5:: with SMTP id w5mr9401187ejn.567.1630858471430; Sun, 05 Sep 2021 09:14:31 -0700 (PDT) MIME-Version: 1.0 References: <20210905155728.11147-1-len.baker@gmx.com> In-Reply-To: <20210905155728.11147-1-len.baker@gmx.com> From: Andy Shevchenko Date: Sun, 5 Sep 2021 19:13:54 +0300 Message-ID: Subject: Re: [PATCH] serial: 8250_pci: Prefer struct_size over open coded arithmetic To: Len Baker Cc: Greg Kroah-Hartman , Jiri Slaby , Kees Cook , "Maciej W. Rozycki" , Andy Shevchenko , Mario Kleiner , Geert Uytterhoeven , Randy Wright , Christian Gmeiner , Tobias Diedrich , YueHaibing , "open list:SERIAL DRIVERS" , linux-hardening@vger.kernel.org, Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Sep 5, 2021 at 6:58 PM Len Baker wrote: > > 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. > > So, use the struct_size() helper to do the arithmetic instead of the > argument "size + size * count" in the kzalloc() function. Makes sense Reviewed-by: Andy Shevchenko > [1] https://www.kernel.org/doc/html/v5.14/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments > > Signed-off-by: Len Baker > --- > drivers/tty/serial/8250/8250_pci.c | 4 +--- > 1 file changed, 1 insertion(+), 3 deletions(-) > > diff --git a/drivers/tty/serial/8250/8250_pci.c b/drivers/tty/serial/8250/8250_pci.c > index a808c283883e..b97ade35d4a3 100644 > --- a/drivers/tty/serial/8250/8250_pci.c > +++ b/drivers/tty/serial/8250/8250_pci.c > @@ -3981,9 +3981,7 @@ pciserial_init_ports(struct pci_dev *dev, const struct pciserial_board *board) > nr_ports = rc; > } > > - priv = kzalloc(sizeof(struct serial_private) + > - sizeof(unsigned int) * nr_ports, > - GFP_KERNEL); > + priv = kzalloc(struct_size(priv, line, nr_ports), GFP_KERNEL); > if (!priv) { > priv = ERR_PTR(-ENOMEM); > goto err_deinit; > -- > 2.25.1 > -- With Best Regards, Andy Shevchenko