@@ -132,6 +132,137 @@ Congratulations on going through the sample! Go on and learn how the magic of
132
132
`kpmake `_ script works, read how the patch is `build under the hood `_ and how
133
133
it is applied by the `kpatch_user `_. Or even jump to our `hacking guide `_!
134
134
135
+ RHEL7 ``glibc `` sample
136
+ ----------------------
137
+
138
+ .. _`glibc sample` :
139
+
140
+ Most of the binaries in the system are coming from distribution packages so
141
+ building patches for them is different from the above. Here is how to do it.
142
+
143
+ This example builds ``glibc `` patch for old fashioned CVE-2015-0235 GHOST _
144
+ vulnerability for RHEL7. The build is done using `scripts/pkgbuild `_ and
145
+ package files are stored in ``../packages/rhel7/glibc/glibc-2.17-55.el7 ``.
146
+
147
+ Preparing environment
148
+ ~~~~~~~~~~~~~~~~~~~~~
149
+
150
+ First, we need the exactly the versions of tools and libs. Let's build a
151
+ docker _ image and container for that:
152
+
153
+ .. code :: console
154
+
155
+ $ docker build docker/kernelcare/centos7/gcc-4.8.2-16.el7 \
156
+ -t kernelcare/centos7:gcc-4.8.2-16.el7
157
+ ...
158
+ $ docker run -v $PWD:/libcare --cap-add SYS_PTRACE -it \
159
+ kernelcare/centos7:gcc-4.8.2-16.el7 /bin/bash
160
+ [root@... /]#
161
+
162
+ Now, from inside the container let's install vulnerable version of glibc:
163
+
164
+ .. code :: console
165
+
166
+ [root@... /]# yum downgrade -y --enablerepo=C7.0.1406-base \
167
+ glibc-2.17-55.el7 glibc-devel-2.17-55.el7 \
168
+ glibc-headers-2.17-55.el7 glibc-common-2.17-55.el7
169
+ ...
170
+
171
+ Build the ``libcare `` tools:
172
+
173
+ .. code :: console
174
+
175
+ [root@... /]# make -C /libcare/src clean all
176
+ ...
177
+
178
+ Now build and run the sample GHOST app that runs 16 threads to constantly check
179
+ whether the ``glibc `` is vulnerable to GHOST _ and prints a dot every time it founds
180
+ it still is:
181
+
182
+ .. code :: console
183
+
184
+ [root@... /]# cd /libcare/samples/ghost
185
+ [root@... ghost]# make
186
+ ...
187
+ [root@... ghost]# ./GHOST
188
+ ............^C
189
+
190
+ Press Ctrl-C to get your console back and let's start building the patch for
191
+ ``glibc ``.
192
+
193
+ Building the patch
194
+ ~~~~~~~~~~~~~~~~~~
195
+
196
+ The build is done in two stages.
197
+
198
+ First, the original package build is repeated with all the `intermediate
199
+ assembly files `_ stored and saved for later. This greatly helps to speed up
200
+ builds against the same base code. Run the following from inside our docker
201
+ container to prebuild ``glibc `` package:
202
+
203
+ .. code :: console
204
+
205
+ [root@... /]# cd /libcare/
206
+ [root@... /libcare]# ./scripts/pkgbuild -p packages/rhel7/glibc/glibc-2.17-55.el7
207
+ ...
208
+
209
+ This should download the package, do a regular RPM build with ``kpatch_cc ``
210
+ wrapper substituted for GCC and store the pre-built data into archive under
211
+ ``/kcdata `` directory:
212
+
213
+ .. code :: console
214
+
215
+ [root@... /libcare]# ls /kcdata
216
+ build.orig-glibc-2.17-55.el7.x86_64.rpm.tgz glibc-2.17-55.el7.src.rpm
217
+
218
+ Now let's build the patch, output will be verbose since it contains tests run
219
+ by the ``kp_patch_test `` defined in ``packages/rhel7/glibc/glibc-2.17-55.el7/info ``:
220
+
221
+ .. code :: console
222
+
223
+ [root@... /libcare]# ./scripts/pkgbuild packages/rhel7/glibc/glibc-2.17-55.el7
224
+ ...
225
+ [root@... /libcare]# ls /kcdata/kpatch*
226
+ /kcdata/kpatch-glibc-2.17-55.el7.x86_64.tgz
227
+
228
+ Unwrap build patches and run the GHOST _ sample:
229
+
230
+ .. code :: console
231
+
232
+ [root@... /libcare]# cd /kcdata
233
+ [root@... /kcdata]# tar xf kpatch*
234
+ [root@... /kcdata]# /libcare/samples/ghost/GHOST 2>/dev/null &
235
+ [root@... /kcdata]# patient_pid=$!
236
+
237
+ And, finally, patch it. All the threads of sample should stop when the GHOST
238
+ vulnerability is finally patched:
239
+
240
+ .. code :: console
241
+
242
+ [root@... /kcdata]# /libcare/src/kpatch_user -v patch -p $patient_pid \
243
+ root/kpatch-glibc-2.17-55.el7.x86_64
244
+ ...
245
+ 1 patch hunk(s) have been successfully applied to PID '...'
246
+ (Press Enter again)
247
+ [1]+ Done /libcare/samples/ghost/GHOST 2> /dev/null
248
+
249
+ You can patch any running application this way:
250
+
251
+ .. code :: console
252
+
253
+ [root@... /kcdata]# sleep 100 &
254
+ [root@... /kcdata]# patient_pid=$!
255
+ [root@... /kcdata]# /libcare/src/kpatch_user -v patch -p $patient_pid \
256
+ root/kpatch-glibc-2.17-55.el7.x86_64
257
+ ...
258
+ 1 patch hunk(s) have been successfully applied to PID '...'
259
+
260
+ Congratulations on finishing this rather confusing sample!
261
+
262
+ .. _GHOST : https://access.redhat.com/articles/1332213
263
+ .. _docker : https://www.docker.com/
264
+
265
+
135
266
136
267
.. contents ::
137
268
@@ -224,6 +355,8 @@ First, the original code is built as is either via make or via packaging
224
355
system. The build is done with compiler substituted to ``kpatch_cc `` wrapper.
225
356
Wrapper's behaviour is configured via environment variables.
226
357
358
+ .. _`intermediate assembly files` :
359
+
227
360
When ``kpatch_cc `` is invoked with ``KPATCH_STAGE=original `` it simply builds
228
361
the project while keeping intermediate assembly files under name
229
362
``.kpatch_${filename}.original.s `` invoking real compiler twice: first with the
@@ -347,6 +480,8 @@ Note that ``kpmake`` uses ``kpatch_cc`` under the hood. Read about it
347
480
Building patch for a package via ``scripts/pkgbuild ``
348
481
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
349
482
483
+ .. _`scripts/pkgbuild` :
484
+
350
485
The ``scripts/pkgbuild `` is responsible for the building of the patch
351
486
and prebuilding the original package and assembly files. At the moment
352
487
it only supports building of the RPM-based packages.
@@ -374,6 +509,7 @@ The project directory contains three main files:
374
509
packages URL and Docker container images with toolchain
375
510
(GCC/binutils) version required to properly build the package.
376
511
512
+ This is not used at the moment and left as information source for the users.
377
513
378
514
The Doctor: ``kpatch_user ``
379
515
---------------------------
@@ -1001,7 +1137,7 @@ Manual Patch Creation
1001
1137
Throughout this section availability of the kpatch tools are assumed. To
1002
1138
build them and add them into PATH do:
1003
1139
1004
- .. code :: shell
1140
+ .. code :: console
1005
1141
1006
1142
$ make -C src
1007
1143
$ export PATH=$PWD/src:$PATH
0 commit comments