linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] scsi: 3w-sas: Fix unterminated strncpy
@ 2019-07-30  8:40 Chuhong Yuan
  2019-07-30 14:56 ` James Bottomley
  0 siblings, 1 reply; 3+ messages in thread
From: Chuhong Yuan @ 2019-07-30  8:40 UTC (permalink / raw)
  Cc: Adam Radford, James E . J . Bottomley, Martin K . Petersen,
	linux-scsi, linux-kernel, Chuhong Yuan

strncpy(dest, src, strlen(src)) leads to unterminated
dest, which is dangerous.
Here driver_version's len is 32 and TW_DRIVER_VERSION
is shorter than 32.
Therefore strcpy is OK.

Signed-off-by: Chuhong Yuan <hslester96@gmail.com>
---
 drivers/scsi/3w-sas.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/scsi/3w-sas.c b/drivers/scsi/3w-sas.c
index dda6fa857709..96f529c613a6 100644
--- a/drivers/scsi/3w-sas.c
+++ b/drivers/scsi/3w-sas.c
@@ -1328,7 +1328,7 @@ static int twl_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset)
 		}
 
 		/* Load rest of compatibility struct */
-		strncpy(tw_dev->tw_compat_info.driver_version, TW_DRIVER_VERSION, strlen(TW_DRIVER_VERSION));
+		strcpy(tw_dev->tw_compat_info.driver_version, TW_DRIVER_VERSION);
 		tw_dev->tw_compat_info.driver_srl_high = TW_CURRENT_DRIVER_SRL;
 		tw_dev->tw_compat_info.driver_branch_high = TW_CURRENT_DRIVER_BRANCH;
 		tw_dev->tw_compat_info.driver_build_high = TW_CURRENT_DRIVER_BUILD;
-- 
2.20.1


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

* Re: [PATCH] scsi: 3w-sas: Fix unterminated strncpy
  2019-07-30  8:40 [PATCH] scsi: 3w-sas: Fix unterminated strncpy Chuhong Yuan
@ 2019-07-30 14:56 ` James Bottomley
  2019-08-05  6:22   ` Chuhong Yuan
  0 siblings, 1 reply; 3+ messages in thread
From: James Bottomley @ 2019-07-30 14:56 UTC (permalink / raw)
  To: Chuhong Yuan; +Cc: Adam Radford, Martin K . Petersen, linux-scsi, linux-kernel

On Tue, 2019-07-30 at 16:40 +0800, Chuhong Yuan wrote:
> strncpy(dest, src, strlen(src)) leads to unterminated
> dest, which is dangerous.

I don't buy that.  The structure is only used for the
TW_IOCTL_GET_COMPATIBILITY_INFO ioctl and all the fields for that are
fixed width and are copied over as such.

> Here driver_version's len is 32 and TW_DRIVER_VERSION
> is shorter than 32.
> Therefore strcpy is OK.

The best practice for copying a string to a fixed width destination
that does get printed within the kernel would be what the 3w-9xxx.c
does

	strlcpy(tw_dev->tw_compat_info.driver_version, TW_DRIVER_VERSION,
		sizeof(tw_dev->tw_compat_info.driver_version));

But as I said, it doesn't really matter for a fixed width field that's
never printed within the kernel.

James


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

* Re: [PATCH] scsi: 3w-sas: Fix unterminated strncpy
  2019-07-30 14:56 ` James Bottomley
@ 2019-08-05  6:22   ` Chuhong Yuan
  0 siblings, 0 replies; 3+ messages in thread
From: Chuhong Yuan @ 2019-08-05  6:22 UTC (permalink / raw)
  To: James Bottomley; +Cc: Adam Radford, Martin K . Petersen, linux-scsi, LKML

On Tue, Jul 30, 2019 at 10:56 PM James Bottomley <jejb@linux.ibm.com> wrote:
>
> On Tue, 2019-07-30 at 16:40 +0800, Chuhong Yuan wrote:
> > strncpy(dest, src, strlen(src)) leads to unterminated
> > dest, which is dangerous.
>
> I don't buy that.  The structure is only used for the
> TW_IOCTL_GET_COMPATIBILITY_INFO ioctl and all the fields for that are
> fixed width and are copied over as such.
>
> > Here driver_version's len is 32 and TW_DRIVER_VERSION
> > is shorter than 32.
> > Therefore strcpy is OK.
>
> The best practice for copying a string to a fixed width destination
> that does get printed within the kernel would be what the 3w-9xxx.c
> does
>
>         strlcpy(tw_dev->tw_compat_info.driver_version, TW_DRIVER_VERSION,
>                 sizeof(tw_dev->tw_compat_info.driver_version));
>

This is right, and strscpy() is better than strlcpy(). strlcpy() is deprecated
now according to the documentation.
I choose strcpy() since it has better performance and there is no worry of
overflow here. And I find there are indeed some places using strcpy() to fix
this problem, like add_man_viewer() in tools/perf/builtin-help.c.

> But as I said, it doesn't really matter for a fixed width field that's
> never printed within the kernel.
>

I think it is not good to leave a exploitable place here, and fixing it does not
need much effort.

Regards,
Chuhong

> James
>

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

end of thread, other threads:[~2019-08-05  6:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-30  8:40 [PATCH] scsi: 3w-sas: Fix unterminated strncpy Chuhong Yuan
2019-07-30 14:56 ` James Bottomley
2019-08-05  6:22   ` Chuhong Yuan

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).