Linux SCSI subsystem development
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: Thorsten Blum <thorsten.blum@linux.dev>
Cc: "James E.J. Bottomley" <James.Bottomley@HansenPartnership.com>,
	"Martin K. Petersen" <martin.petersen@oracle.com>,
	linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH RESEND] scsi: scsi_ioctl: use strnlen in scsi_ioctl_get_pci
Date: Tue, 2 Jun 2026 10:19:34 +0100	[thread overview]
Message-ID: <20260602101934.57687164@pumpkin> (raw)
In-Reply-To: <20260517171546.2304-2-thorsten.blum@linux.dev>

On Sun, 17 May 2026 19:15:47 +0200
Thorsten Blum <thorsten.blum@linux.dev> wrote:

> Use strnlen() to limit string scanning to 20 characters.

Should that code write the trailing '\0'?
Looks like it has been broken since 2.6.29.
Prior to that it always wrote 20 bytes (changing from 8 bytes in 2.5.0)

I think it needs:
	char buf[20];
	int len = strscpy(buf, name);
	if (copy_to_user(arg, buf, len < 0 ? sizeof (buf) : len + 1))
		return -EFAULT;

-- David


> 
> Reformat the code and use tabs instead of spaces while at it.
> 
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
> ---
>  drivers/scsi/scsi_ioctl.c | 9 +++++----
>  1 file changed, 5 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/scsi/scsi_ioctl.c b/drivers/scsi/scsi_ioctl.c
> index 0ddc95bafc71..d98c2f19b1e9 100644
> --- a/drivers/scsi/scsi_ioctl.c
> +++ b/drivers/scsi/scsi_ioctl.c
> @@ -176,10 +176,11 @@ static int scsi_ioctl_get_pci(struct scsi_device *sdev, void __user *arg)
>  
>  	name = dev_name(dev);
>  
> -	/* compatibility with old ioctl which only returned
> -	 * 20 characters */
> -        return copy_to_user(arg, name, min(strlen(name), (size_t)20))
> -		? -EFAULT: 0;
> +	/* compatibility with old ioctl which only returned 20 characters */
> +	if (copy_to_user(arg, name, strnlen(name, 20)))
> +		return -EFAULT;
> +
> +	return 0;
>  }
>  
>  static int sg_get_version(int __user *p)
> 


      parent reply	other threads:[~2026-06-02  9:19 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-17 17:15 [PATCH RESEND] scsi: scsi_ioctl: use strnlen in scsi_ioctl_get_pci Thorsten Blum
2026-05-18  7:29 ` John Garry
2026-05-23  1:13 ` Martin K. Petersen
2026-06-02  2:10 ` Martin K. Petersen
2026-06-02  9:19 ` David Laight [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260602101934.57687164@pumpkin \
    --to=david.laight.linux@gmail.com \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    --cc=thorsten.blum@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox