diff mbox series

policycoreutils: handle argument counter of zero

Message ID 20220126145645.5236-1-cgzones@googlemail.com (mailing list archive)
State Accepted
Headers show
Series policycoreutils: handle argument counter of zero | expand

Commit Message

Christian Göttsche Jan. 26, 2022, 2:56 p.m. UTC
The number of arguments passed to main(), argc, can be zero if the
pathname passed to execve(2) is NULL, e.g. via:

    execve("/path/to/exe", {NULL}, {NULL});

Also avoid NULL pointer dereferences on the argument value.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 policycoreutils/run_init/open_init_pty.c | 2 +-
 policycoreutils/secon/secon.c            | 3 +++
 policycoreutils/setfiles/setfiles.c      | 6 +++++-
 3 files changed, 9 insertions(+), 2 deletions(-)

Comments

James Carter Jan. 26, 2022, 10:12 p.m. UTC | #1
On Wed, Jan 26, 2022 at 4:39 PM Christian Göttsche
<cgzones@googlemail.com> wrote:
>
> The number of arguments passed to main(), argc, can be zero if the
> pathname passed to execve(2) is NULL, e.g. via:
>
>     execve("/path/to/exe", {NULL}, {NULL});
>
> Also avoid NULL pointer dereferences on the argument value.
>
> Signed-off-by: Christian Göttsche <cgzones@googlemail.com>

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

> ---
>  policycoreutils/run_init/open_init_pty.c | 2 +-
>  policycoreutils/secon/secon.c            | 3 +++
>  policycoreutils/setfiles/setfiles.c      | 6 +++++-
>  3 files changed, 9 insertions(+), 2 deletions(-)
>
> diff --git a/policycoreutils/run_init/open_init_pty.c b/policycoreutils/run_init/open_init_pty.c
> index 150cb45e..19101c50 100644
> --- a/policycoreutils/run_init/open_init_pty.c
> +++ b/policycoreutils/run_init/open_init_pty.c
> @@ -244,7 +244,7 @@ int main(int argc, char *argv[])
>         rb_init(&inbuf, inbuf_mem, sizeof(inbuf_mem));
>         rb_init(&outbuf, outbuf_mem, sizeof(outbuf_mem));
>
> -       if (argc == 1) {
> +       if (argc < 2) {
>                 printf("usage: %s PROGRAM [ARGS]...\n", argv[0]);
>                 exit(1);
>         }
> diff --git a/policycoreutils/secon/secon.c b/policycoreutils/secon/secon.c
> index a0957d09..d624fa13 100644
> --- a/policycoreutils/secon/secon.c
> +++ b/policycoreutils/secon/secon.c
> @@ -333,6 +333,9 @@ static void cmd_line(int argc, char *argv[])
>                 opts->from_type = OPTS_FROM_CUR;
>
>         if (opts->from_type == OPTS_FROM_ARG) {
> +               if (!argv[0])
> +                       errx(EXIT_FAILURE, "No argument given");
> +
>                 opts->f.arg = argv[0];
>
>                 if (xstreq(argv[0], "-"))
> diff --git a/policycoreutils/setfiles/setfiles.c b/policycoreutils/setfiles/setfiles.c
> index 44cab46d..ab7016ac 100644
> --- a/policycoreutils/setfiles/setfiles.c
> +++ b/policycoreutils/setfiles/setfiles.c
> @@ -163,6 +163,10 @@ int main(int argc, char **argv)
>         policyfile = NULL;
>
>         r_opts.abort_on_error = 0;
> +       if (!argv[0]) {
> +               fprintf(stderr, "Called without required program name!\n");
> +               exit(-1);
> +       }
>         r_opts.progname = strdup(argv[0]);
>         if (!r_opts.progname) {
>                 fprintf(stderr, "%s:  Out of memory!\n", argv[0]);
> @@ -423,7 +427,7 @@ int main(int argc, char **argv)
>
>                 altpath = argv[optind];
>                 optind++;
> -       } else if (argc == 1)
> +       } else if (argc < 2)
>                 usage(argv[0]);
>
>         /* Set selabel_open options. */
> --
> 2.34.1
>
James Carter Feb. 7, 2022, 5:29 p.m. UTC | #2
On Wed, Jan 26, 2022 at 5:12 PM James Carter <jwcart2@gmail.com> wrote:
>
> On Wed, Jan 26, 2022 at 4:39 PM Christian Göttsche
> <cgzones@googlemail.com> wrote:
> >
> > The number of arguments passed to main(), argc, can be zero if the
> > pathname passed to execve(2) is NULL, e.g. via:
> >
> >     execve("/path/to/exe", {NULL}, {NULL});
> >
> > Also avoid NULL pointer dereferences on the argument value.
> >
> > Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
>
> Acked-by: James Carter <jwcart2@gmail.com>
>

Merged.
Thanks,
Jim

> > ---
> >  policycoreutils/run_init/open_init_pty.c | 2 +-
> >  policycoreutils/secon/secon.c            | 3 +++
> >  policycoreutils/setfiles/setfiles.c      | 6 +++++-
> >  3 files changed, 9 insertions(+), 2 deletions(-)
> >
> > diff --git a/policycoreutils/run_init/open_init_pty.c b/policycoreutils/run_init/open_init_pty.c
> > index 150cb45e..19101c50 100644
> > --- a/policycoreutils/run_init/open_init_pty.c
> > +++ b/policycoreutils/run_init/open_init_pty.c
> > @@ -244,7 +244,7 @@ int main(int argc, char *argv[])
> >         rb_init(&inbuf, inbuf_mem, sizeof(inbuf_mem));
> >         rb_init(&outbuf, outbuf_mem, sizeof(outbuf_mem));
> >
> > -       if (argc == 1) {
> > +       if (argc < 2) {
> >                 printf("usage: %s PROGRAM [ARGS]...\n", argv[0]);
> >                 exit(1);
> >         }
> > diff --git a/policycoreutils/secon/secon.c b/policycoreutils/secon/secon.c
> > index a0957d09..d624fa13 100644
> > --- a/policycoreutils/secon/secon.c
> > +++ b/policycoreutils/secon/secon.c
> > @@ -333,6 +333,9 @@ static void cmd_line(int argc, char *argv[])
> >                 opts->from_type = OPTS_FROM_CUR;
> >
> >         if (opts->from_type == OPTS_FROM_ARG) {
> > +               if (!argv[0])
> > +                       errx(EXIT_FAILURE, "No argument given");
> > +
> >                 opts->f.arg = argv[0];
> >
> >                 if (xstreq(argv[0], "-"))
> > diff --git a/policycoreutils/setfiles/setfiles.c b/policycoreutils/setfiles/setfiles.c
> > index 44cab46d..ab7016ac 100644
> > --- a/policycoreutils/setfiles/setfiles.c
> > +++ b/policycoreutils/setfiles/setfiles.c
> > @@ -163,6 +163,10 @@ int main(int argc, char **argv)
> >         policyfile = NULL;
> >
> >         r_opts.abort_on_error = 0;
> > +       if (!argv[0]) {
> > +               fprintf(stderr, "Called without required program name!\n");
> > +               exit(-1);
> > +       }
> >         r_opts.progname = strdup(argv[0]);
> >         if (!r_opts.progname) {
> >                 fprintf(stderr, "%s:  Out of memory!\n", argv[0]);
> > @@ -423,7 +427,7 @@ int main(int argc, char **argv)
> >
> >                 altpath = argv[optind];
> >                 optind++;
> > -       } else if (argc == 1)
> > +       } else if (argc < 2)
> >                 usage(argv[0]);
> >
> >         /* Set selabel_open options. */
> > --
> > 2.34.1
> >
diff mbox series

Patch

diff --git a/policycoreutils/run_init/open_init_pty.c b/policycoreutils/run_init/open_init_pty.c
index 150cb45e..19101c50 100644
--- a/policycoreutils/run_init/open_init_pty.c
+++ b/policycoreutils/run_init/open_init_pty.c
@@ -244,7 +244,7 @@  int main(int argc, char *argv[])
 	rb_init(&inbuf, inbuf_mem, sizeof(inbuf_mem));
 	rb_init(&outbuf, outbuf_mem, sizeof(outbuf_mem));
 
-	if (argc == 1) {
+	if (argc < 2) {
 		printf("usage: %s PROGRAM [ARGS]...\n", argv[0]);
 		exit(1);
 	}
diff --git a/policycoreutils/secon/secon.c b/policycoreutils/secon/secon.c
index a0957d09..d624fa13 100644
--- a/policycoreutils/secon/secon.c
+++ b/policycoreutils/secon/secon.c
@@ -333,6 +333,9 @@  static void cmd_line(int argc, char *argv[])
 		opts->from_type = OPTS_FROM_CUR;
 
 	if (opts->from_type == OPTS_FROM_ARG) {
+		if (!argv[0])
+			errx(EXIT_FAILURE, "No argument given");
+
 		opts->f.arg = argv[0];
 
 		if (xstreq(argv[0], "-"))
diff --git a/policycoreutils/setfiles/setfiles.c b/policycoreutils/setfiles/setfiles.c
index 44cab46d..ab7016ac 100644
--- a/policycoreutils/setfiles/setfiles.c
+++ b/policycoreutils/setfiles/setfiles.c
@@ -163,6 +163,10 @@  int main(int argc, char **argv)
 	policyfile = NULL;
 
 	r_opts.abort_on_error = 0;
+	if (!argv[0]) {
+		fprintf(stderr, "Called without required program name!\n");
+		exit(-1);
+	}
 	r_opts.progname = strdup(argv[0]);
 	if (!r_opts.progname) {
 		fprintf(stderr, "%s:  Out of memory!\n", argv[0]);
@@ -423,7 +427,7 @@  int main(int argc, char **argv)
 
 		altpath = argv[optind];
 		optind++;
-	} else if (argc == 1)
+	} else if (argc < 2)
 		usage(argv[0]);
 
 	/* Set selabel_open options. */