Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buffer overflow in msOWSParseRequestMetadata #4393

Open
constantinius opened this issue Jul 17, 2012 · 15 comments
Open

Buffer overflow in msOWSParseRequestMetadata #4393

constantinius opened this issue Jul 17, 2012 · 15 comments

Comments

@constantinius
Copy link
Contributor

Since version 6 of Mapserver a bug exists in msOWSParseRequestMetadata, resulting in a buffer overflow/stack smashing on 32 bit machines (the error may also be applicable for other architectures, but did not yet show).

The cause is the char requestBuffer[32]; where data is written over the boundaries of the array in some cases.

Example MapFile: http://pastebin.com/pTvyS4q6
Result: http://pastebin.com/Xp15Pkwd

@ghost ghost assigned aboudreault Jul 17, 2012
@sdlime
Copy link
Member

sdlime commented Jul 17, 2012

Need to confirm affected versions and get a fix together...

CC'ing @aboudreault @dmorissette @assefay

@dmorissette
Copy link
Contributor

I had a quick look at the code and your test case (without actually running it because I'm not setup at the moment)

Please note that the value of "wms_enable_request" should be space-delimited and not comma-delimited, so the quick fix for you would be to use:

"wms_enable_request"      "getcapabilities getmap getfeatureinfo"

A short term fix for the buffer overflow in 6.0.x could be to allocate requestBuffer to be of size strlen(metadata)+1 at the beginning of msOWSParseRequestMetadata() (and make sure it is freed before returning).

For the longer term 6.2 fix, this part of the code could probably be simplified with some refactoring. @tbonfort had some suggestions to that effect on the mapserver-dev list.

@tbonfort
Copy link
Member

No need for rushing out a 6.0.4 then, right ? I'm not milestoning a rewrite for 6.2 though as we are too far into the release process for this kind of change. A simple fix for 6.2 could be to allow a comma as a separator.

@tbonfort
Copy link
Member

This is my proposed change for msOWSParseRequestMetadata:

/* msOWSParseRequestMetadata 
 *  
 * This function parse a enable_request metadata string and check if the
 * given request is present and enabled.
 * 
 * returns:
 *  - MS_TRUE if the request was explicitely enabled
 *  - MS_FALSE if the request was explicitely disabled
 *  - MS_UNKNOWN if there was no entry for this request
 */
int msOWSParseRequestMetadata(const char *metadata, const char *request)
{
  int authorize = MS_UNKNOWN;
  char *ptr = NULL;

  ptr = strchr(metadata,'*');
  if(ptr) {
    if(ptr == metadata || *(ptr-1)!='!') {
      authorize = MS_TRUE;
    } else {
      /* found a '!*' */
      authorize = MS_FALSE;
    }
  }

  ptr = strcasestr(metadata,request);
  if(ptr) {
    if(ptr == metadata || *(ptr-1)!='!') {
      authorize = MS_TRUE;
    } else {
      /* found a '!request' */
      authorize = MS_FALSE;
    }
  }
  return authorize;
}

@constantinius
Copy link
Contributor Author

@dmorissette: The problem also persists when I use

"wms_enable_request"      "getcapabilities getmap getfeatureinfo"

So this is not a fix!

@tbonfort: Your solution looks simple and nice and is easily understandable in case of future edits.

@tbonfort
Copy link
Member

I have tried with

"wms_enable_request"      "getcapabilities getmap getfeatureinfo"

and get neither a segfault nor any related valgrind errors.

@constantinius
Copy link
Contributor Author

As I've said, the problem did only occur in special setups (like the LiveDVD). On my local machine I don't have problems either.

@tbonfort
Copy link
Member

@constantinius can you post the valgrind output of the crash please ?

@constantinius
Copy link
Contributor Author

@tbonfort: http://pastebin.com/r7Qc5Qqs

Not much additional info I fear.

@constantinius
Copy link
Contributor Author

@tbonfort: I only set the "wms_enable_request" "getcapabilities getmap getfeatureinfo" in the map.web.metadata not in the layer one.

Setting the field in the layer to that value actually solved the issue. But the function should still be redone, IMHO.

@sdlime
Copy link
Member

sdlime commented Jul 18, 2012

I agree on not rushing a 6.0.4. The overflow cannot be triggered externally, only though direct mapfile editing. -Steve

@dmorissette
Copy link
Contributor

Agreed with no need for 6.0.4. And I like @tbonfort's proposed new version of msOWSParseRequestMetadata().

@tbonfort
Copy link
Member

pushing this to 6.4

@tbonfort
Copy link
Member

demilestoning until someone feels the need to fix this

@jratike80
Copy link

The place to insert the patch is perhaps https://github.com/mapserver/mapserver/blob/branch-7-0/mapows.c#L743. I am not sure if this part of code is still the same, the history is https://github.com/mapserver/mapserver/commits/branch-7-0/mapows.c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants