# \[patch\] buffer overflow in q\_parser.y

**URL:** <https://www.ruby-forum.com/t/patch-buffer-overflow-in-q-parser-y/69859>\
**Category:** Ferret\
**Created:** [September 25, 2006, 3:20am UTC](https://www.ruby-forum.com/t/patch-buffer-overflow-in-q-parser-y/69859 "2006-09-25T03:20:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![trucker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@trucker](https://www.ruby-forum.com/u/trucker)\
**Post date:** [September 25, 2006, 3:20am UTC](https://www.ruby-forum.com/t/patch-buffer-overflow-in-q-parser-y/69859/1 "2006-09-25T03:20:57Z")

</div>

Hi Dave,

The patch below corrects a buffer overflow bug in q\_parser.y. Since it  
is triggered by excessively long query strings, I believe that this bug  
could be exploited to allow arbitrary code execution if a query string  
supplied by a user is passed in directly to Ferret and not truncatated.  
If I’m right, you should consider a new release asap.

I’ve fixed it to simply allocate more memory if the standard buffers  
aren’t enough (because I had some long (i.e. \> 255) query strings that I  
needed to support), but there are other solutions as well.

# Index: c/include/search.h

— c/include/search.h (revision 615)  
+++ c/include/search.h (working copy)  
@@ -819,6 +819,7 @@  
char \*qstr;  
char \*qstrp;  
char buf[QP\_CONC\_WORDS][MAX\_WORD\_SIZE];

- char \*dynbuf;  
int buf\_index;  
HashTable \*field\_cache;  
HashSet \*fields;  
Index: c/src/q\_parser.y  
===================================================================  
— c/src/q\_parser.y (revision 615)  
+++ c/src/q\_parser.y (working copy)  
@@ -173,6 +173,11 @@  
char \*bufp = buf;  
qp-\>buf\_index = (qp-\>buf\_index + 1) % QP\_CONC\_WORDS;

- if (qp-\>dynbuf) {

- 

```
 free(qp->dynbuf);

```

- 

```
 qp->dynbuf = NULL;

```

- }

- qp-\>qstrp–; /\* need to back up one character \*/

- /\* we’ve exceeded the static buffer. switch to the dynamic

- 

```
 one. */

```

- 
- if (!qp-\>dynbuf && ((bufp - buf) == MAX\_WORD\_SIZE)) {

- 

```
  qp->dynbuf = calloc(strlen(qp->qstr) + 1, sizeof(char));

```

- 

```
  strncpy(qp->dynbuf, buf, MAX_WORD_SIZE);

```

- 

```
  buf = qp->dynbuf;

```

- 

```
  bufp = buf + MAX_WORD_SIZE;

```

- }  
}  
qp-\>qstrp–;  
/\* check for keywords. There are only four so we have a bit of a  
hack which  
@@ -262,7 +276,7 @@  
}  
mutex\_unlock(&qp-\>mutex);  
RAISE(PARSE\_ERROR, "couldn’t parse query ``%s’’. Error message  
"

- 

```
         " was %se", buf, (char *)msg);

```

- 

```
         "was: %s", buf, (char *)msg);

```

}  
return 0;  
}  
@@ -707,6 +721,9 @@  
if (self-\>tokenized\_fields) {  
hs\_destroy(self-\>tokenized\_fields);  
}
- if (self-\>dynbuf) {
- 

```
 free(self->dynbuf);

```

- }  
hs\_destroy(self-\>all\_fields);  
hs\_destroy(self-\>fields\_buf);  
h\_destroy(self-\>field\_cache);  
@@ -754,6 +771,7 @@  
self-\>analyzer = analyzer;  
self-\>ts\_cache = h\_new\_str(&free, (free\_ft)&ts\_deref);  
self-\>buf\_index = 0;
- self-\>dynbuf = NULL;  
self-\>non\_tokenizer = non\_tokenizer\_new();  
mutex\_init(&self-\>mutex, NULL);  
return self;

---

<div class="post-metadata">

**Author:** ![trucker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@trucker](https://www.ruby-forum.com/u/trucker)\
**Post date:** [September 25, 2006, 7:23am UTC](https://www.ruby-forum.com/t/patch-buffer-overflow-in-q-parser-y/69859/2 "2006-09-25T07:23:26Z")

</div>

On 9/25/06, William M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> needed to support), but there are other solutions as well.  
> int buf\_index;
> 
> - if (qp-\>dynbuf) {  
> }  
> qp-\>qstrp–;  
> @@ -707,6 +721,9 @@  
> self-\>analyzer = analyzer;  
> self-\>ts\_cache = h\_new\_str(&free, (free\_ft)&ts\_deref);  
> self-\>buf\_index = 0;
> - self-\>dynbuf = NULL;  
> self-\>non\_tokenizer = non\_tokenizer\_new();  
> mutex\_init(&self-\>mutex, NULL);  
> return self;

Thanks, I’ll release a new gem ASAP.

cheers,  
Dave
