open.c 33.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds's avatar
Linus Torvalds committed
2
3
4
5
6
7
8
9
10
/*
 *  linux/fs/open.c
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 */

#include <linux/string.h>
#include <linux/mm.h>
#include <linux/file.h>
Al Viro's avatar
Al Viro committed
11
#include <linux/fdtable.h>
Robert Love's avatar
Robert Love committed
12
#include <linux/fsnotify.h>
Linus Torvalds's avatar
Linus Torvalds committed
13
14
15
16
#include <linux/module.h>
#include <linux/tty.h>
#include <linux/namei.h>
#include <linux/backing-dev.h>
17
#include <linux/capability.h>
18
#include <linux/securebits.h>
Linus Torvalds's avatar
Linus Torvalds committed
19
20
#include <linux/security.h>
#include <linux/mount.h>
21
#include <linux/fcntl.h>
22
#include <linux/slab.h>
23
#include <linux/uaccess.h>
Linus Torvalds's avatar
Linus Torvalds committed
24
#include <linux/fs.h>
25
#include <linux/personality.h>
Linus Torvalds's avatar
Linus Torvalds committed
26
27
#include <linux/pagemap.h>
#include <linux/syscalls.h>
28
#include <linux/rcupdate.h>
29
#include <linux/audit.h>
30
#include <linux/falloc.h>
31
#include <linux/fs_struct.h>
32
#include <linux/ima.h>
33
#include <linux/dnotify.h>
Al Viro's avatar
Al Viro committed
34
#include <linux/compat.h>
Linus Torvalds's avatar
Linus Torvalds committed
35

36
37
#include "internal.h"

38
39
int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
	struct file *filp)
Linus Torvalds's avatar
Linus Torvalds committed
40
{
41
	int ret;
Linus Torvalds's avatar
Linus Torvalds committed
42
43
44
45
46
47
48
	struct iattr newattrs;

	/* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
	if (length < 0)
		return -EINVAL;

	newattrs.ia_size = length;
49
	newattrs.ia_valid = ATTR_SIZE | time_attrs;
50
51
52
53
	if (filp) {
		newattrs.ia_file = filp;
		newattrs.ia_valid |= ATTR_FILE;
	}
Linus Torvalds's avatar
Linus Torvalds committed
54

55
56
57
58
	/* Remove suid, sgid, and file capabilities on truncate too */
	ret = dentry_needs_remove_privs(dentry);
	if (ret < 0)
		return ret;
59
60
	if (ret)
		newattrs.ia_valid |= ret | ATTR_FORCE;
61

Al Viro's avatar
Al Viro committed
62
	inode_lock(dentry->d_inode);
63
64
	/* Note any delegations or leases have already been broken: */
	ret = notify_change(dentry, &newattrs, NULL);
Al Viro's avatar
Al Viro committed
65
	inode_unlock(dentry->d_inode);
66
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
67
68
}

Al Viro's avatar
Al Viro committed
69
long vfs_truncate(const struct path *path, loff_t length)
Linus Torvalds's avatar
Linus Torvalds committed
70
{
71
	struct inode *inode;
72
	long error;
Linus Torvalds's avatar
Linus Torvalds committed
73

74
	inode = path->dentry->d_inode;
Linus Torvalds's avatar
Linus Torvalds committed
75
76
77

	/* For directories it's -EISDIR, for other non-regulars - -EINVAL */
	if (S_ISDIR(inode->i_mode))
78
		return -EISDIR;
Linus Torvalds's avatar
Linus Torvalds committed
79
	if (!S_ISREG(inode->i_mode))
80
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
81

82
	error = mnt_want_write(path->mnt);
Linus Torvalds's avatar
Linus Torvalds committed
83
	if (error)
84
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
85

86
	error = inode_permission(inode, MAY_WRITE);
87
88
	if (error)
		goto mnt_drop_write_and_out;
Linus Torvalds's avatar
Linus Torvalds committed
89
90

	error = -EPERM;
91
	if (IS_APPEND(inode))
92
		goto mnt_drop_write_and_out;
Linus Torvalds's avatar
Linus Torvalds committed
93

94
	error = get_write_access(inode);
Linus Torvalds's avatar
Linus Torvalds committed
95
	if (error)
96
		goto mnt_drop_write_and_out;
Linus Torvalds's avatar
Linus Torvalds committed
97

98
99
100
101
	/*
	 * Make sure that there are no leases.  get_write_access() protects
	 * against the truncate racing with a lease-granting setlease().
	 */
102
	error = break_lease(inode, O_WRONLY);
Linus Torvalds's avatar
Linus Torvalds committed
103
	if (error)
104
		goto put_write_and_out;
Linus Torvalds's avatar
Linus Torvalds committed
105
106

	error = locks_verify_truncate(inode, NULL, length);
107
	if (!error)
108
		error = security_path_truncate(path);
109
	if (!error)
110
		error = do_truncate(path->dentry, length, 0, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
111

112
put_write_and_out:
113
	put_write_access(inode);
114
mnt_drop_write_and_out:
115
	mnt_drop_write(path->mnt);
Linus Torvalds's avatar
Linus Torvalds committed
116
117
118
out:
	return error;
}
119
120
EXPORT_SYMBOL_GPL(vfs_truncate);

121
long do_sys_truncate(const char __user *pathname, loff_t length)
122
{
123
	unsigned int lookup_flags = LOOKUP_FOLLOW;
124
125
126
127
128
129
	struct path path;
	int error;

	if (length < 0)	/* sorry, but loff_t says... */
		return -EINVAL;

130
131
retry:
	error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
132
133
134
135
	if (!error) {
		error = vfs_truncate(&path, length);
		path_put(&path);
	}
136
137
138
139
	if (retry_estale(error, lookup_flags)) {
		lookup_flags |= LOOKUP_REVAL;
		goto retry;
	}
140
141
	return error;
}
Linus Torvalds's avatar
Linus Torvalds committed
142

143
SYSCALL_DEFINE2(truncate, const char __user *, path, long, length)
Linus Torvalds's avatar
Linus Torvalds committed
144
{
145
	return do_sys_truncate(path, length);
Linus Torvalds's avatar
Linus Torvalds committed
146
147
}

Al Viro's avatar
Al Viro committed
148
149
150
151
152
153
154
#ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path, compat_off_t, length)
{
	return do_sys_truncate(path, length);
}
#endif

155
long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
Linus Torvalds's avatar
Linus Torvalds committed
156
{
Al Viro's avatar
Al Viro committed
157
	struct inode *inode;
Linus Torvalds's avatar
Linus Torvalds committed
158
	struct dentry *dentry;
159
	struct fd f;
Linus Torvalds's avatar
Linus Torvalds committed
160
161
162
163
164
165
	int error;

	error = -EINVAL;
	if (length < 0)
		goto out;
	error = -EBADF;
166
167
	f = fdget(fd);
	if (!f.file)
Linus Torvalds's avatar
Linus Torvalds committed
168
169
170
		goto out;

	/* explicitly opened as large or we are on 64-bit box */
171
	if (f.file->f_flags & O_LARGEFILE)
Linus Torvalds's avatar
Linus Torvalds committed
172
173
		small = 0;

174
	dentry = f.file->f_path.dentry;
Linus Torvalds's avatar
Linus Torvalds committed
175
176
	inode = dentry->d_inode;
	error = -EINVAL;
177
	if (!S_ISREG(inode->i_mode) || !(f.file->f_mode & FMODE_WRITE))
Linus Torvalds's avatar
Linus Torvalds committed
178
179
180
181
182
183
184
185
		goto out_putf;

	error = -EINVAL;
	/* Cannot ftruncate over 2^31 bytes without large file support */
	if (small && length > MAX_NON_LFS)
		goto out_putf;

	error = -EPERM;
186
187
	/* Check IS_APPEND on real upper inode */
	if (IS_APPEND(file_inode(f.file)))
Linus Torvalds's avatar
Linus Torvalds committed
188
189
		goto out_putf;

190
	sb_start_write(inode->i_sb);
191
	error = locks_verify_truncate(inode, f.file, length);
192
	if (!error)
193
		error = security_path_truncate(&f.file->f_path);
Linus Torvalds's avatar
Linus Torvalds committed
194
	if (!error)
195
		error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, f.file);
196
	sb_end_write(inode->i_sb);
Linus Torvalds's avatar
Linus Torvalds committed
197
out_putf:
198
	fdput(f);
Linus Torvalds's avatar
Linus Torvalds committed
199
200
201
202
out:
	return error;
}

203
SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length)
Linus Torvalds's avatar
Linus Torvalds committed
204
{
205
	return do_sys_ftruncate(fd, length, 1);
Linus Torvalds's avatar
Linus Torvalds committed
206
207
}

Al Viro's avatar
Al Viro committed
208
209
210
211
212
213
214
#ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE2(ftruncate, unsigned int, fd, compat_ulong_t, length)
{
	return do_sys_ftruncate(fd, length, 1);
}
#endif

Linus Torvalds's avatar
Linus Torvalds committed
215
216
/* LFS versions of truncate are only needed on 32 bit machines */
#if BITS_PER_LONG == 32
217
SYSCALL_DEFINE2(truncate64, const char __user *, path, loff_t, length)
Linus Torvalds's avatar
Linus Torvalds committed
218
219
220
221
{
	return do_sys_truncate(path, length);
}

222
SYSCALL_DEFINE2(ftruncate64, unsigned int, fd, loff_t, length)
Linus Torvalds's avatar
Linus Torvalds committed
223
{
224
	return do_sys_ftruncate(fd, length, 0);
Linus Torvalds's avatar
Linus Torvalds committed
225
}
226
#endif /* BITS_PER_LONG == 32 */
Linus Torvalds's avatar
Linus Torvalds committed
227

228

229
int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
230
{
Al Viro's avatar
Al Viro committed
231
	struct inode *inode = file_inode(file);
232
	long ret;
233
234

	if (offset < 0 || len <= 0)
235
		return -EINVAL;
236
237

	/* Return error if mode is not supported */
238
	if (mode & ~FALLOC_FL_SUPPORTED_MASK)
239
240
241
242
243
		return -EOPNOTSUPP;

	/* Punch hole and zero range are mutually exclusive */
	if ((mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) ==
	    (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE))
244
245
246
247
248
		return -EOPNOTSUPP;

	/* Punch hole must have keep size set */
	if ((mode & FALLOC_FL_PUNCH_HOLE) &&
	    !(mode & FALLOC_FL_KEEP_SIZE))
249
		return -EOPNOTSUPP;
250

251
252
253
254
255
	/* Collapse range should only be used exclusively. */
	if ((mode & FALLOC_FL_COLLAPSE_RANGE) &&
	    (mode & ~FALLOC_FL_COLLAPSE_RANGE))
		return -EINVAL;

256
257
258
259
260
	/* Insert range should only be used exclusively. */
	if ((mode & FALLOC_FL_INSERT_RANGE) &&
	    (mode & ~FALLOC_FL_INSERT_RANGE))
		return -EINVAL;

261
262
263
264
265
	/* Unshare range should only be used with allocate mode. */
	if ((mode & FALLOC_FL_UNSHARE_RANGE) &&
	    (mode & ~(FALLOC_FL_UNSHARE_RANGE | FALLOC_FL_KEEP_SIZE)))
		return -EINVAL;

266
	if (!(file->f_mode & FMODE_WRITE))
267
		return -EBADF;
268

269
	/*
270
	 * We can only allow pure fallocate on append only files
271
	 */
272
	if ((mode & ~FALLOC_FL_KEEP_SIZE) && IS_APPEND(inode))
273
274
275
276
277
		return -EPERM;

	if (IS_IMMUTABLE(inode))
		return -EPERM;

278
	/*
279
	 * We cannot allow any fallocate operation on an active swapfile
280
281
	 */
	if (IS_SWAPFILE(inode))
282
		return -ETXTBSY;
283

284
285
286
287
288
289
	/*
	 * Revalidate the write permissions, in case security policy has
	 * changed since the files were opened.
	 */
	ret = security_file_permission(file, MAY_WRITE);
	if (ret)
290
		return ret;
291
292

	if (S_ISFIFO(inode->i_mode))
293
		return -ESPIPE;
294

295
296
297
298
	if (S_ISDIR(inode->i_mode))
		return -EISDIR;

	if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
299
		return -ENODEV;
300
301
302

	/* Check for wrap through zero too */
	if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
303
		return -EFBIG;
304

305
	if (!file->f_op->fallocate)
306
		return -EOPNOTSUPP;
307

308
	file_start_write(file);
309
	ret = file->f_op->fallocate(file, mode, offset, len);
310
311
312
313
314
315
316
317
318
319
320

	/*
	 * Create inotify and fanotify events.
	 *
	 * To keep the logic simple always create events if fallocate succeeds.
	 * This implies that events are even created if the file size remains
	 * unchanged, e.g. when using flag FALLOC_FL_KEEP_SIZE.
	 */
	if (ret == 0)
		fsnotify_modify(file);

321
	file_end_write(file);
322
	return ret;
323
}
324
EXPORT_SYMBOL_GPL(vfs_fallocate);
325

326
int ksys_fallocate(int fd, int mode, loff_t offset, loff_t len)
327
{
328
	struct fd f = fdget(fd);
329
330
	int error = -EBADF;

331
	if (f.file) {
332
		error = vfs_fallocate(f.file, mode, offset, len);
333
		fdput(f);
334
335
	}
	return error;
336
}
337

338
339
340
341
342
SYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len)
{
	return ksys_fallocate(fd, mode, offset, len);
}

Linus Torvalds's avatar
Linus Torvalds committed
343
344
345
346
347
/*
 * access() needs to use the real uid/gid, not the effective uid/gid.
 * We do this by temporarily clearing all FS-related capabilities and
 * switching the fsuid/fsgid around to the real ones.
 */
348
static const struct cred *access_override_creds(void)
Linus Torvalds's avatar
Linus Torvalds committed
349
{
350
351
	const struct cred *old_cred;
	struct cred *override_cred;
Linus Torvalds's avatar
Linus Torvalds committed
352

353
354
	override_cred = prepare_creds();
	if (!override_cred)
355
		return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
356

357
358
	override_cred->fsuid = override_cred->uid;
	override_cred->fsgid = override_cred->gid;
Linus Torvalds's avatar
Linus Torvalds committed
359

360
	if (!issecure(SECURE_NO_SETUID_FIXUP)) {
David Howells's avatar
David Howells committed
361
		/* Clear the capabilities if we switch to a non-root user */
362
363
		kuid_t root_uid = make_kuid(override_cred->user_ns, 0);
		if (!uid_eq(override_cred->uid, root_uid))
364
			cap_clear(override_cred->cap_effective);
365
		else
366
367
			override_cred->cap_effective =
				override_cred->cap_permitted;
368
	}
Linus Torvalds's avatar
Linus Torvalds committed
369

370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
	/*
	 * The new set of credentials can *only* be used in
	 * task-synchronous circumstances, and does not need
	 * RCU freeing, unless somebody then takes a separate
	 * reference to it.
	 *
	 * NOTE! This is _only_ true because this credential
	 * is used purely for override_creds() that installs
	 * it as the subjective cred. Other threads will be
	 * accessing ->real_cred, not the subjective cred.
	 *
	 * If somebody _does_ make a copy of this (using the
	 * 'get_current_cred()' function), that will clear the
	 * non_rcu field, because now that other user may be
	 * expecting RCU freeing. But normal thread-synchronous
	 * cred accesses will keep things non-RCY.
	 */
	override_cred->non_rcu = 1;

389
	old_cred = override_creds(override_cred);
390
391
392
393
394
395
396

	/* override_cred() gets its own ref */
	put_cred(override_cred);

	return old_cred;
}

Miklos Szeredi's avatar
Miklos Szeredi committed
397
long do_faccessat(int dfd, const char __user *filename, int mode, int flags)
398
399
400
401
402
{
	struct path path;
	struct inode *inode;
	int res;
	unsigned int lookup_flags = LOOKUP_FOLLOW;
Miklos Szeredi's avatar
Miklos Szeredi committed
403
	const struct cred *old_cred = NULL;
404
405
406
407

	if (mode & ~S_IRWXO)	/* where's F_OK, X_OK, W_OK, R_OK? */
		return -EINVAL;

Miklos Szeredi's avatar
Miklos Szeredi committed
408
409
410
411
412
413
414
415
416
417
418
419
420
	if (flags & ~(AT_EACCESS | AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH))
		return -EINVAL;

	if (flags & AT_SYMLINK_NOFOLLOW)
		lookup_flags &= ~LOOKUP_FOLLOW;
	if (flags & AT_EMPTY_PATH)
		lookup_flags |= LOOKUP_EMPTY;

	if (!(flags & AT_EACCESS)) {
		old_cred = access_override_creds();
		if (!old_cred)
			return -ENOMEM;
	}
421

422
423
retry:
	res = user_path_at(dfd, filename, lookup_flags, &path);
424
425
426
	if (res)
		goto out;

427
	inode = d_backing_inode(path.dentry);
428
429

	if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
430
431
432
433
434
		/*
		 * MAY_EXEC on regular files is denied if the fs is mounted
		 * with the "noexec" flag.
		 */
		res = -EACCES;
435
		if (path_noexec(&path))
436
437
438
			goto out_path_release;
	}

439
	res = inode_permission(inode, mode | MAY_ACCESS);
440
	/* SuS v2 requires we report a read only fs too */
441
	if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
442
		goto out_path_release;
443
444
445
446
447
448
449
450
451
452
	/*
	 * This is a rare case where using __mnt_is_readonly()
	 * is OK without a mnt_want/drop_write() pair.  Since
	 * no actual write to the fs is performed here, we do
	 * not need to telegraph to that to anyone.
	 *
	 * By doing this, we accept that this access is
	 * inherently racy and know that the fs may change
	 * state before we even see this result.
	 */
453
	if (__mnt_is_readonly(path.mnt))
454
		res = -EROFS;
Linus Torvalds's avatar
Linus Torvalds committed
455

456
out_path_release:
457
	path_put(&path);
458
459
460
461
	if (retry_estale(res, lookup_flags)) {
		lookup_flags |= LOOKUP_REVAL;
		goto retry;
	}
462
out:
Miklos Szeredi's avatar
Miklos Szeredi committed
463
464
465
	if (old_cred)
		revert_creds(old_cred);

Linus Torvalds's avatar
Linus Torvalds committed
466
467
468
	return res;
}

469
470
SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
{
Miklos Szeredi's avatar
Miklos Szeredi committed
471
472
473
474
475
476
477
	return do_faccessat(dfd, filename, mode, 0);
}

SYSCALL_DEFINE4(faccessat2, int, dfd, const char __user *, filename, int, mode,
		int, flags)
{
	return do_faccessat(dfd, filename, mode, flags);
478
479
}

480
SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
481
{
Miklos Szeredi's avatar
Miklos Szeredi committed
482
	return do_faccessat(AT_FDCWD, filename, mode, 0);
483
484
}

485
int ksys_chdir(const char __user *filename)
Linus Torvalds's avatar
Linus Torvalds committed
486
{
487
	struct path path;
Linus Torvalds's avatar
Linus Torvalds committed
488
	int error;
489
490
491
	unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
retry:
	error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
Linus Torvalds's avatar
Linus Torvalds committed
492
493
494
	if (error)
		goto out;

Eric Paris's avatar
Eric Paris committed
495
	error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
Linus Torvalds's avatar
Linus Torvalds committed
496
497
498
	if (error)
		goto dput_and_out;

499
	set_fs_pwd(current->fs, &path);
Linus Torvalds's avatar
Linus Torvalds committed
500
501

dput_and_out:
502
	path_put(&path);
503
504
505
506
	if (retry_estale(error, lookup_flags)) {
		lookup_flags |= LOOKUP_REVAL;
		goto retry;
	}
Linus Torvalds's avatar
Linus Torvalds committed
507
508
509
510
out:
	return error;
}

511
512
513
514
515
SYSCALL_DEFINE1(chdir, const char __user *, filename)
{
	return ksys_chdir(filename);
}

516
SYSCALL_DEFINE1(fchdir, unsigned int, fd)
Linus Torvalds's avatar
Linus Torvalds committed
517
{
518
	struct fd f = fdget_raw(fd);
519
	int error;
Linus Torvalds's avatar
Linus Torvalds committed
520
521

	error = -EBADF;
522
	if (!f.file)
Linus Torvalds's avatar
Linus Torvalds committed
523
524
525
		goto out;

	error = -ENOTDIR;
526
	if (!d_can_lookup(f.file->f_path.dentry))
Linus Torvalds's avatar
Linus Torvalds committed
527
528
		goto out_putf;

529
	error = inode_permission(file_inode(f.file), MAY_EXEC | MAY_CHDIR);
Linus Torvalds's avatar
Linus Torvalds committed
530
	if (!error)
531
		set_fs_pwd(current->fs, &f.file->f_path);
Linus Torvalds's avatar
Linus Torvalds committed
532
out_putf:
533
	fdput(f);
Linus Torvalds's avatar
Linus Torvalds committed
534
535
536
537
out:
	return error;
}

538
int ksys_chroot(const char __user *filename)
Linus Torvalds's avatar
Linus Torvalds committed
539
{
540
	struct path path;
Linus Torvalds's avatar
Linus Torvalds committed
541
	int error;
542
543
544
	unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
retry:
	error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
Linus Torvalds's avatar
Linus Torvalds committed
545
546
547
	if (error)
		goto out;

Eric Paris's avatar
Eric Paris committed
548
	error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
Linus Torvalds's avatar
Linus Torvalds committed
549
550
551
552
	if (error)
		goto dput_and_out;

	error = -EPERM;
553
	if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT))
Linus Torvalds's avatar
Linus Torvalds committed
554
		goto dput_and_out;
555
556
557
	error = security_path_chroot(&path);
	if (error)
		goto dput_and_out;
Linus Torvalds's avatar
Linus Torvalds committed
558

559
	set_fs_root(current->fs, &path);
Linus Torvalds's avatar
Linus Torvalds committed
560
561
	error = 0;
dput_and_out:
562
	path_put(&path);
563
564
565
566
	if (retry_estale(error, lookup_flags)) {
		lookup_flags |= LOOKUP_REVAL;
		goto retry;
	}
Linus Torvalds's avatar
Linus Torvalds committed
567
568
569
570
out:
	return error;
}

571
572
573
574
575
SYSCALL_DEFINE1(chroot, const char __user *, filename)
{
	return ksys_chroot(filename);
}

576
static int chmod_common(const struct path *path, umode_t mode)
Linus Torvalds's avatar
Linus Torvalds committed
577
{
578
	struct inode *inode = path->dentry->d_inode;
579
	struct inode *delegated_inode = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
580
	struct iattr newattrs;
581
	int error;
Linus Torvalds's avatar
Linus Torvalds committed
582

583
584
585
	error = mnt_want_write(path->mnt);
	if (error)
		return error;
586
retry_deleg:
Al Viro's avatar
Al Viro committed
587
	inode_lock(inode);
588
	error = security_path_chmod(path, mode);
589
	if (error)
590
		goto out_unlock;
Linus Torvalds's avatar
Linus Torvalds committed
591
592
	newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
	newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
593
	error = notify_change(path->dentry, &newattrs, &delegated_inode);
594
out_unlock:
Al Viro's avatar
Al Viro committed
595
	inode_unlock(inode);
596
597
598
599
600
	if (delegated_inode) {
		error = break_deleg_wait(&delegated_inode);
		if (!error)
			goto retry_deleg;
	}
601
602
603
604
	mnt_drop_write(path->mnt);
	return error;
}

605
int ksys_fchmod(unsigned int fd, umode_t mode)
606
{
Al Viro's avatar
Al Viro committed
607
	struct fd f = fdget(fd);
608
609
	int err = -EBADF;

Al Viro's avatar
Al Viro committed
610
	if (f.file) {
Al Viro's avatar
Al Viro committed
611
		audit_file(f.file);
Al Viro's avatar
Al Viro committed
612
613
		err = chmod_common(&f.file->f_path, mode);
		fdput(f);
614
	}
Linus Torvalds's avatar
Linus Torvalds committed
615
616
617
	return err;
}

618
619
620
621
622
623
SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode)
{
	return ksys_fchmod(fd, mode);
}

int do_fchmodat(int dfd, const char __user *filename, umode_t mode)
Linus Torvalds's avatar
Linus Torvalds committed
624
{
625
	struct path path;
Linus Torvalds's avatar
Linus Torvalds committed
626
	int error;
627
628
629
	unsigned int lookup_flags = LOOKUP_FOLLOW;
retry:
	error = user_path_at(dfd, filename, lookup_flags, &path);
630
631
632
	if (!error) {
		error = chmod_common(&path, mode);
		path_put(&path);
633
634
635
636
		if (retry_estale(error, lookup_flags)) {
			lookup_flags |= LOOKUP_REVAL;
			goto retry;
		}
637
	}
Linus Torvalds's avatar
Linus Torvalds committed
638
639
640
	return error;
}

641
642
643
644
645
646
SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename,
		umode_t, mode)
{
	return do_fchmodat(dfd, filename, mode);
}

647
SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode)
648
{
649
	return do_fchmodat(AT_FDCWD, filename, mode);
650
651
}

652
static int chown_common(const struct path *path, uid_t user, gid_t group)
Linus Torvalds's avatar
Linus Torvalds committed
653
{
654
	struct inode *inode = path->dentry->d_inode;
655
	struct inode *delegated_inode = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
656
657
	int error;
	struct iattr newattrs;
658
659
660
661
662
	kuid_t uid;
	kgid_t gid;

	uid = make_kuid(current_user_ns(), user);
	gid = make_kgid(current_user_ns(), group);
Linus Torvalds's avatar
Linus Torvalds committed
663

664
retry_deleg:
Linus Torvalds's avatar
Linus Torvalds committed
665
666
	newattrs.ia_valid =  ATTR_CTIME;
	if (user != (uid_t) -1) {
667
668
		if (!uid_valid(uid))
			return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
669
		newattrs.ia_valid |= ATTR_UID;
670
		newattrs.ia_uid = uid;
Linus Torvalds's avatar
Linus Torvalds committed
671
672
	}
	if (group != (gid_t) -1) {
673
674
		if (!gid_valid(gid))
			return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
675
		newattrs.ia_valid |= ATTR_GID;
676
		newattrs.ia_gid = gid;
Linus Torvalds's avatar
Linus Torvalds committed
677
678
	}
	if (!S_ISDIR(inode->i_mode))
679
680
		newattrs.ia_valid |=
			ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
Al Viro's avatar
Al Viro committed
681
	inode_lock(inode);
682
	error = security_path_chown(path, uid, gid);
683
	if (!error)
684
		error = notify_change(path->dentry, &newattrs, &delegated_inode);
Al Viro's avatar
Al Viro committed
685
	inode_unlock(inode);
686
687
688
689
690
	if (delegated_inode) {
		error = break_deleg_wait(&delegated_inode);
		if (!error)
			goto retry_deleg;
	}
Linus Torvalds's avatar
Linus Torvalds committed
691
692
693
	return error;
}

694
695
int do_fchownat(int dfd, const char __user *filename, uid_t user, gid_t group,
		int flag)
696
{
697
	struct path path;
698
	int error = -EINVAL;
699
	int lookup_flags;
700

701
	if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
702
703
		goto out;

704
705
706
	lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
	if (flag & AT_EMPTY_PATH)
		lookup_flags |= LOOKUP_EMPTY;
707
retry:
708
	error = user_path_at(dfd, filename, lookup_flags, &path);
709
710
	if (error)
		goto out;
711
	error = mnt_want_write(path.mnt);
712
713
	if (error)
		goto out_release;
714
	error = chown_common(&path, user, group);
715
	mnt_drop_write(path.mnt);
716
out_release:
717
	path_put(&path);
718
719
720
721
	if (retry_estale(error, lookup_flags)) {
		lookup_flags |= LOOKUP_REVAL;
		goto retry;
	}
722
723
724
725
out:
	return error;
}

726
727
728
729
730
731
SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
		gid_t, group, int, flag)
{
	return do_fchownat(dfd, filename, user, group, flag);
}

732
SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
Linus Torvalds's avatar
Linus Torvalds committed
733
{
734
	return do_fchownat(AT_FDCWD, filename, user, group, 0);
735
}
Linus Torvalds's avatar
Linus Torvalds committed
736

737
738
SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
{
739
740
	return do_fchownat(AT_FDCWD, filename, user, group,
			   AT_SYMLINK_NOFOLLOW);
Linus Torvalds's avatar
Linus Torvalds committed
741
742
}

743
int ksys_fchown(unsigned int fd, uid_t user, gid_t group)
Linus Torvalds's avatar
Linus Torvalds committed
744
{
745
	struct fd f = fdget(fd);
Linus Torvalds's avatar
Linus Torvalds committed
746
747
	int error = -EBADF;

748
	if (!f.file)
749
750
		goto out;

751
	error = mnt_want_write_file(f.file);
752
753
	if (error)
		goto out_fput;
Al Viro's avatar
Al Viro committed
754
	audit_file(f.file);
755
	error = chown_common(&f.file->f_path, user, group);
756
	mnt_drop_write_file(f.file);
757
out_fput:
758
	fdput(f);
759
out:
Linus Torvalds's avatar
Linus Torvalds committed
760
761
762
	return error;
}

763
764
765
766
767
SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
{
	return ksys_fchown(fd, user, group);
}

768
static int do_dentry_open(struct file *f,
769
			  struct inode *inode,
770
			  int (*open)(struct inode *, struct file *))
Linus Torvalds's avatar
Linus Torvalds committed
771
{
772
	static const struct file_operations empty_fops = {};
Linus Torvalds's avatar
Linus Torvalds committed
773
774
	int error;

775
	path_get(&f->f_path);
776
	f->f_inode = inode;
Linus Torvalds's avatar
Linus Torvalds committed
777
	f->f_mapping = inode->i_mapping;
778
	f->f_wb_err = filemap_sample_wb_err(f->f_mapping);
779
	f->f_sb_err = file_sample_sb_err(f);
780

Al Viro's avatar
Al Viro committed
781
	if (unlikely(f->f_flags & O_PATH)) {
Al Viro's avatar
Al Viro committed
782
		f->f_mode = FMODE_PATH | FMODE_OPENED;
783
		f->f_op = &empty_fops;
784
		return 0;
785
786
	}

787
788
789
790
791
792
	/* Any file opened for execve()/uselib() has to be a regular file. */
	if (unlikely(f->f_flags & FMODE_EXEC && !S_ISREG(inode->i_mode))) {
		error = -EACCES;
		goto cleanup_file;
	}

793
	if (f->f_mode & FMODE_WRITE && !special_file(inode->i_mode)) {
794
		error = get_write_access(inode);
Al Viro's avatar
Al Viro committed
795
		if (unlikely(error))
Linus Torvalds's avatar
Linus Torvalds committed
796
			goto cleanup_file;
797
		error = __mnt_want_write(f->f_path.mnt);
Al Viro's avatar
Al Viro committed
798
		if (unlikely(error)) {
799
800
801
			put_write_access(inode);
			goto cleanup_file;
		}
802
		f->f_mode |= FMODE_WRITER;
Linus Torvalds's avatar
Linus Torvalds committed
803
804
	}

805
806
807
808
	/* POSIX.1-2008/SUSv4 Section XSI 2.9.7 */
	if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))
		f->f_mode |= FMODE_ATOMIC_POS;

809
	f->f_op = fops_get(inode->i_fop);
810
	if (WARN_ON(!f->f_op)) {
Al Viro's avatar
Al Viro committed
811
812
813
		error = -ENODEV;
		goto cleanup_all;
	}
814

815
	error = security_file_open(f);
816
817
818
	if (error)
		goto cleanup_all;

819
	error = break_lease(locks_inode(f), f->f_flags);
820
821
822
	if (error)
		goto cleanup_all;

823
824
	/* normally all 3 are set; ->open() can clear them if needed */
	f->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
Al Viro's avatar
Al Viro committed
825
	if (!open)
826
827
828
		open = f->f_op->open;
	if (open) {
		error = open(inode, f);
Linus Torvalds's avatar
Linus Torvalds committed
829
830
831
		if (error)
			goto cleanup_all;
	}
Al Viro's avatar
Al Viro committed
832
	f->f_mode |= FMODE_OPENED;
833
834
	if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
		i_readcount_inc(inode);
835
	if ((f->f_mode & FMODE_READ) &&
Al Viro's avatar
Al Viro committed
836
	     likely(f->f_op->read || f->f_op->read_iter))
837
		f->f_mode |= FMODE_CAN_READ;
838
	if ((f->f_mode & FMODE_WRITE) &&
Al Viro's avatar
Al Viro committed
839
	     likely(f->f_op->write || f->f_op->write_iter))
840
		f->f_mode |= FMODE_CAN_WRITE;
841

842
	f->f_write_hint = WRITE_LIFE_NOT_SET;
Linus Torvalds's avatar
Linus Torvalds committed
843
844
845
	f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);

	file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
846

847
848
849
850
851
	/* NB: we're sure to have correct a_ops only after f_op->open */
	if (f->f_flags & O_DIRECT) {
		if (!f->f_mapping->a_ops || !f->f_mapping->a_ops->direct_IO)
			return -EINVAL;
	}
852
853
854
855
856
857
858
859

	/*
	 * XXX: Huge page cache doesn't support writing yet. Drop all page
	 * cache for this file before processing writes.
	 */
	if ((f->f_mode & FMODE_WRITE) && filemap_nr_thps(inode->i_mapping))
		truncate_pagecache(inode, 0);

860
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
861
862

cleanup_all:
863
864
	if (WARN_ON_ONCE(error > 0))
		error = -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
865
	fops_put(f->f_op);
866
	if (f->f_mode & FMODE_WRITER) {
Linus Torvalds's avatar
Linus Torvalds committed
867
		put_write_access(inode);
868
		__mnt_drop_write(f->f_path.mnt);
869
	}
Linus Torvalds's avatar
Linus Torvalds committed
870
cleanup_file:
871
872
873
	path_put(&f->f_path);
	f->f_path.mnt = NULL;
	f->f_path.dentry = NULL;
874
	f->f_inode = NULL;
875
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
876
877
}

Miklos Szeredi's avatar
Miklos Szeredi committed
878
879
/**
 * finish_open - finish opening a file
880
 * @file: file pointer
Miklos Szeredi's avatar
Miklos Szeredi committed
881
882
 * @dentry: pointer to dentry
 * @open: open callback
883
 * @opened: state of open
Miklos Szeredi's avatar
Miklos Szeredi committed
884
885
886
887
888
 *
 * This can be used to finish opening a file passed to i_op->atomic_open().
 *
 * If the open callback is set to NULL, then the standard f_op->open()