Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2749970imu; Mon, 19 Nov 2018 05:36:09 -0800 (PST) X-Google-Smtp-Source: AJdET5esu+o3gQLPBsWncFeq1AFVlIp1tvI4DbEqZRXvBaVzLDENNgpXudDE3igIxjsKHmByTaDx X-Received: by 2002:a63:111c:: with SMTP id g28mr19931220pgl.85.1542634569771; Mon, 19 Nov 2018 05:36:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542634569; cv=none; d=google.com; s=arc-20160816; b=EXVcnPBZ3nZSV5teX+A3pZmExuFTAkn4yoXU0f4xVCWcCR9dt4XAN0vNCb9BZ9useq MrLmpC+WgVOJ79u7hXRIud3EC7p4yCHgdaUs2pj1xHNMNLTCJF7MHJi0bVZFSQ5sBBer MeNp2c4ppF4epke+m+IbeYVG70PL+l6UdJu82vFPm3vhc3CTqfzPDp0X29yNss9i/Rl3 4QH8gWyIV+tXQlTNfiuv7E6WuwFQFYowp8TnaqxhcKUsZiTIg5+BTfxgzJLOpi+mO0U7 F5NHgGVAppi69EEsZaFmXA4dx9zPUcFYYuIC+U9DbjKSRuzeVjd6Nw+3LgfyFVBfJ5RX xROA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date; bh=uO7Gq8mTNGy8OK81Go6HQxzTrcFgDoMk8/TCbdMK5C0=; b=KNAK4tRXMYHyjamoWUCFXcwqcXuX5601byqu68TPpVkAcfWzR85eiquDz/qnKkTDEA BPqnkYyQxFJJ0xwqrlGSzMjgkvR9+2/lNnHVtBNSmDPixDqu5pRlPXTRTzARLYsoqKUj WE6D5oAjynJNMVOtcdSPC3bjTCXzg4OWBekkgiitI46/vSjFKmT7MgGBn9YpSjSZ+hLs K8sKgQDEZxDhClSPTEAwjOm9oQmXmJqX4FA10hlWGKDall5rr0UwxgRIo/7697FiZtEv S9ILyeHRM4qD2lHcVeOYy0RtYe3tykxWsU/oyKCbaaZneRp5eJyO+N+401Qs4eERsc9B sNig== 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; dmarc=fail (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 d189si27813734pgc.393.2018.11.19.05.35.53; Mon, 19 Nov 2018 05:36:09 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728231AbeKSX4v (ORCPT + 99 others); Mon, 19 Nov 2018 18:56:51 -0500 Received: from mx2.suse.de ([195.135.220.15]:47090 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726189AbeKSX4v (ORCPT ); Mon, 19 Nov 2018 18:56:51 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 7DD53AD48; Mon, 19 Nov 2018 13:33:11 +0000 (UTC) Date: Mon, 19 Nov 2018 14:33:10 +0100 (CET) From: Jiri Kosina To: David Herrmann cc: linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, benjamin.tissoires@redhat.com Subject: Re: [PATCH] Revert "HID: uhid: use strlcpy() instead of strncpy()" In-Reply-To: <20181114131642.21425-1-dh.herrmann@gmail.com> Message-ID: References: <20181114131642.21425-1-dh.herrmann@gmail.com> User-Agent: Alpine 2.21 (LSU 202 2017-01-01) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 14 Nov 2018, David Herrmann wrote: > 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); Applied, thanks. -- Jiri Kosina SUSE Labs