Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp1230272rdb; Fri, 20 Oct 2023 12:05:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFXI3VpU6MuQMd9de/RjXYLQB7r1bU4VBK2m18ugBBx2HtnXOPj9iL+arf0+NuWBKwnldLi X-Received: by 2002:a17:902:e5d0:b0:1c9:d236:b2b with SMTP id u16-20020a170902e5d000b001c9d2360b2bmr7696919plf.12.1697828754419; Fri, 20 Oct 2023 12:05:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697828754; cv=none; d=google.com; s=arc-20160816; b=Sx7iUx8nHCrzMYmvMdctiRV02/EP4LM5HTH5r7SNlr7DQYXVxaa/arngkTF8OggHYC tEwIxNjOLKucOwxej6rdDCgy2dcYb0a3W+WrfunZeImLVKGpnqZ+J4FtMS2LfRwi9bm9 zL5xCkkTcvNdano40y3aNwyypllzEuWOeJeSbYjReImB8Lzp5I9adx6WAvA+46kuTPF9 Jhpd0YQykHDRyWqWYhMLwzKKzTaJwRDaQzRRYFxyMbVU7V1goxjK9Dr70LlNfeiRPecn xhQvwdJDgioZnbYZh9XEqooVDik7xR00d3M/q+VzOuI9ysNl82zyso5CAET/82hu93Vj xjFQ== 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=kK3RwRQ2hFONySy7vb2rlodkP+v8D8sjNHk/2UXCQCw=; fh=KeV11nzvo88hufxlmjsqCfOWznUjQ01GrvsGuBkCEic=; b=noQcT5maON8MB+Z2Htg5mHHZDKT3y3G2RRlFj+d+U0NKp+7A+U5LHnSr5A8yO3x1S6 0u5e9Ao99ufBxUXbhHcrL5gti+cg5Rkk4yN27x11Y0rWZTbvAFJ1nOQ5V58eYwIAh+po 8alPww+RLVbNGny9DimoLMt73D9QKYRXhBdoSODKD2FElCgCq1US4AIQTA5RZwaT/EyA hQyX3uJpaRSjdoFMSo8vVlPhpPA1YEMWB0d0PvlazKlC3MLW62nDGIoedJ6IIJMY2nEN Nr6xWmZ721C3S/HT+QOplzdjpIxgtyg2Z6a3MsNYn1M8MHL9aco1iZ31r3pIg+EwGJD+ cEuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=EFIK5Xpz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id b11-20020a170902d50b00b001c9d771b735si2420632plg.465.2023.10.20.12.05.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Oct 2023 12:05:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=EFIK5Xpz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (Postfix) with ESMTP id 3919D80DCC98; Fri, 20 Oct 2023 12:05:45 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229664AbjJTTFa (ORCPT + 99 others); Fri, 20 Oct 2023 15:05:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229473AbjJTTF3 (ORCPT ); Fri, 20 Oct 2023 15:05:29 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A11F0D4C for ; Fri, 20 Oct 2023 12:05:26 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d9a61f7aaf8so1442629276.3 for ; Fri, 20 Oct 2023 12:05:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697828726; x=1698433526; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=kK3RwRQ2hFONySy7vb2rlodkP+v8D8sjNHk/2UXCQCw=; b=EFIK5Xpz+34ho6bGbNYhtTTAOKRNZBChdY961KgaC3rQm/R9jtB1AvVD9VbDC579IH v5hPaBylm71IcYiqZ0IsNY81y1JL5sr8p8XJ2sBt67yQpNdM00Y8ufjUOk0SCqskkQlo or63JBl69lQ/jjoADt39FhCSIV4QLoxVCURrUNrM+1E3+G3n0g5AKOaCD4MVEzkHYfx6 7LhFSQeCc3WnZuAa0KmUIknkrXA4ftNsDH9zJFLAeTcTIsRKYmqm2vE4vo0VPLbqYRdH Xmm61BhkknQDCIdqX1MbUafc537intIJnbpYjGejoo6VJSuQhwpkCj+fZKW2lZc4TS8i C/7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697828726; x=1698433526; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=kK3RwRQ2hFONySy7vb2rlodkP+v8D8sjNHk/2UXCQCw=; b=c9iHAnir4nXVlenw7H0QAO5n7KPygIiylUJLhS7ogJZ08eIubYRd/aLU7ansYVBTAC B8DUTMaOK/4WHmFXOmVNor+a4t5RR3NQ4BvBhDtlYc+H3JZA65v3hnTEpL5Op8kCgvB3 UwQ6YQff4WXoq4CZNLBsIxMje3FPwPO1O0yqk/nGCTEUx1CZ2h4C3H8BtblMrbcQkOuL eCAC9cyWo2P9P3TlR55UiuIUnrB9o5DMoWiA8TAbOtX8RLBmlrZ6TDn7YTjBMi0bWyJS 2kqOxGtZW69qsk/y3OgG6ZkoR7SXpfmym63fsdOg1mG8gRZvnrsKZz0jOkMfvBrVwfeA svYQ== X-Gm-Message-State: AOJu0YxKDbaaBaGvUjatLUgCK5FfMYPy8fSz5ryPUYdXuqaBQ5SHogFH RFYvkL3FiFs7kobZchoUmnHaIQcFdEv67zqWIA== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a25:d34e:0:b0:d9a:e3d9:99bd with SMTP id e75-20020a25d34e000000b00d9ae3d999bdmr52155ybf.5.1697828725873; Fri, 20 Oct 2023 12:05:25 -0700 (PDT) Date: Fri, 20 Oct 2023 19:05:25 +0000 Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAHTPMmUC/x2NUQrCQAwFr1LybWC70kW9iojYbawB2cZE65bSu xv8nAfzZgUjZTI4NSsozWw8FYd210B+3MpIyIMzxBD3bYgB7a0ly4KD8kxqKNOXFO0j8lywf8U u1Xp1VUefM/aUjrk7pBApgZ+K0p3rP3i+bNsPsPSlOYAAAAA= X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1697828724; l=2863; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=T9L+60rFiC2C22tjDxmZadYML+C8IPdvUbMCCZ+8ZV8=; b=g+KZD1lwFHNJ1xFiRWb7uqcL0Z5bEAbJH48+DOSAsG+RzN1Fpq+/0v1gC/I3Gh+CXpoyvlGbm 8FNycQ/9sVEB7z1Ag9/AEX/GrjaQo1VSyvG8K+T824uWpGW7D3fSaty X-Mailer: b4 0.12.3 Message-ID: <20231020-strncpy-drivers-power-supply-bq256xx_charger-c-v1-1-2fad856124f9@google.com> Subject: [PATCH] power: supply: bq256xx: replace deprecated strncpy with strscpy From: Justin Stitt To: Sebastian Reichel Cc: linux-pm@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=-8.4 required=5.0 tests=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=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.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 (morse.vger.email [0.0.0.0]); Fri, 20 Oct 2023 12:05:45 -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. We expect bq->model_name to be NUL-terminated based on its usage with sysfs_emit and format strings: val->strval is assigned to bq->model_name in bq256xx_get_charger_property(): | val->strval = bq->model_name; ... then in power_supply_sysfs.c we use value.strval with a format string: | ret = sysfs_emit(buf, "%s\n", value.strval); we assigned value.strval via: | ret = power_supply_get_property(psy, psp, &value); ... which invokes psy->desc->get_property(): | return psy->desc->get_property(psy, psp, val); with bq256xx_get_charger_property(): | static const struct power_supply_desc bq256xx_power_supply_desc = { ... | .get_property = bq256xx_get_charger_property, Moreover, no NUL-padding is required as bq is zero-allocated in bq256xx_charger.c: | bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL); Considering the above, a suitable replacement is `strscpy` [2] due to the fact that it guarantees NUL-termination on the destination buffer without unnecessarily NUL-padding. Let's also opt to use the more idiomatic strscpy() usage of (dest, src, sizeof(dest)) as this more closely ties the destination buffer and the length. 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 Similar-to: https://lore.kernel.org/all/20231020-strncpy-drivers-power-supply-bq24190_charger-c-v1-1-e896223cb795@google.com/ Similar-to: https://lore.kernel.org/all/20231020-strncpy-drivers-power-supply-bq2515x_charger-c-v1-1-46664c6edf78@google.com/ Signed-off-by: Justin Stitt --- Note: build-tested only. Found with: $ rg "strncpy\(" --- drivers/power/supply/bq256xx_charger.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/power/supply/bq256xx_charger.c b/drivers/power/supply/bq256xx_charger.c index 82d3cd5ee2f9..85e1185275b9 100644 --- a/drivers/power/supply/bq256xx_charger.c +++ b/drivers/power/supply/bq256xx_charger.c @@ -1706,7 +1706,7 @@ static int bq256xx_probe(struct i2c_client *client) mutex_init(&bq->lock); - strncpy(bq->model_name, id->name, I2C_NAME_SIZE); + strscpy(bq->model_name, id->name, sizeof(bq->model_name)); bq->regmap = devm_regmap_init_i2c(client, bq->chip_info->bq256xx_regmap_config); --- base-commit: bb55d7f7f7445abcc8db50e6a65d4315e79f75c7 change-id: 20231020-strncpy-drivers-power-supply-bq256xx_charger-c-be69c58602e6 Best regards, -- Justin Stitt