Black Lives Matter. Support the Equal Justice Initiative.

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

Documentation: cmd/go/internal/work

     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(`-fdiagnostics-show-note-include-stack`),
    63  	re(`-f(no-)?eliminate-unused-debug-types`),
    64  	re(`-f(no-)?exceptions`),
    65  	re(`-f(no-)?fast-math`),
    66  	re(`-f(no-)?inline-functions`),
    67  	re(`-finput-charset=([^@\-].*)`),
    68  	re(`-f(no-)?fat-lto-objects`),
    69  	re(`-f(no-)?keep-inline-dllexport`),
    70  	re(`-f(no-)?lto`),
    71  	re(`-fmacro-backtrace-limit=(.+)`),
    72  	re(`-fmessage-length=(.+)`),
    73  	re(`-f(no-)?modules`),
    74  	re(`-f(no-)?objc-arc`),
    75  	re(`-f(no-)?objc-nonfragile-abi`),
    76  	re(`-f(no-)?objc-legacy-dispatch`),
    77  	re(`-f(no-)?omit-frame-pointer`),
    78  	re(`-f(no-)?openmp(-simd)?`),
    79  	re(`-f(no-)?permissive`),
    80  	re(`-f(no-)?(pic|PIC|pie|PIE)`),
    81  	re(`-f(no-)?plt`),
    82  	re(`-f(no-)?rtti`),
    83  	re(`-f(no-)?split-stack`),
    84  	re(`-f(no-)?stack-(.+)`),
    85  	re(`-f(no-)?strict-aliasing`),
    86  	re(`-f(un)signed-char`),
    87  	re(`-f(no-)?use-linker-plugin`), // safe if -B is not used; we don't permit -B
    88  	re(`-f(no-)?visibility-inlines-hidden`),
    89  	re(`-fsanitize=(.+)`),
    90  	re(`-ftemplate-depth-(.+)`),
    91  	re(`-fvisibility=(.+)`),
    92  	re(`-g([^@\-].*)?`),
    93  	re(`-m32`),
    94  	re(`-m64`),
    95  	re(`-m(abi|arch|cpu|fpu|tune)=([^@\-].*)`),
    96  	re(`-m(no-)?v?aes`),
    97  	re(`-marm`),
    98  	re(`-m(no-)?avx[0-9a-z]*`),
    99  	re(`-mfloat-abi=([^@\-].*)`),
   100  	re(`-mfpmath=[0-9a-z,+]*`),
   101  	re(`-m(no-)?avx[0-9a-z.]*`),
   102  	re(`-m(no-)?ms-bitfields`),
   103  	re(`-m(no-)?stack-(.+)`),
   104  	re(`-mmacosx-(.+)`),
   105  	re(`-mios-simulator-version-min=(.+)`),
   106  	re(`-miphoneos-version-min=(.+)`),
   107  	re(`-mtvos-simulator-version-min=(.+)`),
   108  	re(`-mtvos-version-min=(.+)`),
   109  	re(`-mwatchos-simulator-version-min=(.+)`),
   110  	re(`-mwatchos-version-min=(.+)`),
   111  	re(`-mnop-fun-dllimport`),
   112  	re(`-m(no-)?sse[0-9.]*`),
   113  	re(`-m(no-)?ssse3`),
   114  	re(`-mthumb(-interwork)?`),
   115  	re(`-mthreads`),
   116  	re(`-mwindows`),
   117  	re(`--param=ssp-buffer-size=[0-9]*`),
   118  	re(`-pedantic(-errors)?`),
   119  	re(`-pipe`),
   120  	re(`-pthread`),
   121  	re(`-?-std=([^@\-].*)`),
   122  	re(`-?-stdlib=([^@\-].*)`),
   123  	re(`--sysroot=([^@\-].*)`),
   124  	re(`-w`),
   125  	re(`-x([^@\-].*)`),
   126  	re(`-v`),
   127  }
   128  
   129  var validCompilerFlagsWithNextArg = []string{
   130  	"-arch",
   131  	"-D",
   132  	"-U",
   133  	"-I",
   134  	"-framework",
   135  	"-include",
   136  	"-isysroot",
   137  	"-isystem",
   138  	"--sysroot",
   139  	"-target",
   140  	"-x",
   141  }
   142  
   143  var validLinkerFlags = []*lazyregexp.Regexp{
   144  	re(`-F([^@\-].*)`),
   145  	re(`-l([^@\-].*)`),
   146  	re(`-L([^@\-].*)`),
   147  	re(`-O`),
   148  	re(`-O([^@\-].*)`),
   149  	re(`-f(no-)?(pic|PIC|pie|PIE)`),
   150  	re(`-f(no-)?openmp(-simd)?`),
   151  	re(`-fsanitize=([^@\-].*)`),
   152  	re(`-flat_namespace`),
   153  	re(`-g([^@\-].*)?`),
   154  	re(`-headerpad_max_install_names`),
   155  	re(`-m(abi|arch|cpu|fpu|tune)=([^@\-].*)`),
   156  	re(`-mfloat-abi=([^@\-].*)`),
   157  	re(`-mmacosx-(.+)`),
   158  	re(`-mios-simulator-version-min=(.+)`),
   159  	re(`-miphoneos-version-min=(.+)`),
   160  	re(`-mthreads`),
   161  	re(`-mwindows`),
   162  	re(`-(pic|PIC|pie|PIE)`),
   163  	re(`-pthread`),
   164  	re(`-rdynamic`),
   165  	re(`-shared`),
   166  	re(`-?-static([-a-z0-9+]*)`),
   167  	re(`-?-stdlib=([^@\-].*)`),
   168  	re(`-v`),
   169  
   170  	// Note that any wildcards in -Wl need to exclude comma,
   171  	// since -Wl splits its argument at commas and passes
   172  	// them all to the linker uninterpreted. Allowing comma
   173  	// in a wildcard would allow tunnelling arbitrary additional
   174  	// linker arguments through one of these.
   175  	re(`-Wl,--(no-)?allow-multiple-definition`),
   176  	re(`-Wl,--(no-)?allow-shlib-undefined`),
   177  	re(`-Wl,--(no-)?as-needed`),
   178  	re(`-Wl,-Bdynamic`),
   179  	re(`-Wl,-berok`),
   180  	re(`-Wl,-Bstatic`),
   181  	re(`-Wl,-Bsymbolic-functions`),
   182  	re(`-Wl,-O([^@,\-][^,]*)?`),
   183  	re(`-Wl,-d[ny]`),
   184  	re(`-Wl,--disable-new-dtags`),
   185  	re(`-Wl,-e[=,][a-zA-Z0-9]*`),
   186  	re(`-Wl,--enable-new-dtags`),
   187  	re(`-Wl,--end-group`),
   188  	re(`-Wl,--(no-)?export-dynamic`),
   189  	re(`-Wl,-E`),
   190  	re(`-Wl,-framework,[^,@\-][^,]+`),
   191  	re(`-Wl,--hash-style=(sysv|gnu|both)`),
   192  	re(`-Wl,-headerpad_max_install_names`),
   193  	re(`-Wl,--no-undefined`),
   194  	re(`-Wl,-R([^@\-][^,@]*$)`),
   195  	re(`-Wl,--just-symbols[=,]([^,@\-][^,@]+)`),
   196  	re(`-Wl,-rpath(-link)?[=,]([^,@\-][^,]+)`),
   197  	re(`-Wl,-s`),
   198  	re(`-Wl,-search_paths_first`),
   199  	re(`-Wl,-sectcreate,([^,@\-][^,]+),([^,@\-][^,]+),([^,@\-][^,]+)`),
   200  	re(`-Wl,--start-group`),
   201  	re(`-Wl,-?-static`),
   202  	re(`-Wl,-?-subsystem,(native|windows|console|posix|xbox)`),
   203  	re(`-Wl,-syslibroot[=,]([^,@\-][^,]+)`),
   204  	re(`-Wl,-undefined[=,]([^,@\-][^,]+)`),
   205  	re(`-Wl,-?-unresolved-symbols=[^,]+`),
   206  	re(`-Wl,--(no-)?warn-([^,]+)`),
   207  	re(`-Wl,-?-wrap[=,][^,@\-][^,]*`),
   208  	re(`-Wl,-z,(no)?execstack`),
   209  	re(`-Wl,-z,relro`),
   210  
   211  	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)
   212  	re(`\./.*\.(a|o|obj|dll|dylib|so|tbd)`),
   213  }
   214  
   215  var validLinkerFlagsWithNextArg = []string{
   216  	"-arch",
   217  	"-F",
   218  	"-l",
   219  	"-L",
   220  	"-framework",
   221  	"-isysroot",
   222  	"--sysroot",
   223  	"-target",
   224  	"-Wl,-framework",
   225  	"-Wl,-rpath",
   226  	"-Wl,-R",
   227  	"-Wl,--just-symbols",
   228  	"-Wl,-undefined",
   229  }
   230  
   231  func checkCompilerFlags(name, source string, list []string) error {
   232  	return checkFlags(name, source, list, validCompilerFlags, validCompilerFlagsWithNextArg)
   233  }
   234  
   235  func checkLinkerFlags(name, source string, list []string) error {
   236  	return checkFlags(name, source, list, validLinkerFlags, validLinkerFlagsWithNextArg)
   237  }
   238  
   239  func checkFlags(name, source string, list []string, valid []*lazyregexp.Regexp, validNext []string) error {
   240  	// Let users override rules with $CGO_CFLAGS_ALLOW, $CGO_CFLAGS_DISALLOW, etc.
   241  	var (
   242  		allow    *regexp.Regexp
   243  		disallow *regexp.Regexp
   244  	)
   245  	if env := cfg.Getenv("CGO_" + name + "_ALLOW"); env != "" {
   246  		r, err := regexp.Compile(env)
   247  		if err != nil {
   248  			return fmt.Errorf("parsing $CGO_%s_ALLOW: %v", name, err)
   249  		}
   250  		allow = r
   251  	}
   252  	if env := cfg.Getenv("CGO_" + name + "_DISALLOW"); env != "" {
   253  		r, err := regexp.Compile(env)
   254  		if err != nil {
   255  			return fmt.Errorf("parsing $CGO_%s_DISALLOW: %v", name, err)
   256  		}
   257  		disallow = r
   258  	}
   259  
   260  Args:
   261  	for i := 0; i < len(list); i++ {
   262  		arg := list[i]
   263  		if disallow != nil && disallow.FindString(arg) == arg {
   264  			goto Bad
   265  		}
   266  		if allow != nil && allow.FindString(arg) == arg {
   267  			continue Args
   268  		}
   269  		for _, re := range valid {
   270  			if re.FindString(arg) == arg { // must be complete match
   271  				continue Args
   272  			}
   273  		}
   274  		for _, x := range validNext {
   275  			if arg == x {
   276  				if i+1 < len(list) && load.SafeArg(list[i+1]) {
   277  					i++
   278  					continue Args
   279  				}
   280  
   281  				// Permit -Wl,-framework -Wl,name.
   282  				if i+1 < len(list) &&
   283  					strings.HasPrefix(arg, "-Wl,") &&
   284  					strings.HasPrefix(list[i+1], "-Wl,") &&
   285  					load.SafeArg(list[i+1][4:]) &&
   286  					!strings.Contains(list[i+1][4:], ",") {
   287  					i++
   288  					continue Args
   289  				}
   290  
   291  				// Permit -I= /path, -I $SYSROOT.
   292  				if i+1 < len(list) && arg == "-I" {
   293  					if (strings.HasPrefix(list[i+1], "=") || strings.HasPrefix(list[i+1], "$SYSROOT")) &&
   294  						load.SafeArg(list[i+1][1:]) {
   295  						i++
   296  						continue Args
   297  					}
   298  				}
   299  
   300  				if i+1 < len(list) {
   301  					return fmt.Errorf("invalid flag in %s: %s %s (see https://golang.org/s/invalidflag)", source, arg, list[i+1])
   302  				}
   303  				return fmt.Errorf("invalid flag in %s: %s without argument (see https://golang.org/s/invalidflag)", source, arg)
   304  			}
   305  		}
   306  	Bad:
   307  		return fmt.Errorf("invalid flag in %s: %s", source, arg)
   308  	}
   309  	return nil
   310  }
   311  

View as plain text