diff mbox series

[02/15] checkpolicy: cleanup resources on parse error

Message ID 20240122135507.63506-2-cgzones@googlemail.com (mailing list archive)
State Accepted
Commit c2fc48be68ee
Delegated to: Petr Lautrbach
Headers show
Series [01/15] checkpolicy: add libfuzz based fuzzer | expand

Commit Message

Christian Göttsche Jan. 22, 2024, 1:54 p.m. UTC
Close the input file and free all memory by the queue and lexer on a
syntax or parse error.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 checkpolicy/parse_util.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

Comments

James Carter Feb. 13, 2024, 8:34 p.m. UTC | #1
On Mon, Jan 22, 2024 at 9:02 AM Christian Göttsche
<cgzones@googlemail.com> wrote:
>
> Close the input file and free all memory by the queue and lexer on a
> syntax or parse error.
>
> Signed-off-by: Christian Göttsche <cgzones@googlemail.com>

Acked-by: James Carter <jwcart2@gmail.com>

> ---
>  checkpolicy/parse_util.c | 26 +++++++++++++++++++-------
>  1 file changed, 19 insertions(+), 7 deletions(-)
>
> diff --git a/checkpolicy/parse_util.c b/checkpolicy/parse_util.c
> index f2d1e04d..eda814e1 100644
> --- a/checkpolicy/parse_util.c
> +++ b/checkpolicy/parse_util.c
> @@ -26,6 +26,7 @@ extern FILE *yyin;
>  extern void init_parser(int);
>  extern int yyparse(void);
>  extern void yyrestart(FILE *);
> +extern int yylex_destroy(void);
>  extern queue_t id_queue;
>  extern unsigned int policydb_errors;
>  extern policydb_t *policydbp;
> @@ -34,6 +35,8 @@ extern void set_source_file(const char *name);
>
>  int read_source_policy(policydb_t * p, const char *file, const char *progname)
>  {
> +       int rc = -1;
> +
>         yyin = fopen(file, "r");
>         if (!yyin) {
>                 fprintf(stderr, "%s:  unable to open %s:  %s\n", progname, file, strerror(errno));
> @@ -41,21 +44,26 @@ int read_source_policy(policydb_t * p, const char *file, const char *progname)
>         }
>         set_source_file(file);
>
> -       if ((id_queue = queue_create()) == NULL) {
> +       id_queue = queue_create();
> +       if (id_queue == NULL) {
>                 fprintf(stderr, "%s: out of memory!\n", progname);
> -               return -1;
> +               goto cleanup;
>         }
>
> +       mlspol = p->mls;
>         policydbp = p;
>         policydbp->name = strdup(file);
> -       mlspol = p->mls;
> +       if (!policydbp->name) {
> +               fprintf(stderr, "%s: out of memory!\n", progname);
> +               goto cleanup;
> +       }
>
>         init_parser(1);
>         if (yyparse() || policydb_errors) {
>                 fprintf(stderr,
>                         "%s:  error(s) encountered while parsing configuration\n",
>                         progname);
> -               return -1;
> +               goto cleanup;
>         }
>         rewind(yyin);
>         init_parser(2);
> @@ -65,11 +73,15 @@ int read_source_policy(policydb_t * p, const char *file, const char *progname)
>                 fprintf(stderr,
>                         "%s:  error(s) encountered while parsing configuration\n",
>                         progname);
> -               return -1;
> +               goto cleanup;
>         }
> -       queue_destroy(id_queue);
>
> +       rc = 0;
> +
> +cleanup:
> +       queue_destroy(id_queue);
>         fclose(yyin);
> +       yylex_destroy();
>
> -       return 0;
> +       return rc;
>  }
> --
> 2.43.0
>
>
James Carter March 4, 2024, 7:16 p.m. UTC | #2
On Tue, Feb 13, 2024 at 3:34 PM James Carter <jwcart2@gmail.com> wrote:
>
> On Mon, Jan 22, 2024 at 9:02 AM Christian Göttsche
> <cgzones@googlemail.com> wrote:
> >
> > Close the input file and free all memory by the queue and lexer on a
> > syntax or parse error.
> >
> > Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
>
> Acked-by: James Carter <jwcart2@gmail.com>
>
Merged.
Thanks,
Jim

> > ---
> >  checkpolicy/parse_util.c | 26 +++++++++++++++++++-------
> >  1 file changed, 19 insertions(+), 7 deletions(-)
> >
> > diff --git a/checkpolicy/parse_util.c b/checkpolicy/parse_util.c
> > index f2d1e04d..eda814e1 100644
> > --- a/checkpolicy/parse_util.c
> > +++ b/checkpolicy/parse_util.c
> > @@ -26,6 +26,7 @@ extern FILE *yyin;
> >  extern void init_parser(int);
> >  extern int yyparse(void);
> >  extern void yyrestart(FILE *);
> > +extern int yylex_destroy(void);
> >  extern queue_t id_queue;
> >  extern unsigned int policydb_errors;
> >  extern policydb_t *policydbp;
> > @@ -34,6 +35,8 @@ extern void set_source_file(const char *name);
> >
> >  int read_source_policy(policydb_t * p, const char *file, const char *progname)
> >  {
> > +       int rc = -1;
> > +
> >         yyin = fopen(file, "r");
> >         if (!yyin) {
> >                 fprintf(stderr, "%s:  unable to open %s:  %s\n", progname, file, strerror(errno));
> > @@ -41,21 +44,26 @@ int read_source_policy(policydb_t * p, const char *file, const char *progname)
> >         }
> >         set_source_file(file);
> >
> > -       if ((id_queue = queue_create()) == NULL) {
> > +       id_queue = queue_create();
> > +       if (id_queue == NULL) {
> >                 fprintf(stderr, "%s: out of memory!\n", progname);
> > -               return -1;
> > +               goto cleanup;
> >         }
> >
> > +       mlspol = p->mls;
> >         policydbp = p;
> >         policydbp->name = strdup(file);
> > -       mlspol = p->mls;
> > +       if (!policydbp->name) {
> > +               fprintf(stderr, "%s: out of memory!\n", progname);
> > +               goto cleanup;
> > +       }
> >
> >         init_parser(1);
> >         if (yyparse() || policydb_errors) {
> >                 fprintf(stderr,
> >                         "%s:  error(s) encountered while parsing configuration\n",
> >                         progname);
> > -               return -1;
> > +               goto cleanup;
> >         }
> >         rewind(yyin);
> >         init_parser(2);
> > @@ -65,11 +73,15 @@ int read_source_policy(policydb_t * p, const char *file, const char *progname)
> >                 fprintf(stderr,
> >                         "%s:  error(s) encountered while parsing configuration\n",
> >                         progname);
> > -               return -1;
> > +               goto cleanup;
> >         }
> > -       queue_destroy(id_queue);
> >
> > +       rc = 0;
> > +
> > +cleanup:
> > +       queue_destroy(id_queue);
> >         fclose(yyin);
> > +       yylex_destroy();
> >
> > -       return 0;
> > +       return rc;
> >  }
> > --
> > 2.43.0
> >
> >
diff mbox series

Patch

diff --git a/checkpolicy/parse_util.c b/checkpolicy/parse_util.c
index f2d1e04d..eda814e1 100644
--- a/checkpolicy/parse_util.c
+++ b/checkpolicy/parse_util.c
@@ -26,6 +26,7 @@  extern FILE *yyin;
 extern void init_parser(int);
 extern int yyparse(void);
 extern void yyrestart(FILE *);
+extern int yylex_destroy(void);
 extern queue_t id_queue;
 extern unsigned int policydb_errors;
 extern policydb_t *policydbp;
@@ -34,6 +35,8 @@  extern void set_source_file(const char *name);
 
 int read_source_policy(policydb_t * p, const char *file, const char *progname)
 {
+	int rc = -1;
+
 	yyin = fopen(file, "r");
 	if (!yyin) {
 		fprintf(stderr, "%s:  unable to open %s:  %s\n", progname, file, strerror(errno));
@@ -41,21 +44,26 @@  int read_source_policy(policydb_t * p, const char *file, const char *progname)
 	}
 	set_source_file(file);
 
-	if ((id_queue = queue_create()) == NULL) {
+	id_queue = queue_create();
+	if (id_queue == NULL) {
 		fprintf(stderr, "%s: out of memory!\n", progname);
-		return -1;
+		goto cleanup;
 	}
 
+	mlspol = p->mls;
 	policydbp = p;
 	policydbp->name = strdup(file);
-	mlspol = p->mls;
+	if (!policydbp->name) {
+		fprintf(stderr, "%s: out of memory!\n", progname);
+		goto cleanup;
+	}
 
 	init_parser(1);
 	if (yyparse() || policydb_errors) {
 		fprintf(stderr,
 			"%s:  error(s) encountered while parsing configuration\n",
 			progname);
-		return -1;
+		goto cleanup;
 	}
 	rewind(yyin);
 	init_parser(2);
@@ -65,11 +73,15 @@  int read_source_policy(policydb_t * p, const char *file, const char *progname)
 		fprintf(stderr,
 			"%s:  error(s) encountered while parsing configuration\n",
 			progname);
-		return -1;
+		goto cleanup;
 	}
-	queue_destroy(id_queue);
 
+	rc = 0;
+
+cleanup:
+	queue_destroy(id_queue);
 	fclose(yyin);
+	yylex_destroy();
 
-	return 0;
+	return rc;
 }