blob: e4b3d7c2c9f55182049b475117cf3fe2c06e76d6 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ulrich Drepper1c710c82007-05-08 00:33:25 -07002#include <linux/file.h>
Dave Hansen74f9fdf2008-02-15 14:37:42 -08003#include <linux/mount.h>
Alexey Dobriyan82b05472006-09-30 23:27:22 -07004#include <linux/namei.h>
Alexey Dobriyan82b05472006-09-30 23:27:22 -07005#include <linux/utime.h>
Adrian Bunk12c2ab52008-02-06 01:36:47 -08006#include <linux/syscalls.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -08007#include <linux/uaccess.h>
Al Viro1a060ba2017-04-08 18:04:59 -04008#include <linux/compat.h>
Alexey Dobriyan82b05472006-09-30 23:27:22 -07009#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 Carstens003d7ab2009-01-14 14:14:21 +010024SYSCALL_DEFINE2(utime, char __user *, filename, struct utimbuf __user *, times)
Alexey Dobriyan82b05472006-09-30 23:27:22 -070025{
Deepa Dinamaniaaed2dd2017-08-02 19:51:15 -070026 struct timespec64 tv[2];
Alexey Dobriyan82b05472006-09-30 23:27:22 -070027
Alexey Dobriyan82b05472006-09-30 23:27:22 -070028 if (times) {
Ulrich Drepper1c710c82007-05-08 00:33:25 -070029 if (get_user(tv[0].tv_sec, &times->actime) ||
30 get_user(tv[1].tv_sec, &times->modtime))
31 return -EFAULT;
32 tv[0].tv_nsec = 0;
33 tv[1].tv_nsec = 0;
Alexey Dobriyan82b05472006-09-30 23:27:22 -070034 }
Ulrich Drepper1c710c82007-05-08 00:33:25 -070035 return do_utimes(AT_FDCWD, filename, times ? tv : NULL, 0);
Alexey Dobriyan82b05472006-09-30 23:27:22 -070036}
37
38#endif
39
Miklos Szeredi043f46f2007-10-16 23:27:07 -070040static bool nsec_valid(long nsec)
41{
Michael Kerrisk4cca9222008-06-09 21:16:08 -070042 if (nsec == UTIME_OMIT || nsec == UTIME_NOW)
Miklos Szeredi043f46f2007-10-16 23:27:07 -070043 return true;
44
45 return nsec >= 0 && nsec <= 999999999;
46}
47
Deepa Dinamaniaaed2dd2017-08-02 19:51:15 -070048static int utimes_common(const struct path *path, struct timespec64 *times)
Alexey Dobriyan82b05472006-09-30 23:27:22 -070049{
50 int error;
Alexey Dobriyan82b05472006-09-30 23:27:22 -070051 struct iattr newattrs;
Miklos Szeredie9b76fe2008-07-01 15:01:27 +020052 struct inode *inode = path->dentry->d_inode;
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -040053 struct inode *delegated_inode = NULL;
Alexey Dobriyan82b05472006-09-30 23:27:22 -070054
Miklos Szeredie9b76fe2008-07-01 15:01:27 +020055 error = mnt_want_write(path->mnt);
Dave Hansen74f9fdf2008-02-15 14:37:42 -080056 if (error)
Miklos Szeredie9b76fe2008-07-01 15:01:27 +020057 goto out;
Alexey Dobriyan82b05472006-09-30 23:27:22 -070058
Michael Kerrisk12fd0d32008-06-09 21:16:07 -070059 if (times && times[0].tv_nsec == UTIME_NOW &&
60 times[1].tv_nsec == UTIME_NOW)
61 times = NULL;
62
Alexey Dobriyan82b05472006-09-30 23:27:22 -070063 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
64 if (times) {
Ulrich Drepper1c710c82007-05-08 00:33:25 -070065 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 Kerrisk4cca9222008-06-09 21:16:08 -070080 /*
Jan Kara31051c82016-05-26 16:55:18 +020081 * Tell setattr_prepare(), that this is an explicit time
Miklos Szeredi9767d742008-07-01 15:01:26 +020082 * update, even if neither ATTR_ATIME_SET nor ATTR_MTIME_SET
83 * were used.
Michael Kerrisk4cca9222008-06-09 21:16:08 -070084 */
Miklos Szeredi9767d742008-07-01 15:01:26 +020085 newattrs.ia_valid |= ATTR_TIMES_SET;
Michael Kerrisk4cca9222008-06-09 21:16:08 -070086 } else {
Miklos Szeredif2b20f62016-09-16 12:44:20 +020087 newattrs.ia_valid |= ATTR_TOUCH;
Alexey Dobriyan82b05472006-09-30 23:27:22 -070088 }
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -040089retry_deleg:
Al Viro59551022016-01-22 15:40:57 -050090 inode_lock(inode);
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -040091 error = notify_change(path->dentry, &newattrs, &delegated_inode);
Al Viro59551022016-01-22 15:40:57 -050092 inode_unlock(inode);
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -040093 if (delegated_inode) {
94 error = break_deleg_wait(&delegated_inode);
95 if (!error)
96 goto retry_deleg;
97 }
Miklos Szeredie9b76fe2008-07-01 15:01:27 +020098
Miklos Szeredie9b76fe2008-07-01 15:01:27 +020099 mnt_drop_write(path->mnt);
100out:
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 Dinamaniaaed2dd2017-08-02 19:51:15 -0700119long do_utimes(int dfd, const char __user *filename, struct timespec64 *times,
David Howellsc7887322010-08-11 11:26:22 +0100120 int flags)
Miklos Szeredie9b76fe2008-07-01 15:01:27 +0200121{
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 Viro2903ff02012-08-28 12:52:22 -0400133 struct fd f;
Miklos Szeredie9b76fe2008-07-01 15:01:27 +0200134
135 if (flags & AT_SYMLINK_NOFOLLOW)
136 goto out;
137
Al Viro2903ff02012-08-28 12:52:22 -0400138 f = fdget(dfd);
Miklos Szeredie9b76fe2008-07-01 15:01:27 +0200139 error = -EBADF;
Al Viro2903ff02012-08-28 12:52:22 -0400140 if (!f.file)
Miklos Szeredie9b76fe2008-07-01 15:01:27 +0200141 goto out;
142
Al Viro2903ff02012-08-28 12:52:22 -0400143 error = utimes_common(&f.file->f_path, times);
144 fdput(f);
Miklos Szeredie9b76fe2008-07-01 15:01:27 +0200145 } else {
Al Viro2d8f3032008-07-22 09:59:21 -0400146 struct path path;
Miklos Szeredie9b76fe2008-07-01 15:01:27 +0200147 int lookup_flags = 0;
148
149 if (!(flags & AT_SYMLINK_NOFOLLOW))
150 lookup_flags |= LOOKUP_FOLLOW;
Jeff Laytona69201d2012-12-11 12:10:14 -0500151retry:
Al Viro2d8f3032008-07-22 09:59:21 -0400152 error = user_path_at(dfd, filename, lookup_flags, &path);
Miklos Szeredie9b76fe2008-07-01 15:01:27 +0200153 if (error)
154 goto out;
155
Al Viro2d8f3032008-07-22 09:59:21 -0400156 error = utimes_common(&path, times);
157 path_put(&path);
Jeff Laytona69201d2012-12-11 12:10:14 -0500158 if (retry_estale(error, lookup_flags)) {
159 lookup_flags |= LOOKUP_REVAL;
160 goto retry;
161 }
Miklos Szeredie9b76fe2008-07-01 15:01:27 +0200162 }
163
Alexey Dobriyan82b05472006-09-30 23:27:22 -0700164out:
165 return error;
166}
167
David Howellsc7887322010-08-11 11:26:22 +0100168SYSCALL_DEFINE4(utimensat, int, dfd, const char __user *, filename,
Heiko Carstens6559eed82009-01-14 14:14:32 +0100169 struct timespec __user *, utimes, int, flags)
Ulrich Drepper1c710c82007-05-08 00:33:25 -0700170{
Deepa Dinamaniaaed2dd2017-08-02 19:51:15 -0700171 struct timespec64 tstimes[2];
Ulrich Drepper1c710c82007-05-08 00:33:25 -0700172
173 if (utimes) {
Deepa Dinamaniaaed2dd2017-08-02 19:51:15 -0700174 if ((get_timespec64(&tstimes[0], &utimes[0]) ||
175 get_timespec64(&tstimes[1], &utimes[1])))
Ulrich Drepper1c710c82007-05-08 00:33:25 -0700176 return -EFAULT;
Ulrich Drepper1c710c82007-05-08 00:33:25 -0700177
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 Howellsc7887322010-08-11 11:26:22 +0100187SYSCALL_DEFINE3(futimesat, int, dfd, const char __user *, filename,
Heiko Carstens6559eed82009-01-14 14:14:32 +0100188 struct timeval __user *, utimes)
Alexey Dobriyan82b05472006-09-30 23:27:22 -0700189{
190 struct timeval times[2];
Deepa Dinamaniaaed2dd2017-08-02 19:51:15 -0700191 struct timespec64 tstimes[2];
Alexey Dobriyan82b05472006-09-30 23:27:22 -0700192
Ulrich Drepper1c710c82007-05-08 00:33:25 -0700193 if (utimes) {
194 if (copy_from_user(&times, 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 Dobriyan82b05472006-09-30 23:27:22 -0700213}
214
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100215SYSCALL_DEFINE2(utimes, char __user *, filename,
216 struct timeval __user *, utimes)
Alexey Dobriyan82b05472006-09-30 23:27:22 -0700217{
218 return sys_futimesat(AT_FDCWD, filename, utimes);
219}
Al Viro1a060ba2017-04-08 18:04:59 -0400220
221#ifdef CONFIG_COMPAT
222/*
223 * Not all architectures have sys_utime, so implement this in terms
224 * of sys_utimes.
225 */
226COMPAT_SYSCALL_DEFINE2(utime, const char __user *, filename,
227 struct compat_utimbuf __user *, t)
228{
Deepa Dinamaniaaed2dd2017-08-02 19:51:15 -0700229 struct timespec64 tv[2];
Al Viro1a060ba2017-04-08 18:04:59 -0400230
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
241COMPAT_SYSCALL_DEFINE4(utimensat, unsigned int, dfd, const char __user *, filename, struct compat_timespec __user *, t, int, flags)
242{
Deepa Dinamaniaaed2dd2017-08-02 19:51:15 -0700243 struct timespec64 tv[2];
Al Viro1a060ba2017-04-08 18:04:59 -0400244
245 if (t) {
Deepa Dinamaniaaed2dd2017-08-02 19:51:15 -0700246 if (compat_get_timespec64(&tv[0], &t[0]) ||
247 compat_get_timespec64(&tv[1], &t[1]))
Al Viro1a060ba2017-04-08 18:04:59 -0400248 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
256COMPAT_SYSCALL_DEFINE3(futimesat, unsigned int, dfd, const char __user *, filename, struct compat_timeval __user *, t)
257{
Deepa Dinamaniaaed2dd2017-08-02 19:51:15 -0700258 struct timespec64 tv[2];
Al Viro1a060ba2017-04-08 18:04:59 -0400259
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
275COMPAT_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