Results 1 to 4 of 4

Thread: Buffers & security question

  1. #1

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Buffers & security question

    So it's been a loooooong time since I've properly done any Win32 API stuff. Years ago, samples like these (& my code too) were commonplace: http://allapi.mentalis.org/apilist/EnumPrinters.shtml# or http://word.mvps.org/faqs/macrosvba/...lePrinters.htm.

    In essence:
    [LIST][1]Try an API call once, passing a dummy value for one of the byref buffer-length related parameters
    [2]Check the return value - if it didn't work, use the retrieved buffer length (now known) & just call the same API call again[/ LIST]

    Um, so wouldn't this cause one of those nasty buffer overflow security leak errors I've heard so much about in the news, causing potentially full remote control? What's the 2016 preferred way of coding & calling APIs with buffer length parameters please?

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Buffers & security question

    Quote Originally Posted by alex_read View Post
    Um, so wouldn't this cause one of those nasty buffer overflow security leak errors I've heard so much about in the news, causing potentially full remote control?
    If you're passing the correct size of your buffer (or have allocated the recommended size), then I don't see how that could cause a buffer overrun. Unless an API has a bug, it won't write more bytes than you've specified (or more than the recommended size).

    Quote Originally Posted by alex_read View Post
    What's the 2016 preferred way of coding & calling APIs with buffer length parameters please?
    It is (usually) same as before. APIs typically do not change their calling behavior because doing so would break programs that depended on those behaviors. MSDN usually documents the recommended steps of filling a buffer in the API's documentation page, so it would be best to follow that.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Buffers & security question

    In my opinion, not fully understanding the API call is the biggest reason for buffer overruns. Each API is defined and parameters are explained thru documentation, i.e., MSDN.

    Some APIs can return the required buffer length in a passed parameter or as the return value of the function. If via a passed parameter, ensure you are passing that parameter ByRef or passing its VarPtr(); otherwise, the called function can actually trigger the crash trying to write to unallocated/protected memory.

    Some APIs may require you to set one or more parameters to null in order to know to return a required buffer size. While others may require you to call a completely different API to get the length, i.e., GetWindowTextLength.

    Some APIs will tell you if the buffer size is too small based on a parameter that contains the buffer size. Of course, if you pass a parameter that says a buffer is of a set size, it best be at least that size, else the called function can trigger a crash.

    Bottom line, know the API function you are calling. Many freebie examples on the net may be incorrect.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: Buffers & security question

    Oh ok perfect - thanks both for the helpful advice & answers!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width