Ulrich Drepper | 1c710c8 | 2007-05-08 00:33:25 -0700 | [diff] [blame] | 1 | #include <linux/file.h> |
Dave Hansen | 74f9fdf | 2008-02-15 14:37:42 -0800 | [diff] [blame] | 2 | #include <linux/mount.h> |
Alexey Dobriyan | 82b0547 | 2006-09-30 23:27:22 -0700 | [diff] [blame] | 3 | #include <linux/namei.h> |
Alexey Dobriyan | 82b0547 | 2006-09-30 23:27:22 -0700 | [diff] [blame] | 4 | #include <linux/utime.h> |
Adrian Bunk | 12c2ab5 | 2008-02-06 01:36:47 -0800 | [diff] [blame] | 5 | #include <linux/syscalls.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 6 | #include <linux/uaccess.h> |
Al Viro | 1a060ba | 2017-04-08 18:04:59 -0400 | [diff] [blame] | 7 | #include <linux/compat.h> |
Alexey Dobriyan | 82b0547 | 2006-09-30 23:27:22 -0700 | [diff] [blame] | 8 | #include <asm/unistd.h> |
| 9 | |
| 10 | #ifdef __ARCH_WANT_SYS_UTIME |
| 11 | |
| 12 | /* |
| 13 | * sys_utime() can be implemented in user-level using sys_utimes(). |
| 14 | * Is this for backwards compatibility? If so, why not move it |
| 15 | * into the appropriate arch directory (for those architectures that |
| 16 | * need it). |
| 17 | */ |
| 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 | */ |
Heiko Carstens | 003d7ab | 2009-01-14 14:14:21 +0100 | [diff] [blame] | 23 | SYSCALL_DEFINE2(utime, char __user *, filename, struct utimbuf __user *, times) |
Alexey Dobriyan | 82b0547 | 2006-09-30 23:27:22 -0700 | [diff] [blame] | 24 | { |
Ulrich Drepper | 1c710c8 | 2007-05-08 00:33:25 -0700 | [diff] [blame] | 25 | struct timespec tv[2]; |
Alexey Dobriyan | 82b0547 | 2006-09-30 23:27:22 -0700 | [diff] [blame] | 26 | |
Alexey Dobriyan | 82b0547 | 2006-09-30 23:27:22 -0700 | [diff] [blame] | 27 | if (times) { |
Ulrich Drepper | 1c710c8 | 2007-05-08 00:33:25 -0700 | [diff] [blame] | 28 | if (get_user(tv[0].tv_sec, ×->actime) || |
| 29 | get_user(tv[1].tv_sec, ×->modtime)) |
| 30 | return -EFAULT; |
| 31 | tv[0].tv_nsec = 0; |
| 32 | tv[1].tv_nsec = 0; |
Alexey Dobriyan | 82b0547 | 2006-09-30 23:27:22 -0700 | [diff] [blame] | 33 | } |
Ulrich Drepper | 1c710c8 | 2007-05-08 00:33:25 -0700 | [diff] [blame] | 34 | return do_utimes(AT_FDCWD, filename, times ? tv : NULL, 0); |
Alexey Dobriyan | 82b0547 | 2006-09-30 23:27:22 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | #endif |
| 38 | |
Miklos Szeredi | 043f46f | 2007-10-16 23:27:07 -0700 | [diff] [blame] | 39 | static bool nsec_valid(long nsec) |
| 40 | { |
Michael Kerrisk | 4cca922 | 2008-06-09 21:16:08 -0700 | [diff] [blame] | 41 | if (nsec == UTIME_OMIT || nsec == UTIME_NOW) |
Miklos Szeredi | 043f46f | 2007-10-16 23:27:07 -0700 | [diff] [blame] | 42 | return true; |
| 43 | |
| 44 | return nsec >= 0 && nsec <= 999999999; |
| 45 | } |
| 46 | |
Al Viro | f0bb5aa | 2016-11-20 20:27:12 -0500 | [diff] [blame] | 47 | static int utimes_common(const struct path *path, struct timespec *times) |
Alexey Dobriyan | 82b0547 | 2006-09-30 23:27:22 -0700 | [diff] [blame] | 48 | { |
| 49 | int error; |
Alexey Dobriyan | 82b0547 | 2006-09-30 23:27:22 -0700 | [diff] [blame] | 50 | struct iattr newattrs; |
Miklos Szeredi | e9b76fe | 2008-07-01 15:01:27 +0200 | [diff] [blame] | 51 | struct inode *inode = path->dentry->d_inode; |
J. Bruce Fields | 27ac0ff | 2011-09-20 17:19:26 -0400 | [diff] [blame] | 52 | struct inode *delegated_inode = NULL; |
Alexey Dobriyan | 82b0547 | 2006-09-30 23:27:22 -0700 | [diff] [blame] | 53 | |
Miklos Szeredi | e9b76fe | 2008-07-01 15:01:27 +0200 | [diff] [blame] | 54 | error = mnt_want_write(path->mnt); |
Dave Hansen | 74f9fdf | 2008-02-15 14:37:42 -0800 | [diff] [blame] | 55 | if (error) |
Miklos Szeredi | e9b76fe | 2008-07-01 15:01:27 +0200 | [diff] [blame] | 56 | goto out; |
Alexey Dobriyan | 82b0547 | 2006-09-30 23:27:22 -0700 | [diff] [blame] | 57 | |
Michael Kerrisk | 12fd0d3 | 2008-06-09 21:16:07 -0700 | [diff] [blame] | 58 | if (times && times[0].tv_nsec == UTIME_NOW && |
| 59 | times[1].tv_nsec == UTIME_NOW) |
| 60 | times = NULL; |
| 61 | |
Alexey Dobriyan | 82b0547 | 2006-09-30 23:27:22 -0700 | [diff] [blame] | 62 | newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME; |
| 63 | if (times) { |
Ulrich Drepper | 1c710c8 | 2007-05-08 00:33:25 -0700 | [diff] [blame] | 64 | if (times[0].tv_nsec == UTIME_OMIT) |
| 65 | newattrs.ia_valid &= ~ATTR_ATIME; |
| 66 | else if (times[0].tv_nsec != UTIME_NOW) { |
| 67 | newattrs.ia_atime.tv_sec = times[0].tv_sec; |
| 68 | newattrs.ia_atime.tv_nsec = times[0].tv_nsec; |
| 69 | newattrs.ia_valid |= ATTR_ATIME_SET; |
| 70 | } |
| 71 | |
| 72 | if (times[1].tv_nsec == UTIME_OMIT) |
| 73 | newattrs.ia_valid &= ~ATTR_MTIME; |
| 74 | else if (times[1].tv_nsec != UTIME_NOW) { |
| 75 | newattrs.ia_mtime.tv_sec = times[1].tv_sec; |
| 76 | newattrs.ia_mtime.tv_nsec = times[1].tv_nsec; |
| 77 | newattrs.ia_valid |= ATTR_MTIME_SET; |
| 78 | } |
Michael Kerrisk | 4cca922 | 2008-06-09 21:16:08 -0700 | [diff] [blame] | 79 | /* |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 80 | * Tell setattr_prepare(), that this is an explicit time |
Miklos Szeredi | 9767d74 | 2008-07-01 15:01:26 +0200 | [diff] [blame] | 81 | * update, even if neither ATTR_ATIME_SET nor ATTR_MTIME_SET |
| 82 | * were used. |
Michael Kerrisk | 4cca922 | 2008-06-09 21:16:08 -0700 | [diff] [blame] | 83 | */ |
Miklos Szeredi | 9767d74 | 2008-07-01 15:01:26 +0200 | [diff] [blame] | 84 | newattrs.ia_valid |= ATTR_TIMES_SET; |
Michael Kerrisk | 4cca922 | 2008-06-09 21:16:08 -0700 | [diff] [blame] | 85 | } else { |
Miklos Szeredi | f2b20f6 | 2016-09-16 12:44:20 +0200 | [diff] [blame] | 86 | newattrs.ia_valid |= ATTR_TOUCH; |
Alexey Dobriyan | 82b0547 | 2006-09-30 23:27:22 -0700 | [diff] [blame] | 87 | } |
J. Bruce Fields | 27ac0ff | 2011-09-20 17:19:26 -0400 | [diff] [blame] | 88 | retry_deleg: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 89 | inode_lock(inode); |
J. Bruce Fields | 27ac0ff | 2011-09-20 17:19:26 -0400 | [diff] [blame] | 90 | error = notify_change(path->dentry, &newattrs, &delegated_inode); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 91 | inode_unlock(inode); |
J. Bruce Fields | 27ac0ff | 2011-09-20 17:19:26 -0400 | [diff] [blame] | 92 | if (delegated_inode) { |
| 93 | error = break_deleg_wait(&delegated_inode); |
| 94 | if (!error) |
| 95 | goto retry_deleg; |
| 96 | } |
Miklos Szeredi | e9b76fe | 2008-07-01 15:01:27 +0200 | [diff] [blame] | 97 | |
Miklos Szeredi | e9b76fe | 2008-07-01 15:01:27 +0200 | [diff] [blame] | 98 | mnt_drop_write(path->mnt); |
| 99 | out: |
| 100 | return error; |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * do_utimes - change times on filename or file descriptor |
| 105 | * @dfd: open file descriptor, -1 or AT_FDCWD |
| 106 | * @filename: path name or NULL |
| 107 | * @times: new times or NULL |
| 108 | * @flags: zero or more flags (only AT_SYMLINK_NOFOLLOW for the moment) |
| 109 | * |
| 110 | * If filename is NULL and dfd refers to an open file, then operate on |
| 111 | * the file. Otherwise look up filename, possibly using dfd as a |
| 112 | * starting point. |
| 113 | * |
| 114 | * If times==NULL, set access and modification to current time, |
| 115 | * must be owner or have write permission. |
| 116 | * Else, update from *times, must be owner or super user. |
| 117 | */ |
David Howells | c788732 | 2010-08-11 11:26:22 +0100 | [diff] [blame] | 118 | long do_utimes(int dfd, const char __user *filename, struct timespec *times, |
| 119 | int flags) |
Miklos Szeredi | e9b76fe | 2008-07-01 15:01:27 +0200 | [diff] [blame] | 120 | { |
| 121 | int error = -EINVAL; |
| 122 | |
| 123 | if (times && (!nsec_valid(times[0].tv_nsec) || |
| 124 | !nsec_valid(times[1].tv_nsec))) { |
| 125 | goto out; |
| 126 | } |
| 127 | |
| 128 | if (flags & ~AT_SYMLINK_NOFOLLOW) |
| 129 | goto out; |
| 130 | |
| 131 | if (filename == NULL && dfd != AT_FDCWD) { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 132 | struct fd f; |
Miklos Szeredi | e9b76fe | 2008-07-01 15:01:27 +0200 | [diff] [blame] | 133 | |
| 134 | if (flags & AT_SYMLINK_NOFOLLOW) |
| 135 | goto out; |
| 136 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 137 | f = fdget(dfd); |
Miklos Szeredi | e9b76fe | 2008-07-01 15:01:27 +0200 | [diff] [blame] | 138 | error = -EBADF; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 139 | if (!f.file) |
Miklos Szeredi | e9b76fe | 2008-07-01 15:01:27 +0200 | [diff] [blame] | 140 | goto out; |
| 141 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 142 | error = utimes_common(&f.file->f_path, times); |
| 143 | fdput(f); |
Miklos Szeredi | e9b76fe | 2008-07-01 15:01:27 +0200 | [diff] [blame] | 144 | } else { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 145 | struct path path; |
Miklos Szeredi | e9b76fe | 2008-07-01 15:01:27 +0200 | [diff] [blame] | 146 | int lookup_flags = 0; |
| 147 | |
| 148 | if (!(flags & AT_SYMLINK_NOFOLLOW)) |
| 149 | lookup_flags |= LOOKUP_FOLLOW; |
Jeff Layton | a69201d | 2012-12-11 12:10:14 -0500 | [diff] [blame] | 150 | retry: |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 151 | error = user_path_at(dfd, filename, lookup_flags, &path); |
Miklos Szeredi | e9b76fe | 2008-07-01 15:01:27 +0200 | [diff] [blame] | 152 | if (error) |
| 153 | goto out; |
| 154 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 155 | error = utimes_common(&path, times); |
| 156 | path_put(&path); |
Jeff Layton | a69201d | 2012-12-11 12:10:14 -0500 | [diff] [blame] | 157 | if (retry_estale(error, lookup_flags)) { |
| 158 | lookup_flags |= LOOKUP_REVAL; |
| 159 | goto retry; |
| 160 | } |
Miklos Szeredi | e9b76fe | 2008-07-01 15:01:27 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Alexey Dobriyan | 82b0547 | 2006-09-30 23:27:22 -0700 | [diff] [blame] | 163 | out: |
| 164 | return error; |
| 165 | } |
| 166 | |
David Howells | c788732 | 2010-08-11 11:26:22 +0100 | [diff] [blame] | 167 | SYSCALL_DEFINE4(utimensat, int, dfd, const char __user *, filename, |
Heiko Carstens | 6559eed8 | 2009-01-14 14:14:32 +0100 | [diff] [blame] | 168 | struct timespec __user *, utimes, int, flags) |
Ulrich Drepper | 1c710c8 | 2007-05-08 00:33:25 -0700 | [diff] [blame] | 169 | { |
| 170 | struct timespec tstimes[2]; |
| 171 | |
| 172 | if (utimes) { |
| 173 | if (copy_from_user(&tstimes, utimes, sizeof(tstimes))) |
| 174 | return -EFAULT; |
Ulrich Drepper | 1c710c8 | 2007-05-08 00:33:25 -0700 | [diff] [blame] | 175 | |
| 176 | /* Nothing to do, we must not even check the path. */ |
| 177 | if (tstimes[0].tv_nsec == UTIME_OMIT && |
| 178 | tstimes[1].tv_nsec == UTIME_OMIT) |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | return do_utimes(dfd, filename, utimes ? tstimes : NULL, flags); |
| 183 | } |
| 184 | |
David Howells | c788732 | 2010-08-11 11:26:22 +0100 | [diff] [blame] | 185 | SYSCALL_DEFINE3(futimesat, int, dfd, const char __user *, filename, |
Heiko Carstens | 6559eed8 | 2009-01-14 14:14:32 +0100 | [diff] [blame] | 186 | struct timeval __user *, utimes) |
Alexey Dobriyan | 82b0547 | 2006-09-30 23:27:22 -0700 | [diff] [blame] | 187 | { |
| 188 | struct timeval times[2]; |
Ulrich Drepper | 1c710c8 | 2007-05-08 00:33:25 -0700 | [diff] [blame] | 189 | struct timespec tstimes[2]; |
Alexey Dobriyan | 82b0547 | 2006-09-30 23:27:22 -0700 | [diff] [blame] | 190 | |
Ulrich Drepper | 1c710c8 | 2007-05-08 00:33:25 -0700 | [diff] [blame] | 191 | if (utimes) { |
| 192 | if (copy_from_user(×, utimes, sizeof(times))) |
| 193 | return -EFAULT; |
| 194 | |
| 195 | /* This test is needed to catch all invalid values. If we |
| 196 | would test only in do_utimes we would miss those invalid |
| 197 | values truncated by the multiplication with 1000. Note |
| 198 | that we also catch UTIME_{NOW,OMIT} here which are only |
| 199 | valid for utimensat. */ |
| 200 | if (times[0].tv_usec >= 1000000 || times[0].tv_usec < 0 || |
| 201 | times[1].tv_usec >= 1000000 || times[1].tv_usec < 0) |
| 202 | return -EINVAL; |
| 203 | |
| 204 | tstimes[0].tv_sec = times[0].tv_sec; |
| 205 | tstimes[0].tv_nsec = 1000 * times[0].tv_usec; |
| 206 | tstimes[1].tv_sec = times[1].tv_sec; |
| 207 | tstimes[1].tv_nsec = 1000 * times[1].tv_usec; |
| 208 | } |
| 209 | |
| 210 | return do_utimes(dfd, filename, utimes ? tstimes : NULL, 0); |
Alexey Dobriyan | 82b0547 | 2006-09-30 23:27:22 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Heiko Carstens | 003d7ab | 2009-01-14 14:14:21 +0100 | [diff] [blame] | 213 | SYSCALL_DEFINE2(utimes, char __user *, filename, |
| 214 | struct timeval __user *, utimes) |
Alexey Dobriyan | 82b0547 | 2006-09-30 23:27:22 -0700 | [diff] [blame] | 215 | { |
| 216 | return sys_futimesat(AT_FDCWD, filename, utimes); |
| 217 | } |
Al Viro | 1a060ba | 2017-04-08 18:04:59 -0400 | [diff] [blame] | 218 | |
| 219 | #ifdef CONFIG_COMPAT |
| 220 | /* |
| 221 | * Not all architectures have sys_utime, so implement this in terms |
| 222 | * of sys_utimes. |
| 223 | */ |
| 224 | COMPAT_SYSCALL_DEFINE2(utime, const char __user *, filename, |
| 225 | struct compat_utimbuf __user *, t) |
| 226 | { |
| 227 | struct timespec tv[2]; |
| 228 | |
| 229 | if (t) { |
| 230 | if (get_user(tv[0].tv_sec, &t->actime) || |
| 231 | get_user(tv[1].tv_sec, &t->modtime)) |
| 232 | return -EFAULT; |
| 233 | tv[0].tv_nsec = 0; |
| 234 | tv[1].tv_nsec = 0; |
| 235 | } |
| 236 | return do_utimes(AT_FDCWD, filename, t ? tv : NULL, 0); |
| 237 | } |
| 238 | |
| 239 | COMPAT_SYSCALL_DEFINE4(utimensat, unsigned int, dfd, const char __user *, filename, struct compat_timespec __user *, t, int, flags) |
| 240 | { |
| 241 | struct timespec tv[2]; |
| 242 | |
| 243 | if (t) { |
| 244 | if (compat_get_timespec(&tv[0], &t[0]) || |
| 245 | compat_get_timespec(&tv[1], &t[1])) |
| 246 | return -EFAULT; |
| 247 | |
| 248 | if (tv[0].tv_nsec == UTIME_OMIT && tv[1].tv_nsec == UTIME_OMIT) |
| 249 | return 0; |
| 250 | } |
| 251 | return do_utimes(dfd, filename, t ? tv : NULL, flags); |
| 252 | } |
| 253 | |
| 254 | COMPAT_SYSCALL_DEFINE3(futimesat, unsigned int, dfd, const char __user *, filename, struct compat_timeval __user *, t) |
| 255 | { |
| 256 | struct timespec tv[2]; |
| 257 | |
| 258 | if (t) { |
| 259 | if (get_user(tv[0].tv_sec, &t[0].tv_sec) || |
| 260 | get_user(tv[0].tv_nsec, &t[0].tv_usec) || |
| 261 | get_user(tv[1].tv_sec, &t[1].tv_sec) || |
| 262 | get_user(tv[1].tv_nsec, &t[1].tv_usec)) |
| 263 | return -EFAULT; |
| 264 | if (tv[0].tv_nsec >= 1000000 || tv[0].tv_nsec < 0 || |
| 265 | tv[1].tv_nsec >= 1000000 || tv[1].tv_nsec < 0) |
| 266 | return -EINVAL; |
| 267 | tv[0].tv_nsec *= 1000; |
| 268 | tv[1].tv_nsec *= 1000; |
| 269 | } |
| 270 | return do_utimes(dfd, filename, t ? tv : NULL, 0); |
| 271 | } |
| 272 | |
| 273 | COMPAT_SYSCALL_DEFINE2(utimes, const char __user *, filename, struct compat_timeval __user *, t) |
| 274 | { |
| 275 | return compat_sys_futimesat(AT_FDCWD, filename, t); |
| 276 | } |
| 277 | #endif |