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