utimes.c 7.6 KB

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