From patchwork Tue Apr 2 10:01:55 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Bobrowski X-Patchwork-Id: 1074165 X-Patchwork-Delegate: petr.vorel@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=lists.linux.it (client-ip=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=mbobrowski.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=mbobrowski-org.20150623.gappssmtp.com header.i=@mbobrowski-org.20150623.gappssmtp.com header.b="0GsW4EMd"; dkim-atps=neutral Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 44YPrf2VG3z9sSy for ; Tue, 2 Apr 2019 21:02:10 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id C27083EA7D9 for ; Tue, 2 Apr 2019 12:02:06 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-5.smtp.seeweb.it (in-5.smtp.seeweb.it [217.194.8.5]) by picard.linux.it (Postfix) with ESMTP id 5DBFA3EA7D9 for ; Tue, 2 Apr 2019 12:02:05 +0200 (CEST) Received: from mail-pl1-x642.google.com (mail-pl1-x642.google.com [IPv6:2607:f8b0:4864:20::642]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by in-5.smtp.seeweb.it (Postfix) with ESMTPS id 30BAD60148C for ; Tue, 2 Apr 2019 12:02:06 +0200 (CEST) Received: by mail-pl1-x642.google.com with SMTP id w23so3068460ply.4 for ; Tue, 02 Apr 2019 03:02:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mbobrowski-org.20150623.gappssmtp.com; s=20150623; h=from:date:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=2tdDAUv72m2Nk/mAfsyvxviNZuqUos5s1zSTudr0HBE=; b=0GsW4EMdhb3mOrIH2XHlJzf+7bYMGFXXYu1dmARvv0IYgO3d8WLOtAQT5G8AvUGZad 9nQaKj0HbMJZ0RctU8EMo5hihzJKfSQCULxBtT7amxbG2bRMmnk8Q0xPtWQJkbzN7TBW 34E2WyOd+fZWW5R5/2RJ9e8QMgXGKztKmM5/yQio7htVOy5kn/wbKWAGczuBn8j4/lKm HxqTUM31uYSsSILIndwG59msLwPqqjmdVhNEGm2qIJJHYPBR/75hWN7nXsELfyqG2pML lOysY3ZU4hMMe/aHynFAV8d//SLGYfZhN9WhEG3U/8D2c+5solI2KYExqk/3eUql2ReM VC4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:date:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=2tdDAUv72m2Nk/mAfsyvxviNZuqUos5s1zSTudr0HBE=; b=qOpUWb4dHQ6oDxiG5vLwAvQKfXrxQI/9HUbhCOYhMGmhBQl5/AQg5Ol00NDrWXhr73 n56QuGaXaOuPVcqMPHDt/rS3mQ0+7SLB87nXFS5OmLMcTadTq6UHwMCKJzL/wfiPxotP ZbSoMlb9vKxyitqAqllsHDhUUbo4EBdJovRFK6OwV7Q1tK61lkVXNG+4qB+AxhQappzv MYoFv4fS2LCO+Wf3MMY51Iwp8OtA7514yPlZ1Ze6VmK/NNU5TFFveUjXFxLkR80/XUtY CG0q/FAzxfLka4SC31BXtvgfSzRmJLmr1IDZiKK1D093Y4BbDxgtYAXLKM3nsulDtMx/ z0SQ== X-Gm-Message-State: APjAAAXZYCW7y0xj8dYhKW64vVBslP4iGFSEo5ig9cOXsRn4ivVY4Rdu +hlGWjMT89Qw2lWbcItyg5gR7lNPlg== X-Google-Smtp-Source: APXvYqxwG1BIh3uaWH9/uBhglCyIZna04rOFiYnQjYK4V6iOVjEeNY/EKFrBe4zpTw+l6++76ioVoQ== X-Received: by 2002:a17:902:2d01:: with SMTP id o1mr70151990plb.155.1554199322087; Tue, 02 Apr 2019 03:02:02 -0700 (PDT) Received: from lithium.mbobrowski.org ([103.230.158.220]) by smtp.gmail.com with ESMTPSA id 17sm21358197pfw.65.2019.04.02.03.02.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 02 Apr 2019 03:02:01 -0700 (PDT) From: Amir Goldstein X-Google-Original-From: Amir Goldstein Date: Tue, 2 Apr 2019 21:01:55 +1100 To: ltp@lists.linux.it Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-Virus-Scanned: clamav-milter 0.99.2 at in-5.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.1 required=7.0 tests=DKIM_SIGNED,DKIM_VALID autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-5.smtp.seeweb.it Cc: jack@suse.cz Subject: [LTP] [PATCH 1/4] syscalls/fanotify01: add FAN_REPORT_FID test cases X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.18 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" For now, only check that event->fd is FAN_NOFD. On old kernels the new test with result in TCONF. tmpfs doesn't support FAN_REPORT_FID (it has zero fsid), so use a blockdev filesystem. It is better to use a blockdev filesystem for the rest of the test cases anyway to get more diversity in testing. Signed-off-by: Amir Goldstein --- testcases/kernel/syscalls/fanotify/fanotify.h | 3 ++ testcases/kernel/syscalls/fanotify/fanotify01.c | 41 +++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/testcases/kernel/syscalls/fanotify/fanotify.h b/testcases/kernel/syscalls/fanotify/fanotify.h index e5cc59232..14654b7c7 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify.h +++ b/testcases/kernel/syscalls/fanotify/fanotify.h @@ -57,6 +57,9 @@ static long fanotify_mark(int fd, unsigned int flags, uint64_t mask, #ifndef FAN_REPORT_TID #define FAN_REPORT_TID 0x00000100 #endif +#ifndef FAN_REPORT_FID +#define FAN_REPORT_FID 0x00000200 +#endif #ifndef FAN_MARK_INODE #define FAN_MARK_INODE 0 diff --git a/testcases/kernel/syscalls/fanotify/fanotify01.c b/testcases/kernel/syscalls/fanotify/fanotify01.c index 9b590746e..44966dce5 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify01.c +++ b/testcases/kernel/syscalls/fanotify/fanotify01.c @@ -37,18 +37,37 @@ static struct tcase { const char *tname; struct fanotify_mark_type mark; + unsigned int init_flags; } tcases[] = { { "inode mark events", INIT_FANOTIFY_MARK_TYPE(INODE), + FAN_CLASS_NOTIF }, { "mount mark events", INIT_FANOTIFY_MARK_TYPE(MOUNT), + FAN_CLASS_NOTIF }, { "filesystem mark events", INIT_FANOTIFY_MARK_TYPE(FILESYSTEM), + FAN_CLASS_NOTIF + }, + { + "inode mark events (FAN_REPORT_FID)", + INIT_FANOTIFY_MARK_TYPE(INODE), + FAN_CLASS_NOTIF|FAN_REPORT_FID + }, + { + "mount mark events (FAN_REPORT_FID)", + INIT_FANOTIFY_MARK_TYPE(MOUNT), + FAN_CLASS_NOTIF|FAN_REPORT_FID + }, + { + "filesystem mark events (FAN_REPORT_FID)", + INIT_FANOTIFY_MARK_TYPE(FILESYSTEM), + FAN_CLASS_NOTIF|FAN_REPORT_FID }, }; @@ -69,7 +88,18 @@ static void test_fanotify(unsigned int n) tst_res(TINFO, "Test #%d: %s", n, tc->tname); - fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF, O_RDONLY); + fd_notify = fanotify_init(tc->init_flags, O_RDONLY); + if (fd_notify < 0) { + if (errno == EINVAL && + (tc->init_flags & FAN_REPORT_FID)) { + tst_res(TCONF, + "FAN_REPORT_FID not supported in kernel?"); + return; + } + tst_brk(TBROK | TERRNO, + "fanotify_init (0x%x, O_RDONLY) " + "failed", tc->init_flags); + } if (fanotify_mark(fd_notify, FAN_MARK_ADD | mark->flag, FAN_ACCESS | FAN_MODIFY | FAN_CLOSE | FAN_OPEN, @@ -265,7 +295,8 @@ static void test_fanotify(unsigned int n) (unsigned)getpid(), event->fd); } else { - if (event->fd == -2) + if (event->fd == -2 || (event->fd == FAN_NOFD && + (tc->init_flags & FAN_REPORT_FID))) goto pass; ret = read(event->fd, buf, BUF_SIZE); if (ret != (int)strlen(fname)) { @@ -327,6 +358,12 @@ pass: static void setup(void) { + int fd; + + /* Check for kernel fanotify support */ + fd = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF, O_RDONLY); + SAFE_CLOSE(fd); + sprintf(fname, MOUNT_PATH"/tfile_%d", getpid()); SAFE_FILE_PRINTF(fname, "1"); } From patchwork Tue Apr 2 10:02:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Bobrowski X-Patchwork-Id: 1074166 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=lists.linux.it (client-ip=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=mbobrowski.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=mbobrowski-org.20150623.gappssmtp.com header.i=@mbobrowski-org.20150623.gappssmtp.com header.b="GyQ4TBpm"; dkim-atps=neutral Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 44YPs13LwFz9sSy for ; Tue, 2 Apr 2019 21:02:29 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id BA4873EA7D9 for ; Tue, 2 Apr 2019 12:02:26 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-2.smtp.seeweb.it (in-2.smtp.seeweb.it [217.194.8.2]) by picard.linux.it (Postfix) with ESMTP id 959283EA7D9 for ; Tue, 2 Apr 2019 12:02:24 +0200 (CEST) Received: from mail-pg1-x543.google.com (mail-pg1-x543.google.com [IPv6:2607:f8b0:4864:20::543]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by in-2.smtp.seeweb.it (Postfix) with ESMTPS id 5BFB960035C for ; Tue, 2 Apr 2019 12:02:23 +0200 (CEST) Received: by mail-pg1-x543.google.com with SMTP id j26so6314016pgl.5 for ; Tue, 02 Apr 2019 03:02:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mbobrowski-org.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=OVS93Hda4pA9xuqvBz1wBLoxvnl85DVjwNmgRPUOdXA=; b=GyQ4TBpmH5Wq42Ma+L5aBoR/K4QSQpF6zo+aH/W+wQ1oAhPFJiowLOZL/4qjDB/RBa oGoAoKA222uy5qM6Vr5yvWUGx7w1biKKdYJC9uBpWNKWzgpUlAmXPiMl2AyM9/XGHudY V7u0jGGX62q64IQOdSrfeyqYSJ0CmiSVUbFzJ5dBSj2Hds2qtkMFZP5/wfT1/n3U963v R5FpQm/jxbfe5S9SZCw8s5mtdBfy485ahh0dE5TF8qdCDss1DcRMxnxhp/2R3N5YAnW8 KZoUdILLTIJ06MlVSdGUFb64PoAx21Pi/aLqOABqjtpF9syUOi2PbW/2I+lJpyTKpXMg vrAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=OVS93Hda4pA9xuqvBz1wBLoxvnl85DVjwNmgRPUOdXA=; b=pyLp17EUyVvfWNgHdrFheKsYlLIUzfVoyQ0GJmOOSc51OmIOZYzG6xUh8Nx7RpaqYY rD/kJTgMmDidl72VmL0nsKDZJvrXeKPNi2Pnn5SAME5F9icgW2j3K/5vCY+5SVSHWPdl t6EuJhvPHNy4pNLbe6jIwIzU316Qt5zfLAaRuwjsbo6SypwBPxP2JULuQdHPapK/qmpf ieeMjYN4obClynIneRLTKPqPiyqf0xFNk10DZcmNwcPcscuzfTQxOkleV5wGCU4jHH5O +zH5NnzE964sB6svgjWfklY+7jpEqg4CRXfwXz2JKAA+6MnPvqLxmcDN0/Bhhi8PKaqU pcYA== X-Gm-Message-State: APjAAAUl61n9n8K+Ah1e8g9rsjBc8RUlBDl6VnEsTMlm8xLGi1rTtlfn 5R7UKDMzsrFCEZ3byI4CVur6GLpowA== X-Google-Smtp-Source: APXvYqwu82E2VLGicpIcXsNT/4UtEKyt9IR+m/ldKja1vaEEIkJiKMDjMBrSMEF2PzprAAPb3MM7Cw== X-Received: by 2002:a63:c10d:: with SMTP id w13mr28430887pgf.311.1554199341292; Tue, 02 Apr 2019 03:02:21 -0700 (PDT) Received: from lithium.mbobrowski.org ([103.230.158.220]) by smtp.gmail.com with ESMTPSA id n21sm618781pfb.42.2019.04.02.03.02.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 02 Apr 2019 03:02:20 -0700 (PDT) Date: Tue, 2 Apr 2019 21:02:15 +1100 From: Matthew Bobrowski To: ltp@lists.linux.it Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-Virus-Scanned: clamav-milter 0.99.2 at in-2.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.1 required=7.0 tests=DKIM_SIGNED,DKIM_VALID autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-2.smtp.seeweb.it Cc: jack@suse.cz Subject: [LTP] [PATCH 2/4] syscalls/fanotify13: new test to verify FAN_REPORT_FID functionality X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.18 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Newly defined test file to validate the fanotify FAN_REPORT_FID functionality. Signed-off-by: Matthew Bobrowski Reviewed-by: Amir Goldstein --- testcases/kernel/syscalls/fanotify/.gitignore | 1 + testcases/kernel/syscalls/fanotify/fanotify.h | 19 +- testcases/kernel/syscalls/fanotify/fanotify13.c | 329 ++++++++++++++++++++++++ 3 files changed, 346 insertions(+), 3 deletions(-) create mode 100644 testcases/kernel/syscalls/fanotify/fanotify13.c diff --git a/testcases/kernel/syscalls/fanotify/.gitignore b/testcases/kernel/syscalls/fanotify/.gitignore index 4256b8cd3..16bdd99e5 100644 --- a/testcases/kernel/syscalls/fanotify/.gitignore +++ b/testcases/kernel/syscalls/fanotify/.gitignore @@ -10,4 +10,5 @@ /fanotify10 /fanotify11 /fanotify12 +/fanotify13 /fanotify_child diff --git a/testcases/kernel/syscalls/fanotify/fanotify.h b/testcases/kernel/syscalls/fanotify/fanotify.h index 14654b7c7..450f3829d 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify.h +++ b/testcases/kernel/syscalls/fanotify/fanotify.h @@ -57,9 +57,6 @@ static long fanotify_mark(int fd, unsigned int flags, uint64_t mask, #ifndef FAN_REPORT_TID #define FAN_REPORT_TID 0x00000100 #endif -#ifndef FAN_REPORT_FID -#define FAN_REPORT_FID 0x00000200 -#endif #ifndef FAN_MARK_INODE #define FAN_MARK_INODE 0 @@ -89,6 +86,22 @@ struct fanotify_mark_type { const char * name; }; +#ifndef FAN_REPORT_FID +#define FAN_REPORT_FID 0x00000200 + +struct fanotify_event_info_header { + uint8_t info_type; + uint8_t pad; + uint16_t len; +}; + +struct fanotify_event_info_fid { + struct fanotify_event_info_header hdr; + __kernel_fsid_t fsid; + unsigned char handle[0]; +}; +#endif + #define INIT_FANOTIFY_MARK_TYPE(t) \ { FAN_MARK_ ## t, "FAN_MARK_" #t } diff --git a/testcases/kernel/syscalls/fanotify/fanotify13.c b/testcases/kernel/syscalls/fanotify/fanotify13.c new file mode 100644 index 000000000..fb49df5f9 --- /dev/null +++ b/testcases/kernel/syscalls/fanotify/fanotify13.c @@ -0,0 +1,329 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2018 Matthew Bobrowski. All Rights Reserved. + * + * Started by Matthew Bobrowski + * + * DESCRIPTION + * Validate that the values returned within an event when + * FAN_REPORT_FID is specified matches those that are obtained via + * explicit invocation to system calls statfs(2) and + * name_to_handle_at(2). + */ +#define _GNU_SOURCE +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include "tst_test.h" +#include "fanotify.h" + +#if defined(HAVE_SYS_FANOTIFY_H) +#include + +#define PATH_LEN 128 +#define BUF_SIZE 256 +#define DIR_ONE "dir_one" +#define FILE_ONE "file_one" +#define FILE_TWO "file_two" +#define MOUNT_PATH "mntpoint" +#define EVENT_MAX ARRAY_SIZE(objects) +#define DIR_PATH_ONE MOUNT_PATH"/"DIR_ONE +#define FILE_PATH_ONE MOUNT_PATH"/"FILE_ONE +#define FILE_PATH_TWO MOUNT_PATH"/"FILE_TWO + +struct event_t { + unsigned long long expected_mask; + __kernel_fsid_t fsid; + struct file_handle handle; + char buf[MAX_HANDLE_SZ]; +}; + +static struct object_t { + const char *path; + int is_dir; +} objects[] = { + {FILE_PATH_ONE, 0}, + {FILE_PATH_TWO, 0}, + {DIR_PATH_ONE, 1} +}; + +static struct test_case_t { + struct fanotify_mark_type mark; + unsigned long long mask; +} test_cases[] = { + { + INIT_FANOTIFY_MARK_TYPE(INODE), + FAN_OPEN | FAN_CLOSE_NOWRITE + }, + { + INIT_FANOTIFY_MARK_TYPE(INODE), + FAN_OPEN | FAN_CLOSE_NOWRITE | FAN_ONDIR + }, + { + INIT_FANOTIFY_MARK_TYPE(MOUNT), + FAN_OPEN | FAN_CLOSE_NOWRITE + }, + { + INIT_FANOTIFY_MARK_TYPE(MOUNT), + FAN_OPEN | FAN_CLOSE_NOWRITE | FAN_ONDIR + }, + { + INIT_FANOTIFY_MARK_TYPE(FILESYSTEM), + FAN_OPEN | FAN_CLOSE_NOWRITE + }, + { + INIT_FANOTIFY_MARK_TYPE(FILESYSTEM), + FAN_OPEN | FAN_CLOSE_NOWRITE | FAN_ONDIR + } +}; + +static int fanotify_fd; +static char events_buf[BUF_SIZE]; +static struct event_t event_set[EVENT_MAX]; + +static void create_objects(void) +{ + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(objects); i++) { + if (objects[i].is_dir) + SAFE_MKDIR(objects[i].path, 0755); + else + SAFE_FILE_PRINTF(objects[i].path, "0"); + } +} + +static void do_setup(void) +{ + int fd; + + /* Check for kernel fanotify support */ + fd = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF, O_RDONLY); + SAFE_CLOSE(fd); + + /* Create file and directories for testing */ + create_objects(); +} + +static void get_object_stats(struct test_case_t *tc) +{ + int mount_id; + unsigned int i; + struct statfs stats; + + for (i = 0; i < ARRAY_SIZE(objects); i++) { + if (statfs(objects[i].path, &stats) == -1) + tst_brk(TBROK | TERRNO, + "statfs(%s, ...) failed", objects[i].path); + memcpy(&event_set[i].fsid, &stats.f_fsid, + sizeof(stats.f_fsid)); + + event_set[i].handle.handle_bytes = MAX_HANDLE_SZ; + if (name_to_handle_at(AT_FDCWD, objects[i].path, + &event_set[i].handle, + &mount_id, 0) == -1) { + if (errno == EOPNOTSUPP) { + tst_res(TCONF, + "filesystem %s does not support file " + "handles", + tst_device->fs_type); + } + tst_brk(TBROK | TERRNO, + "name_to_handle_at(AT_FDCWD, %s, ...) failed", + objects[i].path); + } + + event_set[i].expected_mask = tc->mask; + if (!objects[i].is_dir) + event_set[i].expected_mask &= ~FAN_ONDIR; + } +} + +static int setup_marks(unsigned int fd, struct test_case_t *tc) +{ + unsigned int i; + struct fanotify_mark_type *mark = &tc->mark; + + for (i = 0; i < ARRAY_SIZE(objects); i++) { + if (fanotify_mark(fd, FAN_MARK_ADD | mark->flag, tc->mask, + AT_FDCWD, objects[i].path) == -1) { + if (errno == EINVAL && + mark->flag & FAN_MARK_FILESYSTEM) { + tst_res(TCONF, + "FAN_MARK_FILESYSTEM not supported by " + "kernel"); + return 1; + } else if (errno == ENODEV && + !event_set[i].fsid.val[0] && + !event_set[i].fsid.val[1]) { + tst_res(TCONF, + "FAN_REPORT_FID not supported on " + "filesystem type %s", + tst_device->fs_type); + return 1; + } + tst_brk(TBROK | TERRNO, + "fanotify_mark(%d, FAN_MARK_ADD, FAN_OPEN, " + "AT_FDCWD, %s) failed", + fanotify_fd, objects[i].path); + } + } + return 0; +} + +static void do_test(unsigned int number) +{ + unsigned int i; + int len, fds[ARRAY_SIZE(objects)]; + + struct file_handle *event_file_handle; + struct fanotify_event_metadata *metadata; + struct fanotify_event_info_fid *event_fid; + struct test_case_t *tc = &test_cases[number]; + struct fanotify_mark_type *mark = &tc->mark; + + tst_res(TINFO, + "Test #%d: FAN_REPORT_FID with mark flag: %s", + number, mark->name); + + /* Gets the filesystem fsid and file handle for each object */ + get_object_stats(tc); + + fanotify_fd = fanotify_init(FAN_CLASS_NOTIF | FAN_REPORT_FID, O_RDONLY); + if (fanotify_fd == -1) { + if (errno == EINVAL) { + tst_res(TCONF, + "FAN_REPORT_FID not supported by kernel"); + return; + } + tst_brk(TBROK | TERRNO, + "fanotify_init(FAN_CLASS_NOTIF | FAN_REPORT_FID, " + "O_RDONLY) failed"); + } + + /* Place marks on a set of objects */ + if (setup_marks(fanotify_fd, tc) != 0) + return; + + /* Generate sequence of FAN_OPEN events on objects */ + for (i = 0; i < ARRAY_SIZE(objects); i++) + fds[i] = SAFE_OPEN(objects[i].path, O_RDONLY); + + /* Generate sequence of FAN_CLOSE_NOWRITE events on objects. Each + * FAN_CLOSE_NOWRITE event is expected to be merged with its + * respective FAN_OPEN event that was performed on the same object + */ + for (i = 0; i < ARRAY_SIZE(objects); i++) { + if (fds[i] > 0) + SAFE_CLOSE(fds[i]); + } + + /* Read events from event queue */ + len = SAFE_READ(0, fanotify_fd, events_buf, BUF_SIZE); + + /* Iterate over event queue */ + for (i = 0, metadata = (struct fanotify_event_metadata *) events_buf; + FAN_EVENT_OK(metadata, len); + metadata = FAN_EVENT_NEXT(metadata, len), i++) { + event_fid = (struct fanotify_event_info_fid *) (metadata + 1); + event_file_handle = (struct file_handle *) event_fid->handle; + + /* File descriptor is redundant with FAN_REPORT_FID */ + if (metadata->fd != FAN_NOFD) + tst_res(TFAIL, + "Unexpectedly received file descriptor %d in " + "event. Expected to get FAN_NOFD(%d)", + metadata->fd, FAN_NOFD); + + /* Ensure that the correct mask has been reported in event */ + if (metadata->mask != event_set[i].expected_mask) + tst_res(TFAIL, + "Unexpected mask received: %llx (expected: " + "%llx) in event", + metadata->mask, + event_set[i].expected_mask); + + /* Verify handle_bytes returned in event */ + if (event_file_handle->handle_bytes + != event_set[i].handle.handle_bytes) { + tst_res(TFAIL, + "handle_bytes (%x) returned in event does not " + "equal to handle_bytes (%x) returned in " + "name_to_handle_at(2)", + event_file_handle->handle_bytes, + event_set[i].handle.handle_bytes); + continue; + } + + /* Verify handle_type returned in event */ + if (event_file_handle->handle_type != + event_set[i].handle.handle_type) { + tst_res(TFAIL, + "handle_type (%x) returned in event does not " + "equal to handle_type (%x) returned in " + "name_to_handle_at(2)", + event_file_handle->handle_type, + event_set[i].handle.handle_type); + continue; + } + + /* Verify file identifier f_handle returned in event */ + if (memcmp(event_file_handle->f_handle, + event_set[i].handle.f_handle, + event_set[i].handle.handle_bytes) != 0) { + tst_res(TFAIL, + "event_file_handle->f_handle does not match " + "event_set[i].handle.f_handle returned in " + "name_to_handle_at(2)"); + continue; + } + + /* Verify filesystem ID fsid returned in event */ + if (memcmp(&event_fid->fsid, &event_set[i].fsid, + sizeof(event_set[i].fsid)) != 0) { + tst_res(TFAIL, + "event_fid.fsid != stat.f_fsid that was " + "obtained via statfs(2)"); + continue; + } + + tst_res(TPASS, + "got event: mask=%llx, pid=%d, fid=%x.%x.%lx values " + "returned in event match those returned in statfs(2) " + "and name_to_handle_at(2)", + metadata->mask, + getpid(), + event_fid->fsid.val[0], + event_fid->fsid.val[1], + *(unsigned long *) event_file_handle->f_handle); + } +} + +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, + .needs_root = 1, + .needs_tmpdir = 1, + .mount_device = 1, + .mntpoint = MOUNT_PATH, + .all_filesystems = 1 +}; + +#else + TST_TEST_CONF("System does not have required fanotify support"); +#endif From patchwork Tue Apr 2 10:02:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Bobrowski X-Patchwork-Id: 1074167 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=lists.linux.it (client-ip=213.254.12.146; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=mbobrowski.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=mbobrowski-org.20150623.gappssmtp.com header.i=@mbobrowski-org.20150623.gappssmtp.com header.b="mSRsZ/jC"; dkim-atps=neutral Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 44YPsK3xTsz9sSy for ; Tue, 2 Apr 2019 21:02:45 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id C89DE3EA868 for ; Tue, 2 Apr 2019 12:02:41 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-6.smtp.seeweb.it (in-6.smtp.seeweb.it [IPv6:2001:4b78:1:20::6]) by picard.linux.it (Postfix) with ESMTP id BDA7C3EA7DA for ; Tue, 2 Apr 2019 12:02:38 +0200 (CEST) Received: from mail-pf1-x444.google.com (mail-pf1-x444.google.com [IPv6:2607:f8b0:4864:20::444]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by in-6.smtp.seeweb.it (Postfix) with ESMTPS id 24B421401231 for ; Tue, 2 Apr 2019 12:02:38 +0200 (CEST) Received: by mail-pf1-x444.google.com with SMTP id 10so6128074pfo.5 for ; Tue, 02 Apr 2019 03:02:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mbobrowski-org.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=GO+5xvSciF47IhjaWhHgr+gMKl55wPr0SafjcVoxt9I=; b=mSRsZ/jCV5ibrVcq0OARpGw3St04azbQajNVBzSqcq/zpLqs4MgC+QZph5GCO0wW2j C2Eq56FVY0DkH6LwmotjMrkFy/VJU36RlXUvNrY74sANpUvgms88CtEq5ltDOW/nfzo8 V6InsueLK7jEB19+WvamJjQ+mfI90zbNpUchUdaIDX6VwL2yaKBPong00ahyrseGr4re 1hIcvCCmC8zytZmShfFUtKPCnRrBsu7cKHuPf8ecv1R6Lazu+I32+Rqjq+DFazkhp9OA EKEbwDZHOBbhHy3dfk5m5WO34RKUc7XX0fjoavXDLbhxNDALi/qRWxqpO8PDSUiodw+y 0eaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=GO+5xvSciF47IhjaWhHgr+gMKl55wPr0SafjcVoxt9I=; b=HYzL3dL0nYdU5oK23+WG57/2tbzbhaO17sbI7Lmw9YEmse7XUOQLSAa4gNW82nuufX 6r/7KqxOZWr7v8BKwz6M5iZ+oBF47cgBGfo/fi0FhQKWhjWEoRxCRqJIFOb+Vku+mLVi YuLyx/jdhkqacM6X4JaZzKqTEnTlR9MiFymkIGys3/iyKmCoqF2/xoJLegxrNu3LShbB Qu+L55t5va6ebedUvrDBFkCpsCDXq6myH1qOB4ccWr2fwK/3c7zuu9Jr1bu6JovHOzVi /7hr4raQjhRQfAKt3MrtWA5Xa0WKVtsNMla3X/yzWpEd1mDkk1YA16YeMG8thGXPoO3S e/Ug== X-Gm-Message-State: APjAAAWewt6CAgZeYX7jQgpSh8L4VvGbOPDQ7XuzjsO9bwUOgRhH36vi UytBrpvw2wH6+YStYLC5TcD4QqKjWA== X-Google-Smtp-Source: APXvYqxeUe8jncmjm8Awc1JUo3eraEWJ2t0dsNCdiNNdbAMZ2aMWoQlZU9ofyAIPZSULJ7aOvPrUxw== X-Received: by 2002:a63:3fc3:: with SMTP id m186mr34039656pga.151.1554199356239; Tue, 02 Apr 2019 03:02:36 -0700 (PDT) Received: from lithium.mbobrowski.org ([103.230.158.220]) by smtp.gmail.com with ESMTPSA id p7sm13664519pfp.70.2019.04.02.03.02.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 02 Apr 2019 03:02:35 -0700 (PDT) Date: Tue, 2 Apr 2019 21:02:30 +1100 From: Matthew Bobrowski To: ltp@lists.linux.it Message-ID: <39f154a0b4e9f9180d120f204a42ad99c8a058d8.1554197461.git.mbobrowski@mbobrowski.org> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-Virus-Scanned: clamav-milter 0.99.2 at in-6.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.1 required=7.0 tests=DKIM_SIGNED,DKIM_VALID autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-6.smtp.seeweb.it Cc: jack@suse.cz Subject: [LTP] [PATCH 3/4] syscalls/fanotify14: new test to validate FAN_REPORT_FID interface return values X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.18 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" New test file has been introduced to validate that the fanotify interface returns the correct error values upon specifying invalid flags and masks in conjunction with FAN_REPORT_FID. Signed-off-by: Matthew Bobrowski Reviewed-by: Amir Goldstein --- testcases/kernel/syscalls/fanotify/.gitignore | 1 + testcases/kernel/syscalls/fanotify/fanotify.h | 25 ++++ testcases/kernel/syscalls/fanotify/fanotify14.c | 170 ++++++++++++++++++++++++ 3 files changed, 196 insertions(+) create mode 100644 testcases/kernel/syscalls/fanotify/fanotify14.c diff --git a/testcases/kernel/syscalls/fanotify/.gitignore b/testcases/kernel/syscalls/fanotify/.gitignore index 16bdd99e5..bf389c96a 100644 --- a/testcases/kernel/syscalls/fanotify/.gitignore +++ b/testcases/kernel/syscalls/fanotify/.gitignore @@ -11,4 +11,5 @@ /fanotify11 /fanotify12 /fanotify13 +/fanotify14 /fanotify_child diff --git a/testcases/kernel/syscalls/fanotify/fanotify.h b/testcases/kernel/syscalls/fanotify/fanotify.h index 450f3829d..46a4b6289 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify.h +++ b/testcases/kernel/syscalls/fanotify/fanotify.h @@ -64,6 +64,31 @@ static long fanotify_mark(int fd, unsigned int flags, uint64_t mask, #ifndef FAN_MARK_FILESYSTEM #define FAN_MARK_FILESYSTEM 0x00000100 #endif +/* New dirent event masks */ +#ifndef FAN_ATTRIB +#define FAN_ATTRIB 0x00000004 +#endif +#ifndef FAN_MOVED_FROM +#define FAN_MOVED_FROM 0x00000040 +#endif +#ifndef FAN_MOVED_TO +#define FAN_MOVED_TO 0x00000080 +#endif +#ifndef FAN_CREATE +#define FAN_CREATE 0x00000100 +#endif +#ifndef FAN_DELETE +#define FAN_DELETE 0x00000200 +#endif +#ifndef FAN_DELETE_SELF +#define FAN_DELETE_SELF 0x00000400 +#endif +#ifndef FAN_MOVE_SELF +#define FAN_MOVE_SELF 0x00000800 +#endif +#ifndef FAN_MOVE +#define FAN_MOVE (FAN_MOVED_FROM | FAN_MOVED_TO) +#endif #ifndef FAN_OPEN_EXEC #define FAN_OPEN_EXEC 0x00001000 #endif diff --git a/testcases/kernel/syscalls/fanotify/fanotify14.c b/testcases/kernel/syscalls/fanotify/fanotify14.c new file mode 100644 index 000000000..b80eac99f --- /dev/null +++ b/testcases/kernel/syscalls/fanotify/fanotify14.c @@ -0,0 +1,170 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2018 Matthew Bobrowski. All Rights Reserved. + * + * Started by Matthew Bobrowski + * + * DESCRIPTION + * This test file has been designed to ensure that the fanotify + * system calls fanotify_init(2) and fanotify_mark(2) return the + * correct error code to the calling process when an invalid flag or + * mask value has been specified in conjunction with FAN_REPORT_FID. + */ + +#include "tst_test.h" +#include "fanotify.h" + +#include + +#if defined(HAVE_SYS_FANOTIFY_H) +#include + +#define MNTPOINT "mntpoint" +#define FILE1 MNTPOINT"/file1" + +/* List of inode events that are only available when notification group is + * set to report fid + */ +#define INODE_EVENTS (FAN_ATTRIB | FAN_CREATE | FAN_DELETE | FAN_MOVE | \ + FAN_DELETE_SELF | FAN_MOVE_SELF) + +static int fanotify_fd; + +/* Each test case has been designed in a manner whereby the values defined + * within should result in the interface to return an error to the calling + * process. + */ +static struct test_case_t { + unsigned int init_flags; + unsigned int mark_flags; + unsigned long long mask; +} test_cases[] = { + { + FAN_CLASS_CONTENT | FAN_REPORT_FID, 0, 0 + }, + { + FAN_CLASS_PRE_CONTENT | FAN_REPORT_FID, 0, 0 + }, + { + FAN_CLASS_NOTIF, 0, INODE_EVENTS + }, + { + FAN_CLASS_NOTIF | FAN_REPORT_FID, FAN_MARK_MOUNT, INODE_EVENTS + } +}; + +static void do_setup(void) +{ + int fd; + + /* Check for kernel fanotify support */ + fd = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF, O_RDONLY); + SAFE_CLOSE(fd); + + /* Create temporary test file to place marks on */ + SAFE_FILE_PRINTF(FILE1, "0"); +} + +static void do_test(unsigned int number) +{ + int ret; + struct test_case_t *tc = &test_cases[number]; + + fanotify_fd = fanotify_init(tc->init_flags, O_RDONLY); + + if (fanotify_fd < 0) { + /* EINVAL is to be returned to the calling process when + * an invalid notification class is specified in + * conjunction with FAN_REPORT_FID + */ + if (errno == EINVAL) { + tst_res(TPASS, + "fanotify_fd=%d, fanotify_init(%x, O_RDONLY) " + "failed with error EINVAL as expected", + fanotify_fd, + tc->init_flags); + return; + } + tst_brk(TBROK | TERRNO, + "fanotify_fd=%d, fanotify_init(%x, O_RDONLY) failed", + fanotify_fd, + tc->init_flags); + } + + /* A test case with a mask set to zero indicate that they've been + * specifically designed to test and fail on the fanotify_init() + * system call. + */ + if (tc->mask == 0) { + tst_res(TFAIL, + "fanotify_fd=%d fanotify_init(%x, O_RDONLY) " + "unexpectedly succeeded when tests with mask 0 are" + "expected to fail when calling fanotify_init()", + fanotify_fd, + tc->init_flags); + return; + } + + ret = fanotify_mark(fanotify_fd, FAN_MARK_ADD | tc->mark_flags, + tc->mask, AT_FDCWD, FILE1); + + if (ret < 0) { + /* EINVAL is to be returned to the calling process when + * attempting to use INODE_EVENTS without FAN_REPORT_FID + * specified on the notification group, or using + * INODE_EVENTS with mark type FAN_MARK_MOUNT. + */ + if (errno == EINVAL) { + tst_res(TPASS, + "ret=%d, fanotify_mark(%d, FAN_MARK_ADD | %x, " + "%llx, AT_FDCWD, %s) failed with error EINVAL " + "as expected", + ret, + fanotify_fd, + tc->mark_flags, + tc->mask, + FILE1); + return; + } + tst_brk(TBROK | TERRNO, + "ret=%d, fanotify_mark(%d, FAN_MARK_ADD | %x, %llx, " + "AT_FDCWD, %s) failed", + ret, + fanotify_fd, + tc->mark_flags, + tc->mask, + FILE1); + } + + tst_res(TFAIL, + "fanotify_fd=%d, ret=%d, fanotify_init(%x, O_RDONLY) and " + "fanotify_mark(%d, FAN_MARK_ADD | %x, %llx, AT_FDCWD, %s) did " + "not return any errors as expected", + fanotify_fd, + ret, + tc->init_flags, + fanotify_fd, + tc->mark_flags, + tc->mask, + FILE1); +} + +static void do_cleanup(void) +{ + if (fanotify_fd > 0) + SAFE_CLOSE(fanotify_fd); +} + +static struct tst_test test = { + .needs_root = 1, + .setup = do_setup, + .test = do_test, + .tcnt = ARRAY_SIZE(test_cases), + .cleanup = do_cleanup, + .mount_device = 1, + .mntpoint = MNTPOINT +}; + +#else + TST_TEST_CONF("System does not have required fanotify support") +#endif From patchwork Tue Apr 2 10:02:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Bobrowski X-Patchwork-Id: 1074183 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=lists.linux.it (client-ip=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=mbobrowski.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=mbobrowski-org.20150623.gappssmtp.com header.i=@mbobrowski-org.20150623.gappssmtp.com header.b="h2t/Pzxl"; dkim-atps=neutral Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 44YPsc3yfkz9sSy for ; Tue, 2 Apr 2019 21:03:00 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id E2E433EA887 for ; Tue, 2 Apr 2019 12:02:57 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-6.smtp.seeweb.it (in-6.smtp.seeweb.it [217.194.8.6]) by picard.linux.it (Postfix) with ESMTP id 80B893EA7DA for ; Tue, 2 Apr 2019 12:02:55 +0200 (CEST) Received: from mail-pg1-x542.google.com (mail-pg1-x542.google.com [IPv6:2607:f8b0:4864:20::542]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by in-6.smtp.seeweb.it (Postfix) with ESMTPS id 6218814002B5 for ; Tue, 2 Apr 2019 12:02:54 +0200 (CEST) Received: by mail-pg1-x542.google.com with SMTP id v12so6327796pgq.1 for ; Tue, 02 Apr 2019 03:02:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mbobrowski-org.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=LRaLG7JGOCdp0JRxS/QnfyozNyLBhCupPPiK87Lt8dw=; b=h2t/PzxlluwTl5R1uh6raI6W+nZ4pRGhxLClt7RIhcIVnJLDVpE2VAh9watXRorM0P 95dy7nqBz4mr4pwzk+L23vIRxOON4hoxPWtaNWlbXA3SicCNUgTCQVHMiy2yKiR+/SKI fif0nPVsGH2ZWwzKQkckTSNFgyY++ryB4ABlqi0AQzLiFIzGH7KBWCTm/oKBkG7KWYBX XairEnlpHrKVfjsjOW1lfNdQ9HktE55ZKdG0CcDTxGXZxYeOk7wQPOMsX72pioNVVO+d zYJDSuidcyJiFUZRHT+kiHZP+SzyvWUVbMOk+M5b0bQSTY6GVk6clRxwGU0hlhczF4js bY2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=LRaLG7JGOCdp0JRxS/QnfyozNyLBhCupPPiK87Lt8dw=; b=ZSEXTSsP5iHDxJ2QfGqhXWOWu183SY/xCnaYtcfSiD0byw7DRLSiEv6BcZbqyNoHxL LNeRi1kE+0wTQtDLeZHjTTAsr/hqTTDvjlYiQEx1KRX/aFE1nyHi4Hda3bu9E+Pq/dm5 OP2016d3e3nxX4KCKnj5GMCAeq3tTNm9CRhkvt4TFt53qWlAVdmL36PlEe58A2JV7ve/ LnlXwJFvubDpUpEjJgIqfoIFvD0yuLGc9Z5un+yhy+rwj1jmCR+HLn40i7OYZ2gDcMbR zLJUmIoXqDZFDbHbBI/wHKcEvl0BXNIWYg77KwW7CiORb6hb56/DTktHYUleDBt5CL1S ZcHA== X-Gm-Message-State: APjAAAWyOVlH9NFqPBIDB869gY6oQO2iYR0ekkNPU7bNfjfTyXM1xdu7 YBMs5G3pkzLGgt4mtUdGT2rLmf7I/A== X-Google-Smtp-Source: APXvYqwFh9+Wp6dikgWvBlWfV3dehyvo6Pr9T4TB2FXlRq0mqvQa6YL/HI6oB6soEseqFp94PP4ctQ== X-Received: by 2002:a63:243:: with SMTP id 64mr27472944pgc.214.1554199372463; Tue, 02 Apr 2019 03:02:52 -0700 (PDT) Received: from lithium.mbobrowski.org ([103.230.158.220]) by smtp.gmail.com with ESMTPSA id e4sm8134198pfh.146.2019.04.02.03.02.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 02 Apr 2019 03:02:51 -0700 (PDT) Date: Tue, 2 Apr 2019 21:02:46 +1100 From: Matthew Bobrowski To: ltp@lists.linux.it Message-ID: <2934be190caaaf4bb75caffd731d34711fbf1c61.1554197461.git.mbobrowski@mbobrowski.org> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-Virus-Scanned: clamav-milter 0.99.2 at in-6.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.1 required=7.0 tests=DKIM_SIGNED,DKIM_VALID autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-6.smtp.seeweb.it Cc: jack@suse.cz Subject: [LTP] [PATCH 4/4] syscalls/fanotify15: verify fid for dirent events X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.18 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" New test file that provides coverage for new dirent events. Signed-off-by: Matthew Bobrowski Reviewed-by: Amir Goldstein --- testcases/kernel/syscalls/fanotify/.gitignore | 1 + testcases/kernel/syscalls/fanotify/fanotify15.c | 256 ++++++++++++++++++++++++ 2 files changed, 257 insertions(+) create mode 100644 testcases/kernel/syscalls/fanotify/fanotify15.c diff --git a/testcases/kernel/syscalls/fanotify/.gitignore b/testcases/kernel/syscalls/fanotify/.gitignore index bf389c96a..68e4cc7aa 100644 --- a/testcases/kernel/syscalls/fanotify/.gitignore +++ b/testcases/kernel/syscalls/fanotify/.gitignore @@ -12,4 +12,5 @@ /fanotify12 /fanotify13 /fanotify14 +/fanotify15 /fanotify_child diff --git a/testcases/kernel/syscalls/fanotify/fanotify15.c b/testcases/kernel/syscalls/fanotify/fanotify15.c new file mode 100644 index 000000000..83062443b --- /dev/null +++ b/testcases/kernel/syscalls/fanotify/fanotify15.c @@ -0,0 +1,256 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2019 CTERA Networks. All Rights Reserved. + * + * Started by Amir Goldstein + * Modified by Matthew Bobrowski + * + * DESCRIPTION + * Test file that has been purposely designed to verify + * FAN_REPORT_FID functionality while using newly defined dirent + * events. + */ +#define _GNU_SOURCE +#include "config.h" + +#include +#include +#include +#include +#include + +#include "tst_test.h" +#include "fanotify.h" + +#if defined(HAVE_SYS_FANOTIFY_H) +#include + +#define BUF_SIZE 256 +#define EVENT_MAX 256 + +#define MOUNT_POINT "mntpoint" +#define TEST_DIR MOUNT_POINT"/test_dir" +#define DIR1 TEST_DIR"/dir1" +#define DIR2 TEST_DIR"/dir2" +#define FILE1 TEST_DIR"/file1" +#define FILE2 TEST_DIR"/file2" + +struct event_t { + unsigned long long mask; + __kernel_fsid_t fsid; + struct file_handle handle; + char buf[MAX_HANDLE_SZ]; +}; + +static int fanotify_fd; +static char events_buf[BUF_SIZE]; +static struct event_t event_set[EVENT_MAX]; + +static void do_setup(void) +{ + int fd; + + /* Check kernel for fanotify support */ + fd = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF, O_RDONLY); + SAFE_CLOSE(fd); + + fanotify_fd = fanotify_init(FAN_REPORT_FID, O_RDONLY); + if (fanotify_fd == -1) { + if (errno == EINVAL) + tst_brk(TCONF, + "FAN_REPORT_FID not supported in kernel"); + tst_brk(TBROK | TERRNO, + "fanotify_init(FAN_REPORT_FID, O_RDONLY) failed"); + } + + SAFE_MKDIR(TEST_DIR, 0755); +} + +static void get_fid_data(int i, const char *path) +{ + int mount_id; + struct statfs stats; + + if (statfs(path, &stats) == -1) + tst_brk(TBROK | TERRNO, + "statfs(%s, ...) failed", path); + memcpy(&event_set[i].fsid, &stats.f_fsid, sizeof(stats.f_fsid)); + + event_set[i].handle.handle_bytes = MAX_HANDLE_SZ; + if (name_to_handle_at(AT_FDCWD, path, &event_set[i].handle, + &mount_id, 0) == -1) { + if (errno == EOPNOTSUPP) { + tst_brk(TCONF, + "filesystem %s does not support file handles", + tst_device->fs_type); + } + tst_brk(TBROK | TERRNO, + "name_to_handle_at(AT_FDCWD, %s, ...) failed", + path); + } +} + +static void do_test(void) +{ + int i, fd, len, count = 0; + + struct file_handle *event_file_handle; + struct fanotify_event_metadata *metadata; + struct fanotify_event_info_fid *event_fid; + + if (fanotify_mark(fanotify_fd, FAN_MARK_ADD | FAN_MARK_FILESYSTEM, + FAN_CREATE | FAN_DELETE | FAN_ATTRIB | + FAN_MOVED_FROM | FAN_MOVED_TO | + FAN_DELETE_SELF | FAN_ONDIR, + AT_FDCWD, TEST_DIR) == -1) { + tst_brk(TBROK | TERRNO, + "fanotify_mark(%d, FAN_MARK_ADD, FAN_CREATE | " + "FAN_DELETE | FAN_MOVED_FROM | FAN_MOVED_TO | " + "FAN_DELETE_SELF | FAN_ONDIR, AT_FDCWD, %s) failed", + fanotify_fd, TEST_DIR); + } + + /* Generate a sequence of events */ + event_set[count].mask = FAN_CREATE | FAN_MOVED_FROM | FAN_MOVED_TO | \ + FAN_DELETE; + get_fid_data(count, TEST_DIR); + count++; + + fd = SAFE_CREAT(FILE1, 0644); + SAFE_CLOSE(fd); + + SAFE_RENAME(FILE1, FILE2); + + event_set[count].mask = FAN_ATTRIB | FAN_DELETE_SELF; + get_fid_data(count, FILE2); + count++; + + SAFE_UNLINK(FILE2); + + /* Generate a sequence of events on a directory. Subsequent events + * are merged, so it's required that we set FAN_ONDIR once in + * order to acknowledge that changes related to a subdirectory + * took place. Events on subdirectories are not merged with events + * on non-subdirectories. + */ + event_set[count].mask = FAN_ONDIR | FAN_CREATE | FAN_MOVED_FROM | \ + FAN_MOVED_TO | FAN_DELETE; + get_fid_data(count, TEST_DIR); + count++; + + SAFE_MKDIR(DIR1, 0755); + + SAFE_RENAME(DIR1, DIR2); + + event_set[count].mask = FAN_ONDIR | FAN_DELETE_SELF; + get_fid_data(count, DIR2); + count++; + + SAFE_RMDIR(DIR2); + + /* Read events from the event queue */ + len = SAFE_READ(0, fanotify_fd, events_buf, BUF_SIZE); + + /* Process each event in buffer */ + for (i = 0, metadata = (struct fanotify_event_metadata *) events_buf; + FAN_EVENT_OK(metadata, len); + metadata = FAN_EVENT_NEXT(metadata,len), i++) { + event_fid = (struct fanotify_event_info_fid *) (metadata + 1); + event_file_handle = (struct file_handle *) event_fid->handle; + + if (i >= count) { + tst_res(TFAIL, + "got unnecessary event: mask=%llx " + "pid=%u fd=%d", + (unsigned long long) metadata->mask, + metadata->pid, + metadata->fd); + metadata->mask = 0; + } else if (metadata->fd != FAN_NOFD) { + tst_res(TFAIL, + "Received unexpected file descriptor %d in " + "event. Expected to get FAN_NOFD(%d)", + metadata->fd, FAN_NOFD); + } else if (metadata->mask != event_set[i].mask) { + tst_res(TFAIL, + "Got event: mask=%llx (expected %llx) " + "pid=%u fd=%d", + (unsigned long long) metadata->mask, + event_set[i].mask, + (unsigned) metadata->pid, + metadata->fd); + } else if (metadata->pid != getpid()) { + tst_res(TFAIL, + "Got event: mask=%llx pid=%u " + "(expected %u) fd=%d", + (unsigned long long) metadata->mask, + (unsigned) metadata->pid, + (unsigned) getpid(), + metadata->fd); + } else if (event_file_handle->handle_bytes != + event_set[i].handle.handle_bytes) { + tst_res(TFAIL, + "Got event: handle_bytes (%x) returned in " + "event does not equal handle_bytes (%x) " + "retunred in name_to_handle_at(2)", + event_file_handle->handle_bytes, + event_set[i].handle.handle_bytes); + } else if (event_file_handle->handle_type != + event_set[i].handle.handle_type) { + tst_res(TFAIL, + "handle_type (%x) returned in event does not " + "equal to handle_type (%x) returned in " + "name_to_handle_at(2)", + event_file_handle->handle_type, + event_set[i].handle.handle_type); + } else if (memcmp(event_file_handle->f_handle, + event_set[i].handle.f_handle, + event_set[i].handle.handle_bytes) + != 0) { + tst_res(TFAIL, + "event_file_handle->f_handle does not match " + "handle.f_handle returned in " + "name_to_handle_at(2)"); + } else if (memcmp(&event_fid->fsid, &event_set[i].fsid, + sizeof(event_set[i].fsid)) != 0) { + tst_res(TFAIL, + "event_fid->fsid != stats.f_fsid that was " + "obtained via statfs(2)"); + } else { + tst_res(TPASS, + "Got event: mask=%llx, pid=%u, " + "fid=%x.%x.%lx values", + metadata->mask, + getpid(), + event_fid->fsid.val[0], + event_fid->fsid.val[1], + *(unsigned long *) + event_file_handle->f_handle); + } + } + + for (; i < count; i++) + tst_res(TFAIL, + "Didn't receive event: mask=%llx", + event_set[i].mask); +} + +static void do_cleanup(void) +{ + if (fanotify_fd > 0) + SAFE_CLOSE(fanotify_fd); +} + +static struct tst_test test = { + .needs_root = 1, + .needs_tmpdir = 1, + .mount_device = 1, + .mntpoint = MOUNT_POINT, + .setup = do_setup, + .test_all = do_test, + .cleanup = do_cleanup +}; + +#else + TST_TEST_CONF("System does not have required fanotify support"); +#endif