From: Ian Campbell Subject: Re: [PATCH] libxl: don't accept negative disk or partition indexes Date: Wed, 7 Mar 2012 11:36:42 -0800 Message-ID: <1331149002.14355.3.camel@cthulhu.hellion.org.uk> References: <4F574E370200007800076E23@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <4F574E370200007800076E23@nat28.tlf.novell.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Jan Beulich Cc: xen-devel List-Id: xen-devel@lists.xenproject.org On Wed, 2012-03-07 at 06:01 -0500, Jan Beulich wrote: > When obtained via sscanf(), they were checked against an upper bound > only so far. By converting the local variables' types to "unsigned int" > those bounds checks become sufficient (as a consequence the helper > function's parameter types need to be adjusted too). It's not strictly > necessary to also convert libxl__device_disk_dev_number()'s parameter > types - Any reason not to do it though? > the bounds checking done (now) guarantees that the values won't > run into the negative range of "int" values. > > Signed-off-by: Jan Beulich Acked-by: Ian Campbell > > --- a/tools/libxl/libxl_device.c > +++ b/tools/libxl/libxl_device.c > @@ -260,8 +260,10 @@ int libxl__device_physdisk_major_minor(c > } > > static int device_virtdisk_matches(const char *virtpath, const char *devtype, > - int *index_r, int max_index, > - int *partition_r, int max_partition) { > + unsigned int *index_r, > + unsigned int max_index, > + unsigned int *partition_r, > + unsigned int max_partition) { > const char *p; > char *ep; > int tl, c; > @@ -310,13 +312,13 @@ static int device_virtdisk_matches(const > int libxl__device_disk_dev_number(const char *virtpath, int *pdisk, > int *ppartition) > { > - int disk, partition; > + unsigned int disk, partition; > char *ep; > unsigned long ul; > int chrused; > > chrused = -1; > - if ((sscanf(virtpath, "d%ip%i%n", &disk, &partition, &chrused) >= 2 > + if ((sscanf(virtpath, "d%up%u%n", &disk, &partition, &chrused) >= 2 > && chrused == strlen(virtpath) && disk < (1<<20) && partition < 256) > || > device_virtdisk_matches(virtpath, "xvd", > > >