diff mbox series

libselinux: Ignore missing directories when -i is used

Message ID 20221031164617.3096227-1-vmojzis@redhat.com (mailing list archive)
State Accepted
Commit 2f71384f233a
Headers show
Series libselinux: Ignore missing directories when -i is used | expand

Commit Message

Vit Mojzis Oct. 31, 2022, 4:46 p.m. UTC
Currently "-i" only ignores a file whose parent directory exists. Start also
ignoring paths with missing components.

Fixes:
  # restorecon -i -v -R /var/log/missingdir/missingfile; echo $?
  255
  restorecon: SELinux: Could not get canonical path for /var/log/missingdir/missingfile restorecon: No such file or directory.

Signed-off-by: Vit Mojzis <vmojzis@redhat.com>
---
 libselinux/src/selinux_restorecon.c | 7 +++++++
 1 file changed, 7 insertions(+)

Comments

James Carter Nov. 7, 2022, 7:34 p.m. UTC | #1
On Mon, Oct 31, 2022 at 12:51 PM Vit Mojzis <vmojzis@redhat.com> wrote:
>
> Currently "-i" only ignores a file whose parent directory exists. Start also
> ignoring paths with missing components.
>
> Fixes:
>   # restorecon -i -v -R /var/log/missingdir/missingfile; echo $?
>   255
>   restorecon: SELinux: Could not get canonical path for /var/log/missingdir/missingfile restorecon: No such file or directory.
>
> Signed-off-by: Vit Mojzis <vmojzis@redhat.com>

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

> ---
>  libselinux/src/selinux_restorecon.c | 7 +++++++
>  1 file changed, 7 insertions(+)
>
> diff --git a/libselinux/src/selinux_restorecon.c b/libselinux/src/selinux_restorecon.c
> index 2d24559f..6b5f6921 100644
> --- a/libselinux/src/selinux_restorecon.c
> +++ b/libselinux/src/selinux_restorecon.c
> @@ -1108,6 +1108,10 @@ static int selinux_restorecon_common(const char *pathname_orig,
>                         pathname = realpath(pathname_orig, NULL);
>                         if (!pathname) {
>                                 free(basename_cpy);
> +                               /* missing parent directory */
> +                               if (state.flags.ignore_noent && errno == ENOENT) {
> +                                       return 0;
> +                               }
>                                 goto realpatherr;
>                         }
>                 } else {
> @@ -1121,6 +1125,9 @@ static int selinux_restorecon_common(const char *pathname_orig,
>                         free(dirname_cpy);
>                         if (!pathdnamer) {
>                                 free(basename_cpy);
> +                               if (state.flags.ignore_noent && errno == ENOENT) {
> +                                       return 0;
> +                               }
>                                 goto realpatherr;
>                         }
>                         if (!strcmp(pathdnamer, "/"))
> --
> 2.37.3
>
James Carter Nov. 9, 2022, 1:48 p.m. UTC | #2
On Mon, Nov 7, 2022 at 2:34 PM James Carter <jwcart2@gmail.com> wrote:
>
> On Mon, Oct 31, 2022 at 12:51 PM Vit Mojzis <vmojzis@redhat.com> wrote:
> >
> > Currently "-i" only ignores a file whose parent directory exists. Start also
> > ignoring paths with missing components.
> >
> > Fixes:
> >   # restorecon -i -v -R /var/log/missingdir/missingfile; echo $?
> >   255
> >   restorecon: SELinux: Could not get canonical path for /var/log/missingdir/missingfile restorecon: No such file or directory.
> >
> > Signed-off-by: Vit Mojzis <vmojzis@redhat.com>
>
> Acked-by: James Carter <jwcart2@gmail.com>
>

Merged.
Thanks,
Jim

> > ---
> >  libselinux/src/selinux_restorecon.c | 7 +++++++
> >  1 file changed, 7 insertions(+)
> >
> > diff --git a/libselinux/src/selinux_restorecon.c b/libselinux/src/selinux_restorecon.c
> > index 2d24559f..6b5f6921 100644
> > --- a/libselinux/src/selinux_restorecon.c
> > +++ b/libselinux/src/selinux_restorecon.c
> > @@ -1108,6 +1108,10 @@ static int selinux_restorecon_common(const char *pathname_orig,
> >                         pathname = realpath(pathname_orig, NULL);
> >                         if (!pathname) {
> >                                 free(basename_cpy);
> > +                               /* missing parent directory */
> > +                               if (state.flags.ignore_noent && errno == ENOENT) {
> > +                                       return 0;
> > +                               }
> >                                 goto realpatherr;
> >                         }
> >                 } else {
> > @@ -1121,6 +1125,9 @@ static int selinux_restorecon_common(const char *pathname_orig,
> >                         free(dirname_cpy);
> >                         if (!pathdnamer) {
> >                                 free(basename_cpy);
> > +                               if (state.flags.ignore_noent && errno == ENOENT) {
> > +                                       return 0;
> > +                               }
> >                                 goto realpatherr;
> >                         }
> >                         if (!strcmp(pathdnamer, "/"))
> > --
> > 2.37.3
> >
diff mbox series

Patch

diff --git a/libselinux/src/selinux_restorecon.c b/libselinux/src/selinux_restorecon.c
index 2d24559f..6b5f6921 100644
--- a/libselinux/src/selinux_restorecon.c
+++ b/libselinux/src/selinux_restorecon.c
@@ -1108,6 +1108,10 @@  static int selinux_restorecon_common(const char *pathname_orig,
 			pathname = realpath(pathname_orig, NULL);
 			if (!pathname) {
 				free(basename_cpy);
+				/* missing parent directory */
+				if (state.flags.ignore_noent && errno == ENOENT) {
+					return 0;
+				}
 				goto realpatherr;
 			}
 		} else {
@@ -1121,6 +1125,9 @@  static int selinux_restorecon_common(const char *pathname_orig,
 			free(dirname_cpy);
 			if (!pathdnamer) {
 				free(basename_cpy);
+				if (state.flags.ignore_noent && errno == ENOENT) {
+					return 0;
+				}
 				goto realpatherr;
 			}
 			if (!strcmp(pathdnamer, "/"))