Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753956Ab1BLNtF (ORCPT ); Sat, 12 Feb 2011 08:49:05 -0500 Received: from isrv.corpit.ru ([86.62.121.231]:39702 "EHLO isrv.corpit.ru" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752157Ab1BLNtC (ORCPT ); Sat, 12 Feb 2011 08:49:02 -0500 Message-ID: <4D568FCA.90902@msgid.tls.msk.ru> Date: Sat, 12 Feb 2011 16:48:58 +0300 From: Michael Tokarev Organization: Telecom Service, JSC User-Agent: Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.9.1.16) Gecko/20101227 Icedove/3.0.11 MIME-Version: 1.0 To: "Daniel K." CC: Jesper Juhl , linux-kernel@vger.kernel.org, linux-raid@vger.kernel.org, Neil Brown , Neil Brown Subject: Re: [PATCH] md: Remove risk of overflow via sprintf) by using snprintf() in md_check_recovery() References: <4D56541D.3030409@uw.no> In-Reply-To: <4D56541D.3030409@uw.no> X-Enigmail-Version: 1.0.1 OpenPGP: id=804465C5 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1307 Lines: 30 12.02.2011 12:34, Daniel K. wrote: > Jesper Juhl wrote: >> 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. > >> - sprintf(nm,"rd%d", rdev->raid_disk); >> + snprintf(nm, sizeof(nm), "rd%d", >> rdev->raid_disk); >> sysfs_remove_link(&mddev->kobj, nm); > > What if "rd1234" get truncated to "rd123" and you remove the wrong link. > (No, I didn't actually bother to check how much room was allocated.) That allocation is in the line above first sprintf which you deleted. Sure, didn't bother, it's very difficult. 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. /mjt -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/