@@ -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 *),
@@ -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;
}
}
@@ -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, rec, found_path);
if (!rc)
@@ -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);
@@ -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),
@@ -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);
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> --- libselinux/src/is_customizable_type.c | 7 ++++++- libselinux/src/label_backends_android.c | 5 ++++- libselinux/src/label_file.c | 16 +++++++++++++--- libselinux/src/label_media.c | 5 ++++- libselinux/src/label_support.c | 5 +++-- libselinux/src/label_x.c | 5 ++++- 6 files changed, 34 insertions(+), 9 deletions(-)