]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsfd: refer to /proc/$pid/map_files if a mapped file is masked
authorMasatake YAMATO <yamato@redhat.com>
Sun, 31 Aug 2025 01:03:03 +0000 (10:03 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Thu, 25 Sep 2025 20:27:20 +0000 (05:27 +0900)
To extract file information, lsfd called stat(2) on the the file name
found in /proc/$pid/maps. However, lsfd may retrieve information for
the wrong file if the file is bind-mount'ed after the process maps the
file.

To obtain the information about the originally mapped file, this
change makes lsfd call fstat(2) on the corresponding entry under
/proc/$pid/map_files avoiding the bind-mount ambiguity.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
lsfd-cmd/lsfd.c
tests/expected/lsfd/mkfds-mmap-masked-file [new file with mode: 0644]
tests/ts/lsfd/mkfds-mmap-masked-file [new file with mode: 0755]

index c17209896e3042839593ed9c54f58dddc61d2222..4596697ad94d895937beec15b7c053dde1dda519 100644 (file)
@@ -1017,6 +1017,12 @@ static void parse_maps_line(struct path_cxt *pc, char *buf, struct proc *proc)
                         * "stat by the file name" may not work. In that case,
                         */
                        goto try_map_files;
+               if (sb.st_ino != ino || sb.st_dev != devno)
+                       /* There are two files having the same absolute file names!
+                        *
+                        * Maybe the file is bind-mount'ed after mapped.
+                        */
+                       goto try_map_files;
                f = new_file(proc, stat2class(&sb), &sb, path, -assoc);
        } else {
                /* As used in tcpdump, AF_PACKET socket can be mmap'ed. */
diff --git a/tests/expected/lsfd/mkfds-mmap-masked-file b/tests/expected/lsfd/mkfds-mmap-masked-file
new file mode 100644 (file)
index 0000000..6082693
--- /dev/null
@@ -0,0 +1,4 @@
+INODE: 0
+BEFORE: 0
+INODE: 0
+AFTER: 0
diff --git a/tests/ts/lsfd/mkfds-mmap-masked-file b/tests/ts/lsfd/mkfds-mmap-masked-file
new file mode 100755 (executable)
index 0000000..032c844
--- /dev/null
@@ -0,0 +1,88 @@
+#!/bin/bash
+#
+# Copyright (C) 2025 Masatake YAMATO <yamato@redhat.com>
+#
+# This file is part of util-linux.
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This file is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+TS_TOPDIR="${0%/*}/../.."
+TS_DESC="mmap its backing file is masked by bind mount"
+
+. "$TS_TOPDIR"/functions.sh
+ts_init "$*"
+ts_skip_nonroot
+
+ts_check_test_command "$TS_CMD_LSFD"
+ts_check_test_command "$TS_HELPER_MKFDS"
+ts_check_test_command "$TS_CMD_MOUNT"
+ts_check_test_command "$TS_CMD_UMOUNT"
+ts_check_test_command "$TS_CMD_MOUNTPOINT"
+ts_check_test_command "$TS_CMD_FALLOCATE"
+
+ts_check_prog "rm"
+ts_check_prog "stat"
+
+ts_cd "$TS_OUTDIR"
+
+FILE_MASKED=masked-$$
+FILE_MASKED_INO=
+FILE_MASKING=masking-$$
+FILE_MASKING_INO=
+
+cleanup()
+{
+    "$TS_CMD_UMOUNT" -q ${FILE_MASKED}
+    rm -f ${FILE_MASKED}
+    rm -f ${FILE_MASKING}
+}
+
+trap cleanup EXIT
+
+if ! "$TS_CMD_FALLOCATE" -l 4096 ${FILE_MASKED}; then
+    ts_skip "failed to make a file to be masked"
+fi
+FILE_MASKED_INO=$(stat -c '%i' ${FILE_MASKED})
+
+if ! "$TS_CMD_FALLOCATE" -l 4096 ${FILE_MASKING}; then
+    ts_skip "failed to make a file for masking"
+fi
+FILE_MASKING_INO=$(stat -c '%i' ${FILE_MASKING})
+
+PID=
+
+{
+    coproc MKFDS { "$TS_HELPER_MKFDS" mmap file=${FILE_MASKED}; }
+} >> "$TS_OUTPUT" 2>&1
+
+if read -u ${MKFDS[0]} PID; then
+    {
+       INO_BEFORE=$(${TS_CMD_LSFD} -p "$PID" -n -o INODE -Q 'NAME =~ ".*'${FILE_MASKED}'"')
+       echo "INODE:" $?
+       [[ "${FILE_MASKED_INO}" == "${INO_BEFORE}" ]]
+       echo "BEFORE:" $?
+    } >> "$TS_OUTPUT" 2>&1
+
+    if ! "$TS_CMD_MOUNT" --bind ${FILE_MASKING} ${FILE_MASKED}; then
+       ts_skip "failed to mask ${FILE_MASKED}"
+    fi
+
+    {
+       INO_AFTER=$(${TS_CMD_LSFD} -p "$PID" -n -o INODE -Q 'NAME =~ ".*'${FILE_MASKED}'"')
+       echo "INODE:" $?
+       [[ "${FILE_MASKED_INO}" == "${INO_AFTER}" ]]
+       echo "AFTER:" $?
+    } >> "$TS_OUTPUT" 2>&1
+fi
+
+echo DONE >&"${MKFDS[1]}"
+wait "${MKFDS_PID}"
+ts_finalize