Skip to content

Commit

Permalink
Resolve path while creating executable
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagorb committed Dec 19, 2021
1 parent 62c9bc3 commit 31991c7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
4 changes: 2 additions & 2 deletions snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: suid-wrapper
base: core20
version: '0.1.3'
version: '0.1.4'
summary: Utility to create binary executables to be used with the suid flag
description: |
This utility lets you create an executable binary, with the suid flag set.
Expand Down Expand Up @@ -29,6 +29,6 @@ parts:
build-packages:
- make
override-build: |
APP_VERSION=0.1.3 make release
APP_VERSION=0.1.4 make release
mkdir -p $SNAPCRAFT_PART_INSTALL/bin
cp bin/release/suid-wrapper $SNAPCRAFT_PART_INSTALL/bin/suid-wrapper
22 changes: 21 additions & 1 deletion src/linker.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ static int generate_binary(struct arguments arguments)
{
return 1;
}
char resolved_path[PATH_MAX];
if (realpath(wrapper->argv[0], resolved_path) == NULL)
{
log_error("Unable to locate executable at \"%s\".\nPlease provide the absolute path to the binary.\n", wrapper->argv[0]);
return 1;
}

char marker[] = APP_NAME;
fwrite(marker, sizeof(marker), 1, output);
Expand Down Expand Up @@ -219,13 +225,27 @@ static int generate_binary(struct arguments arguments)

static wrapper *wrapper_build_from_args(struct arguments arguments)
{
if (arguments.argc < 1)
{
log_error("Invalid arguments for wrapper_build_from_args");
return NULL;
}

wrapper *result = wrapper_new(arguments.argc);
if (result == NULL)
{
return NULL;
}

for (int i = 0; i < result->argc; i++)
result->argv[0] = realpath(arguments.argv[0], NULL);
if (result->argv[0] == NULL)
{
log_error("Failed to resolve path to \"%s\".\n", arguments.argv[0]);
wrapper_destroy(result);
return NULL;
}

for (int i = 1; i < result->argc; i++)
{
argv_len_t len = strlen(arguments.argv[i]);
result->argv[i] = (char *)malloc(sizeof(char) * (len + 1));
Expand Down
9 changes: 8 additions & 1 deletion src/runner.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#define _XOPEN_SOURCE 500

#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include "wrapper.h"
Expand Down Expand Up @@ -45,7 +47,12 @@ int main(int argc, char **argv)
}

char *new_env[] = { NULL };
execve(new_argv[0], &new_argv[0], new_env);
int result = execve(new_argv[0], &new_argv[0], new_env);
if (result != 0)
{
fprintf(stderr, "Failed to execute program %s:\n\t%s", new_argv[0], strerror(errno));
return 1;
}
wrapper_destroy(wrapper);

return 0;
Expand Down
29 changes: 7 additions & 22 deletions src/wrapper.c
Original file line number Diff line number Diff line change
@@ -1,38 +1,23 @@
#define _XOPEN_SOURCE 500

#include <unistd.h>
#include <inttypes.h>
#include <linux/limits.h>
#include <string.h>
#include <malloc.h>
#include <stdbool.h>
#include <stdarg.h>
#include <stdlib.h>
#include "wrapper.h"

const uint64_t MAX_MEMORY = 5 * 1024 * 1024;

FILE *open_exe(const char *path)
{
char path1[PATH_MAX];
char path2[PATH_MAX];
char *current_path = path1;
char *next_path = path2;
strncpy(current_path, path, PATH_MAX - 1);
current_path[PATH_MAX - 1] = 0;

ssize_t path_len = readlink(current_path, next_path, PATH_MAX);
while (path_len > 0)
{
log_debug("Following symlink %s...\n", current_path);
next_path[path_len] = 0;
char *temp = current_path;
current_path = next_path;
next_path = temp;
path_len = readlink(current_path, next_path, PATH_MAX) != -1;
}
log_debug("Executable found in %s.\n", current_path);
char resolved_path[PATH_MAX];
if (realpath(path, resolved_path) == NULL)
{
return NULL;
}

return fopen(current_path, "r");
return fopen(resolved_path, "r");
}

wrapper *wrapper_new(int argc)
Expand Down

0 comments on commit 31991c7

Please sign in to comment.