Buffer overflow in sw renderer draw code

Forum rules
Please don't bump threads here if you have a problem - it will often be forgotten about if you do. Instead, make a new thread here.

Post a reply

Smilies
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :geek: :ugeek: :!: :?: :idea: :arrow: :| :mrgreen: :3: :wub: >:( :blergh:
View more smilies

BBCode is OFF
Smilies are ON

Topic review
   

Expand view Topic review: Buffer overflow in sw renderer draw code

Re: Buffer overflow in sw renderer draw code

by Edward-san » Sat Nov 05, 2016 4:49 am

I can confirm everything looks okay now.

Re: Buffer overflow in sw renderer draw code

by dpJudas » Sat Nov 05, 2016 4:38 am

I believe this one is fixed by the new wallscan function.

Re: Buffer overflow in sw renderer draw code

by Edward-san » Wed Jun 08, 2016 1:30 pm

It see nothing out of ordinary, thanks.

Re: [>=g240ca2af] Buffer overflow in wallscan code (again)

by wildweasel » Wed Jun 08, 2016 12:48 pm

Edward-san wrote:... these posts should go to the right bug report. Any moderator available?
A minor bout of thread-surgery later, I think I got it. I hope I didn't miss anything in the process.

Re: [>=g240ca2af] Buffer overflow in wallscan code (again)

by Edward-san » Wed Jun 08, 2016 12:28 pm

... these posts should go to the right bug report. Any moderator available?

Re: [>=g240ca2af] Buffer overflow in wallscan code (again)

by dpJudas » Wed Jun 08, 2016 12:06 pm

OK, further looking at this the problem seem be related to loss of precision when doing floating point math.

The 'bot' argument to wallscan_np2 is -0.0 and the partition value calculated becomes 0.0. The while (partition > bot) therefore runs one iteration too many. Probably will have to go through all the wallscan functions with a fine-comb to make sure all float to fixed conversions are correctly clamped and rounded.

Re: [>=g240ca2af] Buffer overflow in wallscan code (again)

by dpJudas » Wed Jun 08, 2016 11:12 am

Scratch that part about the 171 pixels. I forgot that its doing a stretch of the source texture. Either way, wallscan_np2 is probably the offending function.

Re: [>=g240ca2af] Buffer overflow in wallscan code (again)

by dpJudas » Wed Jun 08, 2016 11:11 am

According to my debugger the texture crashing for me right now is called DOOR3. It is 64x72 and it attempts to do a wallscan 'dc_count' run of 171 pixels.

If I understand the comments in the code right, wallscan is never supposed to do a run more than the height of the texture for non-power-of-two textures. I think that its the wallscan_np2 function that somehow fails to break up the run. That's the function I'm looking at right now. :)

Re: [>=g240ca2af] Buffer overflow in wallscan code (again)

by Edward-san » Wed Jun 08, 2016 11:05 am

Is it there a way to track down the involved texture?

Re: [>=g240ca2af] Buffer overflow in wallscan code (again)

by dpJudas » Wed Jun 08, 2016 10:42 am

Yep, that one matches my call stack a lot better. I'm able to consistently trigger it by moving around in the first room of E1M1 if I add the following asserts to vlinec1:

Code: Select all

	DWORD fracstep = dc_iscale;
	DWORD frac = dc_texturefrac;
	DWORD height = rw_pic->GetHeight();
	assert((frac >> vlinebits) < height);
	frac += dc_count * fracstep;
	assert((frac >> vlinebits) < height);
I tried adding some clamping to the wallscan function ala this:

Code: Select all

dc_texturefrac = xs_ToFixed(fracbits, MAX(dc_texturemid + iscale * (y1ve[0] - CenterY + 0.5), 0.0));
This causes my second assert to fail instead of the first one (with a frac value of exactly height). It would seem that the height of the run is exactly one pixel too much in some condition I've not yet tracked down. I think the crash only shows itself if the texture height is a non power-of-two due to the vlinebits shifting.

Re: [>=g240ca2af] Buffer overflow in wallscan code (again)

by Edward-san » Wed Jun 08, 2016 10:09 am

I guess you meant here.

Re: [>=g240ca2af] Buffer overflow in wallscan code (again)

by dpJudas » Wed Jun 08, 2016 9:29 am

Oops, probably the wrong thread to type this. But the crash is real. ;)

Re: [>=g240ca2af] Buffer overflow in wallscan code (again)

by dpJudas » Wed Jun 08, 2016 9:25 am

I think I may have managed to trigger this in my truecolor branch. At least I detected a buffer underflow as illustrated by the following debugger output:

Image

As you can see, dc_texturefrac somehow manages to have a negative value, which in turn causes it do a buffer overrun. Depending on where the column is in the texture it can cause zdoom to crash.

Re: Buffer overflow in sw renderer draw code

by Edward-san » Tue Feb 02, 2016 1:45 pm

I admit the tag error seems weird, considering that when I run the normal zdoom program in debug mode, the value is not uninitialized and matches the data in the function FTagManager::AddLineID, so ignore that for now.

Now the only thing left is the buffer overflow thing, which seems to be reported by both the address sanitizer and valgrind. Just to clarify: I noticed this in the same period of time as the right edge error.

Re: Buffer overflow in sw renderer draw code

by Edward-san » Mon Feb 01, 2016 2:11 am

Nope, I noticed this also before...

Top