Skip to contentSkip to conversation

fs/locks: filter OFD locks from /proc/locks for foreign pid namespaces

PATCH Superseded Tao Cui 1 participant 1 message Sep 2

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Tao Cui

author
#0 lore raw

PATCH 1/1 fs/locks: filter OFD locks from /proc/locks for foreign pid namespaces

From: Tao Cui <[email protected]>

Since Linux 4.9, /proc/locks only shows locks whose owning process is visible in the reader's pid namespace. The check cannot see the owner of an OFD lock, though: locks_translate_pid() always returns -1 for FL_OFDLCK, so an OFD lock held outside of that namespace bypasses the filter and is shown with its device/inode numbers and byte range, e.g.

    4: OFDLCK ADVISORY  WRITE -1 fd:00:70536961 0 EOF

This was observed on a Kubernetes node: a pod reading /proc/locks listed the OFD write lock of an unrelated host process, while the POSIX locks of the very same process were correctly hidden. The device/inode pair identifies which host file is locked, the byte range discloses where it is actively written, and the appearance/disappearance of entries reflects host task activity, contrary to the documented per-pidns visibility of /proc/locks (proc_locks(5)).

OFD locks record the owner tgid in flc_pid, so use it for the visibility decision in locks_show(). The pid column is still reported as -1 for OFD locks; only the filter decision changes.

Verified with an OFD and a POSIX write lock held in the initial pid namespace while a process in a fresh pid namespace reads /proc/locks:
the OFD entry is visible without this patch and hidden with it, the POSIX entry is hidden in both cases.

---
 fs/locks.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
fs/locks.c +17 -0
index 6e4ff7fcec05..db17879d9319 100644
@@ -3022,6 +3022,23 @@ static int locks_show(struct seq_file *f, void *v)
 
 	cur = hlist_entry(v, struct file_lock_core, flc_link);
 
+	/*
+	 * OFD locks are reported with pid -1, so the filter below cannot see
+	 * their owner; flc_pid holds the owner tgid, so filter on it.
+	 */
+	if ((cur->flc_flags & FL_OFDLCK) && proc_pidns != &init_pid_ns) {
+		struct pid *pid;
+		bool visible = false;
+
+		rcu_read_lock();
+		pid = find_pid_ns(cur->flc_pid, &init_pid_ns);
+		if (pid)
+			visible = pid_nr_ns(pid, proc_pidns) != 0;
+		rcu_read_unlock();
+		if (!visible)
+			return 0;
+	}
+
 	if (locks_translate_pid(cur, proc_pidns) == 0)
 		return 0;
 
Signature · 1 line
2.43.0