Memory protection for lower 16K

Marco Baye Marco.Baye at tu-clausthal.de
Wed Feb 13 14:11:28 GMT 2002


Hi!

Yesterday I learned something that may be of interest for all who
program in C under RISC OS.

As you will know, the lower 16K of memory are a system area, and no
user program should mess with it (and the other 16K before the
application area starts are "scratch space". Whatever).

Anyway, the OS does not use memory protection in these lower 16K, so
if a C program uses an uninitialised or NULL pointer for write
access, it is likely to corrupt something valuable.

So by setting the appropriate bits in the MMU's page table, the
lower 16K of memory can be protected against accidental corruption.
A broken C program will now correctly crash with something like
"illegal address, e.g. wildly outside array bounds".

But that's not what I learned yesterday.


What I learned yesterday is this: Using the SharedCLibrary function

    char* tmpnam(char*);

causes a program to crash when the said protection is used. That's
because the function accesses memory location &FE8 in user mode.
By the way, the tmpnam() function is also called by

    FILE* tmpfile(void);

Now I know why I had to disable my protection before running some
programs.

Does anyone have a solution to this? I changed my protection program
so now it doesn't protect 16K, but 12K (leaving out the first page,
containing &fe8). But I think it'll be better to patch the shared C
lib module accordingly. Anyone done this already?

Oh, and if you want a copy of the protection thingy, don't hesitate
to say so. Just 325 bytes BBC BASIC - run on startup and feel a tiny
bit safer.

Cu,

Marco Baye

-- 
ntalk://Marco@dionin.esw-heim.tu-clausthal.de
65816 Cross assembler    (RISC OS,DOS/Win,Unix/Linux,AmigaOS,OS/2)
ntalk, CBM drive access  (RISC OS)
Mouse driver, boot maker (C-128)
=> http://home.pages.de/~mac_bacon/ <=

... Scotty, I've fallen and I can't beam up !



More information about the oslib-user mailing list