Skip to contentSkip to conversation

md: Remove risk of overflow via sprintf) by using snprintf() in md_check_recovery()

PATCH Bodo Eggert 1 participant 1 message Feb 13, 2011 3 messages not in the archive MD RAID

lore
Share & tools
lore
#3 lore raw
In reply to a message not in the archive
Quoted 14 lines · Michael Tokarev
Quoted 8 lines · Daniel K.
Quoted 5 lines · Jesper Juhl

sprintf() is dangerous - given the wrong source string it will overflow the destination. snprintf() is safer in that at least we'll never overflow the destination. Even if overflow will never happen today, code changes over time and snprintf() is just safer in the long run.

Quoted 4 lines
Quoted 4 lines
(quoted diff) +1 -1
-                        sprintf(nm,"rd%d", rdev->raid_disk);
+                        snprintf(nm, sizeof(nm), "rd%d",
rdev->raid_disk);
                         sysfs_remove_link(&mddev->kobj, nm);
Quoted 4 lines

C'mon guys, this is pointless. 20 bytes allocated for the device name, and this is for raid disk number. It is impossible to have more than 10^17 (20 bytes total, 2 for "rd" and on for the zero terminator) drives in a single array.

If you argue that you might get a buffer overflow, you'll have to check for snprintf errors, too.

Signature · 4 lines
Logic: The art of being wrong with confidence... 

Friß, Spammer: [email protected] [email protected]
 [email protected] [email protected]