162306a36Sopenharmony_ci/* 262306a36Sopenharmony_ci * Copyright (c) 2019 Alexey Dobriyan <adobriyan@gmail.com> 362306a36Sopenharmony_ci * 462306a36Sopenharmony_ci * Permission to use, copy, modify, and distribute this software for any 562306a36Sopenharmony_ci * purpose with or without fee is hereby granted, provided that the above 662306a36Sopenharmony_ci * copyright notice and this permission notice appear in all copies. 762306a36Sopenharmony_ci * 862306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 962306a36Sopenharmony_ci * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 1062306a36Sopenharmony_ci * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 1162306a36Sopenharmony_ci * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 1262306a36Sopenharmony_ci * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 1362306a36Sopenharmony_ci * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 1462306a36Sopenharmony_ci * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 1562306a36Sopenharmony_ci */ 1662306a36Sopenharmony_ci/* Test that pointing #! script interpreter to self doesn't recurse. */ 1762306a36Sopenharmony_ci#include <errno.h> 1862306a36Sopenharmony_ci#include <sched.h> 1962306a36Sopenharmony_ci#include <stdio.h> 2062306a36Sopenharmony_ci#include <string.h> 2162306a36Sopenharmony_ci#include <sys/types.h> 2262306a36Sopenharmony_ci#include <sys/stat.h> 2362306a36Sopenharmony_ci#include <fcntl.h> 2462306a36Sopenharmony_ci#include <sys/mount.h> 2562306a36Sopenharmony_ci#include <unistd.h> 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ciint main(void) 2862306a36Sopenharmony_ci{ 2962306a36Sopenharmony_ci if (unshare(CLONE_NEWNS) == -1) { 3062306a36Sopenharmony_ci if (errno == ENOSYS || errno == EPERM) { 3162306a36Sopenharmony_ci fprintf(stderr, "error: unshare, errno %d\n", errno); 3262306a36Sopenharmony_ci return 4; 3362306a36Sopenharmony_ci } 3462306a36Sopenharmony_ci fprintf(stderr, "error: unshare, errno %d\n", errno); 3562306a36Sopenharmony_ci return 1; 3662306a36Sopenharmony_ci } 3762306a36Sopenharmony_ci if (mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) == -1) { 3862306a36Sopenharmony_ci fprintf(stderr, "error: mount '/', errno %d\n", errno); 3962306a36Sopenharmony_ci return 1; 4062306a36Sopenharmony_ci } 4162306a36Sopenharmony_ci /* Require "exec" filesystem. */ 4262306a36Sopenharmony_ci if (mount(NULL, "/tmp", "ramfs", 0, NULL) == -1) { 4362306a36Sopenharmony_ci fprintf(stderr, "error: mount ramfs, errno %d\n", errno); 4462306a36Sopenharmony_ci return 1; 4562306a36Sopenharmony_ci } 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci#define FILENAME "/tmp/1" 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ci int fd = creat(FILENAME, 0700); 5062306a36Sopenharmony_ci if (fd == -1) { 5162306a36Sopenharmony_ci fprintf(stderr, "error: creat, errno %d\n", errno); 5262306a36Sopenharmony_ci return 1; 5362306a36Sopenharmony_ci } 5462306a36Sopenharmony_ci#define S "#!" FILENAME "\n" 5562306a36Sopenharmony_ci if (write(fd, S, strlen(S)) != strlen(S)) { 5662306a36Sopenharmony_ci fprintf(stderr, "error: write, errno %d\n", errno); 5762306a36Sopenharmony_ci return 1; 5862306a36Sopenharmony_ci } 5962306a36Sopenharmony_ci close(fd); 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ci int rv = execve(FILENAME, NULL, NULL); 6262306a36Sopenharmony_ci if (rv == -1 && errno == ELOOP) { 6362306a36Sopenharmony_ci return 0; 6462306a36Sopenharmony_ci } 6562306a36Sopenharmony_ci fprintf(stderr, "error: execve, rv %d, errno %d\n", rv, errno); 6662306a36Sopenharmony_ci return 1; 6762306a36Sopenharmony_ci} 68