1
0

bundle-libraries.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/usr/bin/env bash
  2. #
  3. # Script to install host system binaries along with required libraries.
  4. #
  5. # Copyright (C) 2012-2017 Jo-Philipp Wich <jo@mein.io>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. DIR="$1"; shift
  21. _cp() {
  22. cp ${VERBOSE:+-v} -L "$1" "$2" || {
  23. echo "cp($1 $2) failed" >&2
  24. exit 1
  25. }
  26. }
  27. _mv() {
  28. mv ${VERBOSE:+-v} "$1" "$2" || {
  29. echo "mv($1 $2) failed" >&2
  30. exit 1
  31. }
  32. }
  33. _md() {
  34. mkdir ${VERBOSE:+-v} -p "$1" || {
  35. echo "mkdir($1) failed" >&2
  36. exit 2
  37. }
  38. }
  39. _ln() {
  40. ln ${VERBOSE:+-v} -sf "$1" "$2" || {
  41. echo "ln($1 $2) failed" >&2
  42. exit 3
  43. }
  44. }
  45. _relpath() {
  46. local base="$(readlink -f "$1")"
  47. local dest="$(readlink -f "$2")"
  48. local up
  49. [ -d "$base" ] || base="${base%/*}"
  50. [ -d "$dest" ] || dest="${dest%/*}"
  51. while true; do
  52. case "$base"
  53. in "$dest"/*)
  54. echo "$up/${base#$dest/}"
  55. break
  56. ;;
  57. *)
  58. dest="${dest%/*}"
  59. up="${up:+$up/}.."
  60. ;;
  61. esac
  62. done
  63. }
  64. _runas_so() {
  65. cat <<-EOT | ${CC:-gcc} -x c -fPIC -shared -o "$1" -
  66. #include <unistd.h>
  67. #include <stdio.h>
  68. #include <stdlib.h>
  69. int mangle_arg0(int argc, char **argv, char **env) {
  70. char *arg0 = getenv("RUNAS_ARG0");
  71. if (arg0) {
  72. argv[0] = arg0;
  73. unsetenv("RUNAS_ARG0");
  74. }
  75. return 0;
  76. }
  77. #ifdef __APPLE__
  78. __attribute__((section("__DATA,__mod_init_func")))
  79. #else
  80. __attribute__((section(".init_array")))
  81. #endif
  82. static void *mangle_arg0_constructor = &mangle_arg0;
  83. EOT
  84. [ -x "$1" ] || {
  85. echo "compiling preload library failed" >&2
  86. exit 5
  87. }
  88. }
  89. _patch_ldso() {
  90. _cp "$1" "$1.patched"
  91. sed -i -e 's,/\(usr\|lib\|etc\)/,/###/,g' "$1.patched"
  92. if "$1.patched" 2>&1 | grep -q -- --library-path; then
  93. _mv "$1.patched" "$1"
  94. else
  95. echo "binary patched ${1##*/} not executable, using original" >&2
  96. rm -f "$1.patched"
  97. fi
  98. }
  99. for LDD in ${PATH//://ldd }/ldd; do
  100. "$LDD" --version >/dev/null 2>/dev/null && break
  101. LDD=""
  102. done
  103. [ -n "$LDD" -a -x "$LDD" ] || LDD=
  104. for BIN in "$@"; do
  105. [ -n "$BIN" -a -n "$DIR" ] || {
  106. echo "Usage: $0 <destdir> <executable> ..." >&2
  107. exit 1
  108. }
  109. [ ! -d "$DIR/lib" ] && {
  110. _md "$DIR/lib"
  111. _md "$DIR/usr"
  112. _ln "../lib" "$DIR/usr/lib"
  113. }
  114. [ ! -x "$DIR/lib/runas.so" ] && {
  115. _runas_so "$DIR/lib/runas.so"
  116. }
  117. LDSO=""
  118. [ -n "$LDD" ] && [ -x "$BIN" ] && file "$BIN" | grep -sqE "ELF.*(executable|interpreter)" && {
  119. for token in $("$LDD" "$BIN" 2>/dev/null); do
  120. case "$token" in */*.so*)
  121. case "$token" in
  122. *ld-*.so*) LDSO="${token##*/}" ;;
  123. esac
  124. dest="$DIR/lib/${token##*/}"
  125. ddir="${dest%/*}"
  126. [ -f "$token" -a ! -f "$dest" ] && {
  127. _md "$ddir"
  128. _cp "$token" "$dest"
  129. [ -n "$LDSO" ] && _patch_ldso "$dest"
  130. }
  131. ;; esac
  132. done
  133. }
  134. # is a dynamically linked executable
  135. if [ -n "$LDSO" ]; then
  136. echo "Bundling ${BIN##*/}"
  137. RUNDIR="$(readlink -f "$BIN")"; RUNDIR="${RUNDIR%/*}"
  138. RUN="${LDSO#ld-}"; RUN="run-${RUN%%.so*}.sh"
  139. REL="$(_relpath "$DIR/lib" "$BIN")"
  140. _mv "$BIN" "$RUNDIR/.${BIN##*/}.bin"
  141. cat <<-EOF > "$BIN"
  142. #!/usr/bin/env bash
  143. dir="\$(dirname "\$0")"
  144. export RUNAS_ARG0="\$0"
  145. export LD_PRELOAD="\$dir/${REL:+$REL/}runas.so"
  146. exec "\$dir/${REL:+$REL/}$LDSO" --library-path "\$dir/${REL:+$REL/}" "\$dir/.${BIN##*/}.bin" "\$@"
  147. EOF
  148. chmod ${VERBOSE:+-v} 0755 "$BIN"
  149. fi
  150. done