From: "Jan Beulich" Subject: Re: blktap kernel module accessing freed memory? Date: Thu, 19 Mar 2009 09:08:58 +0000 Message-ID: <49C219BA.76E4.0078.0@novell.com> References: <49BFC041.76E4.0078.0@novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part527AD4BA.2__=" Return-path: In-Reply-To: <49BFC041.76E4.0078.0@novell.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: julian.chesterfield@cl.cam.ac.uk, Andrew Warfield , julian@xensource.com Cc: 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. --=__Part527AD4BA.2__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline >>> "Jan Beulich" 17.03.09 15:22 >>> >blktap_release() makes use of the vma stored in blktap_mmap(). This vma, >however, should be invalid by the time control reaches blktap_release(), >as it gets freed from release_vma() (in the context of either exit_mm() = or >do_munmap()). > >While it seems this was always the case (and always wrong), is seems this >is becoming more of a problem with the mmap_sem locking additions of c/s >719, as that change results in more uses of the possibly no longer = initialized >data (we just had a report of info->vma->vm_mm being NULL). > >The reason I'm not directly submitting a patch for this is that I'm = uncertain >of the purpose of this extra call to zap_page_range(): While it could be >moved into a to-be-created vm_operations.close actor, it would seem >redundant to do so, since unmap_vmas() already takes care of removing >the mappings for the vma in question. > >Regardless of that, the kfree(info->vma->vm_private_data) must be >moved into this to-be-created vm_operations.close actor imo. And in >order to not risk using info->vma past that actor, it might be a good = idea >to also clear out info->vma there (namely for fast_flush_area(), which >already checks whether that pointer is non-NULL). You seem to be the original authors of blktap, so I'm hoping that you = could comment on this. Additionally I'm attaching a tentative patch (against 2.6.27.x), provided the analysis above is correct. Thanks a lot, Jan --=__Part527AD4BA.2__= Content-Type: text/plain; name="xen-blktap-vma-close.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="xen-blktap-vma-close.patch" From: jbeulich@novell.com=0ASubject: =0AReferences: bnc#484210, bnc#485652= =0APatch-mainline: obsolete=0A=0ADereferencing filp->private_data->vma in = the file_operations.release=0Aactor isn't permitted, as the vma generally = has been destroyed by that=0Atime. The kfree()ing of vma->vm_private_data = must be done in the=0Avm_operations.close actor, and the call to zap_page_r= ange() seems=0Aredundant with the caller of that actor altogether.=0A=0A---= sle11-2009-03-16.orig/drivers/xen/blktap/blktap.c 2008-12-04 = 10:12:32.000000000 +0100=0A+++ sle11-2009-03-16/drivers/xen/blktap/blktap.c= 2009-03-17 16:41:33.000000000 +0100=0A@@ -296,6 +296,10 @@ static = inline int OFFSET_TO_SEG(int offs=0A /*************************************= *****************************=0A * BLKTAP VM OPS=0A */=0A+struct = tap_vma_priv {=0A+ tap_blkif_t *info;=0A+ struct page *map[];=0A+};= =0A =0A static int blktap_fault(struct vm_area_struct *vma, struct = vm_fault *vmf)=0A {=0A@@ -316,7 +320,7 @@ static pte_t blktap_clear_pte(str= uct vm_=0A int offset, seg, usr_idx, pending_idx, mmap_idx;=0A = unsigned long uvstart =3D vma->vm_start + (RING_PAGES << PAGE_SHIFT);=0A = unsigned long kvaddr;=0A- struct page **map;=0A+ struct tap_vma_priv= *priv;=0A struct page *pg;=0A struct grant_handle_pair = *khandle;=0A struct gnttab_unmap_grant_ref unmap[2];=0A@@ -331,12 = +335,12 @@ static pte_t blktap_clear_pte(struct vm_=0A = is_fullmm);=0A =0A info =3D vma->vm_file->private_data= ;=0A- map =3D vma->vm_private_data;=0A+ priv =3D vma->vm_private_da= ta;=0A =0A /* TODO Should these be changed to if statements? */=0A = BUG_ON(!info);=0A BUG_ON(!info->idx_map);=0A- BUG_ON(!map);=0A+ = BUG_ON(!priv);=0A =0A offset =3D (int) ((uvaddr - uvstart) >> PAGE_SHIFT)= ;=0A usr_idx =3D OFFSET_TO_USR_IDX(offset);=0A@@ -348,7 +352,7 @@ = static pte_t blktap_clear_pte(struct vm_=0A kvaddr =3D idx_to_kaddr(mma= p_idx, pending_idx, seg);=0A pg =3D pfn_to_page(__pa(kvaddr) >> = PAGE_SHIFT);=0A ClearPageReserved(pg);=0A- map[offset + = RING_PAGES] =3D NULL;=0A+ priv->map[offset + RING_PAGES] =3D = NULL;=0A =0A khandle =3D &pending_handle(mmap_idx, pending_idx, = seg);=0A =0A@@ -389,9 +393,20 @@ static pte_t blktap_clear_pte(struct = vm_=0A return copy;=0A }=0A =0A+static void blktap_vma_close(struct = vm_area_struct *vma)=0A+{=0A+ struct tap_vma_priv *priv =3D vma->vm_priva= te_data;=0A+=0A+ if (priv) {=0A+ priv->info->vma =3D = NULL;=0A+ kfree(priv);=0A+ }=0A+}=0A+=0A struct = vm_operations_struct blktap_vm_ops =3D {=0A fault: blktap_fault,=0A = zap_pte: blktap_clear_pte,=0A+ close: blktap_vma_close,=0A = };=0A =0A /****************************************************************= **=0A@@ -609,21 +624,6 @@ static int blktap_release(struct inode *=0A = ClearPageReserved(virt_to_page(info->ufe_ring.sring));=0A free_page((= unsigned long) info->ufe_ring.sring);=0A =0A- /* Clear any active = mappings and free foreign map table */=0A- if (info->vma) {=0A- = struct mm_struct *mm =3D info->vma->vm_mm;=0A-=0A- down_write(= &mm->mmap_sem);=0A- zap_page_range(=0A- = info->vma, info->vma->vm_start, =0A- info->vma->vm_end = - info->vma->vm_start, NULL);=0A- up_write(&mm->mmap_sem);=0A= -=0A- kfree(info->vma->vm_private_data);=0A-=0A- = info->vma =3D NULL;=0A- }=0A-=0A if (info->idx_map) {=0A = kfree(info->idx_map);=0A info->idx_map =3D NULL;=0A@@ = -662,8 +662,7 @@ static int blktap_release(struct inode *=0A static int = blktap_mmap(struct file *filp, struct vm_area_struct *vma)=0A {=0A = int size;=0A- struct page **map;=0A- int i;=0A+ struct tap_vma_priv= *priv;=0A tap_blkif_t *info =3D filp->private_data;=0A int = ret;=0A =0A@@ -700,18 +699,16 @@ static int blktap_mmap(struct file = *filp=0A }=0A =0A /* Mark this VM as containing foreign = pages, and set up mappings. */=0A- map =3D kzalloc(((vma->vm_end - = vma->vm_start) >> PAGE_SHIFT)=0A- * sizeof(struct page = *),=0A- GFP_KERNEL);=0A- if (map =3D=3D NULL) {=0A+ = priv =3D kzalloc(sizeof(*priv) + ((vma->vm_end - vma->vm_start)=0A+ = >> PAGE_SHIFT) * sizeof(*priv->map),=0A+ = GFP_KERNEL);=0A+ if (priv =3D=3D NULL) {=0A WPRINTK("Co= uldn't alloc VM_FOREIGN map.\n");=0A goto fail;=0A }=0A+ = priv->info =3D info;=0A =0A- for (i =3D 0; i < ((vma->vm_end - = vma->vm_start) >> PAGE_SHIFT); i++)=0A- map[i] =3D NULL;=0A- = =0A- vma->vm_private_data =3D map;=0A+ vma->vm_private_data =3D = priv;=0A vma->vm_flags |=3D VM_FOREIGN;=0A vma->vm_flags |=3D = VM_DONTCOPY;=0A =0A@@ -1192,7 +1189,7 @@ static int blktap_read_ufe_ring(ta= p_blki=0A for (j =3D 0; j < pending_req->nr_pages; j++) {=0A = =0A unsigned long kvaddr, uvaddr;=0A- = struct page **map =3D info->vma->vm_private_data;=0A+ = struct tap_vma_priv *priv =3D info->vma->vm_private_data;=0A = struct page *pg;=0A int offset;=0A =0A@@ -1203,7 = +1200,7 @@ static int blktap_read_ufe_ring(tap_blki=0A = ClearPageReserved(pg);=0A offset =3D (uvaddr - = info->vma->vm_start) =0A >> PAGE_SHIFT;=0A- = map[offset] =3D NULL;=0A+ priv->map[o= ffset] =3D NULL;=0A }=0A fast_flush_area(pending_req= , pending_idx, usr_idx, info->minor);=0A info->idx_map[usr_i= dx] =3D INVALID_REQ;=0A@@ -1369,6 +1366,7 @@ static void dispatch_rw_block_= io(blkif_t=0A unsigned int nseg;=0A int ret, i, nr_sects =3D 0;=0A = tap_blkif_t *info;=0A+ struct tap_vma_priv *priv;=0A blkif_request_t = *target;=0A int pending_idx =3D RTN_PEND_IDX(pending_req,pending_req->m= em_idx);=0A int usr_idx;=0A@@ -1434,6 +1432,7 @@ static void dispatch_r= w_block_io(blkif_t=0A pending_req->status =3D BLKIF_RSP_OKAY;=0A = pending_req->nr_pages =3D nseg;=0A op =3D 0;=0A+ priv =3D info->vma-= >vm_private_data;=0A mm =3D info->vma->vm_mm;=0A if (!xen_feature(XE= NFEAT_auto_translated_physmap))=0A down_write(&mm->mmap_sem);= =0A@@ -1517,8 +1516,7 @@ static void dispatch_rw_block_io(blkif_t=0A = >> PAGE_SHIFT));=0A = offset =3D (uvaddr - info->vma->vm_start) >> PAGE_SHIFT;=0A = pg =3D pfn_to_page(__pa(kvaddr) >> PAGE_SHIFT);=0A- = ((struct page **)info->vma->vm_private_data)[offset] =3D=0A- = pg;=0A+ priv->map[offset] =3D pg;=0A = }=0A } else {=0A for (i =3D 0; i < nseg; i++) {=0A@@ = -1545,8 +1543,7 @@ static void dispatch_rw_block_io(blkif_t=0A =0A = offset =3D (uvaddr - info->vma->vm_start) >> PAGE_SHIFT;=0A = pg =3D pfn_to_page(__pa(kvaddr) >> PAGE_SHIFT);=0A- = ((struct page **)info->vma->vm_private_data)[offset] =3D=0A- = pg;=0A+ priv->map[offset] =3D pg;=0A = }=0A }=0A =0A --=__Part527AD4BA.2__= 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 --=__Part527AD4BA.2__=--