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