Skip to contentSkip to conversation

UBI_MAX_ERASECOUNTER not used consistently

bug reportish Dan Carpenter 2 participants 2 messages Oct 5, 2012 – Oct 11, 2012 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

Hi Artem,

I normally audit for underflows and I had a question about this code.

mtd/ubi/attach.c
665 static int early_erase_peb(struct ubi_device *ubi,
666 const struct ubi_attach_info *ai, int pnum, int ec)
667 {
668 int err;
669 struct ubi_ec_hdr *ec_hdr;
670
671 if ((long long)ec >= UBI_MAX_ERASECOUNTER) {

Casting to long long doesn't serve any purpose. We normally say that "> UBI_MAX_ERASECOUNTER" is invalid but "== UBI_MAX_ERASECOUNTER" is Ok. The "ec" variable is signed and negative values are missed on this check.

672 /*

   673                   * Erase counter overflow. Upgrade UBI and use 64-bit
   674                   * erase counters internally.
   675                   */

676 ubi_err("erase counter overflow at PEB %d, EC %d", pnum, ec);
677 return -EINVAL;
678 }
679
680 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);

regards,
dan carpenter

#1 lore raw
Quoted 13 lines · Dan Carpenter

Hi Artem,

I normally audit for underflows and I had a question about this code.

mtd/ubi/attach.c
665 static int early_erase_peb(struct ubi_device *ubi,
666 const struct ubi_attach_info *ai, int pnum, int ec)

   667  {
   668          int err;
   669          struct ubi_ec_hdr *ec_hdr;
   670  
   671          if ((long long)ec >= UBI_MAX_ERASECOUNTER) {
Quoted 4 lines

Casting to long long doesn't serve any purpose. We normally say that "> UBI_MAX_ERASECOUNTER" is invalid but "== UBI_MAX_ERASECOUNTER" is Ok. The "ec" variable is signed and negative values are missed on this check.

Yes, the cast is bogus. I can fix it by removing the cast, but would like to give you a chance to send the patch so that you get the credit being the author in the git history. Let me know what you prefer.

Thanks!

Signature · 2 lines
Best Regards,
Artem Bityutskiy