xref: /linux/fs/utimes.c (revision 7fc2cd2e4b398c57c9cf961cfea05eadbf34c05c)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/file.h>
3 #include <linux/mount.h>
4 #include <linux/namei.h>
5 #include <linux/utime.h>
6 #include <linux/syscalls.h>
7 #include <linux/uaccess.h>
8 #include <linux/compat.h>
9 #include <asm/unistd.h>
10 #include <linux/filelock.h>
11 
12 static bool nsec_valid(long nsec)
13 {
14 	if (nsec == UTIME_OMIT || nsec == UTIME_NOW)
15 		return true;
16 
17 	return nsec >= 0 && nsec <= 999999999;
18 }
19 
20 int vfs_utimes(const struct path *path, struct timespec64 *times)
21 {
22 	int error;
23 	struct iattr newattrs;
24 	struct inode *inode = path->dentry->d_inode;
25 	struct delegated_inode delegated_inode = { };
26 
27 	if (times) {
28 		if (!nsec_valid(times[0].tv_nsec) ||
29 		    !nsec_valid(times[1].tv_nsec))
30 			return -EINVAL;
31 		if (times[0].tv_nsec == UTIME_NOW &&
32 		    times[1].tv_nsec == UTIME_NOW)
33 			times = NULL;
34 	}
35 
36 	error = mnt_want_write(path->mnt);
37 	if (error)
38 		goto out;
39 
40 	newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
41 	if (times) {
42 		if (times[0].tv_nsec == UTIME_OMIT)
43 			newattrs.ia_valid &= ~ATTR_ATIME;
44 		else if (times[0].tv_nsec != UTIME_NOW) {
45 			newattrs.ia_atime = times[0];
46 			newattrs.ia_valid |= ATTR_ATIME_SET;
47 		}
48 
49 		if (times[1].tv_nsec == UTIME_OMIT)
50 			newattrs.ia_valid &= ~ATTR_MTIME;
51 		else if (times[1].tv_nsec != UTIME_NOW) {
52 			newattrs.ia_mtime = times[1];
53 			newattrs.ia_valid |= ATTR_MTIME_SET;
54 		}
55 		/*
56 		 * Tell setattr_prepare(), that this is an explicit time
57 		 * update, even if neither ATTR_ATIME_SET nor ATTR_MTIME_SET
58 		 * were used.
59 		 */
60 		newattrs.ia_valid |= ATTR_TIMES_SET;
61 	} else {
62 		newattrs.ia_valid |= ATTR_TOUCH;
63 	}
64 retry_deleg:
65 	inode_lock(inode);
66 	error = notify_change(mnt_idmap(path->mnt), path->dentry, &newattrs,
67 			      &delegated_inode);
68 	inode_unlock(inode);
69 	if (is_delegated(&delegated_inode)) {
70 		error = break_deleg_wait(&delegated_inode);
71 		if (!error)
72 			goto retry_deleg;
73 	}
74 
75 	mnt_drop_write(path->mnt);
76 out:
77 	return error;
78 }
79 EXPORT_SYMBOL_GPL(vfs_utimes);
80 
81 static int do_utimes_path(int dfd, const char __user *filename,
82 		struct timespec64 *times, int flags)
83 {
84 	struct path path;
85 	int lookup_flags = 0, error;
86 
87 	if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH))
88 		return -EINVAL;
89 
90 	if (!(flags & AT_SYMLINK_NOFOLLOW))
91 		lookup_flags |= LOOKUP_FOLLOW;
92 	if (flags & AT_EMPTY_PATH)
93 		lookup_flags |= LOOKUP_EMPTY;
94 
95 retry:
96 	error = user_path_at(dfd, filename, lookup_flags, &path);
97 	if (error)
98 		return error;
99 
100 	error = vfs_utimes(&path, times);
101 	path_put(&path);
102 	if (retry_estale(error, lookup_flags)) {
103 		lookup_flags |= LOOKUP_REVAL;
104 		goto retry;
105 	}
106 
107 	return error;
108 }
109 
110 static int do_utimes_fd(int fd, struct timespec64 *times, int flags)
111 {
112 	if (flags)
113 		return -EINVAL;
114 
115 	CLASS(fd, f)(fd);
116 	if (fd_empty(f))
117 		return -EBADF;
118 	return vfs_utimes(&fd_file(f)->f_path, times);
119 }
120 
121 /*
122  * do_utimes - change times on filename or file descriptor
123  * @dfd: open file descriptor, -1 or AT_FDCWD
124  * @filename: path name or NULL
125  * @times: new times or NULL
126  * @flags: zero or more flags (only AT_SYMLINK_NOFOLLOW for the moment)
127  *
128  * If filename is NULL and dfd refers to an open file, then operate on
129  * the file.  Otherwise look up filename, possibly using dfd as a
130  * starting point.
131  *
132  * If times==NULL, set access and modification to current time,
133  * must be owner or have write permission.
134  * Else, update from *times, must be owner or super user.
135  */
136 long do_utimes(int dfd, const char __user *filename, struct timespec64 *times,
137 	       int flags)
138 {
139 	if (filename == NULL && dfd != AT_FDCWD)
140 		return do_utimes_fd(dfd, times, flags);
141 	return do_utimes_path(dfd, filename, times, flags);
142 }
143 
144 SYSCALL_DEFINE4(utimensat, int, dfd, const char __user *, filename,
145 		struct __kernel_timespec __user *, utimes, int, flags)
146 {
147 	struct timespec64 tstimes[2];
148 
149 	if (utimes) {
150 		if ((get_timespec64(&tstimes[0], &utimes[0]) ||
151 			get_timespec64(&tstimes[1], &utimes[1])))
152 			return -EFAULT;
153 
154 		/* Nothing to do, we must not even check the path.  */
155 		if (tstimes[0].tv_nsec == UTIME_OMIT &&
156 		    tstimes[1].tv_nsec == UTIME_OMIT)
157 			return 0;
158 	}
159 
160 	return do_utimes(dfd, filename, utimes ? tstimes : NULL, flags);
161 }
162 
163 #ifdef __ARCH_WANT_SYS_UTIME
164 /*
165  * futimesat(), utimes() and utime() are older versions of utimensat()
166  * that are provided for compatibility with traditional C libraries.
167  * On modern architectures, we always use libc wrappers around
168  * utimensat() instead.
169  */
170 static long do_futimesat(int dfd, const char __user *filename,
171 			 struct __kernel_old_timeval __user *utimes)
172 {
173 	struct __kernel_old_timeval times[2];
174 	struct timespec64 tstimes[2];
175 
176 	if (utimes) {
177 		if (copy_from_user(&times, utimes, sizeof(times)))
178 			return -EFAULT;
179 
180 		/* This test is needed to catch all invalid values.  If we
181 		   would test only in do_utimes we would miss those invalid
182 		   values truncated by the multiplication with 1000.  Note
183 		   that we also catch UTIME_{NOW,OMIT} here which are only
184 		   valid for utimensat.  */
185 		if (times[0].tv_usec >= 1000000 || times[0].tv_usec < 0 ||
186 		    times[1].tv_usec >= 1000000 || times[1].tv_usec < 0)
187 			return -EINVAL;
188 
189 		tstimes[0].tv_sec = times[0].tv_sec;
190 		tstimes[0].tv_nsec = 1000 * times[0].tv_usec;
191 		tstimes[1].tv_sec = times[1].tv_sec;
192 		tstimes[1].tv_nsec = 1000 * times[1].tv_usec;
193 	}
194 
195 	return do_utimes(dfd, filename, utimes ? tstimes : NULL, 0);
196 }
197 
198 
199 SYSCALL_DEFINE3(futimesat, int, dfd, const char __user *, filename,
200 		struct __kernel_old_timeval __user *, utimes)
201 {
202 	return do_futimesat(dfd, filename, utimes);
203 }
204 
205 SYSCALL_DEFINE2(utimes, char __user *, filename,
206 		struct __kernel_old_timeval __user *, utimes)
207 {
208 	return do_futimesat(AT_FDCWD, filename, utimes);
209 }
210 
211 SYSCALL_DEFINE2(utime, char __user *, filename, struct utimbuf __user *, times)
212 {
213 	struct timespec64 tv[2];
214 
215 	if (times) {
216 		if (get_user(tv[0].tv_sec, &times->actime) ||
217 		    get_user(tv[1].tv_sec, &times->modtime))
218 			return -EFAULT;
219 		tv[0].tv_nsec = 0;
220 		tv[1].tv_nsec = 0;
221 	}
222 	return do_utimes(AT_FDCWD, filename, times ? tv : NULL, 0);
223 }
224 #endif
225 
226 #ifdef CONFIG_COMPAT_32BIT_TIME
227 /*
228  * Not all architectures have sys_utime, so implement this in terms
229  * of sys_utimes.
230  */
231 #ifdef __ARCH_WANT_SYS_UTIME32
232 SYSCALL_DEFINE2(utime32, const char __user *, filename,
233 		struct old_utimbuf32 __user *, t)
234 {
235 	struct timespec64 tv[2];
236 
237 	if (t) {
238 		if (get_user(tv[0].tv_sec, &t->actime) ||
239 		    get_user(tv[1].tv_sec, &t->modtime))
240 			return -EFAULT;
241 		tv[0].tv_nsec = 0;
242 		tv[1].tv_nsec = 0;
243 	}
244 	return do_utimes(AT_FDCWD, filename, t ? tv : NULL, 0);
245 }
246 #endif
247 
248 SYSCALL_DEFINE4(utimensat_time32, unsigned int, dfd, const char __user *, filename, struct old_timespec32 __user *, t, int, flags)
249 {
250 	struct timespec64 tv[2];
251 
252 	if  (t) {
253 		if (get_old_timespec32(&tv[0], &t[0]) ||
254 		    get_old_timespec32(&tv[1], &t[1]))
255 			return -EFAULT;
256 
257 		if (tv[0].tv_nsec == UTIME_OMIT && tv[1].tv_nsec == UTIME_OMIT)
258 			return 0;
259 	}
260 	return do_utimes(dfd, filename, t ? tv : NULL, flags);
261 }
262 
263 #ifdef __ARCH_WANT_SYS_UTIME32
264 static long do_compat_futimesat(unsigned int dfd, const char __user *filename,
265 				struct old_timeval32 __user *t)
266 {
267 	struct timespec64 tv[2];
268 
269 	if (t) {
270 		if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
271 		    get_user(tv[0].tv_nsec, &t[0].tv_usec) ||
272 		    get_user(tv[1].tv_sec, &t[1].tv_sec) ||
273 		    get_user(tv[1].tv_nsec, &t[1].tv_usec))
274 			return -EFAULT;
275 		if (tv[0].tv_nsec >= 1000000 || tv[0].tv_nsec < 0 ||
276 		    tv[1].tv_nsec >= 1000000 || tv[1].tv_nsec < 0)
277 			return -EINVAL;
278 		tv[0].tv_nsec *= 1000;
279 		tv[1].tv_nsec *= 1000;
280 	}
281 	return do_utimes(dfd, filename, t ? tv : NULL, 0);
282 }
283 
284 SYSCALL_DEFINE3(futimesat_time32, unsigned int, dfd,
285 		       const char __user *, filename,
286 		       struct old_timeval32 __user *, t)
287 {
288 	return do_compat_futimesat(dfd, filename, t);
289 }
290 
291 SYSCALL_DEFINE2(utimes_time32, const char __user *, filename, struct old_timeval32 __user *, t)
292 {
293 	return do_compat_futimesat(AT_FDCWD, filename, t);
294 }
295 #endif
296 #endif
297