Bug 3637 - QVMs are allowed to dereference NULL
Status: RESOLVED WONTFIX
Alias: None
Product: ioquake3
Classification: Unclassified
Component: Misc
Version: unspecified
Hardware: Macintosh Linux
: P3 minor
Assignee: Zachary J. Slater
QA Contact: ioquake3 bugzilla mailing list
URL:
Depends on:
Blocks:
 
Reported: 2008-05-08 15:54 EDT by Ben Millwood
Modified: 2009-10-25 19:06:06 EDT
1 user (show)

See Also:


Attachments
Patch that catches NULL derefs from qvms (2.43 KB, patch)
2008-08-04 04:38 EDT, Amanieu d'Antras
Fixed patch (2.58 KB, patch)
2008-08-04 06:57 EDT, Amanieu d'Antras
Updated patch (2.67 KB, patch)
2008-08-23 07:45 EDT, Amanieu d'Antras

Description Ben Millwood 2008-05-08 15:54:15 EDT
The null pointer is guaranteed by C standards to never contain any valid object or function, but Quake's virtual machines seem to allow both writing to and reading from NULL without any obvious side-effects.

I suggest that if the QVM tries to dereference a null pointer the server binary should immediately terminate it with an appropriate error message.
Comment 1 Ludwig Nussel 2008-07-10 16:29:57 EDT
Can you quote the paragraph of the C standard that forbids the use of NULL? :)

It's not forbidden. It just doesn't normally work as the memory page add address zero is not mapped. If you manually map NULL (via mmap and MAP_FIXED, see mmap manpage) you can access that area just fine.
Comment 2 Tim Angus 2008-07-10 16:36:56 EDT
Regardless of the standards, any architecture I've ever come across will complain wildly if you deference NULL. It /should/ crash if such an operation is attempted since otherwise a bug is hidden. I don't think an irrelevant standard is justification for WONTFIX.

Comment 3 Amanieu d'Antras 2008-07-10 17:03:10 EDT
Fixing this would require special memory allocation for qvms in which the first page of memory is reserved. This can cause issues because page size varies between architectures and there is no portable API to do per-page memory allocation which guarantee that an unused page will cause a segfault.

On the other hand I agree that qvms _should_ crash when dereferencing NULL since many people don't test their mods with dll/so.
Comment 4 Ben Millwood 2008-07-10 19:24:01 EDT
Ludwig: I'm looking at an ISO C99 draft retrieved from http://www.open-std.org/jtc1/sc22/wg14/www/standards.html#9899

6.3.2.3 Pointers

[...]

3 An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.
Comment 5 Amanieu d'Antras 2008-07-11 05:40:23 EDT
(In reply to comment #0)
> Quake's virtual machines seem to allow both writing to and
> reading from NULL without any obvious side-effects.

There is a side effect to reading and writing to NULL. In qvms NULL will point to the begining of the data segment, which is the vmCvar_t memory at the begining of {g|cg|ui}_main.c. A simple way to fix this would be to allocate a 0 filled buffer right at the begining of the data section.
Comment 6 Amanieu d'Antras 2008-08-04 04:38:43 EDT
Created attachment 1813 [details]
Patch that catches NULL derefs from qvms
Comment 7 Amanieu d'Antras 2008-08-04 04:41:52 EDT
In order to keep compatibility with existing qvms I have not used a new magic number in the qvm header. Instead, I placed set the first byte of the data section to 1, as it is always 0 in current qvms.
Comment 8 Amanieu d'Antras 2008-08-04 06:57:27 EDT
Created attachment 1814 [details]
Fixed patch
Comment 9 Tim Angus 2008-08-23 06:19:02 EDT
There is potentially useful patch here, reopening.
Comment 10 Amanieu d'Antras 2008-08-23 07:45:42 EDT
Created attachment 1839 [details]
Updated patch

Maybe also allow this feature to be toggled by a cvar?
Comment 11 Amanieu d'Antras 2008-08-23 09:43:40 EDT
http://tremfusion.tremforges.net/hg/tremfusion/rev/72e11d3997f4
This checks for the page size using sysconf before mprotecting, which prevents useful data from being protected on architectures which have a page size larges than 4K.
Comment 12 Tim Angus 2009-10-25 19:06:06 EDT
Amanieu says WONTFIX.