Home | History | Annotate | Download | only in mountns
      1 /* Copyright (c) 2014 Red Hat, Inc.
      2  *
      3  * This program is free software: you can redistribute it and/or modify
      4  * it under the terms of version 2 the GNU General Public License as
      5  * published by the Free Software Foundation.
      6  *
      7  * This program is distributed in the hope that it will be useful,
      8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     10  * GNU General Public License for more details.
     11  *
     12  * You should have received a copy of the GNU General Public License
     13  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     14  ***********************************************************************
     15  * File: mountns04.c
     16  *
     17  * Tests an unbindable mount: unbindable mount is an unbindable
     18  * private mount.
     19  * Description:
     20  * 1. Creates directories "A", "B" and files "A/A", "B/B"
     21  * 2. Unshares mount namespace and makes it private (so mounts/umounts
     22  *    have no effect on a real system)
     23  * 3. Bind mounts directory "A" to "A"
     24  * 4. Makes directory directory "A" unbindable
     25  * 5. Tries to bind mount unbindable "A" to "B":
     26  *    - if it fails, test passes
     27  *    - if it passes, test fails
     28  ***********************************************************************/
     29 
     30 #define _GNU_SOURCE
     31 #include <sys/wait.h>
     32 #include <sys/mount.h>
     33 #include <stdio.h>
     34 #include <errno.h>
     35 #include "test.h"
     36 #include "libclone.h"
     37 #include "safe_macros.h"
     38 #include "mountns_helper.h"
     39 
     40 
     41 char *TCID	= "mountns04";
     42 int TST_TOTAL	= 1;
     43 
     44 
     45 #if defined(MS_SHARED) && defined(MS_PRIVATE) \
     46     && defined(MS_REC) && defined(MS_UNBINDABLE)
     47 
     48 static void test(void)
     49 {
     50 	/* unshares the mount ns */
     51 	if (unshare(CLONE_NEWNS) == -1)
     52 		tst_brkm(TBROK | TERRNO, cleanup, "unshare failed");
     53 	/* makes sure mounts/umounts have no effect on a real system */
     54 	SAFE_MOUNT(cleanup, "none", "/", "none", MS_REC|MS_PRIVATE, NULL);
     55 
     56 	/* bind mounts DIRA to itself */
     57 	SAFE_MOUNT(cleanup, DIRA, DIRA, "none", MS_BIND, NULL);
     58 	/* makes mount DIRA unbindable */
     59 	SAFE_MOUNT(cleanup, "none", DIRA, "none", MS_UNBINDABLE, NULL);
     60 
     61 	/* tries to bind mount unbindable DIRA to DIRB which should fail */
     62 	if (mount(DIRA, DIRB, "none", MS_BIND, NULL) == -1) {
     63 		tst_resm(TPASS, "unbindable mount passed");
     64 	} else {
     65 		SAFE_UMOUNT(cleanup, DIRB);
     66 		tst_resm(TFAIL, "unbindable mount faled");
     67 	}
     68 
     69 	SAFE_UMOUNT(cleanup, DIRA);
     70 }
     71 
     72 int main(int argc, char *argv[])
     73 {
     74 	int lc;
     75 
     76 	tst_parse_opts(argc, argv, NULL, NULL);
     77 
     78 	setup();
     79 
     80 	for (lc = 0; TEST_LOOPING(lc); lc++)
     81 		test();
     82 
     83 	cleanup();
     84 	tst_exit();
     85 }
     86 
     87 #else
     88 int main(void)
     89 {
     90 	tst_brkm(TCONF, NULL, "needed mountflags are not defined");
     91 }
     92 #endif
     93