Message ID | 20230814132025.45364-21-cgzones@googlemail.com (mailing list archive) |
---|---|
State | Accepted |
Commit | 9911f2ac6f77 |
Delegated to: | Petr Lautrbach |
Headers | show |
Series | libselinux: rework selabel_file(5) database | expand |
On Mon, Aug 14, 2023 at 9:41 AM Christian Göttsche <cgzones@googlemail.com> wrote: > > 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> > --- > 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(-) > > diff --git a/libselinux/src/is_customizable_type.c b/libselinux/src/is_customizable_type.c > index f83e1e83..9be50174 100644 > --- a/libselinux/src/is_customizable_type.c > +++ b/libselinux/src/is_customizable_type.c > @@ -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 *), > diff --git a/libselinux/src/label_backends_android.c b/libselinux/src/label_backends_android.c > index c2d78360..6494f3cd 100644 > --- a/libselinux/src/label_backends_android.c > +++ b/libselinux/src/label_backends_android.c > @@ -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; > } > } > > diff --git a/libselinux/src/label_file.c b/libselinux/src/label_file.c > index 471fd56b..a5677411 100644 > --- a/libselinux/src/label_file.c > +++ b/libselinux/src/label_file.c > @@ -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) > diff --git a/libselinux/src/label_media.c b/libselinux/src/label_media.c > index f09461ab..b3443b47 100644 > --- a/libselinux/src/label_media.c > +++ b/libselinux/src/label_media.c > @@ -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); > diff --git a/libselinux/src/label_support.c b/libselinux/src/label_support.c > index 94fb6697..f7ab9292 100644 > --- a/libselinux/src/label_support.c > +++ b/libselinux/src/label_support.c > @@ -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), > diff --git a/libselinux/src/label_x.c b/libselinux/src/label_x.c > index e80bf107..e15190ca 100644 > --- a/libselinux/src/label_x.c > +++ b/libselinux/src/label_x.c > @@ -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); > -- > 2.40.1 >
On Wed, Oct 11, 2023 at 2:48 PM James Carter <jwcart2@gmail.com> wrote: > > On Mon, Aug 14, 2023 at 9:41 AM Christian Göttsche > <cgzones@googlemail.com> wrote: > > > > 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> > Merged. Thanks, Jim > > --- > > 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(-) > > > > diff --git a/libselinux/src/is_customizable_type.c b/libselinux/src/is_customizable_type.c > > index f83e1e83..9be50174 100644 > > --- a/libselinux/src/is_customizable_type.c > > +++ b/libselinux/src/is_customizable_type.c > > @@ -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 *), > > diff --git a/libselinux/src/label_backends_android.c b/libselinux/src/label_backends_android.c > > index c2d78360..6494f3cd 100644 > > --- a/libselinux/src/label_backends_android.c > > +++ b/libselinux/src/label_backends_android.c > > @@ -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; > > } > > } > > > > diff --git a/libselinux/src/label_file.c b/libselinux/src/label_file.c > > index 471fd56b..a5677411 100644 > > --- a/libselinux/src/label_file.c > > +++ b/libselinux/src/label_file.c > > @@ -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) > > diff --git a/libselinux/src/label_media.c b/libselinux/src/label_media.c > > index f09461ab..b3443b47 100644 > > --- a/libselinux/src/label_media.c > > +++ b/libselinux/src/label_media.c > > @@ -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); > > diff --git a/libselinux/src/label_support.c b/libselinux/src/label_support.c > > index 94fb6697..f7ab9292 100644 > > --- a/libselinux/src/label_support.c > > +++ b/libselinux/src/label_support.c > > @@ -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), > > diff --git a/libselinux/src/label_x.c b/libselinux/src/label_x.c > > index e80bf107..e15190ca 100644 > > --- a/libselinux/src/label_x.c > > +++ b/libselinux/src/label_x.c > > @@ -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); > > -- > > 2.40.1 > >
diff --git a/libselinux/src/is_customizable_type.c b/libselinux/src/is_customizable_type.c index f83e1e83..9be50174 100644 --- a/libselinux/src/is_customizable_type.c +++ b/libselinux/src/is_customizable_type.c @@ -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 *), diff --git a/libselinux/src/label_backends_android.c b/libselinux/src/label_backends_android.c index c2d78360..6494f3cd 100644 --- a/libselinux/src/label_backends_android.c +++ b/libselinux/src/label_backends_android.c @@ -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; } } diff --git a/libselinux/src/label_file.c b/libselinux/src/label_file.c index 471fd56b..a5677411 100644 --- a/libselinux/src/label_file.c +++ b/libselinux/src/label_file.c @@ -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) diff --git a/libselinux/src/label_media.c b/libselinux/src/label_media.c index f09461ab..b3443b47 100644 --- a/libselinux/src/label_media.c +++ b/libselinux/src/label_media.c @@ -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); diff --git a/libselinux/src/label_support.c b/libselinux/src/label_support.c index 94fb6697..f7ab9292 100644 --- a/libselinux/src/label_support.c +++ b/libselinux/src/label_support.c @@ -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), diff --git a/libselinux/src/label_x.c b/libselinux/src/label_x.c index e80bf107..e15190ca 100644 --- a/libselinux/src/label_x.c +++ b/libselinux/src/label_x.c @@ -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(-)