utimes.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/file.h>
  3. #include <linux/mount.h>
  4. #include <linux/namei.h>
  5. #include <linux/utime.h>
  6. #include <linux/syscalls.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/compat.h>
  9. #include <asm/unistd.h>
  10. #include <linux/filelock.h>
  11. #include "internal.h"
  12. static bool nsec_valid(long nsec)
  13. {
  14. if (nsec == UTIME_OMIT || nsec == UTIME_NOW)
  15. return true;
  16. return nsec >= 0 && nsec <= 999999999;
  17. }
  18. int vfs_utimes(const struct path *path, struct timespec64 *times)
  19. {
  20. int error;
  21. struct iattr newattrs;
  22. struct inode *inode = path->dentry->d_inode;
  23. struct delegated_inode delegated_inode = { };
  24. if (times) {
  25. if (!nsec_valid(times[0].tv_nsec) ||
  26. !nsec_valid(times[1].tv_nsec))
  27. return -EINVAL;
  28. if (times[0].tv_nsec == UTIME_NOW &&
  29. times[1].tv_nsec == UTIME_NOW)
  30. times = NULL;
  31. }
  32. error = mnt_want_write(path->mnt);
  33. if (error)
  34. goto out;
  35. newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
  36. if (times) {
  37. if (times[0].tv_nsec == UTIME_OMIT)
  38. newattrs.ia_valid &= ~ATTR_ATIME;
  39. else if (times[0].tv_nsec != UTIME_NOW) {
  40. newattrs.ia_atime = times[0];
  41. newattrs.ia_valid |= ATTR_ATIME_SET;
  42. }
  43. if (times[1].tv_nsec == UTIME_OMIT)
  44. newattrs.ia_valid &= ~ATTR_MTIME;
  45. else if (times[1].tv_nsec != UTIME_NOW) {
  46. newattrs.ia_mtime = times[1];
  47. newattrs.ia_valid |= ATTR_MTIME_SET;
  48. }
  49. /*
  50. * Tell setattr_prepare(), that this is an explicit time
  51. * update, even if neither ATTR_ATIME_SET nor ATTR_MTIME_SET
  52. * were used.
  53. */
  54. newattrs.ia_valid |= ATTR_TIMES_SET;
  55. } else {
  56. newattrs.ia_valid |= ATTR_TOUCH;
  57. }
  58. retry_deleg:
  59. inode_lock(inode);
  60. error = notify_change(mnt_idmap(path->mnt), path->dentry, &newattrs,
  61. &delegated_inode);
  62. inode_unlock(inode);
  63. if (is_delegated(&delegated_inode)) {
  64. error = break_deleg_wait(&delegated_inode);
  65. if (!error)
  66. goto retry_deleg;
  67. }
  68. mnt_drop_write(path->mnt);
  69. out:
  70. return error;
  71. }
  72. EXPORT_SYMBOL_GPL(vfs_utimes);
  73. static int do_utimes_path(int dfd, const char __user *filename,
  74. struct timespec64 *times, int flags)
  75. {
  76. struct path path;
  77. int lookup_flags = 0, error;
  78. if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH))
  79. return -EINVAL;
  80. if (!(flags & AT_SYMLINK_NOFOLLOW))
  81. lookup_flags |= LOOKUP_FOLLOW;
  82. CLASS(filename_uflags, name)(filename, flags);
  83. retry:
  84. error = filename_lookup(dfd, name, lookup_flags, &path, NULL);
  85. if (error)
  86. return error;
  87. error = vfs_utimes(&path, times);
  88. path_put(&path);
  89. if (retry_estale(error, lookup_flags)) {
  90. lookup_flags |= LOOKUP_REVAL;
  91. goto retry;
  92. }
  93. return error;
  94. }
  95. static int do_utimes_fd(int fd, struct timespec64 *times, int flags)
  96. {
  97. if (flags)
  98. return -EINVAL;
  99. CLASS(fd, f)(fd);
  100. if (fd_empty(f))
  101. return -EBADF;
  102. return vfs_utimes(&fd_file(f)->f_path, times);
  103. }
  104. /*
  105. * do_utimes - change times on filename or file descriptor
  106. * @dfd: open file descriptor, -1 or AT_FDCWD
  107. * @filename: path name or NULL
  108. * @times: new times or NULL
  109. * @flags: zero or more flags (only AT_SYMLINK_NOFOLLOW for the moment)
  110. *
  111. * If filename is NULL and dfd refers to an open file, then operate on
  112. * the file. Otherwise look up filename, possibly using dfd as a
  113. * starting point.
  114. *
  115. * If times==NULL, set access and modification to current time,
  116. * must be owner or have write permission.
  117. * Else, update from *times, must be owner or super user.
  118. */
  119. long do_utimes(int dfd, const char __user *filename, struct timespec64 *times,
  120. int flags)
  121. {
  122. if (filename == NULL && dfd != AT_FDCWD)
  123. return do_utimes_fd(dfd, times, flags);
  124. return do_utimes_path(dfd, filename, times, flags);
  125. }
  126. SYSCALL_DEFINE4(utimensat, int, dfd, const char __user *, filename,
  127. struct __kernel_timespec __user *, utimes, int, flags)
  128. {
  129. struct timespec64 tstimes[2];
  130. if (utimes) {
  131. if ((get_timespec64(&tstimes[0], &utimes[0]) ||
  132. get_timespec64(&tstimes[1], &utimes[1])))
  133. return -EFAULT;
  134. /* Nothing to do, we must not even check the path. */
  135. if (tstimes[0].tv_nsec == UTIME_OMIT &&
  136. tstimes[1].tv_nsec == UTIME_OMIT)
  137. return 0;
  138. }
  139. return do_utimes(dfd, filename, utimes ? tstimes : NULL, flags);
  140. }
  141. #ifdef __ARCH_WANT_SYS_UTIME
  142. /*
  143. * futimesat(), utimes() and utime() are older versions of utimensat()
  144. * that are provided for compatibility with traditional C libraries.
  145. * On modern architectures, we always use libc wrappers around
  146. * utimensat() instead.
  147. */
  148. static long do_futimesat(int dfd, const char __user *filename,
  149. struct __kernel_old_timeval __user *utimes)
  150. {
  151. struct __kernel_old_timeval times[2];
  152. struct timespec64 tstimes[2];
  153. if (utimes) {
  154. if (copy_from_user(&times, utimes, sizeof(times)))
  155. return -EFAULT;
  156. /* This test is needed to catch all invalid values. If we
  157. would test only in do_utimes we would miss those invalid
  158. values truncated by the multiplication with 1000. Note
  159. that we also catch UTIME_{NOW,OMIT} here which are only
  160. valid for utimensat. */
  161. if (times[0].tv_usec >= 1000000 || times[0].tv_usec < 0 ||
  162. times[1].tv_usec >= 1000000 || times[1].tv_usec < 0)
  163. return -EINVAL;
  164. tstimes[0].tv_sec = times[0].tv_sec;
  165. tstimes[0].tv_nsec = 1000 * times[0].tv_usec;
  166. tstimes[1].tv_sec = times[1].tv_sec;
  167. tstimes[1].tv_nsec = 1000 * times[1].tv_usec;
  168. }
  169. return do_utimes(dfd, filename, utimes ? tstimes : NULL, 0);
  170. }
  171. SYSCALL_DEFINE3(futimesat, int, dfd, const char __user *, filename,
  172. struct __kernel_old_timeval __user *, utimes)
  173. {
  174. return do_futimesat(dfd, filename, utimes);
  175. }
  176. SYSCALL_DEFINE2(utimes, char __user *, filename,
  177. struct __kernel_old_timeval __user *, utimes)
  178. {
  179. return do_futimesat(AT_FDCWD, filename, utimes);
  180. }
  181. SYSCALL_DEFINE2(utime, char __user *, filename, struct utimbuf __user *, times)
  182. {
  183. struct timespec64 tv[2];
  184. if (times) {
  185. if (get_user(tv[0].tv_sec, &times->actime) ||
  186. get_user(tv[1].tv_sec, &times->modtime))
  187. return -EFAULT;
  188. tv[0].tv_nsec = 0;
  189. tv[1].tv_nsec = 0;
  190. }
  191. return do_utimes(AT_FDCWD, filename, times ? tv : NULL, 0);
  192. }
  193. #endif
  194. #ifdef CONFIG_COMPAT_32BIT_TIME
  195. /*
  196. * Not all architectures have sys_utime, so implement this in terms
  197. * of sys_utimes.
  198. */
  199. #ifdef __ARCH_WANT_SYS_UTIME32
  200. SYSCALL_DEFINE2(utime32, const char __user *, filename,
  201. struct old_utimbuf32 __user *, t)
  202. {
  203. struct timespec64 tv[2];
  204. if (t) {
  205. if (get_user(tv[0].tv_sec, &t->actime) ||
  206. get_user(tv[1].tv_sec, &t->modtime))
  207. return -EFAULT;
  208. tv[0].tv_nsec = 0;
  209. tv[1].tv_nsec = 0;
  210. }
  211. return do_utimes(AT_FDCWD, filename, t ? tv : NULL, 0);
  212. }
  213. #endif
  214. SYSCALL_DEFINE4(utimensat_time32, unsigned int, dfd, const char __user *, filename, struct old_timespec32 __user *, t, int, flags)
  215. {
  216. struct timespec64 tv[2];
  217. if (t) {
  218. if (get_old_timespec32(&tv[0], &t[0]) ||
  219. get_old_timespec32(&tv[1], &t[1]))
  220. return -EFAULT;
  221. if (tv[0].tv_nsec == UTIME_OMIT && tv[1].tv_nsec == UTIME_OMIT)
  222. return 0;
  223. }
  224. return do_utimes(dfd, filename, t ? tv : NULL, flags);
  225. }
  226. #ifdef __ARCH_WANT_SYS_UTIME32
  227. static long do_compat_futimesat(unsigned int dfd, const char __user *filename,
  228. struct old_timeval32 __user *t)
  229. {
  230. struct timespec64 tv[2];
  231. if (t) {
  232. if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
  233. get_user(tv[0].tv_nsec, &t[0].tv_usec) ||
  234. get_user(tv[1].tv_sec, &t[1].tv_sec) ||
  235. get_user(tv[1].tv_nsec, &t[1].tv_usec))
  236. return -EFAULT;
  237. if (tv[0].tv_nsec >= 1000000 || tv[0].tv_nsec < 0 ||
  238. tv[1].tv_nsec >= 1000000 || tv[1].tv_nsec < 0)
  239. return -EINVAL;
  240. tv[0].tv_nsec *= 1000;
  241. tv[1].tv_nsec *= 1000;
  242. }
  243. return do_utimes(dfd, filename, t ? tv : NULL, 0);
  244. }
  245. SYSCALL_DEFINE3(futimesat_time32, unsigned int, dfd,
  246. const char __user *, filename,
  247. struct old_timeval32 __user *, t)
  248. {
  249. return do_compat_futimesat(dfd, filename, t);
  250. }
  251. SYSCALL_DEFINE2(utimes_time32, const char __user *, filename, struct old_timeval32 __user *, t)
  252. {
  253. return do_compat_futimesat(AT_FDCWD, filename, t);
  254. }
  255. #endif
  256. #endif