Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp2495584imm; Wed, 16 May 2018 13:55:25 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqPY5CYgg4jgIwPaR+snU5QLFA9bjlsHnfRiYZherXloT8maXkz5ocf+Ujn1k9GAe+e3g07 X-Received: by 2002:a17:902:265:: with SMTP id 92-v6mr2397569plc.368.1526504125391; Wed, 16 May 2018 13:55:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526504125; cv=none; d=google.com; s=arc-20160816; b=ZgxqlFbMh2p/u3UkEvCIIUwpSzuMp/3H5l/3T6qnsyUzCK1FRRlqkKWKAG9DIze/zY g6NhC3FUD9I5Ji2wqH+OIrSjeqC9/7Z9vVboyPET94GnOc3G2KivftQD96q3S1I+9ie+ 1V78baAXwQHhDAgkhM3NPAYypNw6I0JGGNpzlb2CNiAXlRTbk2Y6ZuXEzcKbzJ2Up/BL zUUnaoh6ai38gNHVy5OSnIS6jltjzFICTQ+c79TzunKe/NXy+zOvSzTAGHK88oKcIKIa oBBwl99kc04wfxV8rj8HKADvtGLR7O912crFwXCEIKfX/R3ntDq7Oy6KtnOPyzqtvt1/ f5Kg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:message-id:references :in-reply-to:subject:cc:to:from:date:content-transfer-encoding :mime-version:dkim-signature:dkim-signature :arc-authentication-results; bh=ANBIaML1CeGpDg8P/K+q4iF0/ZBfgXkZQzgidHRkxO8=; b=oZNsBQgFEDm30Gv82R1EG6dkAAakw34aQF6JkxoLeKzjjux1eMri2FE5D+W3HwvYI0 wO2ccAKAP7LxN18qeYES0+5CTv6r1Cmc3geWduXxrUTbxykxZSoy43wbeS8UivqiuDeY Ti49yOos2kLw4nm9HhdjVSjdKgVWUYm6sWHhqqf7nBebtnslTnRMDfdaIYLzoWqZ7/IB xlepRLeuUYI9Upl+KXS5PqWau1zxGfPj4Wj1p+KhSY6SU9jwSxAYP2i5ykvVJ2n99EBm ebzXyabQqYhl5VPgz+MtIvkCZBI649Ca+qPgvP83ARc6cEq7UoHn1/+1vjiC8U58a49W k7Ww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@codeaurora.org header.s=default header.b=cOZ0Cs+2; dkim=pass header.i=@codeaurora.org header.s=default header.b=oV6IhVON; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y8-v6si1390105pgs.351.2018.05.16.13.55.11; Wed, 16 May 2018 13:55:25 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@codeaurora.org header.s=default header.b=cOZ0Cs+2; dkim=pass header.i=@codeaurora.org header.s=default header.b=oV6IhVON; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752022AbeEPUyx (ORCPT + 99 others); Wed, 16 May 2018 16:54:53 -0400 Received: from smtp.codeaurora.org ([198.145.29.96]:42816 "EHLO smtp.codeaurora.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751665AbeEPUyw (ORCPT ); Wed, 16 May 2018 16:54:52 -0400 Received: by smtp.codeaurora.org (Postfix, from userid 1000) id C20A0601EA; Wed, 16 May 2018 20:54:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1526504091; bh=S7/oKn3TqSY/26bEJk1pksRZCvCMqKLyKVC4EQmiWmA=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=cOZ0Cs+2ouG242JXliXV/1Ku2ORUaVE2y1gGwlXW0LiQz5fi1kWuzAfvkyR4z/vZA LcZwctzWXEbkEEltVgH5nGs2LX3FTmbpCfYWQTDsbx3/g8cMZhdGy7YNj+VK69AFHk /PlNhk92h+pIY0DXM72UHkbxND1DX8uw4KnONucs= X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on pdx-caf-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.8 required=2.0 tests=ALL_TRUSTED,BAYES_00, DKIM_SIGNED,T_DKIM_INVALID autolearn=no autolearn_force=no version=3.4.0 Received: from mail.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.codeaurora.org (Postfix) with ESMTP id D2678601EA; Wed, 16 May 2018 20:54:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1526504090; bh=S7/oKn3TqSY/26bEJk1pksRZCvCMqKLyKVC4EQmiWmA=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=oV6IhVONT+yeRSLxEu1uP15Qckv6KoZNanezgedUwf0SgZTf4hrdVT4KScQnsLrIS Bww4vKDihBKTaYj62xtZellK4SW8huEDmJLm8HJIkC1GOrBkiHM95z+cMp2NEYYP+w 2FmW66QUn46bwO6rqZZ6IhwbrNILrRSIH53OUtkM= MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Wed, 16 May 2018 13:54:50 -0700 From: Subhash Jadavani To: Kees Cook Cc: "Martin K. Petersen" , Vinayak Holikatti , "James E.J. Bottomley" , linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi-owner@vger.kernel.org Subject: Re: [PATCH] scsi: ufs: ufshcd: Remove VLA usage In-Reply-To: <20180502235809.GA13998@beast> References: <20180502235809.GA13998@beast> Message-ID: X-Sender: subhashj@codeaurora.org User-Agent: Roundcube Webmail/1.2.5 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2018-05-02 16:58, Kees Cook wrote: > On the quest to remove all VLAs from the kernel[1] this moves buffers > off the stack. In the second instance, this collapses two separately > allocated buffers into a single buffer, since they are used > consecutively, > which saves 256 bytes (QUERY_DESC_MAX_SIZE + 1) of stack space. > > [1] > https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com > > Signed-off-by: Kees Cook > --- > drivers/scsi/ufs/ufshcd.c | 34 ++++++++++++++++++++++++++-------- > 1 file changed, 26 insertions(+), 8 deletions(-) > > diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c > index 00e79057f870..a271534362f6 100644 > --- a/drivers/scsi/ufs/ufshcd.c > +++ b/drivers/scsi/ufs/ufshcd.c > @@ -5958,14 +5958,18 @@ static void ufshcd_init_icc_levels(struct > ufs_hba *hba) > { > int ret; > int buff_len = hba->desc_size.pwr_desc; > - u8 desc_buf[hba->desc_size.pwr_desc]; > + u8 *desc_buf; > + > + desc_buf = kmalloc(buff_len, GFP_KERNEL); > + if (!desc_buf) > + return; > > ret = ufshcd_read_power_desc(hba, desc_buf, buff_len); > if (ret) { > dev_err(hba->dev, > "%s: Failed reading power descriptor.len = %d ret = %d", > __func__, buff_len, ret); > - return; > + goto out; > } > > hba->init_prefetch_data.icc_level = > @@ -5983,6 +5987,8 @@ static void ufshcd_init_icc_levels(struct ufs_hba > *hba) > "%s: Failed configuring bActiveICCLevel = %d ret = %d", > __func__, hba->init_prefetch_data.icc_level , ret); > > +out: > + kfree(desc_buf); > } > > /** > @@ -6052,9 +6058,17 @@ static int ufs_get_device_desc(struct ufs_hba > *hba, > struct ufs_dev_desc *dev_desc) > { > int err; > + size_t buff_len; > u8 model_index; > - u8 str_desc_buf[QUERY_DESC_MAX_SIZE + 1] = {0}; > - u8 desc_buf[hba->desc_size.dev_desc]; > + u8 *desc_buf; > + > + buff_len = max_t(size_t, hba->desc_size.dev_desc, > + QUERY_DESC_MAX_SIZE + 1); > + desc_buf = kmalloc(buff_len, GFP_KERNEL); > + if (!desc_buf) { > + err = -ENOMEM; > + goto out; > + } > > err = ufshcd_read_device_desc(hba, desc_buf, > hba->desc_size.dev_desc); > if (err) { > @@ -6072,7 +6086,10 @@ static int ufs_get_device_desc(struct ufs_hba > *hba, > > model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME]; > > - err = ufshcd_read_string_desc(hba, model_index, str_desc_buf, > + /* Zero-pad entire buffer for string termination. */ > + memset(desc_buf, 0, buff_len); > + > + err = ufshcd_read_string_desc(hba, model_index, desc_buf, > QUERY_DESC_MAX_SIZE, true/*ASCII*/); > if (err) { > dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n", > @@ -6080,15 +6097,16 @@ static int ufs_get_device_desc(struct ufs_hba > *hba, > goto out; > } > > - str_desc_buf[QUERY_DESC_MAX_SIZE] = '\0'; > - strlcpy(dev_desc->model, (str_desc_buf + QUERY_DESC_HDR_SIZE), > - min_t(u8, str_desc_buf[QUERY_DESC_LENGTH_OFFSET], > + desc_buf[QUERY_DESC_MAX_SIZE] = '\0'; > + strlcpy(dev_desc->model, (desc_buf + QUERY_DESC_HDR_SIZE), > + min_t(u8, desc_buf[QUERY_DESC_LENGTH_OFFSET], > MAX_MODEL_LEN)); > > /* Null terminate the model string */ > dev_desc->model[MAX_MODEL_LEN] = '\0'; > > out: > + kfree(desc_buf); > return err; > } > > -- > 2.17.0 Looks good to me. Reviewed-by: Subhash Jadavani -- The Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project