Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp3339341imm; Fri, 20 Jul 2018 14:53:43 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdeErr1tuXrHRE3z4BCdjN12GaGquDpDgK2cj+Iz9wHOgV5vonk0n3bW1xxDPjIwIsEhvDE X-Received: by 2002:a17:902:e005:: with SMTP id ca5-v6mr3636476plb.224.1532123623232; Fri, 20 Jul 2018 14:53:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532123623; cv=none; d=google.com; s=arc-20160816; b=rsLW1XM97k8G1NAys/papQKHGXFLxvh+2BfSHRE/iRwYXadWAHMu9iAbHxFoiSUFeg kBKAm/nRfSPPbNmKYV9mv/XGXqaS25sI6evpxNY1b8JtOJGj7x6PtA3VhYLLxJAIyksd 2Yi+kUdspeqwDF1PcmcLwcdFpKKM4iL0sK4D1LmHtYoBBbIbgbEksE+NUAqmgPkclPe5 BmY+pztCuAcH1NVhtlch7zkuUCotObhSi+ojDu1YSgw8a2YG/CChwSSxkiPARrdGteBt 6/EdkS7Ch2p6rdszT9RVMlU5KWUxTXJOUuiUC3Cjb8PPsgYoQn1SoYQWfAxK1pcT8YU3 qTgQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=1orVEaYmAMXGZtF48KHp+6jXiM33gAzTxh+AeshEm70=; b=ADBj+EVOWCmp+X31vsFsECKRhUkKaMig0DAGzeZvqTyIcnNOu2m+qlQovxrfamvX0B /AqwKoFdwn+OSe7NVdsFrIZJUgiRpKou2jSps28b8zPjm16kRmp0f5cskjGbNJiojyOn szv09wLMZ+9abESIDfNuOUdX8WlPEz+nS466awqM8twjnTb7LlWdNNH/CZ8+NTbmp1/F rQU3RD2iV3CUiNJJwi/TEMXdElc+BfDVuSRM3JJJkNKV08KJrMmuk+xQpCuiNSvks9Tu xVsRpDaiyHHgV59N9/P1/vSfS+AyPo3NqOS1mwvK/C6pBd7fg7oFuRl6bQMwIP4eW7bj VjSA== ARC-Authentication-Results: i=1; mx.google.com; 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 d11-v6si2610217plr.261.2018.07.20.14.53.28; Fri, 20 Jul 2018 14:53:43 -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; 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 S1731313AbeGTWmS (ORCPT + 99 others); Fri, 20 Jul 2018 18:42:18 -0400 Received: from avasout06.plus.net ([212.159.14.18]:48149 "EHLO avasout06.plus.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731108AbeGTWmS (ORCPT ); Fri, 20 Jul 2018 18:42:18 -0400 Received: from hairyalien ([80.229.148.18]) by smtp with SMTP id gdJmfIgPAWLW2gdJnf5XFd; Fri, 20 Jul 2018 22:52:08 +0100 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.3 cv=fJUXI6Se c=1 sm=1 tr=0 a=o7Djd4SkmPXITDn8qH+ssQ==:117 a=o7Djd4SkmPXITDn8qH+ssQ==:17 a=R9QF1RCXAYgA:10 a=V0UhwjhiAAAA:8 a=A3-LDzXwAAAA:8 a=A3I9UrruRtFbHBQDeOQA:9 a=PQrrThv6e6vlBFiu:21 a=xnGkizWhtxF9dybg:21 a=_UVLBGXn8Au_dgoMdIS7:22 a=LycaIqhzDZpX3FTtPRdu:22 Received: by hairyalien (sSMTP sendmail emulation); Fri, 20 Jul 2018 22:52:06 +0100 From: Nick Dyer To: Dmitry Torokhov Cc: linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, Chris Healy , Nikita Yushchenko , Lucas Stach , Nick Dyer Subject: [PATCH v1 07/10] Input: atmel_mxt_ts - zero terminate config firmware file Date: Fri, 20 Jul 2018 22:51:19 +0100 Message-Id: <20180720215122.23558-7-nick@shmanahar.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180720215122.23558-1-nick@shmanahar.org> References: <20180720215122.23558-1-nick@shmanahar.org> X-CMAE-Envelope: MS4wfGwVfyunw3eJB8zxOhF6es32ReCQKflAnn33YJopQksZGxOQEAr8jbi0Wd+5Y1DTy7c5ic2KE2QeIA6yOds6nCDzwgbMQ4qAUN9ypW9RMwS0YKVNKivg +iQB6hCenagPYpitTx5+SAbY69ZZKGA6nW8= Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Nick Dyer We use sscanf to parse the configuration file, so it's necessary to zero terminate the configuration otherwise a truncated file can cause the parser to run off into uninitialised memory. Signed-off-by: Nick Dyer --- drivers/input/touchscreen/atmel_mxt_ts.c | 36 +++++++++++++++++------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c b/drivers/input/touchscreen/atmel_mxt_ts.c index 0ce126e918f1..2d1fddafb7f9 100644 --- a/drivers/input/touchscreen/atmel_mxt_ts.c +++ b/drivers/input/touchscreen/atmel_mxt_ts.c @@ -279,7 +279,7 @@ enum mxt_suspend_mode { /* Config update context */ struct mxt_cfg { - const u8 *raw; + u8 *raw; size_t raw_size; off_t raw_pos; @@ -1451,14 +1451,21 @@ static int mxt_update_cfg(struct mxt_data *data, const struct firmware *fw) u32 info_crc, config_crc, calculated_crc; u16 crc_start = 0; - cfg.raw = fw->data; + /* Make zero terminated copy of the OBP_RAW file */ + cfg.raw = kzalloc(fw->size + 1, GFP_KERNEL); + if (!cfg.raw) + return -ENOMEM; + + memcpy(cfg.raw, fw->data, fw->size); + cfg.raw[fw->size] = '\0'; cfg.raw_size = fw->size; mxt_update_crc(data, MXT_COMMAND_REPORTALL, 1); if (strncmp(cfg.raw, MXT_CFG_MAGIC, strlen(MXT_CFG_MAGIC))) { dev_err(dev, "Unrecognised config file\n"); - return -EINVAL; + ret = -EINVAL; + goto release_raw; } cfg.raw_pos = strlen(MXT_CFG_MAGIC); @@ -1470,7 +1477,8 @@ static int mxt_update_cfg(struct mxt_data *data, const struct firmware *fw) &offset); if (ret != 1) { dev_err(dev, "Bad format\n"); - return -EINVAL; + ret = -EINVAL; + goto release_raw; } cfg.raw_pos += offset; @@ -1478,26 +1486,30 @@ static int mxt_update_cfg(struct mxt_data *data, const struct firmware *fw) if (cfg.info.family_id != data->info->family_id) { dev_err(dev, "Family ID mismatch!\n"); - return -EINVAL; + ret = -EINVAL; + goto release_raw; } if (cfg.info.variant_id != data->info->variant_id) { dev_err(dev, "Variant ID mismatch!\n"); - return -EINVAL; + ret = -EINVAL; + goto release_raw; } /* Read CRCs */ ret = sscanf(cfg.raw + cfg.raw_pos, "%x%n", &info_crc, &offset); if (ret != 1) { dev_err(dev, "Bad format: failed to parse Info CRC\n"); - return -EINVAL; + ret = -EINVAL; + goto release_raw; } cfg.raw_pos += offset; ret = sscanf(cfg.raw + cfg.raw_pos, "%x%n", &config_crc, &offset); if (ret != 1) { dev_err(dev, "Bad format: failed to parse Config CRC\n"); - return -EINVAL; + ret = -EINVAL; + goto release_raw; } cfg.raw_pos += offset; @@ -1530,8 +1542,10 @@ static int mxt_update_cfg(struct mxt_data *data, const struct firmware *fw) MXT_INFO_CHECKSUM_SIZE; cfg.mem_size = data->mem_size - cfg.start_ofs; cfg.mem = kzalloc(cfg.mem_size, GFP_KERNEL); - if (!cfg.mem) - return -ENOMEM; + if (!cfg.mem) { + ret = -ENOMEM; + goto release_raw; + } ret = mxt_prepare_cfg_mem(data, &cfg); if (ret) @@ -1570,6 +1584,8 @@ static int mxt_update_cfg(struct mxt_data *data, const struct firmware *fw) /* T7 config may have changed */ mxt_init_t7_power_cfg(data); +release_raw: + kfree(cfg.raw); release_mem: kfree(cfg.mem); return ret; -- 2.17.1