Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2079553imu; Wed, 28 Nov 2018 22:01:05 -0800 (PST) X-Google-Smtp-Source: AFSGD/U9nqYh3rfH7KVShyzT8yalp8xAkut0FAVoEaxVOQ8KDUuqYhTfG0eF/XsJDgntF2BVgnhh X-Received: by 2002:a62:7687:: with SMTP id r129mr179283pfc.17.1543471265494; Wed, 28 Nov 2018 22:01:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543471265; cv=none; d=google.com; s=arc-20160816; b=DjVywQiPc5B7RiC55UiPWPylNuGeSlrhZVt0mZ0IDa1KIn5lHYJb1M+qbf85H+JrgU BbVOWxaqssO4qKakLgm/ebj7s8DN+7gEhvkGH4M9avhRAR8bVBBV3AOEnmZLCGIO722O BqtvaoWafCpvnx5X5u090/R2gHef2KNDdoGyiu6sOyU4fiuq6XfjWxDEs0A+Sr8hlusZ 3BJDStH1I4JSTdsHx/UZM9XkzORnhe4SnqRJMRI+wMGla3/XsOCZLJh4OHva7BbSWcVY 9heaYObmCl9mKvGEbuBW/FveNTWZgKh3RDxopAph23mWBCb/ICZ7W7gHdQFRz5TbnY+a yEzA== 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:dkim-signature; bh=t98pWQYHymIA3SWSfQfCpOGs6YO75M1Miq8FfaWOOaQ=; b=FDkfDP9uRpUsR9bdNn38BaM6or7ivSbuhACfy35vTBqmGgokOPNlvLgbTeTcnmwabC +U3JqaPMqkdhaX7WpWc33HQwm6pWLIoOg8vFpiYwvM7m4yHXVyUy45atZVIxDcFj6PfB SB71UrsoqMN3/+J1xycrIIISD8LX22kA1nGNhGr/ImNBANTTrdb5IfeVhkkEOBtjciN+ rAzoGcm4RPk52tEGDre8qQhi6d7Woi/unb7EXBF8W26UVZeEnQ8GvF1YFZq8D/5oc26m JX0rDGyizjKiU0d0pqSGYd+RrTpEDUXU+VChREHLovQCWjZk/fqnMMcjZzfCwgHijwzK wlJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="T8Acej/v"; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n13si916213pgp.307.2018.11.28.22.00.50; Wed, 28 Nov 2018 22:01:05 -0800 (PST) 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=@kernel.org header.s=default header.b="T8Acej/v"; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728828AbeK2RDe (ORCPT + 99 others); Thu, 29 Nov 2018 12:03:34 -0500 Received: from mail.kernel.org ([198.145.29.99]:37806 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727402AbeK2RDe (ORCPT ); Thu, 29 Nov 2018 12:03:34 -0500 Received: from sasha-vm.mshome.net (unknown [37.142.5.207]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id DC95D208E7; Thu, 29 Nov 2018 05:59:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1543471163; bh=USgoQ4D9VCbfZVSzN36RiwJFaTW0Xd3TaUs21r/PocE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=T8Acej/vQ9KByqaO7/VIbLbBf7iwVu+rxQe0a3fZdPxKYrKpu8XWvAHoyi7TeqqgZ sOuPvs02PIeL4RzWcrb2VA+PC5pm3OR0e6xfu/LGq7e7jgFGVDuDNQeWh6jFHYaV28 7yqpCHNNnCzcvjH9Q6F3kKKS3a0xdmBsVMKygF+U= From: Sasha Levin To: stable@vger.kernel.org, linux-kernel@vger.kernel.org Cc: David Herrmann , Jiri Kosina , Sasha Levin , linux-input@vger.kernel.org Subject: [PATCH AUTOSEL 4.19 41/68] Revert "HID: uhid: use strlcpy() instead of strncpy()" Date: Thu, 29 Nov 2018 00:55:32 -0500 Message-Id: <20181129055559.159228-41-sashal@kernel.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181129055559.159228-1-sashal@kernel.org> References: <20181129055559.159228-1-sashal@kernel.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: David Herrmann [ Upstream commit 4d26d1d1e8065bb3326a7c06d5d4698e581443a9 ] This reverts commit 336fd4f5f25157e9e8bd50e898a1bbcd99eaea46. Please note that `strlcpy()` does *NOT* do what you think it does. strlcpy() *ALWAYS* reads the full input string, regardless of the 'length' parameter. That is, if the input is not zero-terminated, strlcpy() will *READ* beyond input boundaries. It does this, because it always returns the size it *would* copy if the target was big enough, not the truncated size it actually copied. The original code was perfectly fine. The hid device is zero-initialized and the strncpy() functions copied up to n-1 characters. The result is always zero-terminated this way. This is the third time someone tried to replace strncpy with strlcpy in this function, and gets it wrong. I now added a comment that should at least make people reconsider. Signed-off-by: David Herrmann Signed-off-by: Jiri Kosina Signed-off-by: Sasha Levin --- drivers/hid/uhid.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/drivers/hid/uhid.c b/drivers/hid/uhid.c index 051639c09f72..840634e0f1e3 100644 --- a/drivers/hid/uhid.c +++ b/drivers/hid/uhid.c @@ -497,12 +497,13 @@ static int uhid_dev_create2(struct uhid_device *uhid, goto err_free; } - len = min(sizeof(hid->name), sizeof(ev->u.create2.name)); - strlcpy(hid->name, ev->u.create2.name, len); - len = min(sizeof(hid->phys), sizeof(ev->u.create2.phys)); - strlcpy(hid->phys, ev->u.create2.phys, len); - len = min(sizeof(hid->uniq), sizeof(ev->u.create2.uniq)); - strlcpy(hid->uniq, ev->u.create2.uniq, len); + /* @hid is zero-initialized, strncpy() is correct, strlcpy() not */ + len = min(sizeof(hid->name), sizeof(ev->u.create2.name)) - 1; + strncpy(hid->name, ev->u.create2.name, len); + len = min(sizeof(hid->phys), sizeof(ev->u.create2.phys)) - 1; + strncpy(hid->phys, ev->u.create2.phys, len); + len = min(sizeof(hid->uniq), sizeof(ev->u.create2.uniq)) - 1; + strncpy(hid->uniq, ev->u.create2.uniq, len); hid->ll_driver = &uhid_hid_driver; hid->bus = ev->u.create2.bus; -- 2.17.1