18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci#include <linux/file.h> 38c2ecf20Sopenharmony_ci#include <linux/mount.h> 48c2ecf20Sopenharmony_ci#include <linux/namei.h> 58c2ecf20Sopenharmony_ci#include <linux/utime.h> 68c2ecf20Sopenharmony_ci#include <linux/syscalls.h> 78c2ecf20Sopenharmony_ci#include <linux/uaccess.h> 88c2ecf20Sopenharmony_ci#include <linux/compat.h> 98c2ecf20Sopenharmony_ci#include <asm/unistd.h> 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_cistatic bool nsec_valid(long nsec) 128c2ecf20Sopenharmony_ci{ 138c2ecf20Sopenharmony_ci if (nsec == UTIME_OMIT || nsec == UTIME_NOW) 148c2ecf20Sopenharmony_ci return true; 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci return nsec >= 0 && nsec <= 999999999; 178c2ecf20Sopenharmony_ci} 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ciint vfs_utimes(const struct path *path, struct timespec64 *times) 208c2ecf20Sopenharmony_ci{ 218c2ecf20Sopenharmony_ci int error; 228c2ecf20Sopenharmony_ci struct iattr newattrs; 238c2ecf20Sopenharmony_ci struct inode *inode = path->dentry->d_inode; 248c2ecf20Sopenharmony_ci struct inode *delegated_inode = NULL; 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci if (times) { 278c2ecf20Sopenharmony_ci if (!nsec_valid(times[0].tv_nsec) || 288c2ecf20Sopenharmony_ci !nsec_valid(times[1].tv_nsec)) 298c2ecf20Sopenharmony_ci return -EINVAL; 308c2ecf20Sopenharmony_ci if (times[0].tv_nsec == UTIME_NOW && 318c2ecf20Sopenharmony_ci times[1].tv_nsec == UTIME_NOW) 328c2ecf20Sopenharmony_ci times = NULL; 338c2ecf20Sopenharmony_ci } 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci error = mnt_want_write(path->mnt); 368c2ecf20Sopenharmony_ci if (error) 378c2ecf20Sopenharmony_ci goto out; 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME; 408c2ecf20Sopenharmony_ci if (times) { 418c2ecf20Sopenharmony_ci if (times[0].tv_nsec == UTIME_OMIT) 428c2ecf20Sopenharmony_ci newattrs.ia_valid &= ~ATTR_ATIME; 438c2ecf20Sopenharmony_ci else if (times[0].tv_nsec != UTIME_NOW) { 448c2ecf20Sopenharmony_ci newattrs.ia_atime = times[0]; 458c2ecf20Sopenharmony_ci newattrs.ia_valid |= ATTR_ATIME_SET; 468c2ecf20Sopenharmony_ci } 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci if (times[1].tv_nsec == UTIME_OMIT) 498c2ecf20Sopenharmony_ci newattrs.ia_valid &= ~ATTR_MTIME; 508c2ecf20Sopenharmony_ci else if (times[1].tv_nsec != UTIME_NOW) { 518c2ecf20Sopenharmony_ci newattrs.ia_mtime = times[1]; 528c2ecf20Sopenharmony_ci newattrs.ia_valid |= ATTR_MTIME_SET; 538c2ecf20Sopenharmony_ci } 548c2ecf20Sopenharmony_ci /* 558c2ecf20Sopenharmony_ci * Tell setattr_prepare(), that this is an explicit time 568c2ecf20Sopenharmony_ci * update, even if neither ATTR_ATIME_SET nor ATTR_MTIME_SET 578c2ecf20Sopenharmony_ci * were used. 588c2ecf20Sopenharmony_ci */ 598c2ecf20Sopenharmony_ci newattrs.ia_valid |= ATTR_TIMES_SET; 608c2ecf20Sopenharmony_ci } else { 618c2ecf20Sopenharmony_ci newattrs.ia_valid |= ATTR_TOUCH; 628c2ecf20Sopenharmony_ci } 638c2ecf20Sopenharmony_ciretry_deleg: 648c2ecf20Sopenharmony_ci inode_lock(inode); 658c2ecf20Sopenharmony_ci error = notify_change(path->dentry, &newattrs, &delegated_inode); 668c2ecf20Sopenharmony_ci inode_unlock(inode); 678c2ecf20Sopenharmony_ci if (delegated_inode) { 688c2ecf20Sopenharmony_ci error = break_deleg_wait(&delegated_inode); 698c2ecf20Sopenharmony_ci if (!error) 708c2ecf20Sopenharmony_ci goto retry_deleg; 718c2ecf20Sopenharmony_ci } 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci mnt_drop_write(path->mnt); 748c2ecf20Sopenharmony_ciout: 758c2ecf20Sopenharmony_ci return error; 768c2ecf20Sopenharmony_ci} 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_cistatic int do_utimes_path(int dfd, const char __user *filename, 798c2ecf20Sopenharmony_ci struct timespec64 *times, int flags) 808c2ecf20Sopenharmony_ci{ 818c2ecf20Sopenharmony_ci struct path path; 828c2ecf20Sopenharmony_ci int lookup_flags = 0, error; 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) 858c2ecf20Sopenharmony_ci return -EINVAL; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci if (!(flags & AT_SYMLINK_NOFOLLOW)) 888c2ecf20Sopenharmony_ci lookup_flags |= LOOKUP_FOLLOW; 898c2ecf20Sopenharmony_ci if (flags & AT_EMPTY_PATH) 908c2ecf20Sopenharmony_ci lookup_flags |= LOOKUP_EMPTY; 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ciretry: 938c2ecf20Sopenharmony_ci error = user_path_at(dfd, filename, lookup_flags, &path); 948c2ecf20Sopenharmony_ci if (error) 958c2ecf20Sopenharmony_ci return error; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci error = vfs_utimes(&path, times); 988c2ecf20Sopenharmony_ci path_put(&path); 998c2ecf20Sopenharmony_ci if (retry_estale(error, lookup_flags)) { 1008c2ecf20Sopenharmony_ci lookup_flags |= LOOKUP_REVAL; 1018c2ecf20Sopenharmony_ci goto retry; 1028c2ecf20Sopenharmony_ci } 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci return error; 1058c2ecf20Sopenharmony_ci} 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_cistatic int do_utimes_fd(int fd, struct timespec64 *times, int flags) 1088c2ecf20Sopenharmony_ci{ 1098c2ecf20Sopenharmony_ci struct fd f; 1108c2ecf20Sopenharmony_ci int error; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci if (flags) 1138c2ecf20Sopenharmony_ci return -EINVAL; 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci f = fdget(fd); 1168c2ecf20Sopenharmony_ci if (!f.file) 1178c2ecf20Sopenharmony_ci return -EBADF; 1188c2ecf20Sopenharmony_ci error = vfs_utimes(&f.file->f_path, times); 1198c2ecf20Sopenharmony_ci fdput(f); 1208c2ecf20Sopenharmony_ci return error; 1218c2ecf20Sopenharmony_ci} 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci/* 1248c2ecf20Sopenharmony_ci * do_utimes - change times on filename or file descriptor 1258c2ecf20Sopenharmony_ci * @dfd: open file descriptor, -1 or AT_FDCWD 1268c2ecf20Sopenharmony_ci * @filename: path name or NULL 1278c2ecf20Sopenharmony_ci * @times: new times or NULL 1288c2ecf20Sopenharmony_ci * @flags: zero or more flags (only AT_SYMLINK_NOFOLLOW for the moment) 1298c2ecf20Sopenharmony_ci * 1308c2ecf20Sopenharmony_ci * If filename is NULL and dfd refers to an open file, then operate on 1318c2ecf20Sopenharmony_ci * the file. Otherwise look up filename, possibly using dfd as a 1328c2ecf20Sopenharmony_ci * starting point. 1338c2ecf20Sopenharmony_ci * 1348c2ecf20Sopenharmony_ci * If times==NULL, set access and modification to current time, 1358c2ecf20Sopenharmony_ci * must be owner or have write permission. 1368c2ecf20Sopenharmony_ci * Else, update from *times, must be owner or super user. 1378c2ecf20Sopenharmony_ci */ 1388c2ecf20Sopenharmony_cilong do_utimes(int dfd, const char __user *filename, struct timespec64 *times, 1398c2ecf20Sopenharmony_ci int flags) 1408c2ecf20Sopenharmony_ci{ 1418c2ecf20Sopenharmony_ci if (filename == NULL && dfd != AT_FDCWD) 1428c2ecf20Sopenharmony_ci return do_utimes_fd(dfd, times, flags); 1438c2ecf20Sopenharmony_ci return do_utimes_path(dfd, filename, times, flags); 1448c2ecf20Sopenharmony_ci} 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ciSYSCALL_DEFINE4(utimensat, int, dfd, const char __user *, filename, 1478c2ecf20Sopenharmony_ci struct __kernel_timespec __user *, utimes, int, flags) 1488c2ecf20Sopenharmony_ci{ 1498c2ecf20Sopenharmony_ci struct timespec64 tstimes[2]; 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci if (utimes) { 1528c2ecf20Sopenharmony_ci if ((get_timespec64(&tstimes[0], &utimes[0]) || 1538c2ecf20Sopenharmony_ci get_timespec64(&tstimes[1], &utimes[1]))) 1548c2ecf20Sopenharmony_ci return -EFAULT; 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci /* Nothing to do, we must not even check the path. */ 1578c2ecf20Sopenharmony_ci if (tstimes[0].tv_nsec == UTIME_OMIT && 1588c2ecf20Sopenharmony_ci tstimes[1].tv_nsec == UTIME_OMIT) 1598c2ecf20Sopenharmony_ci return 0; 1608c2ecf20Sopenharmony_ci } 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci return do_utimes(dfd, filename, utimes ? tstimes : NULL, flags); 1638c2ecf20Sopenharmony_ci} 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci#ifdef __ARCH_WANT_SYS_UTIME 1668c2ecf20Sopenharmony_ci/* 1678c2ecf20Sopenharmony_ci * futimesat(), utimes() and utime() are older versions of utimensat() 1688c2ecf20Sopenharmony_ci * that are provided for compatibility with traditional C libraries. 1698c2ecf20Sopenharmony_ci * On modern architectures, we always use libc wrappers around 1708c2ecf20Sopenharmony_ci * utimensat() instead. 1718c2ecf20Sopenharmony_ci */ 1728c2ecf20Sopenharmony_cistatic long do_futimesat(int dfd, const char __user *filename, 1738c2ecf20Sopenharmony_ci struct __kernel_old_timeval __user *utimes) 1748c2ecf20Sopenharmony_ci{ 1758c2ecf20Sopenharmony_ci struct __kernel_old_timeval times[2]; 1768c2ecf20Sopenharmony_ci struct timespec64 tstimes[2]; 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci if (utimes) { 1798c2ecf20Sopenharmony_ci if (copy_from_user(×, utimes, sizeof(times))) 1808c2ecf20Sopenharmony_ci return -EFAULT; 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci /* This test is needed to catch all invalid values. If we 1838c2ecf20Sopenharmony_ci would test only in do_utimes we would miss those invalid 1848c2ecf20Sopenharmony_ci values truncated by the multiplication with 1000. Note 1858c2ecf20Sopenharmony_ci that we also catch UTIME_{NOW,OMIT} here which are only 1868c2ecf20Sopenharmony_ci valid for utimensat. */ 1878c2ecf20Sopenharmony_ci if (times[0].tv_usec >= 1000000 || times[0].tv_usec < 0 || 1888c2ecf20Sopenharmony_ci times[1].tv_usec >= 1000000 || times[1].tv_usec < 0) 1898c2ecf20Sopenharmony_ci return -EINVAL; 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci tstimes[0].tv_sec = times[0].tv_sec; 1928c2ecf20Sopenharmony_ci tstimes[0].tv_nsec = 1000 * times[0].tv_usec; 1938c2ecf20Sopenharmony_ci tstimes[1].tv_sec = times[1].tv_sec; 1948c2ecf20Sopenharmony_ci tstimes[1].tv_nsec = 1000 * times[1].tv_usec; 1958c2ecf20Sopenharmony_ci } 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci return do_utimes(dfd, filename, utimes ? tstimes : NULL, 0); 1988c2ecf20Sopenharmony_ci} 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ciSYSCALL_DEFINE3(futimesat, int, dfd, const char __user *, filename, 2028c2ecf20Sopenharmony_ci struct __kernel_old_timeval __user *, utimes) 2038c2ecf20Sopenharmony_ci{ 2048c2ecf20Sopenharmony_ci return do_futimesat(dfd, filename, utimes); 2058c2ecf20Sopenharmony_ci} 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ciSYSCALL_DEFINE2(utimes, char __user *, filename, 2088c2ecf20Sopenharmony_ci struct __kernel_old_timeval __user *, utimes) 2098c2ecf20Sopenharmony_ci{ 2108c2ecf20Sopenharmony_ci return do_futimesat(AT_FDCWD, filename, utimes); 2118c2ecf20Sopenharmony_ci} 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ciSYSCALL_DEFINE2(utime, char __user *, filename, struct utimbuf __user *, times) 2148c2ecf20Sopenharmony_ci{ 2158c2ecf20Sopenharmony_ci struct timespec64 tv[2]; 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci if (times) { 2188c2ecf20Sopenharmony_ci if (get_user(tv[0].tv_sec, ×->actime) || 2198c2ecf20Sopenharmony_ci get_user(tv[1].tv_sec, ×->modtime)) 2208c2ecf20Sopenharmony_ci return -EFAULT; 2218c2ecf20Sopenharmony_ci tv[0].tv_nsec = 0; 2228c2ecf20Sopenharmony_ci tv[1].tv_nsec = 0; 2238c2ecf20Sopenharmony_ci } 2248c2ecf20Sopenharmony_ci return do_utimes(AT_FDCWD, filename, times ? tv : NULL, 0); 2258c2ecf20Sopenharmony_ci} 2268c2ecf20Sopenharmony_ci#endif 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci#ifdef CONFIG_COMPAT_32BIT_TIME 2298c2ecf20Sopenharmony_ci/* 2308c2ecf20Sopenharmony_ci * Not all architectures have sys_utime, so implement this in terms 2318c2ecf20Sopenharmony_ci * of sys_utimes. 2328c2ecf20Sopenharmony_ci */ 2338c2ecf20Sopenharmony_ci#ifdef __ARCH_WANT_SYS_UTIME32 2348c2ecf20Sopenharmony_ciSYSCALL_DEFINE2(utime32, const char __user *, filename, 2358c2ecf20Sopenharmony_ci struct old_utimbuf32 __user *, t) 2368c2ecf20Sopenharmony_ci{ 2378c2ecf20Sopenharmony_ci struct timespec64 tv[2]; 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci if (t) { 2408c2ecf20Sopenharmony_ci if (get_user(tv[0].tv_sec, &t->actime) || 2418c2ecf20Sopenharmony_ci get_user(tv[1].tv_sec, &t->modtime)) 2428c2ecf20Sopenharmony_ci return -EFAULT; 2438c2ecf20Sopenharmony_ci tv[0].tv_nsec = 0; 2448c2ecf20Sopenharmony_ci tv[1].tv_nsec = 0; 2458c2ecf20Sopenharmony_ci } 2468c2ecf20Sopenharmony_ci return do_utimes(AT_FDCWD, filename, t ? tv : NULL, 0); 2478c2ecf20Sopenharmony_ci} 2488c2ecf20Sopenharmony_ci#endif 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ciSYSCALL_DEFINE4(utimensat_time32, unsigned int, dfd, const char __user *, filename, struct old_timespec32 __user *, t, int, flags) 2518c2ecf20Sopenharmony_ci{ 2528c2ecf20Sopenharmony_ci struct timespec64 tv[2]; 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci if (t) { 2558c2ecf20Sopenharmony_ci if (get_old_timespec32(&tv[0], &t[0]) || 2568c2ecf20Sopenharmony_ci get_old_timespec32(&tv[1], &t[1])) 2578c2ecf20Sopenharmony_ci return -EFAULT; 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci if (tv[0].tv_nsec == UTIME_OMIT && tv[1].tv_nsec == UTIME_OMIT) 2608c2ecf20Sopenharmony_ci return 0; 2618c2ecf20Sopenharmony_ci } 2628c2ecf20Sopenharmony_ci return do_utimes(dfd, filename, t ? tv : NULL, flags); 2638c2ecf20Sopenharmony_ci} 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci#ifdef __ARCH_WANT_SYS_UTIME32 2668c2ecf20Sopenharmony_cistatic long do_compat_futimesat(unsigned int dfd, const char __user *filename, 2678c2ecf20Sopenharmony_ci struct old_timeval32 __user *t) 2688c2ecf20Sopenharmony_ci{ 2698c2ecf20Sopenharmony_ci struct timespec64 tv[2]; 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_ci if (t) { 2728c2ecf20Sopenharmony_ci if (get_user(tv[0].tv_sec, &t[0].tv_sec) || 2738c2ecf20Sopenharmony_ci get_user(tv[0].tv_nsec, &t[0].tv_usec) || 2748c2ecf20Sopenharmony_ci get_user(tv[1].tv_sec, &t[1].tv_sec) || 2758c2ecf20Sopenharmony_ci get_user(tv[1].tv_nsec, &t[1].tv_usec)) 2768c2ecf20Sopenharmony_ci return -EFAULT; 2778c2ecf20Sopenharmony_ci if (tv[0].tv_nsec >= 1000000 || tv[0].tv_nsec < 0 || 2788c2ecf20Sopenharmony_ci tv[1].tv_nsec >= 1000000 || tv[1].tv_nsec < 0) 2798c2ecf20Sopenharmony_ci return -EINVAL; 2808c2ecf20Sopenharmony_ci tv[0].tv_nsec *= 1000; 2818c2ecf20Sopenharmony_ci tv[1].tv_nsec *= 1000; 2828c2ecf20Sopenharmony_ci } 2838c2ecf20Sopenharmony_ci return do_utimes(dfd, filename, t ? tv : NULL, 0); 2848c2ecf20Sopenharmony_ci} 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_ciSYSCALL_DEFINE3(futimesat_time32, unsigned int, dfd, 2878c2ecf20Sopenharmony_ci const char __user *, filename, 2888c2ecf20Sopenharmony_ci struct old_timeval32 __user *, t) 2898c2ecf20Sopenharmony_ci{ 2908c2ecf20Sopenharmony_ci return do_compat_futimesat(dfd, filename, t); 2918c2ecf20Sopenharmony_ci} 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ciSYSCALL_DEFINE2(utimes_time32, const char __user *, filename, struct old_timeval32 __user *, t) 2948c2ecf20Sopenharmony_ci{ 2958c2ecf20Sopenharmony_ci return do_compat_futimesat(AT_FDCWD, filename, t); 2968c2ecf20Sopenharmony_ci} 2978c2ecf20Sopenharmony_ci#endif 2988c2ecf20Sopenharmony_ci#endif 299