Source file src/cmd/go/internal/work/security.go

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Checking of compiler and linker flags.
     6  // We must avoid flags like -fplugin=, which can allow
     7  // arbitrary code execution during the build.
     8  // Do not make changes here without carefully
     9  // considering the implications.
    10  // (That's why the code is isolated in a file named security.go.)
    11  //
    12  // Note that -Wl,foo means split foo on commas and pass to
    13  // the linker, so that -Wl,-foo,bar means pass -foo bar to
    14  // the linker. Similarly -Wa,foo for the assembler and so on.
    15  // If any of these are permitted, the wildcard portion must
    16  // disallow commas.
    17  //
    18  // Note also that GNU binutils accept any argument @foo
    19  // as meaning "read more flags from the file foo", so we must
    20  // guard against any command-line argument beginning with @,
    21  // even things like "-I @foo".
    22  // We use load.SafeArg (which is even more conservative)
    23  // to reject these.
    24  //
    25  // Even worse, gcc -I@foo (one arg) turns into cc1 -I @foo (two args),
    26  // so although gcc doesn't expand the @foo, cc1 will.
    27  // So out of paranoia, we reject @ at the beginning of every
    28  // flag argument that might be split into its own argument.
    29  
    30  package work
    31  
    32  import (
    33  	"fmt"
    34  	"internal/lazyregexp"
    35  	"regexp"
    36  	"strings"
    37  
    38  	"cmd/go/internal/cfg"
    39  	"cmd/go/internal/load"
    40  )
    41  
    42  var re = lazyregexp.New
    43  
    44  var validCompilerFlags = []*lazyregexp.Regexp{
    45  	re(`-D([A-Za-z_][A-Za-z0-9_]*)(=[^@\-]*)?`),
    46  	re(`-U([A-Za-z_][A-Za-z0-9_]*)`),
    47  	re(`-F([^@\-].*)`),
    48  	re(`-I([^@\-].*)`),
    49  	re(`-O`),
    50  	re(`-O([^@\-].*)`),
    51  	re(`-W`),
    52  	re(`-W([^@,]+)`), // -Wall but not -Wa,-foo.
    53  	re(`-Wa,-mbig-obj`),
    54  	re(`-Wp,-D([A-Za-z_][A-Za-z0-9_]*)(=[^@,\-]*)?`),
    55  	re(`-Wp,-U([A-Za-z_][A-Za-z0-9_]*)`),
    56  	re(`-ansi`),
    57  	re(`-f(no-)?asynchronous-unwind-tables`),
    58  	re(`-f(no-)?blocks`),
    59  	re(`-f(no-)builtin-[a-zA-Z0-9_]*`),
    60  	re(`-f(no-)?common`),
    61  	re(`-f(no-)?constant-cfstrings`),
    62  	re(`-fdebug-prefix-map=([^@]+)=([^@]+)`),
    63  	re(`-fdiagnostics-show-note-include-stack`),
    64  	re(`-ffile-prefix-map=([^@]+)=([^@]+)`),
    65  	re(`-fno-canonical-system-headers`),
    66  	re(`-f(no-)?eliminate-unused-debug-types`),
    67  	re(`-f(no-)?exceptions`),
    68  	re(`-f(no-)?fast-math`),
    69  	re(`-f(no-)?inline-functions`),
    70  	re(`-finput-charset=([^@\-].*)`),
    71  	re(`-f(no-)?fat-lto-objects`),
    72  	re(`-f(no-)?keep-inline-dllexport`),
    73  	re(`-f(no-)?lto`),
    74  	re(`-fmacro-backtrace-limit=(.+)`),
    75  	re(`-fmessage-length=(.+)`),
    76  	re(`-f(no-)?modules`),
    77  	re(`-f(no-)?objc-arc`),
    78  	re(`-f(no-)?objc-nonfragile-abi`),
    79  	re(`-f(no-)?objc-legacy-dispatch`),
    80  	re(`-f(no-)?omit-frame-pointer`),
    81  	re(`-f(no-)?openmp(-simd)?`),
    82  	re(`-f(no-)?permissive`),
    83  	re(`-f(no-)?(pic|PIC|pie|PIE)`),
    84  	re(`-f(no-)?plt`),
    85  	re(`-f(no-)?rtti`),
    86  	re(`-f(no-)?split-stack`),
    87  	re(`-f(no-)?stack-(.+)`),
    88  	re(`-f(no-)?strict-aliasing`),
    89  	re(`-f(un)signed-char`),
    90  	re(`-f(no-)?use-linker-plugin`), // safe if -B is not used; we don't permit -B
    91  	re(`-f(no-)?visibility-inlines-hidden`),
    92  	re(`-fsanitize=(.+)`),
    93  	re(`-ftemplate-depth-(.+)`),
    94  	re(`-ftls-model=(global-dynamic|local-dynamic|initial-exec|local-exec)`),
    95  	re(`-fvisibility=(.+)`),
    96  	re(`-g([^@\-].*)?`),
    97  	re(`-m32`),
    98  	re(`-m64`),
    99  	re(`-m(abi|arch|cpu|fpu|tune)=([^@\-].*)`),
   100  	re(`-m(no-)?v?aes`),
   101  	re(`-marm`),
   102  	re(`-m(no-)?avx[0-9a-z]*`),
   103  	re(`-mcmodel=[0-9a-z-]+`),
   104  	re(`-mfloat-abi=([^@\-].*)`),
   105  	re(`-mfpmath=[0-9a-z,+]*`),
   106  	re(`-m(no-)?avx[0-9a-z.]*`),
   107  	re(`-m(no-)?ms-bitfields`),
   108  	re(`-m(no-)?stack-(.+)`),
   109  	re(`-mmacosx-(.+)`),
   110  	re(`-mios-simulator-version-min=(.+)`),
   111  	re(`-miphoneos-version-min=(.+)`),
   112  	re(`-mlarge-data-threshold=[0-9]+`),
   113  	re(`-mtvos-simulator-version-min=(.+)`),
   114  	re(`-mtvos-version-min=(.+)`),
   115  	re(`-mwatchos-simulator-version-min=(.+)`),
   116  	re(`-mwatchos-version-min=(.+)`),
   117  	re(`-mnop-fun-dllimport`),
   118  	re(`-m(no-)?sse[0-9.]*`),
   119  	re(`-m(no-)?ssse3`),
   120  	re(`-mthumb(-interwork)?`),
   121  	re(`-mthreads`),
   122  	re(`-mwindows`),
   123  	re(`-no-canonical-prefixes`),
   124  	re(`--param=ssp-buffer-size=[0-9]*`),
   125  	re(`-pedantic(-errors)?`),
   126  	re(`-pipe`),
   127  	re(`-pthread`),
   128  	re(`-?-std=([^@\-].*)`),
   129  	re(`-?-stdlib=([^@\-].*)`),
   130  	re(`--sysroot=([^@\-].*)`),
   131  	re(`-w`),
   132  	re(`-x([^@\-].*)`),
   133  	re(`-v`),
   134  }
   135  
   136  var validCompilerFlagsWithNextArg = []string{
   137  	"-arch",
   138  	"-D",
   139  	"-U",
   140  	"-I",
   141  	"-F",
   142  	"-framework",
   143  	"-include",
   144  	"-isysroot",
   145  	"-isystem",
   146  	"--sysroot",
   147  	"-target",
   148  	"-x",
   149  }
   150  
   151  var invalidLinkerFlags = []*lazyregexp.Regexp{
   152  	// On macOS this means the linker loads and executes the next argument.
   153  	// Have to exclude separately because -lfoo is allowed in general.
   154  	re(`-lto_library`),
   155  }
   156  
   157  var validLinkerFlags = []*lazyregexp.Regexp{
   158  	re(`-F([^@\-].*)`),
   159  	re(`-l([^@\-].*)`),
   160  	re(`-L([^@\-].*)`),
   161  	re(`-O`),
   162  	re(`-O([^@\-].*)`),
   163  	re(`-f(no-)?(pic|PIC|pie|PIE)`),
   164  	re(`-f(no-)?openmp(-simd)?`),
   165  	re(`-fsanitize=([^@\-].*)`),
   166  	re(`-flat_namespace`),
   167  	re(`-g([^@\-].*)?`),
   168  	re(`-headerpad_max_install_names`),
   169  	re(`-m(abi|arch|cpu|fpu|tune)=([^@\-].*)`),
   170  	re(`-mfloat-abi=([^@\-].*)`),
   171  	re(`-mmacosx-(.+)`),
   172  	re(`-mios-simulator-version-min=(.+)`),
   173  	re(`-miphoneos-version-min=(.+)`),
   174  	re(`-mthreads`),
   175  	re(`-mwindows`),
   176  	re(`-(pic|PIC|pie|PIE)`),
   177  	re(`-pthread`),
   178  	re(`-rdynamic`),
   179  	re(`-shared`),
   180  	re(`-?-static([-a-z0-9+]*)`),
   181  	re(`-?-stdlib=([^@\-].*)`),
   182  	re(`-v`),
   183  
   184  	// Note that any wildcards in -Wl need to exclude comma,
   185  	// since -Wl splits its argument at commas and passes
   186  	// them all to the linker uninterpreted. Allowing comma
   187  	// in a wildcard would allow tunneling arbitrary additional
   188  	// linker arguments through one of these.
   189  	re(`-Wl,--(no-)?allow-multiple-definition`),
   190  	re(`-Wl,--(no-)?allow-shlib-undefined`),
   191  	re(`-Wl,--(no-)?as-needed`),
   192  	re(`-Wl,-Bdynamic`),
   193  	re(`-Wl,-berok`),
   194  	re(`-Wl,-Bstatic`),
   195  	re(`-Wl,-Bsymbolic-functions`),
   196  	re(`-Wl,-O[0-9]+`),
   197  	re(`-Wl,-d[ny]`),
   198  	re(`-Wl,--disable-new-dtags`),
   199  	re(`-Wl,-e[=,][a-zA-Z0-9]+`),
   200  	re(`-Wl,--enable-new-dtags`),
   201  	re(`-Wl,--end-group`),
   202  	re(`-Wl,--(no-)?export-dynamic`),
   203  	re(`-Wl,-E`),
   204  	re(`-Wl,-framework,[^,@\-][^,]+`),
   205  	re(`-Wl,--hash-style=(sysv|gnu|both)`),
   206  	re(`-Wl,-headerpad_max_install_names`),
   207  	re(`-Wl,--no-undefined`),
   208  	re(`-Wl,--pop-state`),
   209  	re(`-Wl,--push-state`),
   210  	re(`-Wl,-R,?([^@\-,][^,@]*$)`),
   211  	re(`-Wl,--just-symbols[=,]([^,@\-][^,@]+)`),
   212  	re(`-Wl,-rpath(-link)?[=,]([^,@\-][^,]+)`),
   213  	re(`-Wl,-s`),
   214  	re(`-Wl,-search_paths_first`),
   215  	re(`-Wl,-sectcreate,([^,@\-][^,]+),([^,@\-][^,]+),([^,@\-][^,]+)`),
   216  	re(`-Wl,--start-group`),
   217  	re(`-Wl,-?-static`),
   218  	re(`-Wl,-?-subsystem,(native|windows|console|posix|xbox)`),
   219  	re(`-Wl,-syslibroot[=,]([^,@\-][^,]+)`),
   220  	re(`-Wl,-undefined[=,]([^,@\-][^,]+)`),
   221  	re(`-Wl,-?-unresolved-symbols=[^,]+`),
   222  	re(`-Wl,--(no-)?warn-([^,]+)`),
   223  	re(`-Wl,-?-wrap[=,][^,@\-][^,]*`),
   224  	re(`-Wl(,-z,(relro|now|(no)?execstack))+`),
   225  
   226  	re(`[a-zA-Z0-9_/].*\.(a|o|obj|dll|dylib|so|tbd)`), // direct linker inputs: x.o or libfoo.so (but not -foo.o or @foo.o)
   227  	re(`\./.*\.(a|o|obj|dll|dylib|so|tbd)`),
   228  }
   229  
   230  var validLinkerFlagsWithNextArg = []string{
   231  	"-arch",
   232  	"-F",
   233  	"-l",
   234  	"-L",
   235  	"-framework",
   236  	"-isysroot",
   237  	"--sysroot",
   238  	"-target",
   239  	"-Wl,-framework",
   240  	"-Wl,-rpath",
   241  	"-Wl,-R",
   242  	"-Wl,--just-symbols",
   243  	"-Wl,-undefined",
   244  }
   245  
   246  func checkCompilerFlags(name, source string, list []string) error {
   247  	checkOverrides := true
   248  	return checkFlags(name, source, list, nil, validCompilerFlags, validCompilerFlagsWithNextArg, checkOverrides)
   249  }
   250  
   251  func checkLinkerFlags(name, source string, list []string) error {
   252  	checkOverrides := true
   253  	return checkFlags(name, source, list, invalidLinkerFlags, validLinkerFlags, validLinkerFlagsWithNextArg, checkOverrides)
   254  }
   255  
   256  // checkCompilerFlagsForInternalLink returns an error if 'list'
   257  // contains a flag or flags that may not be fully supported by
   258  // internal linking (meaning that we should punt the link to the
   259  // external linker).
   260  func checkCompilerFlagsForInternalLink(name, source string, list []string) error {
   261  	checkOverrides := false
   262  	if err := checkFlags(name, source, list, nil, validCompilerFlags, validCompilerFlagsWithNextArg, checkOverrides); err != nil {
   263  		return err
   264  	}
   265  	// Currently the only flag on the allow list that causes problems
   266  	// for the linker is "-flto"; check for it manually here.
   267  	for _, fl := range list {
   268  		if strings.HasPrefix(fl, "-flto") {
   269  			return fmt.Errorf("flag %q triggers external linking", fl)
   270  		}
   271  	}
   272  	return nil
   273  }
   274  
   275  func checkFlags(name, source string, list []string, invalid, valid []*lazyregexp.Regexp, validNext []string, checkOverrides bool) error {
   276  	// Let users override rules with $CGO_CFLAGS_ALLOW, $CGO_CFLAGS_DISALLOW, etc.
   277  	var (
   278  		allow    *regexp.Regexp
   279  		disallow *regexp.Regexp
   280  	)
   281  	if checkOverrides {
   282  		if env := cfg.Getenv("CGO_" + name + "_ALLOW"); env != "" {
   283  			r, err := regexp.Compile(env)
   284  			if err != nil {
   285  				return fmt.Errorf("parsing $CGO_%s_ALLOW: %v", name, err)
   286  			}
   287  			allow = r
   288  		}
   289  		if env := cfg.Getenv("CGO_" + name + "_DISALLOW"); env != "" {
   290  			r, err := regexp.Compile(env)
   291  			if err != nil {
   292  				return fmt.Errorf("parsing $CGO_%s_DISALLOW: %v", name, err)
   293  			}
   294  			disallow = r
   295  		}
   296  	}
   297  
   298  Args:
   299  	for i := 0; i < len(list); i++ {
   300  		arg := list[i]
   301  		if disallow != nil && disallow.FindString(arg) == arg {
   302  			goto Bad
   303  		}
   304  		if allow != nil && allow.FindString(arg) == arg {
   305  			continue Args
   306  		}
   307  		for _, re := range invalid {
   308  			if re.FindString(arg) == arg { // must be complete match
   309  				goto Bad
   310  			}
   311  		}
   312  		for _, re := range valid {
   313  			if match := re.FindString(arg); match == arg { // must be complete match
   314  				continue Args
   315  			} else if match == "-Wl,--push-state" {
   316  				// Examples for --push-state are written
   317  				//     -Wl,--push-state,--as-needed
   318  				// Support other commands in the same -Wl arg.
   319  				args := strings.Split(arg, ",")
   320  				for _, a := range args[1:] {
   321  					a = "-Wl," + a
   322  					var found bool
   323  					for _, re := range valid {
   324  						if re.FindString(a) == a {
   325  							found = true
   326  							break
   327  						}
   328  					}
   329  					if !found {
   330  						goto Bad
   331  					}
   332  					for _, re := range invalid {
   333  						if re.FindString(a) == a {
   334  							goto Bad
   335  						}
   336  					}
   337  				}
   338  				continue Args
   339  			}
   340  		}
   341  		for _, x := range validNext {
   342  			if arg == x {
   343  				if i+1 < len(list) && load.SafeArg(list[i+1]) {
   344  					i++
   345  					continue Args
   346  				}
   347  
   348  				// Permit -Wl,-framework -Wl,name.
   349  				if i+1 < len(list) &&
   350  					strings.HasPrefix(arg, "-Wl,") &&
   351  					strings.HasPrefix(list[i+1], "-Wl,") &&
   352  					load.SafeArg(list[i+1][4:]) &&
   353  					!strings.Contains(list[i+1][4:], ",") {
   354  					i++
   355  					continue Args
   356  				}
   357  
   358  				// Permit -I= /path, -I $SYSROOT.
   359  				if i+1 < len(list) && arg == "-I" {
   360  					if (strings.HasPrefix(list[i+1], "=") || strings.HasPrefix(list[i+1], "$SYSROOT")) &&
   361  						load.SafeArg(list[i+1][1:]) {
   362  						i++
   363  						continue Args
   364  					}
   365  				}
   366  
   367  				if i+1 < len(list) {
   368  					return fmt.Errorf("invalid flag in %s: %s %s (see https://golang.org/s/invalidflag)", source, arg, list[i+1])
   369  				}
   370  				return fmt.Errorf("invalid flag in %s: %s without argument (see https://golang.org/s/invalidflag)", source, arg)
   371  			}
   372  		}
   373  	Bad:
   374  		return fmt.Errorf("invalid flag in %s: %s", source, arg)
   375  	}
   376  	return nil
   377  }
   378  

View as plain text