logo

basic-core-handler

Basic Linux coredump handler to process coredump data right away git clone https://anongit.hacktivis.me/git/basic-core-handler.git

core-handler (1841B)


  1. #!/bin/sh
  2. # Copyright © 2024 Haelwenn (lanodan) Monnier <contact+core-handler@hacktivis.me>
  3. # SPDX-License-Identifier: MIT
  4. applyuidgid=$(command -v applyuidgid 2>/dev/null || echo s6-applyuidgid)
  5. # kernel.core_pattern = |/usr/local/bin/core-handler %P %u %g %s %t %c %e %E
  6. grep -q "$0"' %P %u %g %s %t %c %e %E$' /proc/sys/kernel/core_pattern || exit 1
  7. pid="$1"; shift
  8. uid="$1"; shift
  9. gid="$1"; shift
  10. signum="$1"; shift
  11. epoch="$1"; shift
  12. core_limit="$1"; shift
  13. comm="$1"; shift
  14. full_path="$1"; shift
  15. destdir="/var/crash/"
  16. dest="${destdir}/${epoch}-${pid}-${uid}-${comm}"
  17. umask u=r,og=
  18. # Using this script, /var/crash should be "0755/drwxr-xr-x root root"
  19. # Let's make sure it both exists and is safe before writing anything
  20. mkdir -m 0755 -p "${destdir}"
  21. chown 0:0 "${destdir}"
  22. chmod 0755 "${destdir}"
  23. cat - >"${dest}.core"
  24. chown -- "${uid}:${gid}" "${dest}.core"
  25. sync -d "${dest}.core"
  26. {
  27. printf 'pid: %s\n' "$pid"
  28. printf 'uid: %s\n' "$uid"
  29. printf 'gid: %s\n' "$gid"
  30. printf 'signum: %s\n' "$signum"
  31. printf 'epoch: %s\n' "$epoch"
  32. printf 'core_limit: %s\n' "$core_limit"
  33. printf 'comm: %s\n' "$comm"
  34. printf 'full_path: %s\n' "${full_path}" | tr '!' '/'
  35. } > "${dest}.info"
  36. chown -- "${uid}:${gid}" "${dest}.info"
  37. sync -d "${dest}.info"
  38. # Extract the following from a coredump with LLDB:
  39. # - backtrace, all threads
  40. # - current frame for readability
  41. # - global and frame-local variables
  42. # - registers
  43. # - dissasembly of current frame (with mixed source code when available)
  44. nice -n 20 -- "${applyuidgid?}" -u "$uid" -g "$gid" -G '' lldb \
  45. --core "${dest}.core" \
  46. -b \
  47. -o 'bt all' \
  48. -o 'f' \
  49. -o 'v -A -g -P2 -c -s' \
  50. -o 'register read' \
  51. -o 'di -m' \
  52. -o 'quit' \
  53. >"${dest}.backtrace.txt" 2>&1
  54. # -o "session save \"${dest}.backtrace.txt\""
  55. chown -- "${uid}:${gid}" "${dest}.backtrace.txt"
  56. sync -d "${dest}.backtrace.txt"