Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp614568rdh; Sun, 24 Sep 2023 04:50:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFXAtEFFzi7AtZ8fLJMuAvnR8NBd9S3EVzS5kP0BDOcjRGVyMe+To05IdaalF4XmFXsE31B X-Received: by 2002:a05:6808:198f:b0:3a4:2204:e9e6 with SMTP id bj15-20020a056808198f00b003a42204e9e6mr6539961oib.21.1695556216963; Sun, 24 Sep 2023 04:50:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695556216; cv=none; d=google.com; s=arc-20160816; b=TBv0Gn9IR23cf7IlAMXBSv/Dq/0tesN6/CpB8h0f0R0Bu6NizVHlGahdz/G0/Ypk/l OMzeTNv57kEBbjqMu1X8I+nxqu+x1KAgfHcOhWUJerefFDG6q2DZCxN/daPubGPIeYM3 m9DqqQkodkncoxha4ZWyQGdAUrjAeR/wP/jrjGH9yLrtRbxtgARniOF0xzaoiNO6eaiF Z4QQnN39ZchBIDy02C1qhdGhupCFnPk2NJCThur+LAhKi21ZZvDRYL16/zoXkH5gPfy0 SGixqXQppc3MYSLhRkjWZVasYdGELn9I1kAiyuELY8MpfC+PG3R/rJZZ5GyCVP7FdSnr XffA== 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=0sacnJ2cxsyUdNljEIHRMc8mGWJME3BUwqYb875zUKc=; fh=/cjFuIMt1w2hHtR9RgWywlcI+H71ayCunRt6UXbe3qA=; b=Vtj1b6bLPDf0h5OOrMS99r+9Bye7q6edZZdnhRVx0jZLgOmM0bBSYJoFoP+LtOc9yY lyx51KNCdcGcfuska65rJgQNVE3wb7C9i87cwhYXq3NC72gJjE0QUAgaK1su0G6cPOHv Y3XaS3Ntyz2krd+8gCYMS/fh47XEvD1umyuvNIZiSYgnRhwhj2+cXaIRGArnc/iM//QZ CX7JmmYWcLQrgs/VV5gLwjl77H+5QL8KrUrxLA0g2EqJGHpZ8G+n9aWKBKqQW2mpeH3c bDPT7MwryXlfwwjysJlsuyLNiY0OTpGAJijl4hA8Cm+AkH0OsSQYst/Sy09QDy7uoXpW saqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=bNPrIkdh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id z5-20020aa78885000000b0068e45c6ca3fsi8108376pfe.93.2023.09.24.04.50.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Sep 2023 04:50:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=bNPrIkdh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (Postfix) with ESMTP id EADAA80FC14A; Sat, 23 Sep 2023 20:29:50 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229806AbjIXD33 (ORCPT + 99 others); Sat, 23 Sep 2023 23:29:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229437AbjIXD31 (ORCPT ); Sat, 23 Sep 2023 23:29:27 -0400 Received: from mail-yw1-x1136.google.com (mail-yw1-x1136.google.com [IPv6:2607:f8b0:4864:20::1136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD83B124 for ; Sat, 23 Sep 2023 20:29:20 -0700 (PDT) Received: by mail-yw1-x1136.google.com with SMTP id 00721157ae682-59bebd5bdadso52665187b3.0 for ; Sat, 23 Sep 2023 20:29:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1695526160; x=1696130960; 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=0sacnJ2cxsyUdNljEIHRMc8mGWJME3BUwqYb875zUKc=; b=bNPrIkdhXQsPfHHBYGX/l+QLdpOgRh0rElNTjuHlRuPh9+25cPvcdVFJ3GxVoZ2A6S JcPtP06Sz1Tiw/PPvXzrciZ7TQNK2V7d5YRcbrtBj8OHJVp8SprwrOMQ13JLE75KWQcG 8xV6fsM0+mJbQMOuqS/bhspmE8cbq5/pzgGuM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695526160; x=1696130960; 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=0sacnJ2cxsyUdNljEIHRMc8mGWJME3BUwqYb875zUKc=; b=pJImfGZ0s78Dt/XnGWRlwoq29PqvkzZA4q0toYEIrfdKJmrB5XJ1ptlZTlKfV44R11 MZgA9hXVMpl9ifcED3uQzBobwr6XhnSE1W4nKOoMCdQ1Is7WDJbg5qBwe0MV1gVbfp1h JNry5eGlcSABQVZJwdWZTWDVB5HpTxEPV29t31/1YFhn6q9QVvow+xB40QWDV3iOlbwZ 8tkZV117bzUni+z9+OV8gf1C+OsZh5U4GdLz07OQ4lY23TOlLGQzXBL50tZseDikXODA ueSD0LkNILJdIgowlLvpZWHvxTFNEWTuSWfEnFdeuyomp2qFBYamrTuhaygYCzh5fRMF xh1A== X-Gm-Message-State: AOJu0YwTn2M7EV6K4CV1AUmg5FGt4H4UdtBt495gQ0Q2ptGr+ir6l6c3 4DeDLc0nVjFepsuY6eM1gHjXww== X-Received: by 2002:a0d:f685:0:b0:56d:9e2:7d9e with SMTP id g127-20020a0df685000000b0056d09e27d9emr2932368ywf.21.1695526159922; Sat, 23 Sep 2023 20:29:19 -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 hi11-20020a17090b30cb00b00277371fd346sm1120927pjb.30.2023.09.23.20.29.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Sep 2023 20:29:19 -0700 (PDT) Date: Sat, 23 Sep 2023 20:29:18 -0700 From: Kees Cook To: Justin Stitt Cc: Dmitry Torokhov , Chen-Yu Tsai , linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH] Input: axp20x-pek - refactor deprecated strncpy Message-ID: <202309232024.9A31A291@keescook> References: <20230921-strncpy-drivers-input-misc-axp20x-pek-c-v1-1-f7f6f4a5cf81@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230921-strncpy-drivers-input-misc-axp20x-pek-c-v1-1-f7f6f4a5cf81@google.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 fry.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 (fry.vger.email [0.0.0.0]); Sat, 23 Sep 2023 20:29:51 -0700 (PDT) On Thu, Sep 21, 2023 at 09:17:25AM +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. > > Ensuring we have a trailing NUL-byte and checking the length of bytes > copied are both intrinsic behavior of strscpy. > > Therefore, a suitable replacement is `strscpy` [2] due to the fact that > it guarantees NUL-termination on the destination buffer without > unnecessarily NUL-padding. > > It should be noted that the original code can silently truncate and so > can this refactoring. However, a check could be added if truncation > is an issue: > | len = strscpy(val_str, buf, sizeof(val_str)); > | if (len < 0) { // add this > | return -E2BIG; // or -EINVAL > | } > > Also, now check for `len > 0` instead of just a truthy `len` because > `len` is now a signed type and we could run into problems if strscpy > returned -E2BIG which would pass the truthy test. > > 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 > Signed-off-by: Justin Stitt > --- > Note: build-tested only. > --- > drivers/input/misc/axp20x-pek.c | 8 +++----- > 1 file changed, 3 insertions(+), 5 deletions(-) > > diff --git a/drivers/input/misc/axp20x-pek.c b/drivers/input/misc/axp20x-pek.c > index 4581606a28d6..abcf78785b45 100644 > --- a/drivers/input/misc/axp20x-pek.c > +++ b/drivers/input/misc/axp20x-pek.c > @@ -134,16 +134,14 @@ static ssize_t axp20x_store_attr(struct device *dev, > { > struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev); > char val_str[20]; > - size_t len; > + ssize_t len; > int ret, i; > unsigned int val, idx = 0; > unsigned int best_err = UINT_MAX; > > - val_str[sizeof(val_str) - 1] = '\0'; > - strncpy(val_str, buf, sizeof(val_str) - 1); > - len = strlen(val_str); > + len = strscpy(val_str, buf, sizeof(val_str)); > > - if (len && val_str[len - 1] == '\n') > + if (len > 0 && val_str[len - 1] == '\n') > val_str[len - 1] = '\0'; > > ret = kstrtouint(val_str, 10, &val); This code is doing a LOT of work before handing it off to kstrtouint(), and none of it is needed. val_str is never used again, and the work is to make sure the newline is dropped -- but kstrtouint() does this already. I think this can just be: diff --git a/drivers/input/misc/axp20x-pek.c b/drivers/input/misc/axp20x-pek.c index 4581606a28d6..b1389a4c7702 100644 --- a/drivers/input/misc/axp20x-pek.c +++ b/drivers/input/misc/axp20x-pek.c @@ -134,19 +134,11 @@ static ssize_t axp20x_store_attr(struct device *dev, { struct axp20x_pek *axp20x_pek = dev_get_drvdata(dev); char val_str[20]; - size_t len; int ret, i; unsigned int val, idx = 0; unsigned int best_err = UINT_MAX; - val_str[sizeof(val_str) - 1] = '\0'; - strncpy(val_str, buf, sizeof(val_str) - 1); - len = strlen(val_str); - - if (len && val_str[len - 1] == '\n') - val_str[len - 1] = '\0'; - - ret = kstrtouint(val_str, 10, &val); + ret = kstrtouint(buf, 10, &val); if (ret) return ret; And, [broken record] for v2, please update the Subject to better describe the resulting change. :) -Kees -- Kees Cook