Bug 22050 - Linking with -lmcheck does not hook __malloc_initialize_hook correctly
Summary: Linking with -lmcheck does not hook __malloc_initialize_hook correctly
Status: RESOLVED FIXED
Alias: None
Product: glibc
Classification: Unclassified
Component: malloc (show other bugs)
Version: 2.26
: P2 normal
Target Milestone: 2.27
Assignee: Florian Weimer
URL:
Keywords:
Depends on: 23489
Blocks:
  Show dependency treegraph
 
Reported: 2017-08-31 00:30 UTC by mo
Modified: 2018-08-11 18:08 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed: 2017-08-31 00:00:00
fweimer: security-


Attachments
Example code (253 bytes, text/plain)
2017-08-31 00:30 UTC, mo
Details

Note You need to log in before you can comment on or make changes to this bug.
Description mo 2017-08-31 00:30:24 UTC
Created attachment 10378 [details]
Example code

I tested this with the 2.26 release as well as the latest HEAD of master 8325b477b1
GCC: gcc version 7.1.1 20170630 (GCC)

When linking with -lmcheck, the mcheck initialization should be triggered by storing the address of turn_on_mcheck into the symbol __malloc_initialize_hook.
This symbol is checked by ptmalloc_init and called if present. However,
what happens when I try to link with -lmcheck, is a symbol is placed into the binary's data section called "__malloc_initialize_hook". This symbol contains the address of turn_on_mcheck. But when ptmalloc_init checks for the __malloc_initialize_hook, it checks the actual symbol in the libc not in the binary, so essentially the symbol exists twice and mcheck is never initialized.

At this point I'm pretty confused why this happens. The __malloc_initialize_hook is defined in malloc-hooks.h and should be exported, so I don't see why the duplicate symbol in the binary even exists.

It's very likely that I'm wrong and did some fundamental mistake,
but at this point I can't figure out what's wrong and hope that somebody either
spots a bug or my mistake.


Thank you
Moritz
Comment 1 Andreas Schwab 2017-08-31 08:03:34 UTC
This is because __malloc_initialize_hook is no longer exported after commit 2ba3cfa160.
Comment 2 Sourceware Commits 2017-10-16 18:53:53 UTC
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU C Library master sources".

The branch, master has been updated
       via  7ece6cd509703ee151092a50956dbd45ca1b13da (commit)
       via  d8287b36ab44aa2bc3ba59b97433bcf0ab6ae0e8 (commit)
      from  3480ddc40b8f9a9fb277fe1883e359db3db0ab17 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=7ece6cd509703ee151092a50956dbd45ca1b13da

commit 7ece6cd509703ee151092a50956dbd45ca1b13da
Author: Florian Weimer <fweimer@redhat.com>
Date:   Mon Oct 16 20:52:34 2017 +0200

    malloc: Use compat_symbol_reference in libmcheck [BZ #22050]
    
    Since glibc 2.24, __malloc_initialize_hook is a compat symbol.  As a
    result, the link editor does not export a definition of
    __malloc_initialize_hook from the main program, so that it no longer
    interposes the variable definition in libc.so.  Specifying the symbol
    version restores the exported symbol.

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=d8287b36ab44aa2bc3ba59b97433bcf0ab6ae0e8

commit d8287b36ab44aa2bc3ba59b97433bcf0ab6ae0e8
Author: Florian Weimer <fweimer@redhat.com>
Date:   Mon Oct 16 20:50:04 2017 +0200

    malloc: Do not compile mcheck-init.o as libc module
    
    Otherwise, this will lead to a link failure because the reference
    to mcheck is hidden.

-----------------------------------------------------------------------

Summary of changes:
 ChangeLog            |   10 ++++++++++
 malloc/Makefile      |    1 +
 malloc/mcheck-init.c |    3 +++
 3 files changed, 14 insertions(+), 0 deletions(-)
Comment 3 Florian Weimer 2017-10-16 18:55:06 UTC
Fixed in 2.27.