utimes.c 7.6 KB

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