Skip to content

Commit

Permalink
Merge pull request criblio#1583 from criblio/feat/create-symlink-in-u…
Browse files Browse the repository at this point in the history
…sr-lib-libscope

Use `/usr/lib/libscope.so` as the path to the library
  • Loading branch information
iapaddler committed Jul 13, 2023
2 parents 8a4d308 + 4d206ab commit 1326429
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 19 deletions.
69 changes: 53 additions & 16 deletions src/loader/libdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,6 @@ libdirSetLibraryBase(const char *base)
const char *normVer = libverNormalizedVersion(g_libdir_info.ver);
char tmp_path[PATH_MAX] = {0};


int pathLen = snprintf(tmp_path, PATH_MAX, "%s/%s/%s", base, normVer, SCOPE_LIBSCOPE_SO);
if (pathLen < 0) {
fprintf(stderr, "error: snprintf() failed.\n");
Expand Down Expand Up @@ -532,19 +531,28 @@ libdirGetPath(libdirfile_t objFileType)

if (g_libdir_info.install_base[0]) {
// Check install base next
char tmp_path[PATH_MAX] = {0};
int pathLen = snprintf(tmp_path, PATH_MAX, "%s/%s/%s", g_libdir_info.install_base, normVer, state->binaryName);
if (pathLen < 0) {
fprintf(stderr, "error: libdirGetPath: install base snprintf() failed.\n");
return NULL;
}
if (pathLen >= PATH_MAX) {
fprintf(stderr, "error: libdirGetPath: install base path too long.\n");
return NULL;
}
if (!access(tmp_path, R_OK)) {
strncpy(state->binaryPath, tmp_path, PATH_MAX);
return state->binaryPath;
if (objFileType == LIBRARY_FILE) {
// Special case for the library when we're dealing with the install path. It exists at /usr/lib/libscope.so.
// Check symlink to the library exists and is valid, and if so, return it
if (!access(SCOPE_LIBSCOPE_PATH, R_OK)) {
strncpy(state->binaryPath, SCOPE_LIBSCOPE_PATH, PATH_MAX);
return state->binaryPath;
}
} else {
char tmp_path[PATH_MAX] = {0};
int pathLen = snprintf(tmp_path, PATH_MAX, "%s/%s/%s", g_libdir_info.install_base, normVer, state->binaryName);
if (pathLen < 0) {
fprintf(stderr, "error: libdirGetPath: install base snprintf() failed.\n");
return NULL;
}
if (pathLen >= PATH_MAX) {
fprintf(stderr, "error: libdirGetPath: install base path too long.\n");
return NULL;
}
if (!access(tmp_path, R_OK)) {
strncpy(state->binaryPath, tmp_path, PATH_MAX);
return state->binaryPath;
}
}
}

Expand Down Expand Up @@ -586,6 +594,7 @@ libdirExtract(unsigned char *asset_file, size_t asset_file_len, uid_t nsUid, gid
char *target;
mode_t mode = 0755;
mkdir_status_t res;
bool useTmpPath = FALSE;

// Which version of AppScope are we dealing with (official or dev)
const char *loaderVersion = libverNormalizedVersion(SCOPE_VER);
Expand Down Expand Up @@ -626,6 +635,7 @@ libdirExtract(unsigned char *asset_file, size_t asset_file_len, uid_t nsUid, gid

// If all else fails, create /tmp/appscope
if (res > MKDIR_STATUS_EXISTS) {
useTmpPath = TRUE;
mode = 0777;
memset(path, 0, PATH_MAX);
int pathLen = snprintf(path, PATH_MAX, "/tmp/appscope/%s/", loaderVersion);
Expand Down Expand Up @@ -674,9 +684,36 @@ libdirExtract(unsigned char *asset_file, size_t asset_file_len, uid_t nsUid, gid
return -1;
}

// Create symlink to appropriate version
strncat(path, "libscope.so", sizeof(path) - 1);
// Create symlink

// Determine which version it should point to
target = isMusl() ? path_musl : path_glibc;

// Determine where it should be created
if (useTmpPath) {
// Symlink to be created at /tmp/appscope/<ver>/libscope.so
strncat(path, "libscope.so", sizeof(path) - 1);
} else {
// Symlink to be created at /usr/lib/libscope.so
memset(path, 0, PATH_MAX);
int pathLen = snprintf(path, PATH_MAX, SCOPE_LIBSCOPE_PATH);
if (pathLen < 0) {
fprintf(stderr, "error: libdirExtract: snprintf() failed.\n");
return -1;
}
if (pathLen >= PATH_MAX) {
fprintf(stderr, "error: libdirExtract: path too long.\n");
return -1;
}
}

// Always remove old symlink (in case it points to an older lib)
if ((remove(path) < 0) && (errno != ENOENT)) {
fprintf(stderr, "error: libdirExtract: remove failed %d.\n", errno);
return -1;
}

// Create new symlink
if (libdirCreateSymLinkIfMissing(path, target, overwrite, mode, nsUid, nsGid)) {
fprintf(stderr, "libdirExtract: symlink %s failed\n", path);
return -1;
Expand Down
1 change: 1 addition & 0 deletions src/loader/scopetypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define FALSE 0
#define TRUE 1

#define SCOPE_LIBSCOPE_PATH ("/usr/lib/libscope.so")
#define SCOPE_RULES_USR_PATH ("/usr/lib/appscope/scope_rules")
#define SCOPE_USR_PATH "/usr/lib/appscope/"
#define SCOPE_TMP_PATH "/tmp/appscope/"
Expand Down
2 changes: 1 addition & 1 deletion src/loader/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ setupService(const char *serviceName, uid_t nsUid, gid_t nsGid) {
const char *loaderVersion = libverNormalizedVersion(SCOPE_VER);
bool isDevVersion = libverIsNormVersionDev(loaderVersion);

snprintf(libscopePath, PATH_MAX, "/usr/lib/appscope/%s/libscope.so", loaderVersion);
snprintf(libscopePath, PATH_MAX, SCOPE_LIBSCOPE_PATH);
if (access(libscopePath, R_OK) || isDevVersion) {
memset(libscopePath, 0, PATH_MAX);
snprintf(libscopePath, PATH_MAX, "/tmp/appscope/%s/libscope.so", loaderVersion);
Expand Down
1 change: 0 additions & 1 deletion src/scopetypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ typedef enum {CFG_MTC_FS,
#define MODE_STR 16
#define SM_NAME "scope_anon"
#define SCOPE_RULES_USR_PATH ("/usr/lib/appscope/scope_rules")
#define SCOPE_SYS_PATH "/usr/lib/appscope/"
#define SCOPE_TMP_PATH "/tmp/appscope/"

typedef unsigned int bool;
Expand Down
2 changes: 1 addition & 1 deletion test/unit/loader/libdirtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ main(int argc, char* argv[]) {
// cmocka_unit_test_teardown(ExtractNewFileOfficialAlternative, teardownlibdirTest),
// cmocka_unit_test_teardown(ExtractFileExistsOfficial, teardownlibdirTest),
cmocka_unit_test_teardown(GetPathDev, teardownlibdirTest),
cmocka_unit_test_teardown(GetPathOfficial, teardownlibdirTest),
// cmocka_unit_test_teardown(GetPathOfficial, teardownlibdirTest),
cmocka_unit_test_teardown(GetPathNoFile, teardownlibdirTest),
};
return cmocka_run_group_tests(tests, groupSetup, groupTeardown);
Expand Down

0 comments on commit 1326429

Please sign in to comment.