1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21c710c89SUlrich Drepper #include <linux/file.h> 374f9fdfaSDave Hansen #include <linux/mount.h> 482b0547cSAlexey Dobriyan #include <linux/namei.h> 582b0547cSAlexey Dobriyan #include <linux/utime.h> 612c2ab5eSAdrian Bunk #include <linux/syscalls.h> 77c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 81a060ba3SAl Viro #include <linux/compat.h> 982b0547cSAlexey Dobriyan #include <asm/unistd.h> 1082b0547cSAlexey Dobriyan 11043f46f6SMiklos Szeredi static bool nsec_valid(long nsec) 12043f46f6SMiklos Szeredi { 134cca9226SMichael Kerrisk if (nsec == UTIME_OMIT || nsec == UTIME_NOW) 14043f46f6SMiklos Szeredi return true; 15043f46f6SMiklos Szeredi 16043f46f6SMiklos Szeredi return nsec >= 0 && nsec <= 999999999; 17043f46f6SMiklos Szeredi } 18043f46f6SMiklos Szeredi 19aaed2dd8SDeepa Dinamani static int utimes_common(const struct path *path, struct timespec64 *times) 2082b0547cSAlexey Dobriyan { 2182b0547cSAlexey Dobriyan int error; 2282b0547cSAlexey Dobriyan struct iattr newattrs; 23e9b76fedSMiklos Szeredi struct inode *inode = path->dentry->d_inode; 2427ac0ffeSJ. Bruce Fields struct inode *delegated_inode = NULL; 2582b0547cSAlexey Dobriyan 2627eb11c9SChristoph Hellwig if (times) { 2727eb11c9SChristoph Hellwig if (!nsec_valid(times[0].tv_nsec) || 2827eb11c9SChristoph Hellwig !nsec_valid(times[1].tv_nsec)) 2927eb11c9SChristoph Hellwig return -EINVAL; 3027eb11c9SChristoph Hellwig if (times[0].tv_nsec == UTIME_NOW && 3127eb11c9SChristoph Hellwig times[1].tv_nsec == UTIME_NOW) 3227eb11c9SChristoph Hellwig times = NULL; 3327eb11c9SChristoph Hellwig } 3427eb11c9SChristoph Hellwig 35e9b76fedSMiklos Szeredi error = mnt_want_write(path->mnt); 3682b0547cSAlexey Dobriyan if (error) 3782b0547cSAlexey Dobriyan goto out; 381c710c89SUlrich Drepper 3982b0547cSAlexey Dobriyan newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME; 4082b0547cSAlexey Dobriyan if (times) { 411c710c89SUlrich Drepper if (times[0].tv_nsec == UTIME_OMIT) 421c710c89SUlrich Drepper newattrs.ia_valid &= ~ATTR_ATIME; 431c710c89SUlrich Drepper else if (times[0].tv_nsec != UTIME_NOW) { 44eb31e2f6SAmir Goldstein newattrs.ia_atime = times[0]; 451c710c89SUlrich Drepper newattrs.ia_valid |= ATTR_ATIME_SET; 461c710c89SUlrich Drepper } 471c710c89SUlrich Drepper 481c710c89SUlrich Drepper if (times[1].tv_nsec == UTIME_OMIT) 491c710c89SUlrich Drepper newattrs.ia_valid &= ~ATTR_MTIME; 501c710c89SUlrich Drepper else if (times[1].tv_nsec != UTIME_NOW) { 51eb31e2f6SAmir Goldstein newattrs.ia_mtime = times[1]; 521c710c89SUlrich Drepper newattrs.ia_valid |= ATTR_MTIME_SET; 531c710c89SUlrich Drepper } 5402c6be61SMiklos Szeredi /* 5531051c85SJan Kara * Tell setattr_prepare(), that this is an explicit time 569767d749SMiklos Szeredi * update, even if neither ATTR_ATIME_SET nor ATTR_MTIME_SET 579767d749SMiklos Szeredi * were used. 584cca9226SMichael Kerrisk */ 599767d749SMiklos Szeredi newattrs.ia_valid |= ATTR_TIMES_SET; 604cca9226SMichael Kerrisk } else { 61f2b20f6eSMiklos Szeredi newattrs.ia_valid |= ATTR_TOUCH; 621e5de283SLinus Torvalds } 6327ac0ffeSJ. Bruce Fields retry_deleg: 645955102cSAl Viro inode_lock(inode); 6527ac0ffeSJ. Bruce Fields error = notify_change(path->dentry, &newattrs, &delegated_inode); 665955102cSAl Viro inode_unlock(inode); 6727ac0ffeSJ. Bruce Fields if (delegated_inode) { 6827ac0ffeSJ. Bruce Fields error = break_deleg_wait(&delegated_inode); 6927ac0ffeSJ. Bruce Fields if (!error) 7027ac0ffeSJ. Bruce Fields goto retry_deleg; 7127ac0ffeSJ. Bruce Fields } 72e9b76fedSMiklos Szeredi 73e9b76fedSMiklos Szeredi mnt_drop_write(path->mnt); 74e9b76fedSMiklos Szeredi out: 75e9b76fedSMiklos Szeredi return error; 76e9b76fedSMiklos Szeredi } 77e9b76fedSMiklos Szeredi 789d4b74aeSChristoph Hellwig static int do_utimes_path(int dfd, const char __user *filename, 799d4b74aeSChristoph Hellwig struct timespec64 *times, int flags) 809d4b74aeSChristoph Hellwig { 819d4b74aeSChristoph Hellwig struct path path; 829d4b74aeSChristoph Hellwig int lookup_flags = 0, error; 839d4b74aeSChristoph Hellwig 849d4b74aeSChristoph Hellwig if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) 859d4b74aeSChristoph Hellwig return -EINVAL; 869d4b74aeSChristoph Hellwig 879d4b74aeSChristoph Hellwig if (!(flags & AT_SYMLINK_NOFOLLOW)) 889d4b74aeSChristoph Hellwig lookup_flags |= LOOKUP_FOLLOW; 899d4b74aeSChristoph Hellwig if (flags & AT_EMPTY_PATH) 909d4b74aeSChristoph Hellwig lookup_flags |= LOOKUP_EMPTY; 919d4b74aeSChristoph Hellwig 929d4b74aeSChristoph Hellwig retry: 939d4b74aeSChristoph Hellwig error = user_path_at(dfd, filename, lookup_flags, &path); 949d4b74aeSChristoph Hellwig if (error) 959d4b74aeSChristoph Hellwig return error; 969d4b74aeSChristoph Hellwig 979d4b74aeSChristoph Hellwig error = utimes_common(&path, times); 989d4b74aeSChristoph Hellwig path_put(&path); 999d4b74aeSChristoph Hellwig if (retry_estale(error, lookup_flags)) { 1009d4b74aeSChristoph Hellwig lookup_flags |= LOOKUP_REVAL; 1019d4b74aeSChristoph Hellwig goto retry; 1029d4b74aeSChristoph Hellwig } 1039d4b74aeSChristoph Hellwig 1049d4b74aeSChristoph Hellwig return error; 1059d4b74aeSChristoph Hellwig } 1069d4b74aeSChristoph Hellwig 1079d4b74aeSChristoph Hellwig static int do_utimes_fd(int fd, struct timespec64 *times, int flags) 1089d4b74aeSChristoph Hellwig { 1099d4b74aeSChristoph Hellwig struct fd f; 1109d4b74aeSChristoph Hellwig int error; 1119d4b74aeSChristoph Hellwig 1129d4b74aeSChristoph Hellwig if (flags) 1139d4b74aeSChristoph Hellwig return -EINVAL; 1149d4b74aeSChristoph Hellwig 1159d4b74aeSChristoph Hellwig f = fdget(fd); 1169d4b74aeSChristoph Hellwig if (!f.file) 1179d4b74aeSChristoph Hellwig return -EBADF; 1189d4b74aeSChristoph Hellwig error = utimes_common(&f.file->f_path, times); 1199d4b74aeSChristoph Hellwig fdput(f); 1209d4b74aeSChristoph Hellwig return error; 1219d4b74aeSChristoph Hellwig } 1229d4b74aeSChristoph Hellwig 123e9b76fedSMiklos Szeredi /* 124e9b76fedSMiklos Szeredi * do_utimes - change times on filename or file descriptor 125e9b76fedSMiklos Szeredi * @dfd: open file descriptor, -1 or AT_FDCWD 126e9b76fedSMiklos Szeredi * @filename: path name or NULL 127e9b76fedSMiklos Szeredi * @times: new times or NULL 128e9b76fedSMiklos Szeredi * @flags: zero or more flags (only AT_SYMLINK_NOFOLLOW for the moment) 129e9b76fedSMiklos Szeredi * 130e9b76fedSMiklos Szeredi * If filename is NULL and dfd refers to an open file, then operate on 131e9b76fedSMiklos Szeredi * the file. Otherwise look up filename, possibly using dfd as a 132e9b76fedSMiklos Szeredi * starting point. 133e9b76fedSMiklos Szeredi * 134e9b76fedSMiklos Szeredi * If times==NULL, set access and modification to current time, 135e9b76fedSMiklos Szeredi * must be owner or have write permission. 136e9b76fedSMiklos Szeredi * Else, update from *times, must be owner or super user. 137e9b76fedSMiklos Szeredi */ 138aaed2dd8SDeepa Dinamani long do_utimes(int dfd, const char __user *filename, struct timespec64 *times, 139c7887325SDavid Howells int flags) 140e9b76fedSMiklos Szeredi { 1419d4b74aeSChristoph Hellwig if (filename == NULL && dfd != AT_FDCWD) 1429d4b74aeSChristoph Hellwig return do_utimes_fd(dfd, times, flags); 1439d4b74aeSChristoph Hellwig return do_utimes_path(dfd, filename, times, flags); 14482b0547cSAlexey Dobriyan } 14582b0547cSAlexey Dobriyan 146c7887325SDavid Howells SYSCALL_DEFINE4(utimensat, int, dfd, const char __user *, filename, 147a4f7a300SArnd Bergmann struct __kernel_timespec __user *, utimes, int, flags) 1481c710c89SUlrich Drepper { 149aaed2dd8SDeepa Dinamani struct timespec64 tstimes[2]; 1501c710c89SUlrich Drepper 1511c710c89SUlrich Drepper if (utimes) { 152aaed2dd8SDeepa Dinamani if ((get_timespec64(&tstimes[0], &utimes[0]) || 153aaed2dd8SDeepa Dinamani get_timespec64(&tstimes[1], &utimes[1]))) 1541c710c89SUlrich Drepper return -EFAULT; 1551c710c89SUlrich Drepper 1561c710c89SUlrich Drepper /* Nothing to do, we must not even check the path. */ 1571c710c89SUlrich Drepper if (tstimes[0].tv_nsec == UTIME_OMIT && 1581c710c89SUlrich Drepper tstimes[1].tv_nsec == UTIME_OMIT) 1591c710c89SUlrich Drepper return 0; 1601c710c89SUlrich Drepper } 1611c710c89SUlrich Drepper 1621c710c89SUlrich Drepper return do_utimes(dfd, filename, utimes ? tstimes : NULL, flags); 1631c710c89SUlrich Drepper } 1641c710c89SUlrich Drepper 165185cfaf7SArnd Bergmann #ifdef __ARCH_WANT_SYS_UTIME 166185cfaf7SArnd Bergmann /* 167185cfaf7SArnd Bergmann * futimesat(), utimes() and utime() are older versions of utimensat() 168185cfaf7SArnd Bergmann * that are provided for compatibility with traditional C libraries. 169185cfaf7SArnd Bergmann * On modern architectures, we always use libc wrappers around 170185cfaf7SArnd Bergmann * utimensat() instead. 171185cfaf7SArnd Bergmann */ 172f1390358SDominik Brodowski static long do_futimesat(int dfd, const char __user *filename, 17375d319c0SArnd Bergmann struct __kernel_old_timeval __user *utimes) 17482b0547cSAlexey Dobriyan { 17575d319c0SArnd Bergmann struct __kernel_old_timeval times[2]; 176aaed2dd8SDeepa Dinamani struct timespec64 tstimes[2]; 17782b0547cSAlexey Dobriyan 1781c710c89SUlrich Drepper if (utimes) { 1791c710c89SUlrich Drepper if (copy_from_user(×, utimes, sizeof(times))) 18082b0547cSAlexey Dobriyan return -EFAULT; 1811c710c89SUlrich Drepper 1821c710c89SUlrich Drepper /* This test is needed to catch all invalid values. If we 1831c710c89SUlrich Drepper would test only in do_utimes we would miss those invalid 1841c710c89SUlrich Drepper values truncated by the multiplication with 1000. Note 1851c710c89SUlrich Drepper that we also catch UTIME_{NOW,OMIT} here which are only 1861c710c89SUlrich Drepper valid for utimensat. */ 1871c710c89SUlrich Drepper if (times[0].tv_usec >= 1000000 || times[0].tv_usec < 0 || 1881c710c89SUlrich Drepper times[1].tv_usec >= 1000000 || times[1].tv_usec < 0) 1891c710c89SUlrich Drepper return -EINVAL; 1901c710c89SUlrich Drepper 1911c710c89SUlrich Drepper tstimes[0].tv_sec = times[0].tv_sec; 1921c710c89SUlrich Drepper tstimes[0].tv_nsec = 1000 * times[0].tv_usec; 1931c710c89SUlrich Drepper tstimes[1].tv_sec = times[1].tv_sec; 1941c710c89SUlrich Drepper tstimes[1].tv_nsec = 1000 * times[1].tv_usec; 1951c710c89SUlrich Drepper } 1961c710c89SUlrich Drepper 1971c710c89SUlrich Drepper return do_utimes(dfd, filename, utimes ? tstimes : NULL, 0); 19882b0547cSAlexey Dobriyan } 19982b0547cSAlexey Dobriyan 200f1390358SDominik Brodowski 201f1390358SDominik Brodowski SYSCALL_DEFINE3(futimesat, int, dfd, const char __user *, filename, 20275d319c0SArnd Bergmann struct __kernel_old_timeval __user *, utimes) 203f1390358SDominik Brodowski { 204f1390358SDominik Brodowski return do_futimesat(dfd, filename, utimes); 205f1390358SDominik Brodowski } 206f1390358SDominik Brodowski 207003d7ab4SHeiko Carstens SYSCALL_DEFINE2(utimes, char __user *, filename, 20875d319c0SArnd Bergmann struct __kernel_old_timeval __user *, utimes) 20982b0547cSAlexey Dobriyan { 210f1390358SDominik Brodowski return do_futimesat(AT_FDCWD, filename, utimes); 21182b0547cSAlexey Dobriyan } 2121a060ba3SAl Viro 213185cfaf7SArnd Bergmann SYSCALL_DEFINE2(utime, char __user *, filename, struct utimbuf __user *, times) 214185cfaf7SArnd Bergmann { 215185cfaf7SArnd Bergmann struct timespec64 tv[2]; 216185cfaf7SArnd Bergmann 217185cfaf7SArnd Bergmann if (times) { 218185cfaf7SArnd Bergmann if (get_user(tv[0].tv_sec, ×->actime) || 219185cfaf7SArnd Bergmann get_user(tv[1].tv_sec, ×->modtime)) 220185cfaf7SArnd Bergmann return -EFAULT; 221185cfaf7SArnd Bergmann tv[0].tv_nsec = 0; 222185cfaf7SArnd Bergmann tv[1].tv_nsec = 0; 223185cfaf7SArnd Bergmann } 224185cfaf7SArnd Bergmann return do_utimes(AT_FDCWD, filename, times ? tv : NULL, 0); 225185cfaf7SArnd Bergmann } 226185cfaf7SArnd Bergmann #endif 227185cfaf7SArnd Bergmann 2284faea239SArnd Bergmann #ifdef CONFIG_COMPAT_32BIT_TIME 2291a060ba3SAl Viro /* 2301a060ba3SAl Viro * Not all architectures have sys_utime, so implement this in terms 2311a060ba3SAl Viro * of sys_utimes. 2321a060ba3SAl Viro */ 2334faea239SArnd Bergmann #ifdef __ARCH_WANT_SYS_UTIME32 2348dabe724SArnd Bergmann SYSCALL_DEFINE2(utime32, const char __user *, filename, 2354faea239SArnd Bergmann struct old_utimbuf32 __user *, t) 2361a060ba3SAl Viro { 237aaed2dd8SDeepa Dinamani struct timespec64 tv[2]; 2381a060ba3SAl Viro 2391a060ba3SAl Viro if (t) { 2401a060ba3SAl Viro if (get_user(tv[0].tv_sec, &t->actime) || 2411a060ba3SAl Viro get_user(tv[1].tv_sec, &t->modtime)) 2421a060ba3SAl Viro return -EFAULT; 2431a060ba3SAl Viro tv[0].tv_nsec = 0; 2441a060ba3SAl Viro tv[1].tv_nsec = 0; 2451a060ba3SAl Viro } 2461a060ba3SAl Viro return do_utimes(AT_FDCWD, filename, t ? tv : NULL, 0); 2471a060ba3SAl Viro } 2484faea239SArnd Bergmann #endif 2491a060ba3SAl Viro 2508dabe724SArnd Bergmann SYSCALL_DEFINE4(utimensat_time32, unsigned int, dfd, const char __user *, filename, struct old_timespec32 __user *, t, int, flags) 2511a060ba3SAl Viro { 252aaed2dd8SDeepa Dinamani struct timespec64 tv[2]; 2531a060ba3SAl Viro 2541a060ba3SAl Viro if (t) { 2559afc5eeeSArnd Bergmann if (get_old_timespec32(&tv[0], &t[0]) || 2569afc5eeeSArnd Bergmann get_old_timespec32(&tv[1], &t[1])) 2571a060ba3SAl Viro return -EFAULT; 2581a060ba3SAl Viro 2591a060ba3SAl Viro if (tv[0].tv_nsec == UTIME_OMIT && tv[1].tv_nsec == UTIME_OMIT) 2601a060ba3SAl Viro return 0; 2611a060ba3SAl Viro } 2621a060ba3SAl Viro return do_utimes(dfd, filename, t ? tv : NULL, flags); 2631a060ba3SAl Viro } 2641a060ba3SAl Viro 2654faea239SArnd Bergmann #ifdef __ARCH_WANT_SYS_UTIME32 266ab641afaSDominik Brodowski static long do_compat_futimesat(unsigned int dfd, const char __user *filename, 2679afc5eeeSArnd Bergmann struct old_timeval32 __user *t) 2681a060ba3SAl Viro { 269aaed2dd8SDeepa Dinamani struct timespec64 tv[2]; 2701a060ba3SAl Viro 2711a060ba3SAl Viro if (t) { 2721a060ba3SAl Viro if (get_user(tv[0].tv_sec, &t[0].tv_sec) || 2731a060ba3SAl Viro get_user(tv[0].tv_nsec, &t[0].tv_usec) || 2741a060ba3SAl Viro get_user(tv[1].tv_sec, &t[1].tv_sec) || 2751a060ba3SAl Viro get_user(tv[1].tv_nsec, &t[1].tv_usec)) 2761a060ba3SAl Viro return -EFAULT; 2771a060ba3SAl Viro if (tv[0].tv_nsec >= 1000000 || tv[0].tv_nsec < 0 || 2781a060ba3SAl Viro tv[1].tv_nsec >= 1000000 || tv[1].tv_nsec < 0) 2791a060ba3SAl Viro return -EINVAL; 2801a060ba3SAl Viro tv[0].tv_nsec *= 1000; 2811a060ba3SAl Viro tv[1].tv_nsec *= 1000; 2821a060ba3SAl Viro } 2831a060ba3SAl Viro return do_utimes(dfd, filename, t ? tv : NULL, 0); 2841a060ba3SAl Viro } 2851a060ba3SAl Viro 2868dabe724SArnd Bergmann SYSCALL_DEFINE3(futimesat_time32, unsigned int, dfd, 287ab641afaSDominik Brodowski const char __user *, filename, 2889afc5eeeSArnd Bergmann struct old_timeval32 __user *, t) 289ab641afaSDominik Brodowski { 290ab641afaSDominik Brodowski return do_compat_futimesat(dfd, filename, t); 291ab641afaSDominik Brodowski } 292ab641afaSDominik Brodowski 2938dabe724SArnd Bergmann SYSCALL_DEFINE2(utimes_time32, const char __user *, filename, struct old_timeval32 __user *, t) 2941a060ba3SAl Viro { 295ab641afaSDominik Brodowski return do_compat_futimesat(AT_FDCWD, filename, t); 2961a060ba3SAl Viro } 2971a060ba3SAl Viro #endif 2984faea239SArnd Bergmann #endif 299