libselinux: check for stream rewind failures

Use fseek(3) instead of rewind(3) to detect failures.

Drop the final rewind in digest_add_specfile(), since all callers are
going to close the stream without any further action.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
This commit is contained in:
Christian Göttsche 2023-08-09 19:57:13 +02:00 committed by James Carter
parent 275daa4e0b
commit 9911f2ac6f
6 changed files with 34 additions and 9 deletions

View File

@ -31,7 +31,12 @@ static void customizable_init(void)
while (fgets_unlocked(buf, selinux_page_size, fp) && ctr < UINT_MAX) {
ctr++;
}
rewind(fp);
if (fseek(fp, 0L, SEEK_SET) == -1) {
fclose(fp);
return;
}
if (ctr) {
list =
(char **) calloc(sizeof(char *),

View File

@ -208,7 +208,10 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
goto finish;
maxnspec = data->nspec;
rewind(fp);
status = fseek(fp, 0L, SEEK_SET);
if (status == -1)
goto finish;
}
}

View File

@ -519,12 +519,16 @@ static char *rolling_append(char *current, const char *suffix, size_t max)
return current;
}
static bool fcontext_is_binary(FILE *fp)
static int fcontext_is_binary(FILE *fp)
{
uint32_t magic;
int rc;
size_t len = fread(&magic, sizeof(magic), 1, fp);
rewind(fp);
rc = fseek(fp, 0L, SEEK_SET);
if (rc == -1)
return -1;
return (len && (magic == SELINUX_MAGIC_COMPILED_FCONTEXT));
}
@ -622,7 +626,13 @@ static int process_file(const char *path, const char *suffix,
if (fp == NULL)
return -1;
rc = fcontext_is_binary(fp) ?
rc = fcontext_is_binary(fp);
if (rc < 0) {
(void) fclose(fp);
return -1;
}
rc = rc ?
load_mmap(fp, sb.st_size, rec, found_path) :
process_text_file(fp, prefix, rec, found_path);
if (!rc)

View File

@ -130,7 +130,10 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
goto finish;
memset(data->spec_arr, 0, sizeof(spec_t)*data->nspec);
maxnspec = data->nspec;
rewind(fp);
status = fseek(fp, 0L, SEEK_SET);
if (status == -1)
goto finish;
}
}
free(line_buf);

View File

@ -174,12 +174,13 @@ int digest_add_specfile(struct selabel_digest *digest, FILE *fp,
digest->hashbuf = tmp_buf;
if (fp) {
rewind(fp);
if (fseek(fp, 0L, SEEK_SET) == -1)
return -1;
if (fread(digest->hashbuf + (digest->hashbuf_size - buf_len),
1, buf_len, fp) != buf_len)
return -1;
rewind(fp);
} else if (from_addr) {
tmp_buf = memcpy(digest->hashbuf +
(digest->hashbuf_size - buf_len),

View File

@ -157,7 +157,10 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
goto finish;
memset(data->spec_arr, 0, sizeof(spec_t)*data->nspec);
maxnspec = data->nspec;
rewind(fp);
status = fseek(fp, 0L, SEEK_SET);
if (status == -1)
goto finish;
}
}
free(line_buf);