Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1606528 > unrolled thread

[PATCH] scsi: fcoe: sanity check string size for store_ctrl_mode option

Started byColin King <colin.king@canonical.com>
First post2017-03-22 15:10 +0100
Last post2017-03-22 20:50 +0100
Articles 4 — 3 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH] scsi: fcoe: sanity check string size for store_ctrl_mode option Colin King <colin.king@canonical.com> - 2017-03-22 15:10 +0100
    Re: [PATCH] scsi: fcoe: sanity check string size for store_ctrl_mode  option Colin Ian King <colin.king@canonical.com> - 2017-03-22 20:50 +0100
      Re: [PATCH] scsi: fcoe: sanity check string size for store_ctrl_mode  option Dan Carpenter <dan.carpenter@oracle.com> - 2017-03-23 12:50 +0100
    Re: [PATCH] scsi: fcoe: sanity check string size for store_ctrl_mode  option Dan Carpenter <dan.carpenter@oracle.com> - 2017-03-22 20:50 +0100

#1606528 — [PATCH] scsi: fcoe: sanity check string size for store_ctrl_mode option

FromColin King <colin.king@canonical.com>
Date2017-03-22 15:10 +0100
Subject[PATCH] scsi: fcoe: sanity check string size for store_ctrl_mode option
Message-ID<tnP7z-3yz-9@gated-at.bofh.it>
From: Colin Ian King <colin.king@canonical.com>

Reading and writing to mode[count - 1] implies the count should not
be less than 1 so add a sanity check for this.

Detected with CoverityScan, CID#1357345 ("Overflowed array index write")

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 drivers/scsi/fcoe/fcoe_sysfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/scsi/fcoe/fcoe_sysfs.c b/drivers/scsi/fcoe/fcoe_sysfs.c
index 9cf3d56296ab..e298240c728c 100644
--- a/drivers/scsi/fcoe/fcoe_sysfs.c
+++ b/drivers/scsi/fcoe/fcoe_sysfs.c
@@ -288,7 +288,7 @@ static ssize_t store_ctlr_mode(struct device *dev,
 	struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
 	char mode[FCOE_MAX_MODENAME_LEN + 1];
 
-	if (count > FCOE_MAX_MODENAME_LEN)
+	if (count < 1 || count > FCOE_MAX_MODENAME_LEN)
 		return -EINVAL;
 
 	strncpy(mode, buf, count);
-- 
2.11.0

[toc] | [next] | [standalone]


#1606949 — Re: [PATCH] scsi: fcoe: sanity check string size for store_ctrl_mode option

FromColin Ian King <colin.king@canonical.com>
Date2017-03-22 20:50 +0100
SubjectRe: [PATCH] scsi: fcoe: sanity check string size for store_ctrl_mode option
Message-ID<tnUqC-7zp-15@gated-at.bofh.it>
In reply to#1606528
On 22/03/17 19:39, Dan Carpenter wrote:
> On Wed, Mar 22, 2017 at 02:01:37PM +0000, Colin King wrote:
>> From: Colin Ian King <colin.king@canonical.com>
>>
>> Reading and writing to mode[count - 1] implies the count should not
>> be less than 1 so add a sanity check for this.
>>
>> Detected with CoverityScan, CID#1357345 ("Overflowed array index write")
>>
>> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> 
> This is harmless, of course, but count can't be zero.  This is a sysfs
> file so we test for zero size writes in sysfs_kf_write() and return
> early.

Ah, thanks for pointing out that. I overlooked that detail.

Colin

> 
> regards,
> dan carpenter
> 

[toc] | [prev] | [next] | [standalone]


#1607396 — Re: [PATCH] scsi: fcoe: sanity check string size for store_ctrl_mode option

FromDan Carpenter <dan.carpenter@oracle.com>
Date2017-03-23 12:50 +0100
SubjectRe: [PATCH] scsi: fcoe: sanity check string size for store_ctrl_mode option
Message-ID<to9pD-1Et-5@gated-at.bofh.it>
In reply to#1606949
On Wed, Mar 22, 2017 at 07:42:08PM +0000, Colin Ian King wrote:
> On 22/03/17 19:39, Dan Carpenter wrote:
> > On Wed, Mar 22, 2017 at 02:01:37PM +0000, Colin King wrote:
> >> From: Colin Ian King <colin.king@canonical.com>
> >>
> >> Reading and writing to mode[count - 1] implies the count should not
> >> be less than 1 so add a sanity check for this.
> >>
> >> Detected with CoverityScan, CID#1357345 ("Overflowed array index write")
> >>
> >> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> > 
> > This is harmless, of course, but count can't be zero.  This is a sysfs
> > file so we test for zero size writes in sysfs_kf_write() and return
> > early.
> 
> Ah, thanks for pointing out that. I overlooked that detail.
> 

The only reason I know this stuff is because it's annotated in Smatch.
So I do this:

diff --git a/drivers/scsi/fcoe/fcoe_sysfs.c b/drivers/scsi/fcoe/fcoe_sysfs.c
index 9cf3d56296ab..c491ad8fb0a8 100644
--- a/drivers/scsi/fcoe/fcoe_sysfs.c
+++ b/drivers/scsi/fcoe/fcoe_sysfs.c
@@ -281,6 +281,7 @@ static ssize_t show_ctlr_mode(struct device *dev,
 			"%s\n", name);
 }
 
+#include "/home/dcarpenter/progs/smatch/devel/check_debug.h"
 static ssize_t store_ctlr_mode(struct device *dev,
 			       struct device_attribute *attr,
 			       const char *buf, size_t count)
@@ -288,6 +289,7 @@ static ssize_t store_ctlr_mode(struct device *dev,
 	struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
 	char mode[FCOE_MAX_MODENAME_LEN + 1];
 
+	__smatch_implied(count);
 	if (count > FCOE_MAX_MODENAME_LEN)
 		return -EINVAL;
 

Then when I run kchecker drivers/scsi/fcoe/fcoe_sysfs.c, it tells me:

drivers/scsi/fcoe/fcoe_sysfs.c:292 store_ctlr_mode() implied: count = '1-1000000000,2147479552'

Which is sort of surprising...  The 1000000000 value is a hack I made so
that it would never complain that "off + count" will wrap.  But
apparently something has changed so it's also picking up the true limit
of count which is 2147479552.

Then I run the following commands to view the call tree:
    smdb.py store_ctlr_mode
    smdb.py dev_attr_store
    smdb.py sysfs_kf_write
I have some vim macros so I can look these up really quickly.

regards,
dan carpenter

[toc] | [prev] | [next] | [standalone]


#1606950 — Re: [PATCH] scsi: fcoe: sanity check string size for store_ctrl_mode option

FromDan Carpenter <dan.carpenter@oracle.com>
Date2017-03-22 20:50 +0100
SubjectRe: [PATCH] scsi: fcoe: sanity check string size for store_ctrl_mode option
Message-ID<tnUqC-7zp-13@gated-at.bofh.it>
In reply to#1606528
On Wed, Mar 22, 2017 at 02:01:37PM +0000, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> Reading and writing to mode[count - 1] implies the count should not
> be less than 1 so add a sanity check for this.
> 
> Detected with CoverityScan, CID#1357345 ("Overflowed array index write")
> 
> Signed-off-by: Colin Ian King <colin.king@canonical.com>

This is harmless, of course, but count can't be zero.  This is a sysfs
file so we test for zero size writes in sysfs_kf_write() and return
early.

regards,
dan carpenter

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web