utimes.c 7.1 KB

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