You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@spamassassin.apache.org by bu...@bugzilla.spamassassin.org on 2004/12/13 21:48:59 UTC

[Bug 4028] New: memleak in libspamc.c

http://bugzilla.spamassassin.org/show_bug.cgi?id=4028

           Summary: memleak in libspamc.c
           Product: Spamassassin
           Version: 3.0.1
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P5
         Component: Libraries
        AssignedTo: dev@spamassassin.apache.org
        ReportedBy: torte@netztorte.de


There seems to be a leak in libspamc.c (from the 3.0.1 tarball),
line 1090:

void message_cleanup(struct message *m)
{
    if (m->out != NULL && m->pre != NULL && m->out != m->pre+m->pre_len)
	free(m->out);
...
}

In non-BSMTP mode, m->pre is NULL, so m->out does never get freed
(I couldnt find a reason for those checks in the bsmtp part, so
I think it should be omitted completely - but you probably know
better).

I guess it should read:
    if (m->out != NULL)
	free(m->out);

Btw., I don't know if the leak has an effect in the spamassassin
distribution, we use libspamc in a plugin for sylpheed-claws,
thats where I found the problem.



------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

[Bug 4028] memleak in libspamc.c

Posted by bu...@bugzilla.spamassassin.org.
http://bugzilla.spamassassin.org/show_bug.cgi?id=4028





------- Additional Comments From sidney@sidney.com  2004-12-18 19:43 -------
I agree. I commited a change (rev 122740) that adds if (m->outbuf) before the
free(m->outbuf). I also changed the indentation of the next three lines (which I
had added in the last commit) to match the tab width used by the rest of the
file which is different from the settings in my editor.

I didn't create an attachment for such a trivial patch.




------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

[Bug 4028] memleak in libspamc.c

Posted by bu...@bugzilla.spamassassin.org.
http://bugzilla.spamassassin.org/show_bug.cgi?id=4028





------- Additional Comments From nix@esperi.org.uk  2005-01-04 11:59 -------
Subject: Re:  memleak in libspamc.c

On Sat, 18 Dec 2004, lwilton@earthlink.net said:
> Sidney, in use_msg_for_out it would be safer to check that outbuf isn't null
> before freeing it.  Free isn't delete, free will crash on a null pointer
> free.

POSIX says otherwise: free(NULL) is explicitly permitted.





------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

[Bug 4028] memleak in libspamc.c

Posted by bu...@bugzilla.spamassassin.org.
http://bugzilla.spamassassin.org/show_bug.cgi?id=4028





------- Additional Comments From sidney@sidney.com  2004-12-18 11:56 -------
Created an attachment (id=2573)
 --> (http://bugzilla.spamassassin.org/attachment.cgi?id=2573&action=view)
Patch to libspamc that is easier to be sure has no memory leak

I looked at the code. It doesn't seem right to me and it seems confusing.

m->out may be set to some malloc'd memory. That memory may be freed, at which
point m->out is be set to m->msg, a pointer to whose value changes.

Thus the logic should be:
  if m->out points to its own buffer, free it.
  if m->raw points to anything, free it
  if m->pre points to anything, free it.

The problem is that test for "if m->out points to its own buffer". I'm sure
that whoever wrote the code originally had in their head exactly what is done
with m->out, m->msg, and m->pre so that the test would be exactly correct. And
maybe whoever has changed code afterwards did not accidentally change something
in a way that changed the assumptions of the test. But I'm skeptical.

I'm attaching a patch that I think is a lot safer. It does this instead of the
above:
  m->outbuf is set to some malloc'd memory.
  m->out is initially set to m->outbuf. Later it may be set to m-msg, at which
point m->outbuf can be freed. That code is repeated four times and I'm adding a
line to it, so I'm putting it in a static subroutine.

Then the cleanup code becomes:

  if m->outbuf points to anything, free it.
  if m->raw points to anything, free it
  if m->pre points to anything, free it.

The patch passes make test so I'm checking this in to trunk. If anyone doesn't
like it, please let me know.




------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

[Bug 4028] memleak in libspamc.c

Posted by bu...@bugzilla.spamassassin.org.
http://bugzilla.spamassassin.org/show_bug.cgi?id=4028





------- Additional Comments From nix@esperi.org.uk  2005-01-05 03:45 -------
Subject: Re:  memleak in libspamc.c

On Tue, 4 Jan 2005, bugzilla-daemon@bugzilla.spamassassin.org spake:
>> POSIX says otherwise: free(NULL) is explicitly permitted.
> 
> yes, but we need to support non-POSIX platforms.  checking for NULL is
> a good plan.

It certainly can't do any harm. Also, I audited a bunch of Unix boxes
yesterday evening and found two (an old AIX box, and one running DEC
OSF/1 4.0d) where free(NULL) crashes.

So even platforms that should follow POSIX don't necessarily conform
well enough :(





------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

[Bug 4028] memleak in libspamc.c

Posted by bu...@bugzilla.spamassassin.org.
http://bugzilla.spamassassin.org/show_bug.cgi?id=4028





------- Additional Comments From sidney@sidney.com  2004-12-13 13:11 -------
Assuming that there was a reason for the check, it looks like what was intended was

    if (m->out != NULL && (m->pre == NULL || m->out != m->pre+m->pre_len))
	free(m->out);

I didn't look at the code yet to see if m->out really should not be freed if it
is equal to m->pre+m->pre_len, or if that is a mistake.



------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

[Bug 4028] memleak in libspamc.c

Posted by bu...@bugzilla.spamassassin.org.
http://bugzilla.spamassassin.org/show_bug.cgi?id=4028





------- Additional Comments From jm@jmason.org  2005-01-04 12:14 -------
Subject: Re:  memleak in libspamc.c 

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


> > Sidney, in use_msg_for_out it would be safer to check that outbuf isn't null
> > before freeing it.  Free isn't delete, free will crash on a null pointer
> > free.
> 
> POSIX says otherwise: free(NULL) is explicitly permitted.

yes, but we need to support non-POSIX platforms.  checking for NULL is
a good plan.

- --j.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Exmh CVS

iD8DBQFB2vkPMJF5cimLx9ARAr0GAJwPpEI6ZpwSmJhvLjI6JD7T2hBDegCgqkDG
4d8NsjCJ8gp2eS9pga24WzQ=
=m8rK
-----END PGP SIGNATURE-----





------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

[Bug 4028] memleak in libspamc.c

Posted by bu...@bugzilla.spamassassin.org.
http://bugzilla.spamassassin.org/show_bug.cgi?id=4028





------- Additional Comments From lwilton@earthlink.net  2004-12-18 15:46 -------
Subject: Re:  memleak in libspamc.c

Sidney, in use_msg_for_out it would be safer to check that outbuf isn't null
before freeing it.  Free isn't delete, free will crash on a null pointer
free.

Now, you may KNOW from the logic that outbuf will never be null when that
routine is called, so the existing code may be safe.  But the next person
that calls that routine someplace may violate those unstated assumptions.
Better to be safe and check first.

        Loren





------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

[Bug 4028] memleak in libspamc.c

Posted by bu...@bugzilla.spamassassin.org.
http://bugzilla.spamassassin.org/show_bug.cgi?id=4028

sidney@sidney.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED
   Target Milestone|Future                      |3.1.0



------- Additional Comments From sidney@sidney.com  2004-12-18 11:59 -------
Fixed, checked in to rev 122717




------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.