Skip to contentSkip to conversation

stat.2: add note about stat being racy

PATCH In review Jann Horn 2 participants 2 messages Apr 20, 2015 – Apr 21, 2015 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

Jann Horn

author
#0 lore raw

PATCH 1/1 stat.2: add note about stat being racy

The same thing was fixed for execve() in commit 8b01fc86b9f425899f8a3a8fc1c47d73c2c20543, but for performance reasons, that simple patch won't work for stat().

---
 man2/stat.2 | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
man2/stat.2 +23 -0
index 0d244af..69ec6a4 100644
@@ -175,6 +175,29 @@ that may be present between some fields on various architectures.
 Consult the glibc and kernel source code
 if you need to know the details.
 
+\# Background: inode attributes are modified with i_mutex held, but
+\# read by stat() without taking the mutex.
+.I Note:
+For performance and simplicity reasons, different fields in the
+.I stat
+structure may contain state information from different moments
+during the execution of the syscall. For example, if
+.IR st_mode ,
+.IR st_uid
+and
+.IR st_gid
+are changed by another process by calling
+.BR chown (2) ,
+.BR stat ()
+might return the old
+.I st_mode
+together with the new
+.IR st_uid ,
+or the old
+.I st_uid
+together with the new
+.IR st_mode .
+
 The
 .I st_dev
 field describes the device on which this file resides.
Signature · 1 line
2.1.4

A message that is not in the archive (sent off-list or not yet received): 20150420204601.GA9904-J1fxOzX/[email protected]

#2 lore raw
Quoted 3 lines · Jann Horn

The same thing was fixed for execve() in commit 8b01fc86b9f425899f8a3a8fc1c47d73c2c20543, but for performance reasons, that simple patch won't work for stat().

Thanks, Jann! Applied.

Cheers,

Michael

Quoted 39 lines
---
 man2/stat.2 | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
man2/stat.2 +23 -0
index 0d244af..69ec6a4 100644
@@ -175,6 +175,29 @@ that may be present between some fields on various architectures.
 Consult the glibc and kernel source code
 if you need to know the details.
 
+\# Background: inode attributes are modified with i_mutex held, but
+\# read by stat() without taking the mutex.
+.I Note:
+For performance and simplicity reasons, different fields in the
+.I stat
+structure may contain state information from different moments
+during the execution of the syscall. For example, if
+.IR st_mode ,
+.IR st_uid
+and
+.IR st_gid
+are changed by another process by calling
+.BR chown (2) ,
+.BR stat ()
+might return the old
+.I st_mode
+together with the new
+.IR st_uid ,
+or the old
+.I st_uid
+together with the new
+.IR st_mode .
+
 The
 .I st_dev
 field describes the device on which this file resides.
Signature · 7 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html