Skip to contentSkip to conversation

fixfiles: Unmount temporary bind mounts on SIGINT

PATCH Superseded Petr Lautrbach 4 participants 15 messages Sep 15, 2022 – Nov 5, 2022 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fixfiles: Unmount temporary bind mounts on SIGINT

fixfiles -M relabel temporary bind mounts filestems before relabeling but it leaves a directory mounted in /tmp/tmp.XXXX when a user hits CTRL-C. It means that if the user run fixfiles -M relabel again and answered Y to clean out /tmp directory, it would remove all data from mounted fs.

---
 policycoreutils/scripts/fixfiles | 11 +++++++++++
 1 file changed, 11 insertions(+)
policycoreutils/scripts/fixfiles +11 -0
index c72ca0eb9d61..6811921970f2 100755
@@ -207,6 +207,15 @@ rpm -q --qf '[%{FILESTATES} %{FILENAMES}\n]' "$1" | grep '^0 ' | cut -f2- -d ' '
 [ ${PIPESTATUS[0]} != 0 ] && echo "$1 not found" >/dev/stderr
 }
 
+# unmount tmp bind mount before exit
+umount_TMP_MOUNT() {
+	if [ -n "$TMP_MOUNT" ]; then
+	     umount "${TMP_MOUNT}${m}" || exit 130
+	     rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+	fi
+	exit 130
+}
+
 #
 # restore
 # if called with -n will only check file context
@@ -251,6 +260,7 @@ case "$RESTORE_MODE" in
 	    else
 	        # we bind mount so we can fix the labels of files that have already been
 	        # mounted over
+	        trap umount_TMP_MOUNT SIGINT
 	        for m in `echo $FILESYSTEMSRW`; do
 	            TMP_MOUNT="$(mktemp -d)"
 	            test -z ${TMP_MOUNT+x} && echo "Unable to find temporary directory!" && exit 1
@@ -261,6 +271,7 @@ case "$RESTORE_MODE" in
 	            umount "${TMP_MOUNT}${m}" || exit 1
 	            rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
 	        done;
+	        trap SIGINT
 	    fi
 	else
 	    echo >&2 "fixfiles: No suitable file systems found"
Signature · 1 line
2.37.3
#1 lore raw
Quoted 50 lines · Petr Lautrbach

fixfiles -M relabel temporary bind mounts filestems before relabeling but it leaves a directory mounted in /tmp/tmp.XXXX when a user hits CTRL-C. It means that if the user run fixfiles -M relabel again and answered Y to clean out /tmp directory, it would remove all data from mounted fs.

---
 policycoreutils/scripts/fixfiles | 11 +++++++++++
 1 file changed, 11 insertions(+)
policycoreutils/scripts/fixfiles +11 -1
index c72ca0eb9d61..6811921970f2 100755
@@ -207,6 +207,15 @@ rpm -q --qf '[%{FILESTATES} %{FILENAMES}\n]' "$1" | grep '^0 ' | cut -f2- -d ' '
 [ ${PIPESTATUS[0]} != 0 ] && echo "$1 not found" >/dev/stderr
 }
 
+# unmount tmp bind mount before exit
+umount_TMP_MOUNT() {
+       if [ -n "$TMP_MOUNT" ]; then
+            umount "${TMP_MOUNT}${m}" || exit 130
+            rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+       fi
+       exit 130
+}
+
 #
 # restore
 # if called with -n will only check file context
@@ -251,6 +260,7 @@ case "$RESTORE_MODE" in
            else
                # we bind mount so we can fix the labels of files that have already been
                # mounted over
+               trap umount_TMP_MOUNT SIGINT
                for m in `echo $FILESYSTEMSRW`; do
                    TMP_MOUNT="$(mktemp -d)"
                    test -z ${TMP_MOUNT+x} && echo "Unable to find temporary directory!" && exit 1
@@ -261,6 +271,7 @@ case "$RESTORE_MODE" in
                    umount "${TMP_MOUNT}${m}" || exit 1
                    rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
                done;
+               trap SIGINT
            fi
        else
            echo >&2 "fixfiles: No suitable file systems found"
--
Quoted 2 lines

2.37.3

And what if the fixfiles process is terminated via SIGTERM or even SIGKILL? Or a power failure occurs at the wrong time? What if some other process leaves behind a bind mount / other mount in /tmp?

My suggestion would be to:
1) Change the trap from SIGINT to EXIT. That will cover both SIGTERM and SIGINT.
2) Additionally modify fullrelabel() to not traverse across mounts when doing the removing (+ possibly exit with an error whenever a mountpoint is encountered - OR - try to unmount the mounts instead of removing their contents).

Signature · 3 lines
Ondrej Mosnacek
Senior Software Engineer, Linux Security - SELinux kernel
Red Hat, Inc.
#2 lore raw
Quoted 48 lines · Ondrej Mosnacek
Quoted 47 lines · Petr Lautrbach

fixfiles -M relabel temporary bind mounts filestems before relabeling but it leaves a directory mounted in /tmp/tmp.XXXX when a user hits CTRL-C. It means that if the user run fixfiles -M relabel again and answered Y to clean out /tmp directory, it would remove all data from mounted fs.

---
 policycoreutils/scripts/fixfiles | 11 +++++++++++
 1 file changed, 11 insertions(+)
policycoreutils/scripts/fixfiles +11 -0
index c72ca0eb9d61..6811921970f2 100755
@@ -207,6 +207,15 @@ rpm -q --qf '[%{FILESTATES} %{FILENAMES}\n]' "$1" | grep '^0 ' | cut -f2- -d ' '
 [ ${PIPESTATUS[0]} != 0 ] && echo "$1 not found" >/dev/stderr
 }
 
+# unmount tmp bind mount before exit
+umount_TMP_MOUNT() {
+       if [ -n "$TMP_MOUNT" ]; then
+            umount "${TMP_MOUNT}${m}" || exit 130
+            rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+       fi
+       exit 130
+}
+
 #
 # restore
 # if called with -n will only check file context
@@ -251,6 +260,7 @@ case "$RESTORE_MODE" in
            else
                # we bind mount so we can fix the labels of files that have already been
                # mounted over
+               trap umount_TMP_MOUNT SIGINT
                for m in `echo $FILESYSTEMSRW`; do
                    TMP_MOUNT="$(mktemp -d)"
                    test -z ${TMP_MOUNT+x} && echo "Unable to find temporary directory!" && exit 1
@@ -261,6 +271,7 @@ case "$RESTORE_MODE" in
                    umount "${TMP_MOUNT}${m}" || exit 1
                    rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
                done;
+               trap SIGINT
            fi
Quoted 8 lines
Quoted 5 lines
policycoreutils/scripts/fixfiles +0 -1
        else
            echo >&2 "fixfiles: No suitable file systems found"
--

2.37.3

And what if the fixfiles process is terminated via SIGTERM or even SIGKILL?

Good point.

Quoted 1 line

Or a power failure occurs at the wrong time?

After power failure and reboot there's no bind mountpoints from fixfiles left.

Quoted 2 lines

What if some
other process leaves behind a bind mount / other mount in /tmp?

I don't know. But it's up to users to decide. If they are not sure, they should answer 'No' to /tmp clean up.

Quoted 2 lines

My suggestion would be to:
1) Change the trap from SIGINT to EXIT. That will cover both SIGTERM and SIGINT.

I'll send updated patch with this.

Quoted 4 lines

2) Additionally modify fullrelabel() to not traverse across mounts when doing the removing (+ possibly exit with an error whenever a mountpoint is encountered - OR - try to unmount the mounts instead of removing their contents).

Given that I don't know what was the original motivation for the cleaing code and users are asked whether they want to clean up /tmp and they are warned that they would need to reboot after this operation, I'm not sure how I would defend the change.

Thanks,

Petr

Quoted 4 lines
Signature · 3 lines
Ondrej Mosnacek
Senior Software Engineer, Linux Security - SELinux kernel
Red Hat, Inc.
#3 lore raw
Quoted 59 lines · Petr Lautrbach
Quoted 48 lines · Ondrej Mosnacek
Quoted 47 lines · Petr Lautrbach

fixfiles -M relabel temporary bind mounts filestems before relabeling but it leaves a directory mounted in /tmp/tmp.XXXX when a user hits CTRL-C. It means that if the user run fixfiles -M relabel again and answered Y to clean out /tmp directory, it would remove all data from mounted fs.

---
 policycoreutils/scripts/fixfiles | 11 +++++++++++
 1 file changed, 11 insertions(+)
policycoreutils/scripts/fixfiles +11 -0
index c72ca0eb9d61..6811921970f2 100755
@@ -207,6 +207,15 @@ rpm -q --qf '[%{FILESTATES} %{FILENAMES}\n]' "$1" | grep '^0 ' | cut -f2- -d ' '
 [ ${PIPESTATUS[0]} != 0 ] && echo "$1 not found" >/dev/stderr
 }
 
+# unmount tmp bind mount before exit
+umount_TMP_MOUNT() {
+       if [ -n "$TMP_MOUNT" ]; then
+            umount "${TMP_MOUNT}${m}" || exit 130
+            rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+       fi
+       exit 130
+}
+
 #
 # restore
 # if called with -n will only check file context
@@ -251,6 +260,7 @@ case "$RESTORE_MODE" in
            else
                # we bind mount so we can fix the labels of files that have already been
                # mounted over
+               trap umount_TMP_MOUNT SIGINT
                for m in `echo $FILESYSTEMSRW`; do
                    TMP_MOUNT="$(mktemp -d)"
                    test -z ${TMP_MOUNT+x} && echo "Unable to find temporary directory!" && exit 1
@@ -261,6 +271,7 @@ case "$RESTORE_MODE" in
                    umount "${TMP_MOUNT}${m}" || exit 1
                    rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
                done;
+               trap SIGINT
            fi
Quoted 8 lines
Quoted 5 lines
policycoreutils/scripts/fixfiles +0 -1
        else
            echo >&2 "fixfiles: No suitable file systems found"
--

2.37.3

And what if the fixfiles process is terminated via SIGTERM or even SIGKILL?

Quoted 6 lines

Good point.

Quoted 1 line

Or a power failure occurs at the wrong time?

After power failure and reboot there's no bind mountpoints from fixfiles left.

Indeed, sorry for the brainfart.

Quoted 5 lines
Quoted 2 lines

What if some
other process leaves behind a bind mount / other mount in /tmp?

I don't know. But it's up to users to decide. If they are not sure, they should answer 'No' to /tmp clean up.

I disagree. We shouldn't offer the user an easy way of shooting themselves in the foot for no good reason.

Quoted 12 lines
Quoted 4 lines

My suggestion would be to:
1) Change the trap from SIGINT to EXIT. That will cover both SIGTERM and SIGINT.

I'll send updated patch with this.

Quoted 4 lines

2) Additionally modify fullrelabel() to not traverse across mounts when doing the removing (+ possibly exit with an error whenever a mountpoint is encountered - OR - try to unmount the mounts instead of removing their contents).

Quoted 4 lines

Given that I don't know what was the original motivation for the cleaing code and users are asked whether they want to clean up /tmp and they are warned that they would need to reboot after this operation, I'm not sure how I would defend the change.

The reboot will not fix the deletion of all files caused by a leftover mount. If you don't want to harden the removal, then at least please add a big warning to the prompt that the operation will also attempt to delete files inside mounts mounted under /tmp. (But that's pretty silly, when you could instead just add -xdev and avoid the problem altogether...)

I would say the motivation is simply to force the re-creation of all temporary files in /tmp so that any changes in type transition rules are applied. It is illogical to remove also contents of mounts, as these will likely use different labeling rules (depending on the superblock or in case of a bind mount the original path). There is just no way that deleting across mounts would be anything but a horrible idea in this case.

--
Ondrej Mosnacek
Senior Software Engineer, Linux Security - SELinux kernel Red Hat, Inc.

#4 lore raw
Quoted 95 lines · Ondrej Mosnacek
Quoted 59 lines · Petr Lautrbach
Quoted 48 lines · Ondrej Mosnacek
Quoted 47 lines · Petr Lautrbach

fixfiles -M relabel temporary bind mounts filestems before relabeling but it leaves a directory mounted in /tmp/tmp.XXXX when a user hits CTRL-C. It means that if the user run fixfiles -M relabel again and answered Y to clean out /tmp directory, it would remove all data from mounted fs.

---
 policycoreutils/scripts/fixfiles | 11 +++++++++++
 1 file changed, 11 insertions(+)
policycoreutils/scripts/fixfiles +11 -0
index c72ca0eb9d61..6811921970f2 100755
@@ -207,6 +207,15 @@ rpm -q --qf '[%{FILESTATES} %{FILENAMES}\n]' "$1" | grep '^0 ' | cut -f2- -d ' '
 [ ${PIPESTATUS[0]} != 0 ] && echo "$1 not found" >/dev/stderr
 }
 
+# unmount tmp bind mount before exit
+umount_TMP_MOUNT() {
+       if [ -n "$TMP_MOUNT" ]; then
+            umount "${TMP_MOUNT}${m}" || exit 130
+            rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+       fi
+       exit 130
+}
+
 #
 # restore
 # if called with -n will only check file context
@@ -251,6 +260,7 @@ case "$RESTORE_MODE" in
            else
                # we bind mount so we can fix the labels of files that have already been
                # mounted over
+               trap umount_TMP_MOUNT SIGINT
                for m in `echo $FILESYSTEMSRW`; do
                    TMP_MOUNT="$(mktemp -d)"
                    test -z ${TMP_MOUNT+x} && echo "Unable to find temporary directory!" && exit 1
@@ -261,6 +271,7 @@ case "$RESTORE_MODE" in
                    umount "${TMP_MOUNT}${m}" || exit 1
                    rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
                done;
+               trap SIGINT
            fi
Quoted 8 lines
Quoted 5 lines
policycoreutils/scripts/fixfiles +0 -1
        else
            echo >&2 "fixfiles: No suitable file systems found"
--

2.37.3

And what if the fixfiles process is terminated via SIGTERM or even SIGKILL?

Quoted 6 lines

Good point.

Quoted 1 line

Or a power failure occurs at the wrong time?

After power failure and reboot there's no bind mountpoints from fixfiles left.

Indeed, sorry for the brainfart.

Quoted 5 lines
Quoted 2 lines

What if some
other process leaves behind a bind mount / other mount in /tmp?

I don't know. But it's up to users to decide. If they are not sure, they should answer 'No' to /tmp clean up.

I disagree. We shouldn't offer the user an easy way of shooting themselves in the foot for no good reason.

Quoted 12 lines
Quoted 4 lines

My suggestion would be to:
1) Change the trap from SIGINT to EXIT. That will cover both SIGTERM and SIGINT.

I'll send updated patch with this.

Quoted 4 lines

2) Additionally modify fullrelabel() to not traverse across mounts when doing the removing (+ possibly exit with an error whenever a mountpoint is encountered - OR - try to unmount the mounts instead of removing their contents).

Quoted 4 lines

Given that I don't know what was the original motivation for the cleaing code and users are asked whether they want to clean up /tmp and they are warned that they would need to reboot after this operation, I'm not sure how I would defend the change.

Quoted 6 lines

The reboot will not fix the deletion of all files caused by a leftover mount. If you don't want to harden the removal, then at least please add a big warning to the prompt that the operation will also attempt to delete files inside mounts mounted under /tmp. (But that's pretty silly, when you could instead just add -xdev and avoid the problem altogether...)

There's prompt which says:

"this command can remove all files in /tmp"

so users are already warned.

Quoted 8 lines

I would say the motivation is simply to force the re-creation of all temporary files in /tmp so that any changes in type transition rules are applied. It is illogical to remove also contents of mounts, as these will likely use different labeling rules (depending on the superblock or in case of a bind mount the original path). There is just no way that deleting across mounts would be anything but a horrible idea in this case.

My problem is that when users run fixfiles twice, it could remove files from /

Your problem is that when users use /tmp as a place for mountpoints, running fixfiles (even once) could remove data from their mount points inside /tmp.

They are just different issues.

Petr

#5 lore raw

PATCH v2 1/1 fixfiles: Unmount temporary bind mounts on SIGINT

In reply to Ondrej Mosnacek

fixfiles -M relabel temporary bind mounts filestems before relabeling but it leaves a directory mounted in /tmp/tmp.XXXX when a user hits CTRL-C. It means that if the user run fixfiles -M relabel again and answered Y to clean out /tmp directory, it would remove all data from mounted fs.

---
v2:

- set trap on EXIT instead of SIGINT

 policycoreutils/scripts/fixfiles | 11 +++++++++++
 1 file changed, 11 insertions(+)
policycoreutils/scripts/fixfiles +11 -0
index c72ca0eb9d61..d763fa83eefd 100755
@@ -207,6 +207,15 @@ rpm -q --qf '[%{FILESTATES} %{FILENAMES}\n]' "$1" | grep '^0 ' | cut -f2- -d ' '
 [ ${PIPESTATUS[0]} != 0 ] && echo "$1 not found" >/dev/stderr
 }
 
+# unmount tmp bind mount before exit
+umount_TMP_MOUNT() {
+	if [ -n "$TMP_MOUNT" ]; then
+	     umount "${TMP_MOUNT}${m}" || exit 130
+	     rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+	fi
+	exit 130
+}
+
 #
 # restore
 # if called with -n will only check file context
@@ -251,6 +260,7 @@ case "$RESTORE_MODE" in
 	    else
 	        # we bind mount so we can fix the labels of files that have already been
 	        # mounted over
+	        trap umount_TMP_MOUNT EXIT
 	        for m in `echo $FILESYSTEMSRW`; do
 	            TMP_MOUNT="$(mktemp -d)"
 	            test -z ${TMP_MOUNT+x} && echo "Unable to find temporary directory!" && exit 1
@@ -261,6 +271,7 @@ case "$RESTORE_MODE" in
 	            umount "${TMP_MOUNT}${m}" || exit 1
 	            rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
 	        done;
+	        trap EXIT
 	    fi
 	else
 	    echo >&2 "fixfiles: No suitable file systems found"
Signature · 1 line
2.37.3
#6 lore raw

PATCH v3 1/1 fixfiles: Unmount temporary bind mounts on SIGINT

fixfiles -M relabel temporary bind mounts filestems before relabeling but it leaves a directory mounted in /tmp/tmp.XXXX when a user hits CTRL-C. It means that if the user run fixfiles -M relabel again and answered Y to clean out /tmp directory, it would remove all data from mounted fs.

This patch changes the location where fixfiles mounts fs to /run and adds a handler for exit signals which tries to umount fs mounted by fixfiles.

---
v2:

- set trap on EXIT instead of SIGINT

v3:

- use /run instead of /tmp for mountpoints

 policycoreutils/scripts/fixfiles | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
policycoreutils/scripts/fixfiles +12 -1
index c72ca0eb9d61..acf5f0996c19 100755
@@ -207,6 +207,15 @@ rpm -q --qf '[%{FILESTATES} %{FILENAMES}\n]' "$1" | grep '^0 ' | cut -f2- -d ' '
 [ ${PIPESTATUS[0]} != 0 ] && echo "$1 not found" >/dev/stderr
 }
 
+# unmount tmp bind mount before exit
+umount_TMP_MOUNT() {
+	if [ -n "$TMP_MOUNT" ]; then
+	     umount "${TMP_MOUNT}${m}" || exit 130
+	     rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+	fi
+	exit 130
+}
+
 #
 # restore
 # if called with -n will only check file context
@@ -251,8 +260,9 @@ case "$RESTORE_MODE" in
 	    else
 	        # we bind mount so we can fix the labels of files that have already been
 	        # mounted over
+	        trap umount_TMP_MOUNT EXIT
 	        for m in `echo $FILESYSTEMSRW`; do
-	            TMP_MOUNT="$(mktemp -d)"
+	            TMP_MOUNT="$(mktemp -p /run -d fixfiles.XXXXXXXXXX)"
 	            test -z ${TMP_MOUNT+x} && echo "Unable to find temporary directory!" && exit 1
 
 	            mkdir -p "${TMP_MOUNT}${m}" || exit 1
@@ -261,6 +271,7 @@ case "$RESTORE_MODE" in
 	            umount "${TMP_MOUNT}${m}" || exit 1
 	            rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
 	        done;
+	        trap EXIT
 	    fi
 	else
 	    echo >&2 "fixfiles: No suitable file systems found"
Signature · 1 line
2.37.3
#7 lore raw
Quoted 10 lines · Petr Lautrbach

fixfiles -M relabel temporary bind mounts filestems before relabeling but it leaves a directory mounted in /tmp/tmp.XXXX when a user hits CTRL-C. It means that if the user run fixfiles -M relabel again and answered Y to clean out /tmp directory, it would remove all data from mounted fs.

This patch changes the location where fixfiles mounts fs to /run and adds a handler for exit signals which tries to umount fs mounted by fixfiles.

What about additionally using mount namespaces, if available:

@@ -256,10 +256,16 @@

                   test -z ${TMP_MOUNT+x} && echo "Unable to find

temporary directory!" && exit 1

                   mkdir -p "${TMP_MOUNT}${m}" || exit 1
-                   mount --bind "${m}" "${TMP_MOUNT}${m}" || exit 1
-                   ${SETFILES} ${VERBOSE} ${EXCLUDEDIRS} ${FORCEFLAG}
${THREADS} $* -q ${FC} -r "${TMP_MOUNT}" "${TMP_MOUNT}${m}"
-                   umount "${TMP_MOUNT}${m}" || exit 1
-                   rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+                   if ! unshare --mount /bin/sh -c "mount --bind

\"${m}\" \"${TMP_MOUNT}${m}\" || exit 1 && ${SETFILES} ${VERBOSE} ${EXCLUDEDIRS} ${FORCEFLAG} ${THREADS} $* -q ${FC} -r \"${TMP_MOUNT}\" \"${TMP_MOUNT}${m}\" || true"; then
+ echo "Creating new mount namespace failed,
operating in root namespace"
+ mount --bind "${m}" "${TMP_MOUNT}${m}" || exit 1
+ ${SETFILES} ${VERBOSE} ${EXCLUDEDIRS}
${FORCEFLAG} ${THREADS} $* -q ${FC} -r "${TMP_MOUNT}" "${TMP_MOUNT}${m}"
+ umount "${TMP_MOUNT}${m}" || exit 1
+ fi
+ if [ "${m}" != '/' ]; then
+ rmdir "${TMP_MOUNT}${m}" || echo "Error
cleaning up ${TMP_MOUNT}${m}."
+ fi
+ rmdir "${TMP_MOUNT}" || echo "Error cleaning
up ${TMP_MOUNT}."

               done;
           fi
       else

?

Quoted 59 lines

---
v2:

- set trap on EXIT instead of SIGINT

v3:

- use /run instead of /tmp for mountpoints

 policycoreutils/scripts/fixfiles | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
policycoreutils/scripts/fixfiles +12 -2
index c72ca0eb9d61..acf5f0996c19 100755
@@ -207,6 +207,15 @@ rpm -q --qf '[%{FILESTATES} %{FILENAMES}\n]' "$1" | grep '^0 ' | cut -f2- -d ' '
 [ ${PIPESTATUS[0]} != 0 ] && echo "$1 not found" >/dev/stderr
 }
 
+# unmount tmp bind mount before exit
+umount_TMP_MOUNT() {
+       if [ -n "$TMP_MOUNT" ]; then
+            umount "${TMP_MOUNT}${m}" || exit 130
+            rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+       fi
+       exit 130
+}
+
 #
 # restore
 # if called with -n will only check file context
@@ -251,8 +260,9 @@ case "$RESTORE_MODE" in
            else
                # we bind mount so we can fix the labels of files that have already been
                # mounted over
+               trap umount_TMP_MOUNT EXIT
                for m in `echo $FILESYSTEMSRW`; do
-                   TMP_MOUNT="$(mktemp -d)"
+                   TMP_MOUNT="$(mktemp -p /run -d fixfiles.XXXXXXXXXX)"
                    test -z ${TMP_MOUNT+x} && echo "Unable to find temporary directory!" && exit 1
 
                    mkdir -p "${TMP_MOUNT}${m}" || exit 1
@@ -261,6 +271,7 @@ case "$RESTORE_MODE" in
                    umount "${TMP_MOUNT}${m}" || exit 1
                    rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
                done;
+               trap EXIT
            fi
        else
            echo >&2 "fixfiles: No suitable file systems found"
--

2.37.3

#8 lore raw

(Resending without HTML...)

Quoted 9 lines · Christian Göttsche
Quoted 7 lines · Petr Lautrbach

fixfiles -M relabel temporary bind mounts filestems before relabeling but it leaves a directory mounted in /tmp/tmp.XXXX when a user hits CTRL-C. It means that if the user run fixfiles -M relabel again and answered Y to clean out /tmp directory, it would remove all data from mounted fs.

Quoted 5 lines
Quoted 3 lines

This patch changes the location where fixfiles mounts fs to /run and adds a handler for exit signals which tries to umount fs mounted by fixfiles.

What about additionally using mount namespaces, if available:

What benefit would it bring? (Sorry, I'm not very familiar with mount namespaces.)

Quoted 82 lines
(quoted diff) +0 -0
@@ -256,10 +256,16 @@
                   test -z ${TMP_MOUNT+x} && echo "Unable to find

temporary directory!" && exit 1

                   mkdir -p "${TMP_MOUNT}${m}" || exit 1
(quoted diff) +0 -2
-                   mount --bind "${m}" "${TMP_MOUNT}${m}" || exit 1
-                   ${SETFILES} ${VERBOSE} ${EXCLUDEDIRS} ${FORCEFLAG}

${THREADS} $* -q ${FC} -r "${TMP_MOUNT}" "${TMP_MOUNT}${m}"

(quoted diff) +1 -2
-                   umount "${TMP_MOUNT}${m}" || exit 1
-                   rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+                   if ! unshare --mount /bin/sh -c "mount --bind

\"${m}\" \"${TMP_MOUNT}${m}\" || exit 1 && ${SETFILES} ${VERBOSE} ${EXCLUDEDIRS} ${FORCEFLAG} ${THREADS} $* -q ${FC} -r \"${TMP_MOUNT}\" \"${TMP_MOUNT}${m}\" || true"; then
+ echo "Creating new mount namespace failed,
operating in root namespace"

(quoted diff) +2 -0
+                       mount --bind "${m}" "${TMP_MOUNT}${m}" || exit 1
+                       ${SETFILES} ${VERBOSE} ${EXCLUDEDIRS}

${FORCEFLAG} ${THREADS} $* -q ${FC} -r "${TMP_MOUNT}" "${TMP_MOUNT}${m}"

(quoted diff) +4 -0
+                       umount "${TMP_MOUNT}${m}" || exit 1
+                    fi
+                    if [ "${m}" != '/' ]; then
+                        rmdir "${TMP_MOUNT}${m}" || echo "Error

cleaning up ${TMP_MOUNT}${m}."

(quoted diff) +2 -0
+                    fi
+                   rmdir "${TMP_MOUNT}"     || echo "Error cleaning

up ${TMP_MOUNT}."

               done;
           fi
       else

?

Quoted 47 lines

---
v2:

- set trap on EXIT instead of SIGINT

v3:

- use /run instead of /tmp for mountpoints

 policycoreutils/scripts/fixfiles | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
policycoreutils/scripts/fixfiles +11 -1
index c72ca0eb9d61..acf5f0996c19 100755
@@ -207,6 +207,15 @@ rpm -q --qf '[%{FILESTATES} %{FILENAMES}\n]' "$1" | grep '^0 ' | cut -f2- -d ' '
 [ ${PIPESTATUS[0]} != 0 ] && echo "$1 not found" >/dev/stderr
 }
 
+# unmount tmp bind mount before exit
+umount_TMP_MOUNT() {
+       if [ -n "$TMP_MOUNT" ]; then
+            umount "${TMP_MOUNT}${m}" || exit 130
+            rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+       fi
+       exit 130
+}
+
 #
 # restore
 # if called with -n will only check file context
@@ -251,8 +260,9 @@ case "$RESTORE_MODE" in
            else
                # we bind mount so we can fix the labels of files that have already been
                # mounted over
+               trap umount_TMP_MOUNT EXIT
                for m in `echo $FILESYSTEMSRW`; do
-                   TMP_MOUNT="$(mktemp -d)"
+                   TMP_MOUNT="$(mktemp -p /run -d fixfiles.XXXXXXXXXX)"
                    test -z ${TMP_MOUNT+x} && echo "Unable to find temporary directory!" && exit 1
 
Quoted 13 lines
Quoted 10 lines
policycoreutils/scripts/fixfiles +1 -1
                    mkdir -p "${TMP_MOUNT}${m}" || exit 1
@@ -261,6 +271,7 @@ case "$RESTORE_MODE" in
                    umount "${TMP_MOUNT}${m}" || exit 1
                    rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
                done;
+               trap EXIT
            fi
        else
            echo >&2 "fixfiles: No suitable file systems found"
--
Quoted 2 lines

2.37.3

--
Ondrej Mosnacek
Senior Software Engineer, Linux Security - SELinux kernel Red Hat, Inc.

#9 lore raw
Quoted 14 lines · Ondrej Mosnacek

(Resending without HTML...)

Quoted 9 lines · Christian Göttsche
Quoted 7 lines · Petr Lautrbach

fixfiles -M relabel temporary bind mounts filestems before relabeling but it leaves a directory mounted in /tmp/tmp.XXXX when a user hits CTRL-C. It means that if the user run fixfiles -M relabel again and answered Y to clean out /tmp directory, it would remove all data from mounted fs.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

This patch changes the location where fixfiles mounts fs to /run and adds a handler for exit signals which tries to umount fs mounted by fixfiles.

What about additionally using mount namespaces, if available:

What benefit would it bring? (Sorry, I'm not very familiar with mount namespaces.)

The bind mounts will only be visible to the process and its children used in the unshare command (and processes explicitly joining that mount namespace) and not to other (parallel or subsequent) instances of fixfiles.

Quoted 102 lines
Quoted 83 lines
(quoted diff) +0 -0
@@ -256,10 +256,16 @@
                   test -z ${TMP_MOUNT+x} && echo "Unable to find

temporary directory!" && exit 1

                   mkdir -p "${TMP_MOUNT}${m}" || exit 1
(quoted diff) +0 -2
-                   mount --bind "${m}" "${TMP_MOUNT}${m}" || exit 1
-                   ${SETFILES} ${VERBOSE} ${EXCLUDEDIRS} ${FORCEFLAG}

${THREADS} $* -q ${FC} -r "${TMP_MOUNT}" "${TMP_MOUNT}${m}"

(quoted diff) +1 -2
-                   umount "${TMP_MOUNT}${m}" || exit 1
-                   rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+                   if ! unshare --mount /bin/sh -c "mount --bind

\"${m}\" \"${TMP_MOUNT}${m}\" || exit 1 && ${SETFILES} ${VERBOSE} ${EXCLUDEDIRS} ${FORCEFLAG} ${THREADS} $* -q ${FC} -r \"${TMP_MOUNT}\" \"${TMP_MOUNT}${m}\" || true"; then
+ echo "Creating new mount namespace failed,
operating in root namespace"

(quoted diff) +2 -0
+                       mount --bind "${m}" "${TMP_MOUNT}${m}" || exit 1
+                       ${SETFILES} ${VERBOSE} ${EXCLUDEDIRS}

${FORCEFLAG} ${THREADS} $* -q ${FC} -r "${TMP_MOUNT}" "${TMP_MOUNT}${m}"

(quoted diff) +4 -0
+                       umount "${TMP_MOUNT}${m}" || exit 1
+                    fi
+                    if [ "${m}" != '/' ]; then
+                        rmdir "${TMP_MOUNT}${m}" || echo "Error

cleaning up ${TMP_MOUNT}${m}."

(quoted diff) +2 -0
+                    fi
+                   rmdir "${TMP_MOUNT}"     || echo "Error cleaning

up ${TMP_MOUNT}."

               done;
           fi
       else

?

Quoted 47 lines

---
v2:

- set trap on EXIT instead of SIGINT

v3:

- use /run instead of /tmp for mountpoints

 policycoreutils/scripts/fixfiles | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
policycoreutils/scripts/fixfiles +11 -1
index c72ca0eb9d61..acf5f0996c19 100755
@@ -207,6 +207,15 @@ rpm -q --qf '[%{FILESTATES} %{FILENAMES}\n]' "$1" | grep '^0 ' | cut -f2- -d ' '
 [ ${PIPESTATUS[0]} != 0 ] && echo "$1 not found" >/dev/stderr
 }
 
+# unmount tmp bind mount before exit
+umount_TMP_MOUNT() {
+       if [ -n "$TMP_MOUNT" ]; then
+            umount "${TMP_MOUNT}${m}" || exit 130
+            rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+       fi
+       exit 130
+}
+
 #
 # restore
 # if called with -n will only check file context
@@ -251,8 +260,9 @@ case "$RESTORE_MODE" in
            else
                # we bind mount so we can fix the labels of files that have already been
                # mounted over
+               trap umount_TMP_MOUNT EXIT
                for m in `echo $FILESYSTEMSRW`; do
-                   TMP_MOUNT="$(mktemp -d)"
+                   TMP_MOUNT="$(mktemp -p /run -d fixfiles.XXXXXXXXXX)"
                    test -z ${TMP_MOUNT+x} && echo "Unable to find temporary directory!" && exit 1
 
Quoted 13 lines
Quoted 10 lines
policycoreutils/scripts/fixfiles +1 -1
                    mkdir -p "${TMP_MOUNT}${m}" || exit 1
@@ -261,6 +271,7 @@ case "$RESTORE_MODE" in
                    umount "${TMP_MOUNT}${m}" || exit 1
                    rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
                done;
+               trap EXIT
            fi
        else
            echo >&2 "fixfiles: No suitable file systems found"
--
Quoted 2 lines

2.37.3

--
Ondrej Mosnacek
Senior Software Engineer, Linux Security - SELinux kernel Red Hat, Inc.

#10 lore raw

PATCH v4 1/1 fixfiles: Unmount temporary bind mounts on SIGINT

In reply to Christian Göttsche

fixfiles -M relabel temporary bind mounts filestems before relabeling but it leaves a directory mounted in /tmp/tmp.XXXX when a user hits CTRL-C. It means that if the user run fixfiles -M relabel again and answered Y to clean out /tmp directory, it would remove all data from mounted fs.

This patch changes the location where fixfiles mounts fs to /run, uses private mount namespace via unshare and adds a handler for exit signals which tries to umount fs mounted by fixfiles.

---
v2:

- set trap on EXIT instead of SIGINT

v3:

- use /run instead of /tmp for mountpoints

v4:

- use mount namespace as suggested by Christian Göttsche <[email protected]> (September 16) (inbox)

 policycoreutils/scripts/fixfiles | 36 +++++++++++++++++++++++++-------
 1 file changed, 28 insertions(+), 8 deletions(-)
policycoreutils/scripts/fixfiles +28 -8
index c72ca0eb9d61..af64a5a567a6 100755
@@ -207,6 +207,25 @@ rpm -q --qf '[%{FILESTATES} %{FILENAMES}\n]' "$1" | grep '^0 ' | cut -f2- -d ' '
 [ ${PIPESTATUS[0]} != 0 ] && echo "$1 not found" >/dev/stderr
 }
 
+# unmount tmp bind mount before exit
+umount_TMP_MOUNT() {
+	if [ -n "$TMP_MOUNT" ]; then
+	     umount "${TMP_MOUNT}${m}" || exit 130
+	     rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+	fi
+	exit 130
+}
+
+fix_labels_on_mountpoint() {
+	test -z ${TMP_MOUNT+x} && echo "Unable to find temporary directory!" && exit 1
+	mkdir -p "${TMP_MOUNT}${m}" || exit 1
+	mount --bind "${m}" "${TMP_MOUNT}${m}" || exit 1
+	${SETFILES} ${VERBOSE} ${EXCLUDEDIRS} ${FORCEFLAG} ${THREADS} $* -q ${FC} -r "${TMP_MOUNT}" "${TMP_MOUNT}${m}"
+	umount "${TMP_MOUNT}${m}" || exit 1
+	rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+}
+export -f fix_labels_on_mountpoint
+
 #
 # restore
 # if called with -n will only check file context
@@ -252,14 +271,15 @@ case "$RESTORE_MODE" in
 	        # we bind mount so we can fix the labels of files that have already been
 	        # mounted over
 	        for m in `echo $FILESYSTEMSRW`; do
-	            TMP_MOUNT="$(mktemp -d)"
-	            test -z ${TMP_MOUNT+x} && echo "Unable to find temporary directory!" && exit 1
-
-	            mkdir -p "${TMP_MOUNT}${m}" || exit 1
-	            mount --bind "${m}" "${TMP_MOUNT}${m}" || exit 1
-	            ${SETFILES} ${VERBOSE} ${EXCLUDEDIRS} ${FORCEFLAG} ${THREADS} $* -q ${FC} -r "${TMP_MOUNT}" "${TMP_MOUNT}${m}"
-	            umount "${TMP_MOUNT}${m}" || exit 1
-	            rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+	          	TMP_MOUNT="$(mktemp -p /run -d fixfiles.XXXXXXXXXX)"
+	            export SETFILES VERBOSE EXCLUDEDIRS FORCEFLAG THREADS FC TMP_MOUNT m
+	            if type unshare &> /dev/null; then
+	                unshare -m bash -x -c "fix_labels_on_mountpoint" $* || exit $?
+	            else
+	                trap umount_TMP_MOUNT EXIT
+	                fix_labels_on_mountpoint $*
+	                trap EXIT
+	            fi
 	        done;
 	    fi
 	else
Signature · 1 line
2.37.3
#11 lore raw
Quoted 6 lines · Petr Lautrbach

fixfiles -M relabel temporary bind mounts filestems before relabeling but it leaves a directory mounted in /tmp/tmp.XXXX when a user hits CTRL-C. It means that if the user run fixfiles -M relabel again and answered Y to clean out /tmp directory, it would remove all data from mounted fs.

Quoted 7 lines

This patch changes the location where fixfiles mounts fs to /run, uses private mount namespace via unshare and adds a handler for exit signals which tries to umount fs mounted by fixfiles.

Is there anyone who objects?

Petr

Quoted 73 lines

---
v2:

- set trap on EXIT instead of SIGINT

v3:

- use /run instead of /tmp for mountpoints

v4:

- use mount namespace as suggested by Christian Göttsche <[email protected]> (September 16) (inbox)

 policycoreutils/scripts/fixfiles | 36 +++++++++++++++++++++++++-------
 1 file changed, 28 insertions(+), 8 deletions(-)
policycoreutils/scripts/fixfiles +28 -8
index c72ca0eb9d61..af64a5a567a6 100755
@@ -207,6 +207,25 @@ rpm -q --qf '[%{FILESTATES} %{FILENAMES}\n]' "$1" | grep '^0 ' | cut -f2- -d ' '
 [ ${PIPESTATUS[0]} != 0 ] && echo "$1 not found" >/dev/stderr
 }
 
+# unmount tmp bind mount before exit
+umount_TMP_MOUNT() {
+	if [ -n "$TMP_MOUNT" ]; then
+	     umount "${TMP_MOUNT}${m}" || exit 130
+	     rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+	fi
+	exit 130
+}
+
+fix_labels_on_mountpoint() {
+	test -z ${TMP_MOUNT+x} && echo "Unable to find temporary directory!" && exit 1
+	mkdir -p "${TMP_MOUNT}${m}" || exit 1
+	mount --bind "${m}" "${TMP_MOUNT}${m}" || exit 1
+	${SETFILES} ${VERBOSE} ${EXCLUDEDIRS} ${FORCEFLAG} ${THREADS} $* -q ${FC} -r "${TMP_MOUNT}" "${TMP_MOUNT}${m}"
+	umount "${TMP_MOUNT}${m}" || exit 1
+	rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+}
+export -f fix_labels_on_mountpoint
+
 #
 # restore
 # if called with -n will only check file context
@@ -252,14 +271,15 @@ case "$RESTORE_MODE" in
 	        # we bind mount so we can fix the labels of files that have already been
 	        # mounted over
 	        for m in `echo $FILESYSTEMSRW`; do
-	            TMP_MOUNT="$(mktemp -d)"
-	            test -z ${TMP_MOUNT+x} && echo "Unable to find temporary directory!" && exit 1
-
-	            mkdir -p "${TMP_MOUNT}${m}" || exit 1
-	            mount --bind "${m}" "${TMP_MOUNT}${m}" || exit 1
-	            ${SETFILES} ${VERBOSE} ${EXCLUDEDIRS} ${FORCEFLAG} ${THREADS} $* -q ${FC} -r "${TMP_MOUNT}" "${TMP_MOUNT}${m}"
-	            umount "${TMP_MOUNT}${m}" || exit 1
-	            rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+	          	TMP_MOUNT="$(mktemp -p /run -d fixfiles.XXXXXXXXXX)"
+	            export SETFILES VERBOSE EXCLUDEDIRS FORCEFLAG THREADS FC TMP_MOUNT m
+	            if type unshare &> /dev/null; then
+	                unshare -m bash -x -c "fix_labels_on_mountpoint" $* || exit $?
+	            else
+	                trap umount_TMP_MOUNT EXIT
+	                fix_labels_on_mountpoint $*
+	                trap EXIT
+	            fi
 	        done;
 	    fi
 	else
Signature · 1 line
2.37.3
#12 lore raw
Quoted 75 lines · Petr Lautrbach
Quoted 6 lines · Petr Lautrbach

fixfiles -M relabel temporary bind mounts filestems before relabeling but it leaves a directory mounted in /tmp/tmp.XXXX when a user hits CTRL-C. It means that if the user run fixfiles -M relabel again and answered Y to clean out /tmp directory, it would remove all data from mounted fs.

Quoted 7 lines

This patch changes the location where fixfiles mounts fs to /run, uses private mount namespace via unshare and adds a handler for exit signals which tries to umount fs mounted by fixfiles.

Is there anyone who objects?

Petr

Quoted 52 lines

---
v2:

- set trap on EXIT instead of SIGINT

v3:

- use /run instead of /tmp for mountpoints

v4:

- use mount namespace as suggested by Christian Göttsche <[email protected]> (September 16) (inbox)

 policycoreutils/scripts/fixfiles | 36 +++++++++++++++++++++++++-------
 1 file changed, 28 insertions(+), 8 deletions(-)
policycoreutils/scripts/fixfiles +19 -1
index c72ca0eb9d61..af64a5a567a6 100755
@@ -207,6 +207,25 @@ rpm -q --qf '[%{FILESTATES} %{FILENAMES}\n]' "$1" | grep '^0 ' | cut -f2- -d ' '
 [ ${PIPESTATUS[0]} != 0 ] && echo "$1 not found" >/dev/stderr
 }
 
+# unmount tmp bind mount before exit
+umount_TMP_MOUNT() {
+     if [ -n "$TMP_MOUNT" ]; then
+          umount "${TMP_MOUNT}${m}" || exit 130
+          rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+     fi
+     exit 130
+}
+
+fix_labels_on_mountpoint() {
+     test -z ${TMP_MOUNT+x} && echo "Unable to find temporary directory!" && exit 1
+     mkdir -p "${TMP_MOUNT}${m}" || exit 1
+     mount --bind "${m}" "${TMP_MOUNT}${m}" || exit 1
+     ${SETFILES} ${VERBOSE} ${EXCLUDEDIRS} ${FORCEFLAG} ${THREADS} $* -q ${FC} -r "${TMP_MOUNT}" "${TMP_MOUNT}${m}"
+     umount "${TMP_MOUNT}${m}" || exit 1
+     rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+}
+export -f fix_labels_on_mountpoint
+
 #
 # restore
 # if called with -n will only check file context
@@ -252,14 +271,15 @@ case "$RESTORE_MODE" in
              # we bind mount so we can fix the labels of files that have already been
              # mounted over
              for m in `echo $FILESYSTEMSRW`; do
-                 TMP_MOUNT="$(mktemp -d)"
Quoted 8 lines

policycoreutils/scripts/fixfiles case "$RESTORE_MODE" in · line 274

Quoted 8 lines
policycoreutils/scripts/fixfiles +1 -7
-                 test -z ${TMP_MOUNT+x} && echo "Unable to find temporary directory!" && exit 1
-
-                 mkdir -p "${TMP_MOUNT}${m}" || exit 1
-                 mount --bind "${m}" "${TMP_MOUNT}${m}" || exit 1
-                 ${SETFILES} ${VERBOSE} ${EXCLUDEDIRS} ${FORCEFLAG} ${THREADS} $* -q ${FC} -r "${TMP_MOUNT}" "${TMP_MOUNT}${m}"
-                 umount "${TMP_MOUNT}${m}" || exit 1
-                 rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+                     TMP_MOUNT="$(mktemp -p /run -d fixfiles.XXXXXXXXXX)"

Whitespace issue:

git apply ~/Downloads/v4-fixfiles-Unmount-temporary-bind-mounts-on-SIGINT.patch /home/christian/Downloads/v4-fixfiles-Unmount-temporary-bind-mounts-on-SIGINT.patch:137:
space before tab in indent.

                        TMP_MOUNT="$(mktemp -p /run -d fixfiles.XXXXXXXXXX)"

warning: 1 line adds whitespace errors.

Quoted 3 lines

policycoreutils/scripts/fixfiles case "$RESTORE_MODE" in · line 277

Quoted 3 lines
policycoreutils/scripts/fixfiles +3 -0
+                 export SETFILES VERBOSE EXCLUDEDIRS FORCEFLAG THREADS FC TMP_MOUNT m
+                 if type unshare &> /dev/null; then
+                     unshare -m bash -x -c "fix_labels_on_mountpoint" $* || exit $?

Two issues:

I.
The -x flag make the output unreadable (especially for check/verify).

II.
The option (e.g. -n for check/verify) is not passed, should be -c "fix_labels_on_mountpoint $*".

Quoted 11 lines
Quoted 10 lines
policycoreutils/scripts/fixfiles +5 -1
+                 else
+                     trap umount_TMP_MOUNT EXIT
+                     fix_labels_on_mountpoint $*
+                     trap EXIT
+                 fi
              done;
          fi
      else
--

2.37.3

#13 lore raw
Quoted 93 lines · Christian Göttsche
Quoted 75 lines · Petr Lautrbach
Quoted 6 lines · Petr Lautrbach

fixfiles -M relabel temporary bind mounts filestems before relabeling but it leaves a directory mounted in /tmp/tmp.XXXX when a user hits CTRL-C. It means that if the user run fixfiles -M relabel again and answered Y to clean out /tmp directory, it would remove all data from mounted fs.

Quoted 7 lines

This patch changes the location where fixfiles mounts fs to /run, uses private mount namespace via unshare and adds a handler for exit signals which tries to umount fs mounted by fixfiles.

Is there anyone who objects?

Petr

Quoted 52 lines

---
v2:

- set trap on EXIT instead of SIGINT

v3:

- use /run instead of /tmp for mountpoints

v4:

- use mount namespace as suggested by Christian Göttsche <[email protected]> (September 16) (inbox)

 policycoreutils/scripts/fixfiles | 36 +++++++++++++++++++++++++-------
 1 file changed, 28 insertions(+), 8 deletions(-)
policycoreutils/scripts/fixfiles +19 -1
index c72ca0eb9d61..af64a5a567a6 100755
@@ -207,6 +207,25 @@ rpm -q --qf '[%{FILESTATES} %{FILENAMES}\n]' "$1" | grep '^0 ' | cut -f2- -d ' '
 [ ${PIPESTATUS[0]} != 0 ] && echo "$1 not found" >/dev/stderr
 }
 
+# unmount tmp bind mount before exit
+umount_TMP_MOUNT() {
+     if [ -n "$TMP_MOUNT" ]; then
+          umount "${TMP_MOUNT}${m}" || exit 130
+          rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+     fi
+     exit 130
+}
+
+fix_labels_on_mountpoint() {
+     test -z ${TMP_MOUNT+x} && echo "Unable to find temporary directory!" && exit 1
+     mkdir -p "${TMP_MOUNT}${m}" || exit 1
+     mount --bind "${m}" "${TMP_MOUNT}${m}" || exit 1
+     ${SETFILES} ${VERBOSE} ${EXCLUDEDIRS} ${FORCEFLAG} ${THREADS} $* -q ${FC} -r "${TMP_MOUNT}" "${TMP_MOUNT}${m}"
+     umount "${TMP_MOUNT}${m}" || exit 1
+     rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+}
+export -f fix_labels_on_mountpoint
+
 #
 # restore
 # if called with -n will only check file context
@@ -252,14 +271,15 @@ case "$RESTORE_MODE" in
              # we bind mount so we can fix the labels of files that have already been
              # mounted over
              for m in `echo $FILESYSTEMSRW`; do
-                 TMP_MOUNT="$(mktemp -d)"
Quoted 8 lines

policycoreutils/scripts/fixfiles case "$RESTORE_MODE" in · line 274

Quoted 8 lines
policycoreutils/scripts/fixfiles +1 -7
-                 test -z ${TMP_MOUNT+x} && echo "Unable to find temporary directory!" && exit 1
-
-                 mkdir -p "${TMP_MOUNT}${m}" || exit 1
-                 mount --bind "${m}" "${TMP_MOUNT}${m}" || exit 1
-                 ${SETFILES} ${VERBOSE} ${EXCLUDEDIRS} ${FORCEFLAG} ${THREADS} $* -q ${FC} -r "${TMP_MOUNT}" "${TMP_MOUNT}${m}"
-                 umount "${TMP_MOUNT}${m}" || exit 1
-                 rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+                     TMP_MOUNT="$(mktemp -p /run -d fixfiles.XXXXXXXXXX)"

Whitespace issue:

git apply ~/Downloads/v4-fixfiles-Unmount-temporary-bind-mounts-on-SIGINT.patch /home/christian/Downloads/v4-fixfiles-Unmount-temporary-bind-mounts-on-SIGINT.patch:137:
space before tab in indent.

                        TMP_MOUNT="$(mktemp -p /run -d fixfiles.XXXXXXXXXX)"

warning: 1 line adds whitespace errors.

Quoted 8 lines
Quoted 3 lines

policycoreutils/scripts/fixfiles case "$RESTORE_MODE" in · line 277

Quoted 3 lines
policycoreutils/scripts/fixfiles +3 -0
+                 export SETFILES VERBOSE EXCLUDEDIRS FORCEFLAG THREADS FC TMP_MOUNT m
+                 if type unshare &> /dev/null; then
+                     unshare -m bash -x -c "fix_labels_on_mountpoint" $* || exit $?

Two issues:

I.
The -x flag make the output unreadable (especially for check/verify).

This is leftover from my debugging. It is not supposed to be there.

Quoted 3 lines

II.
The option (e.g. -n for check/verify) is not passed, should be -c "fix_labels_on_mountpoint $*".

I'll fix it.

Thanks!

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
policycoreutils/scripts/fixfiles +5 -1
+                 else
+                     trap umount_TMP_MOUNT EXIT
+                     fix_labels_on_mountpoint $*
+                     trap EXIT
+                 fi
              done;
          fi
      else
--

2.37.3

#14 lore raw
Quoted 16 lines · Petr Lautrbach
Quoted 14 lines · Christian Göttsche
Quoted 13 lines · Petr Lautrbach
Quoted 10 lines · Petr Lautrbach

fixfiles -M relabel temporary bind mounts filestems before relabeling but it leaves a directory mounted in /tmp/tmp.XXXX when a user hits CTRL-C. It means that if the user run fixfiles -M relabel again and answered Y to clean out /tmp directory, it would remove all data from mounted fs.

This patch changes the location where fixfiles mounts fs to /run, uses private mount namespace via unshare and adds a handler for exit signals which tries to umount fs mounted by fixfiles.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

Is there anyone who objects?

Unrelated but thought i'd mention it here:

Since at least Fedora uses btrfs by default on Workstation it might be worth it to look into adding support for btrfs (subvolumes) to fixfiles.

Quoted 108 lines
Quoted 74 lines
Quoted 56 lines

Petr

Quoted 52 lines

---
v2:

- set trap on EXIT instead of SIGINT

v3:

- use /run instead of /tmp for mountpoints

v4:

- use mount namespace as suggested by Christian Göttsche <[email protected]> (September 16) (inbox)

 policycoreutils/scripts/fixfiles | 36 +++++++++++++++++++++++++-------
 1 file changed, 28 insertions(+), 8 deletions(-)
policycoreutils/scripts/fixfiles +19 -1
index c72ca0eb9d61..af64a5a567a6 100755
@@ -207,6 +207,25 @@ rpm -q --qf '[%{FILESTATES} %{FILENAMES}\n]' "$1" | grep '^0 ' | cut -f2- -d ' '
 [ ${PIPESTATUS[0]} != 0 ] && echo "$1 not found" >/dev/stderr
 }
 
+# unmount tmp bind mount before exit
+umount_TMP_MOUNT() {
+     if [ -n "$TMP_MOUNT" ]; then
+          umount "${TMP_MOUNT}${m}" || exit 130
+          rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+     fi
+     exit 130
+}
+
+fix_labels_on_mountpoint() {
+     test -z ${TMP_MOUNT+x} && echo "Unable to find temporary directory!" && exit 1
+     mkdir -p "${TMP_MOUNT}${m}" || exit 1
+     mount --bind "${m}" "${TMP_MOUNT}${m}" || exit 1
+     ${SETFILES} ${VERBOSE} ${EXCLUDEDIRS} ${FORCEFLAG} ${THREADS} $* -q ${FC} -r "${TMP_MOUNT}" "${TMP_MOUNT}${m}"
+     umount "${TMP_MOUNT}${m}" || exit 1
+     rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+}
+export -f fix_labels_on_mountpoint
+
 #
 # restore
 # if called with -n will only check file context
@@ -252,14 +271,15 @@ case "$RESTORE_MODE" in
              # we bind mount so we can fix the labels of files that have already been
              # mounted over
              for m in `echo $FILESYSTEMSRW`; do
-                 TMP_MOUNT="$(mktemp -d)"
Quoted 8 lines

policycoreutils/scripts/fixfiles case "$RESTORE_MODE" in · line 274

Quoted 8 lines
policycoreutils/scripts/fixfiles +1 -7
-                 test -z ${TMP_MOUNT+x} && echo "Unable to find temporary directory!" && exit 1
-
-                 mkdir -p "${TMP_MOUNT}${m}" || exit 1
-                 mount --bind "${m}" "${TMP_MOUNT}${m}" || exit 1
-                 ${SETFILES} ${VERBOSE} ${EXCLUDEDIRS} ${FORCEFLAG} ${THREADS} $* -q ${FC} -r "${TMP_MOUNT}" "${TMP_MOUNT}${m}"
-                 umount "${TMP_MOUNT}${m}" || exit 1
-                 rm -rf "${TMP_MOUNT}" || echo "Error cleaning up."
+                     TMP_MOUNT="$(mktemp -p /run -d fixfiles.XXXXXXXXXX)"

Whitespace issue:

git apply ~/Downloads/v4-fixfiles-Unmount-temporary-bind-mounts-on-SIGINT.patch /home/christian/Downloads/v4-fixfiles-Unmount-temporary-bind-mounts-on-SIGINT.patch:137:
space before tab in indent.

                        TMP_MOUNT="$(mktemp -p /run -d 

fixfiles.XXXXXXXXXX)"
warning: 1 line adds whitespace errors.

Quoted 8 lines
Quoted 3 lines

policycoreutils/scripts/fixfiles case "$RESTORE_MODE" in · line 277

Quoted 3 lines
policycoreutils/scripts/fixfiles +3 -0
+                 export SETFILES VERBOSE EXCLUDEDIRS FORCEFLAG THREADS FC TMP_MOUNT m
+                 if type unshare &> /dev/null; then
+                     unshare -m bash -x -c "fix_labels_on_mountpoint" $* || exit $?

Two issues:

I.
The -x flag make the output unreadable (especially for check/verify).

This is leftover from my debugging. It is not supposed to be there.

Quoted 3 lines

II.
The option (e.g. -n for check/verify) is not passed, should be -c "fix_labels_on_mountpoint $*".

I'll fix it.

Thanks!

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
policycoreutils/scripts/fixfiles +5 -1
+                 else
+                     trap umount_TMP_MOUNT EXIT
+                     fix_labels_on_mountpoint $*
+                     trap EXIT
+                 fi
              done;
          fi
      else
--

2.37.3

Signature · 3 lines
gpg --locate-keys [email protected]
Key fingerprint = FCD2 3660 5D6B 9D27 7FC6  E0FF DA7E 521F 10F6 4098
Dominick Grift