1 // SPDX-License-Identifier: GPL-2.0-only 1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 2 /* 3 * linux/fs/open.c 3 * linux/fs/open.c 4 * 4 * 5 * Copyright (C) 1991, 1992 Linus Torvalds 5 * Copyright (C) 1991, 1992 Linus Torvalds 6 */ 6 */ 7 7 8 #include <linux/string.h> 8 #include <linux/string.h> 9 #include <linux/mm.h> 9 #include <linux/mm.h> 10 #include <linux/file.h> 10 #include <linux/file.h> 11 #include <linux/fdtable.h> 11 #include <linux/fdtable.h> 12 #include <linux/fsnotify.h> 12 #include <linux/fsnotify.h> 13 #include <linux/module.h> 13 #include <linux/module.h> 14 #include <linux/tty.h> 14 #include <linux/tty.h> 15 #include <linux/namei.h> 15 #include <linux/namei.h> 16 #include <linux/backing-dev.h> 16 #include <linux/backing-dev.h> 17 #include <linux/capability.h> 17 #include <linux/capability.h> 18 #include <linux/securebits.h> 18 #include <linux/securebits.h> 19 #include <linux/security.h> 19 #include <linux/security.h> 20 #include <linux/mount.h> 20 #include <linux/mount.h> 21 #include <linux/fcntl.h> 21 #include <linux/fcntl.h> 22 #include <linux/slab.h> 22 #include <linux/slab.h> 23 #include <linux/uaccess.h> 23 #include <linux/uaccess.h> 24 #include <linux/fs.h> 24 #include <linux/fs.h> 25 #include <linux/personality.h> 25 #include <linux/personality.h> 26 #include <linux/pagemap.h> 26 #include <linux/pagemap.h> 27 #include <linux/syscalls.h> 27 #include <linux/syscalls.h> 28 #include <linux/rcupdate.h> 28 #include <linux/rcupdate.h> 29 #include <linux/audit.h> 29 #include <linux/audit.h> 30 #include <linux/falloc.h> 30 #include <linux/falloc.h> 31 #include <linux/fs_struct.h> 31 #include <linux/fs_struct.h> >> 32 #include <linux/ima.h> 32 #include <linux/dnotify.h> 33 #include <linux/dnotify.h> 33 #include <linux/compat.h> 34 #include <linux/compat.h> 34 #include <linux/mnt_idmapping.h> << 35 #include <linux/filelock.h> << 36 35 37 #include "internal.h" 36 #include "internal.h" 38 37 39 int do_truncate(struct mnt_idmap *idmap, struc !! 38 int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs, 40 loff_t length, unsigned int ti !! 39 struct file *filp) 41 { 40 { 42 int ret; 41 int ret; 43 struct iattr newattrs; 42 struct iattr newattrs; 44 43 45 /* Not pretty: "inode->i_size" shouldn 44 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */ 46 if (length < 0) 45 if (length < 0) 47 return -EINVAL; 46 return -EINVAL; 48 47 49 newattrs.ia_size = length; 48 newattrs.ia_size = length; 50 newattrs.ia_valid = ATTR_SIZE | time_a 49 newattrs.ia_valid = ATTR_SIZE | time_attrs; 51 if (filp) { 50 if (filp) { 52 newattrs.ia_file = filp; 51 newattrs.ia_file = filp; 53 newattrs.ia_valid |= ATTR_FILE 52 newattrs.ia_valid |= ATTR_FILE; 54 } 53 } 55 54 56 /* Remove suid, sgid, and file capabil 55 /* Remove suid, sgid, and file capabilities on truncate too */ 57 ret = dentry_needs_remove_privs(idmap, !! 56 ret = dentry_needs_remove_privs(dentry); 58 if (ret < 0) 57 if (ret < 0) 59 return ret; 58 return ret; 60 if (ret) 59 if (ret) 61 newattrs.ia_valid |= ret | ATT 60 newattrs.ia_valid |= ret | ATTR_FORCE; 62 61 63 inode_lock(dentry->d_inode); 62 inode_lock(dentry->d_inode); 64 /* Note any delegations or leases have 63 /* Note any delegations or leases have already been broken: */ 65 ret = notify_change(idmap, dentry, &ne !! 64 ret = notify_change(dentry, &newattrs, NULL); 66 inode_unlock(dentry->d_inode); 65 inode_unlock(dentry->d_inode); 67 return ret; 66 return ret; 68 } 67 } 69 68 70 long vfs_truncate(const struct path *path, lof 69 long vfs_truncate(const struct path *path, loff_t length) 71 { 70 { 72 struct mnt_idmap *idmap; << 73 struct inode *inode; 71 struct inode *inode; 74 long error; 72 long error; 75 73 76 inode = path->dentry->d_inode; 74 inode = path->dentry->d_inode; 77 75 78 /* For directories it's -EISDIR, for o 76 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */ 79 if (S_ISDIR(inode->i_mode)) 77 if (S_ISDIR(inode->i_mode)) 80 return -EISDIR; 78 return -EISDIR; 81 if (!S_ISREG(inode->i_mode)) 79 if (!S_ISREG(inode->i_mode)) 82 return -EINVAL; 80 return -EINVAL; 83 81 84 error = mnt_want_write(path->mnt); 82 error = mnt_want_write(path->mnt); 85 if (error) 83 if (error) 86 goto out; 84 goto out; 87 85 88 idmap = mnt_idmap(path->mnt); !! 86 error = inode_permission(inode, MAY_WRITE); 89 error = inode_permission(idmap, inode, << 90 if (error) 87 if (error) 91 goto mnt_drop_write_and_out; 88 goto mnt_drop_write_and_out; 92 89 93 error = -EPERM; 90 error = -EPERM; 94 if (IS_APPEND(inode)) 91 if (IS_APPEND(inode)) 95 goto mnt_drop_write_and_out; 92 goto mnt_drop_write_and_out; 96 93 97 error = get_write_access(inode); 94 error = get_write_access(inode); 98 if (error) 95 if (error) 99 goto mnt_drop_write_and_out; 96 goto mnt_drop_write_and_out; 100 97 101 /* 98 /* 102 * Make sure that there are no leases. 99 * Make sure that there are no leases. get_write_access() protects 103 * against the truncate racing with a 100 * against the truncate racing with a lease-granting setlease(). 104 */ 101 */ 105 error = break_lease(inode, O_WRONLY); 102 error = break_lease(inode, O_WRONLY); 106 if (error) 103 if (error) 107 goto put_write_and_out; 104 goto put_write_and_out; 108 105 109 error = security_path_truncate(path); !! 106 error = locks_verify_truncate(inode, NULL, length); 110 if (!error) 107 if (!error) 111 error = do_truncate(idmap, pat !! 108 error = security_path_truncate(path); >> 109 if (!error) >> 110 error = do_truncate(path->dentry, length, 0, NULL); 112 111 113 put_write_and_out: 112 put_write_and_out: 114 put_write_access(inode); 113 put_write_access(inode); 115 mnt_drop_write_and_out: 114 mnt_drop_write_and_out: 116 mnt_drop_write(path->mnt); 115 mnt_drop_write(path->mnt); 117 out: 116 out: 118 return error; 117 return error; 119 } 118 } 120 EXPORT_SYMBOL_GPL(vfs_truncate); 119 EXPORT_SYMBOL_GPL(vfs_truncate); 121 120 122 long do_sys_truncate(const char __user *pathna 121 long do_sys_truncate(const char __user *pathname, loff_t length) 123 { 122 { 124 unsigned int lookup_flags = LOOKUP_FOL 123 unsigned int lookup_flags = LOOKUP_FOLLOW; 125 struct path path; 124 struct path path; 126 int error; 125 int error; 127 126 128 if (length < 0) /* sorry, but loff_t s 127 if (length < 0) /* sorry, but loff_t says... */ 129 return -EINVAL; 128 return -EINVAL; 130 129 131 retry: 130 retry: 132 error = user_path_at(AT_FDCWD, pathnam 131 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path); 133 if (!error) { 132 if (!error) { 134 error = vfs_truncate(&path, le 133 error = vfs_truncate(&path, length); 135 path_put(&path); 134 path_put(&path); 136 } 135 } 137 if (retry_estale(error, lookup_flags)) 136 if (retry_estale(error, lookup_flags)) { 138 lookup_flags |= LOOKUP_REVAL; 137 lookup_flags |= LOOKUP_REVAL; 139 goto retry; 138 goto retry; 140 } 139 } 141 return error; 140 return error; 142 } 141 } 143 142 144 SYSCALL_DEFINE2(truncate, const char __user *, 143 SYSCALL_DEFINE2(truncate, const char __user *, path, long, length) 145 { 144 { 146 return do_sys_truncate(path, length); 145 return do_sys_truncate(path, length); 147 } 146 } 148 147 149 #ifdef CONFIG_COMPAT 148 #ifdef CONFIG_COMPAT 150 COMPAT_SYSCALL_DEFINE2(truncate, const char __ 149 COMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path, compat_off_t, length) 151 { 150 { 152 return do_sys_truncate(path, length); 151 return do_sys_truncate(path, length); 153 } 152 } 154 #endif 153 #endif 155 154 156 long do_ftruncate(struct file *file, loff_t le !! 155 long do_sys_ftruncate(unsigned int fd, loff_t length, int small) 157 { 156 { 158 struct inode *inode; 157 struct inode *inode; 159 struct dentry *dentry; 158 struct dentry *dentry; >> 159 struct fd f; 160 int error; 160 int error; 161 161 >> 162 error = -EINVAL; >> 163 if (length < 0) >> 164 goto out; >> 165 error = -EBADF; >> 166 f = fdget(fd); >> 167 if (!f.file) >> 168 goto out; >> 169 162 /* explicitly opened as large or we ar 170 /* explicitly opened as large or we are on 64-bit box */ 163 if (file->f_flags & O_LARGEFILE) !! 171 if (f.file->f_flags & O_LARGEFILE) 164 small = 0; 172 small = 0; 165 173 166 dentry = file->f_path.dentry; !! 174 dentry = f.file->f_path.dentry; 167 inode = dentry->d_inode; 175 inode = dentry->d_inode; 168 if (!S_ISREG(inode->i_mode) || !(file- !! 176 error = -EINVAL; 169 return -EINVAL; !! 177 if (!S_ISREG(inode->i_mode) || !(f.file->f_mode & FMODE_WRITE)) >> 178 goto out_putf; 170 179 >> 180 error = -EINVAL; 171 /* Cannot ftruncate over 2^31 bytes wi 181 /* Cannot ftruncate over 2^31 bytes without large file support */ 172 if (small && length > MAX_NON_LFS) 182 if (small && length > MAX_NON_LFS) 173 return -EINVAL; !! 183 goto out_putf; 174 184 >> 185 error = -EPERM; 175 /* Check IS_APPEND on real upper inode 186 /* Check IS_APPEND on real upper inode */ 176 if (IS_APPEND(file_inode(file))) !! 187 if (IS_APPEND(file_inode(f.file))) 177 return -EPERM; !! 188 goto out_putf; >> 189 178 sb_start_write(inode->i_sb); 190 sb_start_write(inode->i_sb); 179 error = security_file_truncate(file); !! 191 error = locks_verify_truncate(inode, f.file, length); 180 if (!error) 192 if (!error) 181 error = do_truncate(file_mnt_i !! 193 error = security_path_truncate(&f.file->f_path); 182 ATTR_MTIME !! 194 if (!error) >> 195 error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, f.file); 183 sb_end_write(inode->i_sb); 196 sb_end_write(inode->i_sb); 184 !! 197 out_putf: 185 return error; << 186 } << 187 << 188 long do_sys_ftruncate(unsigned int fd, loff_t << 189 { << 190 struct fd f; << 191 int error; << 192 << 193 if (length < 0) << 194 return -EINVAL; << 195 f = fdget(fd); << 196 if (!f.file) << 197 return -EBADF; << 198 << 199 error = do_ftruncate(f.file, length, s << 200 << 201 fdput(f); 198 fdput(f); >> 199 out: 202 return error; 200 return error; 203 } 201 } 204 202 205 SYSCALL_DEFINE2(ftruncate, unsigned int, fd, o !! 203 SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length) 206 { 204 { 207 return do_sys_ftruncate(fd, length, 1) 205 return do_sys_ftruncate(fd, length, 1); 208 } 206 } 209 207 210 #ifdef CONFIG_COMPAT 208 #ifdef CONFIG_COMPAT 211 COMPAT_SYSCALL_DEFINE2(ftruncate, unsigned int !! 209 COMPAT_SYSCALL_DEFINE2(ftruncate, unsigned int, fd, compat_ulong_t, length) 212 { 210 { 213 return do_sys_ftruncate(fd, length, 1) 211 return do_sys_ftruncate(fd, length, 1); 214 } 212 } 215 #endif 213 #endif 216 214 217 /* LFS versions of truncate are only needed on 215 /* LFS versions of truncate are only needed on 32 bit machines */ 218 #if BITS_PER_LONG == 32 216 #if BITS_PER_LONG == 32 219 SYSCALL_DEFINE2(truncate64, const char __user 217 SYSCALL_DEFINE2(truncate64, const char __user *, path, loff_t, length) 220 { 218 { 221 return do_sys_truncate(path, length); 219 return do_sys_truncate(path, length); 222 } 220 } 223 221 224 SYSCALL_DEFINE2(ftruncate64, unsigned int, fd, 222 SYSCALL_DEFINE2(ftruncate64, unsigned int, fd, loff_t, length) 225 { 223 { 226 return do_sys_ftruncate(fd, length, 0) 224 return do_sys_ftruncate(fd, length, 0); 227 } 225 } 228 #endif /* BITS_PER_LONG == 32 */ 226 #endif /* BITS_PER_LONG == 32 */ 229 227 230 #if defined(CONFIG_COMPAT) && defined(__ARCH_W << 231 COMPAT_SYSCALL_DEFINE3(truncate64, const char << 232 compat_arg_u64_dual(len << 233 { << 234 return ksys_truncate(pathname, compat_ << 235 } << 236 #endif << 237 << 238 #if defined(CONFIG_COMPAT) && defined(__ARCH_W << 239 COMPAT_SYSCALL_DEFINE3(ftruncate64, unsigned i << 240 compat_arg_u64_dual(len << 241 { << 242 return ksys_ftruncate(fd, compat_arg_u << 243 } << 244 #endif << 245 228 246 int vfs_fallocate(struct file *file, int mode, 229 int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len) 247 { 230 { 248 struct inode *inode = file_inode(file) 231 struct inode *inode = file_inode(file); 249 long ret; 232 long ret; 250 loff_t sum; << 251 233 252 if (offset < 0 || len <= 0) 234 if (offset < 0 || len <= 0) 253 return -EINVAL; 235 return -EINVAL; 254 236 255 /* Return error if mode is not support 237 /* Return error if mode is not supported */ 256 if (mode & ~FALLOC_FL_SUPPORTED_MASK) 238 if (mode & ~FALLOC_FL_SUPPORTED_MASK) 257 return -EOPNOTSUPP; 239 return -EOPNOTSUPP; 258 240 259 /* Punch hole and zero range are mutua 241 /* Punch hole and zero range are mutually exclusive */ 260 if ((mode & (FALLOC_FL_PUNCH_HOLE | FA 242 if ((mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) == 261 (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ 243 (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) 262 return -EOPNOTSUPP; 244 return -EOPNOTSUPP; 263 245 264 /* Punch hole must have keep size set 246 /* Punch hole must have keep size set */ 265 if ((mode & FALLOC_FL_PUNCH_HOLE) && 247 if ((mode & FALLOC_FL_PUNCH_HOLE) && 266 !(mode & FALLOC_FL_KEEP_SIZE)) 248 !(mode & FALLOC_FL_KEEP_SIZE)) 267 return -EOPNOTSUPP; 249 return -EOPNOTSUPP; 268 250 269 /* Collapse range should only be used 251 /* Collapse range should only be used exclusively. */ 270 if ((mode & FALLOC_FL_COLLAPSE_RANGE) 252 if ((mode & FALLOC_FL_COLLAPSE_RANGE) && 271 (mode & ~FALLOC_FL_COLLAPSE_RANGE) 253 (mode & ~FALLOC_FL_COLLAPSE_RANGE)) 272 return -EINVAL; 254 return -EINVAL; 273 255 274 /* Insert range should only be used ex 256 /* Insert range should only be used exclusively. */ 275 if ((mode & FALLOC_FL_INSERT_RANGE) && 257 if ((mode & FALLOC_FL_INSERT_RANGE) && 276 (mode & ~FALLOC_FL_INSERT_RANGE)) 258 (mode & ~FALLOC_FL_INSERT_RANGE)) 277 return -EINVAL; 259 return -EINVAL; 278 260 279 /* Unshare range should only be used w 261 /* Unshare range should only be used with allocate mode. */ 280 if ((mode & FALLOC_FL_UNSHARE_RANGE) & 262 if ((mode & FALLOC_FL_UNSHARE_RANGE) && 281 (mode & ~(FALLOC_FL_UNSHARE_RANGE 263 (mode & ~(FALLOC_FL_UNSHARE_RANGE | FALLOC_FL_KEEP_SIZE))) 282 return -EINVAL; 264 return -EINVAL; 283 265 284 if (!(file->f_mode & FMODE_WRITE)) 266 if (!(file->f_mode & FMODE_WRITE)) 285 return -EBADF; 267 return -EBADF; 286 268 287 /* 269 /* 288 * We can only allow pure fallocate on 270 * We can only allow pure fallocate on append only files 289 */ 271 */ 290 if ((mode & ~FALLOC_FL_KEEP_SIZE) && I 272 if ((mode & ~FALLOC_FL_KEEP_SIZE) && IS_APPEND(inode)) 291 return -EPERM; 273 return -EPERM; 292 274 293 if (IS_IMMUTABLE(inode)) 275 if (IS_IMMUTABLE(inode)) 294 return -EPERM; 276 return -EPERM; 295 277 296 /* 278 /* 297 * We cannot allow any fallocate opera 279 * We cannot allow any fallocate operation on an active swapfile 298 */ 280 */ 299 if (IS_SWAPFILE(inode)) 281 if (IS_SWAPFILE(inode)) 300 return -ETXTBSY; 282 return -ETXTBSY; 301 283 302 /* 284 /* 303 * Revalidate the write permissions, i 285 * Revalidate the write permissions, in case security policy has 304 * changed since the files were opened 286 * changed since the files were opened. 305 */ 287 */ 306 ret = security_file_permission(file, M 288 ret = security_file_permission(file, MAY_WRITE); 307 if (ret) 289 if (ret) 308 return ret; 290 return ret; 309 291 310 ret = fsnotify_file_area_perm(file, MA << 311 if (ret) << 312 return ret; << 313 << 314 if (S_ISFIFO(inode->i_mode)) 292 if (S_ISFIFO(inode->i_mode)) 315 return -ESPIPE; 293 return -ESPIPE; 316 294 317 if (S_ISDIR(inode->i_mode)) 295 if (S_ISDIR(inode->i_mode)) 318 return -EISDIR; 296 return -EISDIR; 319 297 320 if (!S_ISREG(inode->i_mode) && !S_ISBL 298 if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode)) 321 return -ENODEV; 299 return -ENODEV; 322 300 323 /* Check for wraparound */ !! 301 /* Check for wrap through zero too */ 324 if (check_add_overflow(offset, len, &s !! 302 if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0)) 325 return -EFBIG; << 326 << 327 if (sum > inode->i_sb->s_maxbytes) << 328 return -EFBIG; 303 return -EFBIG; 329 304 330 if (!file->f_op->fallocate) 305 if (!file->f_op->fallocate) 331 return -EOPNOTSUPP; 306 return -EOPNOTSUPP; 332 307 333 file_start_write(file); 308 file_start_write(file); 334 ret = file->f_op->fallocate(file, mode 309 ret = file->f_op->fallocate(file, mode, offset, len); 335 310 336 /* 311 /* 337 * Create inotify and fanotify events. 312 * Create inotify and fanotify events. 338 * 313 * 339 * To keep the logic simple always cre 314 * To keep the logic simple always create events if fallocate succeeds. 340 * This implies that events are even c 315 * This implies that events are even created if the file size remains 341 * unchanged, e.g. when using flag FAL 316 * unchanged, e.g. when using flag FALLOC_FL_KEEP_SIZE. 342 */ 317 */ 343 if (ret == 0) 318 if (ret == 0) 344 fsnotify_modify(file); 319 fsnotify_modify(file); 345 320 346 file_end_write(file); 321 file_end_write(file); 347 return ret; 322 return ret; 348 } 323 } 349 EXPORT_SYMBOL_GPL(vfs_fallocate); 324 EXPORT_SYMBOL_GPL(vfs_fallocate); 350 325 351 int ksys_fallocate(int fd, int mode, loff_t of 326 int ksys_fallocate(int fd, int mode, loff_t offset, loff_t len) 352 { 327 { 353 struct fd f = fdget(fd); 328 struct fd f = fdget(fd); 354 int error = -EBADF; 329 int error = -EBADF; 355 330 356 if (f.file) { 331 if (f.file) { 357 error = vfs_fallocate(f.file, 332 error = vfs_fallocate(f.file, mode, offset, len); 358 fdput(f); 333 fdput(f); 359 } 334 } 360 return error; 335 return error; 361 } 336 } 362 337 363 SYSCALL_DEFINE4(fallocate, int, fd, int, mode, 338 SYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len) 364 { 339 { 365 return ksys_fallocate(fd, mode, offset 340 return ksys_fallocate(fd, mode, offset, len); 366 } 341 } 367 342 368 #if defined(CONFIG_COMPAT) && defined(__ARCH_W << 369 COMPAT_SYSCALL_DEFINE6(fallocate, int, fd, int << 370 compat_arg_u64_dual(len << 371 { << 372 return ksys_fallocate(fd, mode, compat << 373 compat_arg_u64_g << 374 } << 375 #endif << 376 << 377 /* 343 /* 378 * access() needs to use the real uid/gid, not 344 * access() needs to use the real uid/gid, not the effective uid/gid. 379 * We do this by temporarily clearing all FS-r 345 * We do this by temporarily clearing all FS-related capabilities and 380 * switching the fsuid/fsgid around to the rea 346 * switching the fsuid/fsgid around to the real ones. 381 * << 382 * Creating new credentials is expensive, so w << 383 * which we can if the result would match what << 384 */ 347 */ 385 static bool access_need_override_creds(int fla << 386 { << 387 const struct cred *cred; << 388 << 389 if (flags & AT_EACCESS) << 390 return false; << 391 << 392 cred = current_cred(); << 393 if (!uid_eq(cred->fsuid, cred->uid) || << 394 !gid_eq(cred->fsgid, cred->gid)) << 395 return true; << 396 << 397 if (!issecure(SECURE_NO_SETUID_FIXUP)) << 398 kuid_t root_uid = make_kuid(cr << 399 if (!uid_eq(cred->uid, root_ui << 400 if (!cap_isclear(cred- << 401 return true; << 402 } else { << 403 if (!cap_isidentical(c << 404 cred->cap_permitte << 405 return true; << 406 } << 407 } << 408 << 409 return false; << 410 } << 411 << 412 static const struct cred *access_override_cred 348 static const struct cred *access_override_creds(void) 413 { 349 { 414 const struct cred *old_cred; 350 const struct cred *old_cred; 415 struct cred *override_cred; 351 struct cred *override_cred; 416 352 417 override_cred = prepare_creds(); 353 override_cred = prepare_creds(); 418 if (!override_cred) 354 if (!override_cred) 419 return NULL; 355 return NULL; 420 356 421 /* << 422 * XXX access_need_override_creds perf << 423 * this work. Make sure it stays in sy << 424 * routine. << 425 */ << 426 << 427 override_cred->fsuid = override_cred-> 357 override_cred->fsuid = override_cred->uid; 428 override_cred->fsgid = override_cred-> 358 override_cred->fsgid = override_cred->gid; 429 359 430 if (!issecure(SECURE_NO_SETUID_FIXUP)) 360 if (!issecure(SECURE_NO_SETUID_FIXUP)) { 431 /* Clear the capabilities if w 361 /* Clear the capabilities if we switch to a non-root user */ 432 kuid_t root_uid = make_kuid(ov 362 kuid_t root_uid = make_kuid(override_cred->user_ns, 0); 433 if (!uid_eq(override_cred->uid 363 if (!uid_eq(override_cred->uid, root_uid)) 434 cap_clear(override_cre 364 cap_clear(override_cred->cap_effective); 435 else 365 else 436 override_cred->cap_eff 366 override_cred->cap_effective = 437 override_cred- 367 override_cred->cap_permitted; 438 } 368 } 439 369 440 /* 370 /* 441 * The new set of credentials can *onl 371 * The new set of credentials can *only* be used in 442 * task-synchronous circumstances, and 372 * task-synchronous circumstances, and does not need 443 * RCU freeing, unless somebody then t 373 * RCU freeing, unless somebody then takes a separate 444 * reference to it. 374 * reference to it. 445 * 375 * 446 * NOTE! This is _only_ true because t 376 * NOTE! This is _only_ true because this credential 447 * is used purely for override_creds() 377 * is used purely for override_creds() that installs 448 * it as the subjective cred. Other th 378 * it as the subjective cred. Other threads will be 449 * accessing ->real_cred, not the subj 379 * accessing ->real_cred, not the subjective cred. 450 * 380 * 451 * If somebody _does_ make a copy of t 381 * If somebody _does_ make a copy of this (using the 452 * 'get_current_cred()' function), tha 382 * 'get_current_cred()' function), that will clear the 453 * non_rcu field, because now that oth 383 * non_rcu field, because now that other user may be 454 * expecting RCU freeing. But normal t 384 * expecting RCU freeing. But normal thread-synchronous 455 * cred accesses will keep things non- !! 385 * cred accesses will keep things non-RCY. 456 * freeing. << 457 */ 386 */ 458 override_cred->non_rcu = 1; 387 override_cred->non_rcu = 1; 459 388 460 old_cred = override_creds(override_cre 389 old_cred = override_creds(override_cred); 461 390 462 /* override_cred() gets its own ref */ 391 /* override_cred() gets its own ref */ 463 put_cred(override_cred); 392 put_cred(override_cred); 464 393 465 return old_cred; 394 return old_cred; 466 } 395 } 467 396 468 static long do_faccessat(int dfd, const char _ 397 static long do_faccessat(int dfd, const char __user *filename, int mode, int flags) 469 { 398 { 470 struct path path; 399 struct path path; 471 struct inode *inode; 400 struct inode *inode; 472 int res; 401 int res; 473 unsigned int lookup_flags = LOOKUP_FOL 402 unsigned int lookup_flags = LOOKUP_FOLLOW; 474 const struct cred *old_cred = NULL; 403 const struct cred *old_cred = NULL; 475 404 476 if (mode & ~S_IRWXO) /* where's F_O 405 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */ 477 return -EINVAL; 406 return -EINVAL; 478 407 479 if (flags & ~(AT_EACCESS | AT_SYMLINK_ 408 if (flags & ~(AT_EACCESS | AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) 480 return -EINVAL; 409 return -EINVAL; 481 410 482 if (flags & AT_SYMLINK_NOFOLLOW) 411 if (flags & AT_SYMLINK_NOFOLLOW) 483 lookup_flags &= ~LOOKUP_FOLLOW 412 lookup_flags &= ~LOOKUP_FOLLOW; 484 if (flags & AT_EMPTY_PATH) 413 if (flags & AT_EMPTY_PATH) 485 lookup_flags |= LOOKUP_EMPTY; 414 lookup_flags |= LOOKUP_EMPTY; 486 415 487 if (access_need_override_creds(flags)) !! 416 if (!(flags & AT_EACCESS)) { 488 old_cred = access_override_cre 417 old_cred = access_override_creds(); 489 if (!old_cred) 418 if (!old_cred) 490 return -ENOMEM; 419 return -ENOMEM; 491 } 420 } 492 421 493 retry: 422 retry: 494 res = user_path_at(dfd, filename, look 423 res = user_path_at(dfd, filename, lookup_flags, &path); 495 if (res) 424 if (res) 496 goto out; 425 goto out; 497 426 498 inode = d_backing_inode(path.dentry); 427 inode = d_backing_inode(path.dentry); 499 428 500 if ((mode & MAY_EXEC) && S_ISREG(inode 429 if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) { 501 /* 430 /* 502 * MAY_EXEC on regular files i 431 * MAY_EXEC on regular files is denied if the fs is mounted 503 * with the "noexec" flag. 432 * with the "noexec" flag. 504 */ 433 */ 505 res = -EACCES; 434 res = -EACCES; 506 if (path_noexec(&path)) 435 if (path_noexec(&path)) 507 goto out_path_release; 436 goto out_path_release; 508 } 437 } 509 438 510 res = inode_permission(mnt_idmap(path. !! 439 res = inode_permission(inode, mode | MAY_ACCESS); 511 /* SuS v2 requires we report a read on 440 /* SuS v2 requires we report a read only fs too */ 512 if (res || !(mode & S_IWOTH) || specia 441 if (res || !(mode & S_IWOTH) || special_file(inode->i_mode)) 513 goto out_path_release; 442 goto out_path_release; 514 /* 443 /* 515 * This is a rare case where using __m 444 * This is a rare case where using __mnt_is_readonly() 516 * is OK without a mnt_want/drop_write 445 * is OK without a mnt_want/drop_write() pair. Since 517 * no actual write to the fs is perfor 446 * no actual write to the fs is performed here, we do 518 * not need to telegraph to that to an 447 * not need to telegraph to that to anyone. 519 * 448 * 520 * By doing this, we accept that this 449 * By doing this, we accept that this access is 521 * inherently racy and know that the f 450 * inherently racy and know that the fs may change 522 * state before we even see this resul 451 * state before we even see this result. 523 */ 452 */ 524 if (__mnt_is_readonly(path.mnt)) 453 if (__mnt_is_readonly(path.mnt)) 525 res = -EROFS; 454 res = -EROFS; 526 455 527 out_path_release: 456 out_path_release: 528 path_put(&path); 457 path_put(&path); 529 if (retry_estale(res, lookup_flags)) { 458 if (retry_estale(res, lookup_flags)) { 530 lookup_flags |= LOOKUP_REVAL; 459 lookup_flags |= LOOKUP_REVAL; 531 goto retry; 460 goto retry; 532 } 461 } 533 out: 462 out: 534 if (old_cred) 463 if (old_cred) 535 revert_creds(old_cred); 464 revert_creds(old_cred); 536 465 537 return res; 466 return res; 538 } 467 } 539 468 540 SYSCALL_DEFINE3(faccessat, int, dfd, const cha 469 SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode) 541 { 470 { 542 return do_faccessat(dfd, filename, mod 471 return do_faccessat(dfd, filename, mode, 0); 543 } 472 } 544 473 545 SYSCALL_DEFINE4(faccessat2, int, dfd, const ch 474 SYSCALL_DEFINE4(faccessat2, int, dfd, const char __user *, filename, int, mode, 546 int, flags) 475 int, flags) 547 { 476 { 548 return do_faccessat(dfd, filename, mod 477 return do_faccessat(dfd, filename, mode, flags); 549 } 478 } 550 479 551 SYSCALL_DEFINE2(access, const char __user *, f 480 SYSCALL_DEFINE2(access, const char __user *, filename, int, mode) 552 { 481 { 553 return do_faccessat(AT_FDCWD, filename 482 return do_faccessat(AT_FDCWD, filename, mode, 0); 554 } 483 } 555 484 556 SYSCALL_DEFINE1(chdir, const char __user *, fi 485 SYSCALL_DEFINE1(chdir, const char __user *, filename) 557 { 486 { 558 struct path path; 487 struct path path; 559 int error; 488 int error; 560 unsigned int lookup_flags = LOOKUP_FOL 489 unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 561 retry: 490 retry: 562 error = user_path_at(AT_FDCWD, filenam 491 error = user_path_at(AT_FDCWD, filename, lookup_flags, &path); 563 if (error) 492 if (error) 564 goto out; 493 goto out; 565 494 566 error = path_permission(&path, MAY_EXE !! 495 error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR); 567 if (error) 496 if (error) 568 goto dput_and_out; 497 goto dput_and_out; 569 498 570 set_fs_pwd(current->fs, &path); 499 set_fs_pwd(current->fs, &path); 571 500 572 dput_and_out: 501 dput_and_out: 573 path_put(&path); 502 path_put(&path); 574 if (retry_estale(error, lookup_flags)) 503 if (retry_estale(error, lookup_flags)) { 575 lookup_flags |= LOOKUP_REVAL; 504 lookup_flags |= LOOKUP_REVAL; 576 goto retry; 505 goto retry; 577 } 506 } 578 out: 507 out: 579 return error; 508 return error; 580 } 509 } 581 510 582 SYSCALL_DEFINE1(fchdir, unsigned int, fd) 511 SYSCALL_DEFINE1(fchdir, unsigned int, fd) 583 { 512 { 584 struct fd f = fdget_raw(fd); 513 struct fd f = fdget_raw(fd); 585 int error; 514 int error; 586 515 587 error = -EBADF; 516 error = -EBADF; 588 if (!f.file) 517 if (!f.file) 589 goto out; 518 goto out; 590 519 591 error = -ENOTDIR; 520 error = -ENOTDIR; 592 if (!d_can_lookup(f.file->f_path.dentr 521 if (!d_can_lookup(f.file->f_path.dentry)) 593 goto out_putf; 522 goto out_putf; 594 523 595 error = file_permission(f.file, MAY_EX !! 524 error = inode_permission(file_inode(f.file), MAY_EXEC | MAY_CHDIR); 596 if (!error) 525 if (!error) 597 set_fs_pwd(current->fs, &f.fil 526 set_fs_pwd(current->fs, &f.file->f_path); 598 out_putf: 527 out_putf: 599 fdput(f); 528 fdput(f); 600 out: 529 out: 601 return error; 530 return error; 602 } 531 } 603 532 604 SYSCALL_DEFINE1(chroot, const char __user *, f 533 SYSCALL_DEFINE1(chroot, const char __user *, filename) 605 { 534 { 606 struct path path; 535 struct path path; 607 int error; 536 int error; 608 unsigned int lookup_flags = LOOKUP_FOL 537 unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 609 retry: 538 retry: 610 error = user_path_at(AT_FDCWD, filenam 539 error = user_path_at(AT_FDCWD, filename, lookup_flags, &path); 611 if (error) 540 if (error) 612 goto out; 541 goto out; 613 542 614 error = path_permission(&path, MAY_EXE !! 543 error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR); 615 if (error) 544 if (error) 616 goto dput_and_out; 545 goto dput_and_out; 617 546 618 error = -EPERM; 547 error = -EPERM; 619 if (!ns_capable(current_user_ns(), CAP 548 if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT)) 620 goto dput_and_out; 549 goto dput_and_out; 621 error = security_path_chroot(&path); 550 error = security_path_chroot(&path); 622 if (error) 551 if (error) 623 goto dput_and_out; 552 goto dput_and_out; 624 553 625 set_fs_root(current->fs, &path); 554 set_fs_root(current->fs, &path); 626 error = 0; 555 error = 0; 627 dput_and_out: 556 dput_and_out: 628 path_put(&path); 557 path_put(&path); 629 if (retry_estale(error, lookup_flags)) 558 if (retry_estale(error, lookup_flags)) { 630 lookup_flags |= LOOKUP_REVAL; 559 lookup_flags |= LOOKUP_REVAL; 631 goto retry; 560 goto retry; 632 } 561 } 633 out: 562 out: 634 return error; 563 return error; 635 } 564 } 636 565 637 int chmod_common(const struct path *path, umod 566 int chmod_common(const struct path *path, umode_t mode) 638 { 567 { 639 struct inode *inode = path->dentry->d_ 568 struct inode *inode = path->dentry->d_inode; 640 struct inode *delegated_inode = NULL; 569 struct inode *delegated_inode = NULL; 641 struct iattr newattrs; 570 struct iattr newattrs; 642 int error; 571 int error; 643 572 644 error = mnt_want_write(path->mnt); 573 error = mnt_want_write(path->mnt); 645 if (error) 574 if (error) 646 return error; 575 return error; 647 retry_deleg: 576 retry_deleg: 648 inode_lock(inode); 577 inode_lock(inode); 649 error = security_path_chmod(path, mode 578 error = security_path_chmod(path, mode); 650 if (error) 579 if (error) 651 goto out_unlock; 580 goto out_unlock; 652 newattrs.ia_mode = (mode & S_IALLUGO) 581 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO); 653 newattrs.ia_valid = ATTR_MODE | ATTR_C 582 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME; 654 error = notify_change(mnt_idmap(path-> !! 583 error = notify_change(path->dentry, &newattrs, &delegated_inode); 655 &newattrs, &dele << 656 out_unlock: 584 out_unlock: 657 inode_unlock(inode); 585 inode_unlock(inode); 658 if (delegated_inode) { 586 if (delegated_inode) { 659 error = break_deleg_wait(&dele 587 error = break_deleg_wait(&delegated_inode); 660 if (!error) 588 if (!error) 661 goto retry_deleg; 589 goto retry_deleg; 662 } 590 } 663 mnt_drop_write(path->mnt); 591 mnt_drop_write(path->mnt); 664 return error; 592 return error; 665 } 593 } 666 594 667 int vfs_fchmod(struct file *file, umode_t mode 595 int vfs_fchmod(struct file *file, umode_t mode) 668 { 596 { 669 audit_file(file); 597 audit_file(file); 670 return chmod_common(&file->f_path, mod 598 return chmod_common(&file->f_path, mode); 671 } 599 } 672 600 673 SYSCALL_DEFINE2(fchmod, unsigned int, fd, umod 601 SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode) 674 { 602 { 675 struct fd f = fdget(fd); 603 struct fd f = fdget(fd); 676 int err = -EBADF; 604 int err = -EBADF; 677 605 678 if (f.file) { 606 if (f.file) { 679 err = vfs_fchmod(f.file, mode) 607 err = vfs_fchmod(f.file, mode); 680 fdput(f); 608 fdput(f); 681 } 609 } 682 return err; 610 return err; 683 } 611 } 684 612 685 static int do_fchmodat(int dfd, const char __u !! 613 static int do_fchmodat(int dfd, const char __user *filename, umode_t mode) 686 unsigned int flags) << 687 { 614 { 688 struct path path; 615 struct path path; 689 int error; 616 int error; 690 unsigned int lookup_flags; !! 617 unsigned int lookup_flags = LOOKUP_FOLLOW; 691 << 692 if (unlikely(flags & ~(AT_SYMLINK_NOFO << 693 return -EINVAL; << 694 << 695 lookup_flags = (flags & AT_SYMLINK_NOF << 696 if (flags & AT_EMPTY_PATH) << 697 lookup_flags |= LOOKUP_EMPTY; << 698 << 699 retry: 618 retry: 700 error = user_path_at(dfd, filename, lo 619 error = user_path_at(dfd, filename, lookup_flags, &path); 701 if (!error) { 620 if (!error) { 702 error = chmod_common(&path, mo 621 error = chmod_common(&path, mode); 703 path_put(&path); 622 path_put(&path); 704 if (retry_estale(error, lookup 623 if (retry_estale(error, lookup_flags)) { 705 lookup_flags |= LOOKUP 624 lookup_flags |= LOOKUP_REVAL; 706 goto retry; 625 goto retry; 707 } 626 } 708 } 627 } 709 return error; 628 return error; 710 } 629 } 711 630 712 SYSCALL_DEFINE4(fchmodat2, int, dfd, const cha << 713 umode_t, mode, unsigned int, f << 714 { << 715 return do_fchmodat(dfd, filename, mode << 716 } << 717 << 718 SYSCALL_DEFINE3(fchmodat, int, dfd, const char 631 SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, 719 umode_t, mode) 632 umode_t, mode) 720 { 633 { 721 return do_fchmodat(dfd, filename, mode !! 634 return do_fchmodat(dfd, filename, mode); 722 } 635 } 723 636 724 SYSCALL_DEFINE2(chmod, const char __user *, fi 637 SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode) 725 { 638 { 726 return do_fchmodat(AT_FDCWD, filename, !! 639 return do_fchmodat(AT_FDCWD, filename, mode); 727 } << 728 << 729 /* << 730 * Check whether @kuid is valid and if so gene << 731 * ia_vfsuid. << 732 * << 733 * Return: true if @kuid is valid, false if no << 734 */ << 735 static inline bool setattr_vfsuid(struct iattr << 736 { << 737 if (!uid_valid(kuid)) << 738 return false; << 739 attr->ia_valid |= ATTR_UID; << 740 attr->ia_vfsuid = VFSUIDT_INIT(kuid); << 741 return true; << 742 } << 743 << 744 /* << 745 * Check whether @kgid is valid and if so gene << 746 * ia_vfsgid. << 747 * << 748 * Return: true if @kgid is valid, false if no << 749 */ << 750 static inline bool setattr_vfsgid(struct iattr << 751 { << 752 if (!gid_valid(kgid)) << 753 return false; << 754 attr->ia_valid |= ATTR_GID; << 755 attr->ia_vfsgid = VFSGIDT_INIT(kgid); << 756 return true; << 757 } 640 } 758 641 759 int chown_common(const struct path *path, uid_ 642 int chown_common(const struct path *path, uid_t user, gid_t group) 760 { 643 { 761 struct mnt_idmap *idmap; << 762 struct user_namespace *fs_userns; << 763 struct inode *inode = path->dentry->d_ 644 struct inode *inode = path->dentry->d_inode; 764 struct inode *delegated_inode = NULL; 645 struct inode *delegated_inode = NULL; 765 int error; 646 int error; 766 struct iattr newattrs; 647 struct iattr newattrs; 767 kuid_t uid; 648 kuid_t uid; 768 kgid_t gid; 649 kgid_t gid; 769 650 770 uid = make_kuid(current_user_ns(), use 651 uid = make_kuid(current_user_ns(), user); 771 gid = make_kgid(current_user_ns(), gro 652 gid = make_kgid(current_user_ns(), group); 772 653 773 idmap = mnt_idmap(path->mnt); << 774 fs_userns = i_user_ns(inode); << 775 << 776 retry_deleg: 654 retry_deleg: 777 newattrs.ia_vfsuid = INVALID_VFSUID; << 778 newattrs.ia_vfsgid = INVALID_VFSGID; << 779 newattrs.ia_valid = ATTR_CTIME; 655 newattrs.ia_valid = ATTR_CTIME; 780 if ((user != (uid_t)-1) && !setattr_vf !! 656 if (user != (uid_t) -1) { 781 return -EINVAL; !! 657 if (!uid_valid(uid)) 782 if ((group != (gid_t)-1) && !setattr_v !! 658 return -EINVAL; 783 return -EINVAL; !! 659 newattrs.ia_valid |= ATTR_UID; 784 inode_lock(inode); !! 660 newattrs.ia_uid = uid; >> 661 } >> 662 if (group != (gid_t) -1) { >> 663 if (!gid_valid(gid)) >> 664 return -EINVAL; >> 665 newattrs.ia_valid |= ATTR_GID; >> 666 newattrs.ia_gid = gid; >> 667 } 785 if (!S_ISDIR(inode->i_mode)) 668 if (!S_ISDIR(inode->i_mode)) 786 newattrs.ia_valid |= ATTR_KILL !! 669 newattrs.ia_valid |= 787 setattr_s !! 670 ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV; 788 /* Continue to send actual fs values, !! 671 inode_lock(inode); 789 error = security_path_chown( !! 672 error = security_path_chown(path, uid, gid); 790 path, << 791 from_vfsuid(idmap, fs_userns, << 792 from_vfsgid(idmap, fs_userns, << 793 if (!error) 673 if (!error) 794 error = notify_change(idmap, p !! 674 error = notify_change(path->dentry, &newattrs, &delegated_inode); 795 &delegat << 796 inode_unlock(inode); 675 inode_unlock(inode); 797 if (delegated_inode) { 676 if (delegated_inode) { 798 error = break_deleg_wait(&dele 677 error = break_deleg_wait(&delegated_inode); 799 if (!error) 678 if (!error) 800 goto retry_deleg; 679 goto retry_deleg; 801 } 680 } 802 return error; 681 return error; 803 } 682 } 804 683 805 int do_fchownat(int dfd, const char __user *fi 684 int do_fchownat(int dfd, const char __user *filename, uid_t user, gid_t group, 806 int flag) 685 int flag) 807 { 686 { 808 struct path path; 687 struct path path; 809 int error = -EINVAL; 688 int error = -EINVAL; 810 int lookup_flags; 689 int lookup_flags; 811 690 812 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT 691 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0) 813 goto out; 692 goto out; 814 693 815 lookup_flags = (flag & AT_SYMLINK_NOFO 694 lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW; 816 if (flag & AT_EMPTY_PATH) 695 if (flag & AT_EMPTY_PATH) 817 lookup_flags |= LOOKUP_EMPTY; 696 lookup_flags |= LOOKUP_EMPTY; 818 retry: 697 retry: 819 error = user_path_at(dfd, filename, lo 698 error = user_path_at(dfd, filename, lookup_flags, &path); 820 if (error) 699 if (error) 821 goto out; 700 goto out; 822 error = mnt_want_write(path.mnt); 701 error = mnt_want_write(path.mnt); 823 if (error) 702 if (error) 824 goto out_release; 703 goto out_release; 825 error = chown_common(&path, user, grou 704 error = chown_common(&path, user, group); 826 mnt_drop_write(path.mnt); 705 mnt_drop_write(path.mnt); 827 out_release: 706 out_release: 828 path_put(&path); 707 path_put(&path); 829 if (retry_estale(error, lookup_flags)) 708 if (retry_estale(error, lookup_flags)) { 830 lookup_flags |= LOOKUP_REVAL; 709 lookup_flags |= LOOKUP_REVAL; 831 goto retry; 710 goto retry; 832 } 711 } 833 out: 712 out: 834 return error; 713 return error; 835 } 714 } 836 715 837 SYSCALL_DEFINE5(fchownat, int, dfd, const char 716 SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user, 838 gid_t, group, int, flag) 717 gid_t, group, int, flag) 839 { 718 { 840 return do_fchownat(dfd, filename, user 719 return do_fchownat(dfd, filename, user, group, flag); 841 } 720 } 842 721 843 SYSCALL_DEFINE3(chown, const char __user *, fi 722 SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group) 844 { 723 { 845 return do_fchownat(AT_FDCWD, filename, 724 return do_fchownat(AT_FDCWD, filename, user, group, 0); 846 } 725 } 847 726 848 SYSCALL_DEFINE3(lchown, const char __user *, f 727 SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group) 849 { 728 { 850 return do_fchownat(AT_FDCWD, filename, 729 return do_fchownat(AT_FDCWD, filename, user, group, 851 AT_SYMLINK_NOFOLLOW 730 AT_SYMLINK_NOFOLLOW); 852 } 731 } 853 732 854 int vfs_fchown(struct file *file, uid_t user, 733 int vfs_fchown(struct file *file, uid_t user, gid_t group) 855 { 734 { 856 int error; 735 int error; 857 736 858 error = mnt_want_write_file(file); 737 error = mnt_want_write_file(file); 859 if (error) 738 if (error) 860 return error; 739 return error; 861 audit_file(file); 740 audit_file(file); 862 error = chown_common(&file->f_path, us 741 error = chown_common(&file->f_path, user, group); 863 mnt_drop_write_file(file); 742 mnt_drop_write_file(file); 864 return error; 743 return error; 865 } 744 } 866 745 867 int ksys_fchown(unsigned int fd, uid_t user, g 746 int ksys_fchown(unsigned int fd, uid_t user, gid_t group) 868 { 747 { 869 struct fd f = fdget(fd); 748 struct fd f = fdget(fd); 870 int error = -EBADF; 749 int error = -EBADF; 871 750 872 if (f.file) { 751 if (f.file) { 873 error = vfs_fchown(f.file, use 752 error = vfs_fchown(f.file, user, group); 874 fdput(f); 753 fdput(f); 875 } 754 } 876 return error; 755 return error; 877 } 756 } 878 757 879 SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_ 758 SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group) 880 { 759 { 881 return ksys_fchown(fd, user, group); 760 return ksys_fchown(fd, user, group); 882 } 761 } 883 762 884 static inline int file_get_write_access(struct << 885 { << 886 int error; << 887 << 888 error = get_write_access(f->f_inode); << 889 if (unlikely(error)) << 890 return error; << 891 error = mnt_get_write_access(f->f_path << 892 if (unlikely(error)) << 893 goto cleanup_inode; << 894 if (unlikely(f->f_mode & FMODE_BACKING << 895 error = mnt_get_write_access(b << 896 if (unlikely(error)) << 897 goto cleanup_mnt; << 898 } << 899 return 0; << 900 << 901 cleanup_mnt: << 902 mnt_put_write_access(f->f_path.mnt); << 903 cleanup_inode: << 904 put_write_access(f->f_inode); << 905 return error; << 906 } << 907 << 908 static int do_dentry_open(struct file *f, 763 static int do_dentry_open(struct file *f, >> 764 struct inode *inode, 909 int (*open)(struct i 765 int (*open)(struct inode *, struct file *)) 910 { 766 { 911 static const struct file_operations em 767 static const struct file_operations empty_fops = {}; 912 struct inode *inode = f->f_path.dentry << 913 int error; 768 int error; 914 769 915 path_get(&f->f_path); 770 path_get(&f->f_path); 916 f->f_inode = inode; 771 f->f_inode = inode; 917 f->f_mapping = inode->i_mapping; 772 f->f_mapping = inode->i_mapping; 918 f->f_wb_err = filemap_sample_wb_err(f- 773 f->f_wb_err = filemap_sample_wb_err(f->f_mapping); 919 f->f_sb_err = file_sample_sb_err(f); 774 f->f_sb_err = file_sample_sb_err(f); 920 775 921 if (unlikely(f->f_flags & O_PATH)) { 776 if (unlikely(f->f_flags & O_PATH)) { 922 f->f_mode = FMODE_PATH | FMODE 777 f->f_mode = FMODE_PATH | FMODE_OPENED; 923 f->f_op = &empty_fops; 778 f->f_op = &empty_fops; 924 return 0; 779 return 0; 925 } 780 } 926 781 927 if ((f->f_mode & (FMODE_READ | FMODE_W !! 782 if (f->f_mode & FMODE_WRITE && !special_file(inode->i_mode)) { 928 i_readcount_inc(inode); !! 783 error = get_write_access(inode); 929 } else if (f->f_mode & FMODE_WRITE && << 930 error = file_get_write_access( << 931 if (unlikely(error)) 784 if (unlikely(error)) 932 goto cleanup_file; 785 goto cleanup_file; >> 786 error = __mnt_want_write(f->f_path.mnt); >> 787 if (unlikely(error)) { >> 788 put_write_access(inode); >> 789 goto cleanup_file; >> 790 } 933 f->f_mode |= FMODE_WRITER; 791 f->f_mode |= FMODE_WRITER; 934 } 792 } 935 793 936 /* POSIX.1-2008/SUSv4 Section XSI 2.9. 794 /* POSIX.1-2008/SUSv4 Section XSI 2.9.7 */ 937 if (S_ISREG(inode->i_mode) || S_ISDIR( 795 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) 938 f->f_mode |= FMODE_ATOMIC_POS; 796 f->f_mode |= FMODE_ATOMIC_POS; 939 797 940 f->f_op = fops_get(inode->i_fop); 798 f->f_op = fops_get(inode->i_fop); 941 if (WARN_ON(!f->f_op)) { 799 if (WARN_ON(!f->f_op)) { 942 error = -ENODEV; 800 error = -ENODEV; 943 goto cleanup_all; 801 goto cleanup_all; 944 } 802 } 945 803 946 error = security_file_open(f); 804 error = security_file_open(f); 947 if (error) 805 if (error) 948 goto cleanup_all; 806 goto cleanup_all; 949 807 950 error = break_lease(file_inode(f), f-> !! 808 error = break_lease(locks_inode(f), f->f_flags); 951 if (error) 809 if (error) 952 goto cleanup_all; 810 goto cleanup_all; 953 811 954 /* normally all 3 are set; ->open() ca 812 /* normally all 3 are set; ->open() can clear them if needed */ 955 f->f_mode |= FMODE_LSEEK | FMODE_PREAD 813 f->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE; 956 if (!open) 814 if (!open) 957 open = f->f_op->open; 815 open = f->f_op->open; 958 if (open) { 816 if (open) { 959 error = open(inode, f); 817 error = open(inode, f); 960 if (error) 818 if (error) 961 goto cleanup_all; 819 goto cleanup_all; 962 } 820 } 963 f->f_mode |= FMODE_OPENED; 821 f->f_mode |= FMODE_OPENED; >> 822 if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) >> 823 i_readcount_inc(inode); 964 if ((f->f_mode & FMODE_READ) && 824 if ((f->f_mode & FMODE_READ) && 965 likely(f->f_op->read || f->f_op-> 825 likely(f->f_op->read || f->f_op->read_iter)) 966 f->f_mode |= FMODE_CAN_READ; 826 f->f_mode |= FMODE_CAN_READ; 967 if ((f->f_mode & FMODE_WRITE) && 827 if ((f->f_mode & FMODE_WRITE) && 968 likely(f->f_op->write || f->f_op- 828 likely(f->f_op->write || f->f_op->write_iter)) 969 f->f_mode |= FMODE_CAN_WRITE; 829 f->f_mode |= FMODE_CAN_WRITE; 970 if ((f->f_mode & FMODE_LSEEK) && !f->f << 971 f->f_mode &= ~FMODE_LSEEK; << 972 if (f->f_mapping->a_ops && f->f_mappin << 973 f->f_mode |= FMODE_CAN_ODIRECT << 974 830 >> 831 f->f_write_hint = WRITE_LIFE_NOT_SET; 975 f->f_flags &= ~(O_CREAT | O_EXCL | O_N 832 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC); 976 f->f_iocb_flags = iocb_flags(f); << 977 833 978 file_ra_state_init(&f->f_ra, f->f_mapp 834 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping); 979 835 980 if ((f->f_flags & O_DIRECT) && !(f->f_ !! 836 /* NB: we're sure to have correct a_ops only after f_op->open */ 981 return -EINVAL; !! 837 if (f->f_flags & O_DIRECT) { >> 838 if (!f->f_mapping->a_ops || !f->f_mapping->a_ops->direct_IO) >> 839 return -EINVAL; >> 840 } 982 841 983 /* 842 /* 984 * XXX: Huge page cache doesn't suppor 843 * XXX: Huge page cache doesn't support writing yet. Drop all page 985 * cache for this file before processi 844 * cache for this file before processing writes. 986 */ 845 */ 987 if (f->f_mode & FMODE_WRITE) { !! 846 if ((f->f_mode & FMODE_WRITE) && filemap_nr_thps(inode->i_mapping)) 988 /* !! 847 truncate_pagecache(inode, 0); 989 * Depends on full fence from << 990 * against collapse_file() reg << 991 * updates. Ensures subsequent << 992 * cache will fail. << 993 */ << 994 if (filemap_nr_thps(inode->i_m << 995 struct address_space * << 996 << 997 filemap_invalidate_loc << 998 /* << 999 * unmap_mapping_range << 1000 * here, because the << 1001 * unmapped mapping ( << 1002 * shared libraries h << 1003 */ << 1004 unmap_mapping_range(m << 1005 truncate_inode_pages( << 1006 filemap_invalidate_un << 1007 } << 1008 } << 1009 848 1010 return 0; 849 return 0; 1011 850 1012 cleanup_all: 851 cleanup_all: 1013 if (WARN_ON_ONCE(error > 0)) 852 if (WARN_ON_ONCE(error > 0)) 1014 error = -EINVAL; 853 error = -EINVAL; 1015 fops_put(f->f_op); 854 fops_put(f->f_op); 1016 put_file_access(f); !! 855 if (f->f_mode & FMODE_WRITER) { >> 856 put_write_access(inode); >> 857 __mnt_drop_write(f->f_path.mnt); >> 858 } 1017 cleanup_file: 859 cleanup_file: 1018 path_put(&f->f_path); 860 path_put(&f->f_path); 1019 f->f_path.mnt = NULL; 861 f->f_path.mnt = NULL; 1020 f->f_path.dentry = NULL; 862 f->f_path.dentry = NULL; 1021 f->f_inode = NULL; 863 f->f_inode = NULL; 1022 return error; 864 return error; 1023 } 865 } 1024 866 1025 /** 867 /** 1026 * finish_open - finish opening a file 868 * finish_open - finish opening a file 1027 * @file: file pointer 869 * @file: file pointer 1028 * @dentry: pointer to dentry 870 * @dentry: pointer to dentry 1029 * @open: open callback 871 * @open: open callback >> 872 * @opened: state of open 1030 * 873 * 1031 * This can be used to finish opening a file 874 * This can be used to finish opening a file passed to i_op->atomic_open(). 1032 * 875 * 1033 * If the open callback is set to NULL, then 876 * If the open callback is set to NULL, then the standard f_op->open() 1034 * filesystem callback is substituted. 877 * filesystem callback is substituted. 1035 * 878 * 1036 * NB: the dentry reference is _not_ consumed 879 * NB: the dentry reference is _not_ consumed. If, for example, the dentry is 1037 * the return value of d_splice_alias(), then 880 * the return value of d_splice_alias(), then the caller needs to perform dput() 1038 * on it after finish_open(). 881 * on it after finish_open(). 1039 * 882 * 1040 * Returns zero on success or -errno if the o 883 * Returns zero on success or -errno if the open failed. 1041 */ 884 */ 1042 int finish_open(struct file *file, struct den 885 int finish_open(struct file *file, struct dentry *dentry, 1043 int (*open)(struct inode *, s 886 int (*open)(struct inode *, struct file *)) 1044 { 887 { 1045 BUG_ON(file->f_mode & FMODE_OPENED); 888 BUG_ON(file->f_mode & FMODE_OPENED); /* once it's opened, it's opened */ 1046 889 1047 file->f_path.dentry = dentry; 890 file->f_path.dentry = dentry; 1048 return do_dentry_open(file, open); !! 891 return do_dentry_open(file, d_backing_inode(dentry), open); 1049 } 892 } 1050 EXPORT_SYMBOL(finish_open); 893 EXPORT_SYMBOL(finish_open); 1051 894 1052 /** 895 /** 1053 * finish_no_open - finish ->atomic_open() wi 896 * finish_no_open - finish ->atomic_open() without opening the file 1054 * 897 * 1055 * @file: file pointer 898 * @file: file pointer 1056 * @dentry: dentry or NULL (as returned from 899 * @dentry: dentry or NULL (as returned from ->lookup()) 1057 * 900 * 1058 * This can be used to set the result of a su 901 * This can be used to set the result of a successful lookup in ->atomic_open(). 1059 * 902 * 1060 * NB: unlike finish_open() this function doe 903 * NB: unlike finish_open() this function does consume the dentry reference and 1061 * the caller need not dput() it. 904 * the caller need not dput() it. 1062 * 905 * 1063 * Returns "" which must be the return value 906 * Returns "" which must be the return value of ->atomic_open() after having 1064 * called this function. 907 * called this function. 1065 */ 908 */ 1066 int finish_no_open(struct file *file, struct 909 int finish_no_open(struct file *file, struct dentry *dentry) 1067 { 910 { 1068 file->f_path.dentry = dentry; 911 file->f_path.dentry = dentry; 1069 return 0; 912 return 0; 1070 } 913 } 1071 EXPORT_SYMBOL(finish_no_open); 914 EXPORT_SYMBOL(finish_no_open); 1072 915 1073 char *file_path(struct file *filp, char *buf, 916 char *file_path(struct file *filp, char *buf, int buflen) 1074 { 917 { 1075 return d_path(&filp->f_path, buf, buf 918 return d_path(&filp->f_path, buf, buflen); 1076 } 919 } 1077 EXPORT_SYMBOL(file_path); 920 EXPORT_SYMBOL(file_path); 1078 921 1079 /** 922 /** 1080 * vfs_open - open the file at the given path 923 * vfs_open - open the file at the given path 1081 * @path: path to open 924 * @path: path to open 1082 * @file: newly allocated file with f_flag in 925 * @file: newly allocated file with f_flag initialized >> 926 * @cred: credentials to use 1083 */ 927 */ 1084 int vfs_open(const struct path *path, struct 928 int vfs_open(const struct path *path, struct file *file) 1085 { 929 { 1086 int ret; << 1087 << 1088 file->f_path = *path; 930 file->f_path = *path; 1089 ret = do_dentry_open(file, NULL); !! 931 return do_dentry_open(file, d_backing_inode(path->dentry), NULL); 1090 if (!ret) { << 1091 /* << 1092 * Once we return a file with << 1093 * fsnotify_close(), so we ne << 1094 * symmetry. << 1095 */ << 1096 fsnotify_open(file); << 1097 } << 1098 return ret; << 1099 } 932 } 1100 933 1101 struct file *dentry_open(const struct path *p 934 struct file *dentry_open(const struct path *path, int flags, 1102 const struct cred *c 935 const struct cred *cred) 1103 { 936 { 1104 int error; 937 int error; 1105 struct file *f; 938 struct file *f; 1106 939 >> 940 validate_creds(cred); >> 941 1107 /* We must always pass in a valid mou 942 /* We must always pass in a valid mount pointer. */ 1108 BUG_ON(!path->mnt); 943 BUG_ON(!path->mnt); 1109 944 1110 f = alloc_empty_file(flags, cred); 945 f = alloc_empty_file(flags, cred); 1111 if (!IS_ERR(f)) { 946 if (!IS_ERR(f)) { 1112 error = vfs_open(path, f); 947 error = vfs_open(path, f); 1113 if (error) { 948 if (error) { 1114 fput(f); 949 fput(f); 1115 f = ERR_PTR(error); 950 f = ERR_PTR(error); 1116 } 951 } 1117 } 952 } 1118 return f; 953 return f; 1119 } 954 } 1120 EXPORT_SYMBOL(dentry_open); 955 EXPORT_SYMBOL(dentry_open); 1121 956 1122 /** !! 957 struct file *open_with_fake_path(const struct path *path, int flags, 1123 * dentry_create - Create and open a file !! 958 struct inode *inode, const struct cred *cred) 1124 * @path: path to create << 1125 * @flags: O_ flags << 1126 * @mode: mode bits for new file << 1127 * @cred: credentials to use << 1128 * << 1129 * Caller must hold the parent directory's lo << 1130 * a negative dentry, placed in @path->dentry << 1131 * << 1132 * Caller sets @path->mnt to the vfsmount of << 1133 * the new file is to be created. The parent << 1134 * negative dentry must reside on the same fi << 1135 * << 1136 * On success, returns a "struct file *". Oth << 1137 * is returned. << 1138 */ << 1139 struct file *dentry_create(const struct path << 1140 const struct cred << 1141 { 959 { 1142 struct file *f; !! 960 struct file *f = alloc_empty_file_noaccount(flags, cred); 1143 int error; !! 961 if (!IS_ERR(f)) { 1144 !! 962 int error; 1145 f = alloc_empty_file(flags, cred); << 1146 if (IS_ERR(f)) << 1147 return f; << 1148 << 1149 error = vfs_create(mnt_idmap(path->mn << 1150 d_inode(path->dent << 1151 path->dentry, mode << 1152 if (!error) << 1153 error = vfs_open(path, f); << 1154 << 1155 if (unlikely(error)) { << 1156 fput(f); << 1157 return ERR_PTR(error); << 1158 } << 1159 return f; << 1160 } << 1161 EXPORT_SYMBOL(dentry_create); << 1162 << 1163 /** << 1164 * kernel_file_open - open a file for kernel << 1165 * @path: path of the file to open << 1166 * @flags: open flags << 1167 * @cred: credentials for open << 1168 * << 1169 * Open a file for use by in-kernel consumers << 1170 * against nr_files and must not be installed << 1171 * table. << 1172 * << 1173 * Return: Opened file on success, an error p << 1174 */ << 1175 struct file *kernel_file_open(const struct pa << 1176 const struct << 1177 { << 1178 struct file *f; << 1179 int error; << 1180 963 1181 f = alloc_empty_file_noaccount(flags, !! 964 f->f_path = *path; 1182 if (IS_ERR(f)) !! 965 error = do_dentry_open(f, inode, NULL); 1183 return f; !! 966 if (error) { 1184 !! 967 fput(f); 1185 f->f_path = *path; !! 968 f = ERR_PTR(error); 1186 error = do_dentry_open(f, NULL); !! 969 } 1187 if (error) { << 1188 fput(f); << 1189 return ERR_PTR(error); << 1190 } 970 } 1191 << 1192 fsnotify_open(f); << 1193 return f; 971 return f; 1194 } 972 } 1195 EXPORT_SYMBOL_GPL(kernel_file_open); !! 973 EXPORT_SYMBOL(open_with_fake_path); 1196 974 1197 #define WILL_CREATE(flags) (flags & (O_C 975 #define WILL_CREATE(flags) (flags & (O_CREAT | __O_TMPFILE)) 1198 #define O_PATH_FLAGS (O_DIRECTORY 976 #define O_PATH_FLAGS (O_DIRECTORY | O_NOFOLLOW | O_PATH | O_CLOEXEC) 1199 977 1200 inline struct open_how build_open_how(int fla 978 inline struct open_how build_open_how(int flags, umode_t mode) 1201 { 979 { 1202 struct open_how how = { 980 struct open_how how = { 1203 .flags = flags & VALID_OPEN_F 981 .flags = flags & VALID_OPEN_FLAGS, 1204 .mode = mode & S_IALLUGO, 982 .mode = mode & S_IALLUGO, 1205 }; 983 }; 1206 984 1207 /* O_PATH beats everything else. */ 985 /* O_PATH beats everything else. */ 1208 if (how.flags & O_PATH) 986 if (how.flags & O_PATH) 1209 how.flags &= O_PATH_FLAGS; 987 how.flags &= O_PATH_FLAGS; 1210 /* Modes should only be set for creat 988 /* Modes should only be set for create-like flags. */ 1211 if (!WILL_CREATE(how.flags)) 989 if (!WILL_CREATE(how.flags)) 1212 how.mode = 0; 990 how.mode = 0; 1213 return how; 991 return how; 1214 } 992 } 1215 993 1216 inline int build_open_flags(const struct open 994 inline int build_open_flags(const struct open_how *how, struct open_flags *op) 1217 { 995 { 1218 u64 flags = how->flags; !! 996 int flags = how->flags; 1219 u64 strip = __FMODE_NONOTIFY | O_CLOE << 1220 int lookup_flags = 0; 997 int lookup_flags = 0; 1221 int acc_mode = ACC_MODE(flags); 998 int acc_mode = ACC_MODE(flags); 1222 999 1223 BUILD_BUG_ON_MSG(upper_32_bits(VALID_ !! 1000 /* Must never be set by userspace */ 1224 "struct open_flags d !! 1001 flags &= ~(FMODE_NONOTIFY | O_CLOEXEC); 1225 << 1226 /* << 1227 * Strip flags that either shouldn't << 1228 * FMODE_NONOTIFY or that aren't rele << 1229 * open_flags like O_CLOEXEC. << 1230 */ << 1231 flags &= ~strip; << 1232 1002 1233 /* 1003 /* 1234 * Older syscalls implicitly clear al 1004 * Older syscalls implicitly clear all of the invalid flags or argument 1235 * values before calling build_open_f 1005 * values before calling build_open_flags(), but openat2(2) checks all 1236 * of its arguments. 1006 * of its arguments. 1237 */ 1007 */ 1238 if (flags & ~VALID_OPEN_FLAGS) 1008 if (flags & ~VALID_OPEN_FLAGS) 1239 return -EINVAL; 1009 return -EINVAL; 1240 if (how->resolve & ~VALID_RESOLVE_FLA 1010 if (how->resolve & ~VALID_RESOLVE_FLAGS) 1241 return -EINVAL; 1011 return -EINVAL; 1242 1012 1243 /* Scoping flags are mutually exclusi 1013 /* Scoping flags are mutually exclusive. */ 1244 if ((how->resolve & RESOLVE_BENEATH) 1014 if ((how->resolve & RESOLVE_BENEATH) && (how->resolve & RESOLVE_IN_ROOT)) 1245 return -EINVAL; 1015 return -EINVAL; 1246 1016 1247 /* Deal with the mode. */ 1017 /* Deal with the mode. */ 1248 if (WILL_CREATE(flags)) { 1018 if (WILL_CREATE(flags)) { 1249 if (how->mode & ~S_IALLUGO) 1019 if (how->mode & ~S_IALLUGO) 1250 return -EINVAL; 1020 return -EINVAL; 1251 op->mode = how->mode | S_IFRE 1021 op->mode = how->mode | S_IFREG; 1252 } else { 1022 } else { 1253 if (how->mode != 0) 1023 if (how->mode != 0) 1254 return -EINVAL; 1024 return -EINVAL; 1255 op->mode = 0; 1025 op->mode = 0; 1256 } 1026 } 1257 1027 1258 /* 1028 /* 1259 * Block bugs where O_DIRECTORY | O_C !! 1029 * In order to ensure programs get explicit errors when trying to use 1260 * Note, that blocking O_DIRECTORY | !! 1030 * O_TMPFILE on old kernels, O_TMPFILE is implemented such that it 1261 * O_TMPFILE below which requires O_D !! 1031 * looks like (O_DIRECTORY|O_RDWR & ~O_CREAT) to old kernels. But we >> 1032 * have to require userspace to explicitly set it. 1262 */ 1033 */ 1263 if ((flags & (O_DIRECTORY | O_CREAT)) << 1264 return -EINVAL; << 1265 << 1266 /* Now handle the creative implementa << 1267 if (flags & __O_TMPFILE) { 1034 if (flags & __O_TMPFILE) { 1268 /* !! 1035 if ((flags & O_TMPFILE_MASK) != O_TMPFILE) 1269 * In order to ensure program << 1270 * to use O_TMPFILE on old ke << 1271 * is raised alongside __O_TM << 1272 */ << 1273 if (!(flags & O_DIRECTORY)) << 1274 return -EINVAL; 1036 return -EINVAL; 1275 if (!(acc_mode & MAY_WRITE)) 1037 if (!(acc_mode & MAY_WRITE)) 1276 return -EINVAL; 1038 return -EINVAL; 1277 } 1039 } 1278 if (flags & O_PATH) { 1040 if (flags & O_PATH) { 1279 /* O_PATH only permits certai 1041 /* O_PATH only permits certain other flags to be set. */ 1280 if (flags & ~O_PATH_FLAGS) 1042 if (flags & ~O_PATH_FLAGS) 1281 return -EINVAL; 1043 return -EINVAL; 1282 acc_mode = 0; 1044 acc_mode = 0; 1283 } 1045 } 1284 1046 1285 /* 1047 /* 1286 * O_SYNC is implemented as __O_SYNC| 1048 * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only 1287 * check for O_DSYNC if the need any 1049 * check for O_DSYNC if the need any syncing at all we enforce it's 1288 * always set instead of having to de 1050 * always set instead of having to deal with possibly weird behaviour 1289 * for malicious applications setting 1051 * for malicious applications setting only __O_SYNC. 1290 */ 1052 */ 1291 if (flags & __O_SYNC) 1053 if (flags & __O_SYNC) 1292 flags |= O_DSYNC; 1054 flags |= O_DSYNC; 1293 1055 1294 op->open_flag = flags; 1056 op->open_flag = flags; 1295 1057 1296 /* O_TRUNC implies we need access che 1058 /* O_TRUNC implies we need access checks for write permissions */ 1297 if (flags & O_TRUNC) 1059 if (flags & O_TRUNC) 1298 acc_mode |= MAY_WRITE; 1060 acc_mode |= MAY_WRITE; 1299 1061 1300 /* Allow the LSM permission hook to d 1062 /* Allow the LSM permission hook to distinguish append 1301 access from general write access. 1063 access from general write access. */ 1302 if (flags & O_APPEND) 1064 if (flags & O_APPEND) 1303 acc_mode |= MAY_APPEND; 1065 acc_mode |= MAY_APPEND; 1304 1066 1305 op->acc_mode = acc_mode; 1067 op->acc_mode = acc_mode; 1306 1068 1307 op->intent = flags & O_PATH ? 0 : LOO 1069 op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN; 1308 1070 1309 if (flags & O_CREAT) { 1071 if (flags & O_CREAT) { 1310 op->intent |= LOOKUP_CREATE; 1072 op->intent |= LOOKUP_CREATE; 1311 if (flags & O_EXCL) { 1073 if (flags & O_EXCL) { 1312 op->intent |= LOOKUP_ 1074 op->intent |= LOOKUP_EXCL; 1313 flags |= O_NOFOLLOW; 1075 flags |= O_NOFOLLOW; 1314 } 1076 } 1315 } 1077 } 1316 1078 1317 if (flags & O_DIRECTORY) 1079 if (flags & O_DIRECTORY) 1318 lookup_flags |= LOOKUP_DIRECT 1080 lookup_flags |= LOOKUP_DIRECTORY; 1319 if (!(flags & O_NOFOLLOW)) 1081 if (!(flags & O_NOFOLLOW)) 1320 lookup_flags |= LOOKUP_FOLLOW 1082 lookup_flags |= LOOKUP_FOLLOW; 1321 1083 1322 if (how->resolve & RESOLVE_NO_XDEV) 1084 if (how->resolve & RESOLVE_NO_XDEV) 1323 lookup_flags |= LOOKUP_NO_XDE 1085 lookup_flags |= LOOKUP_NO_XDEV; 1324 if (how->resolve & RESOLVE_NO_MAGICLI 1086 if (how->resolve & RESOLVE_NO_MAGICLINKS) 1325 lookup_flags |= LOOKUP_NO_MAG 1087 lookup_flags |= LOOKUP_NO_MAGICLINKS; 1326 if (how->resolve & RESOLVE_NO_SYMLINK 1088 if (how->resolve & RESOLVE_NO_SYMLINKS) 1327 lookup_flags |= LOOKUP_NO_SYM 1089 lookup_flags |= LOOKUP_NO_SYMLINKS; 1328 if (how->resolve & RESOLVE_BENEATH) 1090 if (how->resolve & RESOLVE_BENEATH) 1329 lookup_flags |= LOOKUP_BENEAT 1091 lookup_flags |= LOOKUP_BENEATH; 1330 if (how->resolve & RESOLVE_IN_ROOT) 1092 if (how->resolve & RESOLVE_IN_ROOT) 1331 lookup_flags |= LOOKUP_IN_ROO 1093 lookup_flags |= LOOKUP_IN_ROOT; 1332 if (how->resolve & RESOLVE_CACHED) { << 1333 /* Don't bother even trying f << 1334 if (flags & (O_TRUNC | O_CREA << 1335 return -EAGAIN; << 1336 lookup_flags |= LOOKUP_CACHED << 1337 } << 1338 1094 1339 op->lookup_flags = lookup_flags; 1095 op->lookup_flags = lookup_flags; 1340 return 0; 1096 return 0; 1341 } 1097 } 1342 1098 1343 /** 1099 /** 1344 * file_open_name - open file and return file 1100 * file_open_name - open file and return file pointer 1345 * 1101 * 1346 * @name: struct filename containing pa 1102 * @name: struct filename containing path to open 1347 * @flags: open flags as per the open(2) 1103 * @flags: open flags as per the open(2) second argument 1348 * @mode: mode for the new file if O_CR 1104 * @mode: mode for the new file if O_CREAT is set, else ignored 1349 * 1105 * 1350 * This is the helper to open a file from ker 1106 * This is the helper to open a file from kernelspace if you really 1351 * have to. But in generally you should not 1107 * have to. But in generally you should not do this, so please move 1352 * along, nothing to see here.. 1108 * along, nothing to see here.. 1353 */ 1109 */ 1354 struct file *file_open_name(struct filename * 1110 struct file *file_open_name(struct filename *name, int flags, umode_t mode) 1355 { 1111 { 1356 struct open_flags op; 1112 struct open_flags op; 1357 struct open_how how = build_open_how( 1113 struct open_how how = build_open_how(flags, mode); 1358 int err = build_open_flags(&how, &op) 1114 int err = build_open_flags(&how, &op); 1359 if (err) 1115 if (err) 1360 return ERR_PTR(err); 1116 return ERR_PTR(err); 1361 return do_filp_open(AT_FDCWD, name, & 1117 return do_filp_open(AT_FDCWD, name, &op); 1362 } 1118 } 1363 1119 1364 /** 1120 /** 1365 * filp_open - open file and return file poin 1121 * filp_open - open file and return file pointer 1366 * 1122 * 1367 * @filename: path to open 1123 * @filename: path to open 1368 * @flags: open flags as per the open(2) 1124 * @flags: open flags as per the open(2) second argument 1369 * @mode: mode for the new file if O_CR 1125 * @mode: mode for the new file if O_CREAT is set, else ignored 1370 * 1126 * 1371 * This is the helper to open a file from ker 1127 * This is the helper to open a file from kernelspace if you really 1372 * have to. But in generally you should not 1128 * have to. But in generally you should not do this, so please move 1373 * along, nothing to see here.. 1129 * along, nothing to see here.. 1374 */ 1130 */ 1375 struct file *filp_open(const char *filename, 1131 struct file *filp_open(const char *filename, int flags, umode_t mode) 1376 { 1132 { 1377 struct filename *name = getname_kerne 1133 struct filename *name = getname_kernel(filename); 1378 struct file *file = ERR_CAST(name); 1134 struct file *file = ERR_CAST(name); 1379 !! 1135 1380 if (!IS_ERR(name)) { 1136 if (!IS_ERR(name)) { 1381 file = file_open_name(name, f 1137 file = file_open_name(name, flags, mode); 1382 putname(name); 1138 putname(name); 1383 } 1139 } 1384 return file; 1140 return file; 1385 } 1141 } 1386 EXPORT_SYMBOL(filp_open); 1142 EXPORT_SYMBOL(filp_open); 1387 1143 1388 struct file *file_open_root(const struct path !! 1144 struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt, 1389 const char *filen 1145 const char *filename, int flags, umode_t mode) 1390 { 1146 { 1391 struct open_flags op; 1147 struct open_flags op; 1392 struct open_how how = build_open_how( 1148 struct open_how how = build_open_how(flags, mode); 1393 int err = build_open_flags(&how, &op) 1149 int err = build_open_flags(&how, &op); 1394 if (err) 1150 if (err) 1395 return ERR_PTR(err); 1151 return ERR_PTR(err); 1396 return do_file_open_root(root, filena !! 1152 return do_file_open_root(dentry, mnt, filename, &op); 1397 } 1153 } 1398 EXPORT_SYMBOL(file_open_root); 1154 EXPORT_SYMBOL(file_open_root); 1399 1155 1400 static long do_sys_openat2(int dfd, const cha 1156 static long do_sys_openat2(int dfd, const char __user *filename, 1401 struct open_how *h 1157 struct open_how *how) 1402 { 1158 { 1403 struct open_flags op; 1159 struct open_flags op; 1404 int fd = build_open_flags(how, &op); 1160 int fd = build_open_flags(how, &op); 1405 struct filename *tmp; 1161 struct filename *tmp; 1406 1162 1407 if (fd) 1163 if (fd) 1408 return fd; 1164 return fd; 1409 1165 1410 tmp = getname(filename); 1166 tmp = getname(filename); 1411 if (IS_ERR(tmp)) 1167 if (IS_ERR(tmp)) 1412 return PTR_ERR(tmp); 1168 return PTR_ERR(tmp); 1413 1169 1414 fd = get_unused_fd_flags(how->flags); 1170 fd = get_unused_fd_flags(how->flags); 1415 if (fd >= 0) { 1171 if (fd >= 0) { 1416 struct file *f = do_filp_open 1172 struct file *f = do_filp_open(dfd, tmp, &op); 1417 if (IS_ERR(f)) { 1173 if (IS_ERR(f)) { 1418 put_unused_fd(fd); 1174 put_unused_fd(fd); 1419 fd = PTR_ERR(f); 1175 fd = PTR_ERR(f); 1420 } else { 1176 } else { >> 1177 fsnotify_open(f); 1421 fd_install(fd, f); 1178 fd_install(fd, f); 1422 } 1179 } 1423 } 1180 } 1424 putname(tmp); 1181 putname(tmp); 1425 return fd; 1182 return fd; 1426 } 1183 } 1427 1184 1428 long do_sys_open(int dfd, const char __user * 1185 long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode) 1429 { 1186 { 1430 struct open_how how = build_open_how( 1187 struct open_how how = build_open_how(flags, mode); 1431 return do_sys_openat2(dfd, filename, 1188 return do_sys_openat2(dfd, filename, &how); 1432 } 1189 } 1433 1190 1434 1191 1435 SYSCALL_DEFINE3(open, const char __user *, fi 1192 SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode) 1436 { 1193 { 1437 if (force_o_largefile()) 1194 if (force_o_largefile()) 1438 flags |= O_LARGEFILE; 1195 flags |= O_LARGEFILE; 1439 return do_sys_open(AT_FDCWD, filename 1196 return do_sys_open(AT_FDCWD, filename, flags, mode); 1440 } 1197 } 1441 1198 1442 SYSCALL_DEFINE4(openat, int, dfd, const char 1199 SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, 1443 umode_t, mode) 1200 umode_t, mode) 1444 { 1201 { 1445 if (force_o_largefile()) 1202 if (force_o_largefile()) 1446 flags |= O_LARGEFILE; 1203 flags |= O_LARGEFILE; 1447 return do_sys_open(dfd, filename, fla 1204 return do_sys_open(dfd, filename, flags, mode); 1448 } 1205 } 1449 1206 1450 SYSCALL_DEFINE4(openat2, int, dfd, const char 1207 SYSCALL_DEFINE4(openat2, int, dfd, const char __user *, filename, 1451 struct open_how __user *, how 1208 struct open_how __user *, how, size_t, usize) 1452 { 1209 { 1453 int err; 1210 int err; 1454 struct open_how tmp; 1211 struct open_how tmp; 1455 1212 1456 BUILD_BUG_ON(sizeof(struct open_how) 1213 BUILD_BUG_ON(sizeof(struct open_how) < OPEN_HOW_SIZE_VER0); 1457 BUILD_BUG_ON(sizeof(struct open_how) 1214 BUILD_BUG_ON(sizeof(struct open_how) != OPEN_HOW_SIZE_LATEST); 1458 1215 1459 if (unlikely(usize < OPEN_HOW_SIZE_VE 1216 if (unlikely(usize < OPEN_HOW_SIZE_VER0)) 1460 return -EINVAL; 1217 return -EINVAL; 1461 1218 1462 err = copy_struct_from_user(&tmp, siz 1219 err = copy_struct_from_user(&tmp, sizeof(tmp), how, usize); 1463 if (err) 1220 if (err) 1464 return err; 1221 return err; 1465 1222 1466 audit_openat2_how(&tmp); << 1467 << 1468 /* O_LARGEFILE is only allowed for no 1223 /* O_LARGEFILE is only allowed for non-O_PATH. */ 1469 if (!(tmp.flags & O_PATH) && force_o_ 1224 if (!(tmp.flags & O_PATH) && force_o_largefile()) 1470 tmp.flags |= O_LARGEFILE; 1225 tmp.flags |= O_LARGEFILE; 1471 1226 1472 return do_sys_openat2(dfd, filename, 1227 return do_sys_openat2(dfd, filename, &tmp); 1473 } 1228 } 1474 1229 1475 #ifdef CONFIG_COMPAT 1230 #ifdef CONFIG_COMPAT 1476 /* 1231 /* 1477 * Exactly like sys_open(), except that it do 1232 * Exactly like sys_open(), except that it doesn't set the 1478 * O_LARGEFILE flag. 1233 * O_LARGEFILE flag. 1479 */ 1234 */ 1480 COMPAT_SYSCALL_DEFINE3(open, const char __use 1235 COMPAT_SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode) 1481 { 1236 { 1482 return do_sys_open(AT_FDCWD, filename 1237 return do_sys_open(AT_FDCWD, filename, flags, mode); 1483 } 1238 } 1484 1239 1485 /* 1240 /* 1486 * Exactly like sys_openat(), except that it 1241 * Exactly like sys_openat(), except that it doesn't set the 1487 * O_LARGEFILE flag. 1242 * O_LARGEFILE flag. 1488 */ 1243 */ 1489 COMPAT_SYSCALL_DEFINE4(openat, int, dfd, cons 1244 COMPAT_SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, umode_t, mode) 1490 { 1245 { 1491 return do_sys_open(dfd, filename, fla 1246 return do_sys_open(dfd, filename, flags, mode); 1492 } 1247 } 1493 #endif 1248 #endif 1494 1249 1495 #ifndef __alpha__ 1250 #ifndef __alpha__ 1496 1251 1497 /* 1252 /* 1498 * For backward compatibility? Maybe this sh 1253 * For backward compatibility? Maybe this should be moved 1499 * into arch/i386 instead? 1254 * into arch/i386 instead? 1500 */ 1255 */ 1501 SYSCALL_DEFINE2(creat, const char __user *, p 1256 SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode) 1502 { 1257 { 1503 int flags = O_CREAT | O_WRONLY | O_TR 1258 int flags = O_CREAT | O_WRONLY | O_TRUNC; 1504 1259 1505 if (force_o_largefile()) 1260 if (force_o_largefile()) 1506 flags |= O_LARGEFILE; 1261 flags |= O_LARGEFILE; 1507 return do_sys_open(AT_FDCWD, pathname 1262 return do_sys_open(AT_FDCWD, pathname, flags, mode); 1508 } 1263 } 1509 #endif 1264 #endif 1510 1265 1511 /* 1266 /* 1512 * "id" is the POSIX thread ID. We use the 1267 * "id" is the POSIX thread ID. We use the 1513 * files pointer for this.. 1268 * files pointer for this.. 1514 */ 1269 */ 1515 static int filp_flush(struct file *filp, fl_o !! 1270 int filp_close(struct file *filp, fl_owner_t id) 1516 { 1271 { 1517 int retval = 0; 1272 int retval = 0; 1518 1273 1519 if (CHECK_DATA_CORRUPTION(file_count( !! 1274 if (!file_count(filp)) { 1520 "VFS: Close: file cou !! 1275 printk(KERN_ERR "VFS: Close: file count is 0\n"); 1521 filp->f_op)) { << 1522 return 0; 1276 return 0; 1523 } 1277 } 1524 1278 1525 if (filp->f_op->flush) 1279 if (filp->f_op->flush) 1526 retval = filp->f_op->flush(fi 1280 retval = filp->f_op->flush(filp, id); 1527 1281 1528 if (likely(!(filp->f_mode & FMODE_PAT 1282 if (likely(!(filp->f_mode & FMODE_PATH))) { 1529 dnotify_flush(filp, id); 1283 dnotify_flush(filp, id); 1530 locks_remove_posix(filp, id); 1284 locks_remove_posix(filp, id); 1531 } 1285 } 1532 return retval; << 1533 } << 1534 << 1535 int filp_close(struct file *filp, fl_owner_t << 1536 { << 1537 int retval; << 1538 << 1539 retval = filp_flush(filp, id); << 1540 fput(filp); 1286 fput(filp); 1541 << 1542 return retval; 1287 return retval; 1543 } 1288 } >> 1289 1544 EXPORT_SYMBOL(filp_close); 1290 EXPORT_SYMBOL(filp_close); 1545 1291 1546 /* 1292 /* 1547 * Careful here! We test whether the file poi 1293 * Careful here! We test whether the file pointer is NULL before 1548 * releasing the fd. This ensures that one cl 1294 * releasing the fd. This ensures that one clone task can't release 1549 * an fd while another clone is opening it. 1295 * an fd while another clone is opening it. 1550 */ 1296 */ 1551 SYSCALL_DEFINE1(close, unsigned int, fd) 1297 SYSCALL_DEFINE1(close, unsigned int, fd) 1552 { 1298 { 1553 int retval; !! 1299 int retval = close_fd(fd); 1554 struct file *file; << 1555 << 1556 file = file_close_fd(fd); << 1557 if (!file) << 1558 return -EBADF; << 1559 << 1560 retval = filp_flush(file, current->fi << 1561 << 1562 /* << 1563 * We're returning to user space. Don << 1564 * with any delayed fput() cases. << 1565 */ << 1566 __fput_sync(file); << 1567 1300 1568 /* can't restart close syscall becaus 1301 /* can't restart close syscall because file table entry was cleared */ 1569 if (unlikely(retval == -ERESTARTSYS | 1302 if (unlikely(retval == -ERESTARTSYS || 1570 retval == -ERESTARTNOINT 1303 retval == -ERESTARTNOINTR || 1571 retval == -ERESTARTNOHAN 1304 retval == -ERESTARTNOHAND || 1572 retval == -ERESTART_REST 1305 retval == -ERESTART_RESTARTBLOCK)) 1573 retval = -EINTR; 1306 retval = -EINTR; 1574 1307 1575 return retval; 1308 return retval; 1576 } 1309 } 1577 1310 1578 /** 1311 /** 1579 * sys_close_range() - Close all file descrip !! 1312 * close_range() - Close all file descriptors in a given range. 1580 * 1313 * 1581 * @fd: starting file descriptor to close 1314 * @fd: starting file descriptor to close 1582 * @max_fd: last file descriptor to close 1315 * @max_fd: last file descriptor to close 1583 * @flags: reserved for future extensions 1316 * @flags: reserved for future extensions 1584 * 1317 * 1585 * This closes a range of file descriptors. A 1318 * This closes a range of file descriptors. All file descriptors 1586 * from @fd up to and including @max_fd are c 1319 * from @fd up to and including @max_fd are closed. 1587 * Currently, errors to close a given file de 1320 * Currently, errors to close a given file descriptor are ignored. 1588 */ 1321 */ 1589 SYSCALL_DEFINE3(close_range, unsigned int, fd 1322 SYSCALL_DEFINE3(close_range, unsigned int, fd, unsigned int, max_fd, 1590 unsigned int, flags) 1323 unsigned int, flags) 1591 { 1324 { 1592 return __close_range(fd, max_fd, flag 1325 return __close_range(fd, max_fd, flags); 1593 } 1326 } 1594 1327 1595 /* 1328 /* 1596 * This routine simulates a hangup on the tty 1329 * This routine simulates a hangup on the tty, to arrange that users 1597 * are given clean terminals at login time. 1330 * are given clean terminals at login time. 1598 */ 1331 */ 1599 SYSCALL_DEFINE0(vhangup) 1332 SYSCALL_DEFINE0(vhangup) 1600 { 1333 { 1601 if (!ccs_capable(CCS_SYS_VHANGUP)) 1334 if (!ccs_capable(CCS_SYS_VHANGUP)) 1602 return -EPERM; 1335 return -EPERM; 1603 if (capable(CAP_SYS_TTY_CONFIG)) { 1336 if (capable(CAP_SYS_TTY_CONFIG)) { 1604 tty_vhangup_self(); 1337 tty_vhangup_self(); 1605 return 0; 1338 return 0; 1606 } 1339 } 1607 return -EPERM; 1340 return -EPERM; 1608 } 1341 } 1609 1342 1610 /* 1343 /* 1611 * Called when an inode is about to be open. 1344 * Called when an inode is about to be open. 1612 * We use this to disallow opening large file 1345 * We use this to disallow opening large files on 32bit systems if 1613 * the caller didn't specify O_LARGEFILE. On 1346 * the caller didn't specify O_LARGEFILE. On 64bit systems we force 1614 * on this flag in sys_open. 1347 * on this flag in sys_open. 1615 */ 1348 */ 1616 int generic_file_open(struct inode * inode, s 1349 int generic_file_open(struct inode * inode, struct file * filp) 1617 { 1350 { 1618 if (!(filp->f_flags & O_LARGEFILE) && 1351 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS) 1619 return -EOVERFLOW; 1352 return -EOVERFLOW; 1620 return 0; 1353 return 0; 1621 } 1354 } 1622 1355 1623 EXPORT_SYMBOL(generic_file_open); 1356 EXPORT_SYMBOL(generic_file_open); 1624 1357 1625 /* 1358 /* 1626 * This is used by subsystems that don't want 1359 * This is used by subsystems that don't want seekable 1627 * file descriptors. The function is not supp 1360 * file descriptors. The function is not supposed to ever fail, the only 1628 * reason it returns an 'int' and not 'void' 1361 * reason it returns an 'int' and not 'void' is so that it can be plugged 1629 * directly into file_operations structure. 1362 * directly into file_operations structure. 1630 */ 1363 */ 1631 int nonseekable_open(struct inode *inode, str 1364 int nonseekable_open(struct inode *inode, struct file *filp) 1632 { 1365 { 1633 filp->f_mode &= ~(FMODE_LSEEK | FMODE 1366 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE); 1634 return 0; 1367 return 0; 1635 } 1368 } 1636 1369 1637 EXPORT_SYMBOL(nonseekable_open); 1370 EXPORT_SYMBOL(nonseekable_open); 1638 1371 1639 /* 1372 /* 1640 * stream_open is used by subsystems that wan 1373 * stream_open is used by subsystems that want stream-like file descriptors. 1641 * Such file descriptors are not seekable and 1374 * Such file descriptors are not seekable and don't have notion of position 1642 * (file.f_pos is always 0 and ppos passed to 1375 * (file.f_pos is always 0 and ppos passed to .read()/.write() is always NULL). 1643 * Contrary to file descriptors of other regu 1376 * Contrary to file descriptors of other regular files, .read() and .write() 1644 * can run simultaneously. 1377 * can run simultaneously. 1645 * 1378 * 1646 * stream_open never fails and is marked to r 1379 * stream_open never fails and is marked to return int so that it could be 1647 * directly used as file_operations.open . 1380 * directly used as file_operations.open . 1648 */ 1381 */ 1649 int stream_open(struct inode *inode, struct f 1382 int stream_open(struct inode *inode, struct file *filp) 1650 { 1383 { 1651 filp->f_mode &= ~(FMODE_LSEEK | FMODE 1384 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE | FMODE_ATOMIC_POS); 1652 filp->f_mode |= FMODE_STREAM; 1385 filp->f_mode |= FMODE_STREAM; 1653 return 0; 1386 return 0; 1654 } 1387 } 1655 1388 1656 EXPORT_SYMBOL(stream_open); 1389 EXPORT_SYMBOL(stream_open); 1657 1390
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.