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

go/vt/sqlparser: index out of range #767

Closed
dvyukov opened this issue Jun 7, 2015 · 6 comments
Closed

go/vt/sqlparser: index out of range #767

dvyukov opened this issue Jun 7, 2015 · 6 comments

Comments

@dvyukov
Copy link
Contributor

dvyukov commented Jun 7, 2015

The following program crashes with the panic:

package main

import (
    "github.com/youtube/vitess/go/vt/sqlparser"
)

func main() {
    sql := "SET o=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0=t(0"
    sqlparser.Parse(sql)
}
panic: runtime error: index out of range

goroutine 1 [running]:
github.com/youtube/vitess/go/vt/sqlparser.yyParse(0x7fd9d9e3bc08, 0xc208066000, 0x662df8)
    src/github.com/youtube/vitess/go/vt/sqlparser/yaccpar:207 +0xd7c1
github.com/youtube/vitess/go/vt/sqlparser.Parse(0x5cd9f0, 0xc9, 0x0, 0x0, 0x0, 0x0)
    src/github.com/youtube/vitess/go/vt/sqlparser/ast.go:31 +0xa0
main.main()
    sql.go:9 +0x40

on commit

discovered with https://github.com/dvyukov/go-fuzz

@enisoc
Copy link
Member

enisoc commented Jun 10, 2015

@sougou This appears to be a bug in the yacc-generated code. Is there anything we can do? Try a newer version of yacc?

@sougou
Copy link
Contributor

sougou commented Jun 10, 2015

I've been meaning to look at it once I'm done with my benchmark work. Looking at the test, it most likely is a stack overflow due high level of nesting. I'll either have to change to grammar to reduce more aggressively rather than shifting, or limit the nesting count and return a meaningful error.
I don't think yacc has changed in 20 years :).

@enisoc
Copy link
Member

enisoc commented Jun 10, 2015

Well, the "go tool yacc" version has been improving: https://github.com/golang/go/commits/master/src/cmd/yacc :)

@dvyukov
Copy link
Contributor Author

dvyukov commented Jun 16, 2015

Here are two more reproducers with slightly different stacks (one of them is probably the same as original):

"SELECT(F(F(F(F(F(F(F" +
    "(F(F(F(F(F(F(F(F(F(F" +
    "(F(F(F(F(F(F(F(F(F(F" +
    "(F(F(F(F(F(F(F(F(F(F" +
    "(F(F(F(F(F(F(F(F(F(F" +
    "(F(F(F(F(F(F(F(F(F(F" +
    "(F(F(F(F(F(F(F(F(F(F" +
    "(F(F(F(F(F(F(F(F(F(F" +
    "(F(F(F(F(F(F(F(F(F(F" +
    "(F(F(F(F(F(F(F(F(F(F" +
    "B,:B,:A\t(F("
"SELECT(F(F(F(F(F(F(F" +
    "(F(F(F(F(F(F(F(F(F(F" +
    "(F(F(F(F(F(F(F(F(F(F" +
    "(F(F(F(F(F(F(F(F(F(F" +
    "(F(F(F(F(F(F(F(F(F(F" +
    "(F(F(F(F(F(F(F(F(F(F" +
    "(F(F(F(F(F(F(F(F(F(F" +
    "(F(F(F(F(F(F(F(F(F(F" +
    "(F(F(F(F(F(F(F(F(F(F" +
    "(F(F(F(F(F(F(F(F(F(F" +
    "(0"

@sougou
Copy link
Contributor

sougou commented Jul 2, 2015

I don't know what changed, but I wasn't able to reproduce this from the latest pull. In any case, I have a CL that limits nesting of anything parenthesized to a fixed number. I'll hard code that to 200.
It's possible that there are other nestable constructs in the grammar that don't use parenthesis. We can cross those bridges as they come up.

sougou added a commit that referenced this issue Jul 2, 2015
This addresses #767
as well as other situations where there's possibility of
indefinite nesting of SQL constructs. There may be other
non-aprentheszed constructs that allow nesting, but this fix
doesn't address them for now.
I've also made a few lint fixes. sql.go is still in violation,
but that requires bigger work.
@sougou
Copy link
Contributor

sougou commented Jul 2, 2015

#845

@sougou sougou closed this as completed Jul 4, 2015
maxencoder pushed a commit to maxencoder/mixer that referenced this issue Oct 3, 2016
Otherwise it's yacc generated code is vulnerable to DOS.

vitessio/vitess#767
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants