> So the compiler is assigning the address of the static string “” to a
pointer to a UNICODE_STRING - that alone won’t cause a crash. Nor will
reading the memory pointed at by PUNICODE_STRING now that it is
initialized
to something that happens to be valid memory.
It shouldn’t even compile, because a const wchar_t is incompatible with
PUNICODE_STRING. Now, assume that by some magic, the assignment takes
place. Then it is pointing to some random place in memory, into which
there will be an attempt to write the two USHORTs and the Buffer value.
But string literals are normally placed in a write-protected segment, so
the attempt to write into that space should take an access fault. Assume
it has not been placed in write-protected memory, then it is pointing to a
one-byte location in memory (the NUL character of a zero-length char
literal). In Win32, it will write 8 bytes into that one-byte space; in
Win64 it would write 12 byes into that one-byte space. Oops. The error,
of course, is declaring it asa PUNICODE_STRING; it should be an
uninitialized UNICODE_STRING.
I was a bit surprised that this doesn’t crash:
RtlAnsiStringToUnicodeString(usProc, &asProc, TRUE);
But by specifying TRUE the function allocates the UNICODE_STRING buffer,
and clobbers the allocated space for the static string “” (and probably
the
string adjacent to it) with the updated UNICODE_STRING values.
Note that the UNICODE_STRING Buffer field must exist inside a valid
UNICODE_STRING object, which is not what the PUNICODE_STRING pointer is
pointing to. In user space, steing literals are linked into the code
segment, and the number of times I’ve had to explain why modifying a
pooled string constant was a Really Bad Idea was not quite weekly,
probably more like three times a month.
The code might win the coveted “Worst Code Sample Posted to NTDEV Evah”
prize.
I think I remember a similar-sized example in which EVERY line was wrong;
this one had two correct lines. Maybe you can claim the DbgPrint is
correct, but since it doesn’t include the driver name and doesn’t end with
a newline, I don’t consider it a good example of usage.
But I have a strong suspicion that getProcName function would win the
prize. What do you want to bet that function returns a pointer to a
stack-allocated local?
Mark Roddy
On Tue, May 28, 2013 at 2:11 PM, wrote:
>
>> In reading my answer, I just saw that the initialixation of usProc is
>> initializing a PUNICODE_STRING. This initialization is fatal nonsense.
>>
>> Weirdly, you claim “wcsstr doesn’t seem to work”. It’s not weird at
>> all.
>> And “doesn’t work” is a meaningless nonsense phrase.
>>
>> First: if anything is weird, it is that this compiles without errors or
>> warnings
>> Second: assuming you have disabled or ignored all the warnings, it
>> should
>> bluscreen; if it does not, that is amazingly weird.
>>
>> joe
>>
>> >> Hi everybody !
>> >>
>> >> I’m writing a driver and for some reasons, I need to search a string
>> >> into
>> >> another one.
>> >> The two strings are UNICODE_STRING but weirdly wcsstr doesn’t seem to
>> >> work…
>> >>
>> >> Do you know what could be wrong ?
>> >>
>> >> here is a piece of code :
>> >>
>> >> PUNICODE_STRING procName;
>> >> PUNICODE_STRING usProc = “”;
>> >> ANSI_STRING asProc;
>> >>
>> >> procName = getCurrentProcName();
>> >>
>> >> RtlInitAnsiString(&asProc, pWriteDataBuffer);
>> >> RtlAnsiStringToUnicodeString(usProc, &asProc, TRUE);
>> >>
>> >> if(wcsstr(procName, usProc))
>> >> DbgPrint(“yeah”);
>> >>
>> >>
>> > What surprises me is tbat this code even compiles. Do you even have a
>> > clue as to what a UNICODE_STRING is? RTFM! What, exactly, do you
>> think
>> > assigning “” to a PUNICODE_STRING is going to accomplish? What is
>> > pWriteDataBuffer and where did it come from? If it came from user
>> space,
>> > what leads you to believe it holds an ANSI string (most application
>> > programmers who have a clue think that ‘char’ is a quaint data type
>> left
>> > over from the PDP-11 version of C, but which has little relevance to
>> > modern programming; furthrrmore, by default, all VS projects are
>> created
>> > assuming Unicode). The conversion works because the last parameter is
>> > TRUE, so the erroneous initialization causes no harm. The
>> documentation,
>> > however, is strangely silent on the need to free this buffer, or the
>> > proper means of doing it. But wcsstr wants a pair of PCWSTRs, and
>> that
>> is
>> > not what you gave it. The compiler should have complained bitterly
>> about
>> > this. So any action taken by wcsstr on improper inputs is undefined.
>> > Furthermore, there is really no guarantee that UNICODE_STRINGs are
>> > NUL-terminated, so even if you used
>> > wcsstr(procName->Buffer, usProc->Buffer)
>> > there is no guarantee it would work correctly; it might bluescreen,
>> which
>> > would be a perfectly reasonable response to incorrect arguments.
>> >
>> > I suggest that reading documentation, learning to use /W4, and
>> learning
>> to
>> > use a debugger would all be valuable skills to acquire.
>> >
>> > There are, as far as I can tell, only two correct lines in the above
>> > sample: the declarations of “procName” and “asProc”. Since you do not
>> > show the code for getCurrentProcName, based on what I’ve seen here it
>> is
>> > very probably wrong.
>> > joe
>> >> Thanks in advance !
>> >>
>> >> —
>> >> NTDEV is sponsored by OSR
>> >>
>> >> OSR is HIRING!! See http://www.osr.com/careers
>> >>
>> >> For our schedule of WDF, WDM, debugging and other seminars visit:
>> >> http://www.osr.com/seminars
>> >>
>> >> To unsubscribe, visit the List Server section of OSR Online at
>> >> http://www.osronline.com/page.cfm?name=ListServer
>> >>
>> >
>> >
>> >
>> > —
>> > NTDEV is sponsored by OSR
>> >
>> > OSR is HIRING!! See http://www.osr.com/careers
>> >
>> > For our schedule of WDF, WDM, debugging and other seminars visit:
>> > http://www.osr.com/seminars
>> >
>> > To unsubscribe, visit the List Server section of OSR Online at
>> > http://www.osronline.com/page.cfm?name=ListServer
>> >
>>
>>
>>
>> —
>> NTDEV is sponsored by OSR
>>
>> OSR is HIRING!! See http://www.osr.com/careers
>>
>> For our schedule of WDF, WDM, debugging and other seminars visit:
>> http://www.osr.com/seminars
>>
>> To unsubscribe, visit the List Server section of OSR Online at
>> http://www.osronline.com/page.cfm?name=ListServer
>>
>
> —
> NTDEV is sponsored by OSR
>
> OSR is HIRING!! See http://www.osr.com/careers
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer