Skip to content

Commit f9652e1

Browse files
author
Al Viro
committed
allow build_open_flags() to return an error
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 50cd2c5 commit f9652e1

File tree

4 files changed

+41
-31
lines changed

4 files changed

+41
-31
lines changed

fs/exec.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,14 @@ SYSCALL_DEFINE1(uselib, const char __user *, library)
110110
static const struct open_flags uselib_flags = {
111111
.open_flag = O_LARGEFILE | O_RDONLY | __FMODE_EXEC,
112112
.acc_mode = MAY_READ | MAY_EXEC | MAY_OPEN,
113-
.intent = LOOKUP_OPEN
113+
.intent = LOOKUP_OPEN,
114+
.lookup_flags = LOOKUP_FOLLOW,
114115
};
115116

116117
if (IS_ERR(tmp))
117118
goto out;
118119

119-
file = do_filp_open(AT_FDCWD, tmp, &uselib_flags, LOOKUP_FOLLOW);
120+
file = do_filp_open(AT_FDCWD, tmp, &uselib_flags);
120121
putname(tmp);
121122
error = PTR_ERR(file);
122123
if (IS_ERR(file))
@@ -756,10 +757,11 @@ struct file *open_exec(const char *name)
756757
static const struct open_flags open_exec_flags = {
757758
.open_flag = O_LARGEFILE | O_RDONLY | __FMODE_EXEC,
758759
.acc_mode = MAY_EXEC | MAY_OPEN,
759-
.intent = LOOKUP_OPEN
760+
.intent = LOOKUP_OPEN,
761+
.lookup_flags = LOOKUP_FOLLOW,
760762
};
761763

762-
file = do_filp_open(AT_FDCWD, &tmp, &open_exec_flags, LOOKUP_FOLLOW);
764+
file = do_filp_open(AT_FDCWD, &tmp, &open_exec_flags);
763765
if (IS_ERR(file))
764766
goto out;
765767

fs/internal.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,12 @@ struct open_flags {
9696
umode_t mode;
9797
int acc_mode;
9898
int intent;
99+
int lookup_flags;
99100
};
100101
extern struct file *do_filp_open(int dfd, struct filename *pathname,
101-
const struct open_flags *op, int flags);
102+
const struct open_flags *op);
102103
extern struct file *do_file_open_root(struct dentry *, struct vfsmount *,
103-
const char *, const struct open_flags *, int lookup_flags);
104+
const char *, const struct open_flags *);
104105

105106
extern long do_handle_open(int mountdirfd,
106107
struct file_handle __user *ufh, int open_flag);

fs/namei.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2969,9 +2969,10 @@ static struct file *path_openat(int dfd, struct filename *pathname,
29692969
}
29702970

29712971
struct file *do_filp_open(int dfd, struct filename *pathname,
2972-
const struct open_flags *op, int flags)
2972+
const struct open_flags *op)
29732973
{
29742974
struct nameidata nd;
2975+
int flags = op->lookup_flags;
29752976
struct file *filp;
29762977

29772978
filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
@@ -2983,17 +2984,16 @@ struct file *do_filp_open(int dfd, struct filename *pathname,
29832984
}
29842985

29852986
struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2986-
const char *name, const struct open_flags *op, int flags)
2987+
const char *name, const struct open_flags *op)
29872988
{
29882989
struct nameidata nd;
29892990
struct file *file;
29902991
struct filename filename = { .name = name };
2992+
int flags = op->lookup_flags | LOOKUP_ROOT;
29912993

29922994
nd.root.mnt = mnt;
29932995
nd.root.dentry = dentry;
29942996

2995-
flags |= LOOKUP_ROOT;
2996-
29972997
if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
29982998
return ERR_PTR(-ELOOP);
29992999

fs/open.c

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,8 @@ static inline int build_open_flags(int flags, umode_t mode, struct open_flags *o
876876
lookup_flags |= LOOKUP_DIRECTORY;
877877
if (!(flags & O_NOFOLLOW))
878878
lookup_flags |= LOOKUP_FOLLOW;
879-
return lookup_flags;
879+
op->lookup_flags = lookup_flags;
880+
return 0;
880881
}
881882

882883
/**
@@ -893,8 +894,8 @@ static inline int build_open_flags(int flags, umode_t mode, struct open_flags *o
893894
struct file *file_open_name(struct filename *name, int flags, umode_t mode)
894895
{
895896
struct open_flags op;
896-
int lookup = build_open_flags(flags, mode, &op);
897-
return do_filp_open(AT_FDCWD, name, &op, lookup);
897+
int err = build_open_flags(flags, mode, &op);
898+
return err ? ERR_PTR(err) : do_filp_open(AT_FDCWD, name, &op);
898899
}
899900

900901
/**
@@ -919,37 +920,43 @@ struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt,
919920
const char *filename, int flags)
920921
{
921922
struct open_flags op;
922-
int lookup = build_open_flags(flags, 0, &op);
923+
int err = build_open_flags(flags, 0, &op);
924+
if (err)
925+
return ERR_PTR(err);
923926
if (flags & O_CREAT)
924927
return ERR_PTR(-EINVAL);
925928
if (!filename && (flags & O_DIRECTORY))
926929
if (!dentry->d_inode->i_op->lookup)
927930
return ERR_PTR(-ENOTDIR);
928-
return do_file_open_root(dentry, mnt, filename, &op, lookup);
931+
return do_file_open_root(dentry, mnt, filename, &op);
929932
}
930933
EXPORT_SYMBOL(file_open_root);
931934

932935
long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
933936
{
934937
struct open_flags op;
935-
int lookup = build_open_flags(flags, mode, &op);
936-
struct filename *tmp = getname(filename);
937-
int fd = PTR_ERR(tmp);
938-
939-
if (!IS_ERR(tmp)) {
940-
fd = get_unused_fd_flags(flags);
941-
if (fd >= 0) {
942-
struct file *f = do_filp_open(dfd, tmp, &op, lookup);
943-
if (IS_ERR(f)) {
944-
put_unused_fd(fd);
945-
fd = PTR_ERR(f);
946-
} else {
947-
fsnotify_open(f);
948-
fd_install(fd, f);
949-
}
938+
int fd = build_open_flags(flags, mode, &op);
939+
struct filename *tmp;
940+
941+
if (fd)
942+
return fd;
943+
944+
tmp = getname(filename);
945+
if (IS_ERR(tmp))
946+
return PTR_ERR(tmp);
947+
948+
fd = get_unused_fd_flags(flags);
949+
if (fd >= 0) {
950+
struct file *f = do_filp_open(dfd, tmp, &op);
951+
if (IS_ERR(f)) {
952+
put_unused_fd(fd);
953+
fd = PTR_ERR(f);
954+
} else {
955+
fsnotify_open(f);
956+
fd_install(fd, f);
950957
}
951-
putname(tmp);
952958
}
959+
putname(tmp);
953960
return fd;
954961
}
955962

0 commit comments

Comments
 (0)