Re: Microsoft and memcpy?
- From: Paul Hsieh <websnarf@xxxxxxxxx>
- Date: Mon, 18 May 2009 11:39:11 -0700 (PDT)
On May 18, 11:08 am, rich...@xxxxxxxxxxxxxxx (Richard Tobin) wrote:
In article <gus6sc$sk...@xxxxxxxxxxxxxxxxx>,
Beej Jorgensen <b...@xxxxxxx> wrote:
As for memcpy_s(), I'm having trouble coming up with likely practical
scenarios where it saves you
The idea is presumably that it just provides an increased chance that
a mistake will be detected. It won't always work, because sometimes
the passed-in size will be inaccurate, but perhaps it works often
enough to be worthwhile in an operation the size of Microsoft's.
Internally, Microsoft uses "team foundation" which is a version of
Visual Studio which includes a tool called "PRE-fast". This tool is
similar to LINT. It catches any obvious problems with memcpy() with a
post-compile step analysis. I.e., it figures out all the "base
pointer" and offset arithmetic automatically then tries to figure out
if you are out of bounds.
With memcpy_s() all of a sudden half of that work has been thrust into
the programmer's lap to do a run-time check, while hardly any cases
become checked that could not have been checked at compile time. It
also introduces problems if the programmer miscalculates the base
pointer offset arithmetic, or introduces a wrap around problem as I
alluded to in my other post.
Look, the general thinking behind the _s functions is that programmers
are not thinking in terms of the length limits of their buffers,
therefore we are going to force them to. But in the case of memcpy()
that's not true and this new machinery is just getting in the way (and
that's even if you buy into the thinking behind the _s functions).
What bothers me is that these security efforts totally miss the real
problem. (And this is where Richard Heathfield and I clearly
disagree.) Programmers what to use what's *EASIEST* to use, or
equivalently, what accomplishes the most with the least effort. That's
why they use gets(), strtok() etc. (RH at this point would just prefer
to shoot the "bad" programmers, whereas I would prefer to turn them
into good programmers even if inadvertently.) But the C standard
library is not ideal in this regard by any stretch of the
imagination. So there is an obvious opportunity here, to try to
simultaneously solve the ease-of-use *AND* security issues
simultaneously. In fact, I claim, that you actually cannot solve one
without the other. I.e., so long as the easiest way to sort of get
something to do is unsafe or error prone, you will get unsafe and
error-prone solutions.
In the case of array copying, for example, you could make the
following syntax:
a[] = b[];
and all of a sudden, the "memcpy() problems" have just reduced
significantly. Is there some controversy, or ambiguity with the above
syntax? If a and b are not the same type (meaning base type and
length) then obviously the compiler would just emit and error and
stop; so it would help encourage strong typing in order to leverage
this easier syntax. It even gives the compiler the opportunity to
inline different kinds of copying functions depending on which
strategy would work best as determined at *COMPILE* time rather than
run time (which memcpy() is forced to do). Of course we could always
wrap these in structs, but that's cumbersome, and a brief survey will
show that that is not done in general. The point is that developers
*WILL* change their code in order to leverage this easier syntax which
will makes their code safer.
But of course, nobody bothers to propose something like this.
Certainly not Microsoft. Between Micrsoft's memcpy_s() and the "off
the top of my head" proposal above, which do you think would actually
reduce security incidences related to memcpy()?
--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/
.
- Follow-Ups:
- Re: Microsoft and memcpy?
- From: CBFalconer
- Re: Microsoft and memcpy?
- From: Richard Heathfield
- Re: Microsoft and memcpy?
- From: Eric Sosman
- Re: Microsoft and memcpy?
- From: Spiros Bousbouras
- Re: Microsoft and memcpy?
- References:
- Microsoft and memcpy?
- From: Rui Maciel
- Re: Microsoft and memcpy?
- From: Paul Hsieh
- Re: Microsoft and memcpy?
- From: Richard Heathfield
- Re: Microsoft and memcpy?
- From: Beej Jorgensen
- Re: Microsoft and memcpy?
- From: Richard Tobin
- Microsoft and memcpy?
- Prev by Date: Re: Microsoft and memcpy?
- Next by Date: Re: Microsoft and memcpy?
- Previous by thread: Re: Microsoft and memcpy?
- Next by thread: Re: Microsoft and memcpy?
- Index(es):
Relevant Pages
|