blob: 835d5fdd7f42281b7638ab991a266d5ce5966640 [file] [log] [blame]
David Benjamin6d81cf32017-07-25 15:51:31 -04001// Copyright (c) 2017, Google Inc.
2//
3// Permission to use, copy, modify, and/or distribute this software for any
4// purpose with or without fee is hereby granted, provided that the above
5// copyright notice and this permission notice appear in all copies.
6//
7// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15// check_imported_libraries.go checks that each of its arguments only imports a
16// whitelist of allowed libraries. This is used to avoid accidental dependencies
17// on libstdc++.so.
18package main
19
20import (
21 "debug/elf"
22 "fmt"
23 "os"
24)
25
26func checkImportedLibraries(path string) {
27 file, err := elf.Open(path)
28 if err != nil {
29 fmt.Fprintf(os.Stderr, "Error opening %s: %s\n", path, err)
30 os.Exit(1)
31 }
32 defer file.Close()
33
34 libs, err := file.ImportedLibraries()
35 if err != nil {
36 fmt.Fprintf(os.Stderr, "Error reading %s: %s\n", path, err)
37 os.Exit(1)
38 }
39
40 for _, lib := range libs {
41 if lib != "libc.so.6" && lib != "libcrypto.so" && lib != "libpthread.so.0" {
42 fmt.Printf("Invalid dependency for %s: %s\n", path, lib)
43 fmt.Printf("All dependencies:\n")
44 for _, lib := range libs {
45 fmt.Printf(" %s\n", lib)
46 }
47 os.Exit(1)
48 }
49 }
50}
51
52func main() {
53 for _, path := range os.Args[1:] {
54 checkImportedLibraries(path)
55 }
56}