Skip to contentSkip to conversation

ata: libata-scsi: fix sloppy result type of ata_ioc32()

PATCH In review Sergey Shtylyov 2 participants 7 messages Jun 17, 2022 – Jun 21, 2022 libata

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ata: libata-scsi: fix sloppy result type of ata_ioc32()

While ata_ioc32() returns *int*, its result gets assigned to and compared with the *unsigned long* variable 'val' in ata_sas_scsi_ioctl(), its only caller, which implies a problematic implicit cast -- fix that by returning *unsigned long* instead.

Found by Linux Verification Center (linuxtesting.org) with the SVACE static analysis tool.

---
This patch is against the 'for-next' branch of Damien's 'libata.git' repo.

 drivers/ata/libata-scsi.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/ata/libata-scsi.c +1 -1
Index: libata/drivers/ata/libata-scsi.c
===================================================================
@@ -539,7 +539,7 @@ int ata_task_ioctl(struct scsi_device *s
 	return rc;
 }
 
-static int ata_ioc32(struct ata_port *ap)
+static unsigned long ata_ioc32(struct ata_port *ap)
 {
 	if (ap->flags & ATA_FLAG_PIO_DMA)
 		return 1;
#1 lore raw
Quoted 21 lines · Sergey Shtylyov

While ata_ioc32() returns *int*, its result gets assigned to and compared with the *unsigned long* variable 'val' in ata_sas_scsi_ioctl(), its only caller, which implies a problematic implicit cast -- fix that by returning *unsigned long* instead.

Found by Linux Verification Center (linuxtesting.org) with the SVACE static analysis tool.

---
This patch is against the 'for-next' branch of Damien's 'libata.git' repo.

 drivers/ata/libata-scsi.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Index: libata/drivers/ata/libata-scsi.c
===================================================================

drivers/ata/libata-scsi.c int ata_task_ioctl(struct scsi_device *s · line 545

Quoted 8 lines
drivers/ata/libata-scsi.c +1 -1
@@ -539,7 +539,7 @@ int ata_task_ioctl(struct scsi_device *s
 	return rc;
 }
 
-static int ata_ioc32(struct ata_port *ap)
+static unsigned long ata_ioc32(struct ata_port *ap)
 {
 	if (ap->flags & ATA_FLAG_PIO_DMA)
 		return 1;

Actually, this should be a bool I think and the ioctl code cleaned to use that type since the val argument of the ioctl is also used as a bool.

Signature · 2 lines
Damien Le Moal
Western Digital Research
#2 lore raw
Quoted 24 lines · Damien Le Moal
Quoted 24 lines

While ata_ioc32() returns *int*, its result gets assigned to and compared with the *unsigned long* variable 'val' in ata_sas_scsi_ioctl(), its only caller, which implies a problematic implicit cast -- fix that by returning *unsigned long* instead.

Found by Linux Verification Center (linuxtesting.org) with the SVACE static analysis tool.

---
This patch is against the 'for-next' branch of Damien's 'libata.git' repo.

 drivers/ata/libata-scsi.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Index: libata/drivers/ata/libata-scsi.c
===================================================================

drivers/ata/libata-scsi.c +0 -0
@@ -539,7 +539,7 @@ int ata_task_ioctl(struct scsi_device *s
 	return rc;
 }
 
Quoted 7 lines

drivers/ata/libata-scsi.c int ata_task_ioctl(struct scsi_device *s · line 545

Quoted 5 lines
drivers/ata/libata-scsi.c +1 -1
-static int ata_ioc32(struct ata_port *ap)
+static unsigned long ata_ioc32(struct ata_port *ap)
 {
 	if (ap->flags & ATA_FLAG_PIO_DMA)
 		return 1;

Actually, this should be a bool I think and the ioctl code cleaned to use

By the ioctl code you mean ata_sas_scsi_ioctl()?

Quoted 1 line

that type since the val argument of the ioctl is also used as a bool.

As for HDIO_SET_32BIT, that's prolly OK but what to do with HDIO_GET_32BIT
(it calls put_user() on *unsigned long*)?

MBR, Sergey

#3 lore raw
Quoted 34 lines · Sergey Shtylyov
Quoted 24 lines · Damien Le Moal
Quoted 24 lines

While ata_ioc32() returns *int*, its result gets assigned to and compared with the *unsigned long* variable 'val' in ata_sas_scsi_ioctl(), its only caller, which implies a problematic implicit cast -- fix that by returning *unsigned long* instead.

Found by Linux Verification Center (linuxtesting.org) with the SVACE static analysis tool.

---
This patch is against the 'for-next' branch of Damien's 'libata.git' repo.

 drivers/ata/libata-scsi.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Index: libata/drivers/ata/libata-scsi.c
===================================================================

drivers/ata/libata-scsi.c +0 -0
@@ -539,7 +539,7 @@ int ata_task_ioctl(struct scsi_device *s
 	return rc;
 }
 
Quoted 7 lines

drivers/ata/libata-scsi.c int ata_task_ioctl(struct scsi_device *s · line 545

Quoted 5 lines
drivers/ata/libata-scsi.c +1 -1
-static int ata_ioc32(struct ata_port *ap)
+static unsigned long ata_ioc32(struct ata_port *ap)
 {
 	if (ap->flags & ATA_FLAG_PIO_DMA)
 		return 1;

Actually, this should be a bool I think and the ioctl code cleaned to use

Quoted 1 line

By the ioctl code you mean ata_sas_scsi_ioctl()?

yes.

Quoted 5 lines
Quoted 2 lines

that type since the val argument of the ioctl is also used as a bool.

As for HDIO_SET_32BIT, that's prolly OK but what to do with HDIO_GET_32BIT
(it calls put_user() on *unsigned long*)?

Something like this should work fine:

drivers/ata/libata-scsi.c +0 -0
index 86dbb1cdfabd..ec7f79cbb135 100644
@@ -556,6 +556,7 @@ int ata_sas_scsi_ioctl(struct ata_port *ap, struct

scsi_device *scsidev,
unsigned int cmd, void __user *arg)

 {
        unsigned long val;
+       bool pio32;
        int rc = -EINVAL;
        unsigned long flags;

@@ -571,16 +572,16 @@ int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *scsidev,

                return put_user(val, (unsigned long __user *)arg);

        case HDIO_SET_32BIT:
-               val = (unsigned long) arg;
+               pio32 = !!((unsigned long) arg);
                rc = 0;
                spin_lock_irqsave(ap->lock, flags);
                if (ap->pflags & ATA_PFLAG_PIO32CHANGE) {
-                       if (val)
+                       if (pio32)
                                ap->pflags |= ATA_PFLAG_PIO32;
                        else
                                ap->pflags &= ~ATA_PFLAG_PIO32;
                } else {
-                       if (val != ata_ioc32(ap))
+                       if (pio32 != ata_ioc32(ap))
                                rc = -EINVAL;
                }
                spin_unlock_irqrestore(ap->lock, flags);
Quoted 2 lines

MBR, Sergey

Signature · 2 lines
Damien Le Moal
Western Digital Research
#4 lore raw

On 6/21/22 1:44 AM, Damien Le Moal wrote:
[...]

Quoted 47 lines
Quoted 32 lines
Quoted 24 lines
Quoted 24 lines

While ata_ioc32() returns *int*, its result gets assigned to and compared with the *unsigned long* variable 'val' in ata_sas_scsi_ioctl(), its only caller, which implies a problematic implicit cast -- fix that by returning *unsigned long* instead.

Found by Linux Verification Center (linuxtesting.org) with the SVACE static analysis tool.

---
This patch is against the 'for-next' branch of Damien's 'libata.git' repo.

 drivers/ata/libata-scsi.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Index: libata/drivers/ata/libata-scsi.c
===================================================================

drivers/ata/libata-scsi.c +0 -0
@@ -539,7 +539,7 @@ int ata_task_ioctl(struct scsi_device *s
 	return rc;
 }
 
Quoted 7 lines

drivers/ata/libata-scsi.c int ata_task_ioctl(struct scsi_device *s · line 545

Quoted 5 lines
drivers/ata/libata-scsi.c +1 -1
-static int ata_ioc32(struct ata_port *ap)
+static unsigned long ata_ioc32(struct ata_port *ap)
 {
 	if (ap->flags & ATA_FLAG_PIO_DMA)
 		return 1;

Actually, this should be a bool I think and the ioctl code cleaned to use

Quoted 1 line

By the ioctl code you mean ata_sas_scsi_ioctl()?

yes.

Quoted 4 lines
Quoted 1 line

that type since the val argument of the ioctl is also used as a bool.

As for HDIO_SET_32BIT, that's prolly OK but what to do with HDIO_GET_32BIT
(it calls put_user() on *unsigned long*)?

Something like this should work fine:

drivers/ata/libata-scsi.c +0 -0
index 86dbb1cdfabd..ec7f79cbb135 100644

[...]

drivers/ata/libata-scsi.c int ata_sas_scsi_ioctl(struct ata_port *ap, struct · line 575

Quoted 5 lines
drivers/ata/libata-scsi.c +0 -0
@@ -571,16 +572,16 @@ int ata_sas_scsi_ioctl(struct ata_port *ap, struct
 scsi_device *scsidev,
                return put_user(val, (unsigned long __user *)arg);
 
        case HDIO_SET_32BIT:

Hmm, I told you this *case* is prolly OK -- it was HDIO_GET_32BIT *case* that
I was concerned about... So you mean that HDIO_GET_32BIT handling should remain intact?

Quoted 15 lines
drivers/ata/libata-scsi.c +3 -3
-               val = (unsigned long) arg;
+               pio32 = !!((unsigned long) arg);
                rc = 0;
                spin_lock_irqsave(ap->lock, flags);
                if (ap->pflags & ATA_PFLAG_PIO32CHANGE) {
-                       if (val)
+                       if (pio32)
                                ap->pflags |= ATA_PFLAG_PIO32;
                        else
                                ap->pflags &= ~ATA_PFLAG_PIO32;
                } else {
-                       if (val != ata_ioc32(ap))
+                       if (pio32 != ata_ioc32(ap))
                                rc = -EINVAL;
                }
Quoted 1 line
                spin_unlock_irqrestore(ap->lock, flags);

Not really sure this is worth it... *unsigned long* result type for
ata_ioc32() seems simpler.

MBR, Sergey

#5 lore raw

On 6/21/22 10:14 PM, Sergey Shtylyov wrote:
[...]

Quoted 52 lines
Quoted 47 lines
Quoted 32 lines
Quoted 24 lines
Quoted 24 lines

While ata_ioc32() returns *int*, its result gets assigned to and compared with the *unsigned long* variable 'val' in ata_sas_scsi_ioctl(), its only caller, which implies a problematic implicit cast -- fix that by returning *unsigned long* instead.

Found by Linux Verification Center (linuxtesting.org) with the SVACE static analysis tool.

---
This patch is against the 'for-next' branch of Damien's 'libata.git' repo.

 drivers/ata/libata-scsi.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Index: libata/drivers/ata/libata-scsi.c
===================================================================

drivers/ata/libata-scsi.c +0 -0
@@ -539,7 +539,7 @@ int ata_task_ioctl(struct scsi_device *s
 	return rc;
 }
 
Quoted 7 lines

drivers/ata/libata-scsi.c int ata_task_ioctl(struct scsi_device *s · line 545

Quoted 5 lines
drivers/ata/libata-scsi.c +1 -1
-static int ata_ioc32(struct ata_port *ap)
+static unsigned long ata_ioc32(struct ata_port *ap)
 {
 	if (ap->flags & ATA_FLAG_PIO_DMA)
 		return 1;

Actually, this should be a bool I think and the ioctl code cleaned to use

Quoted 1 line

By the ioctl code you mean ata_sas_scsi_ioctl()?

yes.

Quoted 4 lines
Quoted 1 line

that type since the val argument of the ioctl is also used as a bool.

As for HDIO_SET_32BIT, that's prolly OK but what to do with HDIO_GET_32BIT
(it calls put_user() on *unsigned long*)?

Something like this should work fine:

drivers/ata/libata-scsi.c +0 -0
index 86dbb1cdfabd..ec7f79cbb135 100644

[...]

Quoted 4 lines
drivers/ata/libata-scsi.c +0 -0
@@ -571,16 +572,16 @@ int ata_sas_scsi_ioctl(struct ata_port *ap, struct
 scsi_device *scsidev,
                return put_user(val, (unsigned long __user *)arg);
 
Quoted 8 lines

drivers/ata/libata-scsi.c int ata_sas_scsi_ioctl(struct ata_port *ap, struct · line 575

Quoted 1 line
drivers/ata/libata-scsi.c +0 -0
        case HDIO_SET_32BIT:

Hmm, I told you this *case* is prolly OK -- it was HDIO_GET_32BIT *case* that
I was concerned about... So you mean that HDIO_GET_32BIT handling should remain intact?

Quoted 2 lines
drivers/ata/libata-scsi.c +1 -1
-               val = (unsigned long) arg;
+               pio32 = !!((unsigned long) arg);

No, this one won't do -- it changes the behavior in case ATA_PFLAG_PIO32CHANGE
isn't set... :-/

Quoted 15 lines
Quoted 13 lines
drivers/ata/libata-scsi.c +2 -2
                rc = 0;
                spin_lock_irqsave(ap->lock, flags);
                if (ap->pflags & ATA_PFLAG_PIO32CHANGE) {
-                       if (val)
+                       if (pio32)
                                ap->pflags |= ATA_PFLAG_PIO32;
                        else
                                ap->pflags &= ~ATA_PFLAG_PIO32;
                } else {
-                       if (val != ata_ioc32(ap))
+                       if (pio32 != ata_ioc32(ap))
                                rc = -EINVAL;
                }
Quoted 1 line
                spin_unlock_irqrestore(ap->lock, flags);
Quoted 2 lines

Not really sure this is worth it... *unsigned long* result type for
ata_ioc32() seems simpler.

Actually, even just modifying ata_ioc32() to return 'bool' produces
a seemingly correct code. Note that ata_ioc32() is inlined in any case...

MBR, Sergey

#6 lore raw
Quoted 81 lines · Sergey Shtylyov

On 6/21/22 10:14 PM, Sergey Shtylyov wrote:
[...]

Quoted 52 lines
Quoted 47 lines
Quoted 32 lines
Quoted 24 lines
Quoted 24 lines

While ata_ioc32() returns *int*, its result gets assigned to and compared with the *unsigned long* variable 'val' in ata_sas_scsi_ioctl(), its only caller, which implies a problematic implicit cast -- fix that by returning *unsigned long* instead.

Found by Linux Verification Center (linuxtesting.org) with the SVACE static analysis tool.

---
This patch is against the 'for-next' branch of Damien's 'libata.git' repo.

 drivers/ata/libata-scsi.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Index: libata/drivers/ata/libata-scsi.c
===================================================================

drivers/ata/libata-scsi.c +0 -0
@@ -539,7 +539,7 @@ int ata_task_ioctl(struct scsi_device *s
 	return rc;
 }
 
Quoted 7 lines

drivers/ata/libata-scsi.c int ata_task_ioctl(struct scsi_device *s · line 545

Quoted 5 lines
drivers/ata/libata-scsi.c +1 -1
-static int ata_ioc32(struct ata_port *ap)
+static unsigned long ata_ioc32(struct ata_port *ap)
 {
 	if (ap->flags & ATA_FLAG_PIO_DMA)
 		return 1;

Actually, this should be a bool I think and the ioctl code cleaned to use

Quoted 1 line

By the ioctl code you mean ata_sas_scsi_ioctl()?

yes.

Quoted 4 lines
Quoted 1 line

that type since the val argument of the ioctl is also used as a bool.

As for HDIO_SET_32BIT, that's prolly OK but what to do with HDIO_GET_32BIT
(it calls put_user() on *unsigned long*)?

Something like this should work fine:

drivers/ata/libata-scsi.c +0 -0
index 86dbb1cdfabd..ec7f79cbb135 100644

[...]

Quoted 4 lines
drivers/ata/libata-scsi.c +0 -0
@@ -571,16 +572,16 @@ int ata_sas_scsi_ioctl(struct ata_port *ap, struct
 scsi_device *scsidev,
                return put_user(val, (unsigned long __user *)arg);
 
Quoted 8 lines

drivers/ata/libata-scsi.c int ata_sas_scsi_ioctl(struct ata_port *ap, struct · line 575

Quoted 1 line
drivers/ata/libata-scsi.c +0 -0
        case HDIO_SET_32BIT:

Hmm, I told you this *case* is prolly OK -- it was HDIO_GET_32BIT *case* that
I was concerned about... So you mean that HDIO_GET_32BIT handling should remain intact?

Quoted 2 lines
drivers/ata/libata-scsi.c +1 -1
-               val = (unsigned long) arg;
+               pio32 = !!((unsigned long) arg);

No, this one won't do -- it changes the behavior in case ATA_PFLAG_PIO32CHANGE
isn't set... :-/

Quoted 15 lines
Quoted 14 lines
drivers/ata/libata-scsi.c +2 -2
                rc = 0;
                spin_lock_irqsave(ap->lock, flags);
                if (ap->pflags & ATA_PFLAG_PIO32CHANGE) {
-                       if (val)
+                       if (pio32)
                                ap->pflags |= ATA_PFLAG_PIO32;
                        else
                                ap->pflags &= ~ATA_PFLAG_PIO32;
                } else {
-                       if (val != ata_ioc32(ap))
+                       if (pio32 != ata_ioc32(ap))
                                rc = -EINVAL;
                }
                spin_unlock_irqrestore(ap->lock, flags);
Quoted 5 lines
Quoted 2 lines

Not really sure this is worth it... *unsigned long* result type for
ata_ioc32() seems simpler.

Actually, even just modifying ata_ioc32() to return 'bool' produces
a seemingly correct code. Note that ata_ioc32() is inlined in any case...

If there are no issues with the bool type conversion, I would really prefer that rather than the unsigned long route. The latter is really about silencing a static analyzer rather than ideal code. Given the name of the function, returning an unsigned long is really strange.

Quoted 2 lines

MBR, Sergey

Signature · 2 lines
Damien Le Moal
Western Digital Research