9front - general discussion about 9front
 help / color / mirror / Atom feed
From: igor@9lab.org
To: 9front@9front.org
Cc: igor@9lab.org, ori@eigenstate.org
Subject: Re: [9front] upas/Mail: fix multiple suicides (use after free, double free) in mbox.c (patch)
Date: Mon, 12 Apr 2021 14:49:36 +0200	[thread overview]
Message-ID: <6C9435B0EAB3ECBB65041EE29A4AA82D@9lab.org> (raw)
In-Reply-To: <97FDE9A5306132622D1D4F91D0453FA9@eigenstate.org>

Quoth ori@eigenstate.org:
> Quoth igor@9lab.org:
> > Below is a patch (inline) that fixes two variants of a suicide (use
> > after free, double free) in Mail/mbox.c on latest 9front 'Mit
> > Fruchtgeschmack' (explanation with reproducible test instructions
> > below) when messages are deleted/moved in bulk:
> > 
> > <patch>
> > diff -r 4dfbef4fa4ac sys/src/cmd/upas/Mail/mbox.c
> > --- a/sys/src/cmd/upas/Mail/mbox.c	Sat Apr 03 19:32:47 2021 +0200
> > +++ b/sys/src/cmd/upas/Mail/mbox.c	Fri Apr 09 02:54:10 2021 +0200
> > @@ -675,10 +675,11 @@
> >  static void
> >  mbflush(char **, int)
> >  {
> > -	int i, j, ln, fd;
> > +	int i, j, ln, fd, nmesg;
> >  	char *path;
> >  	Mesg *m, *p;
> >  
> > +	nmesg = mbox.nmesg;
> >  	path = estrjoin(maildir, "/ctl", nil);
> >  	fd = open(path, OWRITE);
> >  	free(path);
> > @@ -708,11 +709,13 @@
> >  			mbredraw(m->child[j], 1, 1);
> >   	}
> >  
> > -	for(i = 0, j = 0; i < mbox.nmesg; i++){
> > +	for(i = 0, j = 0; i < nmesg; i++){
> >  		m = mbox.mesg[i];
> > -		if((m->state & Szap) != 0)
> > +		if((m->state & Szap) != 0){
> >  			mesgfree(m);
> > -		else
> > +			mbox.mesg[i] = nil;
> > +			mbox.nmesg--;
> > +		}else
> >  			mbox.mesg[j++] = m;
> >  	}
> 
> I think I prefer this patch:
>
> diff -r 503c5ef2d2b5 sys/src/cmd/upas/Mail/mbox.c
> --- a/sys/src/cmd/upas/Mail/mbox.c	Sun Apr 11 20:20:41 2021 +0200
> +++ b/sys/src/cmd/upas/Mail/mbox.c	Sun Apr 11 19:58:55 2021 -0700
> @@ -715,6 +715,7 @@
>  		else
>  			mbox.mesg[j++] = m;
>  	}
> +	mbox.nmesg = j;
>  		
>  	close(fd);
>  	fprint(mbox.ctl, "clean\n");
> 

I agree, this is simpler.

Can we set the slot of the deleted message to nil as well to avoid
keeping around pointers to already freed memory?

The following expresses this in a patch:

<snip>
diff -r 4dfbef4fa4ac sys/src/cmd/upas/Mail/mbox.c
--- a/sys/src/cmd/upas/Mail/mbox.c	Sat Apr 03 19:32:47 2021 +0200
+++ b/sys/src/cmd/upas/Mail/mbox.c	Mon Apr 12 14:35:54 2021 +0200
@@ -710,12 +710,14 @@
 
 	for(i = 0, j = 0; i < mbox.nmesg; i++){
 		m = mbox.mesg[i];
-		if((m->state & Szap) != 0)
+		if((m->state & Szap) != 0){
 			mesgfree(m);
-		else
+			mbox.mesg[i] = nil;
+		}else
 			mbox.mesg[j++] = m;
 	}
-		
+	mbox.nmesg = j;
+
 	close(fd);
 	fprint(mbox.ctl, "clean\n");
 }
</snip>

This is an optional request, if you disagree with it I am fine
with your proposal.
 

  reply	other threads:[~2021-04-12 14:05 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-11 20:19 igor
2021-04-12  2:59 ` ori
2021-04-12 12:49   ` igor [this message]
2021-04-12 15:51     ` cinap_lenrek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6C9435B0EAB3ECBB65041EE29A4AA82D@9lab.org \
    --to=igor@9lab.org \
    --cc=9front@9front.org \
    --cc=ori@eigenstate.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).