Skip to content

Commit

Permalink
Add small gdb interface
Browse files Browse the repository at this point in the history
Livepatch load works but it doesn't modify the addresses because there
is nothing to interpret the insn_queue.

Signed-off-by: Giuliano Belinassi <[email protected]>
  • Loading branch information
giulianobelinassi committed Nov 11, 2024
1 parent 2d81243 commit a45a67a
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 13 deletions.
4 changes: 2 additions & 2 deletions include/minielf.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ long Elf_Load_Section(unsigned dest_size, unsigned char *dest,
/** ----- ELF functions related to ULP ----- . */

/** Get the .ulp section from the given ELF file. */
int Get_ULP_Section(unsigned dest_size, unsigned char *dest, const char *file);
long Get_ULP_Section(unsigned dest_size, unsigned char *dest, const char *file);

/** Get the .ulp.rev section from the given ELF file. */
int Get_ULP_REV_Section(unsigned dest_size, unsigned char *dest, const char *file);
long Get_ULP_REV_Section(unsigned dest_size, unsigned char *dest, const char *file);
3 changes: 2 additions & 1 deletion lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ libpulp_la_SOURCES = \
msg_queue.c \
insn_queue.c \
error.c \
minielf.c
minielf.c \
gdb_interface.c

libpulp_la_LDFLAGS = \
-ldl \
Expand Down
131 changes: 131 additions & 0 deletions lib/gdb_interface.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* 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. */

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

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

#include "config.h"
#include "ulp_common.h"
#include "ulp.h"
#include "minielf.h"
#include "error_common.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)
{
/* 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. */
return __ulp_apply_patch();
}


int
gdb_ulp_revert(const char *path)
{
/* 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. */
return __ulp_apply_patch();
}
20 changes: 10 additions & 10 deletions lib/minielf.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,15 @@ long Elf_Load_Section(unsigned dest_size, unsigned char *dest,
}

/** Load the .ulp section of livepatch of `file` into the buffer. */
static int
static long
Get_Elf_Section(unsigned dest_size, unsigned char *dest,
const char *section_name, const char *file)
{
/* Open ELF file. */
int elf_fd = open(file, O_RDONLY);
if (elf_fd < 0) {
warn("Unable to open file %s: %s", file, strerror(errno));
return ENOENT;
return -ENOENT;
}

/* Load ELF file header. */
Expand All @@ -205,7 +205,7 @@ Get_Elf_Section(unsigned dest_size, unsigned char *dest,
warn("File is not an ELF object.");

close(elf_fd);
return EINVAL;
return -EINVAL;
}

/* Load ELF section string table. */
Expand All @@ -221,27 +221,27 @@ Get_Elf_Section(unsigned dest_size, unsigned char *dest,
warn("Section %s not found.", section_name);

close(elf_fd);
return EINVAL;
return -EINVAL;
}

/* Load ELF section into dest. */
long x = Elf_Load_Section(dest_size, dest, &ulp_shdr, elf_fd);
if (x == 0) {
long len = Elf_Load_Section(dest_size, dest, &ulp_shdr, elf_fd);
if (len == 0) {
close(elf_fd);
return EINVAL;
return -EINVAL;
}

close(elf_fd);
return 0;
return len;
}

int
long
Get_ULP_Section(unsigned dest_size, unsigned char *dest, const char *file)
{
return Get_Elf_Section(dest_size, dest, ".ulp", file);
}

int
long
Get_ULP_REV_Section(unsigned dest_size, unsigned char *dest, const char *file)
{
return Get_Elf_Section(dest_size, dest, ".ulp.rev", file);
Expand Down
3 changes: 3 additions & 0 deletions tools/introspection.c
Original file line number Diff line number Diff line change
Expand Up @@ -1848,6 +1848,9 @@ extract_ulp_from_so_to_mem(const char *livepatch, bool revert, char **out,
char *final_meta = (char *)malloc(meta_size);
char *meta_head = final_meta;


/* FIXME: This is absurdly awkward. */

/* Copy the final metadata into final_meta buffer. Things works here as
* follows:
*
Expand Down

0 comments on commit a45a67a

Please sign in to comment.