From patchwork Thu Sep 21 04:54:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Justin Stitt X-Patchwork-Id: 13393638 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B5054CD494B for ; Thu, 21 Sep 2023 04:54:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: Mime-Version:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Owner; bh=jgZUEN6yS+GhJ8nPq/HKdyOGhku2fZgU521quJR7fWE=; b=qvo bjaK9kJxOjwCLGFRJC3gUJ4GNi1AxpUCTb/0vZKfTrmAV4w/DLv9CyqSaYxZBDd/cr1vSdTJ84QKi 4TzMjdIInTM3lPJLg2KMKIV+AdkJt1oVmA7J71xvHq77pKbLYZXrzaVyI0Ows35VcqE7e10TYMrrM FNUZch/ahEl8gEeyn3/PMD98Emstk6Yy/VksUoCGyCZBC5QiMBm1oLCfn9DyKw59HDlmXsBRQ2OVd JM8+lMvShRScP7guqR0X6ZRaA9ZXiGMg4u8nT9yMqRos/XcFKAFgtLRXqhkVizk97vI55I3+29SYv QmMrNwKVchc9AGqcCXFztgdpwQ2kmCQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qjBha-0056Dn-2q; Thu, 21 Sep 2023 04:54:10 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qjBhX-0056Cx-0o for linux-arm-kernel@lists.infradead.org; Thu, 21 Sep 2023 04:54:08 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d81646fcf3eso818726276.0 for ; Wed, 20 Sep 2023 21:54:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695272040; x=1695876840; darn=lists.infradead.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=KBlJtLtsdI3hK0QdaI1JvF2kBovJQMFIeF0KITJB/xY=; b=4KTaQ10XrQmYV+0bPEgGMjcZisnP/B8zYfIH9VpRa4vSDRlWRHuf/bVEU3lqyeoG1T JKm5SJvjkSfIgkukuE9heKZke7KkQ2Athj0i2b9ks+4MxmbxX1o2ac6XZyE/uNUhP+jj 5eU300puk0vwnU5SRqCK75Fa6z/EMlJrQFTh/qkc6J6v32cdYmNDQKAHvyzEViT1omaL 2dwX4Vni0zgXe7g2pm97xYBtd4JanuWh/g+k/AO3WyAwaaNq3BtA2Y6ikrmAU8er9y9i nat8B/Ws55JzDeiZJ3jJ6VoSdADBsrV0Amlqora2rk/jBZKX5rkiuhuGSJuAb+MMzaQ6 IS9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695272040; x=1695876840; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=KBlJtLtsdI3hK0QdaI1JvF2kBovJQMFIeF0KITJB/xY=; b=A8tyG3TGArNCFB0NxleMcldzlUL+cj6uRF0lvz35SDCvst4OQ/+aiaA5h/XOnQqaxZ 4j89K7CBK2xmDIFT2WlHqKY5pF481jersZEjVKrPdY2FWOOC9qU7Hsxm5kngVm8w31yB WE1ASXc5Efoi/8lgJ987tWhg+scvXpx9xUkk+9BUA1aG4koiJZguI6wh5plUGHizv14h ye8Zq3LlFtQGiDXOIoakOx8RQG8//qboAAmnSZq3Y5dGxLG8DLS6bpZCl5NV5Igt1BFh zXKGYFSGHsagwkXtUz9XUlmH/GA/7BjTrU9Vp8EKOw30pFH+ZIuhgeUMyS14dvCB/nDo B+Lw== X-Gm-Message-State: AOJu0YyexJmOgOQ6nB8BQCzL3saMVIe4m4VTx9avm7X4T3H9LF3XnaEU UYLVB6PhRYeJfv/tTwbaR6wUU3pkh/dfzDyfjw== X-Google-Smtp-Source: AGHT+IHI098EEpqO6htZ6MTc85xWWh3YpKsnqENPUYf7F/xvLGI92lITJbHV0USw1ADPIZbp1s0JkljinpuKkIaDuQ== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a25:4082:0:b0:d80:1391:1f1 with SMTP id n124-20020a254082000000b00d80139101f1mr63208yba.1.1695272040719; Wed, 20 Sep 2023 21:54:00 -0700 (PDT) Date: Thu, 21 Sep 2023 04:54:00 +0000 Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAGfMC2UC/x2NSwqEMBAFryK9tsEkw4BeRVzk05nphVG6RRTx7 gZ3VbV47wIlYVIYmguEdlZeShXTNhD/vvwIOVUH21nX9dagblLiemIS3kkUmRf0KdY+O/tSRBN y777+k3MIUJdWoczH+zJO9/0A10IdAXUAAAA= X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1695272040; l=2097; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=wxLohCJ8OZyT4ckFvNL/ySlACAagL4RRycYgRdRDcQE=; b=FJozEDATUZfUMAdvAUx5oyW6ZiVUThemIA/jnMSx8yNUj5AxeA4RjStYDSxol7J7+D4rXF5CQ KDXN5IlUqusC1qJ5YHBdUIOWRiGU0IIeYHvhB5gxVpdkMBQYmxwb0L3 X-Mailer: b4 0.12.3 Message-ID: <20230921-strncpy-drivers-iio-adc-stm32-adc-c-v1-1-c50eca098597@google.com> Subject: [PATCH] iio: adc: stm32-adc: replace deprecated strncpy From: Justin Stitt To: Jonathan Cameron , Lars-Peter Clausen , Maxime Coquelin , Alexandre Torgue Cc: linux-iio@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Justin Stitt X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230920_215407_307977_E891DBB5 X-CRM114-Status: GOOD ( 12.24 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org `strncpy` is deprecated for use on NUL-terminated destination strings [1]. We should prefer more robust and less ambiguous string interfaces. We expect adc->chan_name[val] to be NUL-terminated based on ch_name's use within functions that expect NUL-terminated strings like strncmp and printf-likes: | if (!strncmp(stm32_adc_ic[i].name, ch_name, STM32_ADC_CH_SZ)) { | /* Check internal channel availability */ | switch (i) { | case STM32_ADC_INT_CH_VDDCORE: | if (!adc->cfg->regs->or_vddcore.reg) | dev_warn(&indio_dev->dev, | "%s channel not available\n", ch_name); ... There is no evidence that NUL-padding is needed either. 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. If, for any reason, NUL-padding _is_ required we should go for `strscpy_pad`. 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 Reviewed-by: Kees Cook --- Note: build-tested --- drivers/iio/adc/stm32-adc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) --- base-commit: 2cf0f715623872823a72e451243bbf555d10d032 change-id: 20230921-strncpy-drivers-iio-adc-stm32-adc-c-1bf936a4ffbb Best regards, -- Justin Stitt diff --git a/drivers/iio/adc/stm32-adc.c b/drivers/iio/adc/stm32-adc.c index f7613efb870d..9cdcf396d901 100644 --- a/drivers/iio/adc/stm32-adc.c +++ b/drivers/iio/adc/stm32-adc.c @@ -2209,7 +2209,7 @@ static int stm32_adc_generic_chan_init(struct iio_dev *indio_dev, ret = -EINVAL; goto err; } - strncpy(adc->chan_name[val], name, STM32_ADC_CH_SZ); + strscpy(adc->chan_name[val], name, STM32_ADC_CH_SZ); ret = stm32_adc_populate_int_ch(indio_dev, name, val); if (ret == -ENOENT) continue;