Added memfd_create() testsuite
v3 cover Added memfd_create() testsuite
memfd_create() is a syscall that creates an anonymous file. This syscall was originally introduced in "File Sealing & memfd_create()" patchset by David Herrmann <[email protected]>.
My patchset is a partial port of memfd_create() testsuite to LTP, with lapi extended as necessary. Code that runs _dup, _mmap and _open tests in multi-threaded environment was not ported yet.
V2: fixed LTP style problems, added syscall numbers for each arch
V3: fixed more style problems, moved fallocate.h to include/lapi
Jakub Racek (6):
Added syscall numbers for memfd_create
Added memfd_create() lapi flags
Added fcntl() lapi flags
move fallocate.h to lapi
syscalls: added memfd_create dir and memfd_create/memfd_create01.c syscalls/memfd_create02.c: added new test
include/lapi/fallocate.h | 66 +++++ include/lapi/fcntl.h | 25 ++ include/lapi/memfd.h | 27 ++ runtest/syscalls | 3 + testcases/kernel/include/aarch64.in | 1 + testcases/kernel/include/arm.in | 1 + testcases/kernel/include/hppa.in | 1 + testcases/kernel/include/i386.in | 1 + testcases/kernel/include/ia64.in | 1 + testcases/kernel/include/powerpc.in | 1 + testcases/kernel/include/powerpc64.in | 1 + testcases/kernel/include/s390.in | 1 + testcases/kernel/include/s390x.in | 1 + testcases/kernel/include/sh.in | 1 + testcases/kernel/include/sparc.in | 1 + testcases/kernel/include/sparc64.in | 1 + testcases/kernel/include/x86_64.in | 1 + testcases/kernel/syscalls/.gitignore | 2 + testcases/kernel/syscalls/fallocate/fallocate.h | 70 ----- testcases/kernel/syscalls/fallocate/fallocate01.c | 2 +- testcases/kernel/syscalls/fallocate/fallocate02.c | 2 +- testcases/kernel/syscalls/fallocate/fallocate03.c | 2 +- testcases/kernel/syscalls/fallocate/fallocate04.c | 2 +- testcases/kernel/syscalls/memfd_create/Makefile | 19 ++ .../kernel/syscalls/memfd_create/memfd_create01.c | 284 ++++++++++++++++++ .../kernel/syscalls/memfd_create/memfd_create02.c | 92 ++++++ .../syscalls/memfd_create/memfd_create_common.h | 323 +++++++++++++++++++++ 27 files changed, 858 insertions(+), 74 deletions(-) create mode 100644 include/lapi/fallocate.h create mode 100644 include/lapi/memfd.h delete mode 100644 testcases/kernel/syscalls/fallocate/fallocate.h create mode 100644 testcases/kernel/syscalls/memfd_create/Makefile create mode 100644 testcases/kernel/syscalls/memfd_create/memfd_create01.c create mode 100644 testcases/kernel/syscalls/memfd_create/memfd_create02.c create mode 100644 testcases/kernel/syscalls/memfd_create/memfd_create_common.h
Signature · 1 line
1.8.3.1
PATCH v3 1/6 Added syscall numbers for memfd_create
--- testcases/kernel/include/aarch64.in | 1 + testcases/kernel/include/arm.in | 1 + testcases/kernel/include/hppa.in | 1 + testcases/kernel/include/i386.in | 1 + testcases/kernel/include/ia64.in | 1 + testcases/kernel/include/powerpc.in | 1 + testcases/kernel/include/powerpc64.in | 1 + testcases/kernel/include/s390.in | 1 + testcases/kernel/include/s390x.in | 1 + testcases/kernel/include/sh.in | 1 + testcases/kernel/include/sparc.in | 1 + testcases/kernel/include/sparc64.in | 1 + testcases/kernel/include/x86_64.in | 1 + 13 files changed, 13 insertions(+)
testcases/kernel/include/aarch64.in +1 -0
@@ -255,3 +255,4 @@ setns 268 sendmmsg 269 kcmp 272 getrandom 278 +memfd_create 279
testcases/kernel/include/arm.in +1 -0
@@ -338,3 +338,4 @@ sched_setattr (__NR_SYSCALL_BASE+380) sched_getattr (__NR_SYSCALL_BASE+381) renameat2 (__NR_SYSCALL_BASE+382) getrandom (__NR_SYSCALL_BASE+384) +memfd_create (__NR_SYSCALL_BASE+385)
testcases/kernel/include/hppa.in +1 -0
@@ -15,3 +15,4 @@ faccessat (__NR_openat + 12) splice 291 tee 293 vmsplice 294 +memfd_create 340
testcases/kernel/include/i386.in +1 -0
@@ -338,3 +338,4 @@ sched_setattr 351 sched_getattr 352 renameat2 354 getrandom 355 +memfd_create 356
testcases/kernel/include/ia64.in +1 -0
@@ -294,3 +294,4 @@ fanotify_mark 1324 prlimit64 1325 renameat2 1338 getrandom 1339 +memfd_create 1340
testcases/kernel/include/powerpc.in +1 -0
@@ -345,3 +345,4 @@ sched_setattr 355 sched_getattr 356 renameat2 357 getrandom 359 +memfd_create 360
testcases/kernel/include/powerpc64.in +1 -0
@@ -345,3 +345,4 @@ sched_setattr 355 sched_getattr 356 renameat2 357 getrandom 359 +memfd_create 360
testcases/kernel/include/s390.in +1 -0
@@ -329,3 +329,4 @@ sched_setattr 345 sched_getattr 346 renameat2 347 getrandom 349 +memfd_create 350
testcases/kernel/include/s390x.in +1 -0
@@ -329,3 +329,4 @@ sched_setattr 345 sched_getattr 346 renameat2 347 getrandom 349 +memfd_create 350
testcases/kernel/include/sh.in +1 -0
@@ -362,3 +362,4 @@ fanotify_init 367 fanotify_mark 368 prlimit64 369 kcmp 378 +memfd_create 385
testcases/kernel/include/sparc.in +1 -0
@@ -334,3 +334,4 @@ prlimit64 331 kcmp 341 renameat2 345 getrandom 347 +memfd_create 348
testcases/kernel/include/sparc64.in +1 -0
@@ -310,3 +310,4 @@ prlimit64 331 kcmp 341 renameat2 345 getrandom 347 +memfd_create 348
testcases/kernel/include/x86_64.in +1 -0
@@ -305,3 +305,4 @@ sched_setattr 314 sched_getattr 315 renameat2 316 getrandom 318 +memfd_create 319
Signature · 1 line
1.8.3.1
PATCH v3 2/6 Added memfd_create() lapi flags
In reply to Jakub Racek--- include/lapi/memfd.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 include/lapi/memfd.h
include/lapi/memfd.h new file +27 -0
@@ -0,0 +1,27 @@ +/* +* Copyright (C) 2017 Red Hat, Inc. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of +* the License, or (at your option) any later version. +* +* This program is distributed in the hope that it would be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#ifndef LAPI_MEMFD_H +#define LAPI_MEMFD_H + +/* flags for memfd_create(2) (unsigned int) */ +#ifndef MFD_CLOEXEC +# define MFD_CLOEXEC 0x0001U +#endif +#ifndef MFD_ALLOW_SEALING +# define MFD_ALLOW_SEALING 0x0002U +#endif + +#endif
Signature · 1 line
1.8.3.1
PATCH v3 3/6 Added fcntl() lapi flags
In reply to Jakub Racek--- include/lapi/fcntl.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
include/lapi/fcntl.h +25 -0
@@ -50,6 +50,31 @@# define F_GETPIPE_SZ 1032#endif +/* +* Set/Get seals +*/ +#ifndef F_ADD_SEALS +# define F_ADD_SEALS (1033) +#endif + +#ifndef F_GET_SEALS +# define F_GET_SEALS (1034) +#endif + +#ifndef F_SEAL_SEAL +# define F_SEAL_SEAL 0x0001/* prevent further seals from being set */ +#endif + +#ifndef F_SEAL_SHRINK +# define F_SEAL_SHRINK 0x0002/* prevent file from shrinking */ +#endif +#ifndef F_SEAL_GROW +# define F_SEAL_GROW 0x0004/* prevent file from growing */ +#endif +#ifndef F_SEAL_WRITE +# define F_SEAL_WRITE 0x0008/* prevent writes */ +#endif +#ifndef F_OWNER_PGRP # define F_OWNER_PGRP 2#endif
Signature · 1 line
1.8.3.1
PATCH v3 4/6 move fallocate.h to lapi
In reply to Jakub Racek--- include/lapi/fallocate.h | 66 +++++++++++++++++++++ testcases/kernel/syscalls/fallocate/fallocate.h | 70 ----------------------- testcases/kernel/syscalls/fallocate/fallocate01.c | 2 +- testcases/kernel/syscalls/fallocate/fallocate02.c | 2 +- testcases/kernel/syscalls/fallocate/fallocate03.c | 2 +- testcases/kernel/syscalls/fallocate/fallocate04.c | 2 +- 6 files changed, 70 insertions(+), 74 deletions(-) create mode 100644 include/lapi/fallocate.h delete mode 100644 testcases/kernel/syscalls/fallocate/fallocate.h
include/lapi/fallocate.h new file +66 -0
@@ -0,0 +1,66 @@ +/* +* Copyright (c) International Business Machines Corp., 2007 +* Copyright (c) 2014 Fujitsu Ltd. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Library General Public License for more details. +* +*/ + +#ifndef FALLOCATE_H +#define FALLOCATE_H + +#include <sys/types.h> +#include <endian.h> +#include "config.h" +#include "lapi/abisize.h" +#include "linux_syscall_numbers.h" + +#ifndef SEEK_HOLE +# define SEEK_HOLE 4 +#endif + +#ifndef FALLOC_FL_KEEP_SIZE +# define FALLOC_FL_KEEP_SIZE 0x01 +#endif + +#ifndef FALLOC_FL_PUNCH_HOLE +# define FALLOC_FL_PUNCH_HOLE 0x02 +#endif + +#ifndef FALLOC_FL_COLLAPSE_RANGE +# define FALLOC_FL_COLLAPSE_RANGE 0x08 +#endif + +#ifndef FALLOC_FL_ZERO_RANGE +# define FALLOC_FL_ZERO_RANGE 0x10 +#endif + +#ifndef FALLOC_FL_INSERT_RANGE +# define FALLOC_FL_INSERT_RANGE 0x20 +#endif + +#if !defined(HAVE_FALLOCATE ) +static inline long fallocate (int fd,int mode,loff_t offset,loff_t len) +{ +/* Deal with 32bit ABIs that have 64bit syscalls. */ +# if LTP_USE_64_ABI +return ltp_syscall (__NR_fallocate, fd, mode, offset, len); +# else +return (long )ltp_syscall (__NR_fallocate, fd, mode, +__LONG_LONG_PAIR ((off_t) (offset >>32 ), + (off_t ) offset), +__LONG_LONG_PAIR ((off_t) (len >>32 ), + (off_t ) len)); +# endif +} +#endif + +#endif /* FALLOCATE_H */
testcases/kernel/syscalls/fallocate/fallocate.h deleted +0 -70
70 lines not on this page · raw message
testcases/kernel/syscalls/fallocate/fallocate01.c +1 -1
@@ -100,7 +100,7 @@#include <sys/utsname.h> #include "test.h" -#include "fallocate.h" +#include "lapi/fallocate.h" #include "lapi/fcntl.h" #define BLOCKS_WRITTEN 12
testcases/kernel/syscalls/fallocate/fallocate02.c +1 -1
@@ -39,7 +39,7 @@#include "test.h" #include "safe_macros.h" -#include "fallocate.h" +#include "lapi/fallocate.h" #define BLOCKS_WRITTEN 12#ifdef TEST_DEFAULT
testcases/kernel/syscalls/fallocate/fallocate03.c +1 -1
@@ -96,7 +96,7 @@#include <sys/utsname.h> #include "test.h" -#include "fallocate.h" +#include "lapi/fallocate.h" #define BLOCKS_WRITTEN 12#define HOLE_SIZE_IN_BLOCKS 12
testcases/kernel/syscalls/fallocate/fallocate04.c +1 -1
@@ -32,7 +32,7 @@#include "test.h" #include "safe_macros.h" -#include "fallocate.h" +#include "lapi/fallocate.h" char *TCID ="fallocate04" ;int TST_TOTAL =5 ;
Signature · 1 line
1.8.3.1
PATCH v3 5/6 syscalls: added memfd_create dir and memfd_create/memfd_create01.c
In reply to Jakub Racek--- runtest/syscalls | 2 + testcases/kernel/syscalls/.gitignore | 1 + testcases/kernel/syscalls/memfd_create/Makefile | 19 ++ .../kernel/syscalls/memfd_create/memfd_create01.c | 284 ++++++++++++++++++ .../syscalls/memfd_create/memfd_create_common.h | 323 +++++++++++++++++++++ 5 files changed, 629 insertions(+) create mode 100644 testcases/kernel/syscalls/memfd_create/Makefile create mode 100644 testcases/kernel/syscalls/memfd_create/memfd_create01.c create mode 100644 testcases/kernel/syscalls/memfd_create/memfd_create_common.h
runtest/syscalls +2 -0
@@ -1448,3 +1448,5 @@ futex_wake03 futex_wake03 futex_wake04 futex_wake04 futex_wait_bitset01 futex_wait_bitset01 futex_wait_bitset02 futex_wait_bitset02 + +memfd_create01 memfd_create01
testcases/kernel/syscalls/.gitignore +1 -0
@@ -1116,3 +1116,4 @@ /fanotify/fanotify06 /perf_event_open/perf_event_open01 /perf_event_open/perf_event_open02 +/memfd_create/memfd_create01
testcases/kernel/syscalls/memfd_create/Makefile new file +19 -0
@@ -0,0 +1,19 @@ +# +# Copyright (C) 2017 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; either version 2 of +# the License, or (at your option) any later version. +# +# This program is distributed in the hope that it would be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# + +top_srcdir ?= ../../../.. + +include $(top_srcdir)/include/mk/testcases.mk + +include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/memfd_create/memfd_create01.c new file +284 -0
284 lines not on this page · raw message
testcases/kernel/syscalls/memfd_create/memfd_create_common.h new file +323 -0
323 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH v3 5/6 syscalls: added memfd_create dir and memfd_create/memfd_create01.c
Hi!
Quoted 261 lines
testcases/kernel/syscalls/memfd_create/memfd_create01.c new file +261
-0+/* +* Copyright (C) 2017 Red Hat, Inc. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of +* the License, or (at your option) any later version. +* +* This program is distributed in the hope that it would be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +/* +* Based on Linux/tools/testing/selftests/memfd/memfd_test.c +* by David Herrmann <[email protected]> +* +* 24/02/2017 Port to LTP <[email protected]> +*/ + +#define _GNU_SOURCE + +#include "tst_test.h" +#include "memfd_create_common.h" + +/* +* Do few basic sealing tests to see whether setting/retrieving seals works. +*/ +static void test_basic (int fd) +{ +/* add basic seals */ +SAFE_MFD_HAS_SEALS (fd,0 ); +SAFE_MFD_ADD_SEALS (fd,F_SEAL_SHRINK |F_SEAL_WRITE ); +SAFE_MFD_HAS_SEALS (fd,F_SEAL_SHRINK |F_SEAL_WRITE ); + +/* add them again */ +SAFE_MFD_ADD_SEALS (fd,F_SEAL_SHRINK |F_SEAL_WRITE ); +SAFE_MFD_HAS_SEALS (fd,F_SEAL_SHRINK |F_SEAL_WRITE ); + +/* add more seals and seal against sealing */ +SAFE_MFD_ADD_SEALS (fd,F_SEAL_GROW |F_SEAL_SEAL ); +SAFE_MFD_HAS_SEALS (fd,F_SEAL_SHRINK |F_SEAL_GROW | +F_SEAL_WRITE |F_SEAL_SEAL ); + +/* verify that sealing no longer works */ +SAFE_MFD_FAIL_ADD_SEALS (fd,F_SEAL_GROW ); +SAFE_MFD_FAIL_ADD_SEALS (fd,0 ); + +tst_res (TPASS ,"%s" , __func__); +} + +/* +* Verify that no sealing is possible when memfd is created without +* MFD_ALLOW_SEALING flag. +*/ +static void test_no_sealing_without_flag (int fd) +{ +SAFE_MFD_HAS_SEALS (fd,F_SEAL_SEAL ); +SAFE_MFD_FAIL_ADD_SEALS (fd,F_SEAL_SHRINK |F_SEAL_GROW |F_SEAL_WRITE ); +SAFE_MFD_HAS_SEALS (fd,F_SEAL_SEAL ); + +tst_res (TPASS ,"%s" , __func__); +} + +/* +* Test SEAL_WRITE +* Test whether SEAL_WRITE actually prevents modifications. +*/ +static void test_seal_write (int fd) +{ +SAFE_MFD_HAS_SEALS (fd,0 ); +SAFE_MFD_ADD_SEALS (fd,F_SEAL_WRITE ); +SAFE_MFD_HAS_SEALS (fd,F_SEAL_WRITE ); + +SAFE_MFD_READ (fd); +SAFE_MFD_FAIL_WRITE (fd); +SAFE_MFD_SHRINK (fd); +SAFE_MFD_GROW (fd); +SAFE_MFD_FAIL_GROW_WRITE (fd); + +tst_res (TPASS ,"%s" , __func__); +} + +/* +* Test SEAL_SHRINK +* Test whether SEAL_SHRINK actually prevents shrinking +*/ +static void test_seal_shrink (int fd) +{ +SAFE_MFD_HAS_SEALS (fd,0 ); +SAFE_MFD_ADD_SEALS (fd,F_SEAL_SHRINK ); +SAFE_MFD_HAS_SEALS (fd,F_SEAL_SHRINK ); + +SAFE_MFD_READ (fd); +SAFE_MFD_WRITE (fd); +SAFE_MFD_FAIL_SHRINK (fd); +SAFE_MFD_GROW (fd); +SAFE_MFD_GROW_WRITE (fd); + +tst_res (TPASS ,"%s" , __func__); +} + +/* +* Test SEAL_GROW +* Test whether SEAL_GROW actually prevents growing +*/ +static void test_seal_grow (int fd) +{ +SAFE_MFD_HAS_SEALS (fd,0 ); +SAFE_MFD_ADD_SEALS (fd,F_SEAL_GROW ); +SAFE_MFD_HAS_SEALS (fd,F_SEAL_GROW ); + +SAFE_MFD_READ (fd); +SAFE_MFD_WRITE (fd); +SAFE_MFD_SHRINK (fd); +SAFE_MFD_FAIL_GROW (fd); +SAFE_MFD_FAIL_GROW_WRITE (fd); + +tst_res (TPASS ,"%s" , __func__); +} + +/* +* Test SEAL_SHRINK | SEAL_GROW +* Test whether SEAL_SHRINK | SEAL_GROW actually prevents resizing +*/ +static void test_seal_resize (int fd) +{ +SAFE_MFD_HAS_SEALS (fd,0 ); +SAFE_MFD_ADD_SEALS (fd,F_SEAL_SHRINK |F_SEAL_GROW ); +SAFE_MFD_HAS_SEALS (fd,F_SEAL_SHRINK |F_SEAL_GROW ); + +SAFE_MFD_READ (fd); +SAFE_MFD_WRITE (fd); +SAFE_MFD_FAIL_SHRINK (fd); +SAFE_MFD_FAIL_GROW (fd); +SAFE_MFD_FAIL_GROW_WRITE (fd); + +tst_res (TPASS ,"%s" , __func__); +} + +/* +* Test sharing via dup() +* Test that seals are shared between dupped FDs and they're all equal. +*/ +static void test_share_dup (int fd) +{ +int fd2; + +SAFE_MFD_HAS_SEALS (fd,0 ); + + fd2 =SAFE_DUP (fd); +SAFE_MFD_HAS_SEALS (fd2,0 ); + +SAFE_MFD_ADD_SEALS (fd,F_SEAL_WRITE ); +SAFE_MFD_HAS_SEALS (fd,F_SEAL_WRITE ); +SAFE_MFD_HAS_SEALS (fd2,F_SEAL_WRITE ); + +SAFE_MFD_ADD_SEALS (fd2,F_SEAL_SHRINK ); +SAFE_MFD_HAS_SEALS (fd,F_SEAL_WRITE |F_SEAL_SHRINK ); +SAFE_MFD_HAS_SEALS (fd2,F_SEAL_WRITE |F_SEAL_SHRINK ); + +SAFE_MFD_ADD_SEALS (fd,F_SEAL_SEAL ); +SAFE_MFD_HAS_SEALS (fd,F_SEAL_WRITE |F_SEAL_SHRINK |F_SEAL_SEAL ); +SAFE_MFD_HAS_SEALS (fd2,F_SEAL_WRITE |F_SEAL_SHRINK |F_SEAL_SEAL ); + +SAFE_MFD_FAIL_ADD_SEALS (fd,F_SEAL_GROW ); +SAFE_MFD_FAIL_ADD_SEALS (fd2,F_SEAL_GROW ); +SAFE_MFD_FAIL_ADD_SEALS (fd,F_SEAL_SEAL ); +SAFE_MFD_FAIL_ADD_SEALS (fd2,F_SEAL_SEAL ); + +SAFE_CLOSE (fd2); + +SAFE_MFD_FAIL_ADD_SEALS (fd,F_SEAL_GROW ); + +tst_res (TPASS ,"%s" , __func__); +} + +/* +* Test sealing with active mmap()s +* Modifying seals is only allowed if no other mmap() refs exist. +*/ +static void test_share_mmap (int fd) +{ +void *p; + +SAFE_MFD_HAS_SEALS (fd,0 ); + +/* shared/writable ref prevents sealing WRITE, but allows others */ + p =SAFE_MMAP (NULL ,MFD_DEF_SIZE ,PROT_READ |PROT_WRITE ,MAP_SHARED , + fd,0 ); + +SAFE_MFD_FAIL_ADD_SEALS (fd,F_SEAL_WRITE ); +SAFE_MFD_HAS_SEALS (fd,0 ); +SAFE_MFD_ADD_SEALS (fd,F_SEAL_SHRINK ); +SAFE_MFD_HAS_SEALS (fd,F_SEAL_SHRINK ); +SAFE_MUNMAP (p,MFD_DEF_SIZE ); + +/* readable ref allows sealing */ + p =SAFE_MMAP (NULL ,MFD_DEF_SIZE ,PROT_READ ,MAP_PRIVATE , fd,0 ); +SAFE_MFD_ADD_SEALS (fd,F_SEAL_WRITE ); +SAFE_MFD_HAS_SEALS (fd,F_SEAL_WRITE |F_SEAL_SHRINK ); +SAFE_MUNMAP (p,MFD_DEF_SIZE ); + +tst_res (TPASS ,"%s" , __func__); +} + +/* +* Test sealing with open(/proc/self/fd/%d) +* Via /proc we can get access to a separate file-context for the same memfd. +* This is *not* like dup(), but like a real separate open(). Make sure the +* semantics are as expected and we correctly check for RDONLY / WRONLY / RDWR. +*/ +static void test_share_open (int fd) +{ +int fd2; + +SAFE_MFD_HAS_SEALS (fd,0 ); + + fd2 =SAFE_MFD_OPEN (fd,O_RDWR ,0 ); +SAFE_MFD_ADD_SEALS (fd,F_SEAL_WRITE ); +SAFE_MFD_HAS_SEALS (fd,F_SEAL_WRITE ); +SAFE_MFD_HAS_SEALS (fd2,F_SEAL_WRITE ); + +SAFE_MFD_ADD_SEALS (fd2,F_SEAL_SHRINK ); +SAFE_MFD_HAS_SEALS (fd,F_SEAL_WRITE |F_SEAL_SHRINK ); +SAFE_MFD_HAS_SEALS (fd2,F_SEAL_WRITE |F_SEAL_SHRINK ); + +SAFE_CLOSE (fd); + fd =SAFE_MFD_OPEN (fd2,O_RDONLY ,0 ); + +SAFE_MFD_FAIL_ADD_SEALS (fd,F_SEAL_SEAL ); +SAFE_MFD_HAS_SEALS (fd,F_SEAL_WRITE |F_SEAL_SHRINK ); +SAFE_MFD_HAS_SEALS (fd2,F_SEAL_WRITE |F_SEAL_SHRINK ); + +SAFE_CLOSE (fd2); + +tst_res (TPASS ,"%s" , __func__); +} + +static const struct tcase { +int flags; +void (*func)(int fd); +} tcases[] = { + {MFD_ALLOW_SEALING , &test_basic}, + {0 , &test_no_sealing_without_flag}, + + {MFD_ALLOW_SEALING , &test_seal_write}, + {MFD_ALLOW_SEALING , &test_seal_shrink}, + {MFD_ALLOW_SEALING , &test_seal_grow}, + {MFD_ALLOW_SEALING , &test_seal_resize}, + + {MFD_ALLOW_SEALING , &test_share_dup}, + {MFD_ALLOW_SEALING , &test_share_mmap}, + {MFD_ALLOW_SEALING , &test_share_open}, +}; + +static void verify_memfd_create (unsigned int n) +{ +int fd;
testcases/kernel/syscalls/memfd_create/memfd_create01.c line 271
Quoted 10 lines
testcases/kernel/syscalls/memfd_create/memfd_create01.c new file +10
-0+const struct tcase *tc; + + tc = &tcases[n]; + + fd =SAFE_MFD_NEW (TCID ,MFD_DEF_SIZE , tc->flags); + + tc->func (fd); + +if (fd >0 ) +SAFE_CLOSE (fd);
There is no way that the fd is not valid here, since the SAFE_MFD_NEW() exits the test with tst_brk(). So we can drop the if (fd > 0) check here and just call SAFE_CLOSE(fd);
Quoted 50 lines
testcases/kernel/syscalls/memfd_create/memfd_create01.c new file +13
-0+} + +static void setup (void ) +{ +ASSERT_HAVE_MEMFD_CREATE (); +} + +static struct tst_test test = { + .tid ="memfd_create01" , + .test = verify_memfd_create, + .tcnt =ARRAY_SIZE (tcases), + .setup = setup, +};testcases/kernel/syscalls/memfd_create/memfd_create_common.h new file +31
-0@@ -0,0 +1,323 @@ +/* +* Copyright (C) 2017 Red Hat, Inc. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of +* the License, or (at your option) any later version. +* +* This program is distributed in the hope that it would be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +*/ + +#ifndef MEMFD_TEST_COMMON +#define MEMFD_TEST_COMMON + +#include <sys/types.h> +#include <sys/syscall.h> +#include <sys/uio.h> +#include <lapi/fallocate.h> +#include <lapi/fcntl.h> +#include <lapi/memfd.h> +#include <errno.h> +#include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#define TST_NO_DEFAULT_MAIN
testcases/kernel/syscalls/memfd_create/memfd_create_common.h line 41
Quoted 10 lines
testcases/kernel/syscalls/memfd_create/memfd_create_common.h new file +10
-0+#include <tst_test.h> + +#include "linux_syscall_numbers.h" + +#define MFD_DEF_SIZE 8192 +#define STACK_SIZE 65536 + +static int sys_memfd_create (const char *name,unsigned int flags) +{ +return syscall (__NR_memfd_create, name, flags);
We should call tst_syscall() here, that aborts the test with TCONF if __NR_memfd_create is not defined.
Quoted 14 lines
testcases/kernel/syscalls/memfd_create/memfd_create_common.h new file +14
-0+} + +#define SAFE_MFD_NEW (name, sz, flags) \ + safe_mfd_new(__FILE__, __LINE__, name, (sz), (flags)) + +static int safe_mfd_new (const char *filename,const int lineno, +const char *name,loff_t sz,int flags) +{ +int fd; + + fd =sys_memfd_create (name, flags); +if (fd <0 ) +tst_brk (TBROK |TERRNO ,"sys_memfd_create(%s, %d) failed" , + name, flags);
testcases/kernel/syscalls/memfd_create/memfd_create_common.h line 65
Quoted 10 lines
testcases/kernel/syscalls/memfd_create/memfd_create_common.h new file +10
-0+ +safe_ftruncate (filename, lineno, fd, sz); + +return fd; +} + +#define ASSERT_HAVE_MEMFD_CREATE () \ + do { \ + int r; \ + r = safe_mfd_new(__FILE__, __LINE__, "dummy_call", 0, 0); \
This is broken now, you either have to check errno in safe_mfd_new() and tst_brk() with TCONF when the syscall is not implemented or not enabled in kernel .config. Whitch is usually EINVAL for older kernel that does not know this syscall and ENOSYS for new enough kernels that does not have it enabled, but that should be checked.
Or we should just call the sys_memfd_create() here and check the errno and decide to end the test with TBROK/TCONF accordingly.
Quoted 16 lines
testcases/kernel/syscalls/memfd_create/memfd_create_common.h new file +16
-0+SAFE_CLOSE (r); \ + } while (0) + +#define SAFE_MFD_FAIL_NEW (name, flags) \ + do { \ + int fd; \ + fd = sys_memfd_create(name, (flags)); \ + if (fd >= 0) { \ + safe_close(__FILE__, __LINE__, NULL, fd); \ + tst_brk(TBROK | TERRNO, \ + "memfd_create(%s, %d) should have failed", \ + name, (flags)); \ + } \ + } while (0) + +#define SAFE_MMAP_FAIL (addr, length, prot, flags, fd, offset) \
testcases/kernel/syscalls/memfd_create/memfd_create_common.h line 91
Quoted 10 lines
testcases/kernel/syscalls/memfd_create/memfd_create_common.h new file +10
-0+safe_mmap_fail (__FILE__, __LINE__, (addr), (length), (prot), \ + (flags), (fd), (offset)) + +void safe_mmap_fail (const char *file,const int lineno,void *addr, +size_t length,int prot,int flags,int fd,off_t offset) +{ +if (mmap (addr, length, prot, flags, fd, offset) !=MAP_FAILED ) +tst_brk_ (file, lineno,TBROK |TERRNO , +"mmap() didn't fail as expected" ); +}
What about we prefix the test assertions with check_ rather than safe_ and produce PASS/FAIL messages there. Here we can do something as:
void check_mmap_fail(...)
{
if (mmap(addr, length, prot, flags, fd, offset) != MAP_FAILED) {
tst_res_(TFAIL, file, lineno,
"mmap(%p, %zu, %i, %i, %i, %zi) passed unexpectedly",
addr, lenght, prot, flags, fd, offset);
return;
}
tst_res_(TPASS | TERRNO, file, lineno,
"mmap(%p, %zu, %i, %i, %i, %zi) failed as expected",
addr, lenght, prot, flags, fd, offset);
}testcases/kernel/syscalls/memfd_create/memfd_create_common.h line 102
Quoted 10 lines
testcases/kernel/syscalls/memfd_create/memfd_create_common.h new file +10
-0+#define SAFE_MFD_GET_SEALS (fd) \ + SAFE_FCNTL((fd), F_GET_SEALS) + +#define SAFE_MFD_HAS_SEALS (fd, seals) \ + do { \ + if (SAFE_MFD_GET_SEALS(fd) != (seals)) \ + tst_brk(TBROK | TERRNO, \ + "fcntl(%d, F_GET_SEALS) returned unexpected value", \ + fd); \ + } while (0)
This should be named CHECK_MFD_HAS_SEALS() as well and produce PASS/FAIL.
testcases/kernel/syscalls/memfd_create/memfd_create_common.h line 113
Quoted 10 lines
testcases/kernel/syscalls/memfd_create/memfd_create_common.h new file +10
-0+#define SAFE_MFD_ADD_SEALS (fd, seals) \ + SAFE_FCNTL(fd, F_ADD_SEALS, (seals)) + +#define SAFE_MFD_FAIL_ADD_SEALS (fd, seals) \ + do { \ + if (fcntl(fd, F_ADD_SEALS, (seals)) >= 0) \ + tst_brk(TBROK | TERRNO, \ + "fcntl(%d, F_ADD_SEALS) didn't fail as expected", \ + fd); \ + } while (0)
This should be CHECK_MFD_FAIL_ADD_SEALS() and we should produce PASS/FAIL. And the FAIL should be most likely passed to tst_brk() since otherwise we will continued with the test with unexpected state of the MFD object. So something as:
...
{
if (fcntl(fd, F_ADD_SEALS, (seals)) >= 0)
tst_brk(TFAIL | TERRNO, ...);
tst_res(TPASS | TERRNO,
"fcntl(%d, F_ADD_SEALS) failed as expected",
fd);
}Quoted 24 lines
testcases/kernel/syscalls/memfd_create/memfd_create_common.h new file +24
-0+#define SAFE_MFD_SIZE (fd, size) \ + safe_mfd_size(__FILE__, __LINE__, fd, (size)) + +static void safe_mfd_size (const char *filename,const int lineno, +int fd,size_t size) +{ +struct stat st; + +safe_fstat (filename, lineno, fd, &st); + +if (st.st_size != (long )size) +tst_brk (TBROK |TERRNO ,"unexpected fstat() file size" ); +} + +#define SAFE_MFD_OPEN (fd, flags, mode) \ + safe_mfd_open(__FILE__, __LINE__, fd, (flags), (mode)) + +static int safe_mfd_open (const char *filename,const int lineno,int fd, +int flags,mode_t mode) +{ +char buf[512 ]; + +sprintf (buf,"/proc/self/fd/%d" , fd); +
testcases/kernel/syscalls/memfd_create/memfd_create_common.h line 148
Quoted 10 lines
testcases/kernel/syscalls/memfd_create/memfd_create_common.h new file +10
-0+return safe_open (filename, lineno,NULL , buf, flags, mode); +} + +#define SAFE_MFD_FAIL_OPEN (fd, flags, mode) \ + do { \ + char buf[512]; \ + sprintf(buf, "/proc/self/fd/%d", fd); \ + if (open(buf, (flags), (mode)) >= 0) \ + tst_brk(TBROK, "open() didn't fail as expected"); \ + } while (0)
This one as well.
Quoted 12 lines
testcases/kernel/syscalls/memfd_create/memfd_create_common.h new file +12
-0+#define SAFE_MFD_READ (fd) \ + safe_mfd_read(__FILE__, __LINE__, fd) + +static void safe_mfd_read (const char *filename,const int lineno,int fd) +{ +char buf[16 ]; +void *p; + +safe_read (filename, lineno,NULL ,1 , fd, buf,sizeof (buf)); + +/* verify PROT_READ *is* allowed */ + p = safe_mmap(filename, lineno, NULL, MFD_DEF_SIZE,
testcases/kernel/syscalls/memfd_create/memfd_create_common.h line 171
Quoted 10 lines
testcases/kernel/syscalls/memfd_create/memfd_create_common.h new file +10
-0+ PROT_READ, MAP_PRIVATE, fd, 0); + +safe_munmap (filename, lineno,NULL , p,MFD_DEF_SIZE ); + +/* verify MAP_PRIVATE is *always* allowed (even writable) */ + p =safe_mmap (filename, lineno,NULL ,MFD_DEF_SIZE , +PROT_READ |PROT_WRITE ,MAP_PRIVATE , fd,0 ); + +safe_munmap (filename, lineno,NULL , p,MFD_DEF_SIZE ); +}
This one as well, this should be called something as CHECK_MFD_READABLE() and produce three PASS/FAIL messages.
Quoted 34 lines
testcases/kernel/syscalls/memfd_create/memfd_create_common.h new file +34
-0+#define SAFE_MFD_WRITE (fd) \ + safe_mfd_write(__FILE__, __LINE__, fd) + +static void safe_mfd_write (const char *filename,const int lineno,int fd) +{ +void *p; +int r; + +/* verify write() succeeds */ +safe_write (filename, lineno,NULL ,1 , fd,"\0\0\0\0" ,4 ); + +/* verify PROT_READ | PROT_WRITE is allowed */ + p =safe_mmap (filename, lineno,NULL ,MFD_DEF_SIZE , +PROT_READ |PROT_WRITE ,MAP_SHARED , fd,0 ); + + *(char *)p =0 ; +safe_munmap (filename, lineno,NULL , p,MFD_DEF_SIZE ); + +/* verify PROT_WRITE is allowed */ + p =safe_mmap (filename, lineno,NULL ,MFD_DEF_SIZE , +PROT_WRITE ,MAP_SHARED , fd,0 ); + + *(char *)p =0 ; +safe_munmap (filename, lineno,NULL , p,MFD_DEF_SIZE ); + +/* verify PROT_READ with MAP_SHARED is allowed and a following +* mprotect(PROT_WRITE) allows writing +*/ + p =safe_mmap (filename, lineno,NULL ,MFD_DEF_SIZE , +PROT_READ ,MAP_SHARED , fd,0 ); + + r =mprotect (p,MFD_DEF_SIZE ,PROT_READ |PROT_WRITE ); +if (r <0 ) +tst_brk (TBROK |TERRNO ,"mprotect() failed" );
testcases/kernel/syscalls/memfd_create/memfd_create_common.h line 216
Quoted 10 lines
testcases/kernel/syscalls/memfd_create/memfd_create_common.h new file +10
-0+ + *(char *)p =0 ; +safe_munmap (filename, lineno,NULL , p,MFD_DEF_SIZE ); + +/* verify PUNCH_HOLE works */ + r =fallocate (fd,FALLOC_FL_PUNCH_HOLE |FALLOC_FL_KEEP_SIZE , +0 ,MFD_DEF_SIZE ); +if (r <0 ) +tst_brk (TBROK |TERRNO ,"fallocate(PUNCH_HOLE) failed" ); +}
This one as well, CHECK_MFD_WRITEABLE()
Quoted 30 lines
testcases/kernel/syscalls/memfd_create/memfd_create_common.h new file +30
-0+#define SAFE_MFD_FAIL_WRITE (fd) \ + safe_mfd_fail_write(__FILE__, __LINE__, fd) + +static void safe_mfd_fail_write (const char *filename,const int lineno,int fd) +{ +ssize_t l; +void *p; +int r; + +/* verify write() fails */ + l =write (fd,"data" ,4 ); +if (l != -EPERM ) +tst_brk (TBROK |TERRNO ,"write() didn't fail as expected" ); + +/* verify PROT_READ | PROT_WRITE is not allowed */ +safe_mmap_fail (filename, lineno,NULL ,MFD_DEF_SIZE , +PROT_READ |PROT_WRITE ,MAP_SHARED , fd,0 ); + +/* verify PROT_WRITE is not allowed */ +safe_mmap_fail (filename, lineno,NULL ,MFD_DEF_SIZE , +PROT_WRITE ,MAP_SHARED , fd,0 ); + +/* Verify PROT_READ with MAP_SHARED with a following mprotect is not +* allowed. Note that for r/w the kernel already prevents the mmap. +*/ + p =mmap (NULL ,MFD_DEF_SIZE ,PROT_READ , +MAP_SHARED , fd,0 ); +if (p !=MAP_FAILED ) { +if (mprotect (p,MFD_DEF_SIZE ,PROT_READ |PROT_WRITE ) >=0 ) + tst_brk(TBROK | TERRNO,
testcases/kernel/syscalls/memfd_create/memfd_create_common.h line 257
Quoted 10 lines
testcases/kernel/syscalls/memfd_create/memfd_create_common.h new file +10
-0+ "mmap()+mprotect() didn't fail as expected"); + } + +/* verify PUNCH_HOLE fails */ + r =fallocate (fd,FALLOC_FL_PUNCH_HOLE |FALLOC_FL_KEEP_SIZE , +0 ,MFD_DEF_SIZE ); +if (r >=0 ) +tst_brk (TBROK |TERRNO , +"fallocate(PUNCH_HOLE) didn't fail as expected" ); +}
This one as well, CHECK_MFD_NON_WRITEABLE() or something similar.
Quoted 14 lines
testcases/kernel/syscalls/memfd_create/memfd_create_common.h new file +14
-0+#define SAFE_MFD_SHRINK (fd) \ + safe_mfd_shrink(__FILE__, __LINE__, fd) + +static void safe_mfd_shrink (const char *filename,const int lineno,int fd) +{ +int fd2; + +safe_ftruncate (filename, lineno, fd,MFD_DEF_SIZE /2 ); +safe_mfd_size (filename, lineno, fd,MFD_DEF_SIZE /2 ); + + fd2 =safe_mfd_open (filename, lineno, fd, +O_RDWR |O_CREAT |O_TRUNC ,0600 ); +safe_close (filename, lineno,NULL , fd2); +
testcases/kernel/syscalls/memfd_create/memfd_create_common.h line 282
Quoted 10 lines
testcases/kernel/syscalls/memfd_create/memfd_create_common.h new file +10
-0+safe_mfd_size (filename, lineno, fd,0 ); +} + +#define SAFE_MFD_FAIL_SHRINK (fd) \ + do { \ + if (ftruncate(fd, MFD_DEF_SIZE / 2) >= 0) \ + tst_brk(TBROK | TERRNO, \ + "ftruncate(SHRINK) didn't fail as expected"); \ + SAFE_MFD_FAIL_OPEN(fd, O_RDWR | O_CREAT | O_TRUNC, 0600); \ + } while (0)
CHECK_MFD_SHRINKABLE() and CHECK_MFD_NON_SHRINKABLE() here.
Quoted 13 lines
testcases/kernel/syscalls/memfd_create/memfd_create_common.h new file +13
-0+#define SAFE_MFD_GROW (fd) \ + safe_mfd_grow(__FILE__, __LINE__, fd) + +static void safe_mfd_grow (const char *filename,const int lineno,int fd) +{ +safe_ftruncate (filename, lineno, fd,MFD_DEF_SIZE *2 ); +safe_mfd_size (filename, lineno, fd,MFD_DEF_SIZE *2 ); + +if (fallocate (fd,0 ,0 ,MFD_DEF_SIZE *4 ) <0 ) +tst_brk (TBROK |TERRNO ,"fallocate(ALLOC) failed" ); + +safe_mfd_size (filename, lineno, fd,MFD_DEF_SIZE *4 ); +}
testcases/kernel/syscalls/memfd_create/memfd_create_common.h line 306
Quoted 10 lines
testcases/kernel/syscalls/memfd_create/memfd_create_common.h new file +10
-0+ +#define SAFE_MFD_FAIL_GROW (fd) \ + do { \ + if (ftruncate(fd, MFD_DEF_SIZE * 2) >= 0) \ + tst_brk(TBROK | TERRNO, \ + "ftruncate(GROW) didn't fail as expected"); \ + if (fallocate(fd, 0, 0, MFD_DEF_SIZE * 4) >= 0) \ + tst_brk(TBROK | TERRNO, \ + "ftruncate(ALLOC) didn't fail as expected"); \ + } while (0)
and here, GROWABLE and NON_GROWABLE.
Quoted 6 lines
testcases/kernel/syscalls/memfd_create/memfd_create_common.h new file +6
-0+#define SAFE_MFD_GROW_WRITE (fd) \ + do {\ + char buf[MFD_DEF_SIZE * 8]; \ + if (pwrite(fd, buf, sizeof(buf), 0) != sizeof(buf)) \ + tst_brk(TBROK | TERRNO, "pwrite() failed"); \ + safe_mfd_size(__FILE__, __LINE__, fd, MFD_DEF_SIZE * 8); \
testcases/kernel/syscalls/memfd_create/memfd_create_common.h line 323
Quoted 10 lines
testcases/kernel/syscalls/memfd_create/memfd_create_common.h new file +10
-0+ } while (0) + +#define SAFE_MFD_FAIL_GROW_WRITE (fd) \ + do { \ + char buf[MFD_DEF_SIZE * 8]; \ + if (pwrite(fd, buf, sizeof(buf), 0) == sizeof(buf)) \ + tst_brk(TBROK, "pwrite() didn't fail as expected"); \ + } while (0) + +#endif
Hmm, GROWABLE_BY_WRITE() and NON_GROWABLE_BY_WRITE() or something like that.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH v3 6/6 syscalls/memfd_create02.c: added new test
In reply to Jakub Racek--- runtest/syscalls | 1 + testcases/kernel/syscalls/.gitignore | 1 + .../kernel/syscalls/memfd_create/memfd_create02.c | 92 ++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 testcases/kernel/syscalls/memfd_create/memfd_create02.c
runtest/syscalls +1 -0
@@ -1450,3 +1450,4 @@ futex_wait_bitset01 futex_wait_bitset01 futex_wait_bitset02 futex_wait_bitset02 memfd_create01 memfd_create01 +memfd_create02 memfd_create02
testcases/kernel/syscalls/.gitignore +1 -0
@@ -1117,3 +1117,4 @@ /perf_event_open/perf_event_open01 /perf_event_open/perf_event_open02 /memfd_create/memfd_create01 +/memfd_create/memfd_create02
testcases/kernel/syscalls/memfd_create/memfd_create02.c new file +92 -0
92 lines not on this page · raw message
Signature · 1 line
1.8.3.1