Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp2455550rdb; Thu, 21 Sep 2023 21:31:27 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHjm4bDDRl1CpkrPPhHFGz+k7/COLkdpx8KpO6BXIa/H1xcxHmEIIctJIanJR8pE4Zpu6te X-Received: by 2002:a17:902:74c5:b0:1bd:ca80:6fe6 with SMTP id f5-20020a17090274c500b001bdca806fe6mr5771535plt.41.1695357087041; Thu, 21 Sep 2023 21:31:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695357087; cv=none; d=google.com; s=arc-20160816; b=mSaFKU3XjJYOQgoorFWZC1XYPz8dkARkW2bgtUcqkqUy8KUWB6bm0YfxFN8wmzAA0i O2rjey+5gD5mwbOprsbGtazy/4VqUE+EvCKkoPLvpYGmp3fieED8Q7ttK0NGk89YVMGt IYfbDrqnvIyL1p3Lqkfu6GgqR3Qy5tHw8+/SNP8wEczjNcGjqn9sM7tF4RaJDXohI7P4 Iks7PZ/A//xk4w8RYbC2uf1AUl1shXN89KumpBmK07sDwfS97ySs8pnDz/ctRMIu+6XJ iExo8P5Bf0IcfSd5y05k/tjg22ZJVyk3UpgB4h0yii62V5yFOLJWZBPcNUj6P7wWWMVy Cfgw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :dkim-signature; bh=LPoF8K9D4p8OcXev9tyojzk8WlpW/tXHjjMiYBVvNg8=; fh=i5wiYH9uiPcRM1JvXG/Uh5dy1aaUVr2nNzky90yTiPw=; b=ftjTRiNto0FArpQlGWWR0dcEUEWCI449FT+mkHXyhxkH4ByXZOcz0yrcisMXzz7Tg8 ZCIfU6BViPZqDcBMQ3OoKdGhygmZ6DVUVUfdQ9s3UL8403ZDI/LVnlwEVxflg1k43Hs2 FP2/cHZDN6Hbqmbcayr/NmMs3W01Fq4dy4CLc+XCt24Gpb0qN/Wtmw+CgzBJe81YwJwd odDQsbkWKpoxu4RDRE4J2ZHN/ePWl347j9wAoCrmkYu2aPV//3XqA3c2rl654k9Wzdlg WEa8AvnCbT3QVvUNPKJuqads1Gp4Z+Yo2DA4/nI5bK4i/qnDqrPpzhc57dpZ5+YE+/OA stUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=4cjR3J3r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id d2-20020a170902f14200b001c3ce0922ecsi2623084plb.627.2023.09.21.21.31.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Sep 2023 21:31:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=4cjR3J3r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 0773A80DCA55; Thu, 21 Sep 2023 13:11:50 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231130AbjIUULk (ORCPT + 99 others); Thu, 21 Sep 2023 16:11:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231146AbjIUUL1 (ORCPT ); Thu, 21 Sep 2023 16:11:27 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C78791B9F for ; Thu, 21 Sep 2023 12:00:46 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id 98e67ed59e1d1-274d62ab09bso1113179a91.2 for ; Thu, 21 Sep 2023 12:00:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695322846; x=1695927646; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=LPoF8K9D4p8OcXev9tyojzk8WlpW/tXHjjMiYBVvNg8=; b=4cjR3J3rA5GvlgyawvRc6MduAcCuz1YKdBACdk61Be99jvNBl0BnLRxn+mzKW5yH0R GIP1e9rRquMvRbb+UGnqwsBjgxUee9keCxMVNWsnLfBYjpolD7aJqQtGra1FITTb/TRp DqNnbNOn753KZ5faGU9cF29BNlb9smdwHpKoZiywyd/Pj0x4WlNTs6bXPyH6A9bCq2Yl u1LPaocnOWM0ECYwmpXZs9gTb1z3Hazur+f+Xg+xe91/iGGjo55o54UE+p+mEMxY5yND nUbA7HAr1s+LRhYl/gZwH7UjW68bq9zC66+ohU7lxBufSyUIkLZsojnEiMN0xcEzpzY5 331g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695322846; x=1695927646; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=LPoF8K9D4p8OcXev9tyojzk8WlpW/tXHjjMiYBVvNg8=; b=OVyKoKN3oKj1jrpFlOXosvBHpnbLopavW+WTvECcykE1u1HFVelIyl4nVAYspiTH8v EInnGIsnphel6kK4qqwMb8AzUyB+EICXlNw3sYTPBco4AJPNOwVgdBFG19J0zWyre0ed lcPNnRSjHMqR8Xr1OBU/LVngBbFL27Cz5CDN2Btk74Oo+jBhCsxDTZDvOsDATwGozBgS YNdtCa2Hhy0Btbthwtl+NbgQS+qu+nBWvA/WhKSFoyXd7oycITxvbSvZRCym0eYzZJNi I0BFKWUQYEBgcT0uP4GGfdELf4/53RuQwSFV5qh1SiCH03+hH4yCUTVYHiupH2SgF7CI evmQ== X-Gm-Message-State: AOJu0YxncYJR+NYwfNTN/dZ9ZDkmrF5vJb0GgyygIJ3cPBTO54BFNGW7 xetIBzgfoAUGnphey2ezcEyk0K5Z8gVusl5Ugw== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a25:8ca:0:b0:d13:856b:c10a with SMTP id 193-20020a2508ca000000b00d13856bc10amr65938ybi.3.1695287846713; Thu, 21 Sep 2023 02:17:26 -0700 (PDT) Date: Thu, 21 Sep 2023 09:17:25 +0000 Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIACQKDGUC/x3NQQrCQAxA0auUrA1M04LWq4iLdiZqEMeQ1DJSe vcOXb7N/ys4m7DDtVnBeBGXb65oTw3E15ifjJKqgQJ1YaAWfbYc9Y/JZGFzlKy/GT/iEceiFAo qvzFiF/tzuAzUT1OCWlPjh5TjdLtv2w6OsGzyeQAAAA== X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1695287845; l=2368; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=atTHwoOS3ax7JJoTHqO0Ih64Objj21oOjS1ymiaTpOM=; b=CzS67ZEeOOLd2kKtKJHGJgC5Pd6bB/JpQ7ZFhdM3DO2Y3bTZopyE9hqqeykaMwcB9uPwxcM/Q PEwK3UJgOzGA86AZCv6t/FGU40dAcrqJp4OWV8hS0LdNF4kYJ6BqAQZ X-Mailer: b4 0.12.3 Message-ID: <20230921-strncpy-drivers-input-misc-axp20x-pek-c-v1-1-f7f6f4a5cf81@google.com> Subject: [PATCH] Input: axp20x-pek - refactor deprecated strncpy From: Justin Stitt To: Dmitry Torokhov , Chen-Yu Tsai Cc: linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Justin Stitt Content-Type: text/plain; charset="utf-8" X-Spam-Status: No, score=-7.2 required=5.0 tests=DATE_IN_PAST_06_12, DKIMWL_WL_MED,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.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 (agentk.vger.email [0.0.0.0]); Thu, 21 Sep 2023 13:11:50 -0700 (PDT) `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); --- base-commit: 2cf0f715623872823a72e451243bbf555d10d032 change-id: 20230921-strncpy-drivers-input-misc-axp20x-pek-c-3c4708924bbd Best regards, -- Justin Stitt