github.com/bir3/gocompiler@v0.9.2202/src/cmd/gocmd/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 "fmt" 34 "github.com/bir3/gocompiler/src/internal/lazyregexp" 35 "regexp" 36 "strings" 37 38 "github.com/bir3/gocompiler/src/cmd/gocmd/internal/cfg" 39 "github.com/bir3/gocompiler/src/cmd/gocmd/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(`-fvisibility=(.+)`), 95 re(`-g([^@\-].*)?`), 96 re(`-m32`), 97 re(`-m64`), 98 re(`-m(abi|arch|cpu|fpu|tune)=([^@\-].*)`), 99 re(`-m(no-)?v?aes`), 100 re(`-marm`), 101 re(`-m(no-)?avx[0-9a-z]*`), 102 re(`-mfloat-abi=([^@\-].*)`), 103 re(`-mfpmath=[0-9a-z,+]*`), 104 re(`-m(no-)?avx[0-9a-z.]*`), 105 re(`-m(no-)?ms-bitfields`), 106 re(`-m(no-)?stack-(.+)`), 107 re(`-mmacosx-(.+)`), 108 re(`-mios-simulator-version-min=(.+)`), 109 re(`-miphoneos-version-min=(.+)`), 110 re(`-mtvos-simulator-version-min=(.+)`), 111 re(`-mtvos-version-min=(.+)`), 112 re(`-mwatchos-simulator-version-min=(.+)`), 113 re(`-mwatchos-version-min=(.+)`), 114 re(`-mnop-fun-dllimport`), 115 re(`-m(no-)?sse[0-9.]*`), 116 re(`-m(no-)?ssse3`), 117 re(`-mthumb(-interwork)?`), 118 re(`-mthreads`), 119 re(`-mwindows`), 120 re(`-no-canonical-prefixes`), 121 re(`--param=ssp-buffer-size=[0-9]*`), 122 re(`-pedantic(-errors)?`), 123 re(`-pipe`), 124 re(`-pthread`), 125 re(`-?-std=([^@\-].*)`), 126 re(`-?-stdlib=([^@\-].*)`), 127 re(`--sysroot=([^@\-].*)`), 128 re(`-w`), 129 re(`-x([^@\-].*)`), 130 re(`-v`), 131 } 132 133 var validCompilerFlagsWithNextArg = []string{ 134 "-arch", 135 "-D", 136 "-U", 137 "-I", 138 "-F", 139 "-framework", 140 "-include", 141 "-isysroot", 142 "-isystem", 143 "--sysroot", 144 "-target", 145 "-x", 146 } 147 148 var validLinkerFlags = []*lazyregexp.Regexp{ 149 re(`-F([^@\-].*)`), 150 re(`-l([^@\-].*)`), 151 re(`-L([^@\-].*)`), 152 re(`-O`), 153 re(`-O([^@\-].*)`), 154 re(`-f(no-)?(pic|PIC|pie|PIE)`), 155 re(`-f(no-)?openmp(-simd)?`), 156 re(`-fsanitize=([^@\-].*)`), 157 re(`-flat_namespace`), 158 re(`-g([^@\-].*)?`), 159 re(`-headerpad_max_install_names`), 160 re(`-m(abi|arch|cpu|fpu|tune)=([^@\-].*)`), 161 re(`-mfloat-abi=([^@\-].*)`), 162 re(`-mmacosx-(.+)`), 163 re(`-mios-simulator-version-min=(.+)`), 164 re(`-miphoneos-version-min=(.+)`), 165 re(`-mthreads`), 166 re(`-mwindows`), 167 re(`-(pic|PIC|pie|PIE)`), 168 re(`-pthread`), 169 re(`-rdynamic`), 170 re(`-shared`), 171 re(`-?-static([-a-z0-9+]*)`), 172 re(`-?-stdlib=([^@\-].*)`), 173 re(`-v`), 174 175 // Note that any wildcards in -Wl need to exclude comma, 176 // since -Wl splits its argument at commas and passes 177 // them all to the linker uninterpreted. Allowing comma 178 // in a wildcard would allow tunneling arbitrary additional 179 // linker arguments through one of these. 180 re(`-Wl,--(no-)?allow-multiple-definition`), 181 re(`-Wl,--(no-)?allow-shlib-undefined`), 182 re(`-Wl,--(no-)?as-needed`), 183 re(`-Wl,-Bdynamic`), 184 re(`-Wl,-berok`), 185 re(`-Wl,-Bstatic`), 186 re(`-Wl,-Bsymbolic-functions`), 187 re(`-Wl,-O[0-9]+`), 188 re(`-Wl,-d[ny]`), 189 re(`-Wl,--disable-new-dtags`), 190 re(`-Wl,-e[=,][a-zA-Z0-9]+`), 191 re(`-Wl,--enable-new-dtags`), 192 re(`-Wl,--end-group`), 193 re(`-Wl,--(no-)?export-dynamic`), 194 re(`-Wl,-E`), 195 re(`-Wl,-framework,[^,@\-][^,]+`), 196 re(`-Wl,--hash-style=(sysv|gnu|both)`), 197 re(`-Wl,-headerpad_max_install_names`), 198 re(`-Wl,--no-undefined`), 199 re(`-Wl,-R,?([^@\-,][^,@]*$)`), 200 re(`-Wl,--just-symbols[=,]([^,@\-][^,@]+)`), 201 re(`-Wl,-rpath(-link)?[=,]([^,@\-][^,]+)`), 202 re(`-Wl,-s`), 203 re(`-Wl,-search_paths_first`), 204 re(`-Wl,-sectcreate,([^,@\-][^,]+),([^,@\-][^,]+),([^,@\-][^,]+)`), 205 re(`-Wl,--start-group`), 206 re(`-Wl,-?-static`), 207 re(`-Wl,-?-subsystem,(native|windows|console|posix|xbox)`), 208 re(`-Wl,-syslibroot[=,]([^,@\-][^,]+)`), 209 re(`-Wl,-undefined[=,]([^,@\-][^,]+)`), 210 re(`-Wl,-?-unresolved-symbols=[^,]+`), 211 re(`-Wl,--(no-)?warn-([^,]+)`), 212 re(`-Wl,-?-wrap[=,][^,@\-][^,]*`), 213 re(`-Wl,-z,(no)?execstack`), 214 re(`-Wl,-z,relro`), 215 216 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) 217 re(`\./.*\.(a|o|obj|dll|dylib|so|tbd)`), 218 } 219 220 var validLinkerFlagsWithNextArg = []string{ 221 "-arch", 222 "-F", 223 "-l", 224 "-L", 225 "-framework", 226 "-isysroot", 227 "--sysroot", 228 "-target", 229 "-Wl,-framework", 230 "-Wl,-rpath", 231 "-Wl,-R", 232 "-Wl,--just-symbols", 233 "-Wl,-undefined", 234 } 235 236 func checkCompilerFlags(name, source string, list []string) error { 237 checkOverrides := true 238 return checkFlags(name, source, list, validCompilerFlags, validCompilerFlagsWithNextArg, checkOverrides) 239 } 240 241 func checkLinkerFlags(name, source string, list []string) error { 242 checkOverrides := true 243 return checkFlags(name, source, list, validLinkerFlags, validLinkerFlagsWithNextArg, checkOverrides) 244 } 245 246 // checkCompilerFlagsForInternalLink returns an error if 'list' 247 // contains a flag or flags that may not be fully supported by 248 // internal linking (meaning that we should punt the link to the 249 // external linker). 250 func checkCompilerFlagsForInternalLink(name, source string, list []string) error { 251 checkOverrides := false 252 if err := checkFlags(name, source, list, validCompilerFlags, validCompilerFlagsWithNextArg, checkOverrides); err != nil { 253 return err 254 } 255 // Currently the only flag on the allow list that causes problems 256 // for the linker is "-flto"; check for it manually here. 257 for _, fl := range list { 258 if strings.HasPrefix(fl, "-flto") { 259 return fmt.Errorf("flag %q triggers external linking", fl) 260 } 261 } 262 return nil 263 } 264 265 func checkFlags(name, source string, list []string, valid []*lazyregexp.Regexp, validNext []string, checkOverrides bool) error { 266 // Let users override rules with $CGO_CFLAGS_ALLOW, $CGO_CFLAGS_DISALLOW, etc. 267 var ( 268 allow *regexp.Regexp 269 disallow *regexp.Regexp 270 ) 271 if checkOverrides { 272 if env := cfg.Getenv("CGO_" + name + "_ALLOW"); env != "" { 273 r, err := regexp.Compile(env) 274 if err != nil { 275 return fmt.Errorf("parsing $CGO_%s_ALLOW: %v", name, err) 276 } 277 allow = r 278 } 279 if env := cfg.Getenv("CGO_" + name + "_DISALLOW"); env != "" { 280 r, err := regexp.Compile(env) 281 if err != nil { 282 return fmt.Errorf("parsing $CGO_%s_DISALLOW: %v", name, err) 283 } 284 disallow = r 285 } 286 } 287 288 Args: 289 for i := 0; i < len(list); i++ { 290 arg := list[i] 291 if disallow != nil && disallow.FindString(arg) == arg { 292 goto Bad 293 } 294 if allow != nil && allow.FindString(arg) == arg { 295 continue Args 296 } 297 for _, re := range valid { 298 if re.FindString(arg) == arg { // must be complete match 299 continue Args 300 } 301 } 302 for _, x := range validNext { 303 if arg == x { 304 if i+1 < len(list) && load.SafeArg(list[i+1]) { 305 i++ 306 continue Args 307 } 308 309 // Permit -Wl,-framework -Wl,name. 310 if i+1 < len(list) && 311 strings.HasPrefix(arg, "-Wl,") && 312 strings.HasPrefix(list[i+1], "-Wl,") && 313 load.SafeArg(list[i+1][4:]) && 314 !strings.Contains(list[i+1][4:], ",") { 315 i++ 316 continue Args 317 } 318 319 // Permit -I= /path, -I $SYSROOT. 320 if i+1 < len(list) && arg == "-I" { 321 if (strings.HasPrefix(list[i+1], "=") || strings.HasPrefix(list[i+1], "$SYSROOT")) && 322 load.SafeArg(list[i+1][1:]) { 323 i++ 324 continue Args 325 } 326 } 327 328 if i+1 < len(list) { 329 return fmt.Errorf("invalid flag in %s: %s %s (see https://golang.org/s/invalidflag)", source, arg, list[i+1]) 330 } 331 return fmt.Errorf("invalid flag in %s: %s without argument (see https://golang.org/s/invalidflag)", source, arg) 332 } 333 } 334 Bad: 335 return fmt.Errorf("invalid flag in %s: %s", source, arg) 336 } 337 return nil 338 }