Skip to content

Commit

Permalink
Merge pull request #3 from thiagorb/feature/add-user-env-vars
Browse files Browse the repository at this point in the history
Define user environment variables
  • Loading branch information
thiagorb authored Jul 14, 2023
2 parents 31991c7 + 85a3036 commit 5b1a6e1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 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.4'
version: '0.2.0'
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.4 make release
APP_VERSION=0.2.0 make release
mkdir -p $SNAPCRAFT_PART_INSTALL/bin
cp bin/release/suid-wrapper $SNAPCRAFT_PART_INSTALL/bin/suid-wrapper
34 changes: 28 additions & 6 deletions src/runner.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,20 @@
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <pwd.h>
#include "wrapper.h"

/**
* Format a string and return a pointer to the alloc'd result.
*/
#define ssprintf(...) ({ \
int required_bytes = snprintf(NULL, 0, __VA_ARGS__) + 1; \
char *result = (char *)malloc(sizeof(char) * required_bytes); \
snprintf(result, required_bytes, __VA_ARGS__); \
result; \
})

int main(int argc, char **argv)
{
FILE *self_exe = open_exe("/proc/self/exe");
Expand Down Expand Up @@ -36,6 +48,17 @@ int main(int argc, char **argv)
new_argv[wrapper->argc] = NULL;
}

uid_t uid = getuid();
gid_t gid = getgid();
char *user = getpwuid(uid)->pw_name;

char *new_env[] = {
ssprintf("SUID_WRAPPER_UID=%d", uid),
ssprintf("SUID_WRAPPER_GID=%d", gid),
ssprintf("SUID_WRAPPER_USER=%s", user),
NULL,
};

if (setuid(geteuid()) != 0)
{
log_error("Failed to set uid\n");
Expand All @@ -46,13 +69,12 @@ int main(int argc, char **argv)
log_error("Failed to set gid\n");
}

char *new_env[] = { NULL };
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;
}
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

0 comments on commit 5b1a6e1

Please sign in to comment.