How to make memory read-only in your C programs.

preview_player
Показать описание
---
How to make memory read-only in your C programs // most of the memory we use in our C programs is readable and writable, but sometimes you want to protect some memory and prevent yourself (or some buggy or malicious code) from changing it. This video shows you how.

Related Videos:

***

Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.

About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.

More about me and what I do:

To Support the Channel:
+ like, subscribe, spread the word

Рекомендации по теме
Комментарии
Автор

Thanks. You always make me think, today you taught me something too. Fifty years coding in C and there is always another trick.

greg
Автор

I don't think memory protection is part of the C specifications, it's more of an OS thing.
This is also doable in any language that can call OS APIs.
On Windows it's VirtualProtect (or the lower level NtProtectVirtualMemory)

FreeDomSy-nkue
Автор

Hi! Im new to C programming and I need to make a program which constantly shows in screen the word that the user types and can be stopped by the key word "END" like this:

Type a word: "hello"

Word is hello

Type a word: "bye"

Word is bye

Type a word: "END"

-

It is for a class exercise so it would be amazing if we could use simple functions to make it. Thanks for reading 🙏

pablo
Автор

These videos are super high quality content ❤

mshingote
Автор

Great video! Thank you very much Jacob! 🍀

starcw
Автор

4:44 Be careful! sysconf returns a 'long' data type which can overflow that declared 'integer' pagesize data type, under certain conditions.

Recommended security patch:

static constexpr long kPageSize = sysconf (_SC_PAGE_SIZE);

Then, if kPageSize fits within the INT_MIN and INT_MAX int data type range, then assign it to the local variable.

abdulshabazz
Автор

I remember your lesson about making C code more abstract, we can use this for structures to make their data unchangeable after iniciation. Unless we make special function which will make it possible

alkaratus
Автор

Out of curiosity. Have you ever done windows API programming in C? I've never done so i am curious about how it looks and feels.

lithiumferrate
Автор

I thought you just make your pointer point to const and it's done 😂

bombrix
Автор

couldn't you have used aligned_alloc from stdlib? what are the differences?

MatthisDayer
Автор

Just a quick question is there any limitation on branching out else if's (or, similarly, case's on switch) ?
i.e., if (main cond).. else if (cond 1); else if (cond 2); .. else if (cond n). max number of lines/statements, n? Any thoughts? Thanks, .

mrmfloy
Автор

If only the compiler had some keyword to help with this...

Enomem
Автор

Great! finally I can make own ram drive. What happens when program exists? Would it be still marked read-only? Say one program initialized ram drive and stayed resident in memory. How could another program access/modify the content of proposed ram drive?

mrmfloy
Автор

Would you recommend this for ephemeral encryption keys? Can the memory page still be swapped out? Is it protected against malicious software in kernel-space; for example, root-kits? POSIX is just an API front-end, right? Is an OS's POSIX sub-system verified by independent groups? Can we ever be sure whats happening to these 'protected' spaces?

Jim-vrlx
Автор

Hmm used malloc but didn't use free.

kermitdafrog
Автор

So is a segmentation fault always related to a read-only portion of a page in (virtual) memory being written to or are there other causes too?

duneharv
Автор

make videos about binary programming, or more low level programming

rianxFFF
Автор

9:04 why is p3 6 bytes long while p1 and p2 are 4?
Or are all 3 of them like, 8 bytes long and the leading zeros are just omitted?

drdca
Автор

Hmm, in real world code I would want something else than a segfault. I guess one could capture the signal somehow and do some error handling.

cernejr
Автор

One thing that no one ever covers though: how do I make memory write-only without having to implement a software solution myself, even if it's cheap? Every cycle counts.

andrewporter