Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp1234213rdb; Fri, 20 Oct 2023 12:11:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEIq/xEaH0PSnLhQ1pmWUft4B1rxHRGeH+hyOgY6BOJkF/rAwcpvjctwYkWDEZnuH+C6V9s X-Received: by 2002:a05:6a20:3d03:b0:163:9f1d:b464 with SMTP id y3-20020a056a203d0300b001639f1db464mr3149794pzi.5.1697829111176; Fri, 20 Oct 2023 12:11:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697829111; cv=none; d=google.com; s=arc-20160816; b=dA/GVYhr8qyp6aWFaPySga8c9Fz3SRe04IYnjfSt9f4E+aMuQloblfc6eocFtsbiH/ V/zwdS/N9rC6BCpAth3WdcsHUCwxQVUN1fiJMXt1OV/nkKbdyoepz9NhE/FxdDKZrbrS QYXcl147MQRBP2QSrCFfRiPaXV358Z/+xQSnqE4hmTZsdQfTIS9OVs0NQsXImF12jlsy ugnuc+7bnZdgEUe9bU0FZJdhb3Vch8b2RvIyw1pS2fzR3BaOl/EUACj4q4RGdR1z+UFR Ec4EULC8383kgdIy9E9QczTAw1XEt0liuxKylkgTZeQsXKmqxMt1eRJnkyxrnP4yLqcJ yYaw== 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=JCm2M6I5bgNLk9qAIkXy20cqaWg+1XaBIDAOydYH9Ss=; fh=KeV11nzvo88hufxlmjsqCfOWznUjQ01GrvsGuBkCEic=; b=amTMVSW4WO0IibVsslWqZfi7z0D9xOoTnPlkPKn+fi4o9BJLRutupEGu7K0KdaFH8k xOeYx+DZZ9hzPxM+8gk5tuQta7N5hWdfM3yVWiVSoFo82FKkyXdkQzvKF67Q4J9bgmGm WjTdW09S1MZJbXlhYqigghj2zcIR6sfsVDdn7rtEXjsBdUCuAlQHDJ5k5+HCzcOgrLbd 1tqGrYHog0idwZ4h+g0akJOHoukayRUwaWPsCA1z0QkPVKYTsY0+/uwVtaqV/2f8nlee oUIIj07+iL/oI+uxtITAmyXnHiRLAOKBsdB8IorB+P6wqt2vj6pcIOny1E/rBPgsmn6h iQNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=U5Ny8KDY; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id fd5-20020a056a002e8500b00690258a9777si2640963pfb.20.2023.10.20.12.11.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Oct 2023 12:11:51 -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=@google.com header.s=20230601 header.b=U5Ny8KDY; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id A766E8217DED; Fri, 20 Oct 2023 12:11:24 -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 S229723AbjJTTLN (ORCPT + 99 others); Fri, 20 Oct 2023 15:11:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229614AbjJTTLM (ORCPT ); Fri, 20 Oct 2023 15:11:12 -0400 Received: from mail-oo1-xc4a.google.com (mail-oo1-xc4a.google.com [IPv6:2607:f8b0:4864:20::c4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B176FD5B for ; Fri, 20 Oct 2023 12:11:09 -0700 (PDT) Received: by mail-oo1-xc4a.google.com with SMTP id 006d021491bc7-581f38fe82cso1969672eaf.3 for ; Fri, 20 Oct 2023 12:11:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697829069; x=1698433869; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=JCm2M6I5bgNLk9qAIkXy20cqaWg+1XaBIDAOydYH9Ss=; b=U5Ny8KDY4rZVRqWlkixhibI67Yo2VCbvNUiMvMCxbU4+s4tK+in4eDDVKaMpGaZUvw 7Hk2Uub+4GSzi18+BC6aNa2MWElAd2USZF5UHKn/HwPVDfsS2OObGDOSkXnUUm4TN1N7 UW5aa+1kTPHH0smZ3uEjs2UMwQb2Iw4EA3/V0JXDZmbJ2ja2dujdXlkdRt5LPWnd5+2S XqlAMzlya8RqudGicN7esfIn7ZKeXIfj1h23b7YJn77nZfhwfqjKOEp44Dfe8FZ9nTo6 iueD44LmqxV6/cdlo8AMpNhCVwo6bX4OTIjfZ2uwdLahoBX32hxalTXZDff9wYYNBDld 7UfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697829069; x=1698433869; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=JCm2M6I5bgNLk9qAIkXy20cqaWg+1XaBIDAOydYH9Ss=; b=t/qxnsPxQ5jLznl+Mp4zHrZisfEwqtDv/mB7S+AzVGbd2xTaCN23swiwzvEhTVspFJ 7Qneo2kp9mGvaDHJ+Ly6BxN22FW7IvaHHcqlnoVLA9FPAjGcoAM90hB4tWVx4v4Xs10O +dnDRCtujmkSUwsuCai072lKzy+2Cwu2qFXy3YfmhBBCUzApKs8u2oI88T2D+rKn4yXj 76QYXIosIhYGD2s5PgjjG8Irfttzd01heW0OwUvy9w68LSzId1GRmPKRPgPKeJyMrp9+ 0nLj1C7VvQOiqP5g4ljGCg2BYcc/CikluPHcWSNbIehvczEzOeVhIV1Ih7Kiai+l7IAk Y/MA== X-Gm-Message-State: AOJu0YyCyKbqZDYaGNiwDmAwgJ2ReNmuy+AKwz+nfT6Sm/0bYl27OrgQ GEbVZXFgHjNrXFb9Wwg16FzuwZQaRJf3eq4AeQ== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a4a:7441:0:b0:581:dbb1:219b with SMTP id t1-20020a4a7441000000b00581dbb1219bmr947789ooe.0.1697829068928; Fri, 20 Oct 2023 12:11:08 -0700 (PDT) Date: Fri, 20 Oct 2023 19:11:07 +0000 Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAMrQMmUC/x2NUQrCMBAFr1L224UkpZB4FRExybYuSBp3tbaU3 t3g5zyYNzsoCZPCudtBaGHluTSwpw7S414mQs6NwRnXW+MM6ltKqhtm4YVEsc5fEtRPrc8N48s NwZtbU2Vqc8IQffSjtbk3AdppFRp5/Qcv1+P4AVjzVWGAAAAA X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1697829067; l=2877; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=u+18cFaIHnUUKwHSCJsgL5LfGU9t+G4HsmSCTeLLoLQ=; b=8FO7so2vWbouPTrrT9aPtQNW43rRsmEn5X0jiNZbdX35BM72HcSNWPRlPuRiYWxYkDjOogAhU riJeM/LPOzjBUjlfqbUeeBCfheXKSoLdgYkccwoGD7iS+oBRAs5jLLV X-Mailer: b4 0.12.3 Message-ID: <20231020-strncpy-drivers-power-supply-bq25980_charger-c-v1-1-7b93be54537b@google.com> Subject: [PATCH] power: supply: bq25980: 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 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]); Fri, 20 Oct 2023 12:11:24 -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 bq25980_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 bq25980_get_charger_property(): | static const struct power_supply_desc bq25980_power_supply_desc = { ... | .get_property = bq25980_get_charger_property, Moreover, no NUL-padding is required as bq is zero-allocated in bq25980_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/bq25980_charger.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/power/supply/bq25980_charger.c b/drivers/power/supply/bq25980_charger.c index d8411722266f..0c5e2938bb36 100644 --- a/drivers/power/supply/bq25980_charger.c +++ b/drivers/power/supply/bq25980_charger.c @@ -1223,7 +1223,7 @@ static int bq25980_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->chip_info = &bq25980_chip_info_tbl[id->driver_data]; bq->regmap = devm_regmap_init_i2c(client, --- base-commit: bb55d7f7f7445abcc8db50e6a65d4315e79f75c7 change-id: 20231020-strncpy-drivers-power-supply-bq25980_charger-c-9b8b8f11d309 Best regards, -- Justin Stitt