utimes.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #include <linux/compiler.h>
  2. #include <linux/file.h>
  3. #include <linux/fs.h>
  4. #include <linux/linkage.h>
  5. #include <linux/mount.h>
  6. #include <linux/namei.h>
  7. #include <linux/sched.h>
  8. #include <linux/stat.h>
  9. #include <linux/utime.h>
  10. #include <linux/syscalls.h>
  11. #include <asm/uaccess.h>
  12. #include <asm/unistd.h>
  13. #ifdef __ARCH_WANT_SYS_UTIME
  14. /*
  15. * sys_utime() can be implemented in user-level using sys_utimes().
  16. * Is this for backwards compatibility? If so, why not move it
  17. * into the appropriate arch directory (for those architectures that
  18. * need it).
  19. */
  20. /* If times==NULL, set access and modification to current time,
  21. * must be owner or have write permission.
  22. * Else, update from *times, must be owner or super user.
  23. */
  24. asmlinkage long sys_utime(char __user *filename, struct utimbuf __user *times)
  25. {
  26. struct timespec tv[2];
  27. if (times) {
  28. if (get_user(tv[0].tv_sec, &times->actime) ||
  29. get_user(tv[1].tv_sec, &times->modtime))
  30. return -EFAULT;
  31. tv[0].tv_nsec = 0;
  32. tv[1].tv_nsec = 0;
  33. }
  34. return do_utimes(AT_FDCWD, filename, times ? tv : NULL, 0);
  35. }
  36. #endif
  37. static bool nsec_valid(long nsec)
  38. {
  39. if (nsec == UTIME_OMIT || nsec == UTIME_NOW)
  40. return true;
  41. return nsec >= 0 && nsec <= 999999999;
  42. }
  43. /* If times==NULL, set access and modification to current time,
  44. * must be owner or have write permission.
  45. * Else, update from *times, must be owner or super user.
  46. */
  47. long do_utimes(int dfd, char __user *filename, struct timespec *times, int flags)
  48. {
  49. int error;
  50. struct nameidata nd;
  51. struct dentry *dentry;
  52. struct inode *inode;
  53. struct iattr newattrs;
  54. struct file *f = NULL;
  55. struct vfsmount *mnt;
  56. error = -EINVAL;
  57. if (times && (!nsec_valid(times[0].tv_nsec) ||
  58. !nsec_valid(times[1].tv_nsec))) {
  59. goto out;
  60. }
  61. if (flags & ~AT_SYMLINK_NOFOLLOW)
  62. goto out;
  63. if (filename == NULL && dfd != AT_FDCWD) {
  64. error = -EINVAL;
  65. if (flags & AT_SYMLINK_NOFOLLOW)
  66. goto out;
  67. error = -EBADF;
  68. f = fget(dfd);
  69. if (!f)
  70. goto out;
  71. dentry = f->f_path.dentry;
  72. mnt = f->f_path.mnt;
  73. } else {
  74. error = __user_walk_fd(dfd, filename, (flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW, &nd);
  75. if (error)
  76. goto out;
  77. dentry = nd.path.dentry;
  78. mnt = nd.path.mnt;
  79. }
  80. inode = dentry->d_inode;
  81. error = mnt_want_write(mnt);
  82. if (error)
  83. goto dput_and_out;
  84. if (times && times[0].tv_nsec == UTIME_NOW &&
  85. times[1].tv_nsec == UTIME_NOW)
  86. times = NULL;
  87. /* In most cases, the checks are done in inode_change_ok() */
  88. newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
  89. if (times) {
  90. error = -EPERM;
  91. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  92. goto mnt_drop_write_and_out;
  93. if (times[0].tv_nsec == UTIME_OMIT)
  94. newattrs.ia_valid &= ~ATTR_ATIME;
  95. else if (times[0].tv_nsec != UTIME_NOW) {
  96. newattrs.ia_atime.tv_sec = times[0].tv_sec;
  97. newattrs.ia_atime.tv_nsec = times[0].tv_nsec;
  98. newattrs.ia_valid |= ATTR_ATIME_SET;
  99. }
  100. if (times[1].tv_nsec == UTIME_OMIT)
  101. newattrs.ia_valid &= ~ATTR_MTIME;
  102. else if (times[1].tv_nsec != UTIME_NOW) {
  103. newattrs.ia_mtime.tv_sec = times[1].tv_sec;
  104. newattrs.ia_mtime.tv_nsec = times[1].tv_nsec;
  105. newattrs.ia_valid |= ATTR_MTIME_SET;
  106. }
  107. /*
  108. * For the UTIME_OMIT/UTIME_NOW and UTIME_NOW/UTIME_OMIT
  109. * cases, we need to make an extra check that is not done by
  110. * inode_change_ok().
  111. */
  112. if (((times[0].tv_nsec == UTIME_NOW &&
  113. times[1].tv_nsec == UTIME_OMIT)
  114. ||
  115. (times[0].tv_nsec == UTIME_OMIT &&
  116. times[1].tv_nsec == UTIME_NOW))
  117. && !is_owner_or_cap(inode))
  118. goto mnt_drop_write_and_out;
  119. } else {
  120. /*
  121. * If times is NULL (or both times are UTIME_NOW),
  122. * then we need to check permissions, because
  123. * inode_change_ok() won't do it.
  124. */
  125. error = -EACCES;
  126. if (IS_IMMUTABLE(inode))
  127. goto mnt_drop_write_and_out;
  128. if (!is_owner_or_cap(inode)) {
  129. error = permission(inode, MAY_WRITE, NULL);
  130. if (error)
  131. goto mnt_drop_write_and_out;
  132. }
  133. }
  134. mutex_lock(&inode->i_mutex);
  135. error = notify_change(dentry, &newattrs);
  136. mutex_unlock(&inode->i_mutex);
  137. mnt_drop_write_and_out:
  138. mnt_drop_write(mnt);
  139. dput_and_out:
  140. if (f)
  141. fput(f);
  142. else
  143. path_put(&nd.path);
  144. out:
  145. return error;
  146. }
  147. asmlinkage long sys_utimensat(int dfd, char __user *filename, struct timespec __user *utimes, int flags)
  148. {
  149. struct timespec tstimes[2];
  150. if (utimes) {
  151. if (copy_from_user(&tstimes, utimes, sizeof(tstimes)))
  152. return -EFAULT;
  153. /* Nothing to do, we must not even check the path. */
  154. if (tstimes[0].tv_nsec == UTIME_OMIT &&
  155. tstimes[1].tv_nsec == UTIME_OMIT)
  156. return 0;
  157. }
  158. return do_utimes(dfd, filename, utimes ? tstimes : NULL, flags);
  159. }
  160. asmlinkage long sys_futimesat(int dfd, char __user *filename, struct timeval __user *utimes)
  161. {
  162. struct timeval times[2];
  163. struct timespec 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. asmlinkage long sys_utimes(char __user *filename, struct timeval __user *utimes)
  183. {
  184. return sys_futimesat(AT_FDCWD, filename, utimes);
  185. }