Skip to contentSkip to conversation

fanotify: novel all file access notification and permissions system

PATCH New Eric Paris 1 participant 17 messages Oct 14, 2008

Subsystems (11)
lore
Share & tools
lore

Eric Paris

author
#0 lore raw

cover fanotify: novel all file access notification and permissions system

The following is a file notification and access system intended to allow a variety of userspace programs to get information about filesystem events no matter where or how they happen on a system and use that in conjunction with the actual on disk data related to that event to provide additional services such as file change indexing or content based antivirus scanning. Minor changes are almost certainly possible to make this notification and access interface usable for HSMs. fscking all notify is generally refered to as fanotify, or for the weak of heart you can call it file access notify system. The ideas behind this code are based on talpa the GPL antivirus interface originally pioneered by Sophos and on the feedback from lkml and malware-list. This is however a complete rewrite from scratch, so if you remember talpa 'this ain't it.'

the most up2date (but not always working) patch set can always be found at http://people.redhat.com/~eparis/fanotify

comments, attacks, criticism, bad names, and really just about anything can be sent to me but please lets not rehash useless conversations! I will send the full patch set to both lists, but I'm not going to cc everyone individually.

**fanotify-executive-summary**

fanotify has 9 event types and only sends events for S_ISREG() files. The event types are OPEN_NOEXEC, OPEN_EXEC, READ, WRITE, CLOSE_WRITE, CLOSE_NOWRITE, OPEN_PERM, READ_EXEC_PERM, and READ_NOEXEC_PERM. Events OPEN_PERM, READ_EXEC_PERM and READ_NOEXEC_PERM require that the listener return some sort of allow/deny/more_time response as the original process blocks until it gets an event (or times out.) listeners may register a group which will get notifications about any combination of these events, it will be up to the listener to determine what events they are interested in hearing a mediating access decisions for.

groups are a construct in which userspace indicates what priority (only really used for PERM type events) and what type of events its listeners want to hear. A single group may have unlimited listeners but each event will only go to ONE listener. Two groups may register for the same type of events and one listener in EACH group will get a copy of the event.

The user interface for fanotify is all through socket calls. Userspace pulls events from the kernel using getsockopt and sends responses to the kernel using setsockopt. These are the only socket operations defined for PF_FANOTIFY sockets.

-----------------
fanotify, long winded:

User interface work can be found in net/fanotify. The main implementation is in fs/notify. My layout is like so

fanotify.c --- all functions called from the main kernel
group.c --- groups are my implementation of my multiple listeners
you can register different groups to get different event types.
notification.c --- implementation surrounding the sending of events to a
userspace listener.
fastpath.c --- implementation surrounding the addition of inode
fastpath entries for performance.
access.c --- implementation surrounding the processing of
responses from userspace when the events require a response.

fanotify is a new fscking all notify subsystem. Much as inotify provides notification of filesystem activity for some registered subset of inodes fanotify provides notification of filesystem activity for ALL of the system's S_ISREG() files. fanotify has a smaller number of notifications than inotify.

GROUPS AND EVENTS:
A "group" registers with fanotify and in doing so indicates what that group should be called and what type of events it wishes to receive and in what order it should receive events relative to other groups. An "event" is simply a notification about some filesystem action. The list of all fanotify events are read, write, open_for_exec, open, close_was_not_writable, close_was_writable, open_need_access_decision, read_for exec_need_access_decision, read_need access_decisions. Any number of groups may be created for any subset of event types. One group may register to get reads and writes while another maybe register for opens and read_need_access_desision.

Any number of userspace listeners may be active in a single group. Each group will get ONE copy of any filesystem event. If there are 10 listeners in a single group and one fanotify event is generated only ONE of those listeners will get the event. If more than one group registers for the same type of event one listener in EACH GROUP will get a copy of that event.

It all starts when a listener registers a group. Registering a group is as done by binding group information to a fanotify socket. Simple operation is something like.

addr.name = 123
addr.priority = 456
addr.mask = 0x002

sock = socket(PF_FANOTIFY);
fd = bind(sock, addr);

123 is just the name of the group, used so it is unlikely two listeners will accidentally use the same priority and mask and stumble into each others events. 456 is the priority (only interesting for blocking/access events, will describe later) and 0x002 is FAN_WRITE. If one wanted read and write you would use 0x03 = (FAN_ACCESS | FAN_WRITE). If this is the first time a process has bound to this address a struct fanotify_group is allocated and initialized (see fanotify_find_group()). The group is added to a kernel global list called groups.

The listener should now call getsockopt(fd, FANOTIFY_GET_EVENT,...). Since at this point there are no events this will block waiting for an event.

Now lets say the original process calls open(). Open is going to happen exactly as before until it gets to the fsnotify code (this is where both inotify and dnotify hook into the kernel.) From fsnotify we will call into the function fanotify() with the mask FAN_OPEN. We will then walk to global groups list (which is ordered by priority, low first) looking for any groups which want to receive notification about FAN_OPEN and let's say we will find the group '123' that was registered above. An fanotify_event is allocated and any data we want the listener process to get about the original process is added to the fanotify_event. The event contains a struct path with the dentry and vfsmount from the open done by the original process.

Now we call add_event_to_group_notification() to add the event to the group->notification_list. This function has a little bit of magic. Since an event may be needed in multiple groups notification_list I created a helper structure, a struct fanotify_event_holder. Each entry in the group->event_list points to a unique event_holder which in turn points to the ref counted event in question.

(assuming 2 groups)
group1->notification_list ==> fanotify_event_holder1 ==> single_fanotify_event group2->notification_list ==> fanotify_event_holder2 ==> single_fanotify_event

The magic is that since we will always need at least 1 holder I embedded one fanotify_event_holder inside an fanotity_event. This means that when removing an event from the group->notification_list we may need to free the fanotify_event_holder (if it was allocated seperately) or we may need to just clear it (if it was the embedded holder.)

After the event is added to the group->notification_list we wake up the listener processes. The original process never blocked and at this point and is returning to userspace with the completed open.

Simultaneously the listener process will now remove the event from the group->notification_list, see remove_event_from_group_notification(). Create a new file, fd, and install such in the listener process, see fanotify_notification_read(). We will put_event (since this group is finished with it) and will return the getsockopt() call to userspace.

The listener process will get a structure filled back from teh getsockopt call which will include an fd and some metadata about that fd. This includes things like the original files f_flags, the original processes pid and things like that.

The listener process must call close() when it is finished with this new fd. But lets assume the listener, for whatever reason, decides it doesn't want to hear any more of this type of message for this inode. That means the listener process needs to "create a fastpath" entry. To do this the listener process will need to call setsockopt(fd, FANOTIY_SET_FASTPATH, ...) the struct with the setsockopt will include things like what kind of events we don't want to hear about and what file this is associated with. All fastpaths will be cleared on the next fsnotify write event (happens the same place in the code as mtime update)

Inside the kernel (fanotify_fastpath_add()) what happens is that we will create a new fanotify_fastpath_entry for the group and mask in question and attach it to the inode. The next time a process opens the inode in question we will search the global groups list for a group that matches the mask and we will look at the inode to see if there is a fastpath entry for this group and mask. If there is an entry no event will be added to the group->event_list.

The need for fastpaths (or calling what it really is, an in kernel cache) has been questioned. I decided to include a little unscientific data here. On a 32 way machine a make -j 32 took about 9 minutes 12 seconds. With that same machine running having one group and 32 listeners receiving every fanotify event that the kernel could send to userspace while the listeners were responding to accesses as fast as they could (just a very tight get event, allow loop) it took 95 minutes 12 seconds. Same process with in kernel fastpaths/cache results took 19 minutes 5 seconds. More reasonable event requirements and a single listener took 10 minutes 35 sec. So about a 15% perf hit to do any kind of permission checking to userspace. Anyway, the need for fastpaths is quite clear.

Assuming the event was for FAN_OPEN_PERM much of the above is the same. Biggest difference is that the place from the kernel we will call into the fanotify code is different (fsnotify is not in a good place to provide security hooks). If a group is found that wants this event the event is added to the group->access_list AND to the group->event_list. The original process is then blocked for a (now fixed 5 second) timeout waiting for the event to get a non-zero event->response on the group->access_waitq.

The listener process will get a notification exactly as above from the notification file but this time will need to write an answer to the access file. The answer is again a simple string indicating the cookie and the response (allow/deny). If a response is received from userspace the event is removed from the group->access_list and the original process is woken up to continue, either by looking for the next group or by returning -EPERM. Userspace may also return FAN_RESETTIMER which will reset the 5 second timeout. A badly behaving userspace may hang an open indeffinetly.

If the original process times out waiting for the listener process to give a response we currently just allow the security access.

An interesting part of the code is fastpath cleanup handling. Any time fanotify gets a FAN_MODIFY event we clear the fastpath entries for the associated inode. This means our notification and access decisions are NOT race free but the races are small. This is not perfect security. A 'problomatic' sequence of events would be like such

process1 calls read on file
listener scans file and finds it safe
process2 writes to file
listener creates a fastpath entry for file

Maybe I should add an explicit clear fastpath request so userspace can close that race when it gets the write notification on file (if it so chooses.) Remember I'm not trying to protect against a rogue process intelligently and actively attacking the system. I'm trying to stop a correctly functioning yum from reading a bad rpm that it downloaded. I'm trying to stop an NFS server from accepting bad files and then apache serving them out on the net.

There is the also a race between when a process calls mmap() (at that point we get an event and scan) and when the process actually faults in a page which may have been changed by a write from another process. Its not perfect, but it's damn sure better than what we have now.

Object lifetime:

fanotify_group - exists from registration to unregistration. Unregistration racing with some the associated special file notification open was a bitch to figure out but eventually I based it on the groups existence in the global groups list.

fanotify_event - created inside the main fanotify loop which runs the events list. An event lasts until both the main loop ends AND the event is no longer needed by all groups for which it was queued. A refcnt on the event is taken every time it is added to a group notification/access list and is dropped when the group has removed the event from the list and is finished with its contents.

fanotify_event_holder - allocated when an event is added to a group notification/access list. destroyed when an event is removed from a notification/access list. There is the special case of the embedded holder inside the fanotify_event. The embedded holder is assumed to be available for use if holder->event_list is empty.

fanotify_fastpath_entry - created when a process writes to the fastpath special file and added to the inode list. This entry is destroy in 3 possible places. If an inode has a modify event we flush them all. If an inode is eviced from core we flush them all. If a group is unregistered we flush them all for that group.

---

Eric Paris (16):
fanotify: send file f_flags along with notifications
fanotify: send tgid with notification messages
fanotify: send pid with fanotify notification events
fanotify: ability for userspace to delay responses
fanotify: user interface for access decisions
fanotify: give a special access permission check
fanotify: blocking and access granting
fanotify: add group priorities
fanotify: add a userspace interface for fastpaths
fanotify: fastpath to ignore certain in core inodes
fanotify: add a userspace interface for fanotify notifications
fanotify: make use of the new fsnotify_open_exec calls
fsnotify: sys_execve and sys_uselib do not call into fsnotify
fanotify: fscking all notify, system wide file access notification
fsnotify: pass a file instead of an inode to open, read, and write filesystem notification: create fs/notify to contain all fs notification

 fs/Kconfig                 |   39 --
 fs/Makefile                |    5 
 fs/aio.c                   |    7 
 fs/compat.c                |    5 
 fs/dnotify.c               |  194 -----------
 fs/exec.c                  |   15 +
 fs/inode.c                 |    6 
 fs/inotify.c               |  773 --------------------------------------------
 fs/inotify_user.c          |  781 --------------------------------------------
 fs/nfsd/vfs.c              |    4 
 fs/notify/Kconfig          |   52 +++
 fs/notify/Makefile         |    6 
 fs/notify/access.c         |  187 +++++++++++
 fs/notify/dnotify.c        |  194 +++++++++++
 fs/notify/fanotify.c       |  152 +++++++++
 fs/notify/fanotify.h       |  111 ++++++
 fs/notify/fastpath.c       |  230 +++++++++++++
 fs/notify/group.c          |  142 ++++++++
 fs/notify/inotify.c        |  773 ++++++++++++++++++++++++++++++++++++++++++++
 fs/notify/inotify_user.c   |  781 ++++++++++++++++++++++++++++++++++++++++++++
 fs/notify/notification.c   |  277 ++++++++++++++++
 fs/open.c                  |    7 
 fs/read_write.c            |   14 +
 include/linux/Kbuild       |    1 
 include/linux/fanotify.h   |  186 ++++++++++
 include/linux/fs.h         |    5 
 include/linux/fsnotify.h   |   39 ++
 include/linux/sched.h      |    1 
 include/linux/socket.h     |    5 
 mm/mmap.c                  |    7 
 mm/mprotect.c              |    6 
 mm/nommu.c                 |    7 
 net/Makefile               |    1 
 net/core/sock.c            |    6 
 net/fanotify/Makefile      |    5 
 net/fanotify/af_fanotify.c |  270 +++++++++++++++
 net/fanotify/af_fanotify.h |   20 +
 37 files changed, 3503 insertions(+), 1811 deletions(-)
 delete mode 100644 fs/dnotify.c
 delete mode 100644 fs/inotify.c
 delete mode 100644 fs/inotify_user.c
 create mode 100644 fs/notify/Kconfig
 create mode 100644 fs/notify/Makefile
 create mode 100644 fs/notify/access.c
 create mode 100644 fs/notify/dnotify.c
 create mode 100644 fs/notify/fanotify.c
 create mode 100644 fs/notify/fanotify.h
 create mode 100644 fs/notify/fastpath.c
 create mode 100644 fs/notify/group.c
 create mode 100644 fs/notify/inotify.c
 create mode 100644 fs/notify/inotify_user.c
 create mode 100644 fs/notify/notification.c
 create mode 100644 include/linux/fanotify.h
 create mode 100644 net/fanotify/Makefile
 create mode 100644 net/fanotify/af_fanotify.c
 create mode 100644 net/fanotify/af_fanotify.h
author
#1 lore raw

PATCH 1/16 filesystem notification: create fs/notify to contain all fs notification

Adding yet another filesystem notification system it seemed like a good idea to clean up fs/ but creating an fs/notify and putting everything there.

---

 fs/Kconfig               |   39 --
 fs/Makefile              |    5 
 fs/dnotify.c             |  194 -----------
 fs/inotify.c             |  773 ----------------------------------------------
 fs/inotify_user.c        |  781 ----------------------------------------------
 fs/notify/Kconfig        |   38 ++
 fs/notify/Makefile       |    4 
 fs/notify/dnotify.c      |  194 +++++++++++
 fs/notify/inotify.c      |  773 ++++++++++++++++++++++++++++++++++++++++++++++
 fs/notify/inotify_user.c |  781 ++++++++++++++++++++++++++++++++++++++++++++++
 10 files changed, 1792 insertions(+), 1790 deletions(-)
 delete mode 100644 fs/dnotify.c
 delete mode 100644 fs/inotify.c
 delete mode 100644 fs/inotify_user.c
 create mode 100644 fs/notify/Kconfig
 create mode 100644 fs/notify/Makefile
 create mode 100644 fs/notify/dnotify.c
 create mode 100644 fs/notify/inotify.c
 create mode 100644 fs/notify/inotify_user.c
fs/Kconfig +1 -38
index f54a157..e5e0959 100644
@@ -513,44 +513,7 @@ config OCFS2_DEBUG_FS
 
 endif # BLOCK
 
-config DNOTIFY
-	bool "Dnotify support"
-	default y
-	help
-	  Dnotify is a directory-based per-fd file change notification system
-	  that uses signals to communicate events to user-space.  There exist
-	  superior alternatives, but some applications may still rely on
-	  dnotify.
-
-	  If unsure, say Y.
-
-config INOTIFY
-	bool "Inotify file change notification support"
-	default y
-	---help---
-	  Say Y here to enable inotify support.  Inotify is a file change
-	  notification system and a replacement for dnotify.  Inotify fixes
-	  numerous shortcomings in dnotify and introduces several new features
-	  including multiple file events, one-shot support, and unmount
-	  notification.
-
-	  For more information, see <file:Documentation/filesystems/inotify.txt>
-
-	  If unsure, say Y.
-
-config INOTIFY_USER
-	bool "Inotify support for userspace"
-	depends on INOTIFY
-	default y
-	---help---
-	  Say Y here to enable inotify support for userspace, including the
-	  associated system calls.  Inotify allows monitoring of both files and
-	  directories via a single open fd.  Events are read from the file
-	  descriptor, which is also select()- and poll()-able.
-
-	  For more information, see <file:Documentation/filesystems/inotify.txt>
-
-	  If unsure, say Y.
+source "fs/notify/Kconfig"
 
 config QUOTA
 	bool "Quota support"
fs/Makefile +1 -4
index de404b0..b101855 100644
@@ -20,8 +20,7 @@ obj-y +=	no-block.o
 endif
 
 obj-$(CONFIG_BLK_DEV_INTEGRITY) += bio-integrity.o
-obj-$(CONFIG_INOTIFY)		+= inotify.o
-obj-$(CONFIG_INOTIFY_USER)	+= inotify_user.o
+obj-y				+= notify/
 obj-$(CONFIG_EPOLL)		+= eventpoll.o
 obj-$(CONFIG_ANON_INODES)	+= anon_inodes.o
 obj-$(CONFIG_SIGNALFD)		+= signalfd.o
@@ -55,8 +54,6 @@ obj-$(CONFIG_QFMT_V1)		+= quota_v1.o
 obj-$(CONFIG_QFMT_V2)		+= quota_v2.o
 obj-$(CONFIG_QUOTACTL)		+= quota.o
 
-obj-$(CONFIG_DNOTIFY)		+= dnotify.o
-
 obj-$(CONFIG_PROC_FS)		+= proc/
 obj-y				+= partitions/
 obj-$(CONFIG_SYSFS)		+= sysfs/
fs/dnotify.c deleted +0 -194

194 lines not on this page · raw message

fs/inotify.c deleted +0 -773

773 lines not on this page · raw message

fs/inotify_user.c deleted +0 -781

781 lines not on this page · raw message

fs/notify/Kconfig new file +38 -0
new file mode 100644
index 0000000..23415de
@@ -0,0 +1,38 @@
+config DNOTIFY
+	bool "Dnotify support"
+	default y
+	help
+	  Dnotify is a directory-based per-fd file change notification system
+	  that uses signals to communicate events to user-space.  There exist
+	  superior alternatives, but some applications may still rely on
+	  dnotify.
+
+	  If unsure, say Y.
+
+config INOTIFY
+	bool "Inotify file change notification support"
+	default y
+	---help---
+	  Say Y here to enable inotify support.  Inotify is a file change
+	  notification system and a replacement for dnotify.  Inotify fixes
+	  numerous shortcomings in dnotify and introduces several new features
+	  including multiple file events, one-shot support, and unmount
+	  notification.
+
+	  For more information, see <file:Documentation/filesystems/inotify.txt>
+
+	  If unsure, say Y.
+
+config INOTIFY_USER
+	bool "Inotify support for userspace"
+	depends on INOTIFY
+	default y
+	---help---
+	  Say Y here to enable inotify support for userspace, including the
+	  associated system calls.  Inotify allows monitoring of both files and
+	  directories via a single open fd.  Events are read from the file
+	  descriptor, which is also select()- and poll()-able.
+
+	  For more information, see <file:Documentation/filesystems/inotify.txt>
+
+	  If unsure, say Y.
fs/notify/Makefile new file +4 -0
new file mode 100644
index 0000000..882ecf9
@@ -0,0 +1,4 @@
+obj-$(CONFIG_INOTIFY)		+= inotify.o
+obj-$(CONFIG_INOTIFY_USER)	+= inotify_user.o
+
+obj-$(CONFIG_DNOTIFY)		+= dnotify.o
fs/notify/dnotify.c new file +194 -0

194 lines not on this page · raw message

fs/notify/inotify.c new file +773 -0

773 lines not on this page · raw message

fs/notify/inotify_user.c new file +781 -0

181 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Part of the diff is not shown here. View the raw message.

author
#2 lore raw

PATCH 2/16 fsnotify: pass a file instead of an inode to open, read, and write

In reply to Eric Paris

fanotify, the upcoming notification system actually needs a f_path so it can do opens in the context of listeners, and it needs a file so it can get f_flags from the original process. Close already was passing a file.

---

 fs/compat.c              |    5 ++---
 fs/nfsd/vfs.c            |    4 ++--
 fs/open.c                |    2 +-
 fs/read_write.c          |    8 ++++----
 include/linux/fsnotify.h |    9 ++++++---
 5 files changed, 15 insertions(+), 13 deletions(-)
fs/compat.c +2 -3
index 075d050..f59c796 100644
@@ -1125,11 +1125,10 @@ out:
 	if (iov != iovstack)
 		kfree(iov);
 	if ((ret + (type == READ)) > 0) {
-		struct dentry *dentry = file->f_path.dentry;
 		if (type == READ)
-			fsnotify_access(dentry);
+			fsnotify_access(file);
 		else
-			fsnotify_modify(dentry);
+			fsnotify_modify(file);
 	}
 	return ret;
 }
fs/nfsd/vfs.c +2 -2
index 18060be..b576e38 100644
@@ -937,7 +937,7 @@ nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
 		nfsdstats.io_read += host_err;
 		*count = host_err;
 		err = 0;
-		fsnotify_access(file->f_path.dentry);
+		fsnotify_access(file);
 	} else 
 		err = nfserrno(host_err);
 out:
@@ -1004,7 +1004,7 @@ nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
 	set_fs(oldfs);
 	if (host_err >= 0) {
 		nfsdstats.io_write += cnt;
-		fsnotify_modify(file->f_path.dentry);
+		fsnotify_modify(file);
 	}
 
 	/* clear setuid/setgid flag after write */
fs/open.c +1 -1
index 07da935..1057c34 100644
@@ -1020,7 +1020,7 @@ long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
 				put_unused_fd(fd);
 				fd = PTR_ERR(f);
 			} else {
-				fsnotify_open(f->f_path.dentry);
+				fsnotify_open(f);
 				fd_install(fd, f);
 			}
 		}
fs/read_write.c +4 -4
index 9ba495d..39f1fce 100644
@@ -258,7 +258,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
 		else
 			ret = do_sync_read(file, buf, count, pos);
 		if (ret > 0) {
-			fsnotify_access(file->f_path.dentry);
+			fsnotify_access(file);
 			add_rchar(current, ret);
 		}
 		inc_syscr(current);
@@ -313,7 +313,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
 		else
 			ret = do_sync_write(file, buf, count, pos);
 		if (ret > 0) {
-			fsnotify_modify(file->f_path.dentry);
+			fsnotify_modify(file);
 			add_wchar(current, ret);
 		}
 		inc_syscw(current);
@@ -604,9 +604,9 @@ out:
 		kfree(iov);
 	if ((ret + (type == READ)) > 0) {
 		if (type == READ)
-			fsnotify_access(file->f_path.dentry);
+			fsnotify_access(file);
 		else
-			fsnotify_modify(file->f_path.dentry);
+			fsnotify_modify(file);
 	}
 	return ret;
 }
include/linux/fsnotify.h +6 -3
index a895131..dd76948 100644
@@ -136,8 +136,9 @@ static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
 /*
  * fsnotify_access - file was read
  */
-static inline void fsnotify_access(struct dentry *dentry)
+static inline void fsnotify_access(struct file *file)
 {
+	struct dentry *dentry = file->f_path.dentry;
 	struct inode *inode = dentry->d_inode;
 	u32 mask = IN_ACCESS;
 
@@ -152,8 +153,9 @@ static inline void fsnotify_access(struct dentry *dentry)
 /*
  * fsnotify_modify - file was modified
  */
-static inline void fsnotify_modify(struct dentry *dentry)
+static inline void fsnotify_modify(struct file *file)
 {
+	struct dentry *dentry = file->f_path.dentry;
 	struct inode *inode = dentry->d_inode;
 	u32 mask = IN_MODIFY;
 
@@ -168,8 +170,9 @@ static inline void fsnotify_modify(struct dentry *dentry)
 /*
  * fsnotify_open - file was opened
  */
-static inline void fsnotify_open(struct dentry *dentry)
+static inline void fsnotify_open(struct file *file)
 {
+	struct dentry *dentry = file->f_path.dentry;
 	struct inode *inode = dentry->d_inode;
 	u32 mask = IN_OPEN;
 
author
#3 lore raw

PATCH 3/16 fanotify: fscking all notify, system wide file access notification

In reply to Eric Paris

A new system wide file access notification system.

---

 fs/notify/Kconfig        |   14 ++
 fs/notify/Makefile       |    2 
 fs/notify/fanotify.c     |   99 +++++++++++++++++
 fs/notify/fanotify.h     |   64 +++++++++++
 fs/notify/group.c        |  110 +++++++++++++++++++
 fs/notify/notification.c |  266 ++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/Kbuild     |    1 
 include/linux/fanotify.h |   95 ++++++++++++++++
 include/linux/fsnotify.h |   14 ++
 include/linux/sched.h    |    1 
 10 files changed, 662 insertions(+), 4 deletions(-)
 create mode 100644 fs/notify/fanotify.c
 create mode 100644 fs/notify/fanotify.h
 create mode 100644 fs/notify/group.c
 create mode 100644 fs/notify/notification.c
 create mode 100644 include/linux/fanotify.h
fs/notify/Kconfig +14 -0
index 23415de..97cc832 100644
@@ -36,3 +36,17 @@ config INOTIFY_USER
 	  For more information, see <file:Documentation/filesystems/inotify.txt>
 
 	  If unsure, say Y.
+
+config FANOTIFY
+        bool "Filesystem wide access notification"
+        select SECURITY
+        default y
+        ---help---
+           Say Y here to enable fanotify suport.  fanotify is a system wide
+           file access notification interface.  Events are read from from a
+           single open fd and in doing so a fd is created in the reading process
+           which points to the same data as the one on which the event occured.
+
+           For more information, see <file:Documentation/filesystems/fanotify.txt>
+
+           If unsure, say Y.
fs/notify/Makefile +2 -0
index 882ecf9..c467c97 100644
@@ -2,3 +2,5 @@ obj-$(CONFIG_INOTIFY)		+= inotify.o
 obj-$(CONFIG_INOTIFY_USER)	+= inotify_user.o
 
 obj-$(CONFIG_DNOTIFY)		+= dnotify.o
+
+obj-$(CONFIG_FANOTIFY)		+= fanotify.o notification.o group.o
fs/notify/fanotify.c new file +99 -0

99 lines not on this page · raw message

fs/notify/fanotify.h new file +64 -0

64 lines not on this page · raw message

fs/notify/group.c new file +110 -0

110 lines not on this page · raw message

fs/notify/notification.c new file +266 -0

266 lines not on this page · raw message

include/linux/Kbuild +1 -0
index 282a504..b6372b4 100644
@@ -244,6 +244,7 @@ unifdef-y += inet_diag.h
 unifdef-y += in.h
 unifdef-y += in6.h
 unifdef-y += inotify.h
+unifdef-y += fanotify.h
 unifdef-y += input.h
 unifdef-y += ip.h
 unifdef-y += ipc.h
include/linux/fanotify.h new file +95 -0

95 lines not on this page · raw message

include/linux/fsnotify.h +10 -4
index dd76948..50d8d8b 100644
@@ -13,6 +13,7 @@
 
 #include <linux/dnotify.h>
 #include <linux/inotify.h>
+#include <linux/fanotify.h>
 #include <linux/audit.h>
 
 /*
@@ -148,6 +149,7 @@ static inline void fsnotify_access(struct file *file)
 	dnotify_parent(dentry, DN_ACCESS);
 	inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
 	inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
+	fanotify(file, FAN_ACCESS);
 }
 
 /*
@@ -165,6 +167,7 @@ static inline void fsnotify_modify(struct file *file)
 	dnotify_parent(dentry, DN_MODIFY);
 	inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
 	inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
+	fanotify(file, FAN_MODIFY);
 }
 
 /*
@@ -181,6 +184,7 @@ static inline void fsnotify_open(struct file *file)
 
 	inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
 	inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
+	fanotify(file, FAN_OPEN);
 }
 
 /*
@@ -192,13 +196,15 @@ static inline void fsnotify_close(struct file *file)
 	struct inode *inode = dentry->d_inode;
 	const char *name = dentry->d_name.name;
 	mode_t mode = file->f_mode;
-	u32 mask = (mode & FMODE_WRITE) ? IN_CLOSE_WRITE : IN_CLOSE_NOWRITE;
+	u32 in_mask = (mode & FMODE_WRITE) ? IN_CLOSE_WRITE : IN_CLOSE_NOWRITE;
+	u32 fan_mask = (mode & FMODE_WRITE) ? FAN_CLOSE_WRITE : FAN_CLOSE_NOWRITE;
 
 	if (S_ISDIR(inode->i_mode))
-		mask |= IN_ISDIR;
+		in_mask |= IN_ISDIR;
 
-	inotify_dentry_parent_queue_event(dentry, mask, 0, name);
-	inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
+	inotify_dentry_parent_queue_event(dentry, in_mask, 0, name);
+	inotify_inode_queue_event(inode, in_mask, 0, NULL, NULL);
+	fanotify(file, fan_mask);
 }
 
 /*
include/linux/sched.h +1 -0
index c226c7b..a20cf99 100644
@@ -1491,6 +1491,7 @@ extern cputime_t task_gtime(struct task_struct *p);
 #define PF_EXITING	0x00000004	/* getting shut down */
 #define PF_EXITPIDONE	0x00000008	/* pi exit done on shut down */
 #define PF_VCPU		0x00000010	/* I'm a virtual CPU */
+#define PF_NOFACCESS	0x00000020	/* exclude from all faccesses */
 #define PF_FORKNOEXEC	0x00000040	/* forked but didn't exec */
 #define PF_SUPERPRIV	0x00000100	/* used super-user privileges */
 #define PF_DUMPCORE	0x00000200	/* dumped core */
author
#4 lore raw

PATCH 4/16 fsnotify: sys_execve and sys_uselib do not call into fsnotify

In reply to Eric Paris

sys_execve and sys_uselib do not call into fsnotify so inotify, dnotify, and importantly to me fanotify do not see opens on things which are going to be exectued. Create a generic fsnotify hook for these paths.

---

 fs/exec.c                |    5 +++++
 include/linux/fsnotify.h |    7 +++++++
 2 files changed, 12 insertions(+), 0 deletions(-)
fs/exec.c +5 -0
index cecee50..3612805 100644
@@ -50,6 +50,7 @@
 #include <linux/cn_proc.h>
 #include <linux/audit.h>
 #include <linux/tracehook.h>
+#include <linux/fsnotify.h>
 
 #include <asm/uaccess.h>
 #include <asm/mmu_context.h>
@@ -138,6 +139,8 @@ asmlinkage long sys_uselib(const char __user * library)
 	if (IS_ERR(file))
 		goto out;
 
+	fsnotify_open_exec(file);
+
 	error = -ENOEXEC;
 	if(file->f_op) {
 		struct linux_binfmt * fmt;
@@ -690,6 +693,8 @@ struct file *open_exec(const char *name)
 	if (IS_ERR(file))
 		return file;
 
+	fsnotify_open_exec(file);
+
 	err = deny_write_access(file);
 	if (err) {
 		fput(file);
include/linux/fsnotify.h +7 -0
index 50d8d8b..6fc4160 100644
@@ -171,6 +171,13 @@ static inline void fsnotify_modify(struct file *file)
 }
 
 /*
+ * fsnotify_open_exec - file was opened by execve of uselib
+ */
+static inline void fsnotify_open_exec(struct file *file)
+{
+}
+
+/*
  * fsnotify_open - file was opened
  */
 static inline void fsnotify_open(struct file *file)
author
#5 lore raw

PATCH 5/16 fanotify: make use of the new fsnotify_open_exec calls

In reply to Eric Paris

This function sends fanotify events for opens which we know are being used for exec. These are basically just systecalls to sys_execve and sys_uselib

---

 include/linux/fanotify.h |    4 +++-
 include/linux/fsnotify.h |    3 ++-
 2 files changed, 5 insertions(+), 2 deletions(-)
include/linux/fanotify.h +3 -1
index 7f1179e..c991bd9 100644
@@ -14,13 +14,15 @@
 #define FAN_MODIFY		0x00000002	/* File was modified */
 #define FAN_CLOSE_NOWRITE	0x00000004	/* Unwrittable file closed */
 #define FAN_CLOSE_WRITE		0x00000008	/* Writtable file closed */
-#define FAN_OPEN		0x00000010	/* File was opened */
+#define FAN_OPEN_NOEXEC		0x00000010	/* File was opened */
+#define FAN_OPEN_EXEC		0x00000020	/* File was opened with the intention of being exec'ed */
 
 /* FIXME currently Q's have no limit.... */
 #define FAN_Q_OVERFLOW		0x80000000	/* Event queued overflowed */
 
 /* helper events */
 #define FAN_CLOSE		(FAN_CLOSE_WRITE | FAN_CLOSE_NOWRITE) /* close */
+#define FAN_OPEN		(FAN_OPEN_NOEXEC | FAN_OPEN_EXEC) /* open */
 
 /*
  * All of the events - we build the list by hand so that we can add flags in
include/linux/fsnotify.h +2 -1
index 6fc4160..ccee5ea 100644
@@ -175,6 +175,7 @@ static inline void fsnotify_modify(struct file *file)
  */
 static inline void fsnotify_open_exec(struct file *file)
 {
+	fanotify(file, FAN_OPEN_EXEC);
 }
 
 /*
@@ -191,7 +192,7 @@ static inline void fsnotify_open(struct file *file)
 
 	inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
 	inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
-	fanotify(file, FAN_OPEN);
+	fanotify(file, FAN_OPEN_NOEXEC);
 }
 
 /*
author
#6 lore raw

PATCH 6/16 fanotify: add a userspace interface for fanotify notifications

In reply to Eric Paris

notifications from fanotify need a userspace interface. We are going to try to do it with sockets. Scary....

---

 include/linux/fanotify.h   |   18 +++
 include/linux/socket.h     |    5 +
 net/Makefile               |    1 
 net/core/sock.c            |    6 +
 net/fanotify/Makefile      |    5 +
 net/fanotify/af_fanotify.c |  232 ++++++++++++++++++++++++++++++++++++++++++++
 net/fanotify/af_fanotify.h |   20 ++++
 7 files changed, 283 insertions(+), 4 deletions(-)
 create mode 100644 net/fanotify/Makefile
 create mode 100644 net/fanotify/af_fanotify.c
 create mode 100644 net/fanotify/af_fanotify.h
include/linux/fanotify.h +18 -0
index c991bd9..5add54f 100644
@@ -41,11 +41,22 @@
 #include <sys/types.h>
 #endif
 
+struct fanotify_addr {
+        uint32_t group_num;
+        uint32_t mask;
+        uint32_t timeout;
+        uint32_t unused[9];
+};
+
+/* struct used for FANOTIFY_GET_EVENT */
 struct fanotify_event_metadata {
 	int32_t fd;
 	uint32_t mask;
 };
 
+/* fanotify getsockopt optvals */
+#define FANOTIFY_GET_EVENT	1
+
 #ifdef __KERNEL__
 
 #include <linux/fs.h>
@@ -74,6 +85,8 @@ extern void fanotify(struct file *file, unsigned int mask);
 extern void fanotify_get_group(struct fanotify_group *group);
 extern struct fanotify_group *fanotify_find_group(unsigned int group_num, unsigned int mask);
 extern void fanotify_put_group(struct fanotify_group *group);
+
+extern int fanotify_create_event_fd(struct fanotify_group *group, struct fanotify_event_metadata *data, int nonblock);
 #else
 
 static inline void fanotify(struct file *file, unsigned int mask)
@@ -90,6 +103,11 @@ static inline struct fanotify_group *fanotify_find_group(unsigned int group_num,
 static inline extern void fanotify_put_group(struct fanotify_group *group)
 {}
 
+static inline int fanotify_create_event_fd(struct fanotify_group *group, struct fanotify_event_metadata *data, int nonblock)
+{
+	memset(data, 0, sizeof(struct fanotify_event_metadata));
+	return 0;
+}
 #endif	/* CONFIG_FANOTIFY */
 
 #endif	/* __KERNEL __ */
include/linux/socket.h +4 -1
index 20fc4bb..4fe7b83 100644
@@ -191,7 +191,8 @@ struct ucred {
 #define AF_RXRPC	33	/* RxRPC sockets 		*/
 #define AF_ISDN		34	/* mISDN sockets 		*/
 #define AF_PHONET	35	/* Phonet sockets		*/
-#define AF_MAX		36	/* For now.. */
+#define AF_FANOTIFY	36	/* fscking all access sockets	*/
+#define AF_MAX		37	/* For now.. */
 
 /* Protocol families, same as address families. */
 #define PF_UNSPEC	AF_UNSPEC
@@ -229,6 +230,7 @@ struct ucred {
 #define PF_RXRPC	AF_RXRPC
 #define PF_ISDN		AF_ISDN
 #define PF_PHONET	AF_PHONET
+#define PF_FANOTIFY	AF_FANOTIFY
 #define PF_MAX		AF_MAX
 
 /* Maximum queue length specifiable by listen.  */
@@ -298,6 +300,7 @@ struct ucred {
 #define SOL_PPPOL2TP	273
 #define SOL_BLUETOOTH	274
 #define SOL_PNPIPE	275
+#define SOL_FANOTIFY	276
 
 /* IPX options */
 #define IPX_TYPE	1
net/Makefile +1 -0
index 27d1f10..5b98ba4 100644
@@ -44,6 +44,7 @@ obj-$(CONFIG_ATM)		+= atm/
 obj-$(CONFIG_DECNET)		+= decnet/
 obj-$(CONFIG_ECONET)		+= econet/
 obj-$(CONFIG_PHONET)		+= phonet/
+obj-$(CONFIG_FANOTIFY)		+= fanotify/
 ifneq ($(CONFIG_VLAN_8021Q),)
 obj-y				+= 8021q/
 endif
net/core/sock.c +3 -3
index 5e2a313..c7b0cee 100644
@@ -155,7 +155,7 @@ static const char *af_family_key_strings[AF_MAX+1] = {
   "sk_lock-27"       , "sk_lock-28"          , "sk_lock-AF_CAN"      ,
   "sk_lock-AF_TIPC"  , "sk_lock-AF_BLUETOOTH", "sk_lock-IUCV"        ,
   "sk_lock-AF_RXRPC" , "sk_lock-AF_ISDN"     , "sk_lock-AF_PHONET"   ,
-  "sk_lock-AF_MAX"
+  "sk_lock-AF_FANOTIFY", "sk_lock-AF_MAX"
 };
 static const char *af_family_slock_key_strings[AF_MAX+1] = {
   "slock-AF_UNSPEC", "slock-AF_UNIX"     , "slock-AF_INET"     ,
@@ -170,7 +170,7 @@ static const char *af_family_slock_key_strings[AF_MAX+1] = {
   "slock-27"       , "slock-28"          , "slock-AF_CAN"      ,
   "slock-AF_TIPC"  , "slock-AF_BLUETOOTH", "slock-AF_IUCV"     ,
   "slock-AF_RXRPC" , "slock-AF_ISDN"     , "slock-AF_PHONET"   ,
-  "slock-AF_MAX"
+  "slock=AF_FANOTIFY", "slock-AF_MAX"
 };
 static const char *af_family_clock_key_strings[AF_MAX+1] = {
   "clock-AF_UNSPEC", "clock-AF_UNIX"     , "clock-AF_INET"     ,
@@ -185,7 +185,7 @@ static const char *af_family_clock_key_strings[AF_MAX+1] = {
   "clock-27"       , "clock-28"          , "clock-AF_CAN"      ,
   "clock-AF_TIPC"  , "clock-AF_BLUETOOTH", "clock-AF_IUCV"     ,
   "clock-AF_RXRPC" , "clock-AF_ISDN"     , "clock-AF_PHONET"   ,
-  "clock-AF_MAX"
+  "clock-AF_FANOTIFY", "clock-AF_MAX"
 };
 #endif
 
net/fanotify/Makefile new file +5 -0
new file mode 100644
index 0000000..348cf5e
@@ -0,0 +1,5 @@
+#
+# Makefile for the fanotify AF.
+#
+
+obj-$(CONFIG_FANOTIFY) += af_fanotify.o
net/fanotify/af_fanotify.c new file +232 -0

232 lines not on this page · raw message

net/fanotify/af_fanotify.h new file +20 -0
new file mode 100644
index 0000000..a987d7b
@@ -0,0 +1,20 @@
+#ifndef _LINUX_AF_FANOTIFY_H
+#define _LINUX_AF_FANOTIFY_H
+
+#ifdef __KERNEL__
+
+#include <linux/fanotify.h>
+#include <net/sock.h>
+
+struct fanotify_sock {
+	struct sock		sock;
+	struct fanotify_group	*group;
+};
+
+static inline struct fanotify_sock *fan_sk(struct sock *sock)
+{
+	return (struct fanotify_sock *)sock;
+}
+
+#endif /* __KERNEL__ */
+#endif /* _LINUX_AF_NET_H */
author
#7 lore raw

PATCH 7/16 fanotify: fastpath to ignore certain in core inodes

In reply to Eric Paris

Allows a process to ignore certain events for the given in core inodes. This does not preclude the need to keep a userspace or xattr based cache if a fanotifier listener wants something with a different lifetime than in core inode.

---

 fs/inode.c               |    6 +
 fs/notify/Makefile       |    2 
 fs/notify/fanotify.c     |   19 ++++
 fs/notify/fanotify.h     |   34 +++++++
 fs/notify/fastpath.c     |  230 ++++++++++++++++++++++++++++++++++++++++++++++
 fs/notify/group.c        |    3 +
 include/linux/fanotify.h |   17 +++
 include/linux/fs.h       |    5 +
 include/linux/fsnotify.h |    8 ++
 9 files changed, 323 insertions(+), 1 deletions(-)
 create mode 100644 fs/notify/fastpath.c
fs/inode.c +6 -0
index 0487ddb..cc802ae 100644
@@ -21,6 +21,7 @@
 #include <linux/cdev.h>
 #include <linux/bootmem.h>
 #include <linux/inotify.h>
+#include <linux/fsnotify.h>
 #include <linux/mount.h>
 
 /*
@@ -183,6 +184,10 @@ static struct inode *alloc_inode(struct super_block *sb)
 		}
 		inode->i_private = NULL;
 		inode->i_mapping = mapping;
+#ifdef CONFIG_FANOTIFY
+		INIT_LIST_HEAD(&inode->fastpath_entries);
+		rwlock_init(&inode->fastpath_rwlock);
+#endif
 	}
 	return inode;
 }
@@ -191,6 +196,7 @@ void destroy_inode(struct inode *inode)
 {
 	BUG_ON(inode_has_buffers(inode));
 	security_inode_free(inode);
+	fsnotify_inode_delete(inode);
 	if (inode->i_sb->s_op->destroy_inode)
 		inode->i_sb->s_op->destroy_inode(inode);
 	else
fs/notify/Makefile +1 -1
index c467c97..90cb910 100644
@@ -3,4 +3,4 @@ obj-$(CONFIG_INOTIFY_USER)	+= inotify_user.o
 
 obj-$(CONFIG_DNOTIFY)		+= dnotify.o
 
-obj-$(CONFIG_FANOTIFY)		+= fanotify.o notification.o group.o
+obj-$(CONFIG_FANOTIFY)		+= fanotify.o notification.o group.o fastpath.o
fs/notify/fanotify.c +19 -0
index e2a338e..aa12e91 100644
@@ -35,6 +35,15 @@
 
 struct dentry *fanotify_fs_root;
 
+void fanotify_inode_delete(struct inode *inode)
+{
+	if (likely(list_empty(&fanotify_groups)))
+		return;
+
+	fanotify_fastpath_clear(inode);
+}
+EXPORT_SYMBOL_GPL(fanotify_inode_delete);
+
 void fanotify(struct file *file, unsigned int mask)
 {
 	struct fanotify_group *group;
@@ -52,6 +61,8 @@ void fanotify(struct file *file, unsigned int mask)
 	if (!S_ISREG(inode->i_mode))
 		return;
 
+	if (mask & FAN_MODIFY)
+		fanotify_fastpath_clear(inode);
 	/*
 	 * SRCU!!  the groups list is very very much read only and the path is
 	 * very hot (assuming something is using fanotify)  Not blocking while
@@ -67,6 +78,8 @@ void fanotify(struct file *file, unsigned int mask)
 	idx = srcu_read_lock(&fanotify_grp_srcu_struct);
 	list_for_each_entry_rcu(group, &fanotify_groups, group_list) {
 		if (mask & group->mask) {
+			if (fanotify_is_fastpath(file, mask, group))
+				continue;
 			if (!event) {
 				event = create_event(file, mask);
 				/* shit, we OOM'd and now we can't tell, lets hope something else blows up */
@@ -94,6 +107,12 @@ static __init int fanotify_init(void)
 	if (ret)
 		return ret;
 
+	ret = fanotify_fastpath_init();
+	if (ret) {
+		fanotify_notification_uninit();
+		return ret;
+	}
+
 	return init_srcu_struct(&fanotify_grp_srcu_struct);
 }
 __initcall(fanotify_init);
fs/notify/fanotify.h +34 -0
index 5b0253e..091ea5e 100644
@@ -46,6 +46,31 @@ struct fanotify_event {
 	atomic_t refcnt;	/* how many groups still are using/need to send this event */
 };
 
+/*
+ * a fastpath is simply an entry attached to an in core inode which allows an
+ * fanotify listener to indicate they are no longer interested in events of
+ * a type matching mask.
+ *
+ * these are flushed when an inode is evicted from core or when the inode is
+ * modified (as seen by fsnotify_access)
+ */
+struct fanotify_fastpath_entry {
+	struct fanotify_group *group;	/* group this fastpath entry is for */
+	unsigned int mask;		/* mask this fastpath entry is for */
+	struct inode *inode;		/* inode this entry is associated with */
+	/*
+	 * killcnt starts at 0 and is incremented when this entry will be killed
+	 * since multiple processes my try to kill it at the same time.
+	 * One might try to kill it from walking the i_list when an inode is being
+	 * kicked out of cache and one might try to kill it when an fanotify group
+	 * is being unregistered.  The last one to finish cleaning everything up
+	 * and decrement it to 0 will do the actual killing
+	 */
+	atomic_t killcnt;
+	struct list_head i_list;	/* list of fastpath_entries by inode->fastpath_entries */
+	struct list_head g_list;	/* list of fastpath_entries by group->fastpath_entries */
+};
+
 extern struct mutex fanotify_grp_mutex;
 extern struct srcu_struct fanotify_grp_srcu_struct;
 extern struct list_head fanotify_groups;
@@ -61,4 +86,13 @@ extern struct fanotify_event_holder *alloc_event_holder(void);
 extern void fanotify_destroy_event_holder(struct fanotify_event_holder *holder);
 extern __init int fanotify_notification_init(void);
 extern __init int fanotify_notification_uninit(void);
+
+extern void fanotify_fastpath_get(struct fanotify_fastpath_entry *entry);
+extern void fanotify_fastpath_put(struct fanotify_fastpath_entry *entry);
+extern int fanotify_is_fastpath(struct file *file, unsigned int mask, struct fanotify_group *group);
+extern void fanotify_fastpath_clear_group(struct fanotify_group *group);
+extern void fanotify_fastpath_clear(struct inode *inode);
+extern __init int fanotify_fastpath_init(void);
+extern __init int fanotify_fastpath_uninit(void);
+
 #endif	/* _LINUX_FANOTIFY_PRIVATE_H */
fs/notify/fastpath.c new file +230 -0

230 lines not on this page · raw message

fs/notify/group.c +3 -0
index fc3d736..ff9b5ef 100644
@@ -87,6 +87,9 @@ void fanotify_kill_group(struct fanotify_group *group)
 	/* clear the notification queue of all events */
 	fanotify_notification_clear_group(group);
 
+	/* clear all fastpath entries for this group */
+	fanotify_fastpath_clear_group(group);
+
 	kfree(group);
 }
 
include/linux/fanotify.h +17 -0
index 5add54f..015cfea 100644
@@ -76,17 +76,25 @@ struct fanotify_group {
 	struct mutex notification_mutex;/* protect the notification_list */
 	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 */
+
+	/* 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 */
 };
 
 #ifdef CONFIG_FANOTIFY
 
 extern void fanotify(struct file *file, unsigned int mask);
+extern void fanotify_inode_delete(struct inode *inode);
 
 extern void fanotify_get_group(struct fanotify_group *group);
 extern struct fanotify_group *fanotify_find_group(unsigned int group_num, unsigned int mask);
 extern void fanotify_put_group(struct fanotify_group *group);
 
+/* things called from the socket */
 extern int fanotify_create_event_fd(struct fanotify_group *group, struct fanotify_event_metadata *data, int nonblock);
+extern int fanotify_fastpath_add(struct fanotify_group *group, int fd, unsigned int mask);
+
 #else
 
 static inline void fanotify(struct file *file, unsigned int mask)
@@ -108,6 +116,15 @@ static inline int fanotify_create_event_fd(struct fanotify_group *group, struct
 	memset(data, 0, sizeof(struct fanotify_event_metadata));
 	return 0;
 }
+
+static inline int fanotify_fastpath_add(struct fanotify_group *group, int fd, unsigned int mask)
+{
+	return 0;
+}
+
+static inline void fanotify_inode_delete(struct inode *inode)
+{}
+
 #endif	/* CONFIG_FANOTIFY */
 
 #endif	/* __KERNEL __ */
include/linux/fs.h +5 -0
index 44e3cb2..3d877e3 100644
@@ -676,6 +676,11 @@ struct inode {
 	struct mutex		inotify_mutex;	/* protects the watches list */
 #endif
 
+#ifdef CONFIG_FANOTIFY
+	struct list_head	fastpath_entries; /* fanotify fastpath entries protected by group */
+	rwlock_t		fastpath_rwlock; /* protect the fastpath entries list */
+#endif
+
 	unsigned long		i_state;
 	unsigned long		dirtied_when;	/* jiffies of first dirtying */
 
include/linux/fsnotify.h +8 -0
index ccee5ea..5af4225 100644
@@ -82,6 +82,14 @@ static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
 }
 
 /*
+ * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
+ */
+static inline void fsnotify_inode_delete(struct inode *inode)
+{
+	fanotify_inode_delete(inode);
+}
+
+/*
  * fsnotify_inoderemove - an inode is going away
  */
 static inline void fsnotify_inoderemove(struct inode *inode)
author
#8 lore raw

PATCH 8/16 fanotify: add a userspace interface for fastpaths

In reply to Eric Paris

turns out the fastpath code is useless without an interface. This one works.

---

 fs/notify/group.c          |    3 +++
 include/linux/fanotify.h   |    9 +++++++++
 net/fanotify/af_fanotify.c |   31 ++++++++++++++++++++++++++++++-
 3 files changed, 42 insertions(+), 1 deletions(-)
fs/notify/group.c +3 -0
index ff9b5ef..21037cc 100644
@@ -74,6 +74,9 @@ struct fanotify_group *fanotify_find_group(unsigned int group_num, unsigned int
 	INIT_LIST_HEAD(&group->notification_list);
 	init_waitqueue_head(&group->notification_waitq);
 
+	mutex_init(&group->fastpath_mutex);
+	INIT_LIST_HEAD(&group->fastpath_entries);
+
 	/* add it */
 	list_add_rcu(&group->group_list, &fanotify_groups);
 
include/linux/fanotify.h +9 -0
index 015cfea..a1ea7e0 100644
@@ -57,6 +57,15 @@ struct fanotify_event_metadata {
 /* fanotify getsockopt optvals */
 #define FANOTIFY_GET_EVENT	1
 
+/* struct used for FANOTIFY_SET_FASTPATH */
+struct fanotify_so_fastpath {
+	int32_t fd;
+	uint32_t mask;
+};
+
+/* fanotify setsockopt optvals */
+#define FANOTIFY_SET_FASTPATH	1
+
 #ifdef __KERNEL__
 
 #include <linux/fs.h>
net/fanotify/af_fanotify.c +30 -1
index eb2d023..f63ee7a 100644
@@ -159,6 +159,35 @@ static unsigned int fan_poll(struct file * file, struct socket *sock, poll_table
 	return 0;
 }
 
+static int fan_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen)
+{
+	struct fanotify_sock *fan_sock;
+	struct fanotify_group *group;
+	struct fanotify_so_fastpath fp_data;
+	int ret = 0;
+
+	if (sock->state != SS_CONNECTED)
+		return -EBADF;
+
+	fan_sock = fan_sk(sock->sk);
+	group = fan_sock->group;
+
+	switch(optname) {
+	case FANOTIFY_SET_FASTPATH:
+		if (optlen < sizeof(struct fanotify_so_fastpath))
+			return -ENOMEM;
+		ret = copy_from_user(&fp_data, optval, sizeof(struct fanotify_so_fastpath));
+		if (ret)
+			return ret;
+		fanotify_fastpath_add(group, fp_data.fd, fp_data.mask);
+		break;
+	default:
+		return -ENOPROTOOPT;
+	}
+
+	return ret;
+}
+
 static int fan_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
 {
 	struct fanotify_sock *fan_sock;
@@ -210,7 +239,7 @@ static const struct proto_ops fanotify_proto_ops = {
 	.ioctl =	sock_no_ioctl,
 	.listen =	sock_no_listen,
 	.shutdown =	sock_no_shutdown,
-	.setsockopt =	sock_no_setsockopt,
+	.setsockopt =	fan_setsockopt,
 	.getsockopt =	fan_getsockopt,
 	.sendmsg =	sock_no_sendmsg,
 	.recvmsg =	sock_no_recvmsg,
author
#9 lore raw

PATCH 9/16 fanotify: add group priorities

In reply to Eric Paris

In preperation for blocking fanotify calls group priorities must be added. async events will be sent as quickly as possible without waiting for a response.

---

 fs/notify/group.c          |   30 ++++++++++++++++++++++++------
 include/linux/fanotify.h   |    5 ++++-
 net/fanotify/af_fanotify.c |    2 +-
 3 files changed, 29 insertions(+), 8 deletions(-)
fs/notify/group.c +24 -6
index 21037cc..37b8630 100644
@@ -42,15 +42,16 @@ void fanotify_get_group(struct fanotify_group *group)
 	atomic_inc(&group->refcnt);
 }
 
-struct fanotify_group *fanotify_find_group(unsigned int group_num, unsigned int mask)
+struct fanotify_group *fanotify_find_group(unsigned int priority, unsigned int group_num, unsigned int mask)
 {
 	struct fanotify_group *group_iter;
 	struct fanotify_group *group = NULL;
 
 	mutex_lock(&fanotify_grp_mutex);
 	list_for_each_entry_rcu(group_iter, &fanotify_groups, group_list) {
-		if (group_iter->group_num == group_num) {
-			if (group_iter->mask == mask) {
+		if (group_iter->priority == priority) {
+			if ((group_iter->mask == mask) &&
+			    (group_iter->group_num == group_num)) {
 				fanotify_get_group(group_iter);
 				group = group_iter;
 			} else
@@ -69,6 +70,7 @@ struct fanotify_group *fanotify_find_group(unsigned int group_num, unsigned int
 
 	group->group_num = group_num;
 	group->mask = mask;
+	group->priority = priority;
 
 	mutex_init(&group->notification_mutex);
 	INIT_LIST_HEAD(&group->notification_list);
@@ -77,9 +79,26 @@ struct fanotify_group *fanotify_find_group(unsigned int group_num, unsigned int
 	mutex_init(&group->fastpath_mutex);
 	INIT_LIST_HEAD(&group->fastpath_entries);
 
-	/* add it */
-	list_add_rcu(&group->group_list, &fanotify_groups);
+	/* Do we need to be the first entry? */
+	if (list_empty(&fanotify_groups)) {
+		list_add_rcu(&group->group_list, &fanotify_groups);
+		goto out;
+	}
+
+	list_for_each_entry(group_iter, &fanotify_groups, group_list) {
+		/* insert in front of this one? */
+		if (priority < group_iter->priority) {
+			/* I used list_add_tail() to insert in front of group_iter...  */
+			list_add_tail_rcu(&group->group_list, &group_iter->group_list);
+			break;
+		}
 
+		/* are we at the end?  if so insert at end */
+		if (list_is_last(&group_iter->group_list, &fanotify_groups)) {
+			list_add_tail_rcu(&group->group_list, &fanotify_groups);
+			break;
+		}
+	}
 out:
 	mutex_unlock(&fanotify_grp_mutex);
 	return group;
@@ -100,7 +119,6 @@ void fanotify_put_group(struct fanotify_group *group)
 {
 	mutex_lock(&fanotify_grp_mutex);
 	if (atomic_dec_and_test(&group->refcnt)) {
-
 		list_del_rcu(&group->group_list);
 		mutex_unlock(&fanotify_grp_mutex);
 
include/linux/fanotify.h +4 -1
index a1ea7e0..22496b2 100644
@@ -42,6 +42,7 @@
 #endif
 
 struct fanotify_addr {
+	uint32_t priority;
         uint32_t group_num;
         uint32_t mask;
         uint32_t timeout;
@@ -89,6 +90,8 @@ struct fanotify_group {
 	/* 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 */
+
+	unsigned int priority;		/* order this group should receive msgs.  low first */
 };
 
 #ifdef CONFIG_FANOTIFY
@@ -97,7 +100,7 @@ extern void fanotify(struct file *file, unsigned int mask);
 extern void fanotify_inode_delete(struct inode *inode);
 
 extern void fanotify_get_group(struct fanotify_group *group);
-extern struct fanotify_group *fanotify_find_group(unsigned int group_num, unsigned int mask);
+extern struct fanotify_group *fanotify_find_group(unsigned int priority, unsigned int group_num, unsigned int mask);
 extern void fanotify_put_group(struct fanotify_group *group);
 
 /* things called from the socket */
net/fanotify/af_fanotify.c +1 -1
index f63ee7a..a7d3b96 100644
@@ -143,7 +143,7 @@ static int fan_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
 		return -EINVAL;
 
 	fan_sock = fan_sk(sock->sk);
-	fan_sock->group = fanotify_find_group(fan_addr->group_num, fan_addr->mask);
+	fan_sock->group = fanotify_find_group(fan_addr->priority, fan_addr->group_num, fan_addr->mask);
 
 	if (IS_ERR(fan_sock->group))
 		return PTR_ERR(fan_sock->group);
author
#10 lore raw

PATCH 10/16 fanotify: blocking and access granting

In reply to Eric Paris

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       |  164 ++++++++++++++++++++++++++++++++++++++++++++++
 fs/notify/fanotify.c     |   46 +++++++++++--
 fs/notify/fanotify.h     |   17 ++++-
 fs/notify/group.c        |    8 ++
 fs/notify/notification.c |    2 +
 fs/open.c                |    5 +
 fs/read_write.c          |    6 ++
 include/linux/fanotify.h |   35 +++++++++-
 10 files changed, 278 insertions(+), 14 deletions(-)
 create mode 100644 fs/notify/access.c
fs/aio.c +7 -0
index f658441..008de79 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_ACCESS_PERM);
+		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_ACCESS_PERM);
+		if (unlikely(ret))
+			break;
 		ret = aio_setup_vectored_rw(READ, kiocb);
 		if (ret)
 			break;
fs/notify/Makefile +1 -1
index 90cb910..0af890a 100644
@@ -3,4 +3,4 @@ obj-$(CONFIG_INOTIFY_USER)	+= inotify_user.o
 
 obj-$(CONFIG_DNOTIFY)		+= dnotify.o
 
-obj-$(CONFIG_FANOTIFY)		+= fanotify.o notification.o group.o fastpath.o
+obj-$(CONFIG_FANOTIFY)		+= fanotify.o notification.o group.o fastpath.o access.o
fs/notify/access.c new file +164 -0

164 lines not on this page · raw message

fs/notify/fanotify.c +40 -6

75 lines not on this page · raw message

fs/notify/fanotify.h +13 -4
index 091ea5e..e07703f 100644
@@ -14,12 +14,14 @@
 #include <linux/wait.h>
 
 /*
- * 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
  * 
  */
@@ -44,6 +46,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 */
 };
 
 /*
@@ -95,4 +100,8 @@ extern void fanotify_fastpath_clear(struct inode *inode);
 extern __init int fanotify_fastpath_init(void);
 extern __init int fanotify_fastpath_uninit(void);
 
+extern int fanotify_add_event_to_access(struct fanotify_group *group, struct fanotify_event *event);
+extern int fanotify_get_response_from_access(struct fanotify_group *group, struct fanotify_event *event);
+extern void fanotify_access_clear_group(struct fanotify_group *group);
+
 #endif	/* _LINUX_FANOTIFY_PRIVATE_H */
fs/notify/group.c +8 -0
index 37b8630..68f895e 100644
@@ -79,6 +79,11 @@ struct fanotify_group *fanotify_find_group(unsigned int priority, unsigned int g
 	mutex_init(&group->fastpath_mutex);
 	INIT_LIST_HEAD(&group->fastpath_entries);
 
+	atomic_set(&group->cookie, 0);
+	mutex_init(&group->access_mutex);
+	INIT_LIST_HEAD(&group->access_list);
+	init_waitqueue_head(&group->access_waitq);
+
 	/* Do we need to be the first entry? */
 	if (list_empty(&fanotify_groups)) {
 		list_add_rcu(&group->group_list, &fanotify_groups);
@@ -112,6 +117,9 @@ void fanotify_kill_group(struct fanotify_group *group)
 	/* clear all fastpath entries for this group */
 	fanotify_fastpath_clear_group(group);
 
+	/* set an allow for all outstanding access decisions */
+	fanotify_access_clear_group(group);
+
 	kfree(group);
 }
 
fs/notify/notification.c +2 -0
index 3dd69ef..89bb02c 100644
@@ -176,6 +176,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);
@@ -241,6 +242,7 @@ int fanotify_create_event_fd(struct fanotify_group *group, struct fanotify_event
 
 	data->fd = client_fd;
 	data->mask = event->mask;
+	data->cookie = event->cookie;
 
 	fanotify_put_event(event);
 
fs/open.c +5 -0
index 1057c34..b9d9335 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_PERM);
+	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..a74cf76 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_ACCESS_PERM);
+		if (retval)
+			return retval;
+	}
 	return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
 }
 
include/linux/fanotify.h +32 -3
index 22496b2..0405e4c 100644
@@ -17,6 +17,10 @@
 #define FAN_OPEN_NOEXEC		0x00000010	/* File was opened */
 #define FAN_OPEN_EXEC		0x00000020	/* File was opened with the intention of being exec'ed */
 
+/* userspace may also request blocking for permission checks for open and read */
+#define FAN_ACCESS_PERM		0x00000100
+#define FAN_OPEN_PERM		0x00000200
+
 /* FIXME currently Q's have no limit.... */
 #define FAN_Q_OVERFLOW		0x80000000	/* Event queued overflowed */
 
@@ -33,6 +37,16 @@
 			 FAN_MODIFY |\
 			 FAN_CLOSE |\
 			 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_PERM    (FAN_OPEN_PERM |\
+				FAN_ACCESS_PERM)
+
+/* answers will need to be either allow or deny */
+#define FAN_ALLOW		0x00000001
+#define FAN_DENY		0x00000002
 
 #ifdef __KERNEL__
 #include <linux/types.h>
@@ -53,6 +67,7 @@ struct fanotify_addr {
 struct fanotify_event_metadata {
 	int32_t fd;
 	uint32_t mask;
+	uint64_t cookie;
 };
 
 /* fanotify getsockopt optvals */
@@ -91,12 +106,18 @@ struct fanotify_group {
 	struct mutex fastpath_mutex;	/* protect fastpath_entries list */
 	struct list_head fastpath_entries; /* all fastpath entries for this group */
 
+	/* 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 */
+
 	unsigned int priority;		/* order this group should receive msgs.  low first */
 };
 
 #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);
 
 extern void fanotify_get_group(struct fanotify_group *group);
@@ -106,11 +127,14 @@ extern void fanotify_put_group(struct fanotify_group *group);
 /* things called from the socket */
 extern int fanotify_create_event_fd(struct fanotify_group *group, struct fanotify_event_metadata *data, int nonblock);
 extern int fanotify_fastpath_add(struct fanotify_group *group, int fd, unsigned int mask);
+extern int fanotify_process_access_response(struct fanotify_group *group, unsigned long cookie, unsigned int response);
 
 #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_get_group(struct fanotify_group *group)
 {}
@@ -137,6 +161,11 @@ static inline int fanotify_fastpath_add(struct fanotify_group *group, int fd, un
 static inline void fanotify_inode_delete(struct inode *inode)
 {}
 
+static inline int fanotify_process_access_response(struct fanotify_group *group, unsigned long cookie, unsigned int response)
+{
+	return 0;
+}
+
 #endif	/* CONFIG_FANOTIFY */
 
 #endif	/* __KERNEL __ */
author
#11 lore raw

PATCH 11/16 fanotify: give a special access permission check

In reply to Eric Paris

add a special FANOTIFY_ACCESS_EXEC_PERM check. This will be applied any place a process tries to access a file with execute permissions. Mainly sys_execve, sys_uselib, and mmap'ing a file for exec.

---

 fs/aio.c                 |    4 ++--
 fs/exec.c                |   10 ++++++++++
 fs/read_write.c          |    2 +-
 include/linux/fanotify.h |    6 ++++--
 mm/mmap.c                |    7 +++++++
 mm/mprotect.c            |    6 ++++++
 mm/nommu.c               |    7 +++++++
 7 files changed, 37 insertions(+), 5 deletions(-)
fs/aio.c +2 -2
index 008de79..6395403 100644
@@ -1455,7 +1455,7 @@ static ssize_t aio_setup_iocb(struct kiocb *kiocb)
 		ret = security_file_permission(file, MAY_READ);
 		if (unlikely(ret))
 			break;
-		ret = fanotify(file, FAN_ACCESS_PERM);
+		ret = fanotify(file, FAN_ACCESS_NOEXEC_PERM);
 		if (unlikely(ret))
 			break;
 		ret = aio_setup_single_vector(kiocb);
@@ -1490,7 +1490,7 @@ static ssize_t aio_setup_iocb(struct kiocb *kiocb)
 		ret = security_file_permission(file, MAY_READ);
 		if (unlikely(ret))
 			break;
-		ret = fanotify(file, FAN_ACCESS_PERM);
+		ret = fanotify(file, FAN_ACCESS_NOEXEC_PERM);
 		if (unlikely(ret))
 			break;
 		ret = aio_setup_vectored_rw(READ, kiocb);
fs/exec.c +10 -0
index 3612805..ab45d65 100644
@@ -140,6 +140,11 @@ asmlinkage long sys_uselib(const char __user * library)
 		goto out;
 
 	fsnotify_open_exec(file);
+	error = fanotify(file, FAN_ACCESS_EXEC_PERM);
+	if (error) {
+		fput(file);
+		goto out;
+	}
 
 	error = -ENOEXEC;
 	if(file->f_op) {
@@ -694,6 +699,11 @@ struct file *open_exec(const char *name)
 		return file;
 
 	fsnotify_open_exec(file);
+	err = fanotify(file, FAN_ACCESS_EXEC_PERM);
+	if (err) {
+		fput(file);
+		goto out;
+	}
 
 	err = deny_write_access(file);
 	if (err) {
fs/read_write.c +1 -1
index a74cf76..8ab4646 100644
@@ -203,7 +203,7 @@ int rw_verify_area(int read_write, struct file *file, loff_t *ppos, size_t count
 	if (retval)
 		return retval;
 	if (read_write == READ) {
-		retval = fanotify(file, FAN_ACCESS_PERM);
+		retval = fanotify(file, FAN_ACCESS_NOEXEC_PERM);
 		if (retval)
 			return retval;
 	}
include/linux/fanotify.h +4 -2
index 0405e4c..07b8e77 100644
@@ -18,8 +18,9 @@
 #define FAN_OPEN_EXEC		0x00000020	/* File was opened with the intention of being exec'ed */
 
 /* userspace may also request blocking for permission checks for open and read */
-#define FAN_ACCESS_PERM		0x00000100
-#define FAN_OPEN_PERM		0x00000200
+#define FAN_ACCESS_NOEXEC_PERM	0x00000100
+#define FAN_ACCESS_EXEC_PERM	0x00000200
+#define FAN_OPEN_PERM		0x00000400
 
 /* FIXME currently Q's have no limit.... */
 #define FAN_Q_OVERFLOW		0x80000000	/* Event queued overflowed */
@@ -27,6 +28,7 @@
 /* helper events */
 #define FAN_CLOSE		(FAN_CLOSE_WRITE | FAN_CLOSE_NOWRITE) /* close */
 #define FAN_OPEN		(FAN_OPEN_NOEXEC | FAN_OPEN_EXEC) /* open */
+#define FAN_ACCESS_PERM		(FAN_ACCESS_NOEXEC_PERM | FAN_ACCESS_EXEC_PERM) /* access perm */
 
 /*
  * All of the events - we build the list by hand so that we can add flags in
mm/mmap.c +7 -0
index e7a5a68..35d051f 100644
@@ -18,6 +18,7 @@
 #include <linux/init.h>
 #include <linux/file.h>
 #include <linux/fs.h>
+#include <linux/fanotify.h>
 #include <linux/personality.h>
 #include <linux/security.h>
 #include <linux/hugetlb.h>
@@ -1027,6 +1028,12 @@ unsigned long do_mmap_pgoff(struct file * file, unsigned long addr,
 		default:
 			return -EINVAL;
 		}
+
+		if (prot & PROT_EXEC) {
+			error = fanotify(file, FAN_ACCESS_EXEC_PERM);
+			if (error)
+				return error;
+		}
 	} else {
 		switch (flags & MAP_TYPE) {
 		case MAP_SHARED:
mm/mprotect.c +6 -0
index fded06f..af1da2d 100644
@@ -16,6 +16,7 @@
 #include <linux/fs.h>
 #include <linux/highmem.h>
 #include <linux/security.h>
+#include <linux/fanotify.h>
 #include <linux/mempolicy.h>
 #include <linux/personality.h>
 #include <linux/syscalls.h>
@@ -294,6 +295,11 @@ sys_mprotect(unsigned long start, size_t len, unsigned long prot)
 		if (error)
 			goto out;
 
+		if (vma->vm_file && (prot & PROT_EXEC))
+			error = fanotify(vma->vm_file, FAN_ACCESS_EXEC_PERM);
+		if (error)
+			goto out;
+
 		tmp = vma->vm_end;
 		if (tmp > end)
 			tmp = end;
mm/nommu.c +7 -0
index ed75bc9..f541693 100644
@@ -18,6 +18,7 @@
 #include <linux/mman.h>
 #include <linux/swap.h>
 #include <linux/file.h>
+#include <linux/fanotify.h>
 #include <linux/highmem.h>
 #include <linux/pagemap.h>
 #include <linux/slab.h>
@@ -709,6 +710,12 @@ static int validate_mmap_request(struct file *file,
 			/* backing file is not executable, try to copy */
 			capabilities &= ~BDI_CAP_MAP_DIRECT;
 		}
+
+		if (prot & PROT_EXEC) {
+			ret = fanotify(file, FAN_ACCESS_EXEC_PERM);
+			if (ret)
+				return ret;
+		}
 	}
 	else {
 		/* anonymous mappings are always memory backed and can be
author
#12 lore raw

PATCH 12/16 fanotify: user interface for access decisions

In reply to Eric Paris

turns out that access decisions are useless if there is no way to do anything about them. So we add an interface. Alan will hate it, but at least it works...

---

 include/linux/fanotify.h   |    7 +++++++
 net/fanotify/af_fanotify.c |   11 ++++++++++-
 2 files changed, 17 insertions(+), 1 deletions(-)
include/linux/fanotify.h +7 -0
index 07b8e77..b5c9db9 100644
@@ -81,8 +81,15 @@ struct fanotify_so_fastpath {
 	uint32_t mask;
 };
 
+/* struct used for FANOTIFY_SEND_RESPONSE */
+struct fanotify_so_access {
+	uint64_t cookie;
+	uint32_t response;
+};
+
 /* fanotify setsockopt optvals */
 #define FANOTIFY_SET_FASTPATH	1
+#define FANOTIFY_SEND_RESPONSE	2
 
 #ifdef __KERNEL__
 
net/fanotify/af_fanotify.c +10 -1
index a7d3b96..89099ab 100644
@@ -164,6 +164,7 @@ static int fan_setsockopt(struct socket *sock, int level, int optname, char __us
 	struct fanotify_sock *fan_sock;
 	struct fanotify_group *group;
 	struct fanotify_so_fastpath fp_data;
+	struct fanotify_so_access access_data;
 	int ret = 0;
 
 	if (sock->state != SS_CONNECTED)
@@ -179,7 +180,15 @@ static int fan_setsockopt(struct socket *sock, int level, int optname, char __us
 		ret = copy_from_user(&fp_data, optval, sizeof(struct fanotify_so_fastpath));
 		if (ret)
 			return ret;
-		fanotify_fastpath_add(group, fp_data.fd, fp_data.mask);
+		ret = fanotify_fastpath_add(group, fp_data.fd, fp_data.mask);
+		break;
+	case FANOTIFY_SEND_RESPONSE:
+		if (optlen < sizeof(struct fanotify_so_access))
+			return -ENOMEM;
+		ret = copy_from_user(&access_data, optval, sizeof(struct fanotify_so_access));
+		if (ret)
+			return ret;
+		ret = fanotify_process_access_response(group, access_data.cookie, access_data.response);
 		break;
 	default:
 		return -ENOPROTOOPT;
author
#13 lore raw

PATCH 13/16 fanotify: ability for userspace to delay responses

In reply to Eric Paris

Userspace may know that it is behind, may know it is going to take a while to get a good response, or have any number of other reasons to block an access request. FAN_RESTTIMER will reset the timeout for an fanotify access decision response.

---

 fs/notify/access.c       |   31 +++++++++++++++++++++++++++----
 fs/notify/fanotify.h     |    1 +
 fs/notify/notification.c |    2 ++
 include/linux/fanotify.h |    1 +
 4 files changed, 31 insertions(+), 4 deletions(-)
fs/notify/access.c +27 -4
index 6a2606c..5b3b32e 100644
@@ -55,6 +55,8 @@ int fanotify_add_event_to_access(struct fanotify_group *group, struct fanotify_e
 int fanotify_get_response_from_access(struct fanotify_group *group, struct fanotify_event *event)
 {
 	int ret;
+
+retry:
 	ret = wait_event_interruptible_timeout(group->access_waitq,
 						event->response,
 						msecs_to_jiffies(5000));
@@ -89,13 +91,25 @@ int fanotify_get_response_from_access(struct fanotify_group *group, struct fanot
 	}
 
 	/* userspace responded, convert to something usable */
+	spin_lock(&event->response_lock);
 	switch (event->response) {
 	case FAN_ALLOW:
-		return 0;
+		ret = 0;
+		break;
 	case FAN_DENY:
+		ret = -EPERM;
+		break;
+	case FAN_RESETTIMER:
+		event->response = 0;
+		spin_unlock(&event->response_lock);
+		goto retry;
 	default:
-		return -EPERM;
+		ret = -EPERM;
+		break;
 	}
+	spin_unlock(&event->response_lock);
+
+	return ret;
 }
 
 int fanotify_process_access_response(struct fanotify_group *group, unsigned long cookie, unsigned int response)
@@ -108,7 +122,7 @@ int fanotify_process_access_response(struct fanotify_group *group, unsigned long
 	 * userspace can send a valid responce or we will clean it up after the
 	 * timeout
 	 */
-	if (response & ~(FAN_ALLOW | FAN_DENY))
+	if (response & ~(FAN_ALLOW | FAN_DENY | FAN_RESETTIMER))
 		return -EINVAL;
 
 	mutex_lock(&group->access_mutex);
@@ -117,6 +131,11 @@ int fanotify_process_access_response(struct fanotify_group *group, unsigned long
 			continue;
 
 		event = holder->event;
+
+		/* FAN_RESETTIMER should be left on the list for later....  */
+		if (response == FAN_RESETTIMER)
+			break;
+
 		spin_lock(&event->holder_spinlock);
 		holder->event = NULL;
 		/* as soon as we do this the event.holder might be reused */
@@ -132,9 +151,13 @@ int fanotify_process_access_response(struct fanotify_group *group, unsigned long
 	if (!event)
 		return -ENOENT;
 
+	spin_lock(&event->response_lock);
 	event->response = response;
+	spin_unlock(&event->response_lock);
 	wake_up(&group->access_waitq);
-	fanotify_put_event(event);
+
+	if (response != FAN_RESETTIMER)
+		fanotify_put_event(event);
 
 	return 0;
 }
fs/notify/fanotify.h +1 -0
index e07703f..d65db61 100644
@@ -48,6 +48,7 @@ struct fanotify_event {
 	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;
+	spinlock_t response_lock; /* protects response */
 	unsigned int response;	/* userspace answer to question */
 };
 
fs/notify/notification.c +2 -0
index 89bb02c..dd0d2db 100644
@@ -27,6 +27,7 @@
 #include <linux/path.h>
 #include <linux/sched.h>
 #include <linux/slab.h>
+#include <linux/spinlock.h>
 
 #include <linux/fanotify.h>
 #include "fanotify.h"
@@ -169,6 +170,7 @@ struct fanotify_event *create_event(struct file *file, unsigned int mask)
 	event->holder.event = NULL;
 	INIT_LIST_HEAD(&event->holder.event_list);
 	atomic_set(&event->refcnt, 1);
+	spin_lock_init(&event->response_lock);
 
 	spin_lock_init(&event->holder_spinlock);
 
include/linux/fanotify.h +1 -0
index b5c9db9..a36bd6b 100644
@@ -49,6 +49,7 @@
 /* answers will need to be either allow or deny */
 #define FAN_ALLOW		0x00000001
 #define FAN_DENY		0x00000002
+#define FAN_RESETTIMER		0x00000004
 
 #ifdef __KERNEL__
 #include <linux/types.h>
author
#14 lore raw

PATCH 14/16 fanotify: send pid with fanotify notification events

In reply to Eric Paris

Often things like AV scanners may want to allow access to 'bad' files based on the process making the access help by sending the pid of the originally acting process.

---

 fs/notify/fanotify.h     |    1 +
 fs/notify/notification.c |    3 +++
 include/linux/fanotify.h |    1 +
 3 files changed, 5 insertions(+), 0 deletions(-)
fs/notify/fanotify.h +1 -0
index d65db61..74e5d4d 100644
@@ -48,6 +48,7 @@ struct fanotify_event {
 	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;
+	pid_t pid;		/* pid of the original process */
 	spinlock_t response_lock; /* protects response */
 	unsigned int response;	/* userspace answer to question */
 };
fs/notify/notification.c +3 -0
index dd0d2db..da2045a 100644
@@ -181,6 +181,8 @@ struct fanotify_event *create_event(struct file *file, unsigned int mask)
 	event->cookie = 0;
 	event->mask = mask;
 
+	event->pid = current->pid;
+
 	WARN_ON(!event->path.dentry);
 	WARN_ON(!event->path.mnt);
 
@@ -245,6 +247,7 @@ int fanotify_create_event_fd(struct fanotify_group *group, struct fanotify_event
 	data->fd = client_fd;
 	data->mask = event->mask;
 	data->cookie = event->cookie;
+	data->pid = event->pid;
 
 	fanotify_put_event(event);
 
include/linux/fanotify.h +1 -0
index a36bd6b..ab7744a 100644
@@ -71,6 +71,7 @@ struct fanotify_event_metadata {
 	int32_t fd;
 	uint32_t mask;
 	uint64_t cookie;
+	pid_t pid;
 };
 
 /* fanotify getsockopt optvals */
author
#15 lore raw

PATCH 15/16 fanotify: send tgid with notification messages

In reply to Eric Paris

Send tgid of original process to fanotify listeners

---

 fs/notify/fanotify.h     |    1 +
 fs/notify/notification.c |    2 ++
 include/linux/fanotify.h |    1 +
 3 files changed, 4 insertions(+), 0 deletions(-)
fs/notify/fanotify.h +1 -0
index 74e5d4d..b45c145 100644
@@ -49,6 +49,7 @@ struct fanotify_event {
 	/* if waiting for a userspace access answer this is the cookie they will send back */
 	unsigned long cookie;
 	pid_t pid;		/* pid of the original process */
+	pid_t tgid;		/* tgid of the original process */
 	spinlock_t response_lock; /* protects response */
 	unsigned int response;	/* userspace answer to question */
 };
fs/notify/notification.c +2 -0
index da2045a..acd13b0 100644
@@ -182,6 +182,7 @@ struct fanotify_event *create_event(struct file *file, unsigned int mask)
 	event->mask = mask;
 
 	event->pid = current->pid;
+	event->tgid = current->tgid;
 
 	WARN_ON(!event->path.dentry);
 	WARN_ON(!event->path.mnt);
@@ -248,6 +249,7 @@ int fanotify_create_event_fd(struct fanotify_group *group, struct fanotify_event
 	data->mask = event->mask;
 	data->cookie = event->cookie;
 	data->pid = event->pid;
+	data->tgid = event->tgid;
 
 	fanotify_put_event(event);
 
include/linux/fanotify.h +1 -0
index ab7744a..711efa6 100644
@@ -72,6 +72,7 @@ struct fanotify_event_metadata {
 	uint32_t mask;
 	uint64_t cookie;
 	pid_t pid;
+	pid_t tgid;
 };
 
 /* fanotify getsockopt optvals */
author
#16 lore raw

PATCH 16/16 fanotify: send file f_flags along with notifications

In reply to Eric Paris

fanotify listeners may be interested in what flags the file associated with a particular event were set. This sends f_flags along with the event metadata.

---

 fs/notify/fanotify.h     |    1 +
 fs/notify/notification.c |    2 ++
 include/linux/fanotify.h |    1 +
 3 files changed, 4 insertions(+), 0 deletions(-)
fs/notify/fanotify.h +1 -0
index b45c145..5f774bd 100644
@@ -50,6 +50,7 @@ struct fanotify_event {
 	unsigned long cookie;
 	pid_t pid;		/* pid of the original process */
 	pid_t tgid;		/* tgid of the original process */
+	unsigned int f_flags;	/* f_flags from the original file */
 	spinlock_t response_lock; /* protects response */
 	unsigned int response;	/* userspace answer to question */
 };
fs/notify/notification.c +2 -0
index acd13b0..4d1d046 100644
@@ -183,6 +183,7 @@ struct fanotify_event *create_event(struct file *file, unsigned int mask)
 
 	event->pid = current->pid;
 	event->tgid = current->tgid;
+	event->f_flags = file->f_flags;
 
 	WARN_ON(!event->path.dentry);
 	WARN_ON(!event->path.mnt);
@@ -250,6 +251,7 @@ int fanotify_create_event_fd(struct fanotify_group *group, struct fanotify_event
 	data->cookie = event->cookie;
 	data->pid = event->pid;
 	data->tgid = event->tgid;
+	data->f_flags = event->f_flags;
 
 	fanotify_put_event(event);
 
include/linux/fanotify.h +1 -0
index 711efa6..cecac79 100644
@@ -73,6 +73,7 @@ struct fanotify_event_metadata {
 	uint64_t cookie;
 	pid_t pid;
 	pid_t tgid;
+	uint32_t f_flags;
 };
 
 /* fanotify getsockopt optvals */