xref: /openbmc/linux/fs/utimes.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21c710c89SUlrich Drepper #include <linux/file.h>
374f9fdfaSDave Hansen #include <linux/mount.h>
482b0547cSAlexey Dobriyan #include <linux/namei.h>
582b0547cSAlexey Dobriyan #include <linux/utime.h>
612c2ab5eSAdrian Bunk #include <linux/syscalls.h>
77c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
81a060ba3SAl Viro #include <linux/compat.h>
982b0547cSAlexey Dobriyan #include <asm/unistd.h>
105970e15dSJeff Layton #include <linux/filelock.h>
1182b0547cSAlexey Dobriyan 
nsec_valid(long nsec)12043f46f6SMiklos Szeredi static bool nsec_valid(long nsec)
13043f46f6SMiklos Szeredi {
144cca9226SMichael Kerrisk 	if (nsec == UTIME_OMIT || nsec == UTIME_NOW)
15043f46f6SMiklos Szeredi 		return true;
16043f46f6SMiklos Szeredi 
17043f46f6SMiklos Szeredi 	return nsec >= 0 && nsec <= 999999999;
18043f46f6SMiklos Szeredi }
19043f46f6SMiklos Szeredi 
vfs_utimes(const struct path * path,struct timespec64 * times)20fd5ad30cSChristoph Hellwig int vfs_utimes(const struct path *path, struct timespec64 *times)
2182b0547cSAlexey Dobriyan {
2282b0547cSAlexey Dobriyan 	int error;
2382b0547cSAlexey Dobriyan 	struct iattr newattrs;
24e9b76fedSMiklos Szeredi 	struct inode *inode = path->dentry->d_inode;
2527ac0ffeSJ. Bruce Fields 	struct inode *delegated_inode = NULL;
2682b0547cSAlexey Dobriyan 
2727eb11c9SChristoph Hellwig 	if (times) {
2827eb11c9SChristoph Hellwig 		if (!nsec_valid(times[0].tv_nsec) ||
2927eb11c9SChristoph Hellwig 		    !nsec_valid(times[1].tv_nsec))
3027eb11c9SChristoph Hellwig 			return -EINVAL;
3127eb11c9SChristoph Hellwig 		if (times[0].tv_nsec == UTIME_NOW &&
3227eb11c9SChristoph Hellwig 		    times[1].tv_nsec == UTIME_NOW)
3327eb11c9SChristoph Hellwig 			times = NULL;
3427eb11c9SChristoph Hellwig 	}
3527eb11c9SChristoph Hellwig 
36e9b76fedSMiklos Szeredi 	error = mnt_want_write(path->mnt);
3782b0547cSAlexey Dobriyan 	if (error)
3882b0547cSAlexey Dobriyan 		goto out;
391c710c89SUlrich Drepper 
4082b0547cSAlexey Dobriyan 	newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
4182b0547cSAlexey Dobriyan 	if (times) {
421c710c89SUlrich Drepper 		if (times[0].tv_nsec == UTIME_OMIT)
431c710c89SUlrich Drepper 			newattrs.ia_valid &= ~ATTR_ATIME;
441c710c89SUlrich Drepper 		else if (times[0].tv_nsec != UTIME_NOW) {
45eb31e2f6SAmir Goldstein 			newattrs.ia_atime = times[0];
461c710c89SUlrich Drepper 			newattrs.ia_valid |= ATTR_ATIME_SET;
471c710c89SUlrich Drepper 		}
481c710c89SUlrich Drepper 
491c710c89SUlrich Drepper 		if (times[1].tv_nsec == UTIME_OMIT)
501c710c89SUlrich Drepper 			newattrs.ia_valid &= ~ATTR_MTIME;
511c710c89SUlrich Drepper 		else if (times[1].tv_nsec != UTIME_NOW) {
52eb31e2f6SAmir Goldstein 			newattrs.ia_mtime = times[1];
531c710c89SUlrich Drepper 			newattrs.ia_valid |= ATTR_MTIME_SET;
541c710c89SUlrich Drepper 		}
5502c6be61SMiklos Szeredi 		/*
5631051c85SJan Kara 		 * Tell setattr_prepare(), that this is an explicit time
579767d749SMiklos Szeredi 		 * update, even if neither ATTR_ATIME_SET nor ATTR_MTIME_SET
589767d749SMiklos Szeredi 		 * were used.
594cca9226SMichael Kerrisk 		 */
609767d749SMiklos Szeredi 		newattrs.ia_valid |= ATTR_TIMES_SET;
614cca9226SMichael Kerrisk 	} else {
62f2b20f6eSMiklos Szeredi 		newattrs.ia_valid |= ATTR_TOUCH;
631e5de283SLinus Torvalds 	}
6427ac0ffeSJ. Bruce Fields retry_deleg:
655955102cSAl Viro 	inode_lock(inode);
66*abf08576SChristian Brauner 	error = notify_change(mnt_idmap(path->mnt), path->dentry, &newattrs,
672f221d6fSChristian Brauner 			      &delegated_inode);
685955102cSAl Viro 	inode_unlock(inode);
6927ac0ffeSJ. Bruce Fields 	if (delegated_inode) {
7027ac0ffeSJ. Bruce Fields 		error = break_deleg_wait(&delegated_inode);
7127ac0ffeSJ. Bruce Fields 		if (!error)
7227ac0ffeSJ. Bruce Fields 			goto retry_deleg;
7327ac0ffeSJ. Bruce Fields 	}
74e9b76fedSMiklos Szeredi 
75e9b76fedSMiklos Szeredi 	mnt_drop_write(path->mnt);
76e9b76fedSMiklos Szeredi out:
77e9b76fedSMiklos Szeredi 	return error;
78e9b76fedSMiklos Szeredi }
79e9b76fedSMiklos Szeredi 
do_utimes_path(int dfd,const char __user * filename,struct timespec64 * times,int flags)809d4b74aeSChristoph Hellwig static int do_utimes_path(int dfd, const char __user *filename,
819d4b74aeSChristoph Hellwig 		struct timespec64 *times, int flags)
829d4b74aeSChristoph Hellwig {
839d4b74aeSChristoph Hellwig 	struct path path;
849d4b74aeSChristoph Hellwig 	int lookup_flags = 0, error;
859d4b74aeSChristoph Hellwig 
869d4b74aeSChristoph Hellwig 	if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH))
879d4b74aeSChristoph Hellwig 		return -EINVAL;
889d4b74aeSChristoph Hellwig 
899d4b74aeSChristoph Hellwig 	if (!(flags & AT_SYMLINK_NOFOLLOW))
909d4b74aeSChristoph Hellwig 		lookup_flags |= LOOKUP_FOLLOW;
919d4b74aeSChristoph Hellwig 	if (flags & AT_EMPTY_PATH)
929d4b74aeSChristoph Hellwig 		lookup_flags |= LOOKUP_EMPTY;
939d4b74aeSChristoph Hellwig 
949d4b74aeSChristoph Hellwig retry:
959d4b74aeSChristoph Hellwig 	error = user_path_at(dfd, filename, lookup_flags, &path);
969d4b74aeSChristoph Hellwig 	if (error)
979d4b74aeSChristoph Hellwig 		return error;
989d4b74aeSChristoph Hellwig 
99fd5ad30cSChristoph Hellwig 	error = vfs_utimes(&path, times);
1009d4b74aeSChristoph Hellwig 	path_put(&path);
1019d4b74aeSChristoph Hellwig 	if (retry_estale(error, lookup_flags)) {
1029d4b74aeSChristoph Hellwig 		lookup_flags |= LOOKUP_REVAL;
1039d4b74aeSChristoph Hellwig 		goto retry;
1049d4b74aeSChristoph Hellwig 	}
1059d4b74aeSChristoph Hellwig 
1069d4b74aeSChristoph Hellwig 	return error;
1079d4b74aeSChristoph Hellwig }
1089d4b74aeSChristoph Hellwig 
do_utimes_fd(int fd,struct timespec64 * times,int flags)1099d4b74aeSChristoph Hellwig static int do_utimes_fd(int fd, struct timespec64 *times, int flags)
1109d4b74aeSChristoph Hellwig {
1119d4b74aeSChristoph Hellwig 	struct fd f;
1129d4b74aeSChristoph Hellwig 	int error;
1139d4b74aeSChristoph Hellwig 
1149d4b74aeSChristoph Hellwig 	if (flags)
1159d4b74aeSChristoph Hellwig 		return -EINVAL;
1169d4b74aeSChristoph Hellwig 
1179d4b74aeSChristoph Hellwig 	f = fdget(fd);
1189d4b74aeSChristoph Hellwig 	if (!f.file)
1199d4b74aeSChristoph Hellwig 		return -EBADF;
120fd5ad30cSChristoph Hellwig 	error = vfs_utimes(&f.file->f_path, times);
1219d4b74aeSChristoph Hellwig 	fdput(f);
1229d4b74aeSChristoph Hellwig 	return error;
1239d4b74aeSChristoph Hellwig }
1249d4b74aeSChristoph Hellwig 
125e9b76fedSMiklos Szeredi /*
126e9b76fedSMiklos Szeredi  * do_utimes - change times on filename or file descriptor
127e9b76fedSMiklos Szeredi  * @dfd: open file descriptor, -1 or AT_FDCWD
128e9b76fedSMiklos Szeredi  * @filename: path name or NULL
129e9b76fedSMiklos Szeredi  * @times: new times or NULL
130e9b76fedSMiklos Szeredi  * @flags: zero or more flags (only AT_SYMLINK_NOFOLLOW for the moment)
131e9b76fedSMiklos Szeredi  *
132e9b76fedSMiklos Szeredi  * If filename is NULL and dfd refers to an open file, then operate on
133e9b76fedSMiklos Szeredi  * the file.  Otherwise look up filename, possibly using dfd as a
134e9b76fedSMiklos Szeredi  * starting point.
135e9b76fedSMiklos Szeredi  *
136e9b76fedSMiklos Szeredi  * If times==NULL, set access and modification to current time,
137e9b76fedSMiklos Szeredi  * must be owner or have write permission.
138e9b76fedSMiklos Szeredi  * Else, update from *times, must be owner or super user.
139e9b76fedSMiklos Szeredi  */
do_utimes(int dfd,const char __user * filename,struct timespec64 * times,int flags)140aaed2dd8SDeepa Dinamani long do_utimes(int dfd, const char __user *filename, struct timespec64 *times,
141c7887325SDavid Howells 	       int flags)
142e9b76fedSMiklos Szeredi {
1439d4b74aeSChristoph Hellwig 	if (filename == NULL && dfd != AT_FDCWD)
1449d4b74aeSChristoph Hellwig 		return do_utimes_fd(dfd, times, flags);
1459d4b74aeSChristoph Hellwig 	return do_utimes_path(dfd, filename, times, flags);
14682b0547cSAlexey Dobriyan }
14782b0547cSAlexey Dobriyan 
SYSCALL_DEFINE4(utimensat,int,dfd,const char __user *,filename,struct __kernel_timespec __user *,utimes,int,flags)148c7887325SDavid Howells SYSCALL_DEFINE4(utimensat, int, dfd, const char __user *, filename,
149a4f7a300SArnd Bergmann 		struct __kernel_timespec __user *, utimes, int, flags)
1501c710c89SUlrich Drepper {
151aaed2dd8SDeepa Dinamani 	struct timespec64 tstimes[2];
1521c710c89SUlrich Drepper 
1531c710c89SUlrich Drepper 	if (utimes) {
154aaed2dd8SDeepa Dinamani 		if ((get_timespec64(&tstimes[0], &utimes[0]) ||
155aaed2dd8SDeepa Dinamani 			get_timespec64(&tstimes[1], &utimes[1])))
1561c710c89SUlrich Drepper 			return -EFAULT;
1571c710c89SUlrich Drepper 
1581c710c89SUlrich Drepper 		/* Nothing to do, we must not even check the path.  */
1591c710c89SUlrich Drepper 		if (tstimes[0].tv_nsec == UTIME_OMIT &&
1601c710c89SUlrich Drepper 		    tstimes[1].tv_nsec == UTIME_OMIT)
1611c710c89SUlrich Drepper 			return 0;
1621c710c89SUlrich Drepper 	}
1631c710c89SUlrich Drepper 
1641c710c89SUlrich Drepper 	return do_utimes(dfd, filename, utimes ? tstimes : NULL, flags);
1651c710c89SUlrich Drepper }
1661c710c89SUlrich Drepper 
167185cfaf7SArnd Bergmann #ifdef __ARCH_WANT_SYS_UTIME
168185cfaf7SArnd Bergmann /*
169185cfaf7SArnd Bergmann  * futimesat(), utimes() and utime() are older versions of utimensat()
170185cfaf7SArnd Bergmann  * that are provided for compatibility with traditional C libraries.
171185cfaf7SArnd Bergmann  * On modern architectures, we always use libc wrappers around
172185cfaf7SArnd Bergmann  * utimensat() instead.
173185cfaf7SArnd Bergmann  */
do_futimesat(int dfd,const char __user * filename,struct __kernel_old_timeval __user * utimes)174f1390358SDominik Brodowski static long do_futimesat(int dfd, const char __user *filename,
17575d319c0SArnd Bergmann 			 struct __kernel_old_timeval __user *utimes)
17682b0547cSAlexey Dobriyan {
17775d319c0SArnd Bergmann 	struct __kernel_old_timeval times[2];
178aaed2dd8SDeepa Dinamani 	struct timespec64 tstimes[2];
17982b0547cSAlexey Dobriyan 
1801c710c89SUlrich Drepper 	if (utimes) {
1811c710c89SUlrich Drepper 		if (copy_from_user(&times, utimes, sizeof(times)))
18282b0547cSAlexey Dobriyan 			return -EFAULT;
1831c710c89SUlrich Drepper 
1841c710c89SUlrich Drepper 		/* This test is needed to catch all invalid values.  If we
1851c710c89SUlrich Drepper 		   would test only in do_utimes we would miss those invalid
1861c710c89SUlrich Drepper 		   values truncated by the multiplication with 1000.  Note
1871c710c89SUlrich Drepper 		   that we also catch UTIME_{NOW,OMIT} here which are only
1881c710c89SUlrich Drepper 		   valid for utimensat.  */
1891c710c89SUlrich Drepper 		if (times[0].tv_usec >= 1000000 || times[0].tv_usec < 0 ||
1901c710c89SUlrich Drepper 		    times[1].tv_usec >= 1000000 || times[1].tv_usec < 0)
1911c710c89SUlrich Drepper 			return -EINVAL;
1921c710c89SUlrich Drepper 
1931c710c89SUlrich Drepper 		tstimes[0].tv_sec = times[0].tv_sec;
1941c710c89SUlrich Drepper 		tstimes[0].tv_nsec = 1000 * times[0].tv_usec;
1951c710c89SUlrich Drepper 		tstimes[1].tv_sec = times[1].tv_sec;
1961c710c89SUlrich Drepper 		tstimes[1].tv_nsec = 1000 * times[1].tv_usec;
1971c710c89SUlrich Drepper 	}
1981c710c89SUlrich Drepper 
1991c710c89SUlrich Drepper 	return do_utimes(dfd, filename, utimes ? tstimes : NULL, 0);
20082b0547cSAlexey Dobriyan }
20182b0547cSAlexey Dobriyan 
202f1390358SDominik Brodowski 
SYSCALL_DEFINE3(futimesat,int,dfd,const char __user *,filename,struct __kernel_old_timeval __user *,utimes)203f1390358SDominik Brodowski SYSCALL_DEFINE3(futimesat, int, dfd, const char __user *, filename,
20475d319c0SArnd Bergmann 		struct __kernel_old_timeval __user *, utimes)
205f1390358SDominik Brodowski {
206f1390358SDominik Brodowski 	return do_futimesat(dfd, filename, utimes);
207f1390358SDominik Brodowski }
208f1390358SDominik Brodowski 
SYSCALL_DEFINE2(utimes,char __user *,filename,struct __kernel_old_timeval __user *,utimes)209003d7ab4SHeiko Carstens SYSCALL_DEFINE2(utimes, char __user *, filename,
21075d319c0SArnd Bergmann 		struct __kernel_old_timeval __user *, utimes)
21182b0547cSAlexey Dobriyan {
212f1390358SDominik Brodowski 	return do_futimesat(AT_FDCWD, filename, utimes);
21382b0547cSAlexey Dobriyan }
2141a060ba3SAl Viro 
SYSCALL_DEFINE2(utime,char __user *,filename,struct utimbuf __user *,times)215185cfaf7SArnd Bergmann SYSCALL_DEFINE2(utime, char __user *, filename, struct utimbuf __user *, times)
216185cfaf7SArnd Bergmann {
217185cfaf7SArnd Bergmann 	struct timespec64 tv[2];
218185cfaf7SArnd Bergmann 
219185cfaf7SArnd Bergmann 	if (times) {
220185cfaf7SArnd Bergmann 		if (get_user(tv[0].tv_sec, &times->actime) ||
221185cfaf7SArnd Bergmann 		    get_user(tv[1].tv_sec, &times->modtime))
222185cfaf7SArnd Bergmann 			return -EFAULT;
223185cfaf7SArnd Bergmann 		tv[0].tv_nsec = 0;
224185cfaf7SArnd Bergmann 		tv[1].tv_nsec = 0;
225185cfaf7SArnd Bergmann 	}
226185cfaf7SArnd Bergmann 	return do_utimes(AT_FDCWD, filename, times ? tv : NULL, 0);
227185cfaf7SArnd Bergmann }
228185cfaf7SArnd Bergmann #endif
229185cfaf7SArnd Bergmann 
2304faea239SArnd Bergmann #ifdef CONFIG_COMPAT_32BIT_TIME
2311a060ba3SAl Viro /*
2321a060ba3SAl Viro  * Not all architectures have sys_utime, so implement this in terms
2331a060ba3SAl Viro  * of sys_utimes.
2341a060ba3SAl Viro  */
2354faea239SArnd Bergmann #ifdef __ARCH_WANT_SYS_UTIME32
SYSCALL_DEFINE2(utime32,const char __user *,filename,struct old_utimbuf32 __user *,t)2368dabe724SArnd Bergmann SYSCALL_DEFINE2(utime32, const char __user *, filename,
2374faea239SArnd Bergmann 		struct old_utimbuf32 __user *, t)
2381a060ba3SAl Viro {
239aaed2dd8SDeepa Dinamani 	struct timespec64 tv[2];
2401a060ba3SAl Viro 
2411a060ba3SAl Viro 	if (t) {
2421a060ba3SAl Viro 		if (get_user(tv[0].tv_sec, &t->actime) ||
2431a060ba3SAl Viro 		    get_user(tv[1].tv_sec, &t->modtime))
2441a060ba3SAl Viro 			return -EFAULT;
2451a060ba3SAl Viro 		tv[0].tv_nsec = 0;
2461a060ba3SAl Viro 		tv[1].tv_nsec = 0;
2471a060ba3SAl Viro 	}
2481a060ba3SAl Viro 	return do_utimes(AT_FDCWD, filename, t ? tv : NULL, 0);
2491a060ba3SAl Viro }
2504faea239SArnd Bergmann #endif
2511a060ba3SAl Viro 
SYSCALL_DEFINE4(utimensat_time32,unsigned int,dfd,const char __user *,filename,struct old_timespec32 __user *,t,int,flags)2528dabe724SArnd Bergmann SYSCALL_DEFINE4(utimensat_time32, unsigned int, dfd, const char __user *, filename, struct old_timespec32 __user *, t, int, flags)
2531a060ba3SAl Viro {
254aaed2dd8SDeepa Dinamani 	struct timespec64 tv[2];
2551a060ba3SAl Viro 
2561a060ba3SAl Viro 	if  (t) {
2579afc5eeeSArnd Bergmann 		if (get_old_timespec32(&tv[0], &t[0]) ||
2589afc5eeeSArnd Bergmann 		    get_old_timespec32(&tv[1], &t[1]))
2591a060ba3SAl Viro 			return -EFAULT;
2601a060ba3SAl Viro 
2611a060ba3SAl Viro 		if (tv[0].tv_nsec == UTIME_OMIT && tv[1].tv_nsec == UTIME_OMIT)
2621a060ba3SAl Viro 			return 0;
2631a060ba3SAl Viro 	}
2641a060ba3SAl Viro 	return do_utimes(dfd, filename, t ? tv : NULL, flags);
2651a060ba3SAl Viro }
2661a060ba3SAl Viro 
2674faea239SArnd Bergmann #ifdef __ARCH_WANT_SYS_UTIME32
do_compat_futimesat(unsigned int dfd,const char __user * filename,struct old_timeval32 __user * t)268ab641afaSDominik Brodowski static long do_compat_futimesat(unsigned int dfd, const char __user *filename,
2699afc5eeeSArnd Bergmann 				struct old_timeval32 __user *t)
2701a060ba3SAl Viro {
271aaed2dd8SDeepa Dinamani 	struct timespec64 tv[2];
2721a060ba3SAl Viro 
2731a060ba3SAl Viro 	if (t) {
2741a060ba3SAl Viro 		if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
2751a060ba3SAl Viro 		    get_user(tv[0].tv_nsec, &t[0].tv_usec) ||
2761a060ba3SAl Viro 		    get_user(tv[1].tv_sec, &t[1].tv_sec) ||
2771a060ba3SAl Viro 		    get_user(tv[1].tv_nsec, &t[1].tv_usec))
2781a060ba3SAl Viro 			return -EFAULT;
2791a060ba3SAl Viro 		if (tv[0].tv_nsec >= 1000000 || tv[0].tv_nsec < 0 ||
2801a060ba3SAl Viro 		    tv[1].tv_nsec >= 1000000 || tv[1].tv_nsec < 0)
2811a060ba3SAl Viro 			return -EINVAL;
2821a060ba3SAl Viro 		tv[0].tv_nsec *= 1000;
2831a060ba3SAl Viro 		tv[1].tv_nsec *= 1000;
2841a060ba3SAl Viro 	}
2851a060ba3SAl Viro 	return do_utimes(dfd, filename, t ? tv : NULL, 0);
2861a060ba3SAl Viro }
2871a060ba3SAl Viro 
SYSCALL_DEFINE3(futimesat_time32,unsigned int,dfd,const char __user *,filename,struct old_timeval32 __user *,t)2888dabe724SArnd Bergmann SYSCALL_DEFINE3(futimesat_time32, unsigned int, dfd,
289ab641afaSDominik Brodowski 		       const char __user *, filename,
2909afc5eeeSArnd Bergmann 		       struct old_timeval32 __user *, t)
291ab641afaSDominik Brodowski {
292ab641afaSDominik Brodowski 	return do_compat_futimesat(dfd, filename, t);
293ab641afaSDominik Brodowski }
294ab641afaSDominik Brodowski 
SYSCALL_DEFINE2(utimes_time32,const char __user *,filename,struct old_timeval32 __user *,t)2958dabe724SArnd Bergmann SYSCALL_DEFINE2(utimes_time32, const char __user *, filename, struct old_timeval32 __user *, t)
2961a060ba3SAl Viro {
297ab641afaSDominik Brodowski 	return do_compat_futimesat(AT_FDCWD, filename, t);
2981a060ba3SAl Viro }
2991a060ba3SAl Viro #endif
3004faea239SArnd Bergmann #endif
301