utimes.c 5.1 KB

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