File: common_lib.sh

package info (click to toggle)
libpulp 0.2.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,964 kB
  • sloc: ansic: 11,257; python: 1,110; sh: 881; makefile: 824; cpp: 582; asm: 124
file content (80 lines) | stat: -rw-r--r-- 1,855 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/bash

# Check if running bash
if [ -z "$BASH" ]; then
    echo "This script must be run with bash!"
    exit 1
fi

# Check if user have the necessary tools.
for tool in osc xpath cpio rpm2cpio xxd; do
  which $tool > /dev/null
  if [ $? -ne 0 ]; then
    fail "$tool not found, but is necessary by $0"
  fi
done

# Color special caracters.
GREEN='\033[1;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
PURPLE='\033[1;35m'
NC='\033[0m' # No Color

ok () {
  echo -e "${GREEN}OK: $1${NC}\n"
}

warn() {
  echo -e "${YELLOW}WARNING: $1${NC}\n"
}

fail () {
  echo -e "${RED}ERROR: $1${NC}\n"
  exit 1
}

# Debug to stderr as some functions use stdout to return info
debug() {
  if [[ ! -z "$ULP_DEBUG" ]]; then
    echo -e "${BLUE}DEBUG: $1${NC}\n" 1>&2;
  fi
}

info () {
  echo -e "${PURPLE}$1${NC}\n"
}

is_lib_livepatcheable() {
  ULP_NOPS_LEN=16
  ULP_PRE_NOPS_LEN=14
  ULP_NOP_OPCODE=90

  local lib_path=$1
  local address_of_a_symbol=`objdump -T $lib_path | grep '\.text' | head -n 1 | awk '{ print $1 }'`

  if [ $? -ne 0 ]; then
    fail "Unable to find a symbol in $lib_path"
  fi

  # Convert hexadecimal address in decimal so we can do mathematical operations
  local addr_decimal=`printf "%llu" "0x$address_of_a_symbol"`
  if [ $addr_decimal -eq 0 ]; then
    fail "Symbol at address found is invalid: $address_of_a_symbol"
  fi

  local ulp_prologue_dec=`expr $addr_decimal - $ULP_PRE_NOPS_LEN`
  local ulp_prologue_addr=`printf "0x%lx" $ulp_prologue_dec`

  # Get bytes at target library. It should have $ULP_NOPS_LEN nops (0x90 on x86)
  local insns=`xxd -s $ulp_prologue_addr -l $ULP_PRE_NOPS_LEN -c $ULP_PRE_NOPS_LEN \
    -g $ULP_PRE_NOPS_LEN $lib_path | grep -oEi "($ULP_NOP_OPCODE){$ULP_PRE_NOPS_LEN}"`

  # If library is not livepatcheable, insns is empty.
  if [ -z "$insns" ]; then
    return 1
  fi

  return 0
}