Commit 452452be authored by Eric W. Biederman's avatar Eric W. Biederman Committed by Luis Henriques

sysctl: Allow creating permanently empty directories that serve as mountpoints.

commit f9bd6733 upstream.

Add a magic sysctl table sysctl_mount_point that when used to
create a directory forces that directory to be permanently empty.

Update the code to use make_empty_dir_inode when accessing permanently
empty directories.

Update the code to not allow adding to permanently empty directories.

Update /proc/sys/fs/binfmt_misc to be a permanently empty directory.
Signed-off-by: default avatar"Eric W. Biederman" <ebiederm@xmission.com>
Signed-off-by: default avatarLuis Henriques <luis.henriques@canonical.com>
parent 8f948597
...@@ -19,6 +19,28 @@ static const struct inode_operations proc_sys_inode_operations; ...@@ -19,6 +19,28 @@ static const struct inode_operations proc_sys_inode_operations;
static const struct file_operations proc_sys_dir_file_operations; static const struct file_operations proc_sys_dir_file_operations;
static const struct inode_operations proc_sys_dir_operations; static const struct inode_operations proc_sys_dir_operations;
/* Support for permanently empty directories */
struct ctl_table sysctl_mount_point[] = {
{ }
};
static bool is_empty_dir(struct ctl_table_header *head)
{
return head->ctl_table[0].child == sysctl_mount_point;
}
static void set_empty_dir(struct ctl_dir *dir)
{
dir->header.ctl_table[0].child = sysctl_mount_point;
}
static void clear_empty_dir(struct ctl_dir *dir)
{
dir->header.ctl_table[0].child = NULL;
}
void proc_sys_poll_notify(struct ctl_table_poll *poll) void proc_sys_poll_notify(struct ctl_table_poll *poll)
{ {
if (!poll) if (!poll)
...@@ -187,6 +209,17 @@ static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header) ...@@ -187,6 +209,17 @@ static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
struct ctl_table *entry; struct ctl_table *entry;
int err; int err;
/* Is this a permanently empty directory? */
if (is_empty_dir(&dir->header))
return -EROFS;
/* Am I creating a permanently empty directory? */
if (header->ctl_table == sysctl_mount_point) {
if (!RB_EMPTY_ROOT(&dir->root))
return -EINVAL;
set_empty_dir(dir);
}
dir->header.nreg++; dir->header.nreg++;
header->parent = dir; header->parent = dir;
err = insert_links(header); err = insert_links(header);
...@@ -202,6 +235,8 @@ static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header) ...@@ -202,6 +235,8 @@ static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
erase_header(header); erase_header(header);
put_links(header); put_links(header);
fail_links: fail_links:
if (header->ctl_table == sysctl_mount_point)
clear_empty_dir(dir);
header->parent = NULL; header->parent = NULL;
drop_sysctl_table(&dir->header); drop_sysctl_table(&dir->header);
return err; return err;
...@@ -419,6 +454,8 @@ static struct inode *proc_sys_make_inode(struct super_block *sb, ...@@ -419,6 +454,8 @@ static struct inode *proc_sys_make_inode(struct super_block *sb,
inode->i_mode |= S_IFDIR; inode->i_mode |= S_IFDIR;
inode->i_op = &proc_sys_dir_operations; inode->i_op = &proc_sys_dir_operations;
inode->i_fop = &proc_sys_dir_file_operations; inode->i_fop = &proc_sys_dir_file_operations;
if (is_empty_dir(head))
make_empty_dir_inode(inode);
} }
out: out:
return inode; return inode;
......
...@@ -190,6 +190,9 @@ struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path, ...@@ -190,6 +190,9 @@ struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
void unregister_sysctl_table(struct ctl_table_header * table); void unregister_sysctl_table(struct ctl_table_header * table);
extern int sysctl_init(void); extern int sysctl_init(void);
extern struct ctl_table sysctl_mount_point[];
#else /* CONFIG_SYSCTL */ #else /* CONFIG_SYSCTL */
static inline struct ctl_table_header *register_sysctl_table(struct ctl_table * table) static inline struct ctl_table_header *register_sysctl_table(struct ctl_table * table)
{ {
......
...@@ -1507,12 +1507,6 @@ static struct ctl_table vm_table[] = { ...@@ -1507,12 +1507,6 @@ static struct ctl_table vm_table[] = {
{ } { }
}; };
#if defined(CONFIG_BINFMT_MISC) || defined(CONFIG_BINFMT_MISC_MODULE)
static struct ctl_table binfmt_misc_table[] = {
{ }
};
#endif
static struct ctl_table fs_table[] = { static struct ctl_table fs_table[] = {
{ {
.procname = "inode-nr", .procname = "inode-nr",
...@@ -1666,7 +1660,7 @@ static struct ctl_table fs_table[] = { ...@@ -1666,7 +1660,7 @@ static struct ctl_table fs_table[] = {
{ {
.procname = "binfmt_misc", .procname = "binfmt_misc",
.mode = 0555, .mode = 0555,
.child = binfmt_misc_table, .child = sysctl_mount_point,
}, },
#endif #endif
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment