unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Tomi Ollila <tomi.ollila@iki.fi>
To: David Bremner <david@tethera.net>, notmuch@notmuchmail.org
Subject: Re: [PATCH] lib: fix g_hash_table related read-after-free bug
Date: Wed, 22 Feb 2017 13:25:26 +0200	[thread overview]
Message-ID: <m2wpciv55l.fsf@guru.guru-group.fi> (raw)
In-Reply-To: <20170222103207.10000-1-david@tethera.net>

On Wed, Feb 22 2017, David Bremner <david@tethera.net> wrote:

> The two g_hash_table functions (insert, add) have different behaviour
> with respect to existing keys. g_hash_table_insert frees the new key,
> while g_hash_table_add (which is really g_hash_table_replace in
> disguise) frees the existing key. With this change 'ref' is live until
> the end of the function (assuming single-threaded access to
> 'hash'). We can't guarantee it will continue to be live in the
> future (i.e. there may be a future key duplication) so we copy it with
> the allocation context passed to parse_references (in practice this is
> the notmuch_message_t object whose parents we are finding).
>
> Thanks to Tomi for the simpler approach to the problem based on
> reading the fine glib manual.
> ---

Great work! LGTM.

Tomi

PS: tried to look whethet there were talloc_ref() (the glib way)...
there is "refcounting" but a bit different (unsuitable) way...

>
> this at least passes the --medium memory test. I'll run the full one
> but it probably needs a day or so to complete.
>
>  lib/database.cc | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/lib/database.cc b/lib/database.cc
> index f0bfe566..eddb780c 100644
> --- a/lib/database.cc
> +++ b/lib/database.cc
> @@ -652,7 +652,7 @@ parse_references (void *ctx,
>  	ref = _parse_message_id (ctx, refs, &refs);
>  
>  	if (ref && strcmp (ref, message_id)) {
> -	    g_hash_table_insert (hash, ref, NULL);
> +	    g_hash_table_add (hash, ref);
>  	    last_ref = ref;
>  	}
>      }
> @@ -661,7 +661,7 @@ parse_references (void *ctx,
>       * reference to the database.  We should avoid making a message
>       * its own parent, thus the above check.
>       */
> -    return last_ref;
> +    return talloc_strdup(ctx, last_ref);
>  }
>  
>  notmuch_status_t
> -- 
> 2.11.0

  reply	other threads:[~2017-02-22 11:25 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-19 14:15 read after free in notmuch new David Bremner
2017-02-19 15:29 ` David Bremner
2017-02-21  2:46   ` David Bremner
2017-02-21 20:25     ` Tomi Ollila
2017-02-22  1:05       ` David Bremner
2017-02-22  1:44         ` David Bremner
2017-02-22 10:32           ` [PATCH] lib: fix g_hash_table related read-after-free bug David Bremner
2017-02-22 11:25             ` Tomi Ollila [this message]
2017-02-23 13:12             ` David Bremner
2017-03-01  1:49   ` read after free in notmuch new David Bremner

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

  List information: https://notmuchmail.org/

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

  git send-email \
    --in-reply-to=m2wpciv55l.fsf@guru.guru-group.fi \
    --to=tomi.ollila@iki.fi \
    --cc=david@tethera.net \
    --cc=notmuch@notmuchmail.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.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

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 read-only IMAP folder(s) and NNTP newsgroup(s).