* [LTP] [PATCH 0/2] Test support for new fanotify FAN_REPORT_PIDFD feature @ 2021-10-27 9:44 Matthew Bobrowski via ltp 2021-10-27 9:44 ` [LTP] [PATCH 1/2] syscalls/fanotify20: add new test for FAN_REPORT_PIDFD Matthew Bobrowski via ltp 2021-10-27 9:45 ` [LTP] [PATCH 2/2] syscalls/fanotify21: add new test checking the returned pidfd from fanotify in FAN_REPORT_PIDFD mode Matthew Bobrowski via ltp 0 siblings, 2 replies; 9+ messages in thread From: Matthew Bobrowski via ltp @ 2021-10-27 9:44 UTC (permalink / raw) To: ltp; +Cc: jack 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 -- 2.33.0.1079.g6e70778dc9-goog /M -- Mailing list info: https://lists.linux.it/listinfo/ltp ^ permalink raw reply [flat|nested] 9+ messages in thread
* [LTP] [PATCH 1/2] syscalls/fanotify20: add new test for FAN_REPORT_PIDFD 2021-10-27 9:44 [LTP] [PATCH 0/2] Test support for new fanotify FAN_REPORT_PIDFD feature Matthew Bobrowski via ltp @ 2021-10-27 9:44 ` Matthew Bobrowski via ltp 2021-10-27 10:24 ` Amir Goldstein 2021-10-27 9:45 ` [LTP] [PATCH 2/2] syscalls/fanotify21: add new test checking the returned pidfd from fanotify in FAN_REPORT_PIDFD mode Matthew Bobrowski via ltp 1 sibling, 1 reply; 9+ messages in thread From: Matthew Bobrowski via ltp @ 2021-10-27 9:44 UTC (permalink / raw) To: ltp; +Cc: jack 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. Signed-off-by: Matthew Bobrowski <repnop@google.com> --- 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 diff --git a/configure.ac b/configure.ac index 5bf3c52ec..b62ec5e15 100644 --- a/configure.ac +++ b/configure.ac @@ -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],,,[ diff --git a/testcases/kernel/syscalls/fanotify/.gitignore b/testcases/kernel/syscalls/fanotify/.gitignore index 9554b16b1..c99e6fff7 100644 --- a/testcases/kernel/syscalls/fanotify/.gitignore +++ b/testcases/kernel/syscalls/fanotify/.gitignore @@ -17,4 +17,5 @@ /fanotify17 /fanotify18 /fanotify19 +/fanotify20 /fanotify_child diff --git a/testcases/kernel/syscalls/fanotify/fanotify.h b/testcases/kernel/syscalls/fanotify/fanotify.h index a2be18338..da212d953 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify.h +++ b/testcases/kernel/syscalls/fanotify/fanotify.h @@ -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]) diff --git a/testcases/kernel/syscalls/fanotify/fanotify20.c b/testcases/kernel/syscalls/fanotify/fanotify20.c new file mode 100644 index 000000000..3e7ca697e --- /dev/null +++ b/testcases/kernel/syscalls/fanotify/fanotify20.c @@ -0,0 +1,133 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2021 Google. All Rights Reserved. + * + * Started by Matthew Bobrowski <repnop@google.com> + */ + +/*\ + * [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. + */ + if ((ret = fanotify_init_flags_supported_by_kernel(FAN_REPORT_PIDFD))) { + fanotify_init_flags_err_msg("FAN_REPORT_PIDFD", __FILE__, + __LINE__, tst_brk_, ret); + } +} + +static void do_test(unsigned int num) +{ + struct test_case_t *tc = &test_cases[num]; + + tst_res(TINFO, "Test #%d: %s", num, tc->name); + + fanotify_fd = fanotify_init(tc->init_flags, O_RDONLY); + if (fanotify_fd < 0) { + if (!tc->want_err) { + tst_res(TFAIL, + "fanotify_fd=%d, fanotify_init(%x, O_RDONLY) " + "failed with error -%d but wanted success", + fanotify_fd, tc->init_flags, errno); + return; + } + + if (errno != tc->want_errno) { + tst_res(TFAIL, + "fanotify_fd=%d, fanotify_init(%x, O_RDONLY) " + "failed with an unexpected error code -%d but " + "wanted -%d", + fanotify_fd, tc->init_flags, + errno, tc->want_errno); + return; + } + + tst_res(TPASS, + "fanotify_fd=%d, fanotify_init(%x, O_RDONLY) " + "failed with error -%d as expected", + fanotify_fd, tc->init_flags, errno); + return; + } + + /* + * Catch test cases that had expected to receive an error upon calling + * fanotify_init() but had unexpectedly resulted in a success. + */ + if (tc->want_err) { + tst_res(TFAIL, + "fanotify_fd=%d, fanotify_init(%x, O_RDONLY) " + "unexpectedly returned successfully, wanted error -%d", + fanotify_fd, tc->init_flags, tc->want_errno); + return; + } + + tst_res(TPASS, + "fanotify_fd=%d, fanotify_init(%x, O_RDONLY) " + "successfully initialized notification group", + fanotify_fd, tc->init_flags); + + SAFE_CLOSE(fanotify_fd); +} + +static void do_cleanup(void) +{ + if (fanotify_fd >= 0) + SAFE_CLOSE(fanotify_fd); +} + +static struct tst_test test = { + .setup = do_setup, + .test = do_test, + .tcnt = ARRAY_SIZE(test_cases), + .cleanup = do_cleanup, + .all_filesystems = 1, + .needs_root = 1, + .mntpoint = MOUNT_PATH, +}; + +#else + TST_TEST_TCONF("system doesn't have required fanotify support"); +#endif /* HAVE_SYS_FANOTIFY_H */ -- 2.33.0.1079.g6e70778dc9-goog /M -- Mailing list info: https://lists.linux.it/listinfo/ltp ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [LTP] [PATCH 1/2] syscalls/fanotify20: add new test for FAN_REPORT_PIDFD 2021-10-27 9:44 ` [LTP] [PATCH 1/2] syscalls/fanotify20: add new test for FAN_REPORT_PIDFD Matthew Bobrowski via ltp @ 2021-10-27 10:24 ` Amir Goldstein 2021-10-29 15:04 ` Gabriel Krisman Bertazi 2021-11-01 21:05 ` Matthew Bobrowski via ltp 0 siblings, 2 replies; 9+ messages in thread From: Amir Goldstein @ 2021-10-27 10:24 UTC (permalink / raw) To: Matthew Bobrowski; +Cc: Gabriel Krisman Bertazi, Jan Kara, LTP List On Wed, Oct 27, 2021 at 12:45 PM Matthew Bobrowski <repnop@google.com> wrote: > > 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. > > Signed-off-by: Matthew Bobrowski <repnop@google.com> Reviewed-by: Amir Goldstein <amir73il@gmail.com> > --- > 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 > > diff --git a/configure.ac b/configure.ac > index 5bf3c52ec..b62ec5e15 100644 > --- a/configure.ac > +++ b/configure.ac > @@ -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],,,[ > diff --git a/testcases/kernel/syscalls/fanotify/.gitignore b/testcases/kernel/syscalls/fanotify/.gitignore > index 9554b16b1..c99e6fff7 100644 > --- a/testcases/kernel/syscalls/fanotify/.gitignore > +++ b/testcases/kernel/syscalls/fanotify/.gitignore > @@ -17,4 +17,5 @@ > /fanotify17 > /fanotify18 > /fanotify19 > +/fanotify20 > /fanotify_child > diff --git a/testcases/kernel/syscalls/fanotify/fanotify.h b/testcases/kernel/syscalls/fanotify/fanotify.h > index a2be18338..da212d953 100644 > --- a/testcases/kernel/syscalls/fanotify/fanotify.h > +++ b/testcases/kernel/syscalls/fanotify/fanotify.h > @@ -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]) > diff --git a/testcases/kernel/syscalls/fanotify/fanotify20.c b/testcases/kernel/syscalls/fanotify/fanotify20.c > new file mode 100644 > index 000000000..3e7ca697e > --- /dev/null > +++ b/testcases/kernel/syscalls/fanotify/fanotify20.c > @@ -0,0 +1,133 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later > +/* > + * Copyright (c) 2021 Google. All Rights Reserved. > + * > + * Started by Matthew Bobrowski <repnop@google.com> > + */ > + > +/*\ > + * [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. > + */ > + 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. -- Mailing list info: https://lists.linux.it/listinfo/ltp ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [LTP] [PATCH 1/2] syscalls/fanotify20: add new test for FAN_REPORT_PIDFD 2021-10-27 10:24 ` Amir Goldstein @ 2021-10-29 15:04 ` Gabriel Krisman Bertazi 2021-11-01 21:09 ` Matthew Bobrowski via ltp 2021-11-01 21:05 ` Matthew Bobrowski via ltp 1 sibling, 1 reply; 9+ messages in thread From: Gabriel Krisman Bertazi @ 2021-10-29 15:04 UTC (permalink / raw) To: Amir Goldstein; +Cc: Matthew Bobrowski, Jan Kara, LTP List Amir Goldstein <amir73il@gmail.com> writes: > On Wed, Oct 27, 2021 at 12:45 PM Matthew Bobrowski <repnop@google.com> wrote: >> >> 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. >> >> Signed-off-by: Matthew Bobrowski <repnop@google.com> > > Reviewed-by: Amir Goldstein <amir73il@gmail.com> > >> --- >> 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 >> >> diff --git a/configure.ac b/configure.ac >> index 5bf3c52ec..b62ec5e15 100644 >> --- a/configure.ac >> +++ b/configure.ac >> @@ -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],,,[ >> diff --git a/testcases/kernel/syscalls/fanotify/.gitignore b/testcases/kernel/syscalls/fanotify/.gitignore >> index 9554b16b1..c99e6fff7 100644 >> --- a/testcases/kernel/syscalls/fanotify/.gitignore >> +++ b/testcases/kernel/syscalls/fanotify/.gitignore >> @@ -17,4 +17,5 @@ >> /fanotify17 >> /fanotify18 >> /fanotify19 >> +/fanotify20 >> /fanotify_child >> diff --git a/testcases/kernel/syscalls/fanotify/fanotify.h b/testcases/kernel/syscalls/fanotify/fanotify.h >> index a2be18338..da212d953 100644 >> --- a/testcases/kernel/syscalls/fanotify/fanotify.h >> +++ b/testcases/kernel/syscalls/fanotify/fanotify.h >> @@ -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]) >> diff --git a/testcases/kernel/syscalls/fanotify/fanotify20.c b/testcases/kernel/syscalls/fanotify/fanotify20.c >> new file mode 100644 >> index 000000000..3e7ca697e >> --- /dev/null >> +++ b/testcases/kernel/syscalls/fanotify/fanotify20.c >> @@ -0,0 +1,133 @@ >> +// SPDX-License-Identifier: GPL-2.0-or-later >> +/* >> + * Copyright (c) 2021 Google. All Rights Reserved. >> + * >> + * Started by Matthew Bobrowski <repnop@google.com> >> + */ >> + >> +/*\ >> + * [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. >> + */ >> + 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. -- Gabriel Krisman Bertazi -- Mailing list info: https://lists.linux.it/listinfo/ltp ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [LTP] [PATCH 1/2] syscalls/fanotify20: add new test for FAN_REPORT_PIDFD 2021-10-29 15:04 ` Gabriel Krisman Bertazi @ 2021-11-01 21:09 ` Matthew Bobrowski via ltp 0 siblings, 0 replies; 9+ messages in thread From: Matthew Bobrowski via ltp @ 2021-11-01 21:09 UTC (permalink / raw) To: Gabriel Krisman Bertazi; +Cc: Jan Kara, LTP List On Fri, Oct 29, 2021 at 12:04:54PM -0300, Gabriel Krisman Bertazi wrote: > Amir Goldstein <amir73il@gmail.com> writes: > > > On Wed, Oct 27, 2021 at 12:45 PM Matthew Bobrowski <repnop@google.com> wrote: > >> > >> 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. > >> > >> Signed-off-by: Matthew Bobrowski <repnop@google.com> > > > > Reviewed-by: Amir Goldstein <amir73il@gmail.com> > > > >> --- > >> 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 > >> > >> diff --git a/configure.ac b/configure.ac > >> index 5bf3c52ec..b62ec5e15 100644 > >> --- a/configure.ac > >> +++ b/configure.ac > >> @@ -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],,,[ > >> diff --git a/testcases/kernel/syscalls/fanotify/.gitignore b/testcases/kernel/syscalls/fanotify/.gitignore > >> index 9554b16b1..c99e6fff7 100644 > >> --- a/testcases/kernel/syscalls/fanotify/.gitignore > >> +++ b/testcases/kernel/syscalls/fanotify/.gitignore > >> @@ -17,4 +17,5 @@ > >> /fanotify17 > >> /fanotify18 > >> /fanotify19 > >> +/fanotify20 > >> /fanotify_child > >> diff --git a/testcases/kernel/syscalls/fanotify/fanotify.h b/testcases/kernel/syscalls/fanotify/fanotify.h > >> index a2be18338..da212d953 100644 > >> --- a/testcases/kernel/syscalls/fanotify/fanotify.h > >> +++ b/testcases/kernel/syscalls/fanotify/fanotify.h > >> @@ -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]) > >> diff --git a/testcases/kernel/syscalls/fanotify/fanotify20.c b/testcases/kernel/syscalls/fanotify/fanotify20.c > >> new file mode 100644 > >> index 000000000..3e7ca697e > >> --- /dev/null > >> +++ b/testcases/kernel/syscalls/fanotify/fanotify20.c > >> @@ -0,0 +1,133 @@ > >> +// SPDX-License-Identifier: GPL-2.0-or-later > >> +/* > >> + * Copyright (c) 2021 Google. All Rights Reserved. > >> + * > >> + * Started by Matthew Bobrowski <repnop@google.com> > >> + */ > >> + > >> +/*\ > >> + * [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. > >> + */ > >> + 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. Not fanotify21.c, but rather fanotify22.c, /M -- Mailing list info: https://lists.linux.it/listinfo/ltp ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [LTP] [PATCH 1/2] syscalls/fanotify20: add new test for FAN_REPORT_PIDFD 2021-10-27 10:24 ` Amir Goldstein 2021-10-29 15:04 ` Gabriel Krisman Bertazi @ 2021-11-01 21:05 ` Matthew Bobrowski via ltp 1 sibling, 0 replies; 9+ messages in thread From: Matthew Bobrowski via ltp @ 2021-11-01 21:05 UTC (permalink / raw) To: Amir Goldstein; +Cc: Gabriel Krisman Bertazi, Jan Kara, LTP List On Wed, Oct 27, 2021 at 01:24:50PM +0300, Amir Goldstein wrote: > On Wed, Oct 27, 2021 at 12:45 PM Matthew Bobrowski <repnop@google.com> wrote: > > > > 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. > > > > Signed-off-by: Matthew Bobrowski <repnop@google.com> > > Reviewed-by: Amir Goldstein <amir73il@gmail.com> > > > --- > > 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 > > > > diff --git a/configure.ac b/configure.ac > > index 5bf3c52ec..b62ec5e15 100644 > > --- a/configure.ac > > +++ b/configure.ac > > @@ -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],,,[ > > diff --git a/testcases/kernel/syscalls/fanotify/.gitignore b/testcases/kernel/syscalls/fanotify/.gitignore > > index 9554b16b1..c99e6fff7 100644 > > --- a/testcases/kernel/syscalls/fanotify/.gitignore > > +++ b/testcases/kernel/syscalls/fanotify/.gitignore > > @@ -17,4 +17,5 @@ > > /fanotify17 > > /fanotify18 > > /fanotify19 > > +/fanotify20 > > /fanotify_child > > diff --git a/testcases/kernel/syscalls/fanotify/fanotify.h b/testcases/kernel/syscalls/fanotify/fanotify.h > > index a2be18338..da212d953 100644 > > --- a/testcases/kernel/syscalls/fanotify/fanotify.h > > +++ b/testcases/kernel/syscalls/fanotify/fanotify.h > > @@ -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]) > > diff --git a/testcases/kernel/syscalls/fanotify/fanotify20.c b/testcases/kernel/syscalls/fanotify/fanotify20.c > > new file mode 100644 > > index 000000000..3e7ca697e > > --- /dev/null > > +++ b/testcases/kernel/syscalls/fanotify/fanotify20.c > > @@ -0,0 +1,133 @@ > > +// SPDX-License-Identifier: GPL-2.0-or-later > > +/* > > + * Copyright (c) 2021 Google. All Rights Reserved. > > + * > > + * Started by Matthew Bobrowski <repnop@google.com> > > + */ > > + > > +/*\ > > + * [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. > > + */ > > + 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 -- Mailing list info: https://lists.linux.it/listinfo/ltp ^ permalink raw reply [flat|nested] 9+ messages in thread
* [LTP] [PATCH 2/2] syscalls/fanotify21: add new test checking the returned pidfd from fanotify in FAN_REPORT_PIDFD mode 2021-10-27 9:44 [LTP] [PATCH 0/2] Test support for new fanotify FAN_REPORT_PIDFD feature Matthew Bobrowski via ltp 2021-10-27 9:44 ` [LTP] [PATCH 1/2] syscalls/fanotify20: add new test for FAN_REPORT_PIDFD Matthew Bobrowski via ltp @ 2021-10-27 9:45 ` Matthew Bobrowski via ltp 2021-10-27 10:35 ` Amir Goldstein 1 sibling, 1 reply; 9+ messages in thread From: Matthew Bobrowski via ltp @ 2021-10-27 9:45 UTC (permalink / raw) To: ltp; +Cc: jack 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. Signed-off-by: Matthew Bobrowski <repnop@google.com> --- 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 diff --git a/testcases/kernel/syscalls/fanotify/.gitignore b/testcases/kernel/syscalls/fanotify/.gitignore index c99e6fff7..35e73b91e 100644 --- a/testcases/kernel/syscalls/fanotify/.gitignore +++ b/testcases/kernel/syscalls/fanotify/.gitignore @@ -18,4 +18,5 @@ /fanotify18 /fanotify19 /fanotify20 +/fanotify21 /fanotify_child diff --git a/testcases/kernel/syscalls/fanotify/fanotify21.c b/testcases/kernel/syscalls/fanotify/fanotify21.c new file mode 100644 index 000000000..f64f8fef4 --- /dev/null +++ b/testcases/kernel/syscalls/fanotify/fanotify21.c @@ -0,0 +1,363 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2021 Google. All Rights Reserved. + * + * Started by Matthew Bobrowski <repnop@google.com> + */ + +/*\ + * [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) + 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; +} + +static struct pidfd_fdinfo_t *read_pidfd_fdinfo(int pidfd) +{ + FILE *f; + size_t len; + char *line = NULL, *fdinfo_path; + struct pidfd_fdinfo_t *pidfd_fdinfo; + + pidfd_fdinfo = SAFE_MALLOC(sizeof(struct pidfd_fdinfo_t)); + + SAFE_ASPRINTF(&fdinfo_path, "/proc/self/fdinfo/%d", pidfd); + + f = SAFE_FOPEN(fdinfo_path, "r"); + + while (getline(&line, &len, f) != -1) { + if (parse_pidfd_fdinfo_line(line, pidfd_fdinfo)) { + pidfd_fdinfo = NULL; + break; + } + } + + free(line); + free(fdinfo_path); + SAFE_FCLOSE(f); + + return pidfd_fdinfo; +} + +static void generate_event(void) +{ + int fd; + + /* Generate a single FAN_OPEN event on the watched object. */ + fd = SAFE_OPEN(TEST_FILE, O_RDONLY); + SAFE_CLOSE(fd); +} + +static void do_fork(void) +{ + int status; + pid_t child; + + child = SAFE_FORK(); + if (child == 0) { + SAFE_CLOSE(fanotify_fd); + generate_event(); + exit(EXIT_SUCCESS); + } + + SAFE_WAITPID(child, &status, 0); + if (WIFEXITED(status) && WEXITSTATUS(status) != 0) + tst_brk(TBROK, + "child process terminated incorrectly"); +} + +static void do_setup(void) +{ + int ret, pidfd; + + SAFE_TOUCH(TEST_FILE, 0666, NULL); + + /* + * 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); + } + + fanotify_fd = SAFE_FANOTIFY_INIT(FAN_REPORT_PIDFD, O_RDONLY); + SAFE_FANOTIFY_MARK(fanotify_fd, FAN_MARK_ADD, FAN_OPEN, AT_FDCWD, + TEST_FILE); + + pidfd = pidfd_open(getpid(), 0); + if (pidfd < 0) { + tst_brk(TBROK | TERRNO, + "pidfd=%d, pidfd_open(%d, 0) failed", + pidfd, getpid()); + } + + self_pidfd_fdinfo = read_pidfd_fdinfo(pidfd); + if (self_pidfd_fdinfo == NULL) { + tst_brk(TBROK, + "pidfd=%d, failed to read pidfd fdinfo", + pidfd); + } +} + +static void do_test(unsigned int num) +{ + int i = 0, len; + struct test_case_t *tc = &test_cases[num]; + + tst_res(TINFO, "Test #%d: %s", num, tc->name); + + /* + * Generate the event in either self or a child process. Event + * generation in a child process is done so that the FAN_NOPIDFD case + * can be verified. + */ + if (tc->fork) + do_fork(); + else + generate_event(); + + /* + * Read all of the queued events into the provided event + * buffer. + */ + len = SAFE_READ(0, fanotify_fd, event_buf, sizeof(event_buf)); + while(i < len) { + struct fanotify_event_metadata *event; + struct fanotify_event_info_pidfd *info; + struct pidfd_fdinfo_t *event_pidfd_fdinfo = NULL; + + event = (struct fanotify_event_metadata *)&event_buf[i]; + info = (struct fanotify_event_info_pidfd *)(event + 1); + + /* + * Checks ensuring that pidfd information record object header + * fields are set correctly. + */ + if (info->hdr.info_type != FAN_EVENT_INFO_TYPE_PIDFD) { + tst_res(TFAIL, + "unexpected info_type received in info " + "header (expected: %d, got: %d", + FAN_EVENT_INFO_TYPE_PIDFD, + info->hdr.info_type); + info = NULL; + goto next_event; + } else if (info->hdr.len != + sizeof(struct fanotify_event_info_pidfd)) { + tst_res(TFAIL, + "unexpected info object length " + "(expected: %lu, got: %d", + sizeof(struct fanotify_event_info_pidfd), + info->hdr.len); + info = NULL; + goto next_event; + } + + /* + * Check if pidfd information object reported any errors during + * creation and whether they're expected. + */ + if (info->pidfd < 0 && !tc->want_pidfd_err) { + tst_res(TFAIL, + "pidfd creation failed for pid: %u with pidfd error value " + "set to: %d", + (unsigned)event->pid, + info->pidfd); + goto next_event; + } else if (tc->want_pidfd_err && + info->pidfd != tc->want_pidfd_err) { + tst_res(TFAIL, + "pidfd set to an unexpected error: %d for pid: %u", + info->pidfd, + (unsigned)event->pid); + goto next_event; + } else if (tc->want_pidfd_err && + info->pidfd == tc->want_pidfd_err) { + tst_res(TPASS, + "pid: %u terminated before pidfd was created, " + "pidfd set to the value of: %d, as expected", + (unsigned)event->pid, + FAN_NOPIDFD); + goto next_event; + } + + /* + * No pidfd errors occurred, continue with verifying pidfd + * fdinfo validity. + */ + event_pidfd_fdinfo = read_pidfd_fdinfo(info->pidfd); + if (event_pidfd_fdinfo == NULL) { + tst_brk(TBROK, + "reading fdinfo for pidfd: %d " + "describing pid: %u failed", + info->pidfd, + (unsigned)event->pid); + goto next_event; + } else if (event_pidfd_fdinfo->pid != event->pid) { + tst_res(TFAIL, + "pidfd provided for incorrect pid " + "(expected pidfd for pid: %u, got pidfd for " + "pid: %u)", + (unsigned)event->pid, + (unsigned)event_pidfd_fdinfo->pid); + goto next_event; + } else if (memcmp(event_pidfd_fdinfo, self_pidfd_fdinfo, + sizeof(struct pidfd_fdinfo_t))) { + tst_res(TFAIL, + "pidfd fdinfo values for self and event differ " + "(expected pos: %d, flags: %x, mnt_id: %d, " + "pid: %d, ns_pid: %d, got pos: %d, " + "flags: %x, mnt_id: %d, pid: %d, ns_pid: %d", + self_pidfd_fdinfo->pos, + self_pidfd_fdinfo->flags, + self_pidfd_fdinfo->mnt_id, + self_pidfd_fdinfo->pid, + self_pidfd_fdinfo->ns_pid, + event_pidfd_fdinfo->pos, + event_pidfd_fdinfo->flags, + event_pidfd_fdinfo->mnt_id, + event_pidfd_fdinfo->pid, + event_pidfd_fdinfo->ns_pid); + goto next_event; + } else { + tst_res(TPASS, + "got an event with a valid pidfd info record, " + "mask: %lld, pid: %u, fd: %d, " + "pidfd: %d, info_type: %d, info_len: %d", + (unsigned long long)event->mask, + (unsigned)event->pid, + event->fd, + info->pidfd, + info->hdr.info_type, + info->hdr.len); + } + +next_event: + i += event->event_len; + if (event->fd >= 0) + SAFE_CLOSE(event->fd); + + if (info && info->pidfd >= 0) + SAFE_CLOSE(info->pidfd); + + if (event_pidfd_fdinfo) + free(event_pidfd_fdinfo); + } +} + +static void do_cleanup(void) +{ + if (fanotify_fd >= 0) + SAFE_CLOSE(fanotify_fd); + + if (self_pidfd_fdinfo) + free(self_pidfd_fdinfo); +} + +static struct tst_test test = { + .setup = do_setup, + .test = do_test, + .tcnt = ARRAY_SIZE(test_cases), + .cleanup = do_cleanup, + .all_filesystems = 1, + .needs_root = 1, + .mntpoint = MOUNT_PATH, + .forks_child = 1, +}; + +#else + TST_TEST_TCONF("system doesn't have required fanotify support"); +#endif /* HAVE_SYS_FANOTIFY_H */ -- 2.33.0.1079.g6e70778dc9-goog /M -- Mailing list info: https://lists.linux.it/listinfo/ltp ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [LTP] [PATCH 2/2] syscalls/fanotify21: add new test checking the returned pidfd from fanotify in FAN_REPORT_PIDFD mode 2021-10-27 9:45 ` [LTP] [PATCH 2/2] syscalls/fanotify21: add new test checking the returned pidfd from fanotify in FAN_REPORT_PIDFD mode Matthew Bobrowski via ltp @ 2021-10-27 10:35 ` Amir Goldstein 2021-11-01 21:16 ` Matthew Bobrowski via ltp 0 siblings, 1 reply; 9+ messages in thread From: Amir Goldstein @ 2021-10-27 10:35 UTC (permalink / raw) To: Matthew Bobrowski; +Cc: Jan Kara, LTP List On Wed, Oct 27, 2021 at 12:45 PM Matthew Bobrowski <repnop@google.com> wrote: > > 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. > > Signed-off-by: Matthew Bobrowski <repnop@google.com> > --- > 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 > > diff --git a/testcases/kernel/syscalls/fanotify/.gitignore b/testcases/kernel/syscalls/fanotify/.gitignore > index c99e6fff7..35e73b91e 100644 > --- a/testcases/kernel/syscalls/fanotify/.gitignore > +++ b/testcases/kernel/syscalls/fanotify/.gitignore > @@ -18,4 +18,5 @@ > /fanotify18 > /fanotify19 > /fanotify20 > +/fanotify21 > /fanotify_child > diff --git a/testcases/kernel/syscalls/fanotify/fanotify21.c b/testcases/kernel/syscalls/fanotify/fanotify21.c > new file mode 100644 > index 000000000..f64f8fef4 > --- /dev/null > +++ b/testcases/kernel/syscalls/fanotify/fanotify21.c > @@ -0,0 +1,363 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later > +/* > + * Copyright (c) 2021 Google. All Rights Reserved. > + * > + * Started by Matthew Bobrowski <repnop@google.com> > + */ > + > +/*\ > + * [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) > + 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. -- Mailing list info: https://lists.linux.it/listinfo/ltp ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [LTP] [PATCH 2/2] syscalls/fanotify21: add new test checking the returned pidfd from fanotify in FAN_REPORT_PIDFD mode 2021-10-27 10:35 ` Amir Goldstein @ 2021-11-01 21:16 ` Matthew Bobrowski via ltp 0 siblings, 0 replies; 9+ messages in thread From: Matthew Bobrowski via ltp @ 2021-11-01 21:16 UTC (permalink / raw) To: Amir Goldstein; +Cc: Jan Kara, LTP List On Wed, Oct 27, 2021 at 01:35:53PM +0300, Amir Goldstein wrote: > On Wed, Oct 27, 2021 at 12:45 PM Matthew Bobrowski <repnop@google.com> wrote: > > > > 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. > > > > Signed-off-by: Matthew Bobrowski <repnop@google.com> > > --- > > 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 > > > > diff --git a/testcases/kernel/syscalls/fanotify/.gitignore b/testcases/kernel/syscalls/fanotify/.gitignore > > index c99e6fff7..35e73b91e 100644 > > --- a/testcases/kernel/syscalls/fanotify/.gitignore > > +++ b/testcases/kernel/syscalls/fanotify/.gitignore > > @@ -18,4 +18,5 @@ > > /fanotify18 > > /fanotify19 > > /fanotify20 > > +/fanotify21 > > /fanotify_child > > diff --git a/testcases/kernel/syscalls/fanotify/fanotify21.c b/testcases/kernel/syscalls/fanotify/fanotify21.c > > new file mode 100644 > > index 000000000..f64f8fef4 > > --- /dev/null > > +++ b/testcases/kernel/syscalls/fanotify/fanotify21.c > > @@ -0,0 +1,363 @@ > > +// SPDX-License-Identifier: GPL-2.0-or-later > > +/* > > + * Copyright (c) 2021 Google. All Rights Reserved. > > + * > > + * Started by Matthew Bobrowski <repnop@google.com> > > + */ > > + > > +/*\ > > + * [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) > > + 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. 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 -- Mailing list info: https://lists.linux.it/listinfo/ltp ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2021-11-01 21:16 UTC | newest] Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2021-10-27 9:44 [LTP] [PATCH 0/2] Test support for new fanotify FAN_REPORT_PIDFD feature Matthew Bobrowski via ltp 2021-10-27 9:44 ` [LTP] [PATCH 1/2] syscalls/fanotify20: add new test for FAN_REPORT_PIDFD Matthew Bobrowski via ltp 2021-10-27 10:24 ` Amir Goldstein 2021-10-29 15:04 ` Gabriel Krisman Bertazi 2021-11-01 21:09 ` Matthew Bobrowski via ltp 2021-11-01 21:05 ` Matthew Bobrowski via ltp 2021-10-27 9:45 ` [LTP] [PATCH 2/2] syscalls/fanotify21: add new test checking the returned pidfd from fanotify in FAN_REPORT_PIDFD mode Matthew Bobrowski via ltp 2021-10-27 10:35 ` Amir Goldstein 2021-11-01 21:16 ` Matthew Bobrowski via ltp
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).