Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2019 Alexey Dobriyan <adobriyan@gmail.com>
0003  *
0004  * Permission to use, copy, modify, and distribute this software for any
0005  * purpose with or without fee is hereby granted, provided that the above
0006  * copyright notice and this permission notice appear in all copies.
0007  *
0008  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
0009  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
0010  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
0011  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
0012  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
0013  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
0014  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
0015  */
0016 /* Test that pointing #! script interpreter to self doesn't recurse. */
0017 #include <errno.h>
0018 #include <sched.h>
0019 #include <stdio.h>
0020 #include <string.h>
0021 #include <sys/types.h>
0022 #include <sys/stat.h>
0023 #include <fcntl.h>
0024 #include <sys/mount.h>
0025 #include <unistd.h>
0026 
0027 int main(void)
0028 {
0029     if (unshare(CLONE_NEWNS) == -1) {
0030         if (errno == ENOSYS || errno == EPERM) {
0031             fprintf(stderr, "error: unshare, errno %d\n", errno);
0032             return 4;
0033         }
0034         fprintf(stderr, "error: unshare, errno %d\n", errno);
0035         return 1;
0036     }
0037     if (mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) == -1) {
0038         fprintf(stderr, "error: mount '/', errno %d\n", errno);
0039         return 1;
0040     }
0041     /* Require "exec" filesystem. */
0042     if (mount(NULL, "/tmp", "ramfs", 0, NULL) == -1) {
0043         fprintf(stderr, "error: mount ramfs, errno %d\n", errno);
0044         return 1;
0045     }
0046 
0047 #define FILENAME "/tmp/1"
0048 
0049     int fd = creat(FILENAME, 0700);
0050     if (fd == -1) {
0051         fprintf(stderr, "error: creat, errno %d\n", errno);
0052         return 1;
0053     }
0054 #define S "#!" FILENAME "\n"
0055     if (write(fd, S, strlen(S)) != strlen(S)) {
0056         fprintf(stderr, "error: write, errno %d\n", errno);
0057         return 1;
0058     }
0059     close(fd);
0060 
0061     int rv = execve(FILENAME, NULL, NULL);
0062     if (rv == -1 && errno == ELOOP) {
0063         return 0;
0064     }
0065     fprintf(stderr, "error: execve, rv %d, errno %d\n", rv, errno);
0066     return 1;
0067 }