Skip to contentSkip to conversation

Test support for new fanotify FAN_REPORT_PIDFD feature

PATCH Superseded Matthew Bobrowski via ltp 3 participants 9 messages Oct 27, 2021 – Nov 1, 2021

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Test support for new fanotify FAN_REPORT_PIDFD feature

Hi,

This patch series introduces LTP tests for the new fanotify FAN_REPORT_PIDFD feature which has been merged as part of the v5.15 release.

Matthew Bobrowski (2):
syscalls/fanotify20: add new test for FAN_REPORT_PIDFD syscalls/fanotify21: add new test checking the returned pidfd from
fanotify in FAN_REPORT_PIDFD mode

 configure.ac                                  |   2 +-
 testcases/kernel/syscalls/fanotify/.gitignore |   2 +
 testcases/kernel/syscalls/fanotify/fanotify.h |  21 +
 .../kernel/syscalls/fanotify/fanotify20.c     | 133 +++++++
 .../kernel/syscalls/fanotify/fanotify21.c     | 363 ++++++++++++++++++
 5 files changed, 520 insertions(+), 1 deletion(-)
 create mode 100644 testcases/kernel/syscalls/fanotify/fanotify20.c
 create mode 100644 testcases/kernel/syscalls/fanotify/fanotify21.c
Signature · 6 lines
2.33.0.1079.g6e70778dc9-goog

/M

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH 1/2 syscalls/fanotify20: add new test for FAN_REPORT_PIDFD

This test ensures that the fanotify API returns the expected error status code -EINVAL when an invalid flag is supplied alongside the new FAN_REPORT_PIDFD initialization flag. Currently, FAN_REPORT_TID is the only initialization flag that is not permitted in conjunction with FAN_REPORT_PIDFD, so we explicitly provide test coverage for this.

We also add an extra trivial test case to ensure that the initialization behavior with the other FAN_REPORT_* related flags is working as intended.

---
 configure.ac                                  |   2 +-
 testcases/kernel/syscalls/fanotify/.gitignore |   1 +
 testcases/kernel/syscalls/fanotify/fanotify.h |  21 +++
 .../kernel/syscalls/fanotify/fanotify20.c     | 133 ++++++++++++++++++
 4 files changed, 156 insertions(+), 1 deletion(-)
 create mode 100644 testcases/kernel/syscalls/fanotify/fanotify20.c
configure.ac +1 -1
index 5bf3c52ec..b62ec5e15 100644
@@ -159,7 +159,7 @@ AC_CHECK_MEMBERS([struct utsname.domainname],,,[
 AC_CHECK_TYPES([enum kcmp_type],,,[#include <linux/kcmp.h>])
 AC_CHECK_TYPES([struct acct_v3],,,[#include <sys/acct.h>])
 AC_CHECK_TYPES([struct af_alg_iv, struct sockaddr_alg],,,[# include <linux/if_alg.h>])
-AC_CHECK_TYPES([struct fanotify_event_info_fid, struct fanotify_event_info_header],,,[#include <sys/fanotify.h>])
+AC_CHECK_TYPES([struct fanotify_event_info_fid, struct fanotify_event_info_header, struct fanotify_event_info_pidfd],,,[#include <sys/fanotify.h>])
 AC_CHECK_TYPES([struct file_dedupe_range],,,[#include <linux/fs.h>])
 
 AC_CHECK_TYPES([struct file_handle],,,[
testcases/kernel/syscalls/fanotify/.gitignore +1 -0
index 9554b16b1..c99e6fff7 100644
@@ -17,4 +17,5 @@
 /fanotify17
 /fanotify18
 /fanotify19
+/fanotify20
 /fanotify_child
testcases/kernel/syscalls/fanotify/fanotify.h +21 -0
index a2be18338..da212d953 100644
@@ -78,6 +78,9 @@ static inline int safe_fanotify_mark(const char *file, const int lineno,
 #define FAN_REPORT_NAME		0x00000800
 #define FAN_REPORT_DFID_NAME     (FAN_REPORT_DIR_FID | FAN_REPORT_NAME)
 #endif
+#ifndef FAN_REPORT_PIDFD
+#define FAN_REPORT_PIDFD	0x00000080
+#endif
 
 /* Non-uapi convenience macros */
 #ifndef FAN_REPORT_DFID_NAME_FID
@@ -125,6 +128,14 @@ static inline int safe_fanotify_mark(const char *file, const int lineno,
 #define FAN_OPEN_EXEC_PERM	0x00040000
 #endif
 
+/* Additional error status codes that can be returned to userspace */
+#ifndef FAN_NOPIDFD
+#define FAN_NOPIDFD		-1
+#endif
+#ifndef FAN_EPIDFD
+#define FAN_EPIDFD		-2
+#endif
+
 /* Flags required for unprivileged user group */
 #define FANOTIFY_REQUIRED_USER_INIT_FLAGS    (FAN_REPORT_FID)
 
@@ -164,6 +175,9 @@ typedef struct {
 #ifndef FAN_EVENT_INFO_TYPE_DFID
 #define FAN_EVENT_INFO_TYPE_DFID	3
 #endif
+#ifndef FAN_EVENT_INFO_TYPE_PIDFD
+#define FAN_EVENT_INFO_TYPE_PIDFD	4
+#endif
 
 #ifndef HAVE_STRUCT_FANOTIFY_EVENT_INFO_HEADER
 struct fanotify_event_info_header {
@@ -181,6 +195,13 @@ struct fanotify_event_info_fid {
 };
 #endif /* HAVE_STRUCT_FANOTIFY_EVENT_INFO_FID */
 
+#ifndef HAVE_STRUCT_FANOTIFY_EVENT_INFO_PIDFD
+struct fanotify_event_info_pidfd {
+	struct fanotify_event_info_header hdr;
+	int32_t pidfd;
+};
+#endif /* HAVE_STRUCT_FANOTIFY_EVENT_INFO_PIDFD */
+
 /* NOTE: only for struct fanotify_event_info_fid */
 #ifdef HAVE_STRUCT_FANOTIFY_EVENT_INFO_FID_FSID___VAL
 # define FSID_VAL_MEMBER(fsid, i) (fsid.__val[i])
testcases/kernel/syscalls/fanotify/fanotify20.c new file +133 -0

133 lines not on this page · raw message

Signature · 6 lines
2.33.0.1079.g6e70778dc9-goog

/M

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw

PATCH 2/2 syscalls/fanotify21: add new test checking the returned pidfd from fanotify in FAN_REPORT_PIDFD mode

In reply to Matthew Bobrowski via ltp

A new test that performs verification on the values returned within the struct fanotify_event_info_pidfd record when notification group intialized in FAN_REPORT_PIDFD mode.

---
 testcases/kernel/syscalls/fanotify/.gitignore |   1 +
 .../kernel/syscalls/fanotify/fanotify21.c     | 363 ++++++++++++++++++
 2 files changed, 364 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fanotify/fanotify21.c
testcases/kernel/syscalls/fanotify/.gitignore +1 -0
index c99e6fff7..35e73b91e 100644
@@ -18,4 +18,5 @@
 /fanotify18
 /fanotify19
 /fanotify20
+/fanotify21
 /fanotify_child
testcases/kernel/syscalls/fanotify/fanotify21.c new file +363 -0

363 lines not on this page · raw message

Signature · 6 lines
2.33.0.1079.g6e70778dc9-goog

/M

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v1 1/2
#2 lore raw

Re: [LTP] [PATCH 1/2] syscalls/fanotify20: add new test for FAN_REPORT_PIDFD

In reply to Matthew Bobrowski via ltp
Quoted 7 lines · Matthew Bobrowski

This test ensures that the fanotify API returns the expected error status code -EINVAL when an invalid flag is supplied alongside the new FAN_REPORT_PIDFD initialization flag. Currently, FAN_REPORT_TID is the only initialization flag that is not permitted in conjunction with FAN_REPORT_PIDFD, so we explicitly provide test coverage for this.

Quoted 5 lines

We also add an extra trivial test case to ensure that the initialization behavior with the other FAN_REPORT_* related flags is working as intended.

Quoted 139 lines
---
 configure.ac                                  |   2 +-
 testcases/kernel/syscalls/fanotify/.gitignore |   1 +
 testcases/kernel/syscalls/fanotify/fanotify.h |  21 +++
 .../kernel/syscalls/fanotify/fanotify20.c     | 133 ++++++++++++++++++
 4 files changed, 156 insertions(+), 1 deletion(-)
 create mode 100644 testcases/kernel/syscalls/fanotify/fanotify20.c
configure.ac +1 -1
index 5bf3c52ec..b62ec5e15 100644
@@ -159,7 +159,7 @@ AC_CHECK_MEMBERS([struct utsname.domainname],,,[
 AC_CHECK_TYPES([enum kcmp_type],,,[#include <linux/kcmp.h>])
 AC_CHECK_TYPES([struct acct_v3],,,[#include <sys/acct.h>])
 AC_CHECK_TYPES([struct af_alg_iv, struct sockaddr_alg],,,[# include <linux/if_alg.h>])
-AC_CHECK_TYPES([struct fanotify_event_info_fid, struct fanotify_event_info_header],,,[#include <sys/fanotify.h>])
+AC_CHECK_TYPES([struct fanotify_event_info_fid, struct fanotify_event_info_header, struct fanotify_event_info_pidfd],,,[#include <sys/fanotify.h>])
 AC_CHECK_TYPES([struct file_dedupe_range],,,[#include <linux/fs.h>])
 
 AC_CHECK_TYPES([struct file_handle],,,[
testcases/kernel/syscalls/fanotify/.gitignore +1 -0
index 9554b16b1..c99e6fff7 100644
@@ -17,4 +17,5 @@
 /fanotify17
 /fanotify18
 /fanotify19
+/fanotify20
 /fanotify_child
testcases/kernel/syscalls/fanotify/fanotify.h +21 -0
index a2be18338..da212d953 100644
@@ -78,6 +78,9 @@ static inline int safe_fanotify_mark(const char *file, const int lineno,
 #define FAN_REPORT_NAME                0x00000800
 #define FAN_REPORT_DFID_NAME     (FAN_REPORT_DIR_FID | FAN_REPORT_NAME)
 #endif
+#ifndef FAN_REPORT_PIDFD
+#define FAN_REPORT_PIDFD       0x00000080
+#endif
 
 /* Non-uapi convenience macros */
 #ifndef FAN_REPORT_DFID_NAME_FID
@@ -125,6 +128,14 @@ static inline int safe_fanotify_mark(const char *file, const int lineno,
 #define FAN_OPEN_EXEC_PERM     0x00040000
 #endif
 
+/* Additional error status codes that can be returned to userspace */
+#ifndef FAN_NOPIDFD
+#define FAN_NOPIDFD            -1
+#endif
+#ifndef FAN_EPIDFD
+#define FAN_EPIDFD             -2
+#endif
+
 /* Flags required for unprivileged user group */
 #define FANOTIFY_REQUIRED_USER_INIT_FLAGS    (FAN_REPORT_FID)
 
@@ -164,6 +175,9 @@ typedef struct {
 #ifndef FAN_EVENT_INFO_TYPE_DFID
 #define FAN_EVENT_INFO_TYPE_DFID       3
 #endif
+#ifndef FAN_EVENT_INFO_TYPE_PIDFD
+#define FAN_EVENT_INFO_TYPE_PIDFD      4
+#endif
 
 #ifndef HAVE_STRUCT_FANOTIFY_EVENT_INFO_HEADER
 struct fanotify_event_info_header {
@@ -181,6 +195,13 @@ struct fanotify_event_info_fid {
 };
 #endif /* HAVE_STRUCT_FANOTIFY_EVENT_INFO_FID */
 
+#ifndef HAVE_STRUCT_FANOTIFY_EVENT_INFO_PIDFD
+struct fanotify_event_info_pidfd {
+       struct fanotify_event_info_header hdr;
+       int32_t pidfd;
+};
+#endif /* HAVE_STRUCT_FANOTIFY_EVENT_INFO_PIDFD */
+
 /* NOTE: only for struct fanotify_event_info_fid */
 #ifdef HAVE_STRUCT_FANOTIFY_EVENT_INFO_FID_FSID___VAL
 # define FSID_VAL_MEMBER(fsid, i) (fsid.__val[i])
testcases/kernel/syscalls/fanotify/fanotify20.c new file +49 -0
new file mode 100644
index 000000000..3e7ca697e
@@ -0,0 +1,133 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 Google. All Rights Reserved.
+ *
+ * Started by Matthew Bobrowski <[email protected]>
+ */
+
+/*\
+ * [Description]
+ *
+ * This source file contains a test case which ensures that the fanotify API
+ * returns an expected error code when provided an invalid initialization flag
+ * alongside FAN_REPORT_PIDFD. Additionally, it checks that the operability with
+ * existing FAN_REPORT_* flags is maintained and functioning as intended.
+ */
+
+#define _GNU_SOURCE
+#include "tst_test.h"
+#include <errno.h>
+
+#ifdef HAVE_SYS_FANOTIFY_H
+#include "fanotify.h"
+
+#define MOUNT_PATH     "fs_mnt"
+
+static int fanotify_fd;
+
+static struct test_case_t {
+       char *name;
+       unsigned int init_flags;
+       int want_err;
+       int want_errno;
+} test_cases[] = {
+       {
+               "fail on FAN_REPORT_PIDFD | FAN_REPORT_TID",
+               FAN_REPORT_PIDFD | FAN_REPORT_TID,
+               1,
+               EINVAL,
+       },
+       {
+               "pass on FAN_REPORT_PIDFD | FAN_REPORT_FID | FAN_REPORT_DFID_NAME",
+               FAN_REPORT_PIDFD | FAN_REPORT_FID | FAN_REPORT_DFID_NAME ,
+               0,
+               0,
+       },
+};
+
+static void do_setup(void)
+{

testcases/kernel/syscalls/fanotify/fanotify20.c line 59

Quoted 10 lines
testcases/kernel/syscalls/fanotify/fanotify20.c new file +10 -0
+       int ret;
+
+       /*
+        * An explicit check for FAN_REPORT_PIDFD is performed early on in the
+        * test initialization as it's a prerequisite for all test cases.
+        */
+       if ((ret = fanotify_init_flags_supported_by_kernel(FAN_REPORT_PIDFD))) {
+               fanotify_init_flags_err_msg("FAN_REPORT_PIDFD", __FILE__,
+                                           __LINE__, tst_brk_, ret);
+       }

Since this test is expected to be merged before Gabriel's FAN_FS_ERROR tests I suggest to pick some of Gabriel's prep patches and use a macro here. Not a must.

Thanks,
Amir.

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw

Re: [LTP] [PATCH 2/2] syscalls/fanotify21: add new test checking the returned pidfd from fanotify in FAN_REPORT_PIDFD mode

In reply to Matthew Bobrowski via ltp
Quoted 123 lines · Matthew Bobrowski

A new test that performs verification on the values returned within the struct fanotify_event_info_pidfd record when notification group intialized in FAN_REPORT_PIDFD mode.

---
 testcases/kernel/syscalls/fanotify/.gitignore |   1 +
 .../kernel/syscalls/fanotify/fanotify21.c     | 363 ++++++++++++++++++
 2 files changed, 364 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fanotify/fanotify21.c
testcases/kernel/syscalls/fanotify/.gitignore +1 -0
index c99e6fff7..35e73b91e 100644
@@ -18,4 +18,5 @@
 /fanotify18
 /fanotify19
 /fanotify20
+/fanotify21
 /fanotify_child
testcases/kernel/syscalls/fanotify/fanotify21.c new file +95 -0
new file mode 100644
index 000000000..f64f8fef4
@@ -0,0 +1,363 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 Google. All Rights Reserved.
+ *
+ * Started by Matthew Bobrowski <[email protected]>
+ */
+
+/*\
+ * [Description]
+ *
+ * A test which verifies whether the returned struct
+ * fanotify_event_info_pidfd in FAN_REPORT_PIDFD mode contains the
+ * expected set of information.
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+#include "tst_test.h"
+#include "tst_safe_stdio.h"
+#include "lapi/pidfd_open.h"
+
+#ifdef HAVE_SYS_FANOTIFY_H
+#include "fanotify.h"
+
+#define BUF_SZ         4096
+#define MOUNT_PATH     "fs_mnt"
+#define TEST_FILE      MOUNT_PATH "/testfile"
+
+struct pidfd_fdinfo_t {
+       int pos;
+       int flags;
+       int mnt_id;
+       int pid;
+       int ns_pid;
+};
+
+struct test_case_t {
+       char *name;
+       int fork;
+       int want_pidfd_err;
+} test_cases[] = {
+       {
+               "return a valid pidfd for event created by self",
+               0,
+               0,
+       },
+       {
+               "return invalid pidfd for event created by terminated child",
+               1,
+               FAN_NOPIDFD,
+       },
+};
+
+static int fanotify_fd;
+static char event_buf[BUF_SZ];
+static struct pidfd_fdinfo_t *self_pidfd_fdinfo = NULL;
+
+static char *trim(char *line)
+{
+       char *start = line;
+       char *end = line + strlen(line);
+
+       while(*start && isspace(*start))
+               start++;
+
+       while(end > start && isspace(*(end - 1)))
+               end--;
+
+       *end = '\0';
+       return start;
+}
+
+static int parse_pidfd_fdinfo_line(char *line,
+                                  struct pidfd_fdinfo_t *pidfd_fdinfo)
+{
+       char *ptr, *key, *value;
+
+       ptr = strchr(line, ':');
+       if (ptr == NULL)
+               return -1;
+
+       *ptr++ = '\0';
+       key = trim(line);
+       value = trim(ptr);
+
+       /*
+        * Ensure to cover all keys of interest that may be found within the
+        * pidfd fdinfo. If we encounter an unexpected key, skip it.
+        */
+       if (strcmp(key, "pos") == 0)
+               pidfd_fdinfo->pos = atoi(value);
+       else if (strcmp(key, "flags") == 0)

testcases/kernel/syscalls/fanotify/fanotify21.c line 105

Quoted 10 lines
testcases/kernel/syscalls/fanotify/fanotify21.c new file +10 -0
+               pidfd_fdinfo->flags = (int)strtol(value, NULL, 16);
+       else if (strcmp(key, "mnt_id") == 0)
+               pidfd_fdinfo->mnt_id = atoi(value);
+       else if (strcmp(key, "Pid") == 0)
+               pidfd_fdinfo->pid = atoi(value);
+       else if (strcmp(key, "NSpid") == 0)
+               pidfd_fdinfo->ns_pid = atoi(value);
+
+       return 0;
+}

Please use existing LTP parsing utilities.
It's never a good idea to re-implement these sort of things. With a quick grep I found:

SAFE_FILE_LINES_SCANF("/proc/meminfo", "SwapCached: %ld",

Otherwise, test looks fine to me.

Thanks,
Amir.

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

Re: [LTP] [PATCH 1/2] syscalls/fanotify20: add new test for FAN_REPORT_PIDFD

In reply to Amir Goldstein
Quoted 161 lines · Amir Goldstein
Quoted 7 lines · Matthew Bobrowski

This test ensures that the fanotify API returns the expected error status code -EINVAL when an invalid flag is supplied alongside the new FAN_REPORT_PIDFD initialization flag. Currently, FAN_REPORT_TID is the only initialization flag that is not permitted in conjunction with FAN_REPORT_PIDFD, so we explicitly provide test coverage for this.

Quoted 5 lines

We also add an extra trivial test case to ensure that the initialization behavior with the other FAN_REPORT_* related flags is working as intended.

Quoted 145 lines
---
 configure.ac                                  |   2 +-
 testcases/kernel/syscalls/fanotify/.gitignore |   1 +
 testcases/kernel/syscalls/fanotify/fanotify.h |  21 +++
 .../kernel/syscalls/fanotify/fanotify20.c     | 133 ++++++++++++++++++
 4 files changed, 156 insertions(+), 1 deletion(-)
 create mode 100644 testcases/kernel/syscalls/fanotify/fanotify20.c
configure.ac +1 -1
index 5bf3c52ec..b62ec5e15 100644
@@ -159,7 +159,7 @@ AC_CHECK_MEMBERS([struct utsname.domainname],,,[
 AC_CHECK_TYPES([enum kcmp_type],,,[#include <linux/kcmp.h>])
 AC_CHECK_TYPES([struct acct_v3],,,[#include <sys/acct.h>])
 AC_CHECK_TYPES([struct af_alg_iv, struct sockaddr_alg],,,[# include <linux/if_alg.h>])
-AC_CHECK_TYPES([struct fanotify_event_info_fid, struct fanotify_event_info_header],,,[#include <sys/fanotify.h>])
+AC_CHECK_TYPES([struct fanotify_event_info_fid, struct fanotify_event_info_header, struct fanotify_event_info_pidfd],,,[#include <sys/fanotify.h>])
 AC_CHECK_TYPES([struct file_dedupe_range],,,[#include <linux/fs.h>])
 
 AC_CHECK_TYPES([struct file_handle],,,[
testcases/kernel/syscalls/fanotify/.gitignore +1 -0
index 9554b16b1..c99e6fff7 100644
@@ -17,4 +17,5 @@
 /fanotify17
 /fanotify18
 /fanotify19
+/fanotify20
 /fanotify_child
testcases/kernel/syscalls/fanotify/fanotify.h +21 -0
index a2be18338..da212d953 100644
@@ -78,6 +78,9 @@ static inline int safe_fanotify_mark(const char *file, const int lineno,
 #define FAN_REPORT_NAME                0x00000800
 #define FAN_REPORT_DFID_NAME     (FAN_REPORT_DIR_FID | FAN_REPORT_NAME)
 #endif
+#ifndef FAN_REPORT_PIDFD
+#define FAN_REPORT_PIDFD       0x00000080
+#endif
 
 /* Non-uapi convenience macros */
 #ifndef FAN_REPORT_DFID_NAME_FID
@@ -125,6 +128,14 @@ static inline int safe_fanotify_mark(const char *file, const int lineno,
 #define FAN_OPEN_EXEC_PERM     0x00040000
 #endif
 
+/* Additional error status codes that can be returned to userspace */
+#ifndef FAN_NOPIDFD
+#define FAN_NOPIDFD            -1
+#endif
+#ifndef FAN_EPIDFD
+#define FAN_EPIDFD             -2
+#endif
+
 /* Flags required for unprivileged user group */
 #define FANOTIFY_REQUIRED_USER_INIT_FLAGS    (FAN_REPORT_FID)
 
@@ -164,6 +175,9 @@ typedef struct {
 #ifndef FAN_EVENT_INFO_TYPE_DFID
 #define FAN_EVENT_INFO_TYPE_DFID       3
 #endif
+#ifndef FAN_EVENT_INFO_TYPE_PIDFD
+#define FAN_EVENT_INFO_TYPE_PIDFD      4
+#endif
 
 #ifndef HAVE_STRUCT_FANOTIFY_EVENT_INFO_HEADER
 struct fanotify_event_info_header {
@@ -181,6 +195,13 @@ struct fanotify_event_info_fid {
 };
 #endif /* HAVE_STRUCT_FANOTIFY_EVENT_INFO_FID */
 
+#ifndef HAVE_STRUCT_FANOTIFY_EVENT_INFO_PIDFD
+struct fanotify_event_info_pidfd {
+       struct fanotify_event_info_header hdr;
+       int32_t pidfd;
+};
+#endif /* HAVE_STRUCT_FANOTIFY_EVENT_INFO_PIDFD */
+
 /* NOTE: only for struct fanotify_event_info_fid */
 #ifdef HAVE_STRUCT_FANOTIFY_EVENT_INFO_FID_FSID___VAL
 # define FSID_VAL_MEMBER(fsid, i) (fsid.__val[i])
testcases/kernel/syscalls/fanotify/fanotify20.c new file +55 -0
new file mode 100644
index 000000000..3e7ca697e
@@ -0,0 +1,133 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 Google. All Rights Reserved.
+ *
+ * Started by Matthew Bobrowski <[email protected]>
+ */
+
+/*\
+ * [Description]
+ *
+ * This source file contains a test case which ensures that the fanotify API
+ * returns an expected error code when provided an invalid initialization flag
+ * alongside FAN_REPORT_PIDFD. Additionally, it checks that the operability with
+ * existing FAN_REPORT_* flags is maintained and functioning as intended.
+ */
+
+#define _GNU_SOURCE
+#include "tst_test.h"
+#include <errno.h>
+
+#ifdef HAVE_SYS_FANOTIFY_H
+#include "fanotify.h"
+
+#define MOUNT_PATH     "fs_mnt"
+
+static int fanotify_fd;
+
+static struct test_case_t {
+       char *name;
+       unsigned int init_flags;
+       int want_err;
+       int want_errno;
+} test_cases[] = {
+       {
+               "fail on FAN_REPORT_PIDFD | FAN_REPORT_TID",
+               FAN_REPORT_PIDFD | FAN_REPORT_TID,
+               1,
+               EINVAL,
+       },
+       {
+               "pass on FAN_REPORT_PIDFD | FAN_REPORT_FID | FAN_REPORT_DFID_NAME",
+               FAN_REPORT_PIDFD | FAN_REPORT_FID | FAN_REPORT_DFID_NAME ,
+               0,
+               0,
+       },
+};
+
+static void do_setup(void)
+{
+       int ret;
+
+       /*
+        * An explicit check for FAN_REPORT_PIDFD is performed early on in the
+        * test initialization as it's a prerequisite for all test cases.
+        */
Quoted 8 lines

testcases/kernel/syscalls/fanotify/fanotify20.c line 59

Quoted 4 lines
testcases/kernel/syscalls/fanotify/fanotify20.c new file +4 -0
+       if ((ret = fanotify_init_flags_supported_by_kernel(FAN_REPORT_PIDFD))) {
+               fanotify_init_flags_err_msg("FAN_REPORT_PIDFD", __FILE__,
+                                           __LINE__, tst_brk_, ret);
+       }

Since this test is expected to be merged before Gabriel's FAN_FS_ERROR tests I suggest to pick some of Gabriel's prep patches and use a macro here. Not a must.

So, to facilitate merging, I will submit the next version of my test renamed to fanotify21.c, instead of fanotify20.c.

Signature · 4 lines
Gabriel Krisman Bertazi

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

Re: [LTP] [PATCH 1/2] syscalls/fanotify20: add new test for FAN_REPORT_PIDFD

In reply to Amir Goldstein
Quoted 161 lines · Amir Goldstein
Quoted 7 lines · Matthew Bobrowski

This test ensures that the fanotify API returns the expected error status code -EINVAL when an invalid flag is supplied alongside the new FAN_REPORT_PIDFD initialization flag. Currently, FAN_REPORT_TID is the only initialization flag that is not permitted in conjunction with FAN_REPORT_PIDFD, so we explicitly provide test coverage for this.

Quoted 5 lines

We also add an extra trivial test case to ensure that the initialization behavior with the other FAN_REPORT_* related flags is working as intended.

Quoted 145 lines
---
 configure.ac                                  |   2 +-
 testcases/kernel/syscalls/fanotify/.gitignore |   1 +
 testcases/kernel/syscalls/fanotify/fanotify.h |  21 +++
 .../kernel/syscalls/fanotify/fanotify20.c     | 133 ++++++++++++++++++
 4 files changed, 156 insertions(+), 1 deletion(-)
 create mode 100644 testcases/kernel/syscalls/fanotify/fanotify20.c
configure.ac +1 -1
index 5bf3c52ec..b62ec5e15 100644
@@ -159,7 +159,7 @@ AC_CHECK_MEMBERS([struct utsname.domainname],,,[
 AC_CHECK_TYPES([enum kcmp_type],,,[#include <linux/kcmp.h>])
 AC_CHECK_TYPES([struct acct_v3],,,[#include <sys/acct.h>])
 AC_CHECK_TYPES([struct af_alg_iv, struct sockaddr_alg],,,[# include <linux/if_alg.h>])
-AC_CHECK_TYPES([struct fanotify_event_info_fid, struct fanotify_event_info_header],,,[#include <sys/fanotify.h>])
+AC_CHECK_TYPES([struct fanotify_event_info_fid, struct fanotify_event_info_header, struct fanotify_event_info_pidfd],,,[#include <sys/fanotify.h>])
 AC_CHECK_TYPES([struct file_dedupe_range],,,[#include <linux/fs.h>])
 
 AC_CHECK_TYPES([struct file_handle],,,[
testcases/kernel/syscalls/fanotify/.gitignore +1 -0
index 9554b16b1..c99e6fff7 100644
@@ -17,4 +17,5 @@
 /fanotify17
 /fanotify18
 /fanotify19
+/fanotify20
 /fanotify_child
testcases/kernel/syscalls/fanotify/fanotify.h +21 -0
index a2be18338..da212d953 100644
@@ -78,6 +78,9 @@ static inline int safe_fanotify_mark(const char *file, const int lineno,
 #define FAN_REPORT_NAME                0x00000800
 #define FAN_REPORT_DFID_NAME     (FAN_REPORT_DIR_FID | FAN_REPORT_NAME)
 #endif
+#ifndef FAN_REPORT_PIDFD
+#define FAN_REPORT_PIDFD       0x00000080
+#endif
 
 /* Non-uapi convenience macros */
 #ifndef FAN_REPORT_DFID_NAME_FID
@@ -125,6 +128,14 @@ static inline int safe_fanotify_mark(const char *file, const int lineno,
 #define FAN_OPEN_EXEC_PERM     0x00040000
 #endif
 
+/* Additional error status codes that can be returned to userspace */
+#ifndef FAN_NOPIDFD
+#define FAN_NOPIDFD            -1
+#endif
+#ifndef FAN_EPIDFD
+#define FAN_EPIDFD             -2
+#endif
+
 /* Flags required for unprivileged user group */
 #define FANOTIFY_REQUIRED_USER_INIT_FLAGS    (FAN_REPORT_FID)
 
@@ -164,6 +175,9 @@ typedef struct {
 #ifndef FAN_EVENT_INFO_TYPE_DFID
 #define FAN_EVENT_INFO_TYPE_DFID       3
 #endif
+#ifndef FAN_EVENT_INFO_TYPE_PIDFD
+#define FAN_EVENT_INFO_TYPE_PIDFD      4
+#endif
 
 #ifndef HAVE_STRUCT_FANOTIFY_EVENT_INFO_HEADER
 struct fanotify_event_info_header {
@@ -181,6 +195,13 @@ struct fanotify_event_info_fid {
 };
 #endif /* HAVE_STRUCT_FANOTIFY_EVENT_INFO_FID */
 
+#ifndef HAVE_STRUCT_FANOTIFY_EVENT_INFO_PIDFD
+struct fanotify_event_info_pidfd {
+       struct fanotify_event_info_header hdr;
+       int32_t pidfd;
+};
+#endif /* HAVE_STRUCT_FANOTIFY_EVENT_INFO_PIDFD */
+
 /* NOTE: only for struct fanotify_event_info_fid */
 #ifdef HAVE_STRUCT_FANOTIFY_EVENT_INFO_FID_FSID___VAL
 # define FSID_VAL_MEMBER(fsid, i) (fsid.__val[i])
testcases/kernel/syscalls/fanotify/fanotify20.c new file +55 -0
new file mode 100644
index 000000000..3e7ca697e
@@ -0,0 +1,133 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 Google. All Rights Reserved.
+ *
+ * Started by Matthew Bobrowski <[email protected]>
+ */
+
+/*\
+ * [Description]
+ *
+ * This source file contains a test case which ensures that the fanotify API
+ * returns an expected error code when provided an invalid initialization flag
+ * alongside FAN_REPORT_PIDFD. Additionally, it checks that the operability with
+ * existing FAN_REPORT_* flags is maintained and functioning as intended.
+ */
+
+#define _GNU_SOURCE
+#include "tst_test.h"
+#include <errno.h>
+
+#ifdef HAVE_SYS_FANOTIFY_H
+#include "fanotify.h"
+
+#define MOUNT_PATH     "fs_mnt"
+
+static int fanotify_fd;
+
+static struct test_case_t {
+       char *name;
+       unsigned int init_flags;
+       int want_err;
+       int want_errno;
+} test_cases[] = {
+       {
+               "fail on FAN_REPORT_PIDFD | FAN_REPORT_TID",
+               FAN_REPORT_PIDFD | FAN_REPORT_TID,
+               1,
+               EINVAL,
+       },
+       {
+               "pass on FAN_REPORT_PIDFD | FAN_REPORT_FID | FAN_REPORT_DFID_NAME",
+               FAN_REPORT_PIDFD | FAN_REPORT_FID | FAN_REPORT_DFID_NAME ,
+               0,
+               0,
+       },
+};
+
+static void do_setup(void)
+{
+       int ret;
+
+       /*
+        * An explicit check for FAN_REPORT_PIDFD is performed early on in the
+        * test initialization as it's a prerequisite for all test cases.
+        */
Quoted 8 lines

testcases/kernel/syscalls/fanotify/fanotify20.c line 59

Quoted 4 lines
testcases/kernel/syscalls/fanotify/fanotify20.c new file +4 -0
+       if ((ret = fanotify_init_flags_supported_by_kernel(FAN_REPORT_PIDFD))) {
+               fanotify_init_flags_err_msg("FAN_REPORT_PIDFD", __FILE__,
+                                           __LINE__, tst_brk_, ret);
+       }

Since this test is expected to be merged before Gabriel's FAN_FS_ERROR tests I suggest to pick some of Gabriel's prep patches and use a macro here. Not a must.

Right, let me take a look through Gabriel's series and adapt the relevant macro.

/M

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

Re: [LTP] [PATCH 1/2] syscalls/fanotify20: add new test for FAN_REPORT_PIDFD

In reply to Gabriel Krisman Bertazi
Quoted 172 lines · Gabriel Krisman Bertazi
Quoted 161 lines · Amir Goldstein
Quoted 7 lines · Matthew Bobrowski

This test ensures that the fanotify API returns the expected error status code -EINVAL when an invalid flag is supplied alongside the new FAN_REPORT_PIDFD initialization flag. Currently, FAN_REPORT_TID is the only initialization flag that is not permitted in conjunction with FAN_REPORT_PIDFD, so we explicitly provide test coverage for this.

Quoted 5 lines

We also add an extra trivial test case to ensure that the initialization behavior with the other FAN_REPORT_* related flags is working as intended.

Quoted 145 lines
---
 configure.ac                                  |   2 +-
 testcases/kernel/syscalls/fanotify/.gitignore |   1 +
 testcases/kernel/syscalls/fanotify/fanotify.h |  21 +++
 .../kernel/syscalls/fanotify/fanotify20.c     | 133 ++++++++++++++++++
 4 files changed, 156 insertions(+), 1 deletion(-)
 create mode 100644 testcases/kernel/syscalls/fanotify/fanotify20.c
configure.ac +1 -1
index 5bf3c52ec..b62ec5e15 100644
@@ -159,7 +159,7 @@ AC_CHECK_MEMBERS([struct utsname.domainname],,,[
 AC_CHECK_TYPES([enum kcmp_type],,,[#include <linux/kcmp.h>])
 AC_CHECK_TYPES([struct acct_v3],,,[#include <sys/acct.h>])
 AC_CHECK_TYPES([struct af_alg_iv, struct sockaddr_alg],,,[# include <linux/if_alg.h>])
-AC_CHECK_TYPES([struct fanotify_event_info_fid, struct fanotify_event_info_header],,,[#include <sys/fanotify.h>])
+AC_CHECK_TYPES([struct fanotify_event_info_fid, struct fanotify_event_info_header, struct fanotify_event_info_pidfd],,,[#include <sys/fanotify.h>])
 AC_CHECK_TYPES([struct file_dedupe_range],,,[#include <linux/fs.h>])
 
 AC_CHECK_TYPES([struct file_handle],,,[
testcases/kernel/syscalls/fanotify/.gitignore +1 -0
index 9554b16b1..c99e6fff7 100644
@@ -17,4 +17,5 @@
 /fanotify17
 /fanotify18
 /fanotify19
+/fanotify20
 /fanotify_child
testcases/kernel/syscalls/fanotify/fanotify.h +21 -0
index a2be18338..da212d953 100644
@@ -78,6 +78,9 @@ static inline int safe_fanotify_mark(const char *file, const int lineno,
 #define FAN_REPORT_NAME                0x00000800
 #define FAN_REPORT_DFID_NAME     (FAN_REPORT_DIR_FID | FAN_REPORT_NAME)
 #endif
+#ifndef FAN_REPORT_PIDFD
+#define FAN_REPORT_PIDFD       0x00000080
+#endif
 
 /* Non-uapi convenience macros */
 #ifndef FAN_REPORT_DFID_NAME_FID
@@ -125,6 +128,14 @@ static inline int safe_fanotify_mark(const char *file, const int lineno,
 #define FAN_OPEN_EXEC_PERM     0x00040000
 #endif
 
+/* Additional error status codes that can be returned to userspace */
+#ifndef FAN_NOPIDFD
+#define FAN_NOPIDFD            -1
+#endif
+#ifndef FAN_EPIDFD
+#define FAN_EPIDFD             -2
+#endif
+
 /* Flags required for unprivileged user group */
 #define FANOTIFY_REQUIRED_USER_INIT_FLAGS    (FAN_REPORT_FID)
 
@@ -164,6 +175,9 @@ typedef struct {
 #ifndef FAN_EVENT_INFO_TYPE_DFID
 #define FAN_EVENT_INFO_TYPE_DFID       3
 #endif
+#ifndef FAN_EVENT_INFO_TYPE_PIDFD
+#define FAN_EVENT_INFO_TYPE_PIDFD      4
+#endif
 
 #ifndef HAVE_STRUCT_FANOTIFY_EVENT_INFO_HEADER
 struct fanotify_event_info_header {
@@ -181,6 +195,13 @@ struct fanotify_event_info_fid {
 };
 #endif /* HAVE_STRUCT_FANOTIFY_EVENT_INFO_FID */
 
+#ifndef HAVE_STRUCT_FANOTIFY_EVENT_INFO_PIDFD
+struct fanotify_event_info_pidfd {
+       struct fanotify_event_info_header hdr;
+       int32_t pidfd;
+};
+#endif /* HAVE_STRUCT_FANOTIFY_EVENT_INFO_PIDFD */
+
 /* NOTE: only for struct fanotify_event_info_fid */
 #ifdef HAVE_STRUCT_FANOTIFY_EVENT_INFO_FID_FSID___VAL
 # define FSID_VAL_MEMBER(fsid, i) (fsid.__val[i])
testcases/kernel/syscalls/fanotify/fanotify20.c new file +55 -0
new file mode 100644
index 000000000..3e7ca697e
@@ -0,0 +1,133 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 Google. All Rights Reserved.
+ *
+ * Started by Matthew Bobrowski <[email protected]>
+ */
+
+/*\
+ * [Description]
+ *
+ * This source file contains a test case which ensures that the fanotify API
+ * returns an expected error code when provided an invalid initialization flag
+ * alongside FAN_REPORT_PIDFD. Additionally, it checks that the operability with
+ * existing FAN_REPORT_* flags is maintained and functioning as intended.
+ */
+
+#define _GNU_SOURCE
+#include "tst_test.h"
+#include <errno.h>
+
+#ifdef HAVE_SYS_FANOTIFY_H
+#include "fanotify.h"
+
+#define MOUNT_PATH     "fs_mnt"
+
+static int fanotify_fd;
+
+static struct test_case_t {
+       char *name;
+       unsigned int init_flags;
+       int want_err;
+       int want_errno;
+} test_cases[] = {
+       {
+               "fail on FAN_REPORT_PIDFD | FAN_REPORT_TID",
+               FAN_REPORT_PIDFD | FAN_REPORT_TID,
+               1,
+               EINVAL,
+       },
+       {
+               "pass on FAN_REPORT_PIDFD | FAN_REPORT_FID | FAN_REPORT_DFID_NAME",
+               FAN_REPORT_PIDFD | FAN_REPORT_FID | FAN_REPORT_DFID_NAME ,
+               0,
+               0,
+       },
+};
+
+static void do_setup(void)
+{
+       int ret;
+
+       /*
+        * An explicit check for FAN_REPORT_PIDFD is performed early on in the
+        * test initialization as it's a prerequisite for all test cases.
+        */
Quoted 8 lines

testcases/kernel/syscalls/fanotify/fanotify20.c line 59

Quoted 4 lines
testcases/kernel/syscalls/fanotify/fanotify20.c new file +4 -0
+       if ((ret = fanotify_init_flags_supported_by_kernel(FAN_REPORT_PIDFD))) {
+               fanotify_init_flags_err_msg("FAN_REPORT_PIDFD", __FILE__,
+                                           __LINE__, tst_brk_, ret);
+       }

Since this test is expected to be merged before Gabriel's FAN_FS_ERROR tests I suggest to pick some of Gabriel's prep patches and use a macro here. Not a must.

Quoted 2 lines

So, to facilitate merging, I will submit the next version of my test renamed to fanotify21.c, instead of fanotify20.c.

Not fanotify21.c, but rather fanotify22.c,

/M

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#8 lore raw

Re: [LTP] [PATCH 2/2] syscalls/fanotify21: add new test checking the returned pidfd from fanotify in FAN_REPORT_PIDFD mode

In reply to Amir Goldstein
Quoted 139 lines · Amir Goldstein
Quoted 123 lines · Matthew Bobrowski

A new test that performs verification on the values returned within the struct fanotify_event_info_pidfd record when notification group intialized in FAN_REPORT_PIDFD mode.

---
 testcases/kernel/syscalls/fanotify/.gitignore |   1 +
 .../kernel/syscalls/fanotify/fanotify21.c     | 363 ++++++++++++++++++
 2 files changed, 364 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fanotify/fanotify21.c
testcases/kernel/syscalls/fanotify/.gitignore +1 -0
index c99e6fff7..35e73b91e 100644
@@ -18,4 +18,5 @@
 /fanotify18
 /fanotify19
 /fanotify20
+/fanotify21
 /fanotify_child
testcases/kernel/syscalls/fanotify/fanotify21.c new file +95 -0
new file mode 100644
index 000000000..f64f8fef4
@@ -0,0 +1,363 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 Google. All Rights Reserved.
+ *
+ * Started by Matthew Bobrowski <[email protected]>
+ */
+
+/*\
+ * [Description]
+ *
+ * A test which verifies whether the returned struct
+ * fanotify_event_info_pidfd in FAN_REPORT_PIDFD mode contains the
+ * expected set of information.
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+#include "tst_test.h"
+#include "tst_safe_stdio.h"
+#include "lapi/pidfd_open.h"
+
+#ifdef HAVE_SYS_FANOTIFY_H
+#include "fanotify.h"
+
+#define BUF_SZ         4096
+#define MOUNT_PATH     "fs_mnt"
+#define TEST_FILE      MOUNT_PATH "/testfile"
+
+struct pidfd_fdinfo_t {
+       int pos;
+       int flags;
+       int mnt_id;
+       int pid;
+       int ns_pid;
+};
+
+struct test_case_t {
+       char *name;
+       int fork;
+       int want_pidfd_err;
+} test_cases[] = {
+       {
+               "return a valid pidfd for event created by self",
+               0,
+               0,
+       },
+       {
+               "return invalid pidfd for event created by terminated child",
+               1,
+               FAN_NOPIDFD,
+       },
+};
+
+static int fanotify_fd;
+static char event_buf[BUF_SZ];
+static struct pidfd_fdinfo_t *self_pidfd_fdinfo = NULL;
+
+static char *trim(char *line)
+{
+       char *start = line;
+       char *end = line + strlen(line);
+
+       while(*start && isspace(*start))
+               start++;
+
+       while(end > start && isspace(*(end - 1)))
+               end--;
+
+       *end = '\0';
+       return start;
+}
+
+static int parse_pidfd_fdinfo_line(char *line,
+                                  struct pidfd_fdinfo_t *pidfd_fdinfo)
+{
+       char *ptr, *key, *value;
+
+       ptr = strchr(line, ':');
+       if (ptr == NULL)
+               return -1;
+
+       *ptr++ = '\0';
+       key = trim(line);
+       value = trim(ptr);
+
+       /*
+        * Ensure to cover all keys of interest that may be found within the
+        * pidfd fdinfo. If we encounter an unexpected key, skip it.
+        */
+       if (strcmp(key, "pos") == 0)
+               pidfd_fdinfo->pos = atoi(value);
+       else if (strcmp(key, "flags") == 0)

testcases/kernel/syscalls/fanotify/fanotify21.c line 105

Quoted 10 lines
testcases/kernel/syscalls/fanotify/fanotify21.c new file +10 -0
+               pidfd_fdinfo->flags = (int)strtol(value, NULL, 16);
+       else if (strcmp(key, "mnt_id") == 0)
+               pidfd_fdinfo->mnt_id = atoi(value);
+       else if (strcmp(key, "Pid") == 0)
+               pidfd_fdinfo->pid = atoi(value);
+       else if (strcmp(key, "NSpid") == 0)
+               pidfd_fdinfo->ns_pid = atoi(value);
+
+       return 0;
+}

Please use existing LTP parsing utilities.
It's never a good idea to re-implement these sort of things. With a quick grep I found:

Quoted 3 lines

SAFE_FILE_LINES_SCANF("/proc/meminfo", "SwapCached: %ld",

Otherwise, test looks fine to me.

Ah, wonderful. I wasn't aware that this helper had existed. A failure on my part for not properly checking. I think this will work the treat!

/M

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp