This repository was archived by the owner on Aug 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathREADME
More file actions
172 lines (130 loc) · 5.71 KB
/
README
File metadata and controls
172 lines (130 loc) · 5.71 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
README - livepatch
$Id: README 345 2004-11-04 11:12:24Z ukai $
'livepatch' is a command to apply binary patches on running process.
I wonder this is practical, but it seems to be required in teleco area,
according to Pannus project page (AVL.14.0 of OSDL CGL v3.0).
I just wrote it just for learning and hacking with ptrace(2) and bfd
library in a day. It was very fun!
"livepatch" is just a small userland program which provides functionalities
of dynamic loading and overwriting on-memory code & data in a running process.
We don't need any unofficial kernel patches and/or uncommon libraries.
While applying live patch, that is, overwriting on-memory code & data
in a target process, the target process will be in sleep status.
"livepatch" makes it possible to modify on-memory code & data and load
ELF shared object or arbitrary binary files in a running process.
Thus, "livepatch" may meet AVL.14.0 of OSDL CGL Availability
Requirements Definition - version 3.0 (public draft) in some level.
Note: it's not part of activities of OSDL nor OSDL Japan.
PREREQUISITE
bfd library that comes with binutils.
On Debian GNU/Linux, just install binutils-dev.
(I used binutils-dev 2.15-4)
# apt-get install binutils-dev
BUILD & INSTALL
You need at least livepatch.c and optionally Makefile.
$ ls
Makefile README bfd.c fixup.c fixup.txt livepatch.c
$ make
cc -Wall -O2 -g -c -o livepatch.o livepatch.c
cc -o livepatch livepatch.o -lbfd
$
then, copy livepatch where you want to install.
USAGE
- You should prepare target processes executable files with symbol tables,
that is, not stripped version. If stripped, 'livepatch' can't know
the symbol values of the target process. Note that the running process may
be a process invoked from stripped version of the binary file.
- If you want to add code to the target, you should write it and compile
with -fPIC and -shared, like followings
$ cc -shared -fPIC -o foo.so foo.c
If you want to refer a symbol in the target, you should declare it
with "extern". 'livepatch' will try to resolve these symbols when the
shared object file is loaded by "dl" instruction of 'livepatch'.
- patch instructions that 'livepatch' supports are follows:
- set <addr> <type> <value>
set value that is specified by <type> and <value> to <addr>
in a target process.
- new <memname> <size>
allocate new memory space in a target process.
you can refer this area with <memname> in the following instructions.
- load <memname> <filename>
load a file contents in new memory space in a target process.
you can refer this area with <memname> in the following instructions.
- dl <memname> <filename>
load ELF shared object file in new memory space in a target process
with resolving dynamic symbols.
you can refer this area with <memname> in the following instructions,
and symbols in this ELF shared object with <memname>:<symbol>.
- jmp <addr1> <addr2>
set jmp instruction to <addr2> at <addr1> in a target process, so
that if the process comes (or calls) <addr1>, it will jump to
<addr2>.
In <addr>, you can use the followings
- integer value
It is interpreted as absolute address in a target process.
- $<memname>
start address of <memname> allocated by "new", "load" or "dl".
- $<memname>:<symbol>
address pointed by <symbol> in <memname> loaded by "dl".
- $<memname>:intvalue
start address + intvalue in <memname>
- symbol
address pointed by <symbol> in target.
<type> <value>:
- <type> is int
<value> is interpreted as integer value
(using strtol(<value>, NULL, 0))
- <type> is str
<value> is interpreted as string. (until '\n')
- <type> is hex
<value> is hexadecimal representation.
eg. <value>=686578 -> "hex"
- <type> is addr
<value> is interpreted as <addr> described as above.
- 'livepatch' will read patch instructions from standard input.
You can write them in a file or pass them through pipe.
EXAMPLES
run a target process in some terminal
$ ./target
apply patch with 'livepatch' in another terminal
$ echo 'dl foo patch.so
jmp func_J $foo:func1
set str_P addr $foo:patch_message' | livepatch $(pidof target) ./target
Then, target's func_J function is replaced with func1 function in patch.so
and target's str_P pointer variable points to patch_message in patch.so.
FILES
Makefile - makefile
README - this file
livepatch.c - souce file of livepatch
bfd.c - bfd test program while hacking livepatch
fixup.c - fixuping test program while hacking livepatch
fixup.txt - fixup memo from glibc investigation
and http://ukai.jp/debuan/2002w/elf.txt
AUTHOR
Fumitoshi UKAI <ukai@debian.or.jp>
COPYING
* Copyright (C) 2004 Fumitoshi UKAI <ukai@debian.or.jp>
* All rights reserved.
* This is free software with ABSOLUTELY NO WARRANTY.
*
* You can redistribute it and/or modify it under the terms of
* the GNU General Public License version 2.
TODO
- TLS
- symbol versioning
- GOT hooking (ltrace?)
- linux/i386 architecture only.
even if target_alloc() is ported, maybe unportable in some part
(data size, alignment, ...)
- changing a pointer variable may not work correctly, because its value
is already copied in some other variables, stacks or registers
- changing non volatile pointer may not be reflected in process execution
because its pointer may be stored in register.
- check regs/stack before patching.
- interactive mode with readline?
- dynamic shlib replacement?
(e.g. libc.so upgrading without restarting daemon)
- language binding (ruby?)
- undo modification?
- gdb script?
http://ukai.jp/Software/livepatch/