From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5E1223FB09 for ; Tue, 3 Oct 2023 23:41:54 +0000 (UTC) Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6EEA9AD for ; Tue, 3 Oct 2023 16:41:52 -0700 (PDT) Received: by mail-pj1-x102d.google.com with SMTP id 98e67ed59e1d1-27777b778bfso989877a91.3 for ; Tue, 03 Oct 2023 16:41:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1696376512; x=1696981312; 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=WhYcyR6Fe80biMSdh0fvQHW+o+cY1bEtG/iTTN+PcWg=; b=IHpUi4Ksr7Sxs+EbzIhbDtk+ViKhlKz9Km5JnR4HJs/OQ0KTliyWo//MOgxoddb8T7 6ZlU3Y6KL2eWRewgAHgcBZqEnllPNwEtbaj+u4xJv6SyKhH8YxJDAgYIgrP28fXuE0WL tJRReKVlRVi8HOoqzeZeiR4+VmGIPjVhplyEY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696376512; x=1696981312; 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=WhYcyR6Fe80biMSdh0fvQHW+o+cY1bEtG/iTTN+PcWg=; b=wbbu5NzsmSOKGM+U9WXmG6GvQaZt6bALXMxhT28ltKJrqnKNX6QL8Z5f95+M6/aqku lFxQ74k3dKGWsASuioLXzH4XDKx23djZF3I6uS/i+yUN3sXK1aAjw1FMfFXnMfasa/+8 7ABkDyGS1s1sj9uaxpp2VQXD9V58DWhaJlZNb5YypTz/GsUxwk2UTaeGNV5ISrkKPCqz Yt8wRE001xvxhylTkrUCav5HRoQEpJCDRWhAOVNTJ+AcBvwXjGtislK+nweqGoC2niik 1YIB6ykmtwZ62H0CKWQJl4KwM2Om+SGD/jos2XPMvzwYKK2Ezbkpkxfm0TUZX7ZXBdbt DjBw== X-Gm-Message-State: AOJu0YxKVodivoIfMCrDUDaHqDg8XLx/VivvUIu3Leyh972Qq69mvp/A AazltivL9AuW5z4RnG+7Wk5lGA== X-Google-Smtp-Source: AGHT+IEn0DmTvbVbBFxNdq3OKXXpFw4I9foZJK6ZxmUa8EteSnTE9RTr4xVQgT2VKg+fsVzgi2nZeA== X-Received: by 2002:a17:90a:bf13:b0:277:5652:cb00 with SMTP id c19-20020a17090abf1300b002775652cb00mr674622pjs.24.1696376511915; Tue, 03 Oct 2023 16:41:51 -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 6-20020a17090a1a0600b00263dfe9b972sm188454pjk.0.2023.10.03.16.41.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 16:41:51 -0700 (PDT) Date: Tue, 3 Oct 2023 16:41:50 -0700 From: Kees Cook To: Justin Stitt Cc: David Rheinsberg , Jiri Kosina , Benjamin Tissoires , linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH v2] HID: uhid: replace deprecated strncpy with strscpy Message-ID: <202310031641.53DE6178@keescook> References: <20231003-strncpy-drivers-hid-uhid-c-v2-1-6a501402581e@google.com> Precedence: bulk X-Mailing-List: linux-hardening@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231003-strncpy-drivers-hid-uhid-c-v2-1-6a501402581e@google.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, 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 lindbergh.monkeyblade.net On Tue, Oct 03, 2023 at 09:01:58PM +0000, Justin Stitt wrote: > `strncpy` is deprecated for use on NUL-terminated destination strings > [1] and as such we should prefer more robust and less ambiguous string > interfaces. > > A suitable replacement is `strscpy` [2] due to the fact that it > guarantees NUL-termination on the destination buffer without > unnecessarily NUL-padding. > > Furthermore, let's make sure `hid->xyz` and `ev->u.create2.xyz` are the > same size at compile time to prevent silent truncation. > > With these changes, it is abundantly clear what the intent and behavior > of the code is -- We are getting a string to string copy with > NUL-termination and no truncation. > > Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1] > Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html [2] > Link: https://github.com/KSPP/linux/issues/90 > Cc: linux-hardening@vger.kernel.org > Cc: Kees Cook > Signed-off-by: Justin Stitt Great! This looks much simpler to me. Thanks for adjusting it. Reviewed-by: Kees Cook -- Kees Cook