File: gdb_interface.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 (160 lines) | stat: -rw-r--r-- 4,061 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
/*
 *  libpulp - User-space Livepatching Library
 *
 *  Copyright (C) 2024 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/>.
 */

/* Small interface that allows livepatches to be applied or reverted
 * within gdb.  There is no need to compile this file if
 *  ENABLE_GDB_INTERFACE is not defined.
 */

#include "config.h"

#ifdef ENABLE_GDB_INTERFACE

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <string.h>
#include <sys/types.h>
#include <link.h>

#include "ulp_common.h"
#include "ulp.h"
#include "minielf.h"
#include "error_common.h"
#include "insn_queue_lib.h"

extern char __ulp_metadata_buffer[ULP_METADATA_BUF_LEN];

int
inject_lp_path(const char *path, long metadata_size)
{
  /* FIXME: This is absurdly awkward.  */

  /* Copy the final metadata into final_meta buffer.  Things works here as
   * follows:
   *
   * 1. Copy the first 1 + 32 bytes containing the patch type and patch id.
   * 2. Copy the size of the path to the livepatch container file.
   * 3. Copy the path to the livepatch container file.
   * 4. Copy the remaining metadata stuff.
   *
   * We do it in this way so we don't have to carry the path to the patch
   * container with the patch. This info can be retrieved from the path to
   * patch and avoid problems regarding the application running in another path
   * than the ulp tool.
   *
   * See introspection.c: 1868.
   * */

  long metadata_left = 1 + 32;
  long metadata_right = metadata_size - metadata_left;


  char *head = &__ulp_metadata_buffer[metadata_left];

  uint32_t path_size = strlen(path) + 1;
  uint32_t path_object_size = sizeof(uint32_t) + path_size;

  /* Check if it will still fit the metadata buffer.  */
  if (metadata_size + path_object_size > ULP_METADATA_BUF_LEN) {
    /* Won't fit.  */
    return ENOMEM;
  }

  /* Shift right so it fits.  */
  memmove(head + path_object_size, head, metadata_right);

  /* Inject the path.  */
  memcpy(head, &path_size, sizeof(uint32_t));
  head += sizeof(uint32_t);

  memcpy(head, path, path_size);
  head += path_object_size;

  return 0;
}

int
gdb_ulp_apply(const char *path)
{
  int ret;

  /* Prepare the ULP metadata buffer.  */
  memset(__ulp_metadata_buffer, '\0', ULP_METADATA_BUF_LEN);

  /* Load the .ulp section into the metadata buffer.  */
  long len = Get_ULP_Section(ULP_METADATA_BUF_LEN, (void*)__ulp_metadata_buffer, path);
  if (len < 0) {
    /* Invalid.  */
    return (int) -len;
  }

  if (inject_lp_path(path, len)) {
    return EINVALIDULP;
  }

  /* Trigger the livepatch.  */
  if ((ret = __ulp_apply_patch()) != 0) {
    return ret;
  }

  /* Process instruction queue.  */
  if ((ret = insnq_interpret_from_lib()) != 0) {
    return ret;
  }

  return 0;
}


int
gdb_ulp_revert(const char *path)
{
  int ret;
  /* Prepare the ULP metadata buffer.  */
  memset(__ulp_metadata_buffer, '\0', ULP_METADATA_BUF_LEN);

  /* Load the .ulp section into the metadata buffer.  */
  long len = Get_ULP_REV_Section(ULP_METADATA_BUF_LEN, (void *)__ulp_metadata_buffer, path);
  if (len < 0) {
    /* Invalid.  */
    return (int) -len;
  }

  if (inject_lp_path(path, len)) {
    return EINVALIDULP;
  }

  /* Trigger the livepatch.  */
  if ((ret = __ulp_apply_patch()) != 0) {
    return ret;
  }

  /* Process instruction queue.  */
  if ((ret = insnq_interpret_from_lib()) != 0) {
    return ret;
  }

  return 0;
}

#endif //ENABLE_GDB_INTERFACE