Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp510269pxb; Fri, 29 Oct 2021 14:19:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy5Q/ZWnO0ycfgIsUryawBXVJa1m+9z7DXpbxyAh7G5T5nQk14H67cLv/jN5e6CmdDFSrTP X-Received: by 2002:a17:907:2d2b:: with SMTP id gs43mr15712583ejc.193.1635542344185; Fri, 29 Oct 2021 14:19:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635542344; cv=none; d=google.com; s=arc-20160816; b=XmfPEUnkuk15eUm8Gh4KAjXIY48AaeAZ/JH9X6a2O1w/A63uLRbIMf6T+HpK1q/rSO 0fLdWkGIcLiO6yrCq33L7fboYqei4kQvwNa+Y5ZIkvo4YPkjO20dxWdwC9uvANp7NHHz iMdBk05CAMc3EvH1vMIfUuDCMdFmpeiEoTF3jXWHAa4tKQqWQcG0tYm6/a9pw0k0YKj1 FPfC9rR+sZOXuBewYo1NBktRcaMQhzV9EZvGEmVholkyWUuNmAJ5SH/YudA4lk2S3A3z YKGw6qT5xuqGdv9kYpHrvhs+iMPvCTDvcK3DqsJR/2sp76H5F911xH4JE2/XaGXgHBVM jbPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=VttA7WYRtKExzj9WjyBReJfavtz/7gu9CXPH4ALLW6I=; b=YNGyPCjrzu/XffDK7Jl9qen2qlPNPD7IhqeF9YBo9lceeaulFzIoqBstcm1Ta7ImRD 66vJWrkFz8CuR6AQ+wS3jF81uKROaiyytQ1PIRVPAd+AwpOrjt5WPp4pey9wceeO6FwQ zPfHPsuAZmThHj7nKWqkv351lHojoEYBrVCTVOvOKq5sGE3ITFngXqHAgzzoqEPSTEKq eyR3npFx7UCxGeBm+C0IY0RE70NF2pbcNYryG72a+R5YssnH9ofVOnj/a3Bm2pinEF95 36xExT2vbym/krh2K/UDxHLMTnJCJjk76V+EGATXKwG5Lx8kgRIePdYJenPujcFG8s/W /XEA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=collabora.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y15si14949095eda.144.2021.10.29.14.18.41; Fri, 29 Oct 2021 14:19:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231256AbhJ2VUm (ORCPT + 99 others); Fri, 29 Oct 2021 17:20:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231458AbhJ2VUl (ORCPT ); Fri, 29 Oct 2021 17:20:41 -0400 Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e3e3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5E0AC061570 for ; Fri, 29 Oct 2021 14:18:12 -0700 (PDT) Received: from localhost (unknown [IPv6:2804:14c:124:8a08::1002]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: krisman) by bhuna.collabora.co.uk (Postfix) with ESMTPSA id 2E3821F45C6F; Fri, 29 Oct 2021 22:18:10 +0100 (BST) From: Gabriel Krisman Bertazi To: jack@suse.com, amir73il@gmail.com, repnop@google.com Cc: ltp@lists.linux.it, khazhy@google.com, kernel@collabora.com, linux-ext4@vger.kernel.org, Gabriel Krisman Bertazi Subject: [PATCH v3 3/9] syscalls/fanotify21: Introduce FAN_FS_ERROR test Date: Fri, 29 Oct 2021 18:17:26 -0300 Message-Id: <20211029211732.386127-4-krisman@collabora.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211029211732.386127-1-krisman@collabora.com> References: <20211029211732.386127-1-krisman@collabora.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org fanotify21 is a new test validating the FAN_FS_ERROR file system error event. This adds some basic structure for the next patches and a single test of error reporting during filesystem abort The strategy for error reporting testing in fanotify21 goes like this: - Generate a broken filesystem - Start FAN_FS_ERROR monitoring group - Make the file system notice the error through ordinary operations - Observe the event generated Signed-off-by: Gabriel Krisman Bertazi --- Changes since v1: - Move defines to header file. - Move fanotify_mark(2) to do_test (Amir) - Merge abort test here --- testcases/kernel/syscalls/fanotify/.gitignore | 1 + testcases/kernel/syscalls/fanotify/fanotify.h | 3 + .../kernel/syscalls/fanotify/fanotify21.c | 141 ++++++++++++++++++ 3 files changed, 145 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 9554b16b196e..79ad184d578b 100644 --- a/testcases/kernel/syscalls/fanotify/.gitignore +++ b/testcases/kernel/syscalls/fanotify/.gitignore @@ -17,4 +17,5 @@ /fanotify17 /fanotify18 /fanotify19 +/fanotify21 /fanotify_child diff --git a/testcases/kernel/syscalls/fanotify/fanotify.h b/testcases/kernel/syscalls/fanotify/fanotify.h index 820073709571..99b898554ede 100644 --- a/testcases/kernel/syscalls/fanotify/fanotify.h +++ b/testcases/kernel/syscalls/fanotify/fanotify.h @@ -124,6 +124,9 @@ static inline int safe_fanotify_mark(const char *file, const int lineno, #ifndef FAN_OPEN_EXEC_PERM #define FAN_OPEN_EXEC_PERM 0x00040000 #endif +#ifndef FAN_FS_ERROR +#define FAN_FS_ERROR 0x00008000 +#endif /* Flags required for unprivileged user group */ #define FANOTIFY_REQUIRED_USER_INIT_FLAGS (FAN_REPORT_FID) diff --git a/testcases/kernel/syscalls/fanotify/fanotify21.c b/testcases/kernel/syscalls/fanotify/fanotify21.c new file mode 100644 index 000000000000..9ef687442b7c --- /dev/null +++ b/testcases/kernel/syscalls/fanotify/fanotify21.c @@ -0,0 +1,141 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2021 Collabora Ltd. + * + * Author: Gabriel Krisman Bertazi + * Based on previous work by Amir Goldstein + */ + +/*\ + * [Description] + * Check fanotify FAN_ERROR_FS events triggered by intentionally + * corrupted filesystems: + * + * - Generate a broken filesystem + * - Start FAN_FS_ERROR monitoring group + * - Make the file system notice the error through ordinary operations + * - Observe the event generated + */ + +#define _GNU_SOURCE +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include "tst_test.h" +#include +#include +#include + +#ifdef HAVE_SYS_FANOTIFY_H +#include "fanotify.h" + +#define BUF_SIZE 256 +static char event_buf[BUF_SIZE]; +int fd_notify; + +#define MOUNT_PATH "test_mnt" + +static void trigger_fs_abort(void) +{ + SAFE_MOUNT(tst_device->dev, MOUNT_PATH, tst_device->fs_type, + MS_REMOUNT|MS_RDONLY, "abort"); +} + +static struct test_case { + char *name; + void (*trigger_error)(void); +} testcases[] = { + { + .name = "Trigger abort", + .trigger_error = &trigger_fs_abort, + }, +}; + +int check_error_event_metadata(struct fanotify_event_metadata *event) +{ + int fail = 0; + + if (event->mask != FAN_FS_ERROR) { + fail++; + tst_res(TFAIL, "got unexpected event %llx", + (unsigned long long)event->mask); + } + + if (event->fd != FAN_NOFD) { + fail++; + tst_res(TFAIL, "Weird FAN_FD %llx", + (unsigned long long)event->mask); + } + return fail; +} + +void check_event(char *buf, size_t len, const struct test_case *ex) +{ + struct fanotify_event_metadata *event = + (struct fanotify_event_metadata *) buf; + + if (len < FAN_EVENT_METADATA_LEN) { + tst_res(TFAIL, "No event metadata found"); + return; + } + + if (check_error_event_metadata(event)) + return; + + tst_res(TPASS, "Successfully received: %s", ex->name); +} + +static void do_test(unsigned int i) +{ + const struct test_case *tcase = &testcases[i]; + size_t read_len; + + SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_ADD|FAN_MARK_FILESYSTEM, + FAN_FS_ERROR, AT_FDCWD, MOUNT_PATH); + + tcase->trigger_error(); + + read_len = SAFE_READ(0, fd_notify, event_buf, BUF_SIZE); + + SAFE_FANOTIFY_MARK(fd_notify, FAN_MARK_REMOVE|FAN_MARK_FILESYSTEM, + FAN_FS_ERROR, AT_FDCWD, MOUNT_PATH); + + check_event(event_buf, read_len, tcase); +} + +static void setup(void) +{ + REQUIRE_FANOTIFY_EVENTS_SUPPORTED_ON_FS(FAN_CLASS_NOTIF|FAN_REPORT_FID, + FAN_MARK_FILESYSTEM, + FAN_FS_ERROR, "."); + + fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF|FAN_REPORT_FID, + O_RDONLY); +} + +static void cleanup(void) +{ + if (fd_notify > 0) + SAFE_CLOSE(fd_notify); +} + +static struct tst_test test = { + .test = do_test, + .tcnt = ARRAY_SIZE(testcases), + .setup = setup, + .cleanup = cleanup, + .mount_device = 1, + .mntpoint = MOUNT_PATH, + .all_filesystems = 0, + .needs_root = 1, + .dev_fs_type = "ext4" +}; + +#else + TST_TEST_TCONF("system doesn't have required fanotify support"); +#endif -- 2.33.0