Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between %llx and %p while printing a pointer inside driver code [duplicate]

It seems that casting a void* pointer (allocated by kmalloc) to unsigned long long changes it. Printing them with %p and %llx gives different values. Why is it so? Can anyone explain?

Following is a simple repro for that:

#include <linux/module.h>
#include <linux/slab.h>
#include <linux/init.h>

void* kbuff;
int init_module(void)
{

    kbuff = kzalloc(sizeof(char), GFP_KERNEL);
    pr_info("%p %llx\n",kbuff, (unsigned long long)kbuff);
    return 0;
}

void cleanup_module(void)
{
    kfree(kbuff);
}

The dmesg output comes out to be as follows

[67355.673465] 000000003aeb0247 ffff9ef657a58c00
like image 370
atuly Avatar asked Jul 06 '26 07:07

atuly


1 Answers

From the documentation of printk() (which pr_info calls):

Pointer Types

Pointers printed without a specifier extension (i.e unadorned %p) are hashed to give a unique identifier without leaking kernel addresses to user space. On 64 bit machines the first 32 bits are zeroed. If you really want the address see %px below.

So, this is a security measure. Use the %px format specifier to print the real address (which should match now)

like image 68
Ctx Avatar answered Jul 08 '26 01:07

Ctx