From: "Jan Beulich" Subject: [PATCH] linux/blktap: fix various checks Date: Mon, 19 Apr 2010 14:20:01 +0100 Message-ID: <4BCC74A1020000780003ADA1@vpn.id2.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part260CAC91.0__=" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: xen-devel@lists.xensource.com List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part260CAC91.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline - array indices got checked after having indexed the array already - several were off by one - BLKTAP_IOCTL_FREEINTF should not be used on other than the control device (or the logic should be changed to that when thus used only the respective device can be freed) - BLKTAP_IOCTL_MINOR can reasonably also be used on non-control devices (returning that device's minor and ignoring the passed in argument) Written and tested on 2.6.32.11 and made apply to the 2.6.18 tree without further testing. Signed-off-by: Jan Beulich --- sle11sp1-2010-04-12.orig/drivers/xen/blktap/blktap.c 2010-04-19 = 09:24:00.000000000 +0200 +++ sle11sp1-2010-04-12/drivers/xen/blktap/blktap.c 2010-04-19 = 11:59:19.000000000 +0200 @@ -558,11 +558,11 @@ void signal_tapdisk(int idx)=20 * if the userland tools set things up wrong, this could be = negative; * just don't try to signal in this case */ - if (idx < 0) + if (idx < 0 || idx >=3D MAX_TAP_DEV) return; =20 info =3D tapfds[idx]; - if ((idx < 0) || (idx > MAX_TAP_DEV) || !info) + if (!info) return; =20 if (info->pid > 0) { @@ -586,10 +586,13 @@ static int blktap_open(struct inode *ino /* ctrl device, treat differently */ if (!idx) return 0; + if (idx < 0 || idx >=3D MAX_TAP_DEV) { + WPRINTK("No device /dev/xen/blktap%d\n", idx); + return -ENODEV; + } =20 info =3D tapfds[idx]; - - if ((idx < 0) || (idx > MAX_TAP_DEV) || !info) { + if (!info) { WPRINTK("Unable to open device /dev/xen/blktap%d\n", idx); return -ENODEV; @@ -852,9 +855,11 @@ static int blktap_ioctl(struct inode *in unsigned long dev =3D arg; unsigned long flags; =20 - info =3D tapfds[dev]; + if (info || dev >=3D MAX_TAP_DEV) + return -EINVAL; =20 - if ((dev > MAX_TAP_DEV) || !info) + info =3D tapfds[dev]; + if (!info) return 0; /* should this be an error? */ =20 spin_lock_irqsave(&pending_free_lock, flags); @@ -865,16 +870,19 @@ static int blktap_ioctl(struct inode *in return 0; } case BLKTAP_IOCTL_MINOR: - { - unsigned long dev =3D arg; + if (!info) { + unsigned long dev =3D arg; =20 - info =3D tapfds[dev]; + if (dev >=3D MAX_TAP_DEV) + return -EINVAL; =20 - if ((dev > MAX_TAP_DEV) || !info) - return -EINVAL; + info =3D tapfds[dev]; + if (!info) + return -EINVAL; + } =20 return info->minor; - } + case BLKTAP_IOCTL_MAJOR: return blktap_major; =20 @@ -908,9 +916,11 @@ static void blktap_kick_user(int idx) { tap_blkif_t *info; =20 - info =3D tapfds[idx]; + if (idx < 0 || idx >=3D MAX_TAP_DEV) + return; =20 - if ((idx < 0) || (idx > MAX_TAP_DEV) || !info) + info =3D tapfds[idx]; + if (!info) return; =20 wake_up_interruptible(&info->wait); @@ -1056,9 +1066,8 @@ static void fast_flush_area(pending_req_ struct mm_struct *mm; =09 =20 - info =3D tapfds[tapidx]; - - if ((tapidx < 0) || (tapidx > MAX_TAP_DEV) || !info) { + if ((tapidx < 0) || (tapidx >=3D MAX_TAP_DEV) + || !(info =3D tapfds[tapidx])) { WPRINTK("fast_flush: Couldn't get info!\n"); return; } @@ -1306,7 +1315,7 @@ static int do_block_io_op(blkif_t *blkif rmb(); /* Ensure we see queued requests up to 'rp'. */ =20 /*Check blkif has corresponding UE ring*/ - if (blkif->dev_num < 0) { + if (blkif->dev_num < 0 || blkif->dev_num >=3D MAX_TAP_DEV) { /*oops*/ if (print_dbug) { WPRINTK("Corresponding UE "=20 @@ -1318,8 +1327,7 @@ static int do_block_io_op(blkif_t *blkif =20 info =3D tapfds[blkif->dev_num]; =20 - if (blkif->dev_num > MAX_TAP_DEV || !info || - !test_bit(0, &info->dev_inuse)) { + if (!info || !test_bit(0, &info->dev_inuse)) { if (print_dbug) { WPRINTK("Can't get UE info!\n"); print_dbug =3D 0; @@ -1427,7 +1435,7 @@ static void dispatch_rw_block_io(blkif_t struct mm_struct *mm; struct vm_area_struct *vma =3D NULL; =20 - if (blkif->dev_num < 0 || blkif->dev_num > MAX_TAP_DEV) + if (blkif->dev_num < 0 || blkif->dev_num >=3D MAX_TAP_DEV) goto fail_response; =20 info =3D tapfds[blkif->dev_num]; @@ -1748,7 +1756,7 @@ static int __init blkif_init(void) /* tapfds[0] is always NULL */ blktap_next_minor++; =20 - DPRINTK("Created misc_dev [/dev/xen/blktap%d]\n",i); + DPRINTK("Created misc_dev %d:0 [/dev/xen/blktap0]\n", ret); =20 /* Make sure the xen class exists */ if ((class =3D get_xen_class()) !=3D NULL) { --- sle11sp1-2010-04-12.orig/drivers/xen/blktap2/control.c 2009-11-06 = 10:51:17.000000000 +0100 +++ sle11sp1-2010-04-12/drivers/xen/blktap2/control.c 2010-04-14 = 15:41:58.000000000 +0200 @@ -136,7 +136,7 @@ blktap_control_ioctl(struct inode *inode case BLKTAP2_IOCTL_FREE_TAP: dev =3D arg; =20 - if (dev > MAX_BLKTAP_DEVICE || !blktaps[dev]) + if (dev >=3D MAX_BLKTAP_DEVICE || !blktaps[dev]) return -EINVAL; =20 blktap_control_destroy_device(blktaps[dev]); --- sle11sp1-2010-04-12.orig/drivers/xen/blktap2/ring.c 2009-11-06 = 10:51:32.000000000 +0100 +++ sle11sp1-2010-04-12/drivers/xen/blktap2/ring.c 2010-04-14 = 15:42:20.000000000 +0200 @@ -215,7 +215,7 @@ blktap_ring_open(struct inode *inode, st struct blktap *tap; =20 idx =3D iminor(inode); - if (idx < 0 || idx > MAX_BLKTAP_DEVICE || blktaps[idx] =3D=3D = NULL) { + if (idx < 0 || idx >=3D MAX_BLKTAP_DEVICE || blktaps[idx] =3D=3D = NULL) { BTERR("unable to open device blktap%d\n", idx); return -ENODEV; } --=__Part260CAC91.0__= Content-Type: text/plain; name="xen-blktap-checks.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="xen-blktap-checks.patch" Subject: blktap: fix various checks=0A=0A- array indices got checked after = having indexed the array already=0A- several were off by one=0A- BLKTAP_IOC= TL_FREEINTF should not be used on other than the control=0A device (or = the logic should be changed to that when thus used only=0A the respective = device can be freed)=0A- BLKTAP_IOCTL_MINOR can reasonably also be used on = non-control devices=0A (returning that device's minor and ignoring the = passed in argument)=0A=0AWritten and tested on 2.6.32.11 and made apply to = the 2.6.18 tree=0Awithout further testing.=0A=0ASigned-off-by: Jan Beulich = =0A=0A--- sle11sp1-2010-04-12.orig/drivers/xen/blktap/= blktap.c 2010-04-19 09:24:00.000000000 +0200=0A+++ sle11sp1-2010-04-= 12/drivers/xen/blktap/blktap.c 2010-04-19 11:59:19.000000000 +0200=0A@@ = -558,11 +558,11 @@ void signal_tapdisk(int idx) =0A * if the userland = tools set things up wrong, this could be negative;=0A * just don't try = to signal in this case=0A */=0A- if (idx < 0)=0A+ if (idx < = 0 || idx >=3D MAX_TAP_DEV)=0A return;=0A =0A info =3D tapfds[idx= ];=0A- if ((idx < 0) || (idx > MAX_TAP_DEV) || !info)=0A+ if = (!info)=0A return;=0A =0A if (info->pid > 0) {=0A@@ -586,10 = +586,13 @@ static int blktap_open(struct inode *ino=0A /* ctrl device, = treat differently */=0A if (!idx)=0A return 0;=0A+ if = (idx < 0 || idx >=3D MAX_TAP_DEV) {=0A+ WPRINTK("No device = /dev/xen/blktap%d\n", idx);=0A+ return -ENODEV;=0A+ }=0A =0A = info =3D tapfds[idx];=0A-=0A- if ((idx < 0) || (idx > MAX_TAP_DEV) || = !info) {=0A+ if (!info) {=0A WPRINTK("Unable to open = device /dev/xen/blktap%d\n",=0A idx);=0A = return -ENODEV;=0A@@ -852,9 +855,11 @@ static int blktap_ioctl(struct = inode *in=0A unsigned long dev =3D arg;=0A unsigned = long flags;=0A =0A- info =3D tapfds[dev];=0A+ if = (info || dev >=3D MAX_TAP_DEV)=0A+ return -EINVAL;=0A = =0A- if ((dev > MAX_TAP_DEV) || !info)=0A+ info =3D = tapfds[dev];=0A+ if (!info)=0A return 0; = /* should this be an error? */=0A =0A spin_lock_irqsave(&pending_= free_lock, flags);=0A@@ -865,16 +870,19 @@ static int blktap_ioctl(struct = inode *in=0A return 0;=0A }=0A case BLKTAP_IOCTL_MINOR:=0A= - {=0A- unsigned long dev =3D arg;=0A+ if (!info) = {=0A+ unsigned long dev =3D arg;=0A =0A- = info =3D tapfds[dev];=0A+ if (dev >=3D MAX_TAP_DEV)= =0A+ return -EINVAL;=0A =0A- if ((dev > = MAX_TAP_DEV) || !info)=0A- return -EINVAL;=0A+ = info =3D tapfds[dev];=0A+ if (!info)=0A+ = return -EINVAL;=0A+ }=0A =0A = return info->minor;=0A- }=0A+=0A case BLKTAP_IOCTL_MAJOR:=0A = return blktap_major;=0A =0A@@ -908,9 +916,11 @@ static void blktap_kick_use= r(int idx)=0A {=0A tap_blkif_t *info;=0A =0A- info =3D tapfds[idx= ];=0A+ if (idx < 0 || idx >=3D MAX_TAP_DEV)=0A+ return;=0A = =0A- if ((idx < 0) || (idx > MAX_TAP_DEV) || !info)=0A+ info =3D = tapfds[idx];=0A+ if (!info)=0A return;=0A =0A wake_up_int= erruptible(&info->wait);=0A@@ -1056,9 +1066,8 @@ static void fast_flush_are= a(pending_req_=0A struct mm_struct *mm;=0A =0A =0A- = info =3D tapfds[tapidx];=0A-=0A- if ((tapidx < 0) || (tapidx > = MAX_TAP_DEV) || !info) {=0A+ if ((tapidx < 0) || (tapidx >=3D MAX_TAP_DE= V)=0A+ || !(info =3D tapfds[tapidx])) {=0A WPRINTK("fa= st_flush: Couldn't get info!\n");=0A return;=0A }=0A@@ = -1306,7 +1315,7 @@ static int do_block_io_op(blkif_t *blkif=0A rmb(); /* = Ensure we see queued requests up to 'rp'. */=0A =0A /*Check blkif has = corresponding UE ring*/=0A- if (blkif->dev_num < 0) {=0A+ if = (blkif->dev_num < 0 || blkif->dev_num >=3D MAX_TAP_DEV) {=0A = /*oops*/=0A if (print_dbug) {=0A WPRINTK("Co= rresponding UE " =0A@@ -1318,8 +1327,7 @@ static int do_block_io_op(blkif_t= *blkif=0A =0A info =3D tapfds[blkif->dev_num];=0A =0A- if = (blkif->dev_num > MAX_TAP_DEV || !info ||=0A- !test_bit(0, &info->dev= _inuse)) {=0A+ if (!info || !test_bit(0, &info->dev_inuse)) {=0A = if (print_dbug) {=0A WPRINTK("Can't get UE info!\n");=0A= print_dbug =3D 0;=0A@@ -1427,7 +1435,7 @@ static = void dispatch_rw_block_io(blkif_t=0A struct mm_struct *mm;=0A = struct vm_area_struct *vma =3D NULL;=0A =0A- if (blkif->dev_num < 0 || = blkif->dev_num > MAX_TAP_DEV)=0A+ if (blkif->dev_num < 0 || = blkif->dev_num >=3D MAX_TAP_DEV)=0A goto fail_response;=0A =0A = info =3D tapfds[blkif->dev_num];=0A@@ -1748,7 +1756,7 @@ static = int __init blkif_init(void)=0A /* tapfds[0] is always NULL */=0A = blktap_next_minor++;=0A =0A- DPRINTK("Created misc_dev [/dev/xen/blktap%= d]\n",i);=0A+ DPRINTK("Created misc_dev %d:0 [/dev/xen/blktap0]\n", = ret);=0A =0A /* Make sure the xen class exists */=0A if ((class = =3D get_xen_class()) !=3D NULL) {=0A--- sle11sp1-2010-04-12.orig/drivers/xe= n/blktap2/control.c 2009-11-06 10:51:17.000000000 +0100=0A+++ = sle11sp1-2010-04-12/drivers/xen/blktap2/control.c 2010-04-14 = 15:41:58.000000000 +0200=0A@@ -136,7 +136,7 @@ blktap_control_ioctl(struct = inode *inode=0A case BLKTAP2_IOCTL_FREE_TAP:=0A = dev =3D arg;=0A =0A- if (dev > MAX_BLKTAP_DEVICE || !blktaps[dev= ])=0A+ if (dev >=3D MAX_BLKTAP_DEVICE || !blktaps[dev])=0A = return -EINVAL;=0A =0A blktap_control_destroy_device(blkta= ps[dev]);=0A--- sle11sp1-2010-04-12.orig/drivers/xen/blktap2/ring.c = 2009-11-06 10:51:32.000000000 +0100=0A+++ sle11sp1-2010-04-12/drivers/xen/b= lktap2/ring.c 2010-04-14 15:42:20.000000000 +0200=0A@@ -215,7 +215,7 @@ = blktap_ring_open(struct inode *inode, st=0A struct blktap *tap;=0A =0A = idx =3D iminor(inode);=0A- if (idx < 0 || idx > MAX_BLKTAP_DEV= ICE || blktaps[idx] =3D=3D NULL) {=0A+ if (idx < 0 || idx >=3D MAX_BLKTAP_= DEVICE || blktaps[idx] =3D=3D NULL) {=0A BTERR("unable to = open device blktap%d\n", idx);=0A return -ENODEV;=0A = }=0A --=__Part260CAC91.0__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --=__Part260CAC91.0__=--