Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp6838103imu; Wed, 14 Nov 2018 07:42:47 -0800 (PST) X-Google-Smtp-Source: AJdET5ed7FxJOUN58PkPzr9sM7Qb1Ea0pGjRBlfp4mgdn6bYE/mGJmYpIFbMWSI57Le8GpQqWxVY X-Received: by 2002:a63:164d:: with SMTP id 13-v6mr2216013pgw.103.1542210167236; Wed, 14 Nov 2018 07:42:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542210167; cv=none; d=google.com; s=arc-20160816; b=eOAP7iyPNCQ6RI6cC203yFfxUk5/9k1ARoRIWHTzXaxTb/MvFAu+oRTuv8MKsevDw+ y8AMaJiNsXsO/RzaY3WXYkWA2yn1P39UbY01HtoS5Sz/Y068TO6NuXBNVNaCnaHul+7E PsXcBIPpIQDj0ZbRXNtPcBq5JTCUEvNwY0XjvxaoGOuspo97pUB6q3DsFh7jZg+h0xo8 kvVA1bOiArJH1903tjI5pAn3eFHa7GW0DpieFm6Le+HZoP8X0DkybzeMM4nmNVHssNNH kGb4vI2AAzsDbsB2HRpORFdfWueq1xuXAF7TKgkJDUj9+31YSx2Fv8HwsyS1Lvqa2IHx dPIQ== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=aMOqUwqEcY+BbL/wGADWN2ro23WXGClZxKZcWgBhzjY=; b=R00eRtvmIttvufSRwiySoqz+HbKqLp3OMO7jJzHq6vPb7Go87cjqNvZblHRre8QqwW h3mW31NYUvnVxzPxoviG0sEz6tSzo6+76XDbHZV7hhdHBFM4opyNbmCkKC4/LxRdNsNj eKT/5HbKudzdtyzHZriOmXCzkFLt7qqf5Z5LnfvY7q8Ckfd0rgAnrHLVktVf5RRNSgJ1 Sr/d/R+/VHFCP628Vg5OYftc3h7xhSzcKb7yhU6zkocZIJzzT5xGqgXA8JY0dwWNZNb/ TqUk1lE7wQaNpxlmTvGwQGv4RyedHWG4Ts+fGtbx3AZQS5H7ULfN2rgLk7bgctoGv5Lf N1OA== 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l4-v6si26916007plb.258.2018.11.14.07.42.31; Wed, 14 Nov 2018 07:42:47 -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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733233AbeKOBoa (ORCPT + 99 others); Wed, 14 Nov 2018 20:44:30 -0500 Received: from mail-pl1-f196.google.com ([209.85.214.196]:38219 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728000AbeKOBoa (ORCPT ); Wed, 14 Nov 2018 20:44:30 -0500 Received: by mail-pl1-f196.google.com with SMTP id p4-v6so7963318plo.5 for ; Wed, 14 Nov 2018 07:40:45 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=aMOqUwqEcY+BbL/wGADWN2ro23WXGClZxKZcWgBhzjY=; b=C3CZDVUBHfeAwKHoY3XLtI+8rb8hw7rk5Ee1g0X6uoV3fsPy7fLAulasNZliqtdyqc vvf7OF3Y0yD4xyNe4emWLuh42bJ3Jhn3vhR09SsKMXoHsOaQwizqVjwe/IIZEclJ5cBr NX1BNg12Aep5HU8/OOPA1q1X/AtHvNbpyO2lq8Ax1+DqaAch8LeAsloI/wIswnomMbYN GuN2dpMzxR5fCulBoOJSnkkrKD6y7zx53ovHe7lfUJn5Ae+BYBLuHe+X3Z8e7xseOIn5 GmY/AShCqb9zUPEoxHsySsn0l/2ur7NoA38G/li2bmGXD7a9qk7uY7kHqtnIM7D5Mnm4 DLxQ== X-Gm-Message-State: AGRZ1gI9Asu6F8a/fTlbnsUVLvvUJ332q/M86PUSmiixtCEq+p2cbqPU vLK9TXb8GOEp69iI/musjA+PH4+4tBg= X-Received: by 2002:a17:902:9a91:: with SMTP id w17-v6mr2323462plp.274.1542210045527; Wed, 14 Nov 2018 07:40:45 -0800 (PST) Received: from [192.168.46.186] ([64.114.255.114]) by smtp.gmail.com with ESMTPSA id b27sm13952395pfh.113.2018.11.14.07.40.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 14 Nov 2018 07:40:44 -0800 (PST) Subject: Re: [PATCH] Revert "HID: uhid: use strlcpy() instead of strncpy()" To: David Herrmann , linux-input@vger.kernel.org Cc: linux-kernel@vger.kernel.org, jikos@kernel.org, benjamin.tissoires@redhat.com, Kees Cook References: <20181114131642.21425-1-dh.herrmann@gmail.com> From: Laura Abbott Message-ID: Date: Wed, 14 Nov 2018 07:40:43 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: <20181114131642.21425-1-dh.herrmann@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 11/14/18 5:16 AM, 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. > Can we switch to strscpy instead? This will quiet gcc and avoid the issues with strlcpy. > 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; >