fixfiles: Unmount temporary bind mounts on SIGINT
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
@@ -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
Quoted 50 lines · Petr Lautrbach
fixfiles -M relabeltemporary bind mounts filestems before relabeling but it leaves a directory mounted in/tmp/tmp.XXXXwhen a user hits CTRL-C. It means that if the user runfixfiles -M relabelagain and answered Y to clean out /tmp directory, it would remove all data from mounted fs.Signed-off-by Petr Lautrbach <[email protected]>--- policycoreutils/scripts/fixfiles | 11 +++++++++++ 1 file changed, 11 insertions(+)policycoreutils/scripts/fixfiles +11
-1@@ -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.
Quoted 48 lines · Ondrej Mosnacek
Quoted 47 lines · Petr Lautrbach
fixfiles -M relabeltemporary bind mounts filestems before relabeling but it leaves a directory mounted in/tmp/tmp.XXXXwhen a user hits CTRL-C. It means that if the user runfixfiles -M relabelagain and answered Y to clean out /tmp directory, it would remove all data from mounted fs.Signed-off-by Petr Lautrbach <[email protected]>--- policycoreutils/scripts/fixfiles | 11 +++++++++++ 1 file changed, 11 insertions(+)policycoreutils/scripts/fixfiles +11
-0@@ -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
-1else 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.
Quoted 59 lines · Petr Lautrbach
Quoted 48 lines · Ondrej Mosnacek
Quoted 47 lines · Petr Lautrbach
fixfiles -M relabeltemporary bind mounts filestems before relabeling but it leaves a directory mounted in/tmp/tmp.XXXXwhen a user hits CTRL-C. It means that if the user runfixfiles -M relabelagain and answered Y to clean out /tmp directory, it would remove all data from mounted fs.Signed-off-by Petr Lautrbach <[email protected]>--- policycoreutils/scripts/fixfiles | 11 +++++++++++ 1 file changed, 11 insertions(+)policycoreutils/scripts/fixfiles +11
-0@@ -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 fiQuoted 8 lines
Quoted 5 lines
policycoreutils/scripts/fixfiles +0
-1else 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.
Quoted 95 lines · Ondrej Mosnacek
Quoted 59 lines · Petr Lautrbach
Quoted 48 lines · Ondrej Mosnacek
Quoted 47 lines · Petr Lautrbach
fixfiles -M relabeltemporary bind mounts filestems before relabeling but it leaves a directory mounted in/tmp/tmp.XXXXwhen a user hits CTRL-C. It means that if the user runfixfiles -M relabelagain and answered Y to clean out /tmp directory, it would remove all data from mounted fs.Signed-off-by Petr Lautrbach <[email protected]>--- policycoreutils/scripts/fixfiles | 11 +++++++++++ 1 file changed, 11 insertions(+)policycoreutils/scripts/fixfiles +11
-0@@ -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 fiQuoted 8 lines
Quoted 5 lines
policycoreutils/scripts/fixfiles +0
-1else 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
PATCH v2 1/1 fixfiles: Unmount temporary bind mounts on SIGINT
In reply to Ondrej Mosnacekfixfiles -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
@@ -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
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
@@ -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
Quoted 10 lines · Petr Lautrbach
fixfiles -M relabeltemporary bind mounts filestems before relabeling but it leaves a directory mounted in/tmp/tmp.XXXXwhen a user hits CTRL-C. It means that if the user runfixfiles -M relabelagain and answered Y to clean out /tmp directory, it would remove all data from mounted fs.This patch changes the location where
fixfilesmounts fs to /run and adds a handler for exit signals which tries to umount fs mounted byfixfiles.
What about additionally using mount namespaces, if available:
@@ -256,10 +256,16 @@
test -z ${TMP_MOUNT+x} && echo "Unable to findtemporary 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
Signed-off-by Petr Lautrbach <[email protected]>---
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@@ -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
(Resending without HTML...)
Quoted 9 lines · Christian Göttsche
Quoted 7 lines · Petr Lautrbach
fixfiles -M relabeltemporary bind mounts filestems before relabeling but it leaves a directory mounted in/tmp/tmp.XXXXwhen a user hits CTRL-C. It means that if the user runfixfiles -M relabelagain 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
fixfilesmounts fs to /run and adds a handler for exit signals which tries to umount fs mounted byfixfiles.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 findtemporary 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 "Errorcleaning up ${TMP_MOUNT}${m}."
(quoted diff) +2
-0+ fi + rmdir "${TMP_MOUNT}" || echo "Error cleaningup ${TMP_MOUNT}."
done; fi else?
Quoted 47 lines
Signed-off-by Petr Lautrbach <[email protected]>---
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@@ -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
-1mkdir -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.
Quoted 14 lines · Ondrej Mosnacek
(Resending without HTML...)
Quoted 9 lines · Christian Göttsche
Quoted 7 lines · Petr Lautrbach
fixfiles -M relabeltemporary bind mounts filestems before relabeling but it leaves a directory mounted in/tmp/tmp.XXXXwhen a user hits CTRL-C. It means that if the user runfixfiles -M relabelagain 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
fixfilesmounts fs to /run and adds a handler for exit signals which tries to umount fs mounted byfixfiles.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 findtemporary 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 "Errorcleaning up ${TMP_MOUNT}${m}."
(quoted diff) +2
-0+ fi + rmdir "${TMP_MOUNT}" || echo "Error cleaningup ${TMP_MOUNT}."
done; fi else?
Quoted 47 lines
Signed-off-by Petr Lautrbach <[email protected]>---
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@@ -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 1Quoted 13 lines
Quoted 10 lines
policycoreutils/scripts/fixfiles +1
-1mkdir -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.
PATCH v4 1/1 fixfiles: Unmount temporary bind mounts on SIGINT
In reply to Christian Göttschefixfiles -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
@@ -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
Quoted 6 lines · Petr Lautrbach
fixfiles -M relabeltemporary bind mounts filestems before relabeling but it leaves a directory mounted in/tmp/tmp.XXXXwhen a user hits CTRL-C. It means that if the user runfixfiles -M relabelagain 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
fixfilesmounts fs to /run, uses private mount namespace via unshare and adds a handler for exit signals which tries to umount fs mounted byfixfiles.Signed-off-by Petr Lautrbach <[email protected]>
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@@ -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 elseSignature · 1 line
2.37.3
Quoted 75 lines · Petr Lautrbach
Quoted 6 lines · Petr Lautrbach
fixfiles -M relabeltemporary bind mounts filestems before relabeling but it leaves a directory mounted in/tmp/tmp.XXXXwhen a user hits CTRL-C. It means that if the user runfixfiles -M relabelagain 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
fixfilesmounts fs to /run, uses private mount namespace via unshare and adds a handler for exit signals which tries to umount fs mounted byfixfiles.Signed-off-by Petr Lautrbach <[email protected]>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@@ -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
Quoted 93 lines · Christian Göttsche
Quoted 75 lines · Petr Lautrbach
Quoted 6 lines · Petr Lautrbach
fixfiles -M relabeltemporary bind mounts filestems before relabeling but it leaves a directory mounted in/tmp/tmp.XXXXwhen a user hits CTRL-C. It means that if the user runfixfiles -M relabelagain 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
fixfilesmounts fs to /run, uses private mount namespace via unshare and adds a handler for exit signals which tries to umount fs mounted byfixfiles.Signed-off-by Petr Lautrbach <[email protected]>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@@ -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-xflag 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.-nfor 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
Quoted 16 lines · Petr Lautrbach
Quoted 14 lines · Christian Göttsche
Quoted 13 lines · Petr Lautrbach
Quoted 10 lines · Petr Lautrbach
fixfiles -M relabeltemporary bind mounts filestems before relabeling but it leaves a directory mounted in/tmp/tmp.XXXXwhen a user hits CTRL-C. It means that if the user runfixfiles -M relabelagain and answered Y to clean out /tmp directory, it would remove all data from mounted fs.This patch changes the location where
fixfilesmounts fs to /run, uses private mount namespace via unshare and adds a handler for exit signals which tries to umount fs mounted byfixfiles.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Signed-off-by Petr Lautrbach <[email protected]>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@@ -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 -dfixfiles.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-xflag 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.-nfor 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