From c1fdb6f156f2854c9391028ae73594de626de69d Mon Sep 17 00:00:00 2001 From: Roger Peppe Date: Wed, 2 Mar 2011 15:04:08 -0500 Subject: [PATCH] goinstall: protect against malicious filenames. It was possible to make package run arbitrary commands when installing if its filenames contained make metacharacters. R=rsc, niemeyer CC=golang-dev https://golang.org/cl/4248041 --- src/cmd/goinstall/make.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/cmd/goinstall/make.go b/src/cmd/goinstall/make.go index 8d4d6c5d28..e2d99bb477 100644 --- a/src/cmd/goinstall/make.go +++ b/src/cmd/goinstall/make.go @@ -44,6 +44,9 @@ func domake(dir, pkg string, local bool) (err os.Error) { // installing as package pkg. It includes all *.go files in the directory // except those in package main and those ending in _test.go. func makeMakefile(dir, pkg string) ([]byte, os.Error) { + if !safeName(pkg) { + return nil, os.ErrorString("unsafe name: " + pkg) + } dirInfo, err := scanDir(dir, false) if err != nil { return nil, err @@ -58,16 +61,25 @@ func makeMakefile(dir, pkg string) ([]byte, os.Error) { cgoFiles := dirInfo.cgoFiles isCgo := make(map[string]bool, len(cgoFiles)) for _, file := range cgoFiles { + if !safeName(file) { + return nil, os.ErrorString("bad name: " + file) + } isCgo[file] = true } oFiles := make([]string, 0, len(dirInfo.cFiles)) for _, file := range dirInfo.cFiles { + if !safeName(file) { + return nil, os.ErrorString("unsafe name: " + file) + } oFiles = append(oFiles, file[:len(file)-2]+".o") } goFiles := make([]string, 0, len(dirInfo.goFiles)) for _, file := range dirInfo.goFiles { + if !safeName(file) { + return nil, os.ErrorString("unsafe name: " + file) + } if !isCgo[file] { goFiles = append(goFiles, file) } @@ -81,6 +93,17 @@ func makeMakefile(dir, pkg string) ([]byte, os.Error) { return buf.Bytes(), nil } +var safeBytes = []byte("+-./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz") + +func safeName(s string) bool { + for i := 0; i < len(s); i++ { + if c := s[i]; c < 0x80 && bytes.IndexByte(safeBytes, c) < 0 { + return false + } + } + return true +} + // makedata is the data type for the makefileTemplate. type makedata struct { Pkg string // package import path -- 2.48.1