File: insn_queue.c

package info (click to toggle)
libpulp 0.3.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,976 kB
  • sloc: ansic: 11,792; python: 1,216; sh: 881; makefile: 871; cpp: 582; asm: 387
file content (365 lines) | stat: -rw-r--r-- 9,506 bytes parent folder | download | duplicates (2)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
 *  libpulp - User-space Livepatching Library
 *
 *  Copyright (C) 2021 SUSE Software Solutions GmbH
 *
 *  This file is part of libpulp.
 *
 *  libpulp is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  libpulp is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with libpulp.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "insn_queue.h"

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>
#include <sys/mman.h>

#include "error.h"
#include "ulp_common.h"

/** Global instruction queue object.  */
insn_queue_t __ulp_insn_queue = { .version = INSNQ_CURR_VERSION };

static int
align_to(int val, int bytes)
{
  int mask = bytes - 1;
  return (val + mask) & (~mask);
}

/** @brief Get memory area to write an instruction to in the queue.
 *
 * This function will retrieve an area of memory in the queue object in which
 * an instruction of size `msg_size` can be writen to. The instruction is
 * appended to the end of the queue, and depending of the queue attribute
 * `discard_old_content` it may return NULL if there is pending operations in
 * the queue, or overwrite the instruction that is on the begining of the
 * queue. In case the instruction do not fit in the queue, NULL is returned.
 *
 * @param queue     The instruction queue object.
 * @param msg_size  Size of instruction to allocate area to.
 *
 * @return          Valid pointer to write to in success, NULL otherwise.

  */
void *
insnq_get_writable_area(struct insn_queue *queue, size_t msg_size)
{
  /* Write the msg_queue values in variables for briefness.  */
  int num_insns = queue->num_insns;
  int size = queue->size;
  char *buffer = queue->buffer;

  /* In case the message is empty or it is too large for the buffer, don't
   * bother even trying to insert it.  */
  if (msg_size == 0)
    return NULL;

  /* In case the instruction won't fit the queue, then quickly return with
     NULL as answer.  */
  if (msg_size + size > INSN_BUFFER_MAX) {
    return NULL;
  }

  /* Reserve area for write.  This breaks strict aliasing rules, so this file
     must be compiled with -fno-strict-aliasing.  */
  void *ret = &buffer[size];

  /* Update number of bytes.  */
  size += msg_size;
  num_insns++;

  /* Commit back to original object.  */

  queue->num_insns = num_insns;
  queue->size = size;

  return ret;
}

/** @brief Insert print instruction into the queue.
 *
 * @param queue    The instruction queue object.
 * @param string   String to print.
 */
ulp_error_t
insnq_insert_print(const char *string)
{
  insn_queue_t *queue = &__ulp_insn_queue;

  int string_size = strlen(string) + 1;
  int insn_size = align_to(sizeof(struct ulp_insn_print) + string_size, 4);
  struct ulp_insn_print *insn = insnq_get_writable_area(queue, insn_size);

  if (insn == NULL) {
    set_libpulp_error_state(EINSNQ);
    return EINSNQ;
  }

  insn->base.type = ULP_INSN_PRINT;
  insn->base.size = insn_size;
  memcpy(insn->bytes, string, string_size);

  return ENONE;
}

/** @brief Insert write instruction into the queue.
 *
 * @param queue    The instruction queue object.
 * @param addr     Address to patch.
 * @param n        Number of bytes to patch.
 * @param bytes    Bytes to patch with.
 */
ulp_error_t
insnq_insert_write(void *addr, int n, const void *bytes)
{
  insn_queue_t *queue = &__ulp_insn_queue;

  int insn_size = align_to(sizeof(struct ulp_insn_write) + n, 8);
  struct ulp_insn_write *insn = insnq_get_writable_area(queue, insn_size);

  if (insn == NULL) {
    set_libpulp_error_state(EINSNQ);
    return EINSNQ;
  }

  insn->base.type = ULP_INSN_WRITE;
  insn->base.size = insn_size;
  insn->n = n;
  insn->address = (uintptr_t)addr;
  memcpy(insn->bytes, bytes, n);

  return ENONE;
}

/** @brief Ensure that the instruction queue is empty.
 *
 * When a livepatch is triggered, the instruction queue must be empty in order
 * to safely insert instructions on it.  Otherwise, this means something bad
 * occured on ulp side which prevented the queue to be updated after the insns
 * were executed.
 *
 * This function will block livepatching if not empty.
 *
 * @return 0 if success, anything else if not empty
 *
 */
int
insnq_ensure_emptiness(void)
{
  insn_queue_t *queue = &__ulp_insn_queue;

  if (queue->num_insns > 0 || queue->size > 0) {
    WARN("WARN: instruction queue not empty. This is an indication that "
         "something went wrong on ulp side.");

    set_libpulp_error_state(EINSNQ);
    return 1;
  }

  return 0;
}


/*
 * Read one line from FD into BUF, which must be pre-allocated and large
 * enough to hold LEN characteres. The offset into FD is advanced by the
 * amount of bytes read.
 *
 * @return  -1 on error, 0 on End-of-file, or the amount of bytes read.
 */
static int
read_line(int fd, char *buf, size_t len)
{
  char *ptr;
  int retcode;
  size_t offset;

  /* Read one byte at a time, until a newline is found. */
  offset = 0;
  while (offset < len) {
    ptr = buf + offset;

    /* Read one byte. */
    retcode = read(fd, ptr, 1);

    /* Error with read syscall. */
    if (retcode == -1) {
      if (errno == EINTR || errno == EAGAIN)
        continue;
      else
        return -1;
    }

    /* Stop at EOF or EOL. */
    if (retcode == 0 || *ptr == '\n') {
      return offset;
    }

    offset++; /* Reading one byte at a time. */
  }

  /* EOL not found. */
  return -1;
}

/* @brief Retrieves the memory protection bits of the page containing ADDR.
 *
 * @param addr    Address of the page.
 * @return        If errors ocurred, return -1.
 */
static int __attribute((unused))
memory_protection_get(uintptr_t addr)
{
  char line[LINE_MAX];
  char *str;
  char *end;
  int fd;
  int result;
  int retcode;
  uintptr_t addr1;
  uintptr_t addr2;

  fd = open("/proc/self/maps", O_RDONLY);
  if (fd == -1)
    return -1;

  /* Iterate over /proc/self/maps lines. */
  result = -1;
  for (;;) {

    /* Read one line. */
    retcode = read_line(fd, line, LINE_MAX);
    if (retcode <= 0)
      break;

    /* Parse the address range in the current line. */
    str = line;
    addr1 = strtoul(str, &end, 16);
    str = end + 1; /* Skip the dash used in the range output. */
    addr2 = strtoul(str, &end, 16);

    /* Skip line if target address not within range. */
    if (addr < addr1 || addr >= addr2)
      continue;

    /* Otherwise, parse the memory protection bits. */
    result = 0;
    if (*(end + 1) == 'r')
      result |= PROT_READ;
    if (*(end + 2) == 'w')
      result |= PROT_WRITE;
    if (*(end + 3) == 'x')
      result |= PROT_EXEC;
    break;
  }

  close(fd);
  return result;
}

/* When we are testing insnq there are some functions we do not want in the
   compilation unit.  */
#ifndef DISABLE_INSNQ_FUNCS_FOR_TESTING

/** @brief Interpret WRITE instruction.
 *
 * @param insn    Instruction to interpet. Must be a WRITE instruction.
 *
 * @return        Size of interpreted instruction.
 */
int
insn_interpret_write(struct ulp_insn *insn)
{
  struct ulp_insn_write *winsn = (struct ulp_insn_write *)insn;

  uintptr_t page_mask, page_size;

  page_size = getpagesize();
  page_mask = ~(page_size - 1);

  uintptr_t page1 = winsn->address & page_mask;
  uintptr_t pagen = (winsn->address + winsn->n) & page_mask;

  int num_pages = 1 + (pagen - page1) / page_size;

  int prot[num_pages];

  for (int i = 0; i < num_pages; i++) {
    uintptr_t page = page1 + i * page_size;

    /* Make sure we always get the one with page size.  */
    libpulp_assert(page == (page & page_mask));

    prot[i] = memory_protection_get(page);

    if (prot[i] == -1) {
      WARN("Memory protection get error (%d page)", i);
      return errno;
    }
  }

  for (int i = 0; i < num_pages; i++) {
    uintptr_t page = page1 + i * page_size;
    if (mprotect((void *)page, page_size, prot[i] | PROT_WRITE)) {
      WARN("Memory protection set error (%d page)", i);
      return errno;
    }
  }

  memcpy((void *)winsn->address, winsn->bytes, winsn->n);

  /* Make sure we wrote that.  */
  if (memcmp((void *)winsn->address, winsn->bytes, winsn->n) != 0) {
    WARN("Failed to write at address 0x%lx", winsn->address);
  }

  for (int i = 0; i < num_pages; i++) {
    uintptr_t page = page1 + i * page_size;
    if (mprotect((void *)page, page_size, prot[i])) {
      WARN("Memory protection set error (%d page)", i);
      return errno;
    }
  }

  return insn->size;
}

#endif //DISABLE_INSNQ_FUNCS_FOR_TESTING

/** @brief Process global instruction queue.
  *
  * Processes the global instruction queue that should be sent to the `ulp`
  * command, but we may need to process this queue in the process side if we
  * are debugging libpulp (e.g. patch triggered from gdb interface).
  */
int
insnq_interpret_from_lib(void)
{
  /* Interpret global queue.  */
  struct insn_queue *queue = &__ulp_insn_queue;
  int ret = insnq_interpret(queue);

  /* Clean up the queue.  */
  memset(queue->buffer, 0, INSN_BUFFER_MAX);
  queue->num_insns = 0;
  queue->size = 0;

  return ret;
}