C – kernel will return to zero?

I am using Debian Squeeze and noticed that the memory is always zero. Is this a new feature in the Linux distribution? Some time ago, I believe I can use puts() and output garbage.

I ran this test program many times, but the results of the comments are always the same. (I have in sysctl.conf randomize_va_space = 2, so I know that each time I run it will use a different location of memory.)

char *a = malloc(50000000);
a[49999999] ='';
puts(a); // it outputs nothing since all are zeroes
printf("%p ", a);
if(a[5000] =='') // this condition is always true
{
puts("It is a nul char.");
}

Is it possible Make the system memory not zero? What options does this Debian squeeze install have to activate always zero memory?

In any modern operating system, the only way that newly acquired memory will contain non-zero values ​​is The memory previously released by the program is reused by malloc. When new memory is obtained from the operating system (kernel), it is initially purely virtual. It has no physical existence; instead, it is mapped as a copy-on-write mapping of a single shared memory page, It contains 0 bytes. On the first attempt to write, the kernel will capture the write, allocate a new physical memory page, copy the contents of the original page (0 bytes in this case) to the new page, and then restore Your plan. If the kernel knows that the newly allocated physical memory has been zero-filled, it can even optimize the copy step.

This program is both necessary and effective. This is necessary because it will probably contain data from Handing over the memory of the private data of the kernel or other user processes to your process would be a serious security hole. This is effective because zeroing is not performed at the time of allocation; “zero-filled” pages are just references to shared zero pages.

I am using Debian Squeeze and noticed that the memory is always zero. Is this a new feature in the Linux distribution? Some time ago, I believe I can use puts() and output garbage.

I ran this test program many times, but the results of the comments are always the same. (I have in sysctl.conf randomize_va_space = 2, so I know that each time I run it will use a different location of memory.)

char *a = malloc(50000000);
a[49999999] ='';
puts(a); // it outputs nothing since all are zeroes
printf("%p ", a);
if(a[5000] =='') // this condition is always true
{
puts("It is a nul char.");
}

Is it possible Make the system memory not zero? What options does this Debian squeeze install have to activate always zero memory?

In any modern operating system, the only way that newly acquired memory will contain a non-zero value is for the memory previously released by the program to be reused by malloc. When the operating system (kernel) gets the new memory, it is initially purely virtual. It has no physical existence; instead, it is mapped as a copy-on-write mapping of a single shared memory page, which contains 0 bytes. The first attempt to write Upon entry, the kernel will capture the write, allocate a new page of physical memory, copy the contents of the original page (0 bytes in this case) to the new page, and then resume your plan. If the kernel knows the newly allocated physical memory With zero padding, it can even optimize the copy step.

This program is both necessary and effective. This is necessary because the memory that may contain private data from the kernel or other user processes is handed over to Your process will be a serious security hole. This is effective because zeroing is not performed during allocation; the “zero-filled” page is just a reference to the shared zero page.

< /p>

Leave a Comment

Your email address will not be published.