Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp6668059imu; Wed, 14 Nov 2018 05:17:54 -0800 (PST) X-Google-Smtp-Source: AJdET5dpMcLypwmm1K4Lwh+ukVhbKVEepfVbizPUX5ZD6u7CR9oEDMAlelYWmq8mmJlFFeVZGVIR X-Received: by 2002:a62:8d92:: with SMTP id p18-v6mr1978641pfk.217.1542201474409; Wed, 14 Nov 2018 05:17:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542201474; cv=none; d=google.com; s=arc-20160816; b=yTH2IuvtSkWgdOoO638qML8X4nY91JhOE0VZuwatEiDGwliUkAI6V0ThUeKW9oni52 JnRdTKHzKYBPvsnXtloQGVIMR176rZeAoTJT92cCFug3f12sZnPuRtWQf8IBw4VlJw+q haJ5inXCePTP5eks3EXw/vnKmLu5mqtuAlFfk8sZTiBWkedYmwA8DqvLK21iqxLJGAi6 ES+LkAsbW1HcKdfaNyZ949UZ8gORF89jzyW3NPtymf0AvfImZYBV/mBaqQJypLXDj/WB 0A2BAo0WmcUCdyUCx/mIfYUsZdEU4DJxdPBeHRcRinog65xFZT17Ox9rgJ1bIXer5WH5 s1oQ== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=luGH8fFlOje6y8ZeQbgrxuGMpHoEwVHKefdy8/mTLqA=; b=S+u5YJBe263ECnqQpK91OmO5yctgmoli3ni16glrJikDWfftjnDWVvdLpbXZ0t38am jifBC+H7PbbS1rpWz53khB5/3NSPxUuv5/zGvUoXh4VyPv+Wf0IaV6OTyh4gIH4BHi24 BvXchPEYeJaVxZqLpj1xkkkiZzsRbLmmJjIjAsnNt80y/5eML6YPCfwt0sPh3IjIkVHX CndOByy4GCl4oNGcfZbktEO2LSKiHwJZVFyjspNleCAA400jGtCrh49Yx30R0jasl5Xh 0sXPdsks2Rc71idjTxdsqDhGRKh5OZizxEnfzSZy7veoQzPCkEdsnYjsSLceanw7RBCs 2Ihw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=H+0Oqo0g; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g12-v6si23673624plt.215.2018.11.14.05.17.38; Wed, 14 Nov 2018 05:17:54 -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=@gmail.com header.s=20161025 header.b=H+0Oqo0g; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732931AbeKNXUE (ORCPT + 99 others); Wed, 14 Nov 2018 18:20:04 -0500 Received: from mail-wr1-f66.google.com ([209.85.221.66]:35398 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727837AbeKNXUE (ORCPT ); Wed, 14 Nov 2018 18:20:04 -0500 Received: by mail-wr1-f66.google.com with SMTP id 96so351419wrb.2; Wed, 14 Nov 2018 05:16:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=luGH8fFlOje6y8ZeQbgrxuGMpHoEwVHKefdy8/mTLqA=; b=H+0Oqo0gv1dTrwzsm/iT4WN7jLKctDCxerE0TUZ83L0XDKCKTMme7hw8yRuB8lB0t0 Z37izRJBoUcgQ8Lfk+eRoUhRoADZ8NdxlyZpcd3J3DSqUnV+C93n1zvuSWB3g4gxTWdV IB7/fI/n4mAAI0lunCdIWo48f3HsyY8e9XuBDemitKsCwhKslbCqHr5D6K7+zJJ3o0HZ NIAXDCgcXiYbh1FAURuD22jKJTnCtYgQ2CW+EpdOt3f2+NK4AJR6U7A94E0c9ddZg/wS FJmfK/9HRPgDPmn15uAoA5fVHqi5cCr0ZnFW2k56UmzjinY0QqEz6IbDAXbZ75uV5nl8 A3VQ== 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=luGH8fFlOje6y8ZeQbgrxuGMpHoEwVHKefdy8/mTLqA=; b=ZeeNt9uhuYYQCK5sc73Mg6JsoES0BJXxDraJ1waEp59CBA5Zn+NY5LWAm1pY3mb+7j L+DbGo3kYpsCBkxVubY1MbW917nRP109YJM5W2MgMMuXecUcAtJwGy9kptV39jXkgrrv zqj3BUN6klBvCVX+rjDZIhGeFqSTsrR5CxtYyZvJ1ZTLEIAhfnzEvnWXCQSho3sKRp1i lXATDNI+K5dmdMz+VSFd3SR+27l/JdI6mft/4DBpW4y6Q1v5YKStexrLQppvbZdTtzdj juJLz6Ehv0jXzzH6A1FwzgdRNyze/2wipI/1NTeD+qFiFI2zmHDsFcobIDyUKXqZB2ZX e7bg== X-Gm-Message-State: AGRZ1gJJmOOPOjgMIyyOM5BA4OV2QhOoeO48XcSdsvKu1EjAaXLH9fSB A2d/pQznsphsDRtcQk3F3zF72C/v X-Received: by 2002:adf:ba8b:: with SMTP id p11-v6mr1777935wrg.203.1542201408666; Wed, 14 Nov 2018 05:16:48 -0800 (PST) Received: from david-x1.fritz.box (p200300C2A7086500829F3F8E4928CB10.dip0.t-ipconnect.de. [2003:c2:a708:6500:829f:3f8e:4928:cb10]) by smtp.gmail.com with ESMTPSA id s1sm8818931wro.9.2018.11.14.05.16.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 14 Nov 2018 05:16:47 -0800 (PST) From: David Herrmann To: linux-input@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jikos@kernel.org, benjamin.tissoires@redhat.com, David Herrmann Subject: [PATCH] Revert "HID: uhid: use strlcpy() instead of strncpy()" Date: Wed, 14 Nov 2018 14:16:42 +0100 Message-Id: <20181114131642.21425-1-dh.herrmann@gmail.com> X-Mailer: git-send-email 2.19.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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 fefedc0b4dc6..0dfdd0ac7120 100644 --- a/drivers/hid/uhid.c +++ b/drivers/hid/uhid.c @@ -496,12 +496,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