Skip to content

Commit

Permalink
Support relative includes in ld.so.conf
Browse files Browse the repository at this point in the history
  • Loading branch information
haampie committed Dec 13, 2021
1 parent 9e3f03f commit d119581
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions libtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@

#define SMALL_VEC_SIZE 16
#define MAX_RECURSION_DEPTH 32
#define MAX_PATH_LENGTH 4096

// Libraries we do not show by default -- this reduces the verbosity quite a
// bit.
Expand Down Expand Up @@ -328,7 +329,7 @@ static void string_table_store(struct string_table_t *t, char const *str) {
}

static void string_table_copy_from_file(struct string_table_t *t, FILE *fptr) {
char c;
int c;
// TODO: this could be a bit more efficient...
while ((c = getc(fptr)) != '\0' && c != EOF) {
string_table_maybe_grow(t, 1);
Expand Down Expand Up @@ -417,11 +418,11 @@ static int check_absolute_paths(size_t *needed_not_found,
}

// Copy the path over.
char path[4096];
char path[MAX_PATH_LENGTH];
size_t len = strlen(st->arr + needed_buf_offsets->p[i]);

// Unlikely to happen but good to guard against
if (len >= 4096)
if (len >= MAX_PATH_LENGTH)
continue;

// Include \0
Expand Down Expand Up @@ -473,8 +474,8 @@ static int check_search_paths(struct found_t reason, size_t offset,
size_t depth, struct libtree_state_t *s,
struct compat_t compat) {
int exit_code = 0;
char path[4096];
char *path_end = path + 4096;
char path[MAX_PATH_LENGTH];
char *path_end = path + MAX_PATH_LENGTH;

struct string_table_t const *st = &s->string_table;

Expand Down Expand Up @@ -1199,7 +1200,7 @@ static int recurse(char *current_file, size_t depth, struct libtree_state_t *s,
}

// Store the ORIGIN string.
char origin[4096];
char origin[MAX_PATH_LENGTH];
char *last_slash = strrchr(current_file, '/');
if (last_slash != NULL) {
// Exclude the last slash
Expand Down Expand Up @@ -1390,13 +1391,14 @@ static int parse_ld_config_file(struct string_table_t *st, char *path) {
if (fptr == NULL)
return 1;

char c = '0';
char line[4096];
int c = 0;
char line[MAX_PATH_LENGTH];
char tmp[MAX_PATH_LENGTH];

while (c != EOF) {
size_t line_len = 0;
while ((c = getc(fptr)) != '\n' && c != EOF) {
if (line_len < 4095) {
if (line_len < MAX_PATH_LENGTH - 1) {
line[line_len++] = c;
}
}
Expand All @@ -1415,7 +1417,6 @@ static int parse_ld_config_file(struct string_table_t *st, char *path) {
*comment = '\0';

// Remove trailing whitespace
// although, whitespace is technically allowed in paths :think:
while (end != begin)
if (!isspace(*--end))
break;
Expand All @@ -1431,18 +1432,29 @@ static int parse_ld_config_file(struct string_table_t *st, char *path) {
if (strncmp(begin, "include", 7) == 0 && isspace(begin[7])) {
begin += 8;
// Remove more whitespace.
for (; isspace(*begin); ++begin) {
while (isspace(*begin))
++begin;

// Prepend current dir when include dir is relative.
if (*begin != '/') {
char *wd = strrchr(path, '/');
wd = wd == NULL ? strrchr(path, '\0') : wd;

// bytes until /
size_t wd_len = wd - path;
size_t include_len = end - begin + 1;

// just skip then.
if (wd_len + 1 + include_len >= MAX_PATH_LENGTH)
continue;

memcpy(tmp, path, wd_len);
tmp[wd_len] = '/';
memcpy(tmp + wd_len + 1, begin, include_len);
tmp[wd_len + 1 + include_len] = '\0';
begin = tmp;
}

// String can't be empty as it was trimmed and
// still had whitespace next to include.

// TODO: check if relative globbing to the
// current file is supported or not.
// We do *not* support it right now.
if (*begin != '/')
continue;

ld_conf_globbing(st, begin);
} else {
// Copy over and replace trailing \0 with :.
Expand Down

0 comments on commit d119581

Please sign in to comment.