Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4836876rdb; Fri, 15 Sep 2023 14:00:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF3/t0dXts+LE1w8nau3FmnKm1ziGLqZONQ3tuMufqKWH46321Ck7XldQDAdblCr2mqvlNB X-Received: by 2002:a17:90a:4f01:b0:26d:1eff:619f with SMTP id p1-20020a17090a4f0100b0026d1eff619fmr2560950pjh.2.1694811617343; Fri, 15 Sep 2023 14:00:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694811617; cv=none; d=google.com; s=arc-20160816; b=pmlv49pK7C61PsKna/1qE/P9uMO8NlEFDlZlN0ghAFTkergIbTHsB5Gv7bOtuBmoao TEiVYHyZKCNiqsiGLVm+6ZRInIueUXpdchusX6ruOpaNsc1Ig/2nwChnzvFoHLa7en0h eWjvwr4lqsDljb/C/J1m0PFCxVoWQFzYGsSQP2zN21U7DlYxoKjn7wrSYno3H/Va3u1W TDHW8tzHN7GBS4+88wXjwJ4o5Q1Wyn93hOKgFuGkA9ryFrpRuAQc01u9A5Wsxg3S8Ev/ VnrxFKB8gc4to5VxX87bCP1DxpUhfmln9R98uQXYpSIkhPDtaJPH1wP6uwH5D5Mxzjdq rlyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=p+PPJ/AQSOJ+ikTUxwaFbNmWB98quGmGcGYVc1MYqo4=; fh=nly9U8n9zcs5iYNgxG8UeWl67iOgP6KpSSHsSYIERM8=; b=MnH7N1OIj+htBH1fzrOZE5/tAPkaVxlT5rqL5cHu6ElvlBLjvw629e9IerSl5fG84A w0cexc3Mczcxx0o57fTBT1gz30fMFPoWFjcPHvEGRXz5r/nYisKSQb/w43pZfbqge3E0 toA8sMQLX6A+7FGqLJ4sb9DK3Rq44S450lZ8koe0PEDBd6yGYpsJnm9jgjGipcSRFjNp CLz86L6GHVVTdRcXNUMk/aY8MqM6g5zZCEi3vCLxYIwc2YOjx85jdYhsCTwT6qQkGBTI 5W1FjVX8w0EonawPnBO0+7iYM0uqMJi7xS+yCMw4FsROF6SmH1Yhd2DUUJHq1o4LdV6P 9fVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=jPZozsT2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id np11-20020a17090b4c4b00b0026b51323933si6319320pjb.158.2023.09.15.14.00.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 14:00:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=jPZozsT2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 4957A80984AC; Fri, 15 Sep 2023 13:49:41 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237563AbjIOUs6 (ORCPT + 99 others); Fri, 15 Sep 2023 16:48:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60500 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237674AbjIOUso (ORCPT ); Fri, 15 Sep 2023 16:48:44 -0400 Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44BE22709 for ; Fri, 15 Sep 2023 13:48:25 -0700 (PDT) Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-68fb7fb537dso2440247b3a.2 for ; Fri, 15 Sep 2023 13:48:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1694810905; x=1695415705; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=p+PPJ/AQSOJ+ikTUxwaFbNmWB98quGmGcGYVc1MYqo4=; b=jPZozsT2o/pJmTgFxnggifmMGrjbs5MuLtv7waWc89P3K+bPqQH/p7vjLcadQK+MU7 vxqjrmlBtCmHuw90Q82F0o8yao4Yn3GWTgAs2C31YgChWxjAAUiBiSoMPRoyTcy8lc6m pFCe+p5RaVU7seSH2iTU7EpjQcaH9h4zhxzr8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694810905; x=1695415705; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=p+PPJ/AQSOJ+ikTUxwaFbNmWB98quGmGcGYVc1MYqo4=; b=rf0FBonrKBol/DKKUTrfs8qnsUmxhP0VnFpGqMfiicvm/R0qiOA5ImpuuNLWcdQqpR 6sZp2sONmh0U5JC1aOt95QhrDO0M8IcpicT26cTFHRFDZ5UJjtUKptTP89aAovZiBCEe S6kcQhsfjgsJS+9PSaxsPDTVyAAVgkKQOL8Nb6hjtCj5/lwHdWiPU/2uUvdHcmSKMyEn KkvwQtbskN1b8fRJgvHCJIx0Iic7KyxgqiNFyxrtdIhVzyvZJ6bkJy3rjCqRmrp8Eqc7 5xuXx2cy4pMlGY/qGWc9zxGIUg8ZZFnqZp6Zk5t/RD5FGUjfXYi+2P6oR7IVMdogPI8C p5Ew== X-Gm-Message-State: AOJu0YzPv81EOfJow0VY1z7zWUtBE5MBnTwyoayClhESqOVxRaBY+vFK z2/yslcovLmgkT6EGEz7ImExX7jT4hL/3DYYc3c= X-Received: by 2002:a05:6a00:2d0b:b0:68f:ca4a:efd with SMTP id fa11-20020a056a002d0b00b0068fca4a0efdmr3093829pfb.9.1694810904753; Fri, 15 Sep 2023 13:48:24 -0700 (PDT) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id a6-20020aa78646000000b0069026841829sm3319396pfo.113.2023.09.15.13.48.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 13:48:24 -0700 (PDT) Date: Fri, 15 Sep 2023 13:48:23 -0700 From: Kees Cook To: David Rheinsberg Cc: Justin Stitt , Jiri Kosina , Benjamin Tissoires , linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, David Herrmann Subject: Re: [PATCH] HID: uhid: refactor deprecated strncpy Message-ID: <202309151342.DFA6CA5C7@keescook> References: <20230914-strncpy-drivers-hid-uhid-c-v1-1-18a190060d8d@google.com> <202309142206.60836CE@keescook> <98d981a1-4e4c-4173-b8eb-09b4245bca60@app.fastmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <98d981a1-4e4c-4173-b8eb-09b4245bca60@app.fastmail.com> X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Fri, 15 Sep 2023 13:49:41 -0700 (PDT) On Fri, Sep 15, 2023 at 09:36:23AM +0200, David Rheinsberg wrote: > Hi > > On Fri, Sep 15, 2023, at 7:13 AM, Kees Cook wrote: > >> - /* @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); > > > > ev->u.create2 is: > > struct uhid_create2_req { > > __u8 name[128]; > > __u8 phys[64]; > > __u8 uniq[64]; > > ... > > > > hid is: > > struct hid_device { /* device report descriptor */ > > ... > > char name[128]; /* Device name */ > > char phys[64]; /* Device physical location */ > > char uniq[64]; /* Device unique identifier (serial #) */ > > > > So these "min" calls are redundant -- it wants to copy at most 1 less so > > it can be %NUL terminated. Which is what strscpy() already does. And > > source and dest are the same size, so we can't over-read source if it > > weren't terminated (since strscpy won't overread like strlcpy). > > I *really* think we should keep the `min` calls. The compiler > should already optimize them away, as both arguments are compile-time > constants. There is no inherent reason why source and target are equal in > size. Yes, it is unlikely to change, but I don't understand why we would > want to implicitly rely on it, rather than make the compiler verify it for > us. And `struct hid_device` is very much allowed to change in the future. > > As an alternative, you can use BUILD_BUG_ON() and verify both are equal in length. If we can't depend on ev->u.create2.name/phys/uniq being %NUL-terminated, we've already done the "min" calculations, and we've already got the dest zeroed, then I suspect the thing to do is just use memcpy instead of strncpy (or strscpy). -- Kees Cook