Skip to content

Commit 0481819

Browse files
author
Al Viro
committed
fdget_raw() users: switch to CLASS(fd_raw)
Reviewed-by: Christian Brauner <[email protected]> Signed-off-by: Al Viro <[email protected]>
1 parent a6f4657 commit 0481819

File tree

8 files changed

+47
-83
lines changed

8 files changed

+47
-83
lines changed

arch/arm/kernel/sys_oabi-compat.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,12 @@ asmlinkage long sys_oabi_fcntl64(unsigned int fd, unsigned int cmd,
235235
unsigned long arg)
236236
{
237237
void __user *argp = (void __user *)arg;
238-
struct fd f = fdget_raw(fd);
238+
CLASS(fd_raw, f)(fd);
239239
struct flock64 flock;
240-
long err = -EBADF;
240+
long err;
241241

242-
if (!fd_file(f))
243-
goto out;
242+
if (fd_empty(f))
243+
return -EBADF;
244244

245245
switch (cmd) {
246246
case F_GETLK64:
@@ -271,8 +271,6 @@ asmlinkage long sys_oabi_fcntl64(unsigned int fd, unsigned int cmd,
271271
err = sys_fcntl64(fd, cmd, arg);
272272
break;
273273
}
274-
fdput(f);
275-
out:
276274
return err;
277275
}
278276

fs/fcntl.c

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -570,24 +570,21 @@ static int check_fcntl_cmd(unsigned cmd)
570570

571571
SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
572572
{
573-
struct fd f = fdget_raw(fd);
574-
long err = -EBADF;
573+
CLASS(fd_raw, f)(fd);
574+
long err;
575575

576-
if (!fd_file(f))
577-
goto out;
576+
if (fd_empty(f))
577+
return -EBADF;
578578

579579
if (unlikely(fd_file(f)->f_mode & FMODE_PATH)) {
580580
if (!check_fcntl_cmd(cmd))
581-
goto out1;
581+
return -EBADF;
582582
}
583583

584584
err = security_file_fcntl(fd_file(f), cmd, arg);
585585
if (!err)
586586
err = do_fcntl(fd, cmd, arg, fd_file(f));
587587

588-
out1:
589-
fdput(f);
590-
out:
591588
return err;
592589
}
593590

@@ -596,21 +593,21 @@ SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
596593
unsigned long, arg)
597594
{
598595
void __user *argp = (void __user *)arg;
599-
struct fd f = fdget_raw(fd);
596+
CLASS(fd_raw, f)(fd);
600597
struct flock64 flock;
601-
long err = -EBADF;
598+
long err;
602599

603-
if (!fd_file(f))
604-
goto out;
600+
if (fd_empty(f))
601+
return -EBADF;
605602

606603
if (unlikely(fd_file(f)->f_mode & FMODE_PATH)) {
607604
if (!check_fcntl_cmd(cmd))
608-
goto out1;
605+
return -EBADF;
609606
}
610607

611608
err = security_file_fcntl(fd_file(f), cmd, arg);
612609
if (err)
613-
goto out1;
610+
return err;
614611

615612
switch (cmd) {
616613
case F_GETLK64:
@@ -635,9 +632,6 @@ SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
635632
err = do_fcntl(fd, cmd, arg, fd_file(f));
636633
break;
637634
}
638-
out1:
639-
fdput(f);
640-
out:
641635
return err;
642636
}
643637
#endif
@@ -733,21 +727,21 @@ static int fixup_compat_flock(struct flock *flock)
733727
static long do_compat_fcntl64(unsigned int fd, unsigned int cmd,
734728
compat_ulong_t arg)
735729
{
736-
struct fd f = fdget_raw(fd);
730+
CLASS(fd_raw, f)(fd);
737731
struct flock flock;
738-
long err = -EBADF;
732+
long err;
739733

740-
if (!fd_file(f))
741-
return err;
734+
if (fd_empty(f))
735+
return -EBADF;
742736

743737
if (unlikely(fd_file(f)->f_mode & FMODE_PATH)) {
744738
if (!check_fcntl_cmd(cmd))
745-
goto out_put;
739+
return -EBADF;
746740
}
747741

748742
err = security_file_fcntl(fd_file(f), cmd, arg);
749743
if (err)
750-
goto out_put;
744+
return err;
751745

752746
switch (cmd) {
753747
case F_GETLK:
@@ -790,8 +784,6 @@ static long do_compat_fcntl64(unsigned int fd, unsigned int cmd,
790784
err = do_fcntl(fd, cmd, arg, fd_file(f));
791785
break;
792786
}
793-
out_put:
794-
fdput(f);
795787
return err;
796788
}
797789

fs/namei.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2503,26 +2503,22 @@ static const char *path_init(struct nameidata *nd, unsigned flags)
25032503
}
25042504
} else {
25052505
/* Caller must check execute permissions on the starting path component */
2506-
struct fd f = fdget_raw(nd->dfd);
2506+
CLASS(fd_raw, f)(nd->dfd);
25072507
struct dentry *dentry;
25082508

2509-
if (!fd_file(f))
2509+
if (fd_empty(f))
25102510
return ERR_PTR(-EBADF);
25112511

25122512
if (flags & LOOKUP_LINKAT_EMPTY) {
25132513
if (fd_file(f)->f_cred != current_cred() &&
2514-
!ns_capable(fd_file(f)->f_cred->user_ns, CAP_DAC_READ_SEARCH)) {
2515-
fdput(f);
2514+
!ns_capable(fd_file(f)->f_cred->user_ns, CAP_DAC_READ_SEARCH))
25162515
return ERR_PTR(-ENOENT);
2517-
}
25182516
}
25192517

25202518
dentry = fd_file(f)->f_path.dentry;
25212519

2522-
if (*s && unlikely(!d_can_lookup(dentry))) {
2523-
fdput(f);
2520+
if (*s && unlikely(!d_can_lookup(dentry)))
25242521
return ERR_PTR(-ENOTDIR);
2525-
}
25262522

25272523
nd->path = fd_file(f)->f_path;
25282524
if (flags & LOOKUP_RCU) {
@@ -2532,7 +2528,6 @@ static const char *path_init(struct nameidata *nd, unsigned flags)
25322528
path_get(&nd->path);
25332529
nd->inode = nd->path.dentry->d_inode;
25342530
}
2535-
fdput(f);
25362531
}
25372532

25382533
/* For scoped-lookups we need to set the root to the dirfd as well. */

fs/open.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -580,23 +580,18 @@ SYSCALL_DEFINE1(chdir, const char __user *, filename)
580580

581581
SYSCALL_DEFINE1(fchdir, unsigned int, fd)
582582
{
583-
struct fd f = fdget_raw(fd);
583+
CLASS(fd_raw, f)(fd);
584584
int error;
585585

586-
error = -EBADF;
587-
if (!fd_file(f))
588-
goto out;
586+
if (fd_empty(f))
587+
return -EBADF;
589588

590-
error = -ENOTDIR;
591589
if (!d_can_lookup(fd_file(f)->f_path.dentry))
592-
goto out_putf;
590+
return -ENOTDIR;
593591

594592
error = file_permission(fd_file(f), MAY_EXEC | MAY_CHDIR);
595593
if (!error)
596594
set_fs_pwd(current->fs, &fd_file(f)->f_path);
597-
out_putf:
598-
fdput(f);
599-
out:
600595
return error;
601596
}
602597

fs/quota/quota.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -976,21 +976,19 @@ SYSCALL_DEFINE4(quotactl_fd, unsigned int, fd, unsigned int, cmd,
976976
struct super_block *sb;
977977
unsigned int cmds = cmd >> SUBCMDSHIFT;
978978
unsigned int type = cmd & SUBCMDMASK;
979-
struct fd f;
979+
CLASS(fd_raw, f)(fd);
980980
int ret;
981981

982-
f = fdget_raw(fd);
983-
if (!fd_file(f))
982+
if (fd_empty(f))
984983
return -EBADF;
985984

986-
ret = -EINVAL;
987985
if (type >= MAXQUOTAS)
988-
goto out;
986+
return -EINVAL;
989987

990988
if (quotactl_cmd_write(cmds)) {
991989
ret = mnt_want_write(fd_file(f)->f_path.mnt);
992990
if (ret)
993-
goto out;
991+
return ret;
994992
}
995993

996994
sb = fd_file(f)->f_path.mnt->mnt_sb;
@@ -1008,7 +1006,5 @@ SYSCALL_DEFINE4(quotactl_fd, unsigned int, fd, unsigned int, cmd,
10081006

10091007
if (quotactl_cmd_write(cmds))
10101008
mnt_drop_write(fd_file(f)->f_path.mnt);
1011-
out:
1012-
fdput(f);
10131009
return ret;
10141010
}

fs/statfs.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,11 @@ int user_statfs(const char __user *pathname, struct kstatfs *st)
114114

115115
int fd_statfs(int fd, struct kstatfs *st)
116116
{
117-
struct fd f = fdget_raw(fd);
118-
int error = -EBADF;
119-
if (fd_file(f)) {
120-
error = vfs_statfs(&fd_file(f)->f_path, st);
121-
fdput(f);
122-
}
123-
return error;
117+
CLASS(fd_raw, f)(fd);
118+
119+
if (fd_empty(f))
120+
return -EBADF;
121+
return vfs_statfs(&fd_file(f)->f_path, st);
124122
}
125123

126124
static int do_statfs_native(struct kstatfs *st, struct statfs __user *p)

kernel/cgroup/cgroup.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6966,14 +6966,11 @@ EXPORT_SYMBOL_GPL(cgroup_get_from_path);
69666966
*/
69676967
struct cgroup *cgroup_v1v2_get_from_fd(int fd)
69686968
{
6969-
struct cgroup *cgrp;
6970-
struct fd f = fdget_raw(fd);
6971-
if (!fd_file(f))
6969+
CLASS(fd_raw, f)(fd);
6970+
if (fd_empty(f))
69726971
return ERR_PTR(-EBADF);
69736972

6974-
cgrp = cgroup_v1v2_get_from_file(fd_file(f));
6975-
fdput(f);
6976-
return cgrp;
6973+
return cgroup_v1v2_get_from_file(fd_file(f));
69776974
}
69786975

69796976
/**

security/landlock/syscalls.c

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,12 @@ static struct landlock_ruleset *get_ruleset_from_fd(const int fd,
276276
*/
277277
static int get_path_from_fd(const s32 fd, struct path *const path)
278278
{
279-
struct fd f;
280-
int err = 0;
279+
CLASS(fd_raw, f)(fd);
281280

282281
BUILD_BUG_ON(!__same_type(
283282
fd, ((struct landlock_path_beneath_attr *)NULL)->parent_fd));
284283

285-
/* Handles O_PATH. */
286-
f = fdget_raw(fd);
287-
if (!fd_file(f))
284+
if (fd_empty(f))
288285
return -EBADF;
289286
/*
290287
* Forbids ruleset FDs, internal filesystems (e.g. nsfs), including
@@ -295,16 +292,12 @@ static int get_path_from_fd(const s32 fd, struct path *const path)
295292
(fd_file(f)->f_path.mnt->mnt_flags & MNT_INTERNAL) ||
296293
(fd_file(f)->f_path.dentry->d_sb->s_flags & SB_NOUSER) ||
297294
d_is_negative(fd_file(f)->f_path.dentry) ||
298-
IS_PRIVATE(d_backing_inode(fd_file(f)->f_path.dentry))) {
299-
err = -EBADFD;
300-
goto out_fdput;
301-
}
295+
IS_PRIVATE(d_backing_inode(fd_file(f)->f_path.dentry)))
296+
return -EBADFD;
297+
302298
*path = fd_file(f)->f_path;
303299
path_get(path);
304-
305-
out_fdput:
306-
fdput(f);
307-
return err;
300+
return 0;
308301
}
309302

310303
static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,

0 commit comments

Comments
 (0)