David Benjamin | 6d81cf3 | 2017-07-25 15:51:31 -0400 | [diff] [blame] | 1 | // 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. |
| 18 | package main |
| 19 | |
| 20 | import ( |
| 21 | "debug/elf" |
| 22 | "fmt" |
| 23 | "os" |
| 24 | ) |
| 25 | |
| 26 | func 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 | |
| 52 | func main() { |
| 53 | for _, path := range os.Args[1:] { |
| 54 | checkImportedLibraries(path) |
| 55 | } |
| 56 | } |