generic: check that we can't write to swap files
PATCH 1/1 generic: check that we can't write to swap files
From: Darrick J. Wong <[email protected]>
While active, the media backing a swap file is leased to the kernel. Userspace has no business writing to it. Make sure we can't do this.
Signed-off-by Darrick J. Wong <[email protected]>
--- src/swapon.c | 135 ++++++++++++++++++++++++++++++++++++++++++++++++- tests/generic/717 | 70 +++++++++++++++++++++++++ tests/generic/717.out | 14 +++++ tests/generic/718 | 55 ++++++++++++++++++++ tests/generic/718.out | 12 ++++ tests/generic/group | 2 + 6 files changed, 284 insertions(+), 4 deletions(-) create mode 100755 tests/generic/717 create mode 100644 tests/generic/717.out create mode 100755 tests/generic/718 create mode 100644 tests/generic/718.out
src/swapon.c +131 -4
@@ -3,22 +3,149 @@#include <stdio.h> #include <stdlib.h> #include <unistd.h> +#include <string.h> #include <sys/swap.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/mman.h> +#include <fcntl.h> +#include <signal.h> + +static void usage (const char *prog) +{ +fprintf (stderr,"usage: %s [-v verb] PATH\n" , prog); +exit (EXIT_FAILURE ); +} + +enum verbs { +TEST_SWAPON =0 , +TEST_WRITE , +TEST_MWRITE_AFTER , +TEST_MWRITE_BEFORE_AND_MWRITE_AFTER , +TEST_MWRITE_BEFORE , +MAX_TEST_VERBS , +}; + +#define BUF_SIZE 262144 +static char buf[BUF_SIZE ]; + +static void handle_signal (int signal) +{ +fprintf (stderr,"Caught signal %d, terminating...\n" , signal); +exit (EXIT_FAILURE ); +}int main (int argc,char **argv) { -int ret; +struct sigaction act = { + .sa_handler = handle_signal, + }; +enum verbs verb =TEST_SWAPON ; +void *p; +ssize_t sz; +int fd =-1 ; +int ret, c; + +memset (buf,0x58 ,BUF_SIZE ); + +while ((c =getopt (argc, argv,"v:" )) !=-1 ) { +switch (c) { +case 'v' : + verb =atoi (optarg); +if (verb <TEST_SWAPON || verb >=MAX_TEST_VERBS ) { +fprintf (stderr,"Verbs must be 0-%d.\n" , +MAX_TEST_VERBS -1 ); +usage (argv[0 ]); + } +break ; +default : +usage (argv[0 ]); +break ; + } + } -if (argc !=2 ) { -fprintf (stderr,"usage: %s PATH\n" , argv[0 ]); + ret =sigaction (SIGSEGV , &act,NULL ); +if (ret) { +perror ("sigsegv action" );return EXIT_FAILURE ; } - ret =swapon (argv[1 ],0 ); + ret =sigaction (SIGBUS , &act,NULL ); +if (ret) { +perror ("sigbus action" ); +return EXIT_FAILURE ; + } + +switch (verb) { +case TEST_WRITE : +case TEST_MWRITE_AFTER : +case TEST_MWRITE_BEFORE_AND_MWRITE_AFTER : +case TEST_MWRITE_BEFORE : + fd =open (argv[optind],O_RDWR ); +if (fd <0 ) { +perror (argv[optind]); +return EXIT_FAILURE ; + } +break ; +default : +break ; + } + +switch (verb) { +case TEST_MWRITE_BEFORE_AND_MWRITE_AFTER : +case TEST_MWRITE_BEFORE : + p =mmap (NULL ,BUF_SIZE ,PROT_WRITE |PROT_READ ,MAP_SHARED , + fd,65536 ); +if (p ==MAP_FAILED ) { +perror ("mmap" ); +return EXIT_FAILURE ; + } +memcpy (p, buf,BUF_SIZE ); +break ; +default : +break ; + } + +if (optind != argc -1 ) +usage (argv[0 ]); + + ret =swapon (argv[optind],0 );if (ret) {perror ("swapon" );return EXIT_FAILURE ; } +switch (verb) { +case TEST_WRITE : + sz =pwrite (fd, buf,BUF_SIZE ,65536 ); +if (sz <0 ) { +perror ("pwrite" ); +return EXIT_FAILURE ; + } +break ; +case TEST_MWRITE_AFTER : + p =mmap (NULL ,BUF_SIZE ,PROT_WRITE |PROT_READ ,MAP_SHARED , + fd,65536 ); +if (p ==MAP_FAILED ) { +perror ("mmap" ); +return EXIT_FAILURE ; + } +/* fall through */ +case TEST_MWRITE_BEFORE_AND_MWRITE_AFTER : +memcpy (p, buf,BUF_SIZE ); +break ; +default : +break ; + } + +if (fd >=0 ) { + ret =fsync (fd); +if (ret) +perror ("fsync" ); + ret =close (fd); +if (ret) +perror ("close" ); + } +return EXIT_SUCCESS ; }
tests/generic/717 new file +70 -0
70 lines not on this page · raw message
tests/generic/717.out new file +14 -0
@@ -0,0 +1,14 @@ +QA output created by 717 +verb 1 +pwrite: Text file busy +verb 2 +mmap: Text file busy +verb 3 +Caught signal 7, terminating... +verb 4 +pwrite: Text file busy +pwrite: Text file busy +mmap: Text file busy +no mapped regions, try 'help mmap' +ftruncate: Text file busy +fallocate: Text file busy
tests/generic/718 new file +55 -0
55 lines not on this page · raw message
tests/generic/718.out new file +12 -0
@@ -0,0 +1,12 @@ +QA output created by 718 +verb 1 +pwrite: Text file busy +verb 2 +mmap: Text file busy +verb 3 +Caught signal 7, terminating... +verb 4 +pwrite: Text file busy +pwrite: Text file busy +mmap: Text file busy +no mapped regions, try 'help mmap'
tests/generic/group +2 -0
@@ -570,3 +570,5 @@ 565 auto quick copy_range 566 auto quick quota metadata 567 auto quick rw punch +717 auto quick rw swap +718 auto quick rw swap