utimes.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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_special(long nsec)
  38. {
  39. return nsec == UTIME_OMIT || nsec == UTIME_NOW;
  40. }
  41. static bool nsec_valid(long nsec)
  42. {
  43. if (nsec_special(nsec))
  44. return true;
  45. return nsec >= 0 && nsec <= 999999999;
  46. }
  47. /* If times==NULL, set access and modification to current time,
  48. * must be owner or have write permission.
  49. * Else, update from *times, must be owner or super user.
  50. */
  51. long do_utimes(int dfd, char __user *filename, struct timespec *times, int flags)
  52. {
  53. int error;
  54. struct nameidata nd;
  55. struct dentry *dentry;
  56. struct inode *inode;
  57. struct iattr newattrs;
  58. struct file *f = NULL;
  59. struct vfsmount *mnt;
  60. error = -EINVAL;
  61. if (times && (!nsec_valid(times[0].tv_nsec) ||
  62. !nsec_valid(times[1].tv_nsec))) {
  63. goto out;
  64. }
  65. if (flags & ~AT_SYMLINK_NOFOLLOW)
  66. goto out;
  67. if (filename == NULL && dfd != AT_FDCWD) {
  68. error = -EINVAL;
  69. if (flags & AT_SYMLINK_NOFOLLOW)
  70. goto out;
  71. error = -EBADF;
  72. f = fget(dfd);
  73. if (!f)
  74. goto out;
  75. dentry = f->f_path.dentry;
  76. mnt = f->f_path.mnt;
  77. } else {
  78. error = __user_walk_fd(dfd, filename, (flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW, &nd);
  79. if (error)
  80. goto out;
  81. dentry = nd.path.dentry;
  82. mnt = nd.path.mnt;
  83. }
  84. inode = dentry->d_inode;
  85. error = mnt_want_write(mnt);
  86. if (error)
  87. goto dput_and_out;
  88. if (times && times[0].tv_nsec == UTIME_NOW &&
  89. times[1].tv_nsec == UTIME_NOW)
  90. times = NULL;
  91. /* Don't worry, the checks are done in inode_change_ok() */
  92. newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
  93. if (times) {
  94. error = -EPERM;
  95. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  96. goto mnt_drop_write_and_out;
  97. if (times[0].tv_nsec == UTIME_OMIT)
  98. newattrs.ia_valid &= ~ATTR_ATIME;
  99. else if (times[0].tv_nsec != UTIME_NOW) {
  100. newattrs.ia_atime.tv_sec = times[0].tv_sec;
  101. newattrs.ia_atime.tv_nsec = times[0].tv_nsec;
  102. newattrs.ia_valid |= ATTR_ATIME_SET;
  103. }
  104. if (times[1].tv_nsec == UTIME_OMIT)
  105. newattrs.ia_valid &= ~ATTR_MTIME;
  106. else if (times[1].tv_nsec != UTIME_NOW) {
  107. newattrs.ia_mtime.tv_sec = times[1].tv_sec;
  108. newattrs.ia_mtime.tv_nsec = times[1].tv_nsec;
  109. newattrs.ia_valid |= ATTR_MTIME_SET;
  110. }
  111. }
  112. /*
  113. * If times is NULL or both times are either UTIME_OMIT or
  114. * UTIME_NOW, then need to check permissions, because
  115. * inode_change_ok() won't do it.
  116. */
  117. if (!times || (nsec_special(times[0].tv_nsec) &&
  118. nsec_special(times[1].tv_nsec))) {
  119. error = -EACCES;
  120. if (IS_IMMUTABLE(inode))
  121. goto mnt_drop_write_and_out;
  122. if (!is_owner_or_cap(inode)) {
  123. if (f) {
  124. if (!(f->f_mode & FMODE_WRITE))
  125. goto mnt_drop_write_and_out;
  126. } else {
  127. error = vfs_permission(&nd, MAY_WRITE);
  128. if (error)
  129. goto mnt_drop_write_and_out;
  130. }
  131. }
  132. }
  133. mutex_lock(&inode->i_mutex);
  134. error = notify_change(dentry, &newattrs);
  135. mutex_unlock(&inode->i_mutex);
  136. mnt_drop_write_and_out:
  137. mnt_drop_write(mnt);
  138. dput_and_out:
  139. if (f)
  140. fput(f);
  141. else
  142. path_put(&nd.path);
  143. out:
  144. return error;
  145. }
  146. asmlinkage long sys_utimensat(int dfd, char __user *filename, struct timespec __user *utimes, int flags)
  147. {
  148. struct timespec tstimes[2];
  149. if (utimes) {
  150. if (copy_from_user(&tstimes, utimes, sizeof(tstimes)))
  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. asmlinkage long sys_futimesat(int dfd, char __user *filename, struct timeval __user *utimes)
  160. {
  161. struct timeval times[2];
  162. struct timespec 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. asmlinkage long sys_utimes(char __user *filename, struct timeval __user *utimes)
  182. {
  183. return sys_futimesat(AT_FDCWD, filename, utimes);
  184. }