[kernel-hardening] [RFC v1] debugfs mount options

Vasiliy Kulikov
2011-06-05T18:28:30Z

[-- Attachment #1: Type: text/plain, Size: 3595 bytes --]

This patch adds support of 2 debugfs mount options:

umask=0XXX - umask for newly created debugfs files.

gid=YYY - gid for newly created debugfs files.

While implementing it, I realized that it is probably more usefull to
implement it as 2 sysctls and CONFIG_DEBUGFS_* options - a lot of
debugfs files are created at the boot time, so it makes sense to change
these setting at the compile time and not to bother with chmod'ing
already created files.

diff --git a/fs/debugfs/inode.c b/fs/debugfs/inode.c
index d38c88f..5af9658 100644
--- a/fs/debugfs/inode.c
+++ b/fs/debugfs/inode.c
@@ -21,8 +21,10 @@
#include <linux/kobject.h>
#include <linux/namei.h>
#include <linux/debugfs.h>
+#include <linux/parser.h>
#include <linux/fsnotify.h>
#include <linux/string.h>
+#include <linux/seq_file.h>
#include <linux/magic.h>
#include <linux/slab.h>

@@ -30,6 +32,9 @@ static struct vfsmount *debugfs_mount;
static int debugfs_mount_count;
static bool debugfs_registered;

+unsigned int debugfs_umask;
+gid_t debugfs_gid;
+
static struct inode *debugfs_get_inode(struct super_block *sb, int mode, dev_t dev,
void *data, const struct file_operations *fops)

@@ -63,8 +68,10 @@ static struct inode *debugfs_get_inode(struct super_block *sb, int mode, dev_t d
inc_nlink(inode);
break;
}
+ inode->i_mode &= ~(0777 & debugfs_umask);
+ inode->i_gid = debugfs_gid;
}
- return inode;
+ return inode;
}

/* SMP-safe */
@@ -125,11 +132,85 @@ static inline int debugfs_positive(struct dentry *dentry)
return dentry->d_inode && !d_unhashed(dentry);
}

+enum {
+ Opt_gid, Opt_umask, Opt_err
+};
+
+static const match_table_t debug_tokens = {
+ {Opt_gid, "gid=%u"},
+ {Opt_umask, "umask=%o"},
+ {Opt_err, NULL},
+};
+
+static int debug_parse_options(char *options, struct super_block *sb)
+{
+ char *p;
+ substring_t args[MAX_OPT_ARGS];
+ int option;
+
+ if (!options)
+ return 1;
+
+ while ((p = strsep(&options, ",")) != NULL) {
+ int token;
+ if (!*p)
+ continue;
+
+ token = match_token(p, debug_tokens, args);
+ switch (token) {
+ case Opt_gid:
+ if (match_int(&args[0], &option))
+ return 0;
+ debugfs_gid = option;
+ break;
+ case Opt_umask:
+ if (match_octal(&args[0], &option))
+ return 0;
+ debugfs_umask = option;
+ break;
+ default:
+ pr_err("debugfs: unrecognized mount option \"%s\" "
+ "or missing value", p);
+ return 0;
+ }
+ }
+ return 1;
+}
+
+static int debug_remount_fs(struct super_block *sb, int *flags, char *options)
+{
+ if (debug_parse_options(options, sb))
+ return 0;
+ else
+ return -EINVAL;
+}
+
+int debug_show_options(struct seq_file *seq, struct vfsmount *vfs)
+{
+ if (debugfs_umask)
+ seq_printf(seq, ",umask=%o", debugfs_umask);
+ if (debugfs_gid)
+ seq_printf(seq, ",gid=%lu", (unsigned long)debugfs_gid);
+ return 0;
+}
+
+static const struct super_operations debug_super_operations = {
+ .statfs = simple_statfs,
+ .remount_fs = debug_remount_fs,
+ .show_options = debug_show_options,
+};
+
static int debug_fill_super(struct super_block *sb, void *data, int silent)
{
static struct tree_descr debug_files[] = {{""}};
+ int err;
+
+ if (!debug_parse_options(data, sb))
+ return -EINVAL;

- return simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
+ sb->s_op = &debug_super_operations;
+ err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
+ return err;
}

static struct dentry *debug_mount(struct file_system_type *fs_type,