Skip to content

Commit 56c956e

Browse files
Al Viroanakryiko
Al Viro
authored andcommitted
bpf: convert bpf_token_create() to CLASS(fd, ...)
Keep file reference through the entire thing, don't bother with grabbing struct path reference and while we are at it, don't confuse the hell out of readers by random mix of path.dentry->d_sb and path.mnt->mnt_sb uses - these two are equal, so just put one of those into a local variable and use that. Reviewed-by: Christian Brauner <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Signed-off-by: Al Viro <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]>
1 parent f29035a commit 56c956e

File tree

1 file changed

+23
-42
lines changed

1 file changed

+23
-42
lines changed

kernel/bpf/token.c

+23-42
Original file line numberDiff line numberDiff line change
@@ -116,67 +116,52 @@ int bpf_token_create(union bpf_attr *attr)
116116
struct user_namespace *userns;
117117
struct inode *inode;
118118
struct file *file;
119+
CLASS(fd, f)(attr->token_create.bpffs_fd);
119120
struct path path;
120-
struct fd f;
121+
struct super_block *sb;
121122
umode_t mode;
122123
int err, fd;
123124

124-
f = fdget(attr->token_create.bpffs_fd);
125-
if (!fd_file(f))
125+
if (fd_empty(f))
126126
return -EBADF;
127127

128128
path = fd_file(f)->f_path;
129-
path_get(&path);
130-
fdput(f);
129+
sb = path.dentry->d_sb;
131130

132-
if (path.dentry != path.mnt->mnt_sb->s_root) {
133-
err = -EINVAL;
134-
goto out_path;
135-
}
136-
if (path.mnt->mnt_sb->s_op != &bpf_super_ops) {
137-
err = -EINVAL;
138-
goto out_path;
139-
}
131+
if (path.dentry != sb->s_root)
132+
return -EINVAL;
133+
if (sb->s_op != &bpf_super_ops)
134+
return -EINVAL;
140135
err = path_permission(&path, MAY_ACCESS);
141136
if (err)
142-
goto out_path;
137+
return err;
143138

144-
userns = path.dentry->d_sb->s_user_ns;
139+
userns = sb->s_user_ns;
145140
/*
146141
* Enforce that creators of BPF tokens are in the same user
147142
* namespace as the BPF FS instance. This makes reasoning about
148143
* permissions a lot easier and we can always relax this later.
149144
*/
150-
if (current_user_ns() != userns) {
151-
err = -EPERM;
152-
goto out_path;
153-
}
154-
if (!ns_capable(userns, CAP_BPF)) {
155-
err = -EPERM;
156-
goto out_path;
157-
}
145+
if (current_user_ns() != userns)
146+
return -EPERM;
147+
if (!ns_capable(userns, CAP_BPF))
148+
return -EPERM;
158149

159150
/* Creating BPF token in init_user_ns doesn't make much sense. */
160-
if (current_user_ns() == &init_user_ns) {
161-
err = -EOPNOTSUPP;
162-
goto out_path;
163-
}
151+
if (current_user_ns() == &init_user_ns)
152+
return -EOPNOTSUPP;
164153

165-
mnt_opts = path.dentry->d_sb->s_fs_info;
154+
mnt_opts = sb->s_fs_info;
166155
if (mnt_opts->delegate_cmds == 0 &&
167156
mnt_opts->delegate_maps == 0 &&
168157
mnt_opts->delegate_progs == 0 &&
169-
mnt_opts->delegate_attachs == 0) {
170-
err = -ENOENT; /* no BPF token delegation is set up */
171-
goto out_path;
172-
}
158+
mnt_opts->delegate_attachs == 0)
159+
return -ENOENT; /* no BPF token delegation is set up */
173160

174161
mode = S_IFREG | ((S_IRUSR | S_IWUSR) & ~current_umask());
175-
inode = bpf_get_inode(path.mnt->mnt_sb, NULL, mode);
176-
if (IS_ERR(inode)) {
177-
err = PTR_ERR(inode);
178-
goto out_path;
179-
}
162+
inode = bpf_get_inode(sb, NULL, mode);
163+
if (IS_ERR(inode))
164+
return PTR_ERR(inode);
180165

181166
inode->i_op = &bpf_token_iops;
182167
inode->i_fop = &bpf_token_fops;
@@ -185,8 +170,7 @@ int bpf_token_create(union bpf_attr *attr)
185170
file = alloc_file_pseudo(inode, path.mnt, BPF_TOKEN_INODE_NAME, O_RDWR, &bpf_token_fops);
186171
if (IS_ERR(file)) {
187172
iput(inode);
188-
err = PTR_ERR(file);
189-
goto out_path;
173+
return PTR_ERR(file);
190174
}
191175

192176
token = kzalloc(sizeof(*token), GFP_USER);
@@ -218,15 +202,12 @@ int bpf_token_create(union bpf_attr *attr)
218202
file->private_data = token;
219203
fd_install(fd, file);
220204

221-
path_put(&path);
222205
return fd;
223206

224207
out_token:
225208
bpf_token_free(token);
226209
out_file:
227210
fput(file);
228-
out_path:
229-
path_put(&path);
230211
return err;
231212
}
232213

0 commit comments

Comments
 (0)