Problems with LD_PRELOAD and calloc() interposition for certain executables

前端 未结 4 1097
情歌与酒
情歌与酒 2021-02-09 15:49

Relating to a previous question of mine

I\'ve successfully interposed malloc, but calloc seems to be more problematic.

That is with ce

4条回答
  •  Happy的楠姐
    2021-02-09 16:15

    I know I am a bit late (6 years). But I wanted to override calloc() today and faced a problem because dlsym() internally uses calloc(). I solved it using a simple technique and thought of sharing it here:

    static unsigned char buffer[8192];
    
    void *calloc(size_t nmemb, size_t size)
    {
        if (calloc_ptr == NULL) // obtained from dlsym
                return buffer;
    
        init(); // uses dlsym() to find address of the real calloc()
    
        return calloc_ptr(len);
    }
    
    void free(void *in)
    {
        if (in == buffer)
            return;
    
        free_ptr(in);
    }
    

    buffer satisfies the need of dlsym() till the real calloc() has been located and my calloc_ptr function pointer initialized.

提交回复
热议问题