RajSanpui
RajSanpui

Reputation: 12064

Is there any API or method to prevent read access (and write) on a dynamic allocated memory?

Is there any API or method to prevent read access on a dynamic allocated memory?

char *ptr = malloc(4);`
strcpy(ptr, "Hello");`

Now, i wish to ptr to have no read access and write-protected. How to accomplish this?

I do not wish to use mprotect as it will expect ptr to point to mapped memory, and ampping a dynamic memory every time may not be possible.

Upvotes: 2

Views: 223

Answers (1)

Simon Richter
Simon Richter

Reputation: 29598

mprotect is still the answer. You need a page-aligned allocation with no allocator control information that may need to be writeable -- so allocate your memory by mmaping anonymous memory.

Upvotes: 5

Related Questions