From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.0 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AF8C1C10F14 for ; Mon, 8 Apr 2019 10:52:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8B0E6214C6 for ; Mon, 8 Apr 2019 10:52:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726620AbfDHKw3 (ORCPT ); Mon, 8 Apr 2019 06:52:29 -0400 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:46368 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726050AbfDHKw3 (ORCPT ); Mon, 8 Apr 2019 06:52:29 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 7B121168F; Mon, 8 Apr 2019 03:52:28 -0700 (PDT) Received: from [10.1.196.75] (e110467-lin.cambridge.arm.com [10.1.196.75]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 5A9E13F718; Mon, 8 Apr 2019 03:52:25 -0700 (PDT) Subject: Re: [PATCH] stm class: Fix out of bound access from bitmap allocation To: David Laight , Sai Prakash Ranjan , Alexander Shishkin , Greg Kroah-Hartman , Mulu He , Tingwei Zhang , Maxime Coquelin , Alexandre Torgue , "linux-stm32@st-md-mailman.stormreply.com" , Mathieu Poirier , Suzuki K Poulose , Mike Leach , Leo Yan Cc: Rajendra Nayak , "linux-arm-msm@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" , Sibi Sankar , Vivek Gautam , "linux-arm-kernel@lists.infradead.org" References: <20190405122256.27840-1-saiprakash.ranjan@codeaurora.org> <850ba66b-0e4a-2027-5640-0ee339a7868c@arm.com> <2d5e435bf7b344fb835fc58ac36e1c19@AcuMS.aculab.com> From: Robin Murphy Message-ID: Date: Mon, 8 Apr 2019 11:52:23 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <2d5e435bf7b344fb835fc58ac36e1c19@AcuMS.aculab.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 08/04/2019 11:33, David Laight wrote: > From: Robin Murphy >> Sent: 08 April 2019 11:24 >> On 07/04/2019 05:31, Sai Prakash Ranjan wrote: >>> On 4/5/2019 6:44 PM, David Laight wrote: >>>> From: Sai Prakash Ranjan >>>>> >>>>> diff --git a/drivers/hwtracing/stm/core.c b/drivers/hwtracing/stm/core.c >>>>> index 93ce3aa740a9..21a5838f6e67 100644 >>>>> --- a/drivers/hwtracing/stm/core.c >>>>> +++ b/drivers/hwtracing/stm/core.c >>>>> @@ -168,7 +168,7 @@ static int stp_master_alloc(struct stm_device >>>>> *stm, unsigned int idx) >>>>>       struct stp_master *master; >>>>>       size_t size; >>>>> >>>>> -    size = ALIGN(stm->data->sw_nchannels, 8) / 8; >>>>> +    size = ALIGN(stm->data->sw_nchannels, STM_MASTER_SZ) / >>>>> STM_MASTER_SZ; >>>> >>>> I'm not sure that using STP_MASTER_SZ improves readability at all. >>>> >>> >>> I thought it was better to have a macro than directly specifying >>> sizeof(unsigned long), anyways I can change it. >>> >>>> Is there something that gives the size of a bitmap for 'n' items? >>>> >>> >>> Not sure if there is something. >> >> If you were to ask the question "how does the bitmap code itself know >> what the total size of a bitmap is?", that would quickly lead you >> towards BITS_TO_LONGS() ;) >> >> And given that stp_master::chan_map is already an appropriate type, that >> suggests simplifying the entire calculation down to something neat and >> tidy like: >> >> size = offsetof(struct stp_master, chan_map[BITS_TO_LONGS(stm->data->sw_nchannels)]); > > Except that is invalid. > You can't use offsetof() with something that isn't a compile time constant. Oh, I see the standard does actually say that, although there seem to be enough non-constant uses in the kernel to suggest that it still works in practice. However, while writing the above I was still trying to remember the other thing I'd seen for handling precisely this variable-sized-struct situation, which I've now found again, namely struct_size(). I guess now I understand why that isn't implemented in terms of offsetof(), thanks for the nudge :) Robin.