From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A4723CD37AF for ; Fri, 15 Sep 2023 20:49:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237402AbjIOUsw (ORCPT ); Fri, 15 Sep 2023 16:48:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60516 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237679AbjIOUso (ORCPT ); Fri, 15 Sep 2023 16:48:44 -0400 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44B3F2707 for ; Fri, 15 Sep 2023 13:48:25 -0700 (PDT) Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-68fb7fb537dso2440248b3a.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=OSns6I8cGjeMjZEnxgnUjK14PcTqIJVkUyH1XrSHWhoxlAJFeolehKKWamRSM6Zf/7 lAOZc7oB9QZtRRBntXaq98TNPMravZlAg+crR/Z5zrLK502LRAwQIN59V0Ks76fDD+e+ tO7IQ/JFHMVBgS0H2rFh02eo9V1HmPP33Gl/56iOb0Rt8b+A4wJV7N7wf1cZPGh9qV2+ 4KuloABXpDTrFDaVAKiWou48o5EKlv4/+EhKb+fPy868j/35Lu70g6uZSFv55st9XGeg jy2b5bAV+ymlOW15I47j+HR542nMX2SZiLuVSmNFS49jjqsNZLQJhGBEk3T2lKS6Rdng vW5w== X-Gm-Message-State: AOJu0Yzrct1rKX1jIAW8zij3SSU6+TBnwyxWOvYFEsq/bNJl8hz2y0dr q2qxdc54iLan7vxyZXrYcHBh+A== X-Google-Smtp-Source: AGHT+IFOxKRcwk4qHKzzxE6vlTeEqFbIORzQ9ufHcf3GvQ/kLYWfVDwqBjcVYUd19W39PY4imGNNKA== 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> Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org 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