Received: by 10.192.165.148 with SMTP id m20csp1227666imm; Wed, 2 May 2018 16:59:11 -0700 (PDT) X-Google-Smtp-Source: AB8JxZoo9/GVEr0u9cN8WWMhzyGT3ksw+fbf7Tx5PerFs4HawPL4t7FE8nZP7m8rmCaIqu17+lkg X-Received: by 10.98.160.150 with SMTP id p22mr21194339pfl.9.1525305551393; Wed, 02 May 2018 16:59:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525305551; cv=none; d=google.com; s=arc-20160816; b=Lst+Vpa8ImVWT2IeK3do352zGyiPxdjUERNKFYaphDY421jSSPVpFxmJ7JHK20ztsk mWozEQAuwvVgA6hmEaq3Cgv80FSRX5avkkVro/Rl1yIIDmSsmcYg+KvQpugbDFXhhwh8 Uip8VJCktpXrJx4TRthh8P+OcRcibE2bdih0dnFOaVg5bwRNa8lliID6IZVcho6iSCn1 O2fTvZWliMRNRUA0m3rgQsKlrSF487lwpVzRWBsm1XWxj89gD2OYK1QUUYM5F1/dghs2 qdQQBeo3ExWRYLhbEe9dcIgYxqYqUMqdxgUdWu+1HrkjMgCvdDcJzpfQjKX8xS5BsCEl LRPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition:mime-version :message-id:subject:cc:to:from:date:dkim-signature :arc-authentication-results; bh=KvC0QGfrDqZNFAvFJjIfUGY5gMM9T8/4OATQtnVHJ/U=; b=y3CYAFW38vy8YtteVSMX+OGCjL0ynHRwFUM+RpvOM+tF/zXZp2Mo2u5z9Z5S4/vMNj iQAxK3AhWUMZIcQ0TG+X5/KjMlvKkVWZQvA38LCFvtVlsIePoaQSCzaig/TuxCDoSx1j AbLJZsh3KElW/j+L27V6Kbn8foD391DmNjkNRkoZnRVNQcSlEGV4+sGGn82QmDNCZMZM pHrgJ2oKSdkRFn1cOx5YOW52eyAHWl9CLKMuY1MNRLMr6Vjw5gykyShYKZ6ncgNYCHaL a8XgLmBxcyc7b70qXrasAMe5oevXftzfqbzgerqsQnTNADv2MhgSuv5/IIbG63Lh0FsD EjDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=IoR5D0is; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z21-v6si8514640plo.465.2018.05.02.16.58.27; Wed, 02 May 2018 16:59:11 -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=@chromium.org header.s=google header.b=IoR5D0is; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751847AbeEBX6P (ORCPT + 99 others); Wed, 2 May 2018 19:58:15 -0400 Received: from mail-pf0-f194.google.com ([209.85.192.194]:44017 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751698AbeEBX6N (ORCPT ); Wed, 2 May 2018 19:58:13 -0400 Received: by mail-pf0-f194.google.com with SMTP id b26so378390pfi.10 for ; Wed, 02 May 2018 16:58:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:mime-version:content-disposition; bh=KvC0QGfrDqZNFAvFJjIfUGY5gMM9T8/4OATQtnVHJ/U=; b=IoR5D0is+2eNJIOVz3vhBMsCO0DJq/iq5kXT+Skg8gONgtfufa7SmqDMqELl0AcgjP nw095diUNK/NgmEIRSS+zhWgRTGiBKbWxqBpcdDPE8zjFP31XrmB1D5qUq6XyYRCmAdb 1y1A67a5rFtOIPOeEejZfN7LZbS4jdOqK9Lhk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=KvC0QGfrDqZNFAvFJjIfUGY5gMM9T8/4OATQtnVHJ/U=; b=nSX/KmJd0jKZTdBUCjxuTIsPGPBobu7ea2YRVLCjMX3O9iW960COT2XYB+zyiHRZg0 U/wZTZHsT/2uEwGDaLpM9nTfPPQQ5vZekD2siMVuQuIn0VJKUlkd2GgbEAWGe6jBN6iT fnRfINkoOlyfDEkSsJD4ehU6GeVl8BukioHhNGu1txq+LEc9T+S3YPSm9dj+DAYHY+ol XEfAH8NNwuPh5X55OIKuDfB7fodrAZcpveZFO/RMsqqlO+zodjhc7Je0RAYPR0CbPFFv Gsi5SpQZFSThTo5QSS3s5EHXBB/de8yts1r2d9fjSb1plCr73GqoFL6KUDEjissJkq5l xlrw== X-Gm-Message-State: ALQs6tBJHUy4sxiknFwPzDzmWNpEKhprQEpw4QWUb76oyA9kK/1oOdKY 0CUM6itZpKIzMIFwfd2AS02yKw== X-Received: by 2002:a17:902:264:: with SMTP id 91-v6mr21722913plc.341.1525305492650; Wed, 02 May 2018 16:58:12 -0700 (PDT) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id c20sm6027940pfk.63.2018.05.02.16.58.10 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 02 May 2018 16:58:10 -0700 (PDT) Date: Wed, 2 May 2018 16:58:09 -0700 From: Kees Cook To: "Martin K. Petersen" Cc: Vinayak Holikatti , "James E.J. Bottomley" , linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] scsi: ufs: ufshcd: Remove VLA usage Message-ID: <20180502235809.GA13998@beast> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 -- Kees Cook Pixel Security