diff mbox series

partitions/ibm: refactor deprecated strncpy

Message ID 20230822-strncpy-block-partitions-cmdline-ibm-v1-1-154dea8f755c@google.com (mailing list archive)
State Superseded
Commit 674576f8af1b34d7babade11082c392bcb6081b7
Headers show
Series partitions/ibm: refactor deprecated strncpy | expand

Commit Message

Justin Stitt Aug. 22, 2023, 11:59 p.m. UTC
`strncpy` is deprecated for use on NUL-terminated destination strings [1].

A suitable replacement is `strscpy` [2] due to the fact that it
guarantees NUL-termination on its destination buffer argument which is
_not_ the case for `strncpy`!

Link: 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 <justinstitt@google.com>
---
 block/partitions/ibm.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)


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

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

Comments

Jens Axboe Aug. 23, 2023, 12:09 a.m. UTC | #1
On Tue, 22 Aug 2023 23:59:26 +0000, Justin Stitt wrote:
> `strncpy` is deprecated for use on NUL-terminated destination strings [1].
> 
> A suitable replacement is `strscpy` [2] due to the fact that it
> guarantees NUL-termination on its destination buffer argument which is
> _not_ the case for `strncpy`!
> 
> 
> [...]

Applied, thanks!

[1/1] partitions/ibm: refactor deprecated strncpy
      commit: 674576f8af1b34d7babade11082c392bcb6081b7

Best regards,
Heiko Carstens Aug. 23, 2023, 1:49 p.m. UTC | #2
On Tue, Aug 22, 2023 at 11:59:26PM +0000, Justin Stitt wrote:
> `strncpy` is deprecated for use on NUL-terminated destination strings [1].
> 
> A suitable replacement is `strscpy` [2] due to the fact that it
> guarantees NUL-termination on its destination buffer argument which is
> _not_ the case for `strncpy`!
> 
> Link: 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 <justinstitt@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..e5893cf71b57 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);
> +				strscpy(type, label->vol.vollbl, 4);
> +				strscpy(name, label->vol.volid, 6);
>  			} else {
> -				strncpy(type, label->lnx.vollbl, 4);
> -				strncpy(name, label->lnx.volid, 6);
> +				strscpy(type, label->lnx.vollbl, 4);
> +				strscpy(name, label->lnx.volid, 6);
>  			}
>  			EBCASC(type, 4);
>  			EBCASC(name, 6);

I'm quite sure this is not correct, since both type and name are not
necessarily NUL-terminated, and this code operates on purpose on such
strings.

Since currently Stefan and Jan are both not available, I added Peter
Oberparleiter to Cc who hopefully knows better than me.
Heiko Carstens Aug. 23, 2023, 2:13 p.m. UTC | #3
On Wed, Aug 23, 2023 at 03:49:36PM +0200, Heiko Carstens wrote:
> On Tue, Aug 22, 2023 at 11:59:26PM +0000, Justin Stitt wrote:
> > `strncpy` is deprecated for use on NUL-terminated destination strings [1].
> > 
> > A suitable replacement is `strscpy` [2] due to the fact that it
> > guarantees NUL-termination on its destination buffer argument which is
> > _not_ the case for `strncpy`!
> > 
> > Link: 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 <justinstitt@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..e5893cf71b57 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);
> > +				strscpy(type, label->vol.vollbl, 4);
> > +				strscpy(name, label->vol.volid, 6);
> >  			} else {
> > -				strncpy(type, label->lnx.vollbl, 4);
> > -				strncpy(name, label->lnx.volid, 6);
> > +				strscpy(type, label->lnx.vollbl, 4);
> > +				strscpy(name, label->lnx.volid, 6);
> >  			}
> >  			EBCASC(type, 4);
> >  			EBCASC(name, 6);
> 
> I'm quite sure this is not correct, since both type and name are not
> necessarily NUL-terminated, and this code operates on purpose on such
> strings.
> 
> Since currently Stefan and Jan are both not available, I added Peter
> Oberparleiter to Cc who hopefully knows better than me.

I was just made aware of that this patch is already in linux-next. And
indeed: partition detection does not work anymore for DASDs. With this
patch reverted it works again.

Jens, can you remove or revert this patch again, please?
Jens Axboe Aug. 23, 2023, 2:14 p.m. UTC | #4
On 8/23/23 8:13 AM, Heiko Carstens wrote:
> On Wed, Aug 23, 2023 at 03:49:36PM +0200, Heiko Carstens wrote:
>> On Tue, Aug 22, 2023 at 11:59:26PM +0000, Justin Stitt wrote:
>>> `strncpy` is deprecated for use on NUL-terminated destination strings [1].
>>>
>>> A suitable replacement is `strscpy` [2] due to the fact that it
>>> guarantees NUL-termination on its destination buffer argument which is
>>> _not_ the case for `strncpy`!
>>>
>>> Link: 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 <justinstitt@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..e5893cf71b57 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);
>>> +				strscpy(type, label->vol.vollbl, 4);
>>> +				strscpy(name, label->vol.volid, 6);
>>>  			} else {
>>> -				strncpy(type, label->lnx.vollbl, 4);
>>> -				strncpy(name, label->lnx.volid, 6);
>>> +				strscpy(type, label->lnx.vollbl, 4);
>>> +				strscpy(name, label->lnx.volid, 6);
>>>  			}
>>>  			EBCASC(type, 4);
>>>  			EBCASC(name, 6);
>>
>> I'm quite sure this is not correct, since both type and name are not
>> necessarily NUL-terminated, and this code operates on purpose on such
>> strings.
>>
>> Since currently Stefan and Jan are both not available, I added Peter
>> Oberparleiter to Cc who hopefully knows better than me.
> 
> I was just made aware of that this patch is already in linux-next. And
> indeed: partition detection does not work anymore for DASDs. With this
> patch reverted it works again.
> 
> Jens, can you remove or revert this patch again, please?

Yep will drop it for now.
Kees Cook Aug. 23, 2023, 2:22 p.m. UTC | #5
On August 23, 2023 7:13:04 AM PDT, Heiko Carstens <hca@linux.ibm.com> wrote:
>On Wed, Aug 23, 2023 at 03:49:36PM +0200, Heiko Carstens wrote:
>> On Tue, Aug 22, 2023 at 11:59:26PM +0000, Justin Stitt wrote:
>> > `strncpy` is deprecated for use on NUL-terminated destination strings [1].
>> > 
>> > A suitable replacement is `strscpy` [2] due to the fact that it
>> > guarantees NUL-termination on its destination buffer argument which is
>> > _not_ the case for `strncpy`!
>> > 
>> > Link: 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 <justinstitt@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..e5893cf71b57 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);
>> > +				strscpy(type, label->vol.vollbl, 4);
>> > +				strscpy(name, label->vol.volid, 6);
>> >  			} else {
>> > -				strncpy(type, label->lnx.vollbl, 4);
>> > -				strncpy(name, label->lnx.volid, 6);
>> > +				strscpy(type, label->lnx.vollbl, 4);
>> > +				strscpy(name, label->lnx.volid, 6);
>> >  			}
>> >  			EBCASC(type, 4);
>> >  			EBCASC(name, 6);
>> 
>> I'm quite sure this is not correct, since both type and name are not
>> necessarily NUL-terminated, and this code operates on purpose on such
>> strings.
>> 
>> Since currently Stefan and Jan are both not available, I added Peter
>> Oberparleiter to Cc who hopefully knows better than me.
>
>I was just made aware of that this patch is already in linux-next. And
>indeed: partition detection does not work anymore for DASDs. With this
>patch reverted it works again.
>
>Jens, can you remove or revert this patch again, please?


Looks like this should be strtomem_pad() rather than strscpy()...
diff mbox series

Patch

diff --git a/block/partitions/ibm.c b/block/partitions/ibm.c
index 403756dbd50d..e5893cf71b57 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);
+				strscpy(type, label->vol.vollbl, 4);
+				strscpy(name, label->vol.volid, 6);
 			} else {
-				strncpy(type, label->lnx.vollbl, 4);
-				strncpy(name, label->lnx.volid, 6);
+				strscpy(type, label->lnx.vollbl, 4);
+				strscpy(name, label->lnx.volid, 6);
 			}
 			EBCASC(type, 4);
 			EBCASC(name, 6);