Skip to content

Commit c220e21

Browse files
committed
exportfs: add permission method
This allows filesystems such as pidfs to provide their custom permission checks. Link: https://lore.kernel.org/r/[email protected] Reviewed-by: Amir Goldstein <[email protected]> Reviewed-by: Jan Kara <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent 6ebb05b commit c220e21

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

fs/fhandle.c

+14-21
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,6 @@ static int get_path_from_fd(int fd, struct path *root)
187187
return 0;
188188
}
189189

190-
enum handle_to_path_flags {
191-
HANDLE_CHECK_PERMS = (1 << 0),
192-
HANDLE_CHECK_SUBTREE = (1 << 1),
193-
};
194-
195-
struct handle_to_path_ctx {
196-
struct path root;
197-
enum handle_to_path_flags flags;
198-
unsigned int fh_flags;
199-
};
200-
201190
static int vfs_dentry_acceptable(void *context, struct dentry *dentry)
202191
{
203192
struct handle_to_path_ctx *ctx = context;
@@ -279,13 +268,13 @@ static int do_handle_to_path(struct file_handle *handle, struct path *path,
279268
return 0;
280269
}
281270

282-
static inline bool may_decode_fh(struct handle_to_path_ctx *ctx,
283-
unsigned int o_flags)
271+
static inline int may_decode_fh(struct handle_to_path_ctx *ctx,
272+
unsigned int o_flags)
284273
{
285274
struct path *root = &ctx->root;
286275

287276
if (capable(CAP_DAC_READ_SEARCH))
288-
return true;
277+
return 0;
289278

290279
/*
291280
* Allow relaxed permissions of file handles if the caller has
@@ -309,7 +298,7 @@ static inline bool may_decode_fh(struct handle_to_path_ctx *ctx,
309298
* There's only one dentry for each directory inode (VFS rule)...
310299
*/
311300
if (!(o_flags & O_DIRECTORY))
312-
return false;
301+
return -EPERM;
313302

314303
if (ns_capable(root->mnt->mnt_sb->s_user_ns, CAP_SYS_ADMIN))
315304
ctx->flags = HANDLE_CHECK_PERMS;
@@ -319,14 +308,14 @@ static inline bool may_decode_fh(struct handle_to_path_ctx *ctx,
319308
!has_locked_children(real_mount(root->mnt), root->dentry))
320309
ctx->flags = HANDLE_CHECK_PERMS | HANDLE_CHECK_SUBTREE;
321310
else
322-
return false;
311+
return -EPERM;
323312

324313
/* Are we able to override DAC permissions? */
325314
if (!ns_capable(current_user_ns(), CAP_DAC_READ_SEARCH))
326-
return false;
315+
return -EPERM;
327316

328317
ctx->fh_flags = EXPORT_FH_DIR_ONLY;
329-
return true;
318+
return 0;
330319
}
331320

332321
static int handle_to_path(int mountdirfd, struct file_handle __user *ufh,
@@ -336,15 +325,19 @@ static int handle_to_path(int mountdirfd, struct file_handle __user *ufh,
336325
struct file_handle f_handle;
337326
struct file_handle *handle = NULL;
338327
struct handle_to_path_ctx ctx = {};
328+
const struct export_operations *eops;
339329

340330
retval = get_path_from_fd(mountdirfd, &ctx.root);
341331
if (retval)
342332
goto out_err;
343333

344-
if (!may_decode_fh(&ctx, o_flags)) {
345-
retval = -EPERM;
334+
eops = ctx.root.mnt->mnt_sb->s_export_op;
335+
if (eops && eops->permission)
336+
retval = eops->permission(&ctx, o_flags);
337+
else
338+
retval = may_decode_fh(&ctx, o_flags);
339+
if (retval)
346340
goto out_path;
347-
}
348341

349342
if (copy_from_user(&f_handle, ufh, sizeof(struct file_handle))) {
350343
retval = -EFAULT;

include/linux/exportfs.h

+16-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
#define LINUX_EXPORTFS_H 1
44

55
#include <linux/types.h>
6+
#include <linux/path.h>
67

78
struct dentry;
89
struct iattr;
910
struct inode;
1011
struct iomap;
1112
struct super_block;
1213
struct vfsmount;
13-
struct path;
1414

1515
/* limit the handle size to NFSv4 handle size now */
1616
#define MAX_HANDLE_SZ 128
@@ -157,6 +157,17 @@ struct fid {
157157
};
158158
};
159159

160+
enum handle_to_path_flags {
161+
HANDLE_CHECK_PERMS = (1 << 0),
162+
HANDLE_CHECK_SUBTREE = (1 << 1),
163+
};
164+
165+
struct handle_to_path_ctx {
166+
struct path root;
167+
enum handle_to_path_flags flags;
168+
unsigned int fh_flags;
169+
};
170+
160171
#define EXPORT_FH_CONNECTABLE 0x1 /* Encode file handle with parent */
161172
#define EXPORT_FH_FID 0x2 /* File handle may be non-decodeable */
162173
#define EXPORT_FH_DIR_ONLY 0x4 /* Only decode file handle for a directory */
@@ -226,6 +237,9 @@ struct fid {
226237
* is also a directory. In the event that it cannot be found, or storage
227238
* space cannot be allocated, a %ERR_PTR should be returned.
228239
*
240+
* permission:
241+
* Allow filesystems to specify a custom permission function.
242+
*
229243
* open:
230244
* Allow filesystems to specify a custom open function.
231245
*
@@ -255,6 +269,7 @@ struct export_operations {
255269
bool write, u32 *device_generation);
256270
int (*commit_blocks)(struct inode *inode, struct iomap *iomaps,
257271
int nr_iomaps, struct iattr *iattr);
272+
int (*permission)(struct handle_to_path_ctx *ctx, unsigned int oflags);
258273
struct file * (*open)(struct path *path, unsigned int oflags);
259274
#define EXPORT_OP_NOWCC (0x1) /* don't collect v3 wcc data */
260275
#define EXPORT_OP_NOSUBTREECHK (0x2) /* no subtree checking */

0 commit comments

Comments
 (0)