utimes.c 7.5 KB

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