Commit a825e8a6 authored by Gustavo Niemeyer's avatar Gustavo Niemeyer

cgo: restrict #cgo directives to prevent shell expansion

Fixes issue #1879.

Directives were not directly expanded, but since their
content ended up in makefiles, further expansion would
take place there.  This prevents such artifacts by
restricting the set of characters that may be used in
a directive value.

To build the list of safe characters I went through the
contents of /usr/lib/pkgconfig and extracted LDFLAGS
and CFLAGS information, so hopefully this is a
reasonable default to get started.

R=rsc
CC=golang-dev
https://golang.org/cl/4532092
parent a1d2cbf6
......@@ -104,6 +104,11 @@ NextLine:
if err != nil {
fatalf("%s: bad #cgo option %s: %s", srcfile, k, err)
}
for _, arg := range args {
if !safeName(arg) {
fatalf("%s: #cgo option %s is unsafe: %s", srcfile, k, arg)
}
}
switch k {
......@@ -144,7 +149,7 @@ func (p *Package) addToFlag(flag string, args []string) {
// for packages.
func pkgConfig(packages []string) (cflags, ldflags []string, err os.Error) {
for _, name := range packages {
if len(name) == 0 || !safeName(name) || name[0] == '-' {
if len(name) == 0 || name[0] == '-' {
return nil, nil, os.NewError(fmt.Sprintf("invalid name: %q", name))
}
}
......@@ -231,7 +236,7 @@ func splitQuoted(s string) (r []string, err os.Error) {
return args, err
}
var safeBytes = []byte("+-./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz")
var safeBytes = []byte("+-.,/0123456789=ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz")
func safeName(s string) bool {
if s == "" {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment