vfs: utimensat(): fix error checking for {UTIME_NOW,UTIME_OMIT} case
PATCH 3/4 vfs: utimensat(): fix error checking for {UTIME_NOW,UTIME_OMIT} case
The POSIX.1 draft spec for utimensat() says that to do anything other than setting both timestamps to a time other than the current time (i.e., times is not NULL, and both tv_nsec fields are not UTIME_NOW and both tv_nsec fields are not UTIME_OMIT), either:
a) the caller's effective user ID must match the file owner; or
b) the caller must have appropriate privileges.
If this condition is violated, then the error EPERM should result. However, the current implementation does not generate EPERM if one tv_nsec field is UTIME_NOW while the other is UTIME_OMIT. It should give this error for that case.
This patch:
a) Repairs that problem.
b) Removes the now unneeded nsec_special() helper function.
Miklos suggested an alternative idea, migrating the is_owner_or_cap() checks into fs/attr.c:inode_change_ok() via the use of an ATTR_OWNER_CHECK flag. Maybe we could do that later, but for now I've gone with this version, which is simpler, and can be more easily read as being correct.
fs/utimes.c +14 -8
@@ -40,14 +40,9 @@#endif -static bool nsec_special (long nsec) -{ -return nsec ==UTIME_OMIT || nsec ==UTIME_NOW ; -} -static bool nsec_valid (long nsec) { -if (nsec_special (nsec)) +if (nsec ==UTIME_OMIT || nsec ==UTIME_NOW )return true;return nsec >=0 && nsec <=999999999 ;
@@ -135,8 +130,7 @@* UTIME_NOW, then need to check permissions, because * inode_change_ok() won't do it. */ -if (!times || (nsec_special (times[0 ].tv_nsec) && -nsec_special (times[1 ].tv_nsec))) { +if (!times) { error = -EACCES ;if (IS_IMMUTABLE (inode)) goto mnt_drop_write_and_out;
@@ -151,6 +145,18 @@ goto mnt_drop_write_and_out; } } + }else if ((times[0 ].tv_nsec ==UTIME_NOW && + times[1 ].tv_nsec ==UTIME_OMIT ) + || + (times[0 ].tv_nsec ==UTIME_OMIT && + times[1 ].tv_nsec ==UTIME_NOW )) { + error =-EPERM ; + +if (IS_APPEND (inode) ||IS_IMMUTABLE (inode)) + goto mnt_drop_write_and_out; + +if (!is_owner_or_cap (inode)) + goto mnt_drop_write_and_out; }mutex_lock (&inode->i_mutex); error =notify_change (dentry, &newattrs);
Quoted 16 lines
The POSIX.1 draft spec for
utimensat()says that to do anything other than setting both timestamps to a time other than the current time (i.e., times is not NULL, and bothtv_nsecfields are notUTIME_NOWand bothtv_nsecfields are notUTIME_OMIT), either:a) the caller's effective user ID must match the file owner; or
b) the caller must have appropriate privileges.If this condition is violated, then the error EPERM should result. However, the current implementation does not generate EPERM if one
tv_nsecfield isUTIME_NOWwhile the other isUTIME_OMIT. It should give this error for that case.This patch:
Quoted 8 lines
a) Repairs that problem.
b) Removes the now unneedednsec_special()helper function.Miklos suggested an alternative idea, migrating the
is_owner_or_cap()checks into fs/attr.c:inode_change_ok() via the use of anATTR_OWNER_CHECKflag. Maybe we could do that later, but for now I've gone with this version, which is simpler, and can be more easily read as being correct.
Wise decision.
Quoted 25 lines
CC Miklos Szeredi <[email protected]>CC Al Viro <[email protected]>CC Ulrich Drepper <[email protected]>Signed-off-by Michael Kerrisk <[email protected]>fs/utimes.c +1
-6@@ -40,14 +40,9 @@#endif -static bool nsec_special (long nsec) -{ -return nsec ==UTIME_OMIT || nsec ==UTIME_NOW ; -} -static bool nsec_valid (long nsec) { -if (nsec_special (nsec)) +if (nsec ==UTIME_OMIT || nsec ==UTIME_NOW )return true;return nsec >=0 && nsec <=999999999 ;
fs/utimes.c line 133
Quoted 6 lines
fs/utimes.c +1
-2@@ -135,8 +130,7 @@* UTIME_NOW, then need to check permissions, because * inode_change_ok() won't do it. */ -if (!times || (nsec_special (times[0 ].tv_nsec) && -nsec_special (times[1 ].tv_nsec))) { +if (!times) {
This can be restored to be the "else" branch of the "if (times)".
fs/utimes.c line 152
Quoted 12 lines
fs/utimes.c +5
-0error = -EACCES ;if (IS_IMMUTABLE (inode)) goto mnt_drop_write_and_out;@@ -151,6 +145,18 @@ goto mnt_drop_write_and_out; } } + }else if ((times[0 ].tv_nsec ==UTIME_NOW && + times[1 ].tv_nsec ==UTIME_OMIT ) + || + (times[0 ].tv_nsec ==UTIME_OMIT && + times[1 ].tv_nsec ==UTIME_NOW )) {
And I'd rather put this inside the "if (times)" for clarity.
fs/utimes.c line 156
Quoted 4 lines
fs/utimes.c +4
-0+ error =-EPERM ; + +if (IS_APPEND (inode) ||IS_IMMUTABLE (inode)) + goto mnt_drop_write_and_out;
And then you would've realized, that this check was already done.
fs/utimes.c line 159
Quoted 3 lines
fs/utimes.c +3
-0+ +if (!is_owner_or_cap (inode)) + goto mnt_drop_write_and_out;
OK.
Quoted 6 lines
fs/utimes.c +0
-0}mutex_lock (&inode->i_mutex); error =notify_change (dentry, &newattrs);
fs/utimes.c line 159
Quoted 10 lines
Quoted 5 lines
fs/utimes.c +5
-0+ }else if ((times[0 ].tv_nsec ==UTIME_NOW && + times[1 ].tv_nsec ==UTIME_OMIT ) + || + (times[0 ].tv_nsec ==UTIME_OMIT && + times[1 ].tv_nsec ==UTIME_NOW )) {fs/utimes.c +3
-0+ +if (!is_owner_or_cap (inode)) + goto mnt_drop_write_and_out;
And in fact a little comment wouldn't hurt explaining what exactly is going on here with the permission checking.
The "/* Don't worry, the checks are done in inode_change_ok() */" is really reassuring, but unfortunately not exactly the truth (and never was).
Miklos
Quoted 7 lines
Quoted 5 lines
Miklos suggested an alternative idea, migrating the
is_owner_or_cap()checks into fs/attr.c:inode_change_ok() via the use of anATTR_OWNER_CHECKflag. Maybe we could do that later, but for now I've gone with this version, which is simpler, and can be more easily read as being correct.Wise decision.
Or maybe not. Isn't this a simpler and more readable patch?
fs/attr.c +1 -1
@@ -51,7 +51,7 @@ int inode_change_ok(struct inode *inode, }/* Check for setting the inode time. */ -if (ia_valid & (ATTR_MTIME_SET |ATTR_ATIME_SET )) { +if (ia_valid & (ATTR_MTIME_SET |ATTR_ATIME_SET |ATTR_UPDATE_TIMES )) {if (!is_owner_or_cap (inode)) goto error; }
fs/utimes.c +6 -1
@@ -102,9 +102,14 @@ long do_utimes(int dfd, char __user *filif (error) goto dput_and_out; -/* Don't worry, the checks are done in inode_change_ok() */ newattrs.ia_valid =ATTR_CTIME |ATTR_MTIME |ATTR_ATIME ;if (times) { +/* +* Checking the owner is done in inode_change_ok(), +* even for the special UTIME_OMIT/UTIME_NOW cases. +*/ + newattrs.ia_valid |=ATTR_UPDATE_TIMES + error = -EPERM;if (IS_APPEND (inode) ||IS_IMMUTABLE (inode)) goto mnt_drop_write_and_out;
include/linux/fs.h +1 -0
@@ -333,6 +333,7 @@ typedef void (dio_iodone_t)(struct kiocb#define ATTR_FILE 8192#define ATTR_KILL_PRIV 16384#define ATTR_OPEN 32768/* Truncating from open(O_TRUNC) */ +#define ATTR_UPDATE_TIMES (1 << 16)/* * This is the Inode Attributes structure, used for notify_change(). It
Quoted 9 lines · Miklos Szeredi
Quoted 7 lines
Quoted 5 lines
Miklos suggested an alternative idea, migrating the
is_owner_or_cap()checks into fs/attr.c:inode_change_ok() via the use of anATTR_OWNER_CHECKflag. Maybe we could do that later, but for now I've gone with this version, which is simpler, and can be more easily read as being correct.Wise decision.
Or maybe not. Isn't this a simpler and more readable patch?
The idea is okay (we talked about it already), but I don't like doing it now because it only migrates *one* of the is_owner_or_cap() checks out of do_utimes(). We could revisit this later in the context of migrating both checks out of do_utimes(), but I'd prefer to leave that for now.
Quoted 46 lines
Index:
linux/fs/attr.c
===================================================================fs/attr.c 2008-06-04 07:07:23.000000000 +0200 renamed from fs/attr.c 2008-06-03 13:10:11.000000000 +0200 +1
-1@@ -51,7 +51,7 @@ int inode_change_ok(struct inode *inode, } /* Check for setting the inode time. */ - if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) { + if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_UPDATE_TIMES)) { if (!is_owner_or_cap(inode)) goto error; }Index:
linux/fs/utimes.c
===================================================================fs/utimes.c 2008-06-04 07:10:53.000000000 +0200 renamed from fs/utimes.c 2008-06-03 13:10:11.000000000 +0200 +6
-1@@ -102,9 +102,14 @@ long do_utimes(int dfd, char __user *fil if (error) goto dput_and_out; - /* Don't worry, the checks are done in inode_change_ok() */ newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME; if (times) { + /* + * Checking the owner is done in inode_change_ok(), + * even for the special UTIME_OMIT/UTIME_NOW cases. + */ + newattrs.ia_valid |= ATTR_UPDATE_TIMES + error = -EPERM; if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) goto mnt_drop_write_and_out;Index:
linux/include/linux/fs.h
===================================================================include/linux/fs.h 2008-06-04 07:07:43.000000000 +0200 renamed from include/linux/fs.h 2008-06-03 13:10:25.000000000 +0200 +1
-0@@ -333,6 +333,7 @@ typedef void (dio_iodone_t)(struct kiocb #define ATTR_FILE 8192 #define ATTR_KILL_PRIV 16384 #define ATTR_OPEN 32768 /* Truncating from open(O_TRUNC) */ +#define ATTR_UPDATE_TIMES (1 << 16) /* * This is the Inode Attributes structure, used for notify_change(). It
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Found a bug? http://www.kernel.org/doc/man-pages/reporting_bugs.html
Quoted 50 lines · Miklos Szeredi
Quoted 16 lines
The POSIX.1 draft spec for
utimensat()says that to do anything other than setting both timestamps to a time other than the current time (i.e., times is not NULL, and bothtv_nsecfields are notUTIME_NOWand bothtv_nsecfields are notUTIME_OMIT), either:a) the caller's effective user ID must match the file owner; or
b) the caller must have appropriate privileges.If this condition is violated, then the error EPERM should result. However, the current implementation does not generate EPERM if one
tv_nsecfield isUTIME_NOWwhile the other isUTIME_OMIT. It should give this error for that case.This patch:
Quoted 8 lines
a) Repairs that problem.
b) Removes the now unneedednsec_special()helper function.Miklos suggested an alternative idea, migrating the
is_owner_or_cap()checks into fs/attr.c:inode_change_ok() via the use of anATTR_OWNER_CHECKflag. Maybe we could do that later, but for now I've gone with this version, which is simpler, and can be more easily read as being correct.Wise decision.
Quoted 23 lines
CC Miklos Szeredi <[email protected]>CC Al Viro <[email protected]>CC Ulrich Drepper <[email protected]>Signed-off-by Michael Kerrisk <[email protected]>fs/utimes.c 2008-06-03 23:04:48.000000000 +0200 renamed from fs/utimes.c 2008-06-03 23:11:53.000000000 +0200 +1
-6@@ -40,14 +40,9 @@ #endif -static bool nsec_special(long nsec) -{ - return nsec == UTIME_OMIT || nsec == UTIME_NOW; -} - static bool nsec_valid(long nsec) { - if (nsec_special(nsec)) + if (nsec == UTIME_OMIT || nsec == UTIME_NOW) return true;
Quoted 10 lines
fs/utimes.c 2008-06-03 23:04:48.000000000 +0200 line 133
Quoted 8 lines
fs/utimes.c 2008-06-03 23:04:48.000000000 +0200 renamed from fs/utimes.c 2008-06-03 23:11:53.000000000 +0200 +1
-2return nsec >= 0 && nsec <= 999999999;@@ -135,8 +130,7 @@ * UTIME_NOW, then need to check permissions, because * inode_change_ok() won't do it. */ - if (!times || (nsec_special(times[0].tv_nsec) && - nsec_special(times[1].tv_nsec))) { + if (!times) {This can be restored to be the "else" branch of the "if (times)".
Yes. Thanks.
Quoted 6 lines
Quoted 6 lines
fs/utimes.c 2008-06-03 23:04:48.000000000 +0200 renamed from fs/utimes.c 2008-06-03 23:11:53.000000000 +0200 +0
-0error = -EACCES; if (IS_IMMUTABLE(inode)) goto mnt_drop_write_and_out;@@ -151,6 +145,18 @@ goto mnt_drop_write_and_out; }
Quoted 8 lines
fs/utimes.c 2008-06-03 23:04:48.000000000 +0200 line 152
Quoted 6 lines
fs/utimes.c 2008-06-03 23:04:48.000000000 +0200 renamed from fs/utimes.c 2008-06-03 23:11:53.000000000 +0200 +5
-0} + } else if ((times[0].tv_nsec == UTIME_NOW && + times[1].tv_nsec == UTIME_OMIT) + || + (times[0].tv_nsec == UTIME_OMIT && + times[1].tv_nsec == UTIME_NOW)) {And I'd rather put this inside the "if (times)" for clarity.
Yes.
Quoted 7 lines
fs/utimes.c 2008-06-03 23:04:48.000000000 +0200 line 156
Quoted 5 lines
fs/utimes.c 2008-06-03 23:04:48.000000000 +0200 renamed from fs/utimes.c 2008-06-03 23:11:53.000000000 +0200 +4
-0+ error =-EPERM; + + if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) + goto mnt_drop_write_and_out;And then you would've realized, that this check was already done.
Quite!
I'll make these changes and test.
Cheers,
Michael
Quoted 13 lines
fs/utimes.c 2008-06-03 23:04:48.000000000 +0200 line 159
Quoted 3 lines
fs/utimes.c 2008-06-03 23:04:48.000000000 +0200 renamed from fs/utimes.c 2008-06-03 23:11:53.000000000 +0200 +3
-0+ + if (!is_owner_or_cap(inode)) + goto mnt_drop_write_and_out;OK.
Quoted 6 lines
fs/utimes.c 2008-06-03 23:04:48.000000000 +0200 renamed from fs/utimes.c 2008-06-03 23:11:53.000000000 +0200 +0
-0} mutex_lock(&inode->i_mutex); error = notify_change(dentry, &newattrs);
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Found a bug? http://www.kernel.org/doc/man-pages/reporting_bugs.html