From patchwork Fri Jan 7 16:38:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Cervesato X-Patchwork-Id: 1576704 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=n/7vLXRw; dkim=fail reason="signature verification failed" header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=7nTRY0cW; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) 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=) Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4JVpny3Pfyz9t25 for ; Sat, 8 Jan 2022 03:39:01 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id A50EB3C8839 for ; Fri, 7 Jan 2022 17:38:58 +0100 (CET) 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]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by picard.linux.it (Postfix) with ESMTPS id 0EB463C7730 for ; Fri, 7 Jan 2022 17:38:55 +0100 (CET) Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by in-2.smtp.seeweb.it (Postfix) with ESMTPS id 21E39600235 for ; Fri, 7 Jan 2022 17:38:54 +0100 (CET) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 2E52D1F3A5; Fri, 7 Jan 2022 16:38:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1641573534; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=L/OlixJew2FlYQlgauinkklIKdGswsTd64jhIP0mUqs=; b=n/7vLXRwizZJ6KBAhf231yd+vUeVT44ukcprinEHU2uU7oPmeu2KmoxLMWfRhnp131Mqkv N9x7o/XvtP+7ECypOSPfx3JcLbrRHPKl61zuZjiCkscEDj4mZRnKG14UPQ0XCB4ihmd1dy XKSNO8bVJgdgK5d0E1KLZJIiRoF5Ozw= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1641573534; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=L/OlixJew2FlYQlgauinkklIKdGswsTd64jhIP0mUqs=; b=7nTRY0cW1oCXkCD4uiww7s2mDxO7THrNDXFHrT5ywgoYpZ8mRt1W1nT12Z/ZYC+uU4bdD3 u2UlqvmLILykkbAw== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 04DB913350; Fri, 7 Jan 2022 16:38:53 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id CNzGOp1s2GHqMgAAMHmgww (envelope-from ); Fri, 07 Jan 2022 16:38:53 +0000 From: Andrea Cervesato To: ltp@lists.linux.it Date: Fri, 7 Jan 2022 17:38:34 +0100 Message-Id: <20220107163834.7086-1-andrea.cervesato@suse.de> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.102.4 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, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on in-2.smtp.seeweb.it Subject: [LTP] [PATCH v3] Add wqueue01 test X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 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" This test is testing NOTIFY_KEY_UPDATED event Signed-off-by: Andrea Cervesato --- testcases/kernel/Makefile | 1 + testcases/kernel/watchqueue/.gitignore | 9 ++ testcases/kernel/watchqueue/Makefile | 9 ++ testcases/kernel/watchqueue/common.h | 132 +++++++++++++++++++++++++ testcases/kernel/watchqueue/wqueue01.c | 41 ++++++++ 5 files changed, 192 insertions(+) create mode 100644 testcases/kernel/watchqueue/.gitignore create mode 100644 testcases/kernel/watchqueue/Makefile create mode 100644 testcases/kernel/watchqueue/common.h create mode 100644 testcases/kernel/watchqueue/wqueue01.c diff --git a/testcases/kernel/Makefile b/testcases/kernel/Makefile index 4604f1f38..d44856c57 100644 --- a/testcases/kernel/Makefile +++ b/testcases/kernel/Makefile @@ -38,6 +38,7 @@ SUBDIRS += connectors \ sound \ tracing \ uevents \ + watchqueue \ ifeq ($(WITH_POWER_MANAGEMENT_TESTSUITE),yes) SUBDIRS += power_management diff --git a/testcases/kernel/watchqueue/.gitignore b/testcases/kernel/watchqueue/.gitignore new file mode 100644 index 000000000..dcfcd8272 --- /dev/null +++ b/testcases/kernel/watchqueue/.gitignore @@ -0,0 +1,9 @@ +wqueue01 +wqueue02 +wqueue03 +wqueue04 +wqueue05 +wqueue06 +wqueue07 +wqueue08 +wqueue09 diff --git a/testcases/kernel/watchqueue/Makefile b/testcases/kernel/watchqueue/Makefile new file mode 100644 index 000000000..8a9e4b32f --- /dev/null +++ b/testcases/kernel/watchqueue/Makefile @@ -0,0 +1,9 @@ +# SPDX-License-Identifier: GPL-2.0-or-later + +top_srcdir ?= ../../.. + +include $(top_srcdir)/include/mk/testcases.mk + +LDLIBS += $(KEYUTILS_LIBS) + +include $(top_srcdir)/include/mk/generic_leaf_target.mk diff --git a/testcases/kernel/watchqueue/common.h b/testcases/kernel/watchqueue/common.h new file mode 100644 index 000000000..97e210a29 --- /dev/null +++ b/testcases/kernel/watchqueue/common.h @@ -0,0 +1,132 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2021 SUSE LLC Andrea Cervesato + */ + +#ifndef WQUEUE_COMMON_H__ +#define WQUEUE_COMMON_H__ + +#include "tst_test.h" +#include "lapi/watch_queue.h" +#include "lapi/keyctl.h" + +static struct watch_notification_filter wqueue_filter = { + .nr_filters = 2, + .filters = { + [0] = { + .type = WATCH_TYPE_META, + .subtype_filter[0] = UINT_MAX, + }, + [1] = { + .type = WATCH_TYPE_KEY_NOTIFY, + .subtype_filter[0] = UINT_MAX, + }, + }, +}; + +static const char *key_subtypes[256] = { + [NOTIFY_KEY_INSTANTIATED] = "instantiated", + [NOTIFY_KEY_UPDATED] = "updated", + [NOTIFY_KEY_LINKED] = "linked", + [NOTIFY_KEY_UNLINKED] = "unlinked", + [NOTIFY_KEY_CLEARED] = "cleared", + [NOTIFY_KEY_REVOKED] = "revoked", + [NOTIFY_KEY_INVALIDATED] = "invalidated", + [NOTIFY_KEY_SETATTR] = "setattr", +}; + +static inline int wqueue_key_event(struct watch_notification *n, size_t len, unsigned int wtype, int type) +{ + struct key_notification *k; + const char* msg; + + if (wtype != WATCH_TYPE_KEY_NOTIFY) + return 0; + + if (len != sizeof(struct key_notification)) + tst_brk(TBROK, "Incorrect key message length"); + + k = (struct key_notification *)n; + msg = key_subtypes[n->subtype]; + + tst_res(TINFO, "KEY %08x change=%u[%s] aux=%u", + k->key_id, n->subtype, msg, k->aux); + + if (n->subtype == type) + return 1; + + return 0; +} + +static inline key_serial_t wqueue_add_key(int fd) +{ + key_serial_t key; + + key = add_key("user", "ltptestkey", "a", 1, KEY_SPEC_SESSION_KEYRING); + if (key == -1) + tst_brk(TBROK, "add_key error: %s", tst_strerrno(errno)); + + keyctl(KEYCTL_WATCH_KEY, key, fd, 0x01); + keyctl(KEYCTL_WATCH_KEY, KEY_SPEC_SESSION_KEYRING, fd, 0x02); + + return key; +} + +static inline int wqueue_watch(int buf_size, struct watch_notification_filter *filter) +{ + int pipefd[2]; + int fd; + + SAFE_PIPE2(pipefd, O_NOTIFICATION_PIPE); + + fd = pipefd[0]; + + SAFE_IOCTL(fd, IOC_WATCH_QUEUE_SET_SIZE, buf_size); + SAFE_IOCTL(fd, IOC_WATCH_QUEUE_SET_FILTER, filter); + + return fd; +} + +typedef void (*wqueue_callback) (struct watch_notification *n, size_t len, unsigned int wtype); + +static void wqueue_consumer(int fd, wqueue_callback cb) +{ + unsigned char buffer[433], *p, *end; + union { + struct watch_notification n; + unsigned char buf1[128]; + } n; + ssize_t buf_len; + + tst_res(TINFO, "Reading watch queue events"); + + buf_len = SAFE_READ(0, fd, buffer, sizeof(buffer)); + + p = buffer; + end = buffer + buf_len; + while (p < end) { + size_t largest, len; + + largest = end - p; + if (largest > 128) + largest = 128; + + if (largest < sizeof(struct watch_notification)) + tst_brk(TBROK, "Short message header: %zu", largest); + + memcpy(&n, p, largest); + + tst_res(TINFO, "NOTIFY[%03zx]: ty=%06x sy=%02x i=%08x", + p - buffer, n.n.type, n.n.subtype, n.n.info); + + len = n.n.info & WATCH_INFO_LENGTH; + if (len < sizeof(n.n) || len > largest) + tst_brk(TBROK, "Bad message length: %zu/%zu", len, largest); + + cb(&n.n, len, n.n.type); + + p += len; + } +} + +#endif diff --git a/testcases/kernel/watchqueue/wqueue01.c b/testcases/kernel/watchqueue/wqueue01.c new file mode 100644 index 000000000..33a9de3db --- /dev/null +++ b/testcases/kernel/watchqueue/wqueue01.c @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2021 SUSE LLC Andrea Cervesato + */ + +/*\ + * [Description] + * + * Test if keyctl update is correctly recognized by watch queue. + */ + +#include "tst_test.h" +#include "lapi/keyctl.h" +#include "common.h" + +static void saw_key_updated(struct watch_notification *n, size_t len, unsigned int wtype) +{ + if (wqueue_key_event(n, len, wtype, NOTIFY_KEY_UPDATED)) + tst_res(TPASS, "keyctl update has been recognized"); + else + tst_res(TFAIL, "keyctl update has not been recognized"); +} + +static void run(void) +{ + int fd; + key_serial_t key; + + fd = wqueue_watch(256, &wqueue_filter); + key = wqueue_add_key(fd); + + keyctl(KEYCTL_UPDATE, key, "b", 1); + wqueue_consumer(fd, saw_key_updated); + + SAFE_CLOSE(fd); +} + +static struct tst_test test = { + .test_all = run, + .needs_root = 1, +};