Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to mark a chunk of allocated memory readonly?

if I allocate some memory using malloc() is there a way to mark it readonly. So memcpy() fails if someone attempt to write to it?

This is connected to a faulty api design where users are miss-using a const pointer returned by a method GetValue() which is part of large memory structure. Since we want to avoid copying of large chunk of memory we return live pointer within a structured memory which is of a specific format. Now problem is that some user find hack to get there stuff working by writing to this memory directly and avoid SetValue() call that does allocation and properly handing in memory binary format that we have developed. Although there hack sometime work but sometime it causes memory access violation due to incorrect interpretation of control flags which has been overridden by user.

Educating user is one task but let say for now we want there code to fail.

I am just wondering if we can simply protect against this case.

For analogy assume someone get a blob column from sqlite statement and then write back to it. Although in case of sqlite it will not make sense but this somewhat happing in our case.

like image 818
particle Avatar asked Feb 18 '13 07:02

particle


People also ask

How do you allocate a block of memory?

When you use dynamic memory allocation you have the operating system designate a block of memory of the appropriate size while the program is running. This is done either with the new operator or with a call to the malloc function. The block of memory is allocated and a pointer to the block is returned.

What happens if dynamically allocated memory is not freed?

If dynamically allocated memory is not freed, it results in a memory leak and system will run out of memory.

How do I clear allocated memory with malloc?

You can use malloc() and new in the same program. But you cannot allocate an object with malloc() and free it using delete . Nor can you allocate with new and delete with free() or use realloc() on an array allocated by new .


2 Answers

On most hardware architectures you can only change protection attributes on entire memory pages; you can't mark a fragment of a page read-only.

The relevant APIs are:

  • mprotect() on Unix;
  • VirtualProtect() on Windows.

You'll need to ensure that the memory page doesn't contain anything that you don't want to make read-only. To do this, you'll either have to overallocate with malloc(), or use a different allocation API, such as mmap(), posix_memalign() or VirtualAlloc().

like image 110
NPE Avatar answered Sep 21 '22 16:09

NPE


Depends on the platform. On Linux, you could use mprotect() (http://linux.die.net/man/2/mprotect).

On Windows you might try VirtualProtect() (http://msdn.microsoft.com/en-us/library/windows/desktop/aa366898(v=vs.85).aspx). I've never used it though.

Edit: This is not a duplicate of NPE's answer. NPE originally had a different answer; it was edited later and mprotect() and VirtualProtect() were added.

like image 41
Nikos C. Avatar answered Sep 22 '22 16:09

Nikos C.