Source file src/cmd/go/internal/work/security_test.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  package work
     6  
     7  import (
     8  	"os"
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  var goodCompilerFlags = [][]string{
    14  	{"-DFOO"},
    15  	{"-Dfoo=bar"},
    16  	{"-Ufoo"},
    17  	{"-Ufoo1"},
    18  	{"-F/Qt"},
    19  	{"-F", "/Qt"},
    20  	{"-I/"},
    21  	{"-I/etc/passwd"},
    22  	{"-I."},
    23  	{"-O"},
    24  	{"-O2"},
    25  	{"-Osmall"},
    26  	{"-W"},
    27  	{"-Wall"},
    28  	{"-Wp,-Dfoo=bar"},
    29  	{"-Wp,-Ufoo"},
    30  	{"-Wp,-Dfoo1"},
    31  	{"-Wp,-Ufoo1"},
    32  	{"-flto"},
    33  	{"-fobjc-arc"},
    34  	{"-fno-objc-arc"},
    35  	{"-fomit-frame-pointer"},
    36  	{"-fno-omit-frame-pointer"},
    37  	{"-fpic"},
    38  	{"-fno-pic"},
    39  	{"-fPIC"},
    40  	{"-fno-PIC"},
    41  	{"-fpie"},
    42  	{"-fno-pie"},
    43  	{"-fPIE"},
    44  	{"-fno-PIE"},
    45  	{"-fsplit-stack"},
    46  	{"-fno-split-stack"},
    47  	{"-fstack-xxx"},
    48  	{"-fno-stack-xxx"},
    49  	{"-fsanitize=hands"},
    50  	{"-ftls-model=local-dynamic"},
    51  	{"-g"},
    52  	{"-ggdb"},
    53  	{"-march=souza"},
    54  	{"-mcmodel=medium"},
    55  	{"-mcpu=123"},
    56  	{"-mfpu=123"},
    57  	{"-mlarge-data-threshold=16"},
    58  	{"-mtune=happybirthday"},
    59  	{"-mstack-overflow"},
    60  	{"-mno-stack-overflow"},
    61  	{"-mmacosx-version"},
    62  	{"-mnop-fun-dllimport"},
    63  	{"-pthread"},
    64  	{"-std=c99"},
    65  	{"-xc"},
    66  	{"-D", "FOO"},
    67  	{"-D", "foo=bar"},
    68  	{"-I", "."},
    69  	{"-I", "/etc/passwd"},
    70  	{"-I", "世界"},
    71  	{"-I", "=/usr/include/libxml2"},
    72  	{"-I", "dir"},
    73  	{"-I", "$SYSROOT/dir"},
    74  	{"-isystem", "/usr/include/mozjs-68"},
    75  	{"-include", "/usr/include/mozjs-68/RequiredDefines.h"},
    76  	{"-framework", "Chocolate"},
    77  	{"-x", "c"},
    78  	{"-v"},
    79  }
    80  
    81  var badCompilerFlags = [][]string{
    82  	{"-D@X"},
    83  	{"-D-X"},
    84  	{"-Ufoo=bar"},
    85  	{"-F@dir"},
    86  	{"-F-dir"},
    87  	{"-I@dir"},
    88  	{"-I-dir"},
    89  	{"-O@1"},
    90  	{"-Wa,-foo"},
    91  	{"-W@foo"},
    92  	{"-Wp,-DX,-D@X"},
    93  	{"-Wp,-UX,-U@X"},
    94  	{"-g@gdb"},
    95  	{"-g-gdb"},
    96  	{"-march=@dawn"},
    97  	{"-march=-dawn"},
    98  	{"-mcmodel=@model"},
    99  	{"-mlarge-data-threshold=@12"},
   100  	{"-std=@c99"},
   101  	{"-std=-c99"},
   102  	{"-x@c"},
   103  	{"-x-c"},
   104  	{"-D", "@foo"},
   105  	{"-D", "-foo"},
   106  	{"-I", "@foo"},
   107  	{"-I", "-foo"},
   108  	{"-I", "=@obj"},
   109  	{"-include", "@foo"},
   110  	{"-framework", "-Caffeine"},
   111  	{"-framework", "@Home"},
   112  	{"-x", "--c"},
   113  	{"-x", "@obj"},
   114  }
   115  
   116  func TestCheckCompilerFlags(t *testing.T) {
   117  	for _, f := range goodCompilerFlags {
   118  		if err := checkCompilerFlags("test", "test", f); err != nil {
   119  			t.Errorf("unexpected error for %q: %v", f, err)
   120  		}
   121  	}
   122  	for _, f := range badCompilerFlags {
   123  		if err := checkCompilerFlags("test", "test", f); err == nil {
   124  			t.Errorf("missing error for %q", f)
   125  		}
   126  	}
   127  }
   128  
   129  var goodLinkerFlags = [][]string{
   130  	{"-Fbar"},
   131  	{"-lbar"},
   132  	{"-Lbar"},
   133  	{"-fpic"},
   134  	{"-fno-pic"},
   135  	{"-fPIC"},
   136  	{"-fno-PIC"},
   137  	{"-fpie"},
   138  	{"-fno-pie"},
   139  	{"-fPIE"},
   140  	{"-fno-PIE"},
   141  	{"-fsanitize=hands"},
   142  	{"-g"},
   143  	{"-ggdb"},
   144  	{"-march=souza"},
   145  	{"-mcpu=123"},
   146  	{"-mfpu=123"},
   147  	{"-mtune=happybirthday"},
   148  	{"-pic"},
   149  	{"-pthread"},
   150  	{"-Wl,--hash-style=both"},
   151  	{"-Wl,-rpath,foo"},
   152  	{"-Wl,-rpath,$ORIGIN/foo"},
   153  	{"-Wl,-R", "/foo"},
   154  	{"-Wl,-R", "foo"},
   155  	{"-Wl,-R,foo"},
   156  	{"-Wl,--just-symbols=foo"},
   157  	{"-Wl,--just-symbols,foo"},
   158  	{"-Wl,--warn-error"},
   159  	{"-Wl,--no-warn-error"},
   160  	{"foo.so"},
   161  	{"_世界.dll"},
   162  	{"./x.o"},
   163  	{"libcgosotest.dylib"},
   164  	{"-F", "framework"},
   165  	{"-l", "."},
   166  	{"-l", "/etc/passwd"},
   167  	{"-l", "世界"},
   168  	{"-L", "framework"},
   169  	{"-framework", "Chocolate"},
   170  	{"-v"},
   171  	{"-Wl,-sectcreate,__TEXT,__info_plist,${SRCDIR}/Info.plist"},
   172  	{"-Wl,-framework", "-Wl,Chocolate"},
   173  	{"-Wl,-framework,Chocolate"},
   174  	{"-Wl,-unresolved-symbols=ignore-all"},
   175  	{"-Wl,-z,relro"},
   176  	{"-Wl,-z,relro,-z,now"},
   177  	{"-Wl,-z,now"},
   178  	{"-Wl,-z,noexecstack"},
   179  	{"libcgotbdtest.tbd"},
   180  	{"./libcgotbdtest.tbd"},
   181  	{"-Wl,--push-state"},
   182  	{"-Wl,--pop-state"},
   183  	{"-Wl,--push-state,--as-needed"},
   184  	{"-Wl,--push-state,--no-as-needed,-Bstatic"},
   185  }
   186  
   187  var badLinkerFlags = [][]string{
   188  	{"-DFOO"},
   189  	{"-Dfoo=bar"},
   190  	{"-W"},
   191  	{"-Wall"},
   192  	{"-fobjc-arc"},
   193  	{"-fno-objc-arc"},
   194  	{"-fomit-frame-pointer"},
   195  	{"-fno-omit-frame-pointer"},
   196  	{"-fsplit-stack"},
   197  	{"-fno-split-stack"},
   198  	{"-fstack-xxx"},
   199  	{"-fno-stack-xxx"},
   200  	{"-mstack-overflow"},
   201  	{"-mno-stack-overflow"},
   202  	{"-mnop-fun-dllimport"},
   203  	{"-std=c99"},
   204  	{"-xc"},
   205  	{"-D", "FOO"},
   206  	{"-D", "foo=bar"},
   207  	{"-I", "FOO"},
   208  	{"-L", "@foo"},
   209  	{"-L", "-foo"},
   210  	{"-x", "c"},
   211  	{"-D@X"},
   212  	{"-D-X"},
   213  	{"-I@dir"},
   214  	{"-I-dir"},
   215  	{"-O@1"},
   216  	{"-Wa,-foo"},
   217  	{"-W@foo"},
   218  	{"-g@gdb"},
   219  	{"-g-gdb"},
   220  	{"-march=@dawn"},
   221  	{"-march=-dawn"},
   222  	{"-std=@c99"},
   223  	{"-std=-c99"},
   224  	{"-x@c"},
   225  	{"-x-c"},
   226  	{"-D", "@foo"},
   227  	{"-D", "-foo"},
   228  	{"-I", "@foo"},
   229  	{"-I", "-foo"},
   230  	{"-l", "@foo"},
   231  	{"-l", "-foo"},
   232  	{"-framework", "-Caffeine"},
   233  	{"-framework", "@Home"},
   234  	{"-Wl,-framework,-Caffeine"},
   235  	{"-Wl,-framework", "-Wl,@Home"},
   236  	{"-Wl,-framework", "@Home"},
   237  	{"-Wl,-framework,Chocolate,@Home"},
   238  	{"-Wl,--hash-style=foo"},
   239  	{"-x", "--c"},
   240  	{"-x", "@obj"},
   241  	{"-Wl,-rpath,@foo"},
   242  	{"-Wl,-R,foo,bar"},
   243  	{"-Wl,-R,@foo"},
   244  	{"-Wl,--just-symbols,@foo"},
   245  	{"../x.o"},
   246  	{"-Wl,-R,"},
   247  	{"-Wl,-O"},
   248  	{"-Wl,-e="},
   249  	{"-Wl,-e,"},
   250  	{"-Wl,-R,-flag"},
   251  	{"-Wl,--push-state,"},
   252  	{"-Wl,--push-state,@foo"},
   253  }
   254  
   255  func TestCheckLinkerFlags(t *testing.T) {
   256  	for _, f := range goodLinkerFlags {
   257  		if err := checkLinkerFlags("test", "test", f); err != nil {
   258  			t.Errorf("unexpected error for %q: %v", f, err)
   259  		}
   260  	}
   261  	for _, f := range badLinkerFlags {
   262  		if err := checkLinkerFlags("test", "test", f); err == nil {
   263  			t.Errorf("missing error for %q", f)
   264  		}
   265  	}
   266  }
   267  
   268  func TestCheckFlagAllowDisallow(t *testing.T) {
   269  	if err := checkCompilerFlags("TEST", "test", []string{"-disallow"}); err == nil {
   270  		t.Fatalf("missing error for -disallow")
   271  	}
   272  	os.Setenv("CGO_TEST_ALLOW", "-disallo")
   273  	if err := checkCompilerFlags("TEST", "test", []string{"-disallow"}); err == nil {
   274  		t.Fatalf("missing error for -disallow with CGO_TEST_ALLOW=-disallo")
   275  	}
   276  	os.Setenv("CGO_TEST_ALLOW", "-disallow")
   277  	if err := checkCompilerFlags("TEST", "test", []string{"-disallow"}); err != nil {
   278  		t.Fatalf("unexpected error for -disallow with CGO_TEST_ALLOW=-disallow: %v", err)
   279  	}
   280  	os.Unsetenv("CGO_TEST_ALLOW")
   281  
   282  	if err := checkCompilerFlags("TEST", "test", []string{"-Wall"}); err != nil {
   283  		t.Fatalf("unexpected error for -Wall: %v", err)
   284  	}
   285  	os.Setenv("CGO_TEST_DISALLOW", "-Wall")
   286  	if err := checkCompilerFlags("TEST", "test", []string{"-Wall"}); err == nil {
   287  		t.Fatalf("missing error for -Wall with CGO_TEST_DISALLOW=-Wall")
   288  	}
   289  	os.Setenv("CGO_TEST_ALLOW", "-Wall") // disallow wins
   290  	if err := checkCompilerFlags("TEST", "test", []string{"-Wall"}); err == nil {
   291  		t.Fatalf("missing error for -Wall with CGO_TEST_DISALLOW=-Wall and CGO_TEST_ALLOW=-Wall")
   292  	}
   293  
   294  	os.Setenv("CGO_TEST_ALLOW", "-fplugin.*")
   295  	os.Setenv("CGO_TEST_DISALLOW", "-fplugin=lint.so")
   296  	if err := checkCompilerFlags("TEST", "test", []string{"-fplugin=faster.so"}); err != nil {
   297  		t.Fatalf("unexpected error for -fplugin=faster.so: %v", err)
   298  	}
   299  	if err := checkCompilerFlags("TEST", "test", []string{"-fplugin=lint.so"}); err == nil {
   300  		t.Fatalf("missing error for -fplugin=lint.so: %v", err)
   301  	}
   302  }
   303  
   304  func TestCheckCompilerFlagsForInternalLink(t *testing.T) {
   305  	// Any "bad" compiler flag should trigger external linking.
   306  	for _, f := range badCompilerFlags {
   307  		if err := checkCompilerFlagsForInternalLink("test", "test", f); err == nil {
   308  			t.Errorf("missing error for %q", f)
   309  		}
   310  	}
   311  
   312  	// All "good" compiler flags should not trigger external linking,
   313  	// except for anything that begins with "-flto".
   314  	for _, f := range goodCompilerFlags {
   315  		foundLTO := false
   316  		for _, s := range f {
   317  			if strings.Contains(s, "-flto") {
   318  				foundLTO = true
   319  			}
   320  		}
   321  		if err := checkCompilerFlagsForInternalLink("test", "test", f); err != nil {
   322  			// expect error for -flto
   323  			if !foundLTO {
   324  				t.Errorf("unexpected error for %q: %v", f, err)
   325  			}
   326  		} else {
   327  			// expect no error for everything else
   328  			if foundLTO {
   329  				t.Errorf("missing error for %q: %v", f, err)
   330  			}
   331  		}
   332  	}
   333  }
   334  

View as plain text