VFS: DazukoFS, stackable-fs, file access control
Subsystems (2)
v2 cover VFS: DazukoFS, stackable-fs, file access control
Hello,
This is the second submission of DazukoFS. As suggested by Pavel Machek, I have gone through and cleaned up the coding style, added missing includes, and worked to improve readability. This patchset is against Linux 2.6.29-rc3. Following is the introduction text from the original submission:
This patchset introduces a new stackable filesystem: DazukoFS.
SUMMARY
DazukoFS is a stackable filesystem that provides functionality allowing userspace applications to perform online file access control. When a file is accessed on DazukoFS, registered userspace processes will be notified and given the opportunity to allow or deny the access. To help a registered process make its decision, it receives a read-only open file descriptor to the file being accessed. DazukoFS uses named grouping to determine if multiple registered processes are working together.
The features of DazukoFS can be briefly summarized as follows:
1. Parallel Event Handling
Multiple processes (or threads) may register using the same group name to signify that they are working together. When a file on DazukoFS is accessed, a file access event will be assigned to the first available process in the group. If another file access event occurs before the first has been handled, the next available process in the group will be assigned the event. If no processes in the group are currently available, that particular file access will block. This allows multiple files to be processed simultaneously.
2. Multiple Groups
Different applications may register, each using their own group name. Each group will be given the opportunity to determine if access may be allowed. Access is denied if any group decides to deny access. This allows different access control applications to run simultaneously and independent from one another.
3. Device-Determined Privileges
Each group uses its own dedicated device. When performing file access control, an application only needs read/write access to its group device. The application does not require any other privileges. Since DazukoFS already opens the file being accessed (read-only), a non-privileged registered application is able to perform online file access control for all files (including those that it would normally not have access to).
MOTIVATION
Since 2001 various anti-virus vendors have been providing out-of-tree solutions for online virus scanning. Although GNU/Linux systems currently are not targets of virus authors, many organizations are interested in online virus scanning on Linux-based servers in order to help protect Microsoft Windows clients. It is often argued that file scanning should be implemented in the various services (such as Samba, Apache, vsftpd, etc.), and indeed many such solutions have been implemented. However, there is a continued demand for a kernel-based solution because it can guard the entire filesystem independent from the types and numbers of services running on a system.
In 2002, the Dazuko project was started with the goal of providing a userspace interface on top of a kernel-based file access control mechanism. From early on, the project has aimed to support multiple anti-virus applications simultaneously without any bias towards a particular application. Today several anti-virus vendors choose Dazuko as a basis for their online file scanning solution.
Dazuko originally used syscall-table hooking for its access control mechanism. With the arrival of Linux 2.6, Dazuko switched to using LSM. However, in February 2004, Christoph Hellwig suggested that file access events should be intercepted using a stackable filesystem. After many discussions and several prototypes, it was decided that the Dazuko project would move to become a stackable
filesystem: DazukoFS.
Implementing online file access control using a stackable filesystem has several benefits:
1. No core Linux code must be modified. As a stackable filesystem, DazukoFS is a separate component of the kernel. This could prove very useful if/when anti-virus vendors want to develop new DazukoFS features. With DazukoFS as a separate component, new features could be added without requiring core Linux code changes.
2. On LKML, an interest in a stackable filesystem framework has been expressed. With DazukoFS being another stackable filesystem, we can begin looking at common code between the stackable filesystems and possibly begin identifying a stackable filesystem framework. Using real stackable filesystems as a model is much easier than guessing what should go into a stackable filesystem framework.
The Dazuko project is interested in going mainline with DazukoFS. Nearly seven years of out-of-tree development were more than enough to prove that out-of-tree kernel drivers have an unnecessarily large maintenance cost (which increases with each new kernel release). With DazukoFS mainline, anti-virus vendors would finally have an official interface and implementation on which to base their online scanning applications.
CURRENT STATE
DazukoFS currently includes a minimal set of features. This was done on purpose to try to minimize the amount of code for the initial presentation for mainline acceptance. Here is a brief listing of the current state of the project:
- Only file access events for "open" file operations are intercepted.
- Writing to memory mapped files is not supported.
- Many exotic VFS calls are not stacked upon.
- A bug was recently reported at Red Hat regarding problems using SElinux and stackable filesystems. This affects DazukoFS as well as other stackable filesystems.
https://bugzilla.redhat.com/show_bug.cgi?id=475985
- DazukoFS has been tested primarily on top of ext3 (read/write) for the powerpc architecture.
A WORD ON FANOTIFY
I am aware of the current work of Eric Paris to implement a file access control mechanism within a unified inotify/dnotify framework. Although I welcome any official interface to provide a file access control mechanism for userspace applications in Linux, I feel that DazukoFS provides a more elegant solution. (Note that the two projects do not conflict with each other.) I am open to discuss this point further if anyone is interested.
SUBMISSION PATCHSET
A series of 5 patches will be posted. Each patch provides new features and working code so that DazukoFS can be evaluated with various feature (patch) subsets. The patches can be briefly summarized as follows:
Patch 1: Introduces DazukoFS as a nullfs. This is the raw stackable
filesystem part of DazukoFS and does nothing except stack and pass filesystem calls to the lower filesystem.
Patch 2: Creates /dev/dazukofs.0 for userspace applications to perform
file access control. At this point, all applications are considered to be working together (in the same group).
Patch 3: Creates /dev/dazukofs.ctrl to allow for groups to be added,
listed, and deleted. Also, /dev/dazukofs.[0-9] devices are created to support multiple groups.
Patch 4: Adds a new (optional) command to allow registered processes
to be tracked. The tracking allows processes to be automatically unregistered if they crash and also allows groups to be automatically deleted if no more processes are registered.
Patch 5: Creates /dev/dazukofs.ign as an (optional) mechanism for any
processes to hide themselves from DazukoFS file access control.
Device permissions can be best set using udev rules. The file "Documentation/filesystems/dazukofs.txt" (also updated with each patch) describes how to use and write applications for DazukoFS.
John Ogness
PATCH v2 1/5 VFS: DazukoFS, stackable-fs, file access control
Patch 1: Introduces DazukoFS as a nullfs. This is the raw stackable
filesystem part of DazukoFS and does nothing except stack and pass filesystem calls to the lower filesystem.
Patched against 2.6.29-rc3.
--- Documentation/filesystems/dazukofs.txt | 81 ++ fs/Kconfig | 1 fs/Makefile | 1 fs/dazukofs/Kconfig | 10 fs/dazukofs/Makefile | 7 fs/dazukofs/dazukofs_fs.h | 163 ++++ fs/dazukofs/dentry.c | 163 ++++ fs/dazukofs/file.c | 324 +++++++++ fs/dazukofs/inode.c | 813 +++++++++++++++++++++++ fs/dazukofs/mmap.c | 116 +++ fs/dazukofs/super.c | 335 +++++++++ 11 files changed, 2014 insertions(+)
fs/dazukofs/dazukofs_fs.h new file +163 -0
@@ -0,0 +1,163 @@ +/* dazukofs: access control stackable filesystem + +Copyright (C) 1997-2004 Erez Zadok +Copyright (C) 2001-2004 Stony Brook University +Copyright (C) 2004-2007 International Business Machines Corp. +Copyright (C) 2008-2009 John Ogness +Author: John Ogness <[email protected]> + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#ifndef __DAZUKOFS_FS_H +#define __DAZUKOFS_FS_H + +#include <linux/slab.h> +#include <linux/fs.h> +#include <linux/dcache.h> +#include <linux/mount.h> + +extern struct kmem_cache *dazukofs_dentry_info_cachep; +extern struct kmem_cache *dazukofs_file_info_cachep; +extern const struct file_operations dazukofs_main_fops; +extern const struct file_operations dazukofs_dir_fops; +extern struct dentry_operations dazukofs_dops; +extern const struct address_space_operations dazukofs_aops; + +extern int dazukofs_interpose (struct dentry *lower_dentry, +struct dentry *dentry,struct super_block *sb, +int already_hashed); + +struct dazukofs_sb_info { +struct super_block *lower_sb; +}; + +struct dazukofs_inode_info { +struct inode *lower_inode; + +/* +* the inode (embedded) +*/ +struct inode vfs_inode; +}; + +struct dazukofs_dentry_info { +struct dentry *lower_dentry; +struct vfsmount *lower_mnt; +}; + +struct dazukofs_file_info { +struct file *lower_file; +}; + +static inline struct dazukofs_sb_info *get_sb_private ( +struct super_block *upper_sb) +{ +return upper_sb->s_fs_info; +} + +static inline void set_sb_private (struct super_block *upper_sb, +struct dazukofs_sb_info *sbi) +{ + upper_sb->s_fs_info = sbi; +} + +static inline struct super_block *get_lower_sb (struct super_block *upper_sb) +{ +return get_sb_private (upper_sb)->lower_sb; +} + +static inline void set_lower_sb (struct super_block *upper_sb, +struct super_block *lower_sb) +{ +struct dazukofs_sb_info *sbi =get_sb_private (upper_sb); + sbi->lower_sb = lower_sb; +} + +static inline struct dazukofs_inode_info *get_inode_private ( +struct inode *upper_inode) +{ + return container_of(upper_inode, struct dazukofs_inode_info, + vfs_inode); +} + +static inline struct inode *get_lower_inode (struct inode *upper_inode) +{ +return get_inode_private (upper_inode)->lower_inode; +} + +static inline void set_lower_inode (struct inode *upper_inode, +struct inode *lower_inode) +{ +struct dazukofs_inode_info *dii =get_inode_private (upper_inode); + dii->lower_inode = lower_inode; +} + +static inline struct dazukofs_dentry_info *get_dentry_private ( +struct dentry *upper_dentry) +{ +return upper_dentry->d_fsdata; +} + +static inline void set_dentry_private (struct dentry *upper_dentry, +struct dazukofs_dentry_info *dentryi) +{ + upper_dentry->d_fsdata = dentryi; +} + +static inline struct dentry *get_lower_dentry (struct dentry *upper_dentry) +{ +return get_dentry_private (upper_dentry)->lower_dentry; +} + +static inline struct vfsmount *get_lower_mnt (struct dentry *upper_dentry) +{ +return get_dentry_private (upper_dentry)->lower_mnt; +} + +static inline void set_lower_dentry (struct dentry *upper_dentry, +struct dentry *lower_dentry, +struct vfsmount *lower_mnt) +{ +struct dazukofs_dentry_info *dii =get_dentry_private (upper_dentry); + dii->lower_dentry = lower_dentry; + dii->lower_mnt = lower_mnt; +} + +static inline struct dazukofs_file_info *get_file_private ( +struct file *upper_file) +{ +return upper_file->private_data; +} + +static inline void set_file_private (struct file *upper_file, +struct dazukofs_file_info *filei) +{ + upper_file->private_data = filei; +} + +static inline struct file *get_lower_file (struct file *upper_file) +{ +return get_file_private (upper_file)->lower_file; +} + +static inline void set_lower_file (struct file *upper_file, +struct file *lower_file) +{ +struct dazukofs_file_info *dfi =get_file_private (upper_file); + dfi->lower_file = lower_file; +} + +#endif /* __DAZUKOFS_FS_H */
fs/dazukofs/dentry.c new file +163 -0
163 lines not on this page · raw message
fs/dazukofs/super.c new file +335 -0
335 lines not on this page · raw message
fs/dazukofs/file.c new file +324 -0
324 lines not on this page · raw message
fs/dazukofs/inode.c new file +813 -0
813 lines not on this page · raw message
fs/dazukofs/mmap.c new file +116 -0
116 lines not on this page · raw message
fs/dazukofs/Makefile new file +7 -0
@@ -0,0 +1,7 @@ +# +# Makefile for the Linux dazukofs-filesystem routines. +# + +obj-$(CONFIG_DAZUKOFS_FS) += dazukofs.o + +dazukofs-objs := super.o inode.o file.o dentry.o mmap.o
fs/Makefile +1 -0
@@ -85,6 +85,7 @@ obj-$(CONFIG_HFSPLUS_FS) += hfsplus/ # Before hfs to find wrapped HFS+ obj-$(CONFIG_HFS_FS) += hfs/ obj-$(CONFIG_ECRYPT_FS) += ecryptfs/ +obj-$(CONFIG_DAZUKOFS_FS) += dazukofs/ obj-$(CONFIG_VXFS_FS) += freevxfs/ obj-$(CONFIG_NFS_FS) += nfs/ obj-$(CONFIG_EXPORTFS) += exportfs/
Documentation/filesystems/dazukofs.txt new file +81 -0
81 lines not on this page · raw message
fs/dazukofs/Kconfig new file +10 -0
@@ -0,0 +1,10 @@ +config DAZUKOFS_FS + tristate "DazukoFS filesystem layer support (EXPERIMENTAL)" + depends on EXPERIMENTAL + help + A pass-through stackable filesystem (also referred to as nullfs). + See <file:Documentation/filesystems/dazukofs.txt> to learn more + about DazukoFS. + + To compile this file system support as a module, choose M here: the + module will be called dazukofs.
fs/Kconfig +1 -0
@@ -204,6 +204,7 @@ source "fs/adfs/Kconfig" source "fs/affs/Kconfig" source "fs/ecryptfs/Kconfig" +source "fs/dazukofs/Kconfig" source "fs/hfs/Kconfig" source "fs/hfsplus/Kconfig" source "fs/befs/Kconfig"
PATCH v2 2/5 VFS: DazukoFS, stackable-fs, file access control
Patch 2: Creates /dev/dazukofs.0 for userspace applications to perform
file access control. At this point, all applications are considered to be working together (in the same group).
Patched against 2.6.29-rc3.
--- Documentation/filesystems/dazukofs.txt | 89 +++ fs/dazukofs/Kconfig | 3 fs/dazukofs/Makefile | 3 fs/dazukofs/dev.c | 84 +++ fs/dazukofs/dev.h | 37 + fs/dazukofs/event.c | 630 +++++++++++++++++++++++ fs/dazukofs/event.h | 32 + fs/dazukofs/file.c | 7 fs/dazukofs/group_dev.c | 159 +++++ fs/dazukofs/super.c | 16 10 files changed, 1047 insertions(+), 13 deletions(-)
fs/dazukofs/dev.c new file +84 -0
@@ -0,0 +1,84 @@ +/* dazukofs: access control stackable filesystem + +Copyright (C) 2008-2009 John Ogness +Author: John Ogness <[email protected]> + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#include <linux/device.h> +#include <linux/fs.h> +#include <linux/cdev.h> +#include <linux/uaccess.h> + +#include "dazukofs_fs.h" +#include "event.h" +#include "dev.h" + +static struct class *dazukofs_class; + +static int dev_major; +static int dev_minor_start; +static int dev_minor_end; + +int dazukofs_dev_init (void ) +{ +int err; +dev_t devt; + + err =dazukofs_init_events (); +if (err) + goto error_out1; + + err =alloc_chrdev_region (&devt,0 ,1 ,DEVICE_NAME ); +if (err) + goto error_out2; + dev_major =MAJOR (devt); + dev_minor_start =MINOR (devt); + + dazukofs_class =class_create (THIS_MODULE ,DEVICE_NAME ); +if (IS_ERR (dazukofs_class)) { + err =PTR_ERR (dazukofs_class); + goto error_out3; + } + + dev_minor_end =dazukofs_group_dev_init (dev_major, + dev_minor_start +1 , + dazukofs_class); +if (dev_minor_end <0 ) { + err = dev_minor_end; + goto error_out4; + } + +return 0 ; + +error_out4: +class_destroy (dazukofs_class); +error_out3: +unregister_chrdev_region (MKDEV (dev_major, dev_minor_start),1 ); +error_out2: +dazukofs_destroy_events (); +error_out1: +return err; +} + +void dazukofs_dev_destroy (void ) +{ +dazukofs_group_dev_destroy (dev_major, dev_minor_start +1 , + dev_minor_end, dazukofs_class); +class_destroy (dazukofs_class); +unregister_chrdev_region (MKDEV (dev_major, dev_minor_start),1 ); +dazukofs_destroy_events (); +}
fs/dazukofs/group_dev.c new file +159 -0
159 lines not on this page · raw message
fs/dazukofs/event.c new file +630 -0
630 lines not on this page · raw message
fs/dazukofs/dev.h new file +37 -0
@@ -0,0 +1,37 @@ +/* dazukofs: access control stackable filesystem + +Copyright (C) 2008-2009 John Ogness +Author: John Ogness <[email protected]> + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#ifndef __DEV_H +#define __DEV_H + +#include <linux/device.h> + +#define DEVICE_NAME "dazukofs" + +extern int dazukofs_dev_init (void ); +extern void dazukofs_dev_destroy (void ); + +extern int dazukofs_group_dev_init (int dev_major,int dev_minor_start, +struct class *dazukofs_class); +extern void dazukofs_group_dev_destroy (int dev_major,int dev_minor_start, +int dev_minor_end, +struct class *dazukofs_class); + +#endif /* __DEV_H */
fs/dazukofs/Makefile +2 -1
@@ -4,4 +4,5 @@ obj-$(CONFIG_DAZUKOFS_FS) += dazukofs.o -dazukofs-objs := super.o inode.o file.o dentry.o mmap.o +dazukofs-objs := super.o inode.o file.o dentry.o mmap.o event.o \ + dev.o group_dev.o
fs/dazukofs/super.c +12 -4
@@ -28,6 +28,7 @@#include <linux/mount.h> #include "dazukofs_fs.h" +#include "dev.h" static struct kmem_cache *dazukofs_inode_info_cachep;static struct kmem_cache *dazukofs_sb_info_cachep;
@@ -304,19 +305,25 @@ {int err; - err =init_caches (); + err =dazukofs_dev_init ();if (err) goto error_out1; - err =register_filesystem (&dazukofs_fs_type); + err =init_caches ();if (err) goto error_out2; + err =register_filesystem (&dazukofs_fs_type); +if (err) + goto error_out3; +printk (KERN_INFO "dazukofs: loaded\n" );return 0 ; -error_out2: +error_out3:destroy_caches (); +error_out2: +dazukofs_dev_destroy (); error_out1:return err; }
@@ -325,11 +332,12 @@ {unregister_filesystem (&dazukofs_fs_type);destroy_caches (); +dazukofs_dev_destroy ();printk (KERN_INFO "dazukofs: unloaded\n" ); }MODULE_AUTHOR ("John Ogness" ); -MODULE_DESCRIPTION ("pass-through stackable filesystem" ); +MODULE_DESCRIPTION ("access control stackable filesystem" );MODULE_LICENSE ("GPL" );module_init (init_dazukofs_fs)module_exit (exit_dazukofs_fs)
fs/dazukofs/file.c +6 -1
@@ -29,6 +29,7 @@#include <linux/cred.h> #include "dazukofs_fs.h" +#include "event.h" /** * Description: Called when the VFS needs to move the file position index.
@@ -167,7 +168,11 @@struct dentry *lower_dentry =dget (get_lower_dentry (dentry));struct vfsmount *lower_mnt =mntget (get_lower_mnt (dentry));struct file *lower_file; -int err =0 ; +int err; + + err =dazukofs_check_access (file->f_dentry, file->f_vfsmnt); +if (err) + goto error_out1;set_file_private (file,kmem_cache_zalloc (dazukofs_file_info_cachep,GFP_KERNEL ));
fs/dazukofs/event.h new file +32 -0
@@ -0,0 +1,32 @@ +/* dazukofs: access control stackable filesystem + +Copyright (C) 2008 John Ogness +Author: John Ogness <[email protected]> + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#ifndef __EVENT_H +#define __EVENT_H + +extern int dazukofs_init_events (void ); +extern void dazukofs_destroy_events (void ); + +extern int dazukofs_get_event (unsigned long *event_id,int *fd,pid_t *pid); +extern int dazukofs_return_event (unsigned long event_id,int deny); + +extern int dazukofs_check_access (struct dentry *dentry,struct vfsmount *mnt); + +#endif /* __EVENT_H */
fs/dazukofs/Kconfig +2 -1
@@ -2,7 +2,8 @@ tristate "DazukoFS filesystem layer support (EXPERIMENTAL)" depends on EXPERIMENTAL help - A pass-through stackable filesystem (also referred to as nullfs). + A stackable filesystem to allow userspace applications to perform + online file access control. See <file:Documentation/filesystems/dazukofs.txt> to learn more about DazukoFS.
Documentation/filesystems/dazukofs.txt +83 -6
112 lines not on this page · raw message
PATCH v2 3/5 VFS: DazukoFS, stackable-fs, file access control
Patch 3: Creates /dev/dazukofs.ctrl to allow for groups to be added,
listed, and deleted. Also, /dev/dazukofs.[0-9] devices are created to support multiple groups.
Patched against 2.6.29-rc3.
--- Documentation/filesystems/dazukofs.txt | 73 +++ fs/dazukofs/Makefile | 2 fs/dazukofs/ctrl_dev.c | 198 ++++++++++ fs/dazukofs/dev.c | 18 fs/dazukofs/dev.h | 6 fs/dazukofs/event.c | 445 ++++++++++++++++++++--- fs/dazukofs/event.h | 10 fs/dazukofs/group_dev.c | 107 ++++- 8 files changed, 775 insertions(+), 84 deletions(-)
fs/dazukofs/event.c +405 -40
@@ -26,6 +26,7 @@#include <linux/freezer.h> #include <linux/cred.h> +#include "dev.h" #include "dazukofs_fs.h" struct dazukofs_proc {
@@ -56,20 +57,31 @@ };struct dazukofs_group { +struct list_head list; +char *name; +size_t name_length; +unsigned long group_id;struct dazukofs_event_container todo_list;wait_queue_head_t queue;struct dazukofs_event_container working_list; +atomic_t use_count; +int deprecated; }; -static struct dazukofs_group reg_group; +static struct dazukofs_group group_list; +static int group_count; + +/* protects: group_list, group_count */ +static rwlock_t group_count_rwlock; -/* protects: grp->members, last_event_id, +/* protects: group_list, grp->members, last_event_id, * todo_list, working_list */ static struct mutex work_mutex;static struct mutex proc_mutex;static struct dazukofs_proc proc_list; +static struct kmem_cache *dazukofs_group_cachep;static struct kmem_cache *dazukofs_event_container_cachep;static struct kmem_cache *dazukofs_event_cachep;
@@ -87,11 +99,17 @@ {mutex_init (&proc_mutex);mutex_init (&work_mutex); +rwlock_init (&group_count_rwlock); +INIT_LIST_HEAD (&proc_list.list); +INIT_LIST_HEAD (&group_list.list); -init_waitqueue_head (®_group.queue); -INIT_LIST_HEAD (®_group.todo_list.list); -INIT_LIST_HEAD (®_group.working_list.list); + dazukofs_group_cachep = +kmem_cache_create ("dazukofs_group_cache" , +sizeof (struct dazukofs_group ),0 , +SLAB_HWCACHE_ALIGN ,NULL ); +if (!dazukofs_group_cachep) + goto error_out; dazukofs_event_container_cachep = kmem_cache_create("dazukofs_event_container_cache",
@@ -110,6 +128,8 @@return 0 ; error_out: +if (dazukofs_group_cachep) +kmem_cache_destroy (dazukofs_group_cachep);if (dazukofs_event_container_cachep)kmem_cache_destroy (dazukofs_event_container_cachep);if (dazukofs_event_cachep)
@@ -211,10 +231,17 @@* Description: All pending and in-progress events are released/freed. * Any processes waiting on the queue are woken. * +* The actual group structure is not deleted, but rather marked as +* deprecated. Deprecated group structures are deleted as new +* groups are added. +* * IMPORTANT: This function requires work_mutex to be held! */ static void __remove_group (struct dazukofs_group *grp) { + grp->deprecated =1 ; + group_count--; +__clear_group_event_list (&grp->working_list.list);__clear_group_event_list (&grp->todo_list.list);
@@ -229,17 +256,272 @@*/ void dazukofs_destroy_events (void ) { -/* free the group items */ -mutex_lock (&work_mutex); -__remove_group (®_group); -mutex_unlock (&work_mutex); +struct dazukofs_group *grp; +struct list_head *pos; +struct list_head *q; + +/* +* We are not using any locks here because we assume +* everything else has been already cleaned up by +* the device layer. +*/ + +/* free the groups */ +list_for_each_safe (pos, q, &group_list.list) { + grp = list_entry(pos, struct dazukofs_group, list); +list_del (pos); + +__remove_group (grp); + +/* free group name */ +kfree (grp->name); + +/* free group */ +kmem_cache_free (dazukofs_group_cachep, grp); + }/* free everything else */ +kmem_cache_destroy (dazukofs_group_cachep);kmem_cache_destroy (dazukofs_event_container_cachep);kmem_cache_destroy (dazukofs_event_cachep); }/** +* __check_for_group - check if a group exists +* @name: a group name to check for +* @id: a group id to check for +* @already_exists: will be set if the group already exists +* +* Description: This function checks names and id's to see if a group may +* be created. If the id already exists, but with a different group name, +* the group cannot be created. If the group name exists, but with a +* different id, the group cannot be created. +* +* If the group name exists and the id is already that which is requested, +* the function returns success, but sets the already_exists flag. +* +* IMPORTANT: This function requires work_mutex to be held! +* +* Returns 0 if the group exists or may be created. +*/ +static int __check_for_group (const char *name,int id,int *already_exists) +{ +struct dazukofs_group *grp; +struct list_head *pos; +struct list_head *q; +int id_available =1 ; + + *already_exists =0 ; + +list_for_each_safe (pos, q, &group_list.list) { + grp = list_entry(pos, struct dazukofs_group, list); +if (grp->deprecated) { +/* cleanup deprecated groups */ +if (atomic_read (&grp->use_count) ==0 ) { +list_del (pos); +kfree (grp->name); +kmem_cache_free (dazukofs_group_cachep, grp); + } + }else { +if (strcmp (name, grp->name) ==0 ) { + *already_exists =1 ; +break ; + }else if (grp->group_id == id) { + id_available =0 ; +break ; + } + } + } + +if (*already_exists) +return 0 ; + +if (id_available) { +/* we have found a free id */ +return 0 ; + } + +return -1 ; +} + +/** +* __create_group - allocate and initialize a group structure +* @name: the name of the new group +* @id: the id of the new group +* +* Description: This function allocates and initializes a group +* structure. The group_count should be locked to ensure that +* the group id remains available until the group can be +* added to the group list. +* +* Returns the newly created and initialized group structure. +*/ +static struct dazukofs_group *__create_group (const char *name,int id) +{ +struct dazukofs_group *grp; + + grp =kmem_cache_zalloc (dazukofs_group_cachep,GFP_KERNEL ); +if (!grp) +return NULL ; + +atomic_set (&grp->use_count,0 ); + grp->group_id = id; + grp->name =kstrdup (name,GFP_KERNEL ); +if (!grp->name) { +kmem_cache_free (dazukofs_group_cachep, grp); +return NULL ; + } + grp->name_length =strlen (name); +init_waitqueue_head (&grp->queue); +INIT_LIST_HEAD (&grp->todo_list.list); +INIT_LIST_HEAD (&grp->working_list.list); +return grp; +} + +/** +* dazukofs_add_group - add a new group +* @name: the name of the group to add +* +* Description: This function is called by the device layer to add a new +* group. It returns success if the group has been successfully created +* or if the group already exists. +* +* Returns 0 on success. +*/ +int dazukofs_add_group (const char *name) +{ +int ret =0 ; +int already_exists; +int available_id =0 ; +struct dazukofs_group *grp; + +write_lock (&group_count_rwlock); + +mutex_lock (&work_mutex); +while (__check_for_group (name, available_id, &already_exists) !=0 ) { +/* try again with the next id */ + available_id++; + } +mutex_unlock (&work_mutex); + +if (already_exists) + goto out; + +/* if we are here, the group doesn't already exist */ + +/* do we have room for a new group? */ +if (group_count ==GROUP_COUNT ) { + ret = -EPERM ; + goto out; + } + + grp =__create_group (name, available_id); +if (!grp) { + ret = -ENOMEM ; + goto out; + } + +mutex_lock (&work_mutex); +list_add_tail (&grp->list, &group_list.list); +mutex_unlock (&work_mutex); + + group_count++; +out: +write_unlock (&group_count_rwlock); +return ret; +} + +/** +* dazukofs_remove_group - remove a group +* @name: the name of the group to remove +* +* Description: This function is called by the device layer to remove a +* group. It returns success if the group has been deleted or the group +* does not exist. +* +* Returns 0 on success. +*/ +int dazukofs_remove_group (const char *name) +{ +int ret =0 ; +struct dazukofs_group *grp; +struct list_head *pos; + +write_lock (&group_count_rwlock); + +if (group_count ==0 ) + goto out; + +mutex_lock (&work_mutex); +/* set group deprecated */ +list_for_each (pos, &group_list.list) { + grp = list_entry(pos, struct dazukofs_group, list); +if (!grp->deprecated &&strcmp (name, grp->name) ==0 ) { +__remove_group (grp); +break ; + } + } +mutex_unlock (&work_mutex); +out: +write_unlock (&group_count_rwlock); +return ret; +} + +/** +* dazukofs_get_groups - get the names and id's of active groups as strings +* @buf: to be assigned the list of groups as a single printable string +* +* Description: This function will allocate a string that includes all the +* active (not deprecated) groups and their id's. This function is called +* by the device layer for presenting userspace with the list of groups. +* +* This function will allocate memory that must be freed by the caller. +* +* Returns 0 on success. +*/ +int dazukofs_get_groups (char **buf) +{ +struct dazukofs_group *grp; +char *tmp; +struct list_head *pos; +size_t buflen; +size_t allocsize =256 ; + +tryagain: + *buf =kzalloc (allocsize,GFP_KERNEL ); +if (!*buf) +return -ENOMEM ; + tmp = *buf; + buflen =1 ; + +mutex_lock (&work_mutex); +list_for_each (pos, &group_list.list) { + grp = list_entry(pos, struct dazukofs_group, list); +if (!grp->deprecated) + buflen += grp->name_length +3 ; + } +if (buflen < allocsize) { +list_for_each (pos, &group_list.list) { + grp = list_entry(pos, struct dazukofs_group, list); +if (!grp->deprecated) { +snprintf (tmp, (allocsize -1 ) - (tmp - *buf), +"%lu:%s\n" , grp->group_id, + grp->name); + tmp += grp->name_length +3 ; + } + } +mutex_unlock (&work_mutex); + }else { +mutex_unlock (&work_mutex); + allocsize *=2 ; +kfree (*buf); + goto tryagain; + } + +return 0 ; +} + +/** * check_recursion - check if current process is recursing * * Description: A list of anonymous processes is managed to prevent
@@ -296,14 +578,19 @@/** * check_access_precheck - check if an access event should be generated +* @grp_count: the current number of groups * * Description: Check if the current process should cause an access event * to be generated. * * Returns 0 if an access event should be generated. */ -static int check_access_precheck (void ) +static int check_access_precheck (int grp_count) { +/* do we have any groups? */ +if (grp_count ==0 ) +return -1 ; +/* am I a recursion process? */ if (!check_recursion ())return -1 ;
@@ -312,19 +599,22 @@ }/** -* assign_event_to_group - post an event to be processed +* assign_event_to_groups - post an event to be processed * @evt: the event to be posted -* @ec: the container for the event +* @ec_array: the containers for the event * * Description: This function will assign a unique id to the event. -* The event will be associated with its container and placed on the -* group's todo list. The group will also be woken to handle the new -* event. +* The event will be associated with each container and the container is +* placed on each group's todo list. Each group will also be woken to +* handle the new event. */ static void -assign_event_to_group (struct dazukofs_event *evt, -struct dazukofs_event_container *ec) { -struct dazukofs_group *grp = ®_group; +assign_event_to_groups (struct dazukofs_event *evt, +struct dazukofs_event_container *ec_array[]) +{ +struct dazukofs_group *grp; +struct list_head *pos; +int i;mutex_lock (&work_mutex);mutex_lock (&evt->assigned_mutex);
@@ -334,44 +624,67 @@ last_event_id++; evt->event_id = last_event_id; - ec->event = evt; - evt->assigned =1 ; -list_add_tail (&ec->list, &grp->todo_list.list); +/* assign the event to each group */ + i =0 ; +list_for_each (pos, &group_list.list) { + grp = list_entry(pos, struct dazukofs_group, list); +if (!grp->deprecated) { + ec_array[i]->event = evt; + + evt->assigned++; +list_add_tail (&ec_array[i]->list, + &grp->todo_list.list); -/* notify someone to handle the event */ -wake_up (&grp->queue); +/* notify someone to handle the event */ +wake_up (&grp->queue); + + i++; + } + }mutex_unlock (&evt->assigned_mutex);mutex_unlock (&work_mutex); }/** -* allocate_event_and_container - allocate an event and event container +* allocate_event_and_containers - allocate an event and event containers * @evt: event pointer to be assigned a new event -* @ec: event container to be assigned a new container +* @ec: event container array to be filled with new array of containers +* @grp_count: the number of groups (size of the array) * * Description: New event and event container structures are allocated * and initialized. * * Returns 0 on success. */ -static int allocate_event_and_container (struct dazukofs_event **evt, -struct dazukofs_event_container **ec) +static int +allocate_event_and_containers (struct dazukofs_event **evt, +struct dazukofs_event_container *ec_array[], +int grp_count) { +int i; + *evt =kmem_cache_zalloc (dazukofs_event_cachep,GFP_KERNEL );if (!*evt)return -1 ;init_waitqueue_head (&(*evt)->queue);mutex_init (&(*evt)->assigned_mutex); -/* allocate container now while we don't have a lock */ - *ec =kmem_cache_zalloc (dazukofs_event_container_cachep,GFP_KERNEL ); -if (!*ec) - goto error_out; +/* allocate containers now while we don't have a lock */ +for (i =0 ; i < grp_count; i++) { + ec_array[i] =kmem_cache_zalloc ( + dazukofs_event_container_cachep,GFP_KERNEL ); +if (!ec_array[i]) + goto error_out; + }return 0 ; error_out: +for (i--; i >=0 ; i--) { +kmem_cache_free (dazukofs_event_container_cachep, ec_array[i]); + ec_array[i] =NULL ; + }kmem_cache_free (dazukofs_event_cachep, *evt); *evt =NULL ;return -1 ;
@@ -389,16 +702,21 @@*/ int dazukofs_check_access (struct dentry *dentry,struct vfsmount *mnt) { -struct dazukofs_event_container *ec; +struct dazukofs_event_container *ec_array[GROUP_COUNT ];struct dazukofs_event *evt;int err; -if (check_access_precheck ()) +read_lock (&group_count_rwlock); + +if (check_access_precheck (group_count)) { +read_unlock (&group_count_rwlock);return 0 ; + }/* at this point, the access should be handled */ -if (allocate_event_and_container (&evt, &ec)) { +if (allocate_event_and_containers (&evt, ec_array, group_count)) { +read_unlock (&group_count_rwlock); err = -ENOMEM ; goto out; }
@@ -407,7 +725,9 @@ evt->mnt =mntget (mnt); evt->pid = current->pid; -assign_event_to_group (evt, ec); +assign_event_to_groups (evt, ec_array); + +read_unlock (&group_count_rwlock);/* wait until event completely processed or signal */ err =wait_event_freezable (evt->queue,event_assigned (evt) ==0 );
@@ -422,6 +742,7 @@/** * dazukofs_return_event - return checked file access results +* @group_id: id of the group the event came from * @event_id: the id of the event * @deny: a flag indicating if file access should be denied *
@@ -431,9 +752,10 @@* * Returns 0 on success. */ -int dazukofs_return_event (unsigned long event_id,int deny) +int dazukofs_return_event (unsigned long group_id,unsigned long event_id, +int deny) { -struct dazukofs_group *grp = ®_group; +struct dazukofs_group *grp;struct dazukofs_event_container *ec;struct dazukofs_event *evt =NULL ;struct list_head *pos;
@@ -441,6 +763,20 @@int ret =0 ;mutex_lock (&work_mutex); +list_for_each (pos, &group_list.list) { + grp = list_entry(pos, struct dazukofs_group, list); +if (!grp->deprecated && grp->group_id == group_id) { + found =1 ; +break ; + } + } +if (!found) { + ret = -EFAULT ; +mutex_unlock (&work_mutex); + goto out; + } + + found =0 ;list_for_each (pos, &grp->working_list.list) { ec = list_entry(pos, struct dazukofs_event_container, list); evt = ec->event;
@@ -457,6 +793,7 @@release_event (evt,1 , deny); else ret = -EFAULT ; +out:return ret; }
@@ -591,6 +928,7 @@/** * dazukofs_get_event - get an event to process +* @group_id: id of the group we belong to * @event_id: to be filled in with the new event id * @fd: to be filled in with the opened file descriptor * @pid: to be filled in with the pid of the process generating the event
@@ -601,18 +939,43 @@* * Returns 0 on success. */ -int dazukofs_get_event (unsigned long *event_id,int *fd,pid_t *pid) +int dazukofs_get_event (unsigned long group_id,unsigned long *event_id, +int *fd,pid_t *pid) { -struct dazukofs_group *grp = ®_group; +struct dazukofs_group *grp;struct dazukofs_event_container *ec; +struct list_head *pos; +int found =0 ;int ret =0 ; +mutex_lock (&work_mutex); +list_for_each (pos, &group_list.list) { + grp = list_entry(pos, struct dazukofs_group, list); +if (!grp->deprecated && grp->group_id == group_id) { +atomic_inc (&grp->use_count); + found =1 ; +break ; + } + } +mutex_unlock (&work_mutex); + +if (!found) { + ret = -EFAULT ; + goto out; + } +while (1 ) { ret =wait_event_freezable (grp->queue, -is_event_available (grp)); +is_event_available (grp) || + grp->deprecated);if (ret !=0 )break ; +if (grp->deprecated) { + ret = -EFAULT ; +break ; + } + ec =claim_event (grp);if (ec) { ret =open_file (ec);
@@ -626,5 +989,7 @@ } } } +atomic_dec (&grp->use_count); +out:return ret; }
fs/dazukofs/Makefile +1 -1
@@ -5,4 +5,4 @@ obj-$(CONFIG_DAZUKOFS_FS) += dazukofs.o dazukofs-objs := super.o inode.o file.o dentry.o mmap.o event.o \ - dev.o group_dev.o + dev.o group_dev.o ctrl_dev.o
fs/dazukofs/ctrl_dev.c new file +198 -0
198 lines not on this page · raw message
fs/dazukofs/group_dev.c +81 -26
156 lines not on this page · raw message
fs/dazukofs/dev.c +14 -4
@@ -42,7 +42,7 @@if (err) goto error_out1; - err =alloc_chrdev_region (&devt,0 ,1 ,DEVICE_NAME ); + err =alloc_chrdev_region (&devt,0 ,1 +GROUP_COUNT ,DEVICE_NAME );if (err) goto error_out2; dev_major =MAJOR (devt);
@@ -54,20 +54,28 @@ goto error_out3; } + err =dazukofs_ctrl_dev_init (dev_major, dev_minor_start, + dazukofs_class); +if (err) + goto error_out4; + dev_minor_end =dazukofs_group_dev_init (dev_major, dev_minor_start +1 , dazukofs_class);if (dev_minor_end <0 ) { err = dev_minor_end; - goto error_out4; + goto error_out5; }return 0 ; +error_out5: +dazukofs_ctrl_dev_destroy (dev_major, dev_minor_start, dazukofs_class); error_out4:class_destroy (dazukofs_class); error_out3: -unregister_chrdev_region (MKDEV (dev_major, dev_minor_start),1 ); +unregister_chrdev_region (MKDEV (dev_major, dev_minor_start), +1 +GROUP_COUNT ); error_out2:dazukofs_destroy_events (); error_out1:
@@ -78,7 +86,9 @@ {dazukofs_group_dev_destroy (dev_major, dev_minor_start +1 , dev_minor_end, dazukofs_class); +dazukofs_ctrl_dev_destroy (dev_major, dev_minor_start, dazukofs_class);class_destroy (dazukofs_class); -unregister_chrdev_region (MKDEV (dev_major, dev_minor_start),1 ); +unregister_chrdev_region (MKDEV (dev_major, dev_minor_start), +1 +GROUP_COUNT );dazukofs_destroy_events (); }
fs/dazukofs/dev.h +6 -0
@@ -24,6 +24,7 @@#include <linux/device.h> #define DEVICE_NAME "dazukofs" +#define GROUP_COUNT 10extern int dazukofs_dev_init (void );extern void dazukofs_dev_destroy (void );
@@ -34,4 +35,9 @@int dev_minor_end, struct class *dazukofs_class); +extern int dazukofs_ctrl_dev_init (int dev_major,int dev_minor, +struct class *dazukofs_class); +extern void dazukofs_ctrl_dev_destroy (int dev_major,int dev_minor, +struct class *dazukofs_class); +#endif /* __DEV_H */
fs/dazukofs/event.h +8 -2
@@ -24,9 +24,15 @@extern int dazukofs_init_events (void );extern void dazukofs_destroy_events (void ); -extern int dazukofs_get_event (unsigned long *event_id,int *fd,pid_t *pid); -extern int dazukofs_return_event (unsigned long event_id,int deny); +extern int dazukofs_get_event (unsigned long group_id, +unsigned long *event_id,int *fd,pid_t *pid); +extern int dazukofs_return_event (unsigned long group_id, +unsigned long event_id,int deny);extern int dazukofs_check_access (struct dentry *dentry,struct vfsmount *mnt); +extern int dazukofs_get_groups (char **buf); +extern int dazukofs_add_group (const char *name); +extern int dazukofs_remove_group (const char *name); +#endif /* __EVENT_H */
Documentation/filesystems/dazukofs.txt +62 -11
89 lines not on this page · raw message
PATCH v2 4/5 VFS: DazukoFS, stackable-fs, file access control
Patch 4: Adds a new (optional) command to allow registered processes
to be tracked. The tracking allows processes to be automatically unregistered if they crash and also allows groups to be automatically deleted if no more processes are registered.
Patched against 2.6.29-rc3.
--- Documentation/filesystems/dazukofs.txt | 24 ++++- fs/dazukofs/ctrl_dev.c | 15 ++- fs/dazukofs/event.c | 103 +++++++++++++++++++++-- fs/dazukofs/event.h | 7 + fs/dazukofs/group_dev.c | 30 ++++++ 5 files changed, 162 insertions(+), 17 deletions(-)
fs/dazukofs/ctrl_dev.c +11 -4
@@ -85,7 +85,7 @@ }static int process_command (char *buf,const char *key, -int (*func)(const char *), +int (*func)(const char *,int ),int arg2,int *retcode) {char *p;
@@ -104,7 +104,7 @@ *retcode = -EINVAL ; }else { *p2 =0 ; - *retcode =func (p); + *retcode =func (p, arg2); *p2 =' ' ; }
@@ -131,14 +131,21 @@if (!match || (match && ret >=0 )) {if (process_command (tmp,"del=" , - dazukofs_remove_group, &ret) ==0 ) { + dazukofs_remove_group,0 , &ret) ==0 ) { match =1 ; } }if (!match || (match && ret >=0 )) {if (process_command (tmp,"add=" , - dazukofs_add_group, &ret) ==0 ) { + dazukofs_add_group,0 , &ret) ==0 ) { + match =1 ; + } + } + +if (!match || (match && ret >=0 )) { +if (process_command (tmp,"addtrack=" , + dazukofs_add_group,1 , &ret) ==0 ) { match =1 ; } }
fs/dazukofs/event.c +96 -7
195 lines not on this page · raw message
fs/dazukofs/event.h +5 -2
@@ -31,8 +31,11 @@extern int dazukofs_check_access (struct dentry *dentry,struct vfsmount *mnt); +extern int dazukofs_group_open_tracking (unsigned long group_id); +extern void dazukofs_group_release_tracking (unsigned long group_id); +extern int dazukofs_get_groups (char **buf); -extern int dazukofs_add_group (const char *name); -extern int dazukofs_remove_group (const char *name); +extern int dazukofs_add_group (const char *name,int track); +extern int dazukofs_remove_group (const char *name,int unused);#endif /* __EVENT_H */
fs/dazukofs/group_dev.c +30 -0
@@ -27,6 +27,24 @@#include "event.h" #include "dev.h" +static int dazukofs_group_open (int group_id,struct inode *inode, +struct file *file) +{ +if (dazukofs_group_open_tracking (group_id)) + file->private_data = file; +else + file->private_data =NULL ; +return 0 ; +} + +static int dazukofs_group_release (int group_id,struct inode *inode, +struct file *file) +{ +if (file->private_data) +dazukofs_group_release_tracking (group_id); +return 0 ; +} +static ssize_t dazukofs_group_read (int group_id,struct file *file,char __user *buffer,size_t length,loff_t *pos)
@@ -112,6 +130,16 @@ }#define DECLARE_GROUP_FOPS (group_id) \ +static int \ +dazukofs_group_open_##group_id(struct inode *inode, struct file *file) \ +{ \ + return dazukofs_group_open(group_id, inode, file); \ +} \ +static int \ +dazukofs_group_release_##group_id(struct inode *inode, struct file *file) \ +{ \ + return dazukofs_group_release(group_id, inode, file); \ +} \ static ssize_t \ dazukofs_group_read_##group_id(struct file *file, char __user *buffer, \ size_t length, loff_t *pos) \
@@ -127,6 +155,8 @@ } \ static const struct file_operations group_fops_##group_id = { \ .owner = THIS_MODULE, \ + .open = dazukofs_group_open_##group_id, \ + .release = dazukofs_group_release_##group_id, \ .read = dazukofs_group_read_##group_id, \ .write = dazukofs_group_write_##group_id, \ };
Documentation/filesystems/dazukofs.txt +20 -4
@@ -180,10 +180,26 @@ By closing the device, the application will unregister itself. -NOTE: It is not necessary for the device to be open while the application - decides if access should be allowed. In fact, it doesn't even have to - be the same process that responds. DazukoFS is only interested in a - response that matches the pending event id. +To provide crash protection for applications, groups can be added using +the "addtrack" keyword instead of "add". The keyword "addtrack" tells +DazukoFS to add the group and track the number of registered processes in +that group. Tracking begins as soon as the first process has registered +with DazukoFS. Once all processes of the group have unregistered, DazukoFS +will automatically delete the group. + +If an application crashes, is killed, or ends without closing the device, +DazukoFS will still unregister that process. Using "addtrack" will ensure +that a created group is automatically deleted if the application is not +able to shutdown in a clean manner. + +NOTE: If "addtrack" is used, it is necessary for the device to be kept open + the entire time the application is performing online file access + control. Otherwise the group may become unintentionally deleted. + + If "add" is used, it is not necessary for the device to be kept open + while the application decides if access should be allowed. Actually, + DazukoFS doesn't care which process responds to a file access event. + DazukoFS is only interested in a response for the given event id. A group can be deleted by writing to the /dev/dazukofs.ctrl device. For example, writing:
PATCH v2 5/5 VFS: DazukoFS, stackable-fs, file access control
Patch 5: Creates /dev/dazukofs.ign as an (optional) mechanism for any
processes to hide themselves from DazukoFS file access control.
Patched against 2.6.29-rc3.
--- Documentation/filesystems/dazukofs.txt | 24 ++ fs/dazukofs/Makefile | 2 fs/dazukofs/dev.c | 22 +- fs/dazukofs/dev.h | 6 fs/dazukofs/event.c | 4 fs/dazukofs/ign_dev.c | 186 +++++++++++++++++++++++ 6 files changed, 237 insertions(+), 7 deletions(-)
fs/dazukofs/ign_dev.c new file +186 -0
@@ -0,0 +1,186 @@ +/* dazukofs: access control stackable filesystem + +Copyright (C) 2008 John Ogness +Author: John Ogness <[email protected]> + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#include <linux/device.h> +#include <linux/fs.h> +#include <linux/cdev.h> +#include <linux/uaccess.h> + +#include "dazukofs_fs.h" +#include "dev.h" + +struct dazukofs_proc { +struct list_head list; +struct task_struct *curr; +}; + +static struct dazukofs_proc ign_list; +static struct mutex ign_list_mutex; +static struct kmem_cache *dazukofs_ign_cachep; + +int dazukofs_check_ignore_process (void ) +{ +struct list_head *pos; +struct dazukofs_proc *proc; +int found =0 ; + +mutex_lock (&ign_list_mutex); +list_for_each (pos, &ign_list.list) { + proc = list_entry(pos, struct dazukofs_proc, list); +if (proc->curr == current) { + found =1 ; +break ; + } + } +mutex_unlock (&ign_list_mutex); + +return !found; +} + +static int dazukofs_add_ign (struct file *file) +{ +struct dazukofs_proc *proc = +kmem_cache_zalloc (dazukofs_ign_cachep,GFP_KERNEL ); +if (!proc) { + file->private_data =NULL ; +return -ENOMEM ; + } + + file->private_data = proc; + proc->curr = current; + +mutex_lock (&ign_list_mutex); +list_add (&proc->list, &ign_list.list); +mutex_unlock (&ign_list_mutex); + +return 0 ; +} + +static void dazukofs_remove_ign (struct file *file) +{ +struct list_head *pos; +struct dazukofs_proc *proc =NULL ; +struct dazukofs_proc *check_proc = file->private_data; +int found =0 ; + +if (!check_proc) +return ; + +mutex_lock (&ign_list_mutex); +list_for_each (pos, &ign_list.list) { + proc = list_entry(pos, struct dazukofs_proc, list); +if (proc->curr == check_proc->curr) { + found =1 ; +list_del (pos); +break ; + } + } +mutex_unlock (&ign_list_mutex); + +if (found) { + file->private_data =NULL ; +kmem_cache_free (dazukofs_ign_cachep, proc); + } +} + +static int dazukofs_ign_open (struct inode *inode,struct file *file) +{ +return dazukofs_add_ign (file); +} + +static int dazukofs_ign_release (struct inode *inode,struct file *file) +{ +dazukofs_remove_ign (file); +return 0 ; +} + +static void dazukofs_destroy_ignlist (void ) +{ +struct list_head *pos; +struct list_head *q; +struct dazukofs_proc *proc; + +list_for_each_safe (pos, q, &ign_list.list) { + proc = list_entry(pos, struct dazukofs_proc, list); +list_del (pos); +kmem_cache_free (dazukofs_ign_cachep, proc); + } +} + +static struct cdev ign_cdev; + +static const struct file_operations ign_fops = { + .owner =THIS_MODULE , + .open = dazukofs_ign_open, + .release = dazukofs_ign_release, +}; + +int dazukofs_ign_dev_init (int dev_major,int dev_minor, +struct class *dazukofs_class) +{ +int err =0 ; +struct device *dev; + +INIT_LIST_HEAD (&ign_list.list); +mutex_init (&ign_list_mutex); + + dazukofs_ign_cachep = +kmem_cache_create ("dazukofs_ign_cache" , +sizeof (struct dazukofs_proc ),0 , +SLAB_HWCACHE_ALIGN ,NULL ); +if (!dazukofs_ign_cachep) { + err = -ENOMEM ; + goto error_out1; + } + +/* setup cdev for ignore */ +cdev_init (&ign_cdev, &ign_fops); + ign_cdev.owner =THIS_MODULE ; + err =cdev_add (&ign_cdev,MKDEV (dev_major, dev_minor),1 ); +if (err) + goto error_out2; + +/* create ignore device */ + dev =device_create (dazukofs_class,NULL ,MKDEV (dev_major, dev_minor), +NULL ,"%s.ign" ,DEVICE_NAME ); +if (IS_ERR (dev)) { + err =PTR_ERR (dev); + goto error_out3; + } + +return 0 ; + +error_out3: +cdev_del (&ign_cdev); +error_out2: +dazukofs_destroy_ignlist (); +kmem_cache_destroy (dazukofs_ign_cachep); +error_out1: +return err; +} + +void dazukofs_ign_dev_destroy (int dev_major,int dev_minor, +struct class *dazukofs_class) +{ +device_destroy (dazukofs_class,MKDEV (dev_major, dev_minor)); +cdev_del (&ign_cdev); +dazukofs_destroy_ignlist (); +kmem_cache_destroy (dazukofs_ign_cachep); +}
fs/dazukofs/Makefile +1 -1
@@ -5,4 +5,4 @@ obj-$(CONFIG_DAZUKOFS_FS) += dazukofs.o dazukofs-objs := super.o inode.o file.o dentry.o mmap.o event.o \ - dev.o group_dev.o ctrl_dev.o + dev.o group_dev.o ctrl_dev.o ign_dev.o
fs/dazukofs/event.c +4 -0
@@ -619,6 +619,10 @@if (!check_recursion ())return -1 ; +/* am I an ignored process? */ +if (!dazukofs_check_ignore_process ()) +return -1 ; +return 0 ; }
fs/dazukofs/dev.c +16 -6
@@ -42,7 +42,7 @@if (err) goto error_out1; - err =alloc_chrdev_region (&devt,0 ,1 +GROUP_COUNT ,DEVICE_NAME ); + err =alloc_chrdev_region (&devt,0 ,2 +GROUP_COUNT ,DEVICE_NAME );if (err) goto error_out2; dev_major =MAJOR (devt);
@@ -59,23 +59,31 @@if (err) goto error_out4; + err =dazukofs_ign_dev_init (dev_major, dev_minor_start +1 , + dazukofs_class); +if (err) + goto error_out5; + dev_minor_end =dazukofs_group_dev_init (dev_major, - dev_minor_start +1 , + dev_minor_start +2 , dazukofs_class);if (dev_minor_end <0 ) { err = dev_minor_end; - goto error_out5; + goto error_out6; }return 0 ; +error_out6: +dazukofs_ign_dev_destroy (dev_major, dev_minor_start +1 , + dazukofs_class); error_out5:dazukofs_ctrl_dev_destroy (dev_major, dev_minor_start, dazukofs_class); error_out4:class_destroy (dazukofs_class); error_out3:unregister_chrdev_region (MKDEV (dev_major, dev_minor_start), -1 +GROUP_COUNT ); +2 +GROUP_COUNT ); error_out2:dazukofs_destroy_events (); error_out1:
@@ -84,11 +92,13 @@void dazukofs_dev_destroy (void ) { -dazukofs_group_dev_destroy (dev_major, dev_minor_start +1 , +dazukofs_group_dev_destroy (dev_major, dev_minor_start +2 , dev_minor_end, dazukofs_class); +dazukofs_ign_dev_destroy (dev_major, dev_minor_start +1 , + dazukofs_class);dazukofs_ctrl_dev_destroy (dev_major, dev_minor_start, dazukofs_class);class_destroy (dazukofs_class);unregister_chrdev_region (MKDEV (dev_major, dev_minor_start), -1 +GROUP_COUNT ); +2 +GROUP_COUNT );dazukofs_destroy_events (); }
fs/dazukofs/dev.h +6 -0
@@ -40,4 +40,10 @@extern void dazukofs_ctrl_dev_destroy (int dev_major,int dev_minor,struct class *dazukofs_class); +extern int dazukofs_ign_dev_init (int dev_major,int dev_minor, +struct class *dazukofs_class); +extern void dazukofs_ign_dev_destroy (int dev_major,int dev_minor, +struct class *dazukofs_class); +extern int dazukofs_check_ignore_process (void ); +#endif /* __DEV_H */
Documentation/filesystems/dazukofs.txt +24 -0
@@ -223,3 +223,27 @@ access events to be handled by each group. For this reason it is important that an application deletes a group it has created, once it should no longer perform online file access control. + +All processes on the system that try to access files on a DazukoFS mount will +require authorization (if at least one group exists). This is also true for +registered process that try to access files on a DazukoFS mount. + +IMPORTANT: If registered processes access files on a DazukoFS mount, they + will cause new file access events that must be authorized. This + could lead to deadlock if not properly considered. + +Since the registered process receives an open file descriptor to the file +being accessed, there should be no need for that process to open other +files. However, if the process must open additional files (and these +files potentially lie on a DazukoFS mount), it is possible for processes +to hide themselves from DazukoFS. + +By opening the /dev/dazukofs.ign device, a process will be ignored by +DazukoFS. It does not matter if the process is registered or not. No data +must be written or read from the device. It simply needs to be opened. + +WARNING: Make sure the permissions for /dev/dazukofs.ign are securely + set. Otherwise, any process could potentially hide itself. + +As soon as the /dev/dazukofs.ign device is closed, the process is no +longer hidden.
Quoted 3 lines · John Ogness
Patch 5: Creates
/dev/dazukofs.ignas an (optional) mechanism for any
processes to hide themselves from DazukoFS file access control.
Your reference counting is broken. What happens if a process exits and you are ignoring it.
When fixing this use a struct pid * not struct task_struct *. That is what struct pid is for there is no need to pin the entire task struct into memory after the process exits just because you decided to ignore it.
Eric
Quoted 3 lines · John Ogness
Patch 3: Creates
/dev/dazukofs.ctrlto allow for groups to be added,
listed, and deleted. Also, /dev/dazukofs.[0-9] devices are created to support multiple groups.
Great another global namespace for process groups. What is your plan for making this work in a container?
Eric
Quoted 6 lines · On 2009-02-12, [email protected] (Eric W. Biederman) wrote:
Quoted 3 lines
Patch 3: Creates
/dev/dazukofs.ctrlto allow for groups to be added,
listed, and deleted. Also, /dev/dazukofs.[0-9] devices are created to support multiple groups.Great another global namespace for process groups. What is your plan for making this work in a container?
It is good that you mention it. This is something that I totally missed. I am not yet certain what kind of a role DazukoFS should have within containers. I need to learn the container API's and see what kinds of options are available. The next patchset (which it seems won't be coming too quickly now) will address this in some form.
John Ogness
Quoted 5 lines · John Ogness
Quoted 4 lines · On 2009-02-12, [email protected] (Eric W. Biederman) wrote:
Quoted 3 lines
Patch 3: Creates
/dev/dazukofs.ctrlto allow for groups to be added,
listed, and deleted. Also, /dev/dazukofs.[0-9] devices are created to support multiple groups.
Quoted 8 lines
Quoted 2 lines
Great another global namespace for process groups. What is your plan for making this work in a container?
It is good that you mention it. This is something that I totally missed. I am not yet certain what kind of a role DazukoFS should have within containers. I need to learn the container API's and see what kinds of options are available. The next patchset (which it seems won't be coming too quickly now) will address this in some form.
Per filesystem would be fine.
Eric
Quoted 10 lines · On Tue, 3 Feb 2009 20:18:29 +0100 John Ogness <[email protected]> wrote:
fs/dazukofs/event.c +10
-0+/** +* dazukofs_add_group - add a new group +* @name: the name of the group to add +* +* Description: This function is called by the device layer to add a new +* group. It returns success if the group has been successfully created +* or if the group already exists. +* +* Returns 0 on success. +*/
fs/dazukofs/event.c line 400
Quoted 10 lines
fs/dazukofs/event.c +10
-0+int dazukofs_add_group (const char *name) +{ +int ret =0 ; +int already_exists; +int available_id =0 ; +struct dazukofs_group *grp; + +write_lock (&group_count_rwlock); + +mutex_lock (&work_mutex);
Hi,
you cannot hold spinlock and try to acquire mutex.
write_lock() -> preempt_disable() vs mutex_lock -> schedule()
-FH
Documentation/filesystems/dazukofs.txt line 6
Quoted 10 lines · John Ogness
Index:
linux-2.6.28/Documentation/filesystems/dazukofs.txt
===================================================================Documentation/filesystems/dazukofs.txt +5
-0@@ -2,9 +2,10 @@ + +By opening the device /dev/dazukofs.0 an application has registered itself. +A read on the device will block until a file access event on DazukoFS has +taken place. When a file access event has occured, the read will return with +information about the file access event. For example:
Given that there are more device files to come, I would put this into a separate directory, /dev/dazukofs/0, much like dm-crypt does for its devices.
Quoted 8 lines · Jan Engelhardt
Documentation/filesystems/dazukofs.txt line 123
Quoted 4 lines
Documentation/filesystems/dazukofs.txt +4
-0+By opening the device /dev/dazukofs.0 an application has registered itself. +A read on the device will block until a file access event on DazukoFS has +taken place. When a file access event has occured, the read will return with +information about the file access event. For example:Given that there are more device files to come, I would put this into a separate directory,
/dev/dazukofs/0, much like dm-crypt does for its devices.
I was not sure if I should leave it up to udev rules to organize things. I agree that putting everything under a directory is cleaner. I will make this change for the next patch series.
John Ogness
Quoted 3 lines · John Ogness
Patch 2: Creates
/dev/dazukofs.0for userspace applications to perform
file access control. At this point, all applications are considered to be working together (in the same group).
Please use struct pid properly.
There is no guarantee that whoever opens /dev/dazukofs.0 will be in the global pid namespace.
So you need to properly store a struct pid reference in your
data structures, and call pid_vnr just before you pass the value to userspace.
As it is your code is broken.
Eric
Quoted 8 lines · On 2009-02-12, [email protected] (Eric W. Biederman) wrote:
Please use struct pid properly.
There is no guarantee that whoever opens
/dev/dazukofs.0will be in the global pid namespace.So you need to properly store a struct pid reference in your data structures, and call
pid_vnrjust before you pass the value to userspace.
Thanks. I was not aware of pid.h. I will make this change.
John Ogness
Quoted 20 lines · John Ogness
Patch 1: Introduces DazukoFS as a nullfs. This is the raw stackable
filesystem part of DazukoFS and does nothing except stack and pass filesystem calls to the lower filesystem.Patched against 2.6.29-rc3.
Index:
linux-2.6.28/Documentation/filesystems/dazukofs.txt
===================================================================Documentation/filesystems/dazukofs.txt new file +10
-0+ +===================== + MOUNTING/UNMOUNTING +===================== + +DazukoFS is typically mounted on top of an existing directory. For example, +to stack DazukoFS on top of the /opt directory, the following mount(8) +command can be given: + +# mount -t dazukofs /opt /opt
[...]
Documentation/filesystems/dazukofs.txt line 54
Quoted 7 lines
Documentation/filesystems/dazukofs.txt new file +7
-0+# mount -t dazukofs /opt /mnt + +When accessing files within /mnt, you will be accessing files in /opt +(through DazukoFS). When accessing files directly in /opt, DazukoFS will not +be involved. + +THIS HAS POTENTIAL PROBLEMS!
This ought to be done better in light of the problem that one could issue mount -t dazukofs /mnt /opt
In kernel code, I'm thinking something like
static int foofs_dothis(...)
{
buf = call d_path on sb->s_root
call namei_lookup on buf
use lookup's resulting dentry as source directory
}
This assumes at least that namei_lookup will not return the new SB's dentry for the given path.
Iff that works out somehow, the 'device' part in the mount command line is practically ignored like it is for like procfs.
Al Viro
maintainerQuoted 7 lines · Jan Engelhardt
This ought to be done better in light of the problem that one could issue
mount -t dazukofs /mnt /opt
In kernel code, I'm thinking something likestatic int
foofs_dothis(...){ buf = call d_path on sb->s_root
Call what on what? There is no such thing as the mountpoint of superblock.
Quoted 6 lines · Al Viro
Quoted 4 lines · Jan Engelhardt
This ought to be done better in light of the problem that one could issue
mount -t dazukofs /mnt /opt
In kernel code, I'm thinking something like
Quoted 6 lines
Quoted 3 lines
static int
foofs_dothis(...){ buf = call d_path on sb->s_rootCall what on what? There is no such thing as the mountpoint of superblock.
In fs/namespace.c:do_new_mount there is 'path'. If 'path' was passed down to the filesystem, path->dentry, I would assume, could be used by the filesystem as a pointer to the original directory which dazuko is going to shadow.
Al Viro
maintainerQuoted 12 lines · Jan Engelhardt
Quoted 10 lines · Al Viro
Quoted 7 lines · Jan Engelhardt
This ought to be done better in light of the problem that one could issue
mount -t dazukofs /mnt /opt
In kernel code, I'm thinking something likestatic int
foofs_dothis(...){ buf = call d_path on sb->s_root
Quoted 6 lines
Quoted 2 lines
Call what on what? There is no such thing as the mountpoint of superblock.
In fs/namespace.c:do_new_mount there is 'path'. If 'path' was passed down to the filesystem,
path->dentry, I would assume, could be used by the filesystem as a pointer to the original directory which dazuko is going to shadow.
It will not be passed dow to the filesystem, simply because said filesystem can be silently moved elesewhere and whatever it used to be mounted on - unmounted.
What's more, whatever it shadows might itself be mounted in any number of places, so...
Quoted 25 lines · Jan Engelhardt
Quoted 5 lines
Documentation/filesystems/dazukofs.txt new file +5
-0+DazukoFS is typically mounted on top of an existing directory. For example, +to stack DazukoFS on top of the /opt directory, the following mount(8) +command can be given: + +# mount -t dazukofs /opt /opt[...]
Documentation/filesystems/dazukofs.txt line 54
Quoted 7 lines
Documentation/filesystems/dazukofs.txt new file +7
-0+# mount -t dazukofs /opt /mnt + +When accessing files within /mnt, you will be accessing files in /opt +(through DazukoFS). When accessing files directly in /opt, DazukoFS will not +be involved. + +THIS HAS POTENTIAL PROBLEMS!This ought to be done better in light of the problem that one could issue
mount -t dazukofs /mnt /opt
In kernel code, I'm thinking something likestatic int
foofs_dothis(...)
{
buf = calld_pathonsb->s_root
callnamei_lookupon buf
use lookup's resulting dentry as source directory
}
Quoted 4 lines
This assumes at least that
namei_lookupwill not return the new SB's dentry for the given path.
Iff that works out somehow, the 'device' part in the mount command line is practically ignored like it is for like procfs.
If we want to forbid users from being able to do this, I think it would be simpler just to fail the mount if it is _not_ specified for direct stacking. This forces users to use a syntax that makes it very clear what they are doing.
Since mounting to a different directory could cause problems, I think it will be good to forbid it. I will make this change for the next patch series.
John Ogness
Al Viro
maintainerQuoted 8 lines · John Ogness
If we want to forbid users from being able to do this, I think it would be simpler just to fail the mount if it is _not_ specified for direct stacking. This forces users to use a syntax that makes it very clear what they are doing.
Since mounting to a different directory could cause problems, I think it will be good to forbid it. I will make this change for the next patch series.
And just how are you going to do that? Note that underlying directory may be visible in many places to start with...
Quoted 9 lines · John Ogness
If we want to forbid users from being able to do this, I think it would be simpler just to fail the mount if it is _not_ specified for direct stacking. This forces users to use a syntax that makes it very clear what they are doing.
Since mounting to a different directory could cause problems, I think it will be good to forbid it. I will make this change for the next patch series.
You could write an additional mount helper (and putting that into /sbin/mount.dazukofs) that does all the security checks:
- that the device is the same as mountpoint
- that the device belonging to the underlying '/mnt' is not mounted anywhere else (in this namespace, at least)
- exit(1) otherwise
Sure, it may not protect against all the cases Al can come up with, but it is better than having nothing.
Al Viro
maintainerQuoted 10 lines · Jan Engelhardt
You could write an additional mount helper (and putting that into
/sbin/mount.dazukofs) that does all the security checks:- that the device is the same as mountpoint
- that the device belonging to the underlying '/mnt' is not mounted anywhere else (in this namespace, at least)
- exit(1) otherwiseSure, it may not protect against all the cases Al can come up with, but it is better than having nothing.
It's still racy, at the very least. Folks, seriously, you can not rely on the underlying tree being inaccessible elsewhere. Anything that does stacking has to cope with that possibility; it's not bypassable by userland helpers.
Quoted 8 lines · Al Viro
Quoted 8 lines
You could write an additional mount helper (and putting that into
/sbin/mount.dazukofs) that does all the security checks:- that the device is the same as mountpoint
- that the device belonging to the underlying '/mnt' is not mounted anywhere else (in this namespace, at least)
- exit(1) otherwise
Quoted 7 lines
Quoted 2 lines
Sure, it may not protect against all the cases Al can come up with, but it is better than having nothing.
It's still racy, at the very least. Folks, seriously, you can not rely on the underlying tree being inaccessible elsewhere. Anything that does stacking has to cope with that possibility; it's not bypassable by userland helpers.
Indeed. As long as the stackable filesystem is synchronizing with the lower layer before doing read actions and synchronizes after all actions, there should be no problem.
Currently I see a problem when a new directory is created directly on the lower mount (rather than through the stackable filesystem). A refcount for "." as seen from the stackable filesystem is then incorrect. I will investigate this.
And I will look to see why there is any danger at all. There should not be.
John Ogness