Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp426156pxj; Fri, 7 May 2021 11:39:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzGOAQfftPEbkM7At8eggdBN/zDOTVnRrQ+rRGLKUZs4KD+FOTRGY3XZ9L/IA+zGSKsbAx+ X-Received: by 2002:a17:906:6b8d:: with SMTP id l13mr11341861ejr.169.1620412776424; Fri, 07 May 2021 11:39:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620412776; cv=none; d=google.com; s=arc-20160816; b=DmbAp6uBvGdw4sW7JXHbiV2Ireh7U9chnGxoY+AyBQh2BxmrklUcXRSR4h1GPEbetG O78V19xbB7BvsTD2jxvArAdR56mG0DN2GxWXTjk3ibhVTCANHfvPzkX9/fYdimGHencl HOkHEny3UwOGjfwM0ej3p+pEtSvXhIjgQ1mOPiw6SfBxksYvxfDAnT3Bjga+2bPL4jCr PucqYUh2awpuWWONL9MH//DgJ30TmlV9WidYdRLxOmYgDLlRMd19P4m6jqp0FwOyrjgH p0TH6FSWRRp2ya8xNP1AOOorvIpLiati/JJBKOHSuMPA2ru2qZiP2ztjqO6yuXFKqE6s EjXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=1k6eq9jPcs1/rUuIMW2P4M5PT4z4+pXOzj/IObfRD18=; b=biH89a2yT1rV8G9LO3Iikt5qSRgZjQdWNZi6shdBhbok64gZbWLcp9t/8kobnUbmWq mKs3aMFZlP6D4DA56PVGfEB6IjToN/WPq03ncsSadJ78+DiE9ccGFChRHlZiK5Zf8uHB hWRM0vDUpG+kg2E1f97R9/HJzU3UNMVHR6sftQfe7flzgoc0gl23QR2LlagSkyy08OsI xP3vJKMAdt46aO3e4Ad/E+IbbPjgrhkvirB4dr3AKiSC7ia98kbhZfbbiNRAutr5lpJ0 7eR93EEjdoeMQuo0RWbRCIxD3ufcS8+zDG9COJCoZIQVZHJNonlEf/59kCFvHq0Bv5TT bfRw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=X3O0Wfk2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id qc25si5953411ejb.326.2021.05.07.11.39.09; Fri, 07 May 2021 11:39:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=X3O0Wfk2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229560AbhEGSjF (ORCPT + 99 others); Fri, 7 May 2021 14:39:05 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:54787 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229488AbhEGSjF (ORCPT ); Fri, 7 May 2021 14:39:05 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620412684; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=1k6eq9jPcs1/rUuIMW2P4M5PT4z4+pXOzj/IObfRD18=; b=X3O0Wfk28wLvIKc9BGq2UPFq0CBB9NYaiD92sot5OUY6ja1kGW0BvJbgky2fhpz7qcmDHr OvmIrV9DSGl0O0sR2CIj/p/cSDrhXVzVrl6HTHYrSBClD8oBnNeO0gmL8znTOpK6vfBsz2 +M8B0Za8/gHkF/roI/vqDuKjG1z2Em0= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-87-uED50m3CMNSzIi8iQ50zRw-1; Fri, 07 May 2021 14:38:02 -0400 X-MC-Unique: uED50m3CMNSzIi8iQ50zRw-1 Received: by mail-qt1-f200.google.com with SMTP id i12-20020ac860cc0000b02901cb6d022744so6307659qtm.20 for ; Fri, 07 May 2021 11:38:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=1k6eq9jPcs1/rUuIMW2P4M5PT4z4+pXOzj/IObfRD18=; b=ru85o2h7w/S1jQMArooo2Oh43Vx80iSiv1VynECxcSz8+aos4eTGgFsw0qmVlDCp/0 AwUPfz6tXkBMTMEUNmKBbipVoGxR/LQgCYMwspNk5BHZ2LnN4T+VnfPVF+G5RmkvQss3 ChtlkoTDiIhY4HJdKmkiMt/MrqHdh6LlMtX/e+3v7taUVX4bsMndBy/7vcmE/6MjLrSv 7blwPZSlWlW+hHSX5y0T2RhGyls5LIw/7ATpn5YOchfT6Qkp133XwdIvJouBWCw3254n JNyhcqt7DWtfNkxol1cOr5KEdZGBbbJvMo39T1yXAIenIiFoXiUerwGDHmhCtJRh1iXA S1gA== X-Gm-Message-State: AOAM531s6EibeCEVdkX0STlNJiqeIptqW65ND+LFvL+L5KmD8AlHtRtR fDpdiGGGs6LlOB/Rfmu9erT7N/Uaj66OkBAO10b9zkF+Wg3D8RSvRF5/WgXu3SrMk+3Qki+qmhq QCggj+sZE79cCN8ipz+w08oLb X-Received: by 2002:a37:4017:: with SMTP id n23mr10328950qka.338.1620412682483; Fri, 07 May 2021 11:38:02 -0700 (PDT) X-Received: by 2002:a37:4017:: with SMTP id n23mr10328937qka.338.1620412682253; Fri, 07 May 2021 11:38:02 -0700 (PDT) Received: from localhost.localdomain.com (075-142-250-213.res.spectrum.com. [75.142.250.213]) by smtp.gmail.com with ESMTPSA id s21sm1048679qks.114.2021.05.07.11.38.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 May 2021 11:38:01 -0700 (PDT) From: trix@redhat.com To: michael.zaidman@gmail.com, jikos@kernel.org, benjamin.tissoires@redhat.com Cc: linux-i2c@vger.kernel.org, linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, Tom Rix Subject: [PATCH] HID: ft260: improve error handling of ft260_hid_feature_report_get() Date: Fri, 7 May 2021 11:37:57 -0700 Message-Id: <20210507183757.68810-1-trix@redhat.com> X-Mailer: git-send-email 2.26.3 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Tom Rix Static analysis reports this representative problem hid-ft260.c:787:9: warning: 4th function call argument is an uninitialized value return scnprintf(buf, PAGE_SIZE, "%hi\n", *field); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Uses of ft260_hid_feature_report_get() check if the return size matches the requested size. But the function can also fail with at least -ENOMEM. Add the < 0 checks. In ft260_hid_feature_report_get(), do not do the memcpy to the caller's buffer if there is an error. Fixes: 6a82582d9fa4 ("HID: ft260: add usb hid to i2c host bridge driver") Signed-off-by: Tom Rix --- drivers/hid/hid-ft260.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/drivers/hid/hid-ft260.c b/drivers/hid/hid-ft260.c index 7a9ba984a75a..628fa664a10b 100644 --- a/drivers/hid/hid-ft260.c +++ b/drivers/hid/hid-ft260.c @@ -249,7 +249,8 @@ static int ft260_hid_feature_report_get(struct hid_device *hdev, ret = hid_hw_raw_request(hdev, report_id, buf, len, HID_FEATURE_REPORT, HID_REQ_GET_REPORT); - memcpy(data, buf, len); + if (ret == len) + memcpy(data, buf, len); kfree(buf); return ret; } @@ -295,12 +296,16 @@ static int ft260_xfer_status(struct ft260_device *dev) struct hid_device *hdev = dev->hdev; struct ft260_get_i2c_status_report report; int ret; + int len = sizeof(report); ret = ft260_hid_feature_report_get(hdev, FT260_I2C_STATUS, - (u8 *)&report, sizeof(report)); - if (ret < 0) { + (u8 *)&report, len); + if (ret != len) { hid_err(hdev, "failed to retrieve status: %d\n", ret); - return ret; + if (ret >= 0) + return -EIO; + else + return ret; } dev->clock = le16_to_cpu(report.clock); @@ -728,6 +733,8 @@ static int ft260_get_system_config(struct hid_device *hdev, hid_err(hdev, "failed to retrieve system status\n"); if (ret >= 0) return -EIO; + else + return ret; } return 0; } @@ -782,6 +789,8 @@ static int ft260_byte_show(struct hid_device *hdev, int id, u8 *cfg, int len, ret = ft260_hid_feature_report_get(hdev, id, cfg, len); if (ret != len && ret >= 0) return -EIO; + else if (ret < 0) + return ret; return scnprintf(buf, PAGE_SIZE, "%hi\n", *field); } @@ -794,6 +803,8 @@ static int ft260_word_show(struct hid_device *hdev, int id, u8 *cfg, int len, ret = ft260_hid_feature_report_get(hdev, id, cfg, len); if (ret != len && ret >= 0) return -EIO; + else if (ret < 0) + return ret; return scnprintf(buf, PAGE_SIZE, "%hi\n", le16_to_cpu(*field)); } -- 2.26.3