Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp811779imu; Tue, 11 Dec 2018 07:58:49 -0800 (PST) X-Google-Smtp-Source: AFSGD/VMmvstDgc53HN7ZsqG5y59t2FPbi87aRJEMsXpw8AkHSEtfzcSGtJfZI0MMsfMIDz1p7Tx X-Received: by 2002:a63:5f95:: with SMTP id t143mr15286589pgb.395.1544543929781; Tue, 11 Dec 2018 07:58:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544543929; cv=none; d=google.com; s=arc-20160816; b=yyxK8S9pmSErsBxqhjXYjSlLuoTsB6hDtxKfi7LJSs8ie62lmYJJf/1dVotyWIXI5g ZmBeC+iGGJDAfI7WRsdCPsx1I4oHlUIqO5re8z/uIpXAp1RRID3CXKSZBZqGm4S1f0xs rs1xUBfe3U4PrjCWRospl5CeFbmIS5hqMW7ruLV73LnjohZhU+0rxsRSXXkKIdgaYi9g UDUACaCkQjciQk5QnoGw32id2d8cca44D5EwOu8U+c24bhHCJZd9lldrWxKzM8bEIIXl +/wP9igS8s4TDG/55ljamgI1x1g3fds2c5jUNliN/Ha2db8+skq8CPk3BotQtTY9k8hn nuCg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=j6Q40BhQE63XS0H7eXTx65aGmpoEChGsyw7yIyXd7Ac=; b=o1BPFAtI0wjVbDr1xwYcqZ6jXJi5Vuxhfmdm+mPc5mkXj79ClD/BUjlGCkN4s8dJ13 Sml6+L80urWfA0BGOBeHLmM5+UhTBV3MGWPKGBs9ZPXJgdruLkx1fx2cOb1kQCtCZaGB 2fYnULT9wrBYYCiQitojKaoQhCBjqzzCZ3xJx68pna1V0q23HJKahjuRdIxda3SM53/r hMmdqNAOu2akI6fjkVGW+r39u4RjtVlxiaVl5nbmNGO4W2ontu1BwETLayiLG0iYAKj7 nIfhocUcp7d1Hpowb65BCfmVHk0GhlSMvzQXOZ0XIHgclExwr403JwxPk3zFi0CXowwO ZAbg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="1j/OZcVp"; 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 31si13343267plh.274.2018.12.11.07.58.34; Tue, 11 Dec 2018 07:58:49 -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="1j/OZcVp"; 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 S1730703AbeLKP47 (ORCPT + 99 others); Tue, 11 Dec 2018 10:56:59 -0500 Received: from mail.kernel.org ([198.145.29.99]:45760 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730686AbeLKP4y (ORCPT ); Tue, 11 Dec 2018 10:56:54 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A067F2146E; Tue, 11 Dec 2018 15:56:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1544543814; bh=OMHJTWGVK3H1/PnOllSVHvGsCb/SL7iolFwjaD25umU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1j/OZcVpY+ykqZJN3Py5bVRV2ka/G3v4m3BjOQNIXzI1//CC0fxYWYugvDhovkSI8 rGm2MM72AFtL879YNreODEhd2ukosNgmbfS+ju0fmFjwVAis3qavyqliXfp8xlJfOu VwG9pjaAfQw6g3MdbduTWAHr1c94RW7RQoPLACHk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, David Herrmann , Jiri Kosina , Sasha Levin Subject: [PATCH 4.19 039/118] Revert "HID: uhid: use strlcpy() instead of strncpy()" Date: Tue, 11 Dec 2018 16:40:58 +0100 Message-Id: <20181211151645.814443453@linuxfoundation.org> X-Mailer: git-send-email 2.20.0 In-Reply-To: <20181211151644.216668863@linuxfoundation.org> References: <20181211151644.216668863@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ [ 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.19.1