linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] partitions/ibm: refactor deprecated strncpy
@ 2023-08-23 21:24 Justin Stitt
  2023-08-24  9:02 ` Heiko Carstens
  0 siblings, 1 reply; 2+ messages in thread
From: Justin Stitt @ 2023-08-23 21:24 UTC (permalink / raw)
  To: Stefan Haberland, Jan Hoeppner, Jens Axboe
  Cc: Kees Cook, linux-s390, linux-block, linux-kernel, linux-hardening,
	Justin Stitt

`strncpy` is deprecated [1] and we should favor different interfaces.

A suitable replacement is `strtomem_pad` as it is a more robust and less
ambiguous interface. In this case, the destination buffer is not
necessarily NUL-terminated as Heiko points out [2]. Using `strtomem_pad`
over strncpy means it is now more obvious what is expected of the
destination buffer: 1) Not necessarily NUL-terminated and 2) padded with
NUL-bytes

Link: www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings[1]
Link: https://lore.kernel.org/all/20230823134936.14378-E-hca@linux.ibm.com/ [2]
Link: https://github.com/KSPP/linux/issues/90
Suggested-by: Kees Cook <keescook@chromium.org>
Cc: linux-hardening@vger.kernel.org
Signed-off-by: Justin Stitt <justinstitt@google.com>
---
Changes in v2:
- prefer `strtomem_pad` over `strscpy` (thanks Kees)
- Link to v1: https://lore.kernel.org/r/20230822-strncpy-block-partitions-cmdline-ibm-v1-1-154dea8f755c@google.com
---
 block/partitions/ibm.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/block/partitions/ibm.c b/block/partitions/ibm.c
index 403756dbd50d..56c076c5523d 100644
--- a/block/partitions/ibm.c
+++ b/block/partitions/ibm.c
@@ -111,11 +111,11 @@ static int find_label(struct parsed_partitions *state,
 		    !strcmp(temp, "LNX1") ||
 		    !strcmp(temp, "CMS1")) {
 			if (!strcmp(temp, "VOL1")) {
-				strncpy(type, label->vol.vollbl, 4);
-				strncpy(name, label->vol.volid, 6);
+				strtomem_pad(type, label->vol.vollbl, 4);
+				strtomem_pad(name, label->vol.volid, 6);
 			} else {
-				strncpy(type, label->lnx.vollbl, 4);
-				strncpy(name, label->lnx.volid, 6);
+				strtomem_pad(type, label->lnx.vollbl, 4);
+				strtomem_pad(name, label->lnx.volid, 6);
 			}
 			EBCASC(type, 4);
 			EBCASC(name, 6);

---
base-commit: 706a741595047797872e669b3101429ab8d378ef
change-id: 20230822-strncpy-block-partitions-cmdline-ibm-7f5bcca507ea

Best regards,
--
Justin Stitt <justinstitt@google.com>


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] partitions/ibm: refactor deprecated strncpy
  2023-08-23 21:24 [PATCH v2] partitions/ibm: refactor deprecated strncpy Justin Stitt
@ 2023-08-24  9:02 ` Heiko Carstens
  0 siblings, 0 replies; 2+ messages in thread
From: Heiko Carstens @ 2023-08-24  9:02 UTC (permalink / raw)
  To: Justin Stitt
  Cc: Stefan Haberland, Jan Hoeppner, Jens Axboe, Kees Cook, linux-s390,
	linux-block, linux-kernel, linux-hardening

On Wed, Aug 23, 2023 at 09:24:22PM +0000, Justin Stitt wrote:
> `strncpy` is deprecated [1] and we should favor different interfaces.
> 
> A suitable replacement is `strtomem_pad` as it is a more robust and less
> ambiguous interface. In this case, the destination buffer is not
> necessarily NUL-terminated as Heiko points out [2]. Using `strtomem_pad`
> over strncpy means it is now more obvious what is expected of the
> destination buffer: 1) Not necessarily NUL-terminated and 2) padded with
> NUL-bytes
> 
> Link: www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings[1]
> Link: https://lore.kernel.org/all/20230823134936.14378-E-hca@linux.ibm.com/ [2]
> Link: https://github.com/KSPP/linux/issues/90
> Suggested-by: Kees Cook <keescook@chromium.org>
> Cc: linux-hardening@vger.kernel.org
> Signed-off-by: Justin Stitt <justinstitt@google.com>
> ---
> Changes in v2:
> - prefer `strtomem_pad` over `strscpy` (thanks Kees)
> - Link to v1: https://lore.kernel.org/r/20230822-strncpy-block-partitions-cmdline-ibm-v1-1-154dea8f755c@google.com
> ---
>  block/partitions/ibm.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/block/partitions/ibm.c b/block/partitions/ibm.c
> index 403756dbd50d..56c076c5523d 100644
> --- a/block/partitions/ibm.c
> +++ b/block/partitions/ibm.c
> @@ -111,11 +111,11 @@ static int find_label(struct parsed_partitions *state,
>  		    !strcmp(temp, "LNX1") ||
>  		    !strcmp(temp, "CMS1")) {
>  			if (!strcmp(temp, "VOL1")) {
> -				strncpy(type, label->vol.vollbl, 4);
> -				strncpy(name, label->vol.volid, 6);
> +				strtomem_pad(type, label->vol.vollbl, 4);
> +				strtomem_pad(name, label->vol.volid, 6);
>  			} else {
> -				strncpy(type, label->lnx.vollbl, 4);
> -				strncpy(name, label->lnx.volid, 6);
> +				strtomem_pad(type, label->lnx.vollbl, 4);
> +				strtomem_pad(name, label->lnx.volid, 6);
>  			}
>  			EBCASC(type, 4);
>  			EBCASC(name, 6);

This won't compile if find_label() is not inlined due the BUILD_BUG_ON()
within strtomem_pad(). However instead of sending new versions, I think it
would be better to ask Stefan and Jan to have a look at this. I think there
is room for improvement with the string handling besides getting rid of
strncpy().

And they know best the semantics of the (non-)strings.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-08-24  9:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-23 21:24 [PATCH v2] partitions/ibm: refactor deprecated strncpy Justin Stitt
2023-08-24  9:02 ` Heiko Carstens

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).