Skip to contentSkip to conversation

fanotify: blocking and access granting

RFC In review Eric Paris 1 participant 1 message Sep 26, 2008

Subsystems (4)
lore
Share & tools
lore

Eric Paris

author
#0 lore raw

RFC 7/11 fanotify: blocking and access granting

fanotify: blocking and access granting

From: Eric Paris <[email protected]>

This patch introduces blocking an access granting for fanotify. Events are sent to userspace and the original process is blocked (assuming not O_NONBLOCK) for at most the amount of time determined by the group. Total maximum blocking time is the sum of all groups possible blocking time.

O_NONBLOCK processes trying to read and a group registered to require blocking/access control on read MUST have a fastpath entry in order to ever make progress.

---

 fs/aio.c                      |    7 ++
 fs/notify/Makefile            |    2 -
 fs/notify/access.c            |  137 +++++++++++++++++++++++++++++++++++++++
 fs/notify/access_user.c       |  144 +++++++++++++++++++++++++++++++++++++++++
 fs/notify/fanotify.c          |   47 +++++++++++--
 fs/notify/fanotify.h          |   27 +++++++-
 fs/notify/fastpath.c          |    2 -
 fs/notify/group.c             |    9 +++
 fs/notify/notification.c      |    1 
 fs/notify/notification_user.c |   75 +++++++++++++++++++--
 fs/open.c                     |    5 +
 fs/read_write.c               |    6 ++
 include/linux/fanotify.h      |   23 ++++++-
 13 files changed, 462 insertions(+), 23 deletions(-)
 create mode 100644 fs/notify/access.c
 create mode 100644 fs/notify/access_user.c
fs/aio.c +7 -0
index f658441..febe4d0 100644
@@ -31,6 +31,7 @@
 #include <linux/workqueue.h>
 #include <linux/security.h>
 #include <linux/eventfd.h>
+#include <linux/fanotify.h>
 
 #include <asm/kmap_types.h>
 #include <asm/uaccess.h>
@@ -1454,6 +1455,9 @@ static ssize_t aio_setup_iocb(struct kiocb *kiocb)
 		ret = security_file_permission(file, MAY_READ);
 		if (unlikely(ret))
 			break;
+		ret = fanotify(file, FAN_READ_ACCESS);
+		if (unlikely(ret))
+			break;
 		ret = aio_setup_single_vector(kiocb);
 		if (ret)
 			break;
@@ -1486,6 +1490,9 @@ static ssize_t aio_setup_iocb(struct kiocb *kiocb)
 		ret = security_file_permission(file, MAY_READ);
 		if (unlikely(ret))
 			break;
+		ret = fanotify(file, FAN_READ_ACCESS);
+		if (unlikely(ret))
+			break;
 		ret = aio_setup_vectored_rw(READ, kiocb);
 		if (ret)
 			break;
fs/notify/Makefile +1 -1
index 8fac822..e29435a 100644
@@ -3,4 +3,4 @@ obj-$(CONFIG_INOTIFY_USER)	+= inotify_user.o
 
 obj-$(CONFIG_DNOTIFY)		+= dnotify.o
 
-obj-$(CONFIG_FANOTIFY)		+= fanotify.o notification.o notification_user.o group.o group_user.o info_user.o fastpath.o fastpath_user.o
+obj-$(CONFIG_FANOTIFY)		+= fanotify.o notification.o notification_user.o group.o group_user.o info_user.o fastpath.o fastpath_user.o access.o access_user.o
fs/notify/access.c new file +137 -0

137 lines not on this page · raw message

fs/notify/access_user.c new file +144 -0

144 lines not on this page · raw message

fs/notify/fanotify.c +40 -7

79 lines not on this page · raw message

fs/notify/fanotify.h +23 -4
index 12fb8d3..38e156e 100644
@@ -21,6 +21,12 @@ struct fanotify_group {
 	struct list_head notification_list; /* list of event_holder this group needs to send to userspace */
 	wait_queue_head_t notification_waitq; /* read() on the notification file blocks on this waitq */
 
+	/* needed to track outstanding requests we expect to hear from userspace */
+	atomic_long_t cookie;		/* next cookie send to userspace for a decision */
+	struct mutex access_mutex;	/* protect access_list list */
+	struct list_head access_list;	/* event_holder we need an answer from userspace */
+	wait_queue_head_t access_waitq;	/* we wait here for userspace access decisions */
+
 	/* stores all fastapth entries assoc with this group so they can be cleaned on unregister */
 	struct mutex fastpath_mutex;	/* protect fastpath_entries list */
 	struct list_head fastpath_entries; /* all fastpath entries for this group */
@@ -31,15 +37,18 @@ struct fanotify_group {
 	struct dentry *notification;	/* pointer to fanotify/name/notification dentry */
 	struct dentry *info;		/* pointer to fanotify/name/info dentry */
 	struct dentry *fastpath;	/* pointer to fanotify/name/fastpath dentry */
+	struct dentry *access;		/* pointer to fanotify/name/access dentry */
 };
 
 /*
- * A single event can be queued in multiple group->notification_lists.
+ * A single event can be queued in multiple group->notification_lists and to at
+ * most one group->access_list at the same time.
  *
- * each group->notification_list will point to an event_holer which in turns points
- * to the actual event that needs to be sent to userspace.
+ * each group->notification_list or group->access_list will point to an
+ * event_holer which in turns points to the actual event that needs to be sent
+ * to userspace.
  *
- * Seemed cheaper to create a refcnt'd event and a small holder for every group
+ * Seemed cheaper to create a refcnt'd event and a small holder for every list
  * than create a different event for every group
  * 
  */
@@ -63,6 +72,9 @@ struct fanotify_event {
 	struct path path;	/* path from the original access */
 	unsigned int mask;	/* the type of access */
 	atomic_t refcnt;	/* how many groups still are using/need to send this event */
+	/* if waiting for a userspace access answer this is the cookie they will send back */
+	unsigned long cookie;
+	unsigned int response;	/* userspace answer to question */
 };
 
 
@@ -128,6 +140,13 @@ extern int fanotify_fastpath_add(struct fanotify_group *group, int fd, unsigned
 extern __init int fastpath_init(void);
 extern __init int fastpath_uninit(void);
 
+extern int fanotify_access_user_create(struct fanotify_group *group);
+extern int fanotify_access_user_destroy(struct fanotify_group *group);
+
+extern int add_event_to_group_access(struct fanotify_group *group, struct fanotify_event *event);
+extern int get_response_from_group_access(struct fanotify_group *group, struct fanotify_event *event);
+extern int fanotify_process_access_response(struct fanotify_group *group, unsigned long cookie, unsigned int response);
+
 extern void fanotify_get_group(struct fanotify_group *group);
 extern void fanotify_put_group(struct fanotify_group *group);
 extern int fanotify_register_group(char *name, unsigned int priority, unsigned int mask);
fs/notify/fastpath.c +1 -1
index c134426..7f783c0 100644
@@ -74,7 +74,7 @@ int is_fastpath(struct file *file, unsigned int mask, struct fanotify_group *gro
 	int found = 0;
 
 	/* if noone is listening, might as well fastpath this group huh? */
-	if (!atomic_read(&group->num_clients))
+	if (unlikely(!atomic_read(&group->num_clients)))
 		return 1;
 
 	read_lock(&inode->fastpath_rwlock);
fs/notify/group.c +9 -0
index 81563ad..90cf59e 100644
@@ -49,6 +49,8 @@ void fanotify_kill_group(struct fanotify_group *group)
 
 	fanotify_fastpath_user_destroy(group);
 
+	fanotify_access_user_destroy(group);
+
 	securityfs_remove(group->subdir);
 	group->subdir = NULL;
 
@@ -123,6 +125,11 @@ int fanotify_register_group(char *name, unsigned int priority, unsigned int mask
 	if (rc)
 		goto out_clean_info;
 
+	rc = fanotify_access_user_create(group);
+	if (rc)
+		goto out_clean_fastpath;
+
+
 	/* Do we need to be the first entry? */
 	if (list_empty(&groups)) {
 		list_add_rcu(&group->group_list, &groups);
@@ -148,6 +155,8 @@ int fanotify_register_group(char *name, unsigned int priority, unsigned int mask
 
 	return 0;
 
+out_clean_fastpath:
+	fanotify_fastpath_user_destroy(group);
 out_clean_info:
 	fanotify_info_user_destroy(group);
 out_clean_notification:
fs/notify/notification.c +1 -0
index 8f4f439..e1a6a24 100644
@@ -140,6 +140,7 @@ struct fanotify_event *create_event(struct file *file, unsigned int mask)
 	event->path.mnt = file->f_path.mnt;
 	path_get(&event->path);
 
+	event->cookie = 0;
 	event->mask = mask;
 
 	WARN_ON(!event->path.dentry);
fs/notify/notification_user.c +68 -7

128 lines not on this page · raw message

fs/open.c +5 -0
index 1057c34..06a6f79 100644
@@ -29,6 +29,7 @@
 #include <linux/rcupdate.h>
 #include <linux/audit.h>
 #include <linux/falloc.h>
+#include <linux/fanotify.h>
 
 int vfs_statfs(struct dentry *dentry, struct kstatfs *buf)
 {
@@ -820,6 +821,10 @@ static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
 	if (error)
 		goto cleanup_all;
 
+	error = fanotify(f, FAN_OPEN_ACCESS);
+	if (error)
+		goto cleanup_all;
+
 	if (!open && f->f_op)
 		open = f->f_op->open;
 	if (open) {
fs/read_write.c +6 -0
index 39f1fce..3cc8c41 100644
@@ -16,6 +16,7 @@
 #include <linux/syscalls.h>
 #include <linux/pagemap.h>
 #include <linux/splice.h>
+#include <linux/fanotify.h>
 #include "read_write.h"
 
 #include <asm/uaccess.h>
@@ -201,6 +202,11 @@ int rw_verify_area(int read_write, struct file *file, loff_t *ppos, size_t count
 				read_write == READ ? MAY_READ : MAY_WRITE);
 	if (retval)
 		return retval;
+	if (read_write == READ) {
+		retval = fanotify(file, FAN_READ_ACCESS);
+		if (retval)
+			return retval;
+	}
 	return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
 }
 
include/linux/fanotify.h +20 -3
index a23ab97..5d3e5d0 100644
@@ -16,6 +16,10 @@
 #define FAN_CLOSE_NOWRITE	0x00000008	/* Unwrittable file closed */
 #define FAN_OPEN		0x00000010	/* File was opened */
 
+/* userspace may also request blocking for permission checks for open and read */
+#define FAN_OPEN_ACCESS		0x00000100
+#define FAN_READ_ACCESS		0x00000200
+
 /* FIXME currently Q's have no limit.... */
 #define FAN_Q_OVERFLOW		0x00004000	/* Event queued overflowed */
 
@@ -33,6 +37,17 @@
 			 FAN_CLOSE_NOWRITE |\
 			 FAN_OPEN)
 
+/*
+ * Like the above list of events only these are the event types in which the
+ * kernel will wait for answers.
+ */
+#define FAN_ALL_EVENTS_ACCESS   (FAN_OPEN_ACCESS |\
+				 FAN_READ_ACCESS)
+
+/* answers will need to be either allow or deny */
+#define FAN_ALLOW		0x00000001
+#define FAN_DENY		0x00000010
+
 #ifdef __KERNEL__
 
 #include <linux/fs.h>
@@ -40,13 +55,15 @@
 
 #ifdef CONFIG_FANOTIFY
 
-extern void fanotify(struct file *file, unsigned int mask);
+extern int fanotify(struct file *file, unsigned int mask);
 extern void fanotify_inode_delete(struct inode *inode);
 
 #else
 
-static inline void fanotify(struct file *file, unsigned int mask)
-{}
+static inline int fanotify(struct file *file, unsigned int mask)
+{
+	return 0;
+}
 
 static inline void fanotify_inode_delete(struct inode *inode)
 {}