Skip to content

Commit

Permalink
libselinux: simplify string formatting
Browse files Browse the repository at this point in the history
Instead of using asprintf(3) and heavy string formatting just manually
concatenate the substitution string.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
  • Loading branch information
cgzones authored and jwcart2 committed Dec 4, 2024
1 parent 4d436e4 commit 3ff5f9e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
38 changes: 26 additions & 12 deletions libselinux/src/label_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -1198,9 +1198,9 @@ static void selabel_subs_fini(struct selabel_sub *subs, uint32_t num)
free(subs);
}

static char *selabel_apply_subs(const struct selabel_sub *subs, uint32_t num, const char *src)
static char *selabel_apply_subs(const struct selabel_sub *subs, uint32_t num, const char *src, size_t slen)
{
char *dst;
char *dst, *tmp;
uint32_t len;

for (uint32_t i = 0; i < num; i++) {
Expand All @@ -1214,8 +1214,14 @@ static char *selabel_apply_subs(const struct selabel_sub *subs, uint32_t num, co
len = ptr->slen + 1;
else
len = ptr->slen;
if (asprintf(&dst, "%s%s", ptr->dst, &src[len]) < 0)

dst = malloc(ptr->dlen + slen - len + 1);
if (!dst)
return NULL;

tmp = mempcpy(dst, ptr->dst, ptr->dlen);
tmp = mempcpy(tmp, &src[len], slen - len);
*tmp = '\0';
return dst;
}
}
Expand Down Expand Up @@ -1251,7 +1257,7 @@ static int selabel_subs_init(const char *path, struct selabel_digest *digest,
char *ptr;
char *src = buf;
char *dst;
size_t len;
size_t slen, dlen;

while (*src && isspace((unsigned char)*src))
src++;
Expand All @@ -1272,8 +1278,14 @@ static int selabel_subs_init(const char *path, struct selabel_digest *digest,
if (! *dst)
continue;

len = strlen(src);
if (len >= UINT32_MAX) {
slen = strlen(src);
if (slen >= UINT32_MAX) {
errno = EINVAL;
goto err;
}

dlen = strlen(dst);
if (dlen >= UINT32_MAX) {
errno = EINVAL;
goto err;
}
Expand All @@ -1292,8 +1304,9 @@ static int selabel_subs_init(const char *path, struct selabel_digest *digest,

tmp[tmp_num++] = (struct selabel_sub) {
.src = src_cpy,
.slen = len,
.slen = slen,
.dst = dst_cpy,
.dlen = dlen,
};
src_cpy = NULL;
dst_cpy = NULL;
Expand Down Expand Up @@ -1327,19 +1340,19 @@ static int selabel_subs_init(const char *path, struct selabel_digest *digest,
}
#endif

static char *selabel_sub_key(const struct saved_data *data, const char *key)
static char *selabel_sub_key(const struct saved_data *data, const char *key, size_t key_len)
{
char *ptr, *dptr;

ptr = selabel_apply_subs(data->subs, data->subs_num, key);
ptr = selabel_apply_subs(data->subs, data->subs_num, key, key_len);
if (ptr) {
dptr = selabel_apply_subs(data->dist_subs, data->dist_subs_num, ptr);
dptr = selabel_apply_subs(data->dist_subs, data->dist_subs_num, ptr, strlen(ptr));
if (dptr) {
free(ptr);
ptr = dptr;
}
} else {
ptr = selabel_apply_subs(data->dist_subs, data->dist_subs_num, key);
ptr = selabel_apply_subs(data->dist_subs, data->dist_subs_num, key, key_len);
}

return ptr;
Expand Down Expand Up @@ -1855,9 +1868,10 @@ FUZZ_EXTERN struct lookup_result *lookup_all(struct selabel_handle *rec,

clean_key[len - 1] = '\0';
key = clean_key;
len--;
}

sub = selabel_sub_key(data, key);
sub = selabel_sub_key(data, key, len);
if (sub)
key = sub;

Expand Down
1 change: 1 addition & 0 deletions libselinux/src/label_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ struct selabel_sub {
char *src; /* source path prefix */
char *dst; /* substituted path prefix */
uint32_t slen; /* length of source path prefix */
uint32_t dlen; /* length of substituted path prefix */
};

/* A regular expression file security context specification */
Expand Down

0 comments on commit 3ff5f9e

Please sign in to comment.