utimes.c 7.7 KB

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