check_imported_libraries.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. // check_imported_libraries.go checks that each of its arguments only imports a
  15. // whitelist of allowed libraries. This is used to avoid accidental dependencies
  16. // on libstdc++.so.
  17. package main
  18. import (
  19. "debug/elf"
  20. "fmt"
  21. "os"
  22. )
  23. func checkImportedLibraries(path string) {
  24. file, err := elf.Open(path)
  25. if err != nil {
  26. fmt.Fprintf(os.Stderr, "Error opening %s: %s\n", path, err)
  27. os.Exit(1)
  28. }
  29. defer file.Close()
  30. libs, err := file.ImportedLibraries()
  31. if err != nil {
  32. fmt.Fprintf(os.Stderr, "Error reading %s: %s\n", path, err)
  33. os.Exit(1)
  34. }
  35. for _, lib := range libs {
  36. if lib != "libc.so.6" && lib != "libcrypto.so" && lib != "libpthread.so.0" {
  37. fmt.Printf("Invalid dependency for %s: %s\n", path, lib)
  38. fmt.Printf("All dependencies:\n")
  39. for _, lib := range libs {
  40. fmt.Printf(" %s\n", lib)
  41. }
  42. os.Exit(1)
  43. }
  44. }
  45. }
  46. func main() {
  47. for _, path := range os.Args[1:] {
  48. checkImportedLibraries(path)
  49. }
  50. }