utimes.c 7.4 KB

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