Skip to content

Commit a3af5f8

Browse files
borkmanndavem330
authored andcommitted
bpf: allow for mount options to specify permissions
Since we recently converted the BPF filesystem over to use mount_nodev(), we now have the possibility to also hold mount options in sb's s_fs_info. This work implements mount options support for specifying permissions on the sb's inode, which will be used by tc when it manually needs to mount the fs. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 21116b7 commit a3af5f8

1 file changed

Lines changed: 53 additions & 1 deletion

File tree

‎kernel/bpf/inode.c‎

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/namei.h>
1919
#include <linux/fs.h>
2020
#include <linux/kdev_t.h>
21+
#include <linux/parser.h>
2122
#include <linux/filter.h>
2223
#include <linux/bpf.h>
2324

@@ -364,15 +365,66 @@ static void bpf_evict_inode(struct inode *inode)
364365
static const struct super_operations bpf_super_ops = {
365366
.statfs = simple_statfs,
366367
.drop_inode = generic_delete_inode,
368+
.show_options = generic_show_options,
367369
.evict_inode = bpf_evict_inode,
368370
};
369371

372+
enum {
373+
OPT_MODE,
374+
OPT_ERR,
375+
};
376+
377+
static const match_table_t bpf_mount_tokens = {
378+
{ OPT_MODE, "mode=%o" },
379+
{ OPT_ERR, NULL },
380+
};
381+
382+
struct bpf_mount_opts {
383+
umode_t mode;
384+
};
385+
386+
static int bpf_parse_options(char *data, struct bpf_mount_opts *opts)
387+
{
388+
substring_t args[MAX_OPT_ARGS];
389+
int option, token;
390+
char *ptr;
391+
392+
opts->mode = S_IRWXUGO;
393+
394+
while ((ptr = strsep(&data, ",")) != NULL) {
395+
if (!*ptr)
396+
continue;
397+
398+
token = match_token(ptr, bpf_mount_tokens, args);
399+
switch (token) {
400+
case OPT_MODE:
401+
if (match_octal(&args[0], &option))
402+
return -EINVAL;
403+
opts->mode = option & S_IALLUGO;
404+
break;
405+
/* We might like to report bad mount options here, but
406+
* traditionally we've ignored all mount options, so we'd
407+
* better continue to ignore non-existing options for bpf.
408+
*/
409+
}
410+
}
411+
412+
return 0;
413+
}
414+
370415
static int bpf_fill_super(struct super_block *sb, void *data, int silent)
371416
{
372417
static struct tree_descr bpf_rfiles[] = { { "" } };
418+
struct bpf_mount_opts opts;
373419
struct inode *inode;
374420
int ret;
375421

422+
save_mount_options(sb, data);
423+
424+
ret = bpf_parse_options(data, &opts);
425+
if (ret)
426+
return ret;
427+
376428
ret = simple_fill_super(sb, BPF_FS_MAGIC, bpf_rfiles);
377429
if (ret)
378430
return ret;
@@ -382,7 +434,7 @@ static int bpf_fill_super(struct super_block *sb, void *data, int silent)
382434
inode = sb->s_root->d_inode;
383435
inode->i_op = &bpf_dir_iops;
384436
inode->i_mode &= ~S_IALLUGO;
385-
inode->i_mode |= S_ISVTX | S_IRWXUGO;
437+
inode->i_mode |= S_ISVTX | opts.mode;
386438

387439
return 0;
388440
}

0 commit comments

Comments
 (0)