I want to protect the memory that i get via malloc. I'm having some
problems.
char *mem = malloc(0x1000); //0x1000 is the page size
char *mem2 = malloc(0x1000); //0x1000 is the page size
When I do that assignment, malloc returns an address probably not
multiply of page size.
For example; &mem is 0x804a008 and &mem2 is 0x804b010.
If i gave that address to mprotect, it fails. But if i complete the
address to the multiply of page size (for example 0x804b000), it
succeeds. But this time it protects the whole page. (so from 0x804b000
to 0x804bfff). This result protects the area of mem2 variable. Also
mprotect function gets the length parameter of protection but even if
i gave less than the page size it accepts it as page size.
I also open a file and mmaped that file. This memory can be used with
mprotect with any length. This works fine. But I need to allocate the
memory from RAM not file.
I also tried to mmap the memory that i got via malloc without file but
there is no such use.
So I want to protect the memory that i get via malloc.
I will appriciate any help,
Thanks,
Gokay
Why don't you just use mmap(NULL, PROT_READ | PROT_WRITE, MAP_ANONYMOUS
| MAP_PRIVATE, -1, 0) instead of malloc() to allocate pages in memory ?
> I also open a file and mmaped that file. This memory can be used with
> mprotect with any length. This works fine. But I need to allocate the
> memory from RAM not file.
There's no difference. If you run out of RAM, the data is stored in a
file. If the file fits in RAM, it's kept in RAM.
For small amounts of data, a file is in RAM. For large amounts of
data, RAM is a file.
If you want the file not to have a name, you can do that too. Then
it's even more the same.
DS
> So I want to protect the memory that i get via malloc.
Other posters have suggested anonymous memory mappings. You might also
find the posix_memalign(3) function useful.
-- [mdw]
Never use mprotect() with memory returned by anything other than mmap().
--
Måns Rullgård
ma...@mansr.com
why?
But the example in the mprotect man page uses memalign.
From man mprotect:
buffer = memalign(pagesize, 4 * pagesize);
if (buffer == NULL)
handle_error("memalign");
printf("Start of region: 0x%lx\n", (long) buffer);
if (mprotect(buffer + pagesize * 2, pagesize,
PROT_NONE) == -1)
handle_error("mprotect");
According to Single UNIX Specification it is an undefined behavior.
"The behavior of this function [mprotect()] is unspecified if the
mapping was not established by a call to mmap()."
Pawel Dziepak
> Måns Rullgård wrote:
>> Mark Wooding <m...@distorted.org.uk> writes:
>>
>>> gokayto...@gmail.com writes:
>>>
>>>> So I want to protect the memory that i get via malloc.
>>> Other posters have suggested anonymous memory mappings. You might also
>>> find the posix_memalign(3) function useful.
>> Never use mprotect() with memory returned by anything other than
>> mmap().
>>
>
> why?
Because it is likely to interfere with malloc internals. If you
allocate full pages, and restore the protections before calling
free(), you should be safe of course.
--
Måns Rullgård
ma...@mansr.com