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=-16.1 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, MENTIONS_GIT_HOSTING,NICE_REPLY_A,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_SANE_1 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 61D98C4727C for ; Tue, 29 Sep 2020 23:55:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E237B206D8 for ; Tue, 29 Sep 2020 23:55:28 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=embeddedor.com header.i=@embeddedor.com header.b="QKK+BopR" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729178AbgI2Xz1 (ORCPT ); Tue, 29 Sep 2020 19:55:27 -0400 Received: from gateway30.websitewelcome.com ([192.185.196.18]:32280 "EHLO gateway30.websitewelcome.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728192AbgI2Xz1 (ORCPT ); Tue, 29 Sep 2020 19:55:27 -0400 X-Greylist: delayed 1499 seconds by postgrey-1.27 at vger.kernel.org; Tue, 29 Sep 2020 19:55:25 EDT Received: from cm11.websitewelcome.com (cm11.websitewelcome.com [100.42.49.5]) by gateway30.websitewelcome.com (Postfix) with ESMTP id 63B8A1AB7A for ; Tue, 29 Sep 2020 18:09:04 -0500 (CDT) Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with SMTP id NOk4kK6goOIGpNOk4kb5Ya; Tue, 29 Sep 2020 18:09:04 -0500 X-Authority-Reason: nr=8 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=embeddedor.com; s=default; h=Content-Transfer-Encoding:Content-Type: In-Reply-To:MIME-Version:Date:Message-ID:Subject:From:References:Cc:To:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=F3Hu8rdqxutt27Kuqs0e/1Cw8tmnLLeziqDGNvmqmjo=; b=QKK+BopRHfjGmAN4Ju9rzC4OgN ElYgDFU2I6w8cbKEsM53Oicl3Y0/ZDe38r7c7qq0QpUFZQszW8m2HD60/7ux4Zp84RCo83VqdyAQs Bdg22amapHue2QtBS8tTfQMecwGfGQ+3wUo08Brc/VOGE0GGwCvMtbpfLzenUKMO/c+j3rncANujx 8C5P1RhuNsnq26vWCiAIBv45aftGe5lZGqExgJzdowwdlE88hmdjgn+j9ABPQ8kh+k1qTY9Gyj1Cf MAZADaUyaZDEzWDOz/RFGpvCkVlZt+gDS2LRs63aD05JsQWQq3jGR5efgt6c0shB/guJBdycnYQg0 xTQsdTQQ==; Received: from 187-162-31-110.static.axtel.net ([187.162.31.110]:52182 helo=[192.168.15.4]) by gator4166.hostgator.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1kNOk1-002v7T-Aa; Tue, 29 Sep 2020 18:09:01 -0500 To: "Gustavo A. R. Silva" , Darren Hart , Andy Shevchenko , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "x86@kernel.org H. Peter Anvin" , John Stultz Cc: platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org, Kees Cook , linux-hardening@vger.kernel.org References: <20200518190114.GA7757@embeddedor> <20200521232415.GD29907@embeddedor> From: "Gustavo A. R. Silva" Autocrypt: addr=g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6KFtZosQ02EGMuHQI6Zk91jbx96n rdsSdBLGH3hbvLvjZm3C+fNlVb9uvWbdznObqcJxSH3SGOZ7kCHuVmXUcqozol6ioMHMb+In rHPP16aVDTBTPEGwgxXI38f7SUEn+NpbizWdLNz2hc907DvoPm6HEGCanpcAEQEAAcLBZQQY AQgADwUCWywcDAIbDAUJCWYBgAAKCRBHBbTLRwbbMdsZEACUjmsJx2CAY+QSUMebQRFjKavw XB/xE7fTt2ahuhHT8qQ/lWuRQedg4baInw9nhoPE+VenOzhGeGlsJ0Ys52sdXvUjUocKgUQq 6ekOHbcw919nO5L9J2ejMf/VC/quN3r3xijgRtmuuwZjmmi8ct24TpGeoBK4WrZGh/1hAYw4 ieARvKvgjXRstcEqM5thUNkOOIheud/VpY+48QcccPKbngy//zNJWKbRbeVnimua0OpqRXhC rEVm/xomeOvl1WK1BVO7z8DjSdEBGzbV76sPDJb/fw+y+VWrkEiddD/9CSfgfBNOb1p1jVnT 2mFgGneIWbU0zdDGhleI9UoQTr0e0b/7TU+Jo6TqwosP9nbk5hXw6uR5k5PF8ieyHVq3qatJ 9K1jPkBr8YWtI5uNwJJjTKIA1jHlj8McROroxMdI6qZ/wZ1ImuylpJuJwCDCORYf5kW61fcr HEDlIvGc371OOvw6ejF8ksX5+L2zwh43l/pKkSVGFpxtMV6d6J3eqwTafL86YJWH93PN+ZUh 6i6Rd2U/i8jH5WvzR57UeWxE4P8bQc0hNGrUsHQH6bpHV2lbuhDdqo+cM9ehGZEO3+gCDFmK rjspZjkJbB5Gadzvts5fcWGOXEvuT8uQSvl+vEL0g6vczsyPBtqoBLa9SNrSVtSixD1uOgyt AP7RWS474w== Subject: Re: [PATCH] x86/uv/time: Replace one-element array and save heap space Message-ID: <1d4a85b8-22e3-3c0d-5beb-28dadda5848a@embeddedor.com> Date: Tue, 29 Sep 2020 18:14:45 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <20200521232415.GD29907@embeddedor> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator4166.hostgator.com X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - embeddedor.com X-BWhitelist: no X-Source-IP: 187.162.31.110 X-Source-L: No X-Exim-ID: 1kNOk1-002v7T-Aa X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 187-162-31-110.static.axtel.net ([192.168.15.4]) [187.162.31.110]:52182 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 10 X-Source-Cap: Z3V6aWRpbmU7Z3V6aWRpbmU7Z2F0b3I0MTY2Lmhvc3RnYXRvci5jb20= X-Local-Domain: yes Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi all, Who can take this? Thanks -- Gustavo On 5/21/20 18:24, Gustavo A. R. Silva wrote: > [+CC John Stultz and +Kees' Reviewed-by tag] > > Reviewed-by: Kees Cook > > On Mon, May 18, 2020 at 02:01:14PM -0500, Gustavo A. R. Silva wrote: >> The current codebase makes use of one-element arrays in the following >> form: >> >> struct something { >> int length; >> u8 data[1]; >> }; >> >> struct something *instance; >> >> instance = kmalloc(sizeof(*instance) + size, GFP_KERNEL); >> instance->length = size; >> memcpy(instance->data, source, size); >> >> but the preferred mechanism to declare variable-length types such as >> these ones is a flexible array member[1][2], introduced in C99: >> >> struct foo { >> int stuff; >> struct boo array[]; >> }; >> >> By making use of the mechanism above, we will get a compiler warning >> in case the flexible array does not occur last in the structure, which >> will help us prevent some kind of undefined behavior bugs from being >> inadvertently introduced[3] to the codebase from now on. So, replace >> the one-element array with a flexible-array member. >> >> Also, make use of the new struct_size() helper to properly calculate the >> total size needed to allocate dynamic memory for struct uv_rtc_timer_head. >> Notice that, due to the use of a one-element array, space for an extra >> struct cpu: >> >> struct { >> int lcpu; /* systemwide logical cpu number */ >> u64 expires; /* next timer expiration for this cpu */ >> } cpu[1] >> >> was being allocated at the moment of applying the sizeof operator to >> struct uv_rtc_timer_head in the call to kmalloc_node() at line 159: >> >> 159 head = kmalloc_node(sizeof(struct uv_rtc_timer_head) + >> 160 (uv_blade_nr_possible_cpus(bid) * >> 161 2 * sizeof(u64)), >> 162 GFP_KERNEL, nid); >> >> but that extra cpu[] was never actually being accessed due to the >> following piece of code at line 168: >> >> 168 head->ncpus = uv_blade_nr_possible_cpus(bid); >> >> and the piece of code at line 187: >> >> 187 for (c = 0; c < head->ncpus; c++) { >> 188 u64 exp = head->cpu[c].expires; >> 189 if (exp < lowest) { >> 190 bcpu = c; >> 191 lowest = exp; >> 192 } >> 193 } >> >> so heap space was being wasted. >> >> Another thing important to notice is that through the use of the >> struct_size() helper, code at line 161: >> >> 161 2 * sizeof(u64)), >> >> is changed to now be the actual size of struct cpu; see >> sizeof(*(p)->member) at include/linux/overflow.h:314: >> >> 314 #define struct_size(p, member, n) \ >> 315 __ab_c_size(n, \ >> 316 sizeof(*(p)->member) + __must_be_array((p)->member),\ >> 317 sizeof(*(p))) >> >> As a side note, the original developer could have implemented code at line >> 161: 2 * sizeof(64) as follows: >> >> sizeof(*head->cpu) >> >> This issue has been out there since 2009. >> >> This issue was found with the help of Coccinelle and fixed _manually_. >> >> [1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html >> [2] https://github.com/KSPP/linux/issues/21 >> [3] commit 76497732932f ("cxgb3/l2t: Fix undefined behaviour") >> >> Signed-off-by: Gustavo A. R. Silva >> --- >> arch/x86/platform/uv/uv_time.c | 7 +++---- >> 1 file changed, 3 insertions(+), 4 deletions(-) >> >> diff --git a/arch/x86/platform/uv/uv_time.c b/arch/x86/platform/uv/uv_time.c >> index 7af31b245636..993a8ae6fdfb 100644 >> --- a/arch/x86/platform/uv/uv_time.c >> +++ b/arch/x86/platform/uv/uv_time.c >> @@ -52,7 +52,7 @@ struct uv_rtc_timer_head { >> struct { >> int lcpu; /* systemwide logical cpu number */ >> u64 expires; /* next timer expiration for this cpu */ >> - } cpu[1]; >> + } cpu[]; >> }; >> >> /* >> @@ -156,9 +156,8 @@ static __init int uv_rtc_allocate_timers(void) >> struct uv_rtc_timer_head *head = blade_info[bid]; >> >> if (!head) { >> - head = kmalloc_node(sizeof(struct uv_rtc_timer_head) + >> - (uv_blade_nr_possible_cpus(bid) * >> - 2 * sizeof(u64)), >> + head = kmalloc_node(struct_size(head, cpu, >> + uv_blade_nr_possible_cpus(bid)), >> GFP_KERNEL, nid); >> if (!head) { >> uv_rtc_deallocate_timers(); >> -- >> 2.26.2 >>