Skip to contentSkip to conversation

fanotify: introduce event flags FAN_EXEC and FAN_EXEC_PERM

PATCH In review Matthew Bobrowski 5 participants 15 messages Jul 16, 2018 – Jul 19, 2018

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fanotify: introduce event flags FAN_EXEC and FAN_EXEC_PERM

Currently, the fanotify API does not provide a means for user space programs to register and receive events specifically when a file has been opened with the intent to be executed. Two new event flags FAN_EXEC and FAN_EXEC_PERM have been introduced to the fanotify API along with updates to the generic filesystem notification hooks fsnotify_open and fsnotify_perm in order to support this capability.

---

The proposed changes have been tested against Linus' mainline source tree along with testing them against stable kernel releases 4.17.4, 4.17.5 and 4.17.6.

---

fs/notify/fanotify/fanotify.c +0 -0
index f90842efea13..4882706e2188 100644
@@ -197,6 +197,8 @@ static int fanotify_handle_event(struct fsnotify_group

*group,
  BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM); BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM); BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
+ BUILD_BUG_ON(FAN_EXEC != FS_EXEC);
+ BUILD_BUG_ON(FAN_EXEC_PERM != FS_EXEC_PERM);

  if (!fanotify_should_send_event(iter_info, mask, data, data_type)) return 0;

fs/notify/fsnotify.c +0 -0
index f174397b63a0..ef5d3eca2e62 100644
@@ -393,7 +393,7 @@ static __init int fsnotify_init(void)

 {
  int ret;

- BUG_ON(hweight32(ALL_FSNOTIFY_EVENTS) != 23);
+ BUG_ON(hweight32(ALL_FSNOTIFY_EVENTS) != 25);

  ret = init_srcu_struct(&fsnotify_mark_srcu); if (ret)

include/linux/fsnotify.h +0 -0
index bdaf22582f6e..db3ee74a7903 100644
@@ -42,9 +42,12 @@ static inline int fsnotify_perm(struct file *file, int

mask)
  return 0;
  if (!(mask & (MAY_READ | MAY_OPEN)))
  return 0;
- if (mask & MAY_OPEN)
+ if (mask & MAY_OPEN) {
  fsnotify_mask = FS_OPEN_PERM;
- else if (mask & MAY_READ)
+
+ if (file->f_flags & FMODE_EXEC)
+ fsnotify_mask |= FS_EXEC_PERM;
+ } else if (mask & MAY_READ)
  fsnotify_mask = FS_ACCESS_PERM;
  else
  BUG();
@@ -220,6 +223,9 @@ static inline void fsnotify_open(struct file *file) if (S_ISDIR(inode->i_mode))
  mask |= FS_ISDIR;

+ if (file->f_flags & FMODE_EXEC)
+ mask |= FS_EXEC;
+
  fsnotify_parent(path, NULL, mask);
  fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0); }

(quoted diff) +0 -0

b/include/linux/fsnotify_backend.h
index b38964a7a521..7179a82d60d4 100644

include/linux/fsnotify_backend.h +0 -0
@@ -38,6 +38,7 @@

 #define FS_DELETE 0x00000200 /* Subfile was deleted
*/
 #define FS_DELETE_SELF 0x00000400 /* Self was
deleted */
 #define FS_MOVE_SELF 0x00000800 /* Self was moved */
+#define FS_EXEC 0x00001000 /* File was
executed */

 #define FS_UNMOUNT 0x00002000 /* inode on umount fs
*/
 #define FS_Q_OVERFLOW 0x00004000 /* Event queued
overflowed */
@@ -45,6 +46,7 @@

 #define FS_OPEN_PERM 0x00010000 /* open event in an
permission hook */
 #define FS_ACCESS_PERM 0x00020000 /* access event in
a permissions hook */
+#define FS_EXEC_PERM 0x00040000 /* exec event in
permission hook */

 #define FS_EXCL_UNLINK 0x04000000 /* do not send
events if object is unlinked */
 #define FS_ISDIR 0x40000000 /* event occurred
against dir */
@@ -62,11 +64,12 @@
 #define FS_EVENTS_POSS_ON_CHILD   (FS_ACCESS | FS_MODIFY | FS_ATTRIB |\
     FS_CLOSE_WRITE | FS_CLOSE_NOWRITE | FS_OPEN |\
     FS_MOVED_FROM | FS_MOVED_TO | FS_CREATE |\
-    FS_DELETE | FS_OPEN_PERM |
FS_ACCESS_PERM)
+    FS_DELETE | FS_OPEN_PERM |
FS_ACCESS_PERM |\
+    FS_EXEC | FS_EXEC_PERM)

 #define FS_MOVE (FS_MOVED_FROM | FS_MOVED_TO)

-#define ALL_FSNOTIFY_PERM_EVENTS (FS_OPEN_PERM | FS_ACCESS_PERM) +#define ALL_FSNOTIFY_PERM_EVENTS (FS_OPEN_PERM | FS_ACCESS_PERM | FS_EXEC_PERM)

 #define ALL_FSNOTIFY_EVENTS (FS_ACCESS | FS_MODIFY | FS_ATTRIB | \
       FS_CLOSE_WRITE | FS_CLOSE_NOWRITE | FS_OPEN | \
@@ -75,7 +78,8 @@
       FS_UNMOUNT | FS_Q_OVERFLOW | FS_IN_IGNORED | \
       FS_OPEN_PERM | FS_ACCESS_PERM | FS_EXCL_UNLINK | \
       FS_ISDIR | FS_IN_ONESHOT | FS_DN_RENAME | \
-      FS_DN_MULTISHOT | FS_EVENT_ON_CHILD)
+      FS_DN_MULTISHOT | FS_EVENT_ON_CHILD |\
+      FS_EXEC | FS_EXEC_PERM)

 struct fsnotify_group;
 struct fsnotify_event;

include/uapi/linux/fanotify.h +0 -0
index 74247917de04..80822af0eeac 100644
@@ -10,11 +10,13 @@

 #define FAN_CLOSE_WRITE 0x00000008 /* Writtable file
closed */
 #define FAN_CLOSE_NOWRITE 0x00000010 /* Unwrittable file
closed */
 #define FAN_OPEN 0x00000020 /* File was opened */
+#define FAN_EXEC 0x00001000 /* File was executed */

 #define FAN_Q_OVERFLOW 0x00004000 /* Event queued
overflowed */

 #define FAN_OPEN_PERM 0x00010000 /* File open in
perm check */
 #define FAN_ACCESS_PERM 0x00020000 /* File accessed
in perm check */
+#define FAN_EXEC_PERM 0x00040000 /* File executed in
perm check */

 #define FAN_ONDIR 0x40000000 /* event occurred
against dir */

@@ -69,13 +71,15 @@
 #define FAN_ALL_EVENTS (FAN_ACCESS |\
  FAN_MODIFY |\
  FAN_CLOSE |\
- FAN_OPEN)
+ FAN_OPEN |\
+ FAN_EXEC)

 /*
  * All events which require a permission response from userspace */
 #define FAN_ALL_PERM_EVENTS (FAN_OPEN_PERM |\
-      FAN_ACCESS_PERM)
+      FAN_ACCESS_PERM |\
+      FAN_EXEC_PERM)

 #define FAN_ALL_OUTGOING_EVENTS (FAN_ALL_EVENTS |\
   FAN_ALL_PERM_EVENTS |\

#1 lore raw

Matthew Bobrowski <[email protected]>:

Quoted 6 lines

Currently, the fanotify API does not provide a means for user space programs to register and receive events specifically when a file has been opened with the intent to be executed. Two new event flags FAN_EXEC and FAN_EXEC_PERM have been introduced to the fanotify API along with updates to the generic filesystem notification hooks fsnotify_open and fsnotify_perm in order to support this capability.

Does this affect:

* executable shebang files (files starting with #!)

* interpreted files:
- bash ./xyzzy.sh
- java xyzzy.jar
- python3 xyzzy.py

Marko

maintainer
#2 lore raw
In reply to Matthew Bobrowski
Quoted 8 lines · Matthew Bobrowski

Currently, the fanotify API does not provide a means for user space programs to register and receive events specifically when a file has been opened with the intent to be executed. Two new event flags FAN_EXEC and FAN_EXEC_PERM have been introduced to the fanotify API along with updates to the generic filesystem notification hooks fsnotify_open and fsnotify_perm in order to support this capability.

I miss one important part in this changelog: Why do you need this feature? Monitoring for read would be enough after all...

Honza

Quoted 173 lines

---

The proposed changes have been tested against Linus' mainline source tree along with testing them against stable kernel releases 4.17.4, 4.17.5 and 4.17.6.

---

fs/notify/fanotify/fanotify.c +2 -0
index f90842efea13..4882706e2188 100644
@@ -197,6 +197,8 @@ static int fanotify_handle_event(struct fsnotify_group
 *group,
 �	BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
 �	BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
 �	BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
+	BUILD_BUG_ON(FAN_EXEC != FS_EXEC);
+	BUILD_BUG_ON(FAN_EXEC_PERM != FS_EXEC_PERM);

�
� if (!fanotify_should_send_event(iter_info, mask, data, data_type))
� return 0;

fs/notify/fsnotify.c +0 -0
index f174397b63a0..ef5d3eca2e62 100644
@@ -393,7 +393,7 @@ static __init int fsnotify_init(void)
 �{
 �	int ret;

�

fs/notify/fsnotify.c +1 -1
-	BUG_ON(hweight32(ALL_FSNOTIFY_EVENTS) != 23);
+	BUG_ON(hweight32(ALL_FSNOTIFY_EVENTS) != 25);

�
� ret = init_srcu_struct(&fsnotify_mark_srcu);
� if (ret)

include/linux/fsnotify.h +5 -2
index bdaf22582f6e..db3ee74a7903 100644
@@ -42,9 +42,12 @@ static inline int fsnotify_perm(struct file *file, int
 mask)
 �		return 0;
 �	if (!(mask & (MAY_READ | MAY_OPEN)))
 �		return 0;
-	if (mask & MAY_OPEN)
+	if (mask & MAY_OPEN) {
 �		fsnotify_mask = FS_OPEN_PERM;
-	else if (mask & MAY_READ)
+		
+		if (file->f_flags & FMODE_EXEC)
+			fsnotify_mask |= FS_EXEC_PERM;
+	} else if (mask & MAY_READ)
 �		fsnotify_mask = FS_ACCESS_PERM;

� else
� BUG();

include/linux/fsnotify.h +0 -0
@@ -220,6 +223,9 @@ static inline void fsnotify_open(struct file *file)
 �	if (S_ISDIR(inode->i_mode))
 �		mask |= FS_ISDIR;

�

include/linux/fsnotify.h +3 -0
+	if (file->f_flags & FMODE_EXEC)
+		mask |= FS_EXEC;
+

� fsnotify_parent(path, NULL, mask);
� fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
�}

include/linux/fsnotify_backend.h +0 -0
index b38964a7a521..7179a82d60d4 100644
@@ -38,6 +38,7 @@

�#define FS_DELETE 0x00000200 /* Subfile was deleted
*/
�#define FS_DELETE_SELF 0x00000400 /* Self was
deleted */
�#define FS_MOVE_SELF 0x00000800 /* Self was moved */
+#define FS_EXEC 0x00001000 /* File was
executed */
�
�#define FS_UNMOUNT 0x00002000 /* inode on umount fs
*/
�#define FS_Q_OVERFLOW 0x00004000 /* Event queued
overflowed */

include/linux/fsnotify_backend.h +0 -0
@@ -45,6 +46,7 @@

�
�#define FS_OPEN_PERM 0x00010000 /* open event in an
permission hook */
�#define FS_ACCESS_PERM 0x00020000 /* access event in
a permissions hook */
+#define FS_EXEC_PERM 0x00040000 /* exec event in
permission hook */
�
�#define FS_EXCL_UNLINK 0x04000000 /* do not send
events if object is unlinked */
�#define FS_ISDIR 0x40000000 /* event occurred
against dir */

include/linux/fsnotify_backend.h +0 -0
@@ -62,11 +64,12 @@
 �#define FS_EVENTS_POSS_ON_CHILD���(FS_ACCESS | FS_MODIFY | FS_ATTRIB |\

� ���FS_CLOSE_WRITE | FS_CLOSE_NOWRITE |
FS_OPEN |\
� ���FS_MOVED_FROM | FS_MOVED_TO | FS_CREATE
|\

include/linux/fsnotify_backend.h +0 -1
-				���FS_DELETE | FS_OPEN_PERM |

FS_ACCESS_PERM)

include/linux/fsnotify_backend.h +1 -0
+				���FS_DELETE | FS_OPEN_PERM |

FS_ACCESS_PERM |\

include/linux/fsnotify_backend.h +1 -0
+				���FS_EXEC | FS_EXEC_PERM)

�
�#define FS_MOVE (FS_MOVED_FROM | FS_MOVED_TO)
�

include/linux/fsnotify_backend.h +1 -1
-#define ALL_FSNOTIFY_PERM_EVENTS (FS_OPEN_PERM | FS_ACCESS_PERM)
+#define ALL_FSNOTIFY_PERM_EVENTS (FS_OPEN_PERM | FS_ACCESS_PERM |

FS_EXEC_PERM)
�
�#define ALL_FSNOTIFY_EVENTS (FS_ACCESS | FS_MODIFY | FS_ATTRIB | \
� �����FS_CLOSE_WRITE | FS_CLOSE_NOWRITE | FS_OPEN |
\

include/linux/fsnotify_backend.h +0 -0
@@ -75,7 +78,8 @@

� �����FS_UNMOUNT | FS_Q_OVERFLOW | FS_IN_IGNORED |
\
� �����FS_OPEN_PERM | FS_ACCESS_PERM |
FS_EXCL_UNLINK | \
� �����FS_ISDIR | FS_IN_ONESHOT | FS_DN_RENAME | \

include/linux/fsnotify_backend.h +2 -1
-			�����FS_DN_MULTISHOT | FS_EVENT_ON_CHILD)
+			�����FS_DN_MULTISHOT | FS_EVENT_ON_CHILD |\
+			�����FS_EXEC | FS_EXEC_PERM)

�
�struct fsnotify_group;
�struct fsnotify_event;

include/uapi/linux/fanotify.h +0 -0
index 74247917de04..80822af0eeac 100644
@@ -10,11 +10,13 @@

�#define FAN_CLOSE_WRITE 0x00000008 /* Writtable file
closed */
�#define FAN_CLOSE_NOWRITE 0x00000010 /* Unwrittable file
closed */
�#define FAN_OPEN 0x00000020 /* File was opened */
+#define FAN_EXEC 0x00001000 /* File was executed */
�
�#define FAN_Q_OVERFLOW 0x00004000 /* Event queued
overflowed */
�
�#define FAN_OPEN_PERM 0x00010000 /* File open in
perm check */
�#define FAN_ACCESS_PERM 0x00020000 /* File accessed
in perm check */
+#define FAN_EXEC_PERM 0x00040000 /* File executed in
perm check */
�
�#define FAN_ONDIR 0x40000000 /* event occurred
against dir */
�

include/uapi/linux/fanotify.h +2 -1
@@ -69,13 +71,15 @@
 �#define FAN_ALL_EVENTS (FAN_ACCESS |\
 �			FAN_MODIFY |\
 �			FAN_CLOSE |\
-			FAN_OPEN)
+			FAN_OPEN |\
+			FAN_EXEC)

�
�/*
� * All events which require a permission response from userspace � */
�#define FAN_ALL_PERM_EVENTS (FAN_OPEN_PERM |\

include/uapi/linux/fanotify.h +2 -1
-			�����FAN_ACCESS_PERM)
+			�����FAN_ACCESS_PERM |\
+			�����FAN_EXEC_PERM)

�
�#define FAN_ALL_OUTGOING_EVENTS (FAN_ALL_EVENTS |\
� �FAN_ALL_PERM_EVENTS |\

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#3 lore raw

Hello,

Quoted 8 lines · EDT Jan Kara
Quoted 7 lines · Matthew Bobrowski

Currently, the fanotify API does not provide a means for user space programs to register and receive events specifically when a file has been opened with the intent to be executed. Two new event flags FAN_EXEC and FAN_EXEC_PERM have been introduced to the fanotify API along with updates to the generic filesystem notification hooks fsnotify_open and fsnotify_perm in order to support this capability.

Quoted 4 lines
Quoted 1 line

I miss one important part in this changelog: Why do you need this feature? Monitoring for read would be enough after all...

There are several reasons that I can think of. There are lots of file accesses. It is possible to guess which one is the beginning of an execve, but you really don't know. Apps can be started in so many ways. They can be run from the runtime linker, they have LD_PRELOAD, they can have an unexpected interpreter, they can be statically linked, they can be a script which may present a new pattern of file accesses. With all the variations in how programs can start up, it would be nice to have one anchor that unambiguously says we are overlaying this pid with a whole new app and forget everything you knew about the pid. And the access pattern can be accurately watched because we always have a marker to start the sequence.

Hope this helps...

-Steve

Quoted 175 lines
Quoted 175 lines

The proposed changes have been tested against Linus' mainline source tree along with testing them against stable kernel releases 4.17.4, 4.17.5 and 4.17.6.

---

fs/notify/fanotify/fanotify.c +0 -0
index f90842efea13..4882706e2188 100644
@@ -197,6 +197,8 @@ static int fanotify_handle_event(struct

fsnotify_group
*group,

fs/notify/fanotify/fanotify.c +2 -0
 	BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
 	BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
 	BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
+	BUILD_BUG_ON(FAN_EXEC != FS_EXEC);
+	BUILD_BUG_ON(FAN_EXEC_PERM != FS_EXEC_PERM);
 
 	if (!fanotify_should_send_event(iter_info, mask, data, data_type))
 		return 0;
fs/notify/fsnotify.c +1 -1
index f174397b63a0..ef5d3eca2e62 100644
@@ -393,7 +393,7 @@ static __init int fsnotify_init(void)
 {
 	int ret;
 
-	BUG_ON(hweight32(ALL_FSNOTIFY_EVENTS) != 23);
+	BUG_ON(hweight32(ALL_FSNOTIFY_EVENTS) != 25);
 
 	ret = init_srcu_struct(&fsnotify_mark_srcu);
 	if (ret)
include/linux/fsnotify.h +5 -2
index bdaf22582f6e..db3ee74a7903 100644
@@ -42,9 +42,12 @@ static inline int fsnotify_perm(struct file *file, int
 mask)
 		return 0;
 	if (!(mask & (MAY_READ | MAY_OPEN)))
 		return 0;
-	if (mask & MAY_OPEN)
+	if (mask & MAY_OPEN) {
 		fsnotify_mask = FS_OPEN_PERM;
-	else if (mask & MAY_READ)
+
+		if (file->f_flags & FMODE_EXEC)
+			fsnotify_mask |= FS_EXEC_PERM;
+	} else if (mask & MAY_READ)
 		fsnotify_mask = FS_ACCESS_PERM;
 	else
include/linux/fsnotify.h +3 -0
 		BUG();
@@ -220,6 +223,9 @@ static inline void fsnotify_open(struct file *file)
 	if (S_ISDIR(inode->i_mode))
 		mask |= FS_ISDIR;
 
+	if (file->f_flags & FMODE_EXEC)
+		mask |= FS_EXEC;
+
 	fsnotify_parent(path, NULL, mask);
 	fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
 }
include/linux/fsnotify_backend.h +0 -0
index b38964a7a521..7179a82d60d4 100644
@@ -38,6 +38,7 @@
 #define FS_DELETE		0x00000200	/* Subfile was deleted
 */
 #define FS_DELETE_SELF		0x00000400	/* Self was

deleted */
#define FS_MOVE_SELF 0x00000800 /* Self was moved */
+#define FS_EXEC 0x00001000 /* File was
executed */

#define FS_UNMOUNT 0x00002000 /* inode on umount fs
*/
#define FS_Q_OVERFLOW 0x00004000 /* Event queued
overflowed */

include/linux/fsnotify_backend.h +0 -0
@@ -45,6 +46,7 @@
 
 #define FS_OPEN_PERM		0x00010000	/* open event in an

permission hook */
#define FS_ACCESS_PERM 0x00020000 /* access event in
a permissions hook */
+#define FS_EXEC_PERM 0x00040000 /* exec event in
permission hook */

#define FS_EXCL_UNLINK 0x04000000 /* do not send
events if object is unlinked */
#define FS_ISDIR 0x40000000 /* event occurred
against dir */

include/linux/fsnotify_backend.h +2 -1
@@ -62,11 +64,12 @@
 #define FS_EVENTS_POSS_ON_CHILD   (FS_ACCESS | FS_MODIFY | FS_ATTRIB |\
 				   FS_CLOSE_WRITE | FS_CLOSE_NOWRITE |
 FS_OPEN |\
 				   FS_MOVED_FROM | FS_MOVED_TO | FS_CREATE
 
 |\
 
-				   FS_DELETE | FS_OPEN_PERM |
 FS_ACCESS_PERM)
+				   FS_DELETE | FS_OPEN_PERM |
 FS_ACCESS_PERM |\
+				   FS_EXEC | FS_EXEC_PERM)
 

#define FS_MOVE (FS_MOVED_FROM | FS_MOVED_TO)

include/linux/fsnotify_backend.h +1 -1
-#define ALL_FSNOTIFY_PERM_EVENTS (FS_OPEN_PERM | FS_ACCESS_PERM)
+#define ALL_FSNOTIFY_PERM_EVENTS (FS_OPEN_PERM | FS_ACCESS_PERM |

FS_EXEC_PERM)

#define ALL_FSNOTIFY_EVENTS (FS_ACCESS | FS_MODIFY | FS_ATTRIB | \
FS_CLOSE_WRITE | FS_CLOSE_NOWRITE | FS_OPEN |
\

include/linux/fsnotify_backend.h +2 -1
@@ -75,7 +78,8 @@
 			     FS_UNMOUNT | FS_Q_OVERFLOW | FS_IN_IGNORED |
 \
 			     FS_OPEN_PERM | FS_ACCESS_PERM |
 FS_EXCL_UNLINK | \
 			     FS_ISDIR | FS_IN_ONESHOT | FS_DN_RENAME | \
-			     FS_DN_MULTISHOT | FS_EVENT_ON_CHILD)
+			     FS_DN_MULTISHOT | FS_EVENT_ON_CHILD |\
+			     FS_EXEC | FS_EXEC_PERM)
 

struct fsnotify_group;
struct fsnotify_event;

include/uapi/linux/fanotify.h +0 -0
index 74247917de04..80822af0eeac 100644
@@ -10,11 +10,13 @@
 #define FAN_CLOSE_WRITE		0x00000008	/* Writtable file

closed */
#define FAN_CLOSE_NOWRITE 0x00000010 /* Unwrittable file
closed */
#define FAN_OPEN 0x00000020 /* File was opened */
+#define FAN_EXEC 0x00001000 /* File was executed */

#define FAN_Q_OVERFLOW 0x00004000 /* Event queued
overflowed */

#define FAN_OPEN_PERM 0x00010000 /* File open in
perm check */
#define FAN_ACCESS_PERM 0x00020000 /* File accessed
in perm check */
+#define FAN_EXEC_PERM 0x00040000 /* File executed in
perm check */

#define FAN_ONDIR 0x40000000 /* event occurred
against dir */

include/uapi/linux/fanotify.h +4 -2
@@ -69,13 +71,15 @@
 #define FAN_ALL_EVENTS (FAN_ACCESS |\
 			FAN_MODIFY |\
 			FAN_CLOSE |\
-			FAN_OPEN)
+			FAN_OPEN |\
+			FAN_EXEC)
 
 /*
  * All events which require a permission response from userspace
  */
 #define FAN_ALL_PERM_EVENTS (FAN_OPEN_PERM |\
-			     FAN_ACCESS_PERM)
+			     FAN_ACCESS_PERM |\
+			     FAN_EXEC_PERM)
 
 #define FAN_ALL_OUTGOING_EVENTS	(FAN_ALL_EVENTS |\
 				 FAN_ALL_PERM_EVENTS |\
maintainer
#4 lore raw
Quoted 16 lines · Steve Grubb

Hello,

Quoted 8 lines · EDT Jan Kara
Quoted 7 lines · Matthew Bobrowski

Currently, the fanotify API does not provide a means for user space programs to register and receive events specifically when a file has been opened with the intent to be executed. Two new event flags FAN_EXEC and FAN_EXEC_PERM have been introduced to the fanotify API along with updates to the generic filesystem notification hooks fsnotify_open and fsnotify_perm in order to support this capability.

Quoted 4 lines
Quoted 1 line

I miss one important part in this changelog: Why do you need this feature? Monitoring for read would be enough after all...

Quoted 10 lines

There are several reasons that I can think of. There are lots of file accesses. It is possible to guess which one is the beginning of an execve, but you really don't know. Apps can be started in so many ways. They can be run from the runtime linker, they have LD_PRELOAD, they can have an unexpected interpreter, they can be statically linked, they can be a script which may present a new pattern of file accesses. With all the variations in how programs can start up, it would be nice to have one anchor that unambiguously says we are overlaying this pid with a whole new app and forget everything you knew about the pid. And the access pattern can be accurately watched because we always have a marker to start the sequence.

I don't quite buy your "marker that pid is starting from scratch" argument. Firstly, you'd have to place fanotify watches on all executables in your system to even have a chance to tell that, secondly, the process does not quite start a new - it still inherits some stuff from the old process like open file descriptors... So I understand exec might be interesting for audit purposes but then you should watch it using audit and not fanotify which is for handling / mediating filesystem accesses.

And when you are looking at filesystem accesses, then there's no real difference between exec and read which is exactly why I'm not sure why the new feature is being added.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#5 lore raw
Quoted 33 lines · EDT Jan Kara
Quoted 20 lines · Steve Grubb

Hello,

Quoted 11 lines · EDT Jan Kara
Quoted 10 lines · Matthew Bobrowski

Currently, the fanotify API does not provide a means for user space programs to register and receive events specifically when a file has been
opened with the intent to be executed. Two new event flags FAN_EXEC and
FAN_EXEC_PERM have been introduced to the fanotify API along with updates
to the generic filesystem notification hooks fsnotify_open and fsnotify_perm in order to support this capability.

Quoted 5 lines
Quoted 1 line

I miss one important part in this changelog: Why do you need this feature?
Monitoring for read would be enough after all...

Quoted 11 lines

There are several reasons that I can think of. There are lots of file accesses. It is possible to guess which one is the beginning of an execve, but you really don't know. Apps can be started in so many ways. They can be run from the runtime linker, they have LD_PRELOAD, they can have an unexpected interpreter, they can be statically linked, they can be a script which may present a new pattern of file accesses. With all the variations in how programs can start up, it would be nice to have one anchor that unambiguously says we are overlaying this pid with a whole new app and forget everything you knew about the pid. And the access pattern can be accurately watched because we always have a marker to start the sequence.

Quoted 3 lines

I don't quite buy your "marker that pid is starting from scratch" argument. Firstly, you'd have to place fanotify watches on all executables in your system to even have a chance to tell that,

True and we do.

Quoted 2 lines

secondly, the process does not quite start a new - it still inherits some stuff from the old process like open file descriptors...

True. But that is not exactly relevant to the issues we're looking at.

Quoted 3 lines

So I understand exec might be interesting for audit purposes but then you should watch it using audit and not fanotify which is for handling / mediating filesystem accesses.

This is not being used for the audit system. But let me illustrate the issue that I'm looking at. I have the following sequence of events:

pid=13247 exe=/usr/bin/bash file=/home/sgrubb/test/static/test pid=13250 exe=/usr/bin/bash file=/usr/bin/sed pid=13250 exe=/usr/bin/bash file=/usr/lib64/ld-2.27.so pid=13250 exe=/usr/bin/sed file=/etc/ld.so.cache pid=13250 exe=/usr/bin/sed file=/usr/lib64/libacl.so.1.1.2253 pid=13250 exe=/usr/bin/sed file=/usr/lib64/libselinux.so.1 pid=13250 exe=/usr/bin/sed file=/usr/lib64/libc-2.27.so pid=13250 exe=/usr/bin/sed file=/usr/lib64/libattr.so.1.1.2448 pid=13250 exe=/usr/bin/sed file=/usr/lib64/libpcre2-8.so.0.7.0 pid=13250 exe=/usr/bin/sed file=/usr/lib64/libdl-2.27.so pid=13250 exe=/usr/bin/sed file=/usr/lib64/libpthread-2.27.so pid=13250 exe=/usr/bin/sed file=/usr/lib/locale/locale-archive pid=13259 exe=/usr/bin/bash file=/home/sgrubb/test/static/wc pid=13259 exe=/usr/bin/bash file=/home/sgrubb/test/static/test2

There is a clear pattern for sed. But what was bash doing to test? There is no pattern. Was it an execution or echo "blah" > test? How can you tell? Then what happened to test2? (The first was execution of static app, last was piping the program to wc -c. They pretty much leave the same footprint but what is happening is very different.)

Quoted 3 lines

And when you are looking at filesystem accesses, then there's no real difference between exec and read which is exactly why I'm not sure why the new feature is being added.

Its about intention to do something different with the data after the read. That intention is important and just out of reach. But I otherwise agree that read and execute do pretty much the same thing.

-Steve

maintainer
#6 lore raw
Quoted 38 lines · Steve Grubb
Quoted 33 lines · EDT Jan Kara
Quoted 20 lines · Steve Grubb

Hello,

Quoted 11 lines · EDT Jan Kara
Quoted 10 lines · Matthew Bobrowski

Currently, the fanotify API does not provide a means for user space programs to register and receive events specifically when a file has been
opened with the intent to be executed. Two new event flags FAN_EXEC and
FAN_EXEC_PERM have been introduced to the fanotify API along with updates
to the generic filesystem notification hooks fsnotify_open and fsnotify_perm in order to support this capability.

Quoted 5 lines
Quoted 1 line

I miss one important part in this changelog: Why do you need this feature?
Monitoring for read would be enough after all...

Quoted 11 lines

There are several reasons that I can think of. There are lots of file accesses. It is possible to guess which one is the beginning of an execve, but you really don't know. Apps can be started in so many ways. They can be run from the runtime linker, they have LD_PRELOAD, they can have an unexpected interpreter, they can be statically linked, they can be a script which may present a new pattern of file accesses. With all the variations in how programs can start up, it would be nice to have one anchor that unambiguously says we are overlaying this pid with a whole new app and forget everything you knew about the pid. And the access pattern can be accurately watched because we always have a marker to start the sequence.

Quoted 3 lines

I don't quite buy your "marker that pid is starting from scratch" argument. Firstly, you'd have to place fanotify watches on all executables in your system to even have a chance to tell that,

Quoted 6 lines

True and we do.

Quoted 2 lines

secondly, the process does not quite start a new - it still inherits some stuff from the old process like open file descriptors...

True. But that is not exactly relevant to the issues we're looking at.

OK.

Quoted 8 lines
Quoted 3 lines

So I understand exec might be interesting for audit purposes but then you should watch it using audit and not fanotify which is for handling / mediating filesystem accesses.

This is not being used for the audit system. But let me illustrate the issue that I'm looking at. I have the following sequence of events:

Quoted 14 lines

pid=13247 exe=/usr/bin/bash file=/home/sgrubb/test/static/test pid=13250 exe=/usr/bin/bash file=/usr/bin/sed pid=13250 exe=/usr/bin/bash file=/usr/lib64/ld-2.27.so pid=13250 exe=/usr/bin/sed file=/etc/ld.so.cache pid=13250 exe=/usr/bin/sed file=/usr/lib64/libacl.so.1.1.2253 pid=13250 exe=/usr/bin/sed file=/usr/lib64/libselinux.so.1 pid=13250 exe=/usr/bin/sed file=/usr/lib64/libc-2.27.so pid=13250 exe=/usr/bin/sed file=/usr/lib64/libattr.so.1.1.2448 pid=13250 exe=/usr/bin/sed file=/usr/lib64/libpcre2-8.so.0.7.0 pid=13250 exe=/usr/bin/sed file=/usr/lib64/libdl-2.27.so pid=13250 exe=/usr/bin/sed file=/usr/lib64/libpthread-2.27.so pid=13250 exe=/usr/bin/sed file=/usr/lib/locale/locale-archive pid=13259 exe=/usr/bin/bash file=/home/sgrubb/test/static/wc pid=13259 exe=/usr/bin/bash file=/home/sgrubb/test/static/test2

These are file accesses I suppose.

Quoted 5 lines

There is a clear pattern for sed. But what was bash doing to test? There is no pattern. Was it an execution or echo "blah" > test? How can you tell? Then what happened to test2? (The first was execution of static app, last was piping the program to wc -c. They pretty much leave the same footprint but what is happening is very different.)

Yes, but my point is you just cannot tell what a process is going to do with the file. E.g. if you run a script as ". my_script.sh", it will get executed for all practical purposes but there's no "open for execution" happening - the file is just read as any other and then interpretted. No way to discern this from "<my_script.sh" from kernel POV. And similar thing happens for dynamic libraries which is also executable code.

And if you want to look specifically at "what's getting used for exec(2)?", then just trace exec(2) system call (either using audit or using syscall trace points) and you get all the details including file name.

Quoted 7 lines
Quoted 3 lines

And when you are looking at filesystem accesses, then there's no real difference between exec and read which is exactly why I'm not sure why the new feature is being added.

Its about intention to do something different with the data after the read. That intention is important and just out of reach. But I otherwise agree that read and execute do pretty much the same thing.

Yes, and my point is the intention is sometimes communicated and sometimes not. And I don't want to add an API which is vaguely defined...

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#7 lore raw

Hello Jan,

Quoted 26 lines · EDT Jan Kara
Quoted 17 lines

This is not being used for the audit system. But let me illustrate the issue that I'm looking at. I have the following sequence of events:

pid=13247 exe=/usr/bin/bash file=/home/sgrubb/test/static/test pid=13250 exe=/usr/bin/bash file=/usr/bin/sed pid=13250 exe=/usr/bin/bash file=/usr/lib64/ld-2.27.so pid=13250 exe=/usr/bin/sed file=/etc/ld.so.cache pid=13250 exe=/usr/bin/sed file=/usr/lib64/libacl.so.1.1.2253 pid=13250 exe=/usr/bin/sed file=/usr/lib64/libselinux.so.1 pid=13250 exe=/usr/bin/sed file=/usr/lib64/libc-2.27.so pid=13250 exe=/usr/bin/sed file=/usr/lib64/libattr.so.1.1.2448 pid=13250 exe=/usr/bin/sed file=/usr/lib64/libpcre2-8.so.0.7.0 pid=13250 exe=/usr/bin/sed file=/usr/lib64/libdl-2.27.so pid=13250 exe=/usr/bin/sed file=/usr/lib64/libpthread-2.27.so pid=13250 exe=/usr/bin/sed file=/usr/lib/locale/locale-archive pid=13259 exe=/usr/bin/bash file=/home/sgrubb/test/static/wc pid=13259 exe=/usr/bin/bash file=/home/sgrubb/test/static/test2

These are file accesses I suppose.

Quoted 5 lines

There is a clear pattern for sed. But what was bash doing to test? There is no pattern. Was it an execution or echo "blah" > test? How can you tell? Then what happened to test2? (The first was execution of static app, last was piping the program to wc -c. They pretty much leave the same footprint but what is happening is very different.)

Quoted 3 lines

Yes, but my point is you just cannot tell what a process is going to do with the file. E.g. if you run a script as ". my_script.sh", it will get executed for all practical purposes but there's no "open for execution"

That is true. But a shell script eventually causes a real execution of something. The main problem is when it comes to statically linked programs. You get one view of the program and its gone. Unlike dynamically linked programs where there is a chain of things that happen. But going back to the script example, the binary interpretting the script has not changed so we can continue to maintain information about that pid.

Quoted 3 lines

happening - the file is just read as any other and then interpretted. No way to discern this from "<my_script.sh" from kernel POV. And similar thing happens for dynamic libraries which is also executable code.

Any special handling of shared objects is not needed. The important thing is twofold. The first knowing that any information about that pid is now being overlaid and therefore we have to start over in determining trust. But also if the execution is a statically linked program, we absolutely need to know the intent. It is impossible to tell a benign read or write from an actual execution given what we know via the fanotify interface.

Quoted 3 lines

And if you want to look specifically at "what's getting used for exec(2)?", then just trace exec(2) system call (either using audit or using syscall trace points) and you get all the details including file name.

Wouldn't that hurt overall system performance?

Quoted 10 lines
Quoted 7 lines
Quoted 3 lines

And when you are looking at filesystem accesses, then there's no real difference between exec and read which is exactly why I'm not sure why the new feature is being added.

Its about intention to do something different with the data after the read. That intention is important and just out of reach. But I otherwise agree that read and execute do pretty much the same thing.

Yes, and my point is the intention is sometimes communicated and sometimes not. And I don't want to add an API which is vaguely defined...

To me, its pretty crisp. For everything except statically linked programs, there are patterns of access that tell you what is going on. But sometimes you need that marker to know this is the beginning of a sequence and start over and build up new information. And in the case of statically linked program, there is no pattern of access. It's one shot and its over. It's very important to identify the intent for this item.

And I'd say, impossible to make decisions that allow certain system activity like creation of a file or packaging of a file vs stopping the execution of something unknown. More information is needed so that the system is tolerant of user activity and not a PITA because it has to be super paranoid. For example, system update. All these files are unknown but they are being written and not executed. This activity is OK. But once the update is complete, the extra information let's us know when to start paying attention again.

Cheers,
-Steve

#8 lore raw
In reply to Jan Kara

Hello Jan,

Quoted 2 lines · EDT Jan Kara

Yes, and my point is the intention is sometimes communicated and sometimes not. And I don't want to add an API which is vaguely defined...

Maybe this is better illustrated with actual data. For an application whitelisting daemon to be usable, it has to cache information and look that up rather than make expensive syscalls in order to make a decision. Every syscall it makes is delaying system access.

So, how can we tell when its time to evict cached information and start over? We are given 2 pieces of information in a fanotify event. The fd and the pid. What can be done to find an execve is to stat("/proc/pid") and look at the inode create time. Suppose we have this event stream:

pid=13250 exe=/usr/bin/bash file=/usr/bin/sed <- execve occurs here pid=13250 exe=/usr/bin/bash file=/usr/lib64/ld-2.27.so pid=13250 exe=/usr/bin/sed file=/etc/ld.so.cache <- /proc/pid updated here pid=13250 exe=/usr/bin/sed file=/usr/lib64/libacl.so.1.1.2253 pid=13250 exe=/usr/bin/sed file=/usr/lib64/libselinux.so.1 pid=13250 exe=/usr/bin/sed file=/usr/lib64/libc-2.27.so pid=13250 exe=/usr/bin/sed file=/usr/lib64/libattr.so.1.1.2448 pid=13250 exe=/usr/bin/sed file=/usr/lib64/libpcre2-8.so.0.7.0 pid=13250 exe=/usr/bin/sed file=/usr/lib64/libdl-2.27.so pid=13250 exe=/usr/bin/sed file=/usr/lib64/libpthread-2.27.so pid=13250 exe=/usr/bin/sed file=/usr/lib/locale/locale-archive

In this example, we can determine that execve occurs, but we are 2 accesses late in knowing this. We can keep a ring buffer to have this history for pattern analysis but its not ideal. Contrast that with a statically linked program:

pid=13247 exe=/usr/bin/bash file=/home/sgrubb/static/test <- execeve here ..

We cannot tell that an execution occurs because by the time /proc/pid updates its creation time, execution has already happened and we don't see that executable again until it accesses a file and then we can notice its a new program. By then its too late.

I suppose we could use tracing as you suggested. But then we need to correlate the 2 independent event streams in addition possibly adding additional system overhead. And considering there is an event queue inside the daemon, finding the right fanotify event in the queue to add this metadata to will add complexity and may be prone to error due to racing. A simpler solution is to just tag the access as originating from execve and it's automatically correlated with file access and the cost is perhaps a couple if statements.

Hope this helps...

-Steve

#9 lore raw
In reply to Jan Kara
Quoted 15 lines · Jan Kara
Quoted 14 lines · Steve Grubb

Hello,

Quoted 11 lines · EDT Jan Kara
Quoted 10 lines · Matthew Bobrowski

Currently, the fanotify API does not provide a means for user space programs to register and receive events specifically when a file has been
opened with the intent to be executed. Two new event flags FAN_EXEC and
FAN_EXEC_PERM have been introduced to the fanotify API along with updates
to the generic filesystem notification hooks fsnotify_open and fsnotify_perm in order to support this capability.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 1 line

I miss one important part in this changelog: Why do you need this feature?
Monitoring for read would be enough after all...

We're in the midst of writing an application whitelisting program whereby it's predominantly concerned about file accesses that are a result of an execution attempt. Without going into semantics, the execution and overall access control is enforced by an overarching policy that either permits or denies a file from being executed.

The current implementation makes use of the FAN_PERM_OPEN event flag in order to determine whether a file within a monitored mark point has been potentially opened for execution. Although this does somewhat provide the functionality that we're looking for, it's far from ideal and I personally think we can do a lot better. If in a position where you're monitoring an entire filesystem using the FAN_OPEN_PERM event flag you can only imagine the number of events that applications of this class need to process unnecessarily due to the significant number of files being opened system wide. Having the additional FAN_EXEC* event flags will allow user space applications of such type to explicitly receive and process events that are
in fact a result of file being opened and read with the intent on being
executed.

Irrespective of our particular use case, I believe that such flags will be something that other consumers of this API may also find relatively useful.

Quoted 27 lines
Quoted 8 lines

There are several reasons that I can think of. There are lots of file accesses. It is possible to guess which one is the beginning of an execve,
but you really don't know. Apps can be started in so many ways. They can be
run from the runtime linker, they have LD_PRELOAD, they can have an unexpected interpreter, they can be statically linked, they can be a script

Quoted 8 lines

which may present a new pattern of file accesses. With all the variations in
how programs can start up, it would be nice to have one anchor that unambiguously says we are overlaying this pid with a whole new app and forget
everything you knew about the pid. And the access pattern can be accurately
watched because we always have a marker to start the sequence.

I don't quite buy your "marker that pid is starting from scratch" argument.
Firstly, you'd have to place fanotify watches on all executables in your system to even have a chance to tell that, secondly, the process does not quite start a new - it still inherits some stuff from the old process like
open file descriptors... So I understand exec might be interesting for audit purposes but then you should watch it using audit and not fanotify which is for handling / mediating filesystem accesses.

Quoted 4 lines

And when you are looking at filesystem accesses, then there's no real difference between exec and read which is exactly why I'm not sure why the
new feature is being added.

Although I do completely agree with the fact that there's no real difference between an exec and read, I do however believe that in the context of filesystem accesses it's important to distinguish between the type access i.e. the intent, right? Let me ask you this. Why is that we have additional flags such as FAN_ACCESS and FAN_CLOSE_WRITE for example available within the API? Wouldn't FAN_OPEN be sufficient in terms of understanding whether some type of other access operation is being performed on the file object that has been marked for monitoring? Sure, maybe it would, but it's far from being specific and the entire objective of all the additional flags is to presumably provide a means for a program to receive filesystem access events that are a result of a specific intent or action being performed on the object. Take the following for example:

FAN_ACCESS:

The events that come through for this particular flag are a result of a file having its contents read. Before any contents can be read from it however, the actual file needs to be opened.

FAN_EXEC:

The events that come through for this particular flag are a result of a file being executed. Prior to the file being executed though it needs to be opened, read, and mapped into memory accordingly.

What's the difference? Both perform an open, so wouldn't that just be enough to tell some type of access is undertaken? Well, no. It's the intent that needs highlighting here.

You may sit there and claim that part of a file being executed is to have it opened and read, so why not just use either FAN_ACCESS or FAN_OPEN, right? Well sure, but both operations open and read are in fact implicit actions as a result of an actual explicit intent of the file being executed. Now, how is FAN_ACCESS and any of the other event flags different from the new flags that I've proposed here? After all, isn't exec just not another type of file access despite also potentially having other file actions embedded within it?

maintainer
#10 lore raw
Quoted 22 lines · Matthew Bobrowski
Quoted 15 lines · Jan Kara
Quoted 14 lines · Steve Grubb

Hello,

Quoted 11 lines · EDT Jan Kara
Quoted 10 lines · Matthew Bobrowski

Currently, the fanotify API does not provide a means for user space programs to register and receive events specifically when a file has been
opened with the intent to be executed. Two new event flags FAN_EXEC and
FAN_EXEC_PERM have been introduced to the fanotify API along with updates
to the generic filesystem notification hooks fsnotify_open and fsnotify_perm in order to support this capability.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 1 line

I miss one important part in this changelog: Why do you need this feature?
Monitoring for read would be enough after all...

Quoted 5 lines

We're in the midst of writing an application whitelisting program whereby it's predominantly concerned about file accesses that are a result of an execution attempt. Without going into semantics, the execution and overall access control is enforced by an overarching policy that either permits or denies a file from being executed.

OK, and I guess my point is that fanotify is not, in my opinion, a suitable API for this. What you describe above is exactly what LSMs (Linux Security Modules) are designed to be used for (but admittedly I'm no expert on these). So you can use e.g. Apparmor to whitelist executables, that can be executed by your contained program. Or similarly you can use SELinux, tag executables appropriately and only properly tagged executables will be able to be executed by your contained program. There's no need to reinvent the functionality in fanotify + your framework...

Quoted 13 lines

The current implementation makes use of the FAN_PERM_OPEN event flag in order to determine whether a file within a monitored mark point has been potentially opened for execution. Although this does somewhat provide the functionality that we're looking for, it's far from ideal and I personally think we can do a lot better. If in a position where you're monitoring an entire filesystem using the FAN_OPEN_PERM event flag you can only imagine the number of events that applications of this class need to process unnecessarily due to the significant number of files being opened system wide. Having the additional FAN_EXEC* event flags will allow user space applications of such type to explicitly receive and process events that are
in fact a result of file being opened and read with the intent on being
executed.

Quoted 2 lines

Irrespective of our particular use case, I believe that such flags will be something that other consumers of this API may also find relatively useful.

Another problem I have with the flag is it is not reliable - e.g. shared libraries get executed as well but are not open with FMODE_EXEC. Shell scripts executed as ". script.sh" as well. So the semantics would be weakly defined in my opinion.

Quoted 61 lines
Quoted 27 lines
Quoted 8 lines

There are several reasons that I can think of. There are lots of file� accesses. It is possible to guess which one is the beginning of an execve,�
but you really don't know. Apps can be started in so many ways. They can be�
run from the runtime linker, they have LD_PRELOAD, they can have an� unexpected interpreter, they can be statically linked, they can be a script�

Quoted 8 lines

which may present a new pattern of file accesses. With all the variations in�
how programs can start up, it would be nice to have one anchor that� unambiguously says we are overlaying this pid with a whole new app and forget�
everything you knew about the pid. And the access pattern can be accurately�
watched because we always have a marker to start the sequence.

I don't quite buy your "marker that pid is starting from scratch" argument.
Firstly, you'd have to place fanotify watches on all executables in your system to even have a chance to tell that, secondly, the process does not quite start a new - it still inherits some stuff from the old process like
open file descriptors... So I understand exec might be interesting for audit purposes but then you should watch it using audit and not fanotify which is for handling / mediating filesystem accesses.

Quoted 4 lines

And when you are looking at filesystem accesses, then there's no real difference between exec and read which is exactly why I'm not sure why the
new feature is being added.

Although I do completely agree with the fact that there's no real difference between an exec and read, I do however believe that in the context of filesystem accesses it's important to distinguish between the type access i.e. the intent, right? Let me ask you this. Why is that we have additional flags such as FAN_ACCESS and FAN_CLOSE_WRITE for example available within the API? Wouldn't FAN_OPEN be sufficient in terms of understanding whether some type of other access operation is being performed on the file object that has been marked for monitoring? Sure, maybe it would, but it's far from being specific and the entire objective of all the additional flags is to presumably provide a means for a program to receive filesystem access events that are a result of a specific intent or action being performed on the object. Take the following for example:

FAN_ACCESS:

The events that come through for this particular flag are a result of a file having its contents read. Before any contents can be read from it however, the actual file needs to be opened.

FAN_EXEC:

The events that come through for this particular flag are a result of a file being executed. Prior to the file being executed though it needs to be opened, read, and mapped into memory accordingly.

What's the difference? Both perform an open, so wouldn't that just be enough to tell some type of access is undertaken? Well, no. It's the intent that needs highlighting here.

Quoted 8 lines

You may sit there and claim that part of a file being executed is to have it opened and read, so why not just use either FAN_ACCESS or FAN_OPEN, right? Well sure, but both operations open and read are in fact implicit actions as a result of an actual explicit intent of the file being executed. Now, how is FAN_ACCESS and any of the other event flags different from the new flags that I've proposed here? After all, isn't exec just not another type of file access despite also potentially having other file actions embedded within it?

So fanotify is a filesystem event notification API. For filesystem, open and read are fundamentally different events and as such we have different FAN_OPEN and FAN_ACCESS events in the API. The only disputable events we have in the API are FAN_CLOSE_WRITE vs FAN_CLOSE_NOWRITE - from fs POV there's no big difference. But at least this is 100% reliably (unlike FMODE_EXEC) telling you whether the user was able to modify the file or not and it caters to one of the use cases this API has been created for - virus scanners, file caching daemons, ... - i.e., triggering specific actions based on file contents.

What you are implementing seems to me more like access mediation based on process (or its capabilities) which is really more a task for LSM.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#11 lore raw

Jan Kara <[email protected]>:

Quoted 10 lines

So fanotify is a filesystem event notification API. For filesystem, open and read are fundamentally different events and as such we have different FAN_OPEN and FAN_ACCESS events in the API. The only disputable events we have in the API are FAN_CLOSE_WRITE vs FAN_CLOSE_NOWRITE - from fs POV there's no big difference. But at least this is 100% reliably (unlike FMODE_EXEC) telling you whether the user was able to modify the file or not and it caters to one of the use cases this API has been created for - virus scanners, file caching daemons, ... - i.e., triggering specific actions based on file contents.

As a side note from the virus scanner point of view, FAN_CLOSE_WRITE_PERM would be really useful because it would prevent the hit-and-run corruption of a file. As it stands, fanotify communicates the pid of the culprit but the process is long gone by the time you get to analyze it...

Marko

#12 lore raw
In reply to Jan Kara
Quoted 9 lines · EDT Jan Kara

So fanotify is a filesystem event notification API. For filesystem, open and read are fundamentally different events and as such we have different FAN_OPEN and FAN_ACCESS events in the API. The only disputable events we have in the API are FAN_CLOSE_WRITE vs FAN_CLOSE_NOWRITE - from fs POV there's no big difference. But at least this is 100% reliably (unlike FMODE_EXEC) telling you whether the user was able to modify the file or not and it caters to one of the use cases this API has been created for - virus scanners, file caching daemons, ... - i.e., triggering specific actions based on file contents.

Would it be more acceptable to not add FAN_EXEC_PERM on the front end where you ask for it at fanotify_mark. But rather add only FAN_EXEC? This would reduce the proposed API and just turn it into additional metadata about events that are already being requested. This ways you can do something like:

mask = FAN_OPEN_PERM | FAN_EXEC;

and then pass that to fanotify_mark. It would not affect old programs because they simply wouldn't ask for the bit. Would this be more palatable?

Best Regards,
-Steve

#13 lore raw
In reply to Matthew Bobrowski
Quoted 11 lines · Matthew Bobrowski

Currently, the fanotify API does not provide a means for user space programs to register and receive events specifically when a file has been opened with the intent to be executed. Two new event flags FAN_EXEC and FAN_EXEC_PERM have been introduced to the fanotify API along with updates to the generic filesystem notification hooks fsnotify_open and fsnotify_perm in order to support this capability.

---

[...]

Quoted 6 lines
(quoted diff) +1 -1
@@ -69,13 +71,15 @@
 #define FAN_ALL_EVENTS (FAN_ACCESS |\
                        FAN_MODIFY |\
                        FAN_CLOSE |\
-                       FAN_OPEN)
+                       FAN_OPEN |\

Review line 83

Quoted 10 lines
(quoted diff) +3 -1
+                       FAN_EXEC)
 
 /*
  * All events which require a permission response from userspace
  */
 #define FAN_ALL_PERM_EVENTS (FAN_OPEN_PERM |\
-                            FAN_ACCESS_PERM)
+                            FAN_ACCESS_PERM |\
+                            FAN_EXEC_PERM)
 

If we change these masks that are exposed to user and there is a user program setting a mark with FAN_ALL_EVENTS, recompiling that program with new headers will make the binary incompatible with old kernels.

Jan,

Do you think that is a problem?

Thanks,
Amir.

maintainer
#14 lore raw
Quoted 36 lines · Amir Goldstein
Quoted 11 lines · Matthew Bobrowski

Currently, the fanotify API does not provide a means for user space programs to register and receive events specifically when a file has been opened with the intent to be executed. Two new event flags FAN_EXEC and FAN_EXEC_PERM have been introduced to the fanotify API along with updates to the generic filesystem notification hooks fsnotify_open and fsnotify_perm in order to support this capability.

---

[...]

Quoted 6 lines
(quoted diff) +1 -1
@@ -69,13 +71,15 @@
 #define FAN_ALL_EVENTS (FAN_ACCESS |\
                        FAN_MODIFY |\
                        FAN_CLOSE |\
-                       FAN_OPEN)
+                       FAN_OPEN |\

Review line 83

Quoted 10 lines
(quoted diff) +3 -1
+                       FAN_EXEC)
 
 /*
  * All events which require a permission response from userspace
  */
 #define FAN_ALL_PERM_EVENTS (FAN_OPEN_PERM |\
-                            FAN_ACCESS_PERM)
+                            FAN_ACCESS_PERM |\
+                            FAN_EXEC_PERM)
 

If we change these masks that are exposed to user and there is a user program setting a mark with FAN_ALL_EVENTS, recompiling that program with new headers will make the binary incompatible with old kernels.

Quoted 3 lines

Jan,

Do you think that is a problem?

Hum, good point. Honestly, I think it has been a mistake to export FAN_ALL_EVENTS and FAN_ALL_PERM_EVENTS to userspace. Now either the name is going to be misleading or there's a risk of breaking existing apps as you suggest. But let's decide that once I'm convinced this feature is actually worth it.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR