Exemple #1
0
static void setup(void)
{
	int fd;

	tst_require_root(NULL);

	tst_tmpdir();

	ltpuser = SAFE_GETPWNAM(cleanup, "nobody");

	fd = SAFE_CREAT(cleanup, TEST_FILE5, 0777);
	SAFE_CLOSE(cleanup, fd);

	if (acct(TEST_FILE5) == -1) {
		if (errno == ENOSYS) {
			tst_brkm(TCONF, cleanup,
				 "BSD process accounting is not configured in "
				 "this kernel");
		} else {
			tst_brkm(TBROK | TERRNO, cleanup, "acct failed unexpectedly");
		}
	}

	/* turn off acct, so we are in a known state */
	if (acct(NULL) == -1) {
		if (errno == ENOSYS) {
			tst_brkm(TCONF, cleanup,
				 "BSD process accounting is not configured in "
				 "this kernel");
		} else {
			tst_brkm(TBROK | TERRNO, cleanup, "acct(NULL) failed");
		}
	}

	/* ELOOP SETTING */
	SAFE_SYMLINK(cleanup, TEST_FILE6, "test_file_eloop2");
	SAFE_SYMLINK(cleanup, "test_file_eloop2", TEST_FILE6);

	/* ENAMETOOLONG SETTING */
	memset(nametoolong, 'a', PATH_MAX+1);

	/* EROFS SETTING */
	tst_mkfs(NULL, device, fstype, NULL);
	SAFE_MKDIR(cleanup, "mntpoint", DIR_MODE);
	if (mount(device, "mntpoint", fstype, 0, NULL) < 0) {
		tst_brkm(TBROK | TERRNO, cleanup,
			 "mount device:%s failed", device);
	}
	mount_flag = 1;
	/* Create a file in the file system, then remount it as read-only */
	fd = SAFE_CREAT(cleanup, TEST_FILE8, 0644);
	SAFE_CLOSE(cleanup, fd);
	if (mount(device, "mntpoint", fstype,
		  MS_REMOUNT | MS_RDONLY, NULL) < 0) {
		tst_brkm(TBROK | TERRNO, cleanup,
			 "mount device:%s failed", device);
	}
	mount_flag = 1;
}
Exemple #2
0
static void setup(void)
{
	int fd;

	tst_require_root(NULL);

	check_acct_in_kernel();

	tst_tmpdir();

	ltpuser = SAFE_GETPWNAM(cleanup, "nobody");

	fd = SAFE_CREAT(cleanup, TEST_FILE5, 0777);
	SAFE_CLOSE(cleanup, fd);

	if (acct(TEST_FILE5) == -1)
		tst_brkm(TBROK | TERRNO, cleanup, "acct failed unexpectedly");

	/* turn off acct, so we are in a known state */
	if (acct(NULL) == -1)
		tst_brkm(TBROK | TERRNO, cleanup, "acct(NULL) failed");

	/* ELOOP SETTING */
	SAFE_SYMLINK(cleanup, TEST_FILE6, "test_file_eloop2");
	SAFE_SYMLINK(cleanup, "test_file_eloop2", TEST_FILE6);

	/* ENAMETOOLONG SETTING */
	memset(nametoolong, 'a', PATH_MAX+1);

	/* EROFS SETTING */
	fs_type = tst_dev_fs_type();
	device = tst_acquire_device(cleanup);

	if (!device)
		tst_brkm(TCONF, cleanup, "Failed to obtain block device");

	tst_mkfs(cleanup, device, fs_type, NULL);
	SAFE_MKDIR(cleanup, "mntpoint", DIR_MODE);
	if (mount(device, "mntpoint", fs_type, 0, NULL) < 0) {
		tst_brkm(TBROK | TERRNO, cleanup,
			 "mount device:%s failed", device);
	}
	mount_flag = 1;

	/* Create a file in the file system, then remount it as read-only */
	fd = SAFE_CREAT(cleanup, TEST_FILE8, 0644);
	SAFE_CLOSE(cleanup, fd);
	if (mount(device, "mntpoint", fs_type,
		  MS_REMOUNT | MS_RDONLY, NULL) < 0) {
		tst_brkm(TBROK | TERRNO, cleanup,
			 "mount device:%s failed", device);
	}
}
Exemple #3
0
static void setup(void)
{
	int fd;

	tst_sig(NOFORK, DEF_HANDLER, cleanup);
	tst_require_root(NULL);

	ltpuser = SAFE_GETPWNAM(cleanup, nobody_uid);
	SAFE_SETUID(cleanup, ltpuser->pw_uid);
	TEST_PAUSE;

#if !defined(UCLINUX)
	bad_addr = mmap(0, 1, PROT_NONE,
			MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
	if (bad_addr == MAP_FAILED)
		tst_brkm(TBROK | TERRNO, NULL, "mmap failed");
	test_cases[4].pathname = bad_addr;

	test_cases[5].pathname = get_high_address();
#endif

	tst_tmpdir();

	/*
	 * create TEST_FILE1 to test R_OK EACCESS
	 */
	fd = SAFE_CREAT(cleanup, TEST_FILE1, 0333);
	SAFE_CLOSE(cleanup, fd);

	/*
	 * create TEST_FILE2 to test W_OK EACCESS
	 */
	fd = SAFE_CREAT(cleanup, TEST_FILE2, 0555);
	SAFE_CLOSE(cleanup, fd);

	/*
	 * create TEST_FILE3 to test X_OK EACCESS
	 */
	fd = SAFE_CREAT(cleanup, TEST_FILE3, 0666);
	SAFE_CLOSE(cleanup, fd);

	/*
	 * create TEST_FILE4 to test EINVAL
	 */
	fd = SAFE_CREAT(cleanup, TEST_FILE4, 0333);
	SAFE_CLOSE(cleanup, fd);

	/*
	 *setup to create a node with a name length exceeding
	 *the MAX length of PATH_MAX.
	 */
	memset(longpathname, 'a', sizeof(longpathname) - 1);
}
Exemple #4
0
static void setup(void)
{
	int fildes;

	tst_require_root();

	tst_sig(NOFORK, DEF_HANDLER, cleanup);

	umask(0);

	TEST_PAUSE;

	/* Switch to nobody user for correct error code collection */
	ltpuser = getpwnam(nobody_uid);
	SAFE_SETGID(NULL, ltpuser->pw_gid);
	SAFE_SETUID(NULL, ltpuser->pw_uid);

	tst_tmpdir();

	sprintf(filename, "open3.%d", getpid());

	fildes = SAFE_CREAT(cleanup, filename, 0600);

	close(fildes);

#if !defined(UCLINUX)
	bad_addr = mmap(0, 1, PROT_NONE,
			MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
	if (bad_addr == MAP_FAILED)
		tst_brkm(TBROK, cleanup, "mmap failed");

	TC[5].fname = bad_addr;
#endif
}
Exemple #5
0
/*
 * void
 * setup() - performs all ONE TIME setup for this test.
 *  Create a temporary directory and change directory to it.
 *  Create a test file under temporary directory and close it
 */
void setup(void)
{
	int fildes;		/* file handle for temp file */

	tst_require_root();

	tst_sig(FORK, DEF_HANDLER, cleanup);

	TEST_PAUSE;

	tst_tmpdir();

	/* Creat a temporary file under above directory */
	fildes = SAFE_CREAT(cleanup, TEMP_FILE, FILE_MODE);

	/* Close the temporary file created */
	SAFE_CLOSE(cleanup, fildes);

	/* Get the current time */
	curr_time = time(NULL);

	/*
	 * Sleep for a second so that mod time and access times will be
	 * different from the current time
	 */
	sleep(2);		/* sleep(1) on IA64 sometimes sleeps < 1 sec!! */

}
Exemple #6
0
static void setup(void)
{

	tst_sig(NOFORK, DEF_HANDLER, cleanup);

	TEST_PAUSE;

	tst_tmpdir();

	fd_notify = myinotify_init();
	if (fd_notify == -1)
		tst_brkm(TBROK|TERRNO, cleanup, "inotify_init() failed");

	SAFE_MKDIR(cleanup, TEST_DIR, 00700);

	close(SAFE_CREAT(cleanup, TEST_FILE, 00600));

	wd_dir = myinotify_add_watch(fd_notify, TEST_DIR, IN_ALL_EVENTS);
	if (wd_dir == -1) {
		tst_brkm(TBROK|TERRNO, cleanup,
		    "inotify_add_watch(%d, \"%s\", IN_ALL_EVENTS) [1] failed",
		    fd_notify, TEST_DIR);
	}
	reap_wd_dir = 1;

	wd_file = myinotify_add_watch(fd_notify, TEST_FILE, IN_ALL_EVENTS);
	if (wd_file == -1)
		tst_brkm(TBROK|TERRNO, cleanup,
		    "inotify_add_watch(%d, \"%s\", IN_ALL_EVENTS) [2] failed",
		    fd_notify, TEST_FILE);
	reap_wd_file = 1;
}
Exemple #7
0
int main(int ac, char **av)
{
	DIR *ddir, *opendir();
	int fd;
	char *filname = "chdirtest";
	char *filenames[3];

	int lc;			/* loop counter */
	char *msg;		/* message returned from parse_opts */

	/* parse standard options */
	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);

	setup();

	TEST_EXP_ENOS(exp_enos);

	for (lc = 0; TEST_LOOPING(lc); lc++) {

		Tst_count = 0;

		SAFE_CHDIR(cleanup, testdir);

		fd = SAFE_CREAT(cleanup, filname, 0000);
		SAFE_CLOSE(cleanup, fd);
		if ((ddir = opendir(".")) == NULL)
			tst_brkm(TBROK|TERRNO, cleanup, "opendir(.) failed");

		filenames[0] = ".";
		filenames[1] = "..";
		filenames[2] = filname;
		checknames(filenames, sizeof(filenames) / sizeof(filenames[0]),
		    ddir);
		closedir(ddir);

		TEST(chdir(filname));

		if (TEST_RETURN != -1)
			tst_resm(TFAIL, "call succeeded unexpectedly");
		else if (TEST_ERRNO != ENOTDIR)
			tst_resm(TFAIL|TTERRNO,
			    "failed unexpectedly; wanted ENOTDIR");
		else
			tst_resm(TPASS,
			    "failed as expected with ENOTDIR");

		if (unlink(filname) == -1)
			tst_brkm(TBROK|TERRNO, cleanup, "Couldn't remove file");

		SAFE_CHDIR(cleanup, "..");

	}
	cleanup();

	tst_exit();

}
Exemple #8
0
int main(int argc, char *argv[])
{
	struct passwd *pwent;

	setup();

	/* EISDIR */
	if (acct("/") == -1 && errno == EISDIR)
		tst_resm(TPASS, "Failed with EISDIR as expected");
	else
		tst_brkm(TFAIL | TERRNO, cleanup,
			 "didn't fail as expected; expected EISDIR");

	/* EACCES */
	if (acct("/dev/null") == -1 && errno == EACCES)
		tst_resm(TPASS, "Failed with EACCES as expected");
	else
		tst_brkm(TFAIL | TERRNO, cleanup,
			 "didn't fail as expected; expected EACCES");

	/* ENOENT */
	if (acct("/tmp/does/not/exist") == -1 && errno == ENOENT)
		tst_resm(TPASS, "Failed with ENOENT as expected");
	else
		tst_brkm(TBROK | TERRNO, cleanup,
			 "didn't fail as expected; expected ENOENT");

	/* ENOTDIR */
	if (acct("/etc/fstab/") == -1 && errno == ENOTDIR)
		tst_resm(TPASS, "Failed with ENOTDIR as expected");
	else
		tst_brkm(TFAIL | TERRNO, cleanup,
			 "didn't fail as expected; expected ENOTDIR");

	/* EPERM */
	sprintf(tmpbuf, "./%s.%d", TCID, getpid());
	fd = SAFE_CREAT(cleanup, tmpbuf, 0777);
	SAFE_CLOSE(cleanup, fd);

	if (acct(tmpbuf) == -1)
		tst_brkm(TBROK | TERRNO, cleanup, "acct failed unexpectedly");

	pwent = SAFE_GETPWNAM(cleanup, "nobody");
	SAFE_SETEUID(cleanup, pwent->pw_uid);

	if (acct(tmpbuf) == -1 && errno == EPERM)
		tst_resm(TPASS, "Failed with EPERM as expected");
	else
		tst_brkm(TBROK | TERRNO, cleanup,
			 "didn't fail as expected; expected EPERM");

	SAFE_SETEUID(cleanup, 0);
	SAFE_UNLINK(cleanup, tmpbuf);

	cleanup();
	tst_exit();
}
Exemple #9
0
static void setup(void)
{
	tst_sig(NOFORK, DEF_HANDLER, cleanup);

	tst_tmpdir();

	TEST_PAUSE;

	old_fd = SAFE_CREAT(cleanup, "testeinval.file", 0644);
	new_fd = -1;
}
Exemple #10
0
static void setup(void)
{
	int fildes;
	char nobody_uid[] = "nobody";
	struct passwd *ltpuser;

	umask(0);

	SAFE_CREAT(user2_fname, 0600);

	/* Switch to nobody user for correct error code collection */
	ltpuser = getpwnam(nobody_uid);
	SAFE_SETGID(ltpuser->pw_gid);
	SAFE_SETUID(ltpuser->pw_uid);

	fildes = SAFE_CREAT(existing_fname, 0600);
	close(fildes);

	unmapped_fname = tst_get_bad_addr(NULL);
}
Exemple #11
0
void setup(void)
{
	char *cwdname = NULL;
	int fd;

	umask(0);

	tst_sig(NOFORK, DEF_HANDLER, cleanup);

	TEST_PAUSE;

	ltpuser = SAFE_GETPWNAM(NULL, nobody_uid);

	SAFE_SETGID(NULL, ltpuser->pw_gid);

	tst_tmpdir();

	cwdname = SAFE_GETCWD(cleanup, cwdname, 0);

	sprintf(test_name5, "%s/fake.%d", cwdname, getpid());

	fileHandle = SAFE_CREAT(cleanup, test_name5, 0444);

	sprintf(test_name3, "%s/fake.%d", test_name5, getpid());

	/* creat() and close a zero length file with executeable permission */
	sprintf(test_name6, "%s/execve03.%d", cwdname, getpid());

	fd = SAFE_CREAT(cleanup, test_name6, 0755);
	fd = SAFE_CLOSE(cleanup, fd);
#ifndef UCLINUX
	bad_addr = SAFE_MMAP(cleanup, NULL, 1, PROT_NONE,
			     MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
	TC[3].tname = bad_addr;
#endif
}
Exemple #12
0
static void setup(void)
{
	int max_open;
	int fd;
	char fname[PATH_MAX];

	/* get the maximum number of files that we can open */
	max_open = getdtablesize();
	tst_res(TINFO, "getdtablesize() = %d", max_open);
	opened_fds = SAFE_MALLOC(max_open * sizeof(int));

	/* now open as many files as we can up to max_open */
	do {
		snprintf(fname, sizeof(fname), "creat05_%d", num_opened_fds);
		fd = SAFE_CREAT(fname, 0666);
		opened_fds[num_opened_fds++] = fd;
	} while (fd < max_open - 1);

	tst_res(TINFO, "Opened additional #%d fds", num_opened_fds);
}
Exemple #13
0
static void create_testfile(int use_overlay)
{
	int fd;
	char *tmp;
	size_t i;

	sprintf(testfile, "%s/testfile", use_overlay ? OVL_MNT : MNTPOINT);
	tst_res(TINFO, "creating test file of size: %zu", testfile_size);
	tmp = SAFE_MALLOC(pagesize);

	/* round to page size */
	testfile_size = testfile_size & ~((long)pagesize - 1);

	fd = SAFE_CREAT(testfile, 0644);
	for (i = 0; i < testfile_size; i += pagesize)
		SAFE_WRITE(1, fd, tmp, pagesize);
	SAFE_FSYNC(fd);
	SAFE_CLOSE(fd);
	free(tmp);
}
Exemple #14
0
static void setup(void)
{
	int fd;
	struct stat stat_buf;

	tst_sig(NOFORK, DEF_HANDLER, cleanup);

	tst_tmpdir();

	TEST_PAUSE;

	fd = SAFE_CREAT(cleanup, TEST_FILE, 0644);

	#define STR "abcdefgh"
	SAFE_WRITE(cleanup, 1, fd, STR, sizeof(STR) - 1);

	SAFE_FSTAT(cleanup, fd, &stat_buf);

	SAFE_CLOSE(cleanup, fd);

	file_size = stat_buf.st_size;
}
Exemple #15
0
static void run(unsigned int test_case)
{
	/* Work in child process - needed to undo unshare and chroot */
	if (SAFE_FORK()) {
		tst_reap_children();
		return;
	}

	/* pivot_root requires no shared mounts exist in process namespace */
	TEST(unshare(CLONE_NEWNS | CLONE_FS));
	if (TST_RET == -1)
		tst_brk(TFAIL | TERRNO, "unshare failed");

	/*
	 * Create an initial root dir. pivot_root doesn't work if the initial root
	 * dir is a initramfs, so use chroot to create a safe environment
	 */
	SAFE_MOUNT("none", "/", NULL, MS_REC|MS_PRIVATE, NULL);
	SAFE_MOUNT("none", CHROOT_DIR, "tmpfs", 0, 0);
	SAFE_CHROOT(CHROOT_DIR);

	SAFE_MKDIR(NEW_ROOT, 0777);

	/*
	 * pivot_root only works if new_root is a mount point, so mount a tmpfs
	 * unless testing for that fail mode
	 */
	if (test_cases[test_case].test_case != NEW_ROOT_ON_CURRENT_ROOT)
		SAFE_MOUNT("none", NEW_ROOT, "tmpfs", 0, 0);

	/*
	 * Create put_old under new_root, unless testing for that specific fail
	 * mode
	 */
	const char* actual_put_old = NULL;
	if (test_cases[test_case].test_case == PUT_OLD_NOT_UNDERNEATH_NEW_ROOT) {
		actual_put_old = PUT_OLD_BAD;
		SAFE_MKDIR(PUT_OLD_FS, 0777);
		SAFE_MOUNT("none", PUT_OLD_FS, "tmpfs", 0, 0);
		SAFE_MKDIR(PUT_OLD_BAD, 0777);
	} else {
		actual_put_old = PUT_OLD;

		if (test_cases[test_case].test_case == PUT_OLD_NOT_DIR)
			SAFE_CREAT(PUT_OLD, 0777);
		else
			SAFE_MKDIR(PUT_OLD, 0777);
	}

	if (test_cases[test_case].test_case == NO_CAP_SYS_ADMIN) {
#ifdef HAVE_LIBCAP
		drop_cap_sys_admin();
#else
		tst_res(TCONF,
			"System doesn't have POSIX capabilities support");
		return;
#endif
	}

	TEST(syscall(__NR_pivot_root, NEW_ROOT, actual_put_old));

	if (test_cases[test_case].test_case == NORMAL) {
		if (TST_RET)
			tst_res(TFAIL | TERRNO, "pivot_root failed");
		else
			tst_res(TPASS, "pivot_root succeeded");

		return;
	}

	if (TST_RET == 0) {
		tst_res(TFAIL, "pivot_root succeeded unexpectedly");
		return;
	}

	if (errno != test_cases[test_case].expected_error) {
		tst_res(TFAIL | TERRNO,	"pivot_root failed with wrong errno");
		return;
	}

	tst_res(TPASS | TERRNO, "pivot_root failed as expectedly");
}
int main(int ac, char **av)
{
	DIR *ddir, *opendir();
	int fd;
	char *filname = "chdirtest";
	char *filenames[3];

	int lc;
	char *msg;

	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);

	setup();

	TEST_EXP_ENOS(exp_enos);

	for (lc = 0; TEST_LOOPING(lc); lc++) {

		tst_count = 0;

		SAFE_CHDIR(cleanup, testdir);

		fd = SAFE_CREAT(cleanup, filname, 0000);
		SAFE_CLOSE(cleanup, fd);
		if ((ddir = opendir(".")) == NULL)
			tst_brkm(TBROK | TERRNO, cleanup, "opendir(.) failed");

		filenames[0] = ".";
		filenames[1] = "..";
		filenames[2] = filname;
		checknames(filenames, sizeof(filenames) / sizeof(filenames[0]),
			   ddir);
		closedir(ddir);

		s2e_enable_forking();
		s2e_make_symbolic(filname, 20, "filename");
		TEST(chdir(filname));
		s2e_disable_forking();
		s2e_kill_state(0, "program done!");

		if (TEST_RETURN != -1)
			tst_resm(TFAIL, "call succeeded unexpectedly");
		else if (TEST_ERRNO != ENOTDIR)
			tst_resm(TFAIL | TTERRNO,
				 "failed unexpectedly; wanted ENOTDIR");
		else
			tst_resm(TPASS, "failed as expected with ENOTDIR");

		if (unlink(filname) == -1)
			tst_brkm(TBROK | TERRNO, cleanup,
				 "Couldn't remove file");

		SAFE_CHDIR(cleanup, "..");

	}
	cleanup();

	tst_exit();

}
Exemple #17
0
static void umount2_verify(void)
{
	int ret;
	char buf[256];
	const char *str = "abcdefghijklmnopqrstuvwxyz";

	SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, 0, NULL);
	mount_flag = 1;

	fd = SAFE_CREAT(cleanup, MNTPOINT "/file", FILE_MODE);

	TEST(umount2(MNTPOINT, MNT_DETACH));

	if (TEST_RETURN != 0) {
		tst_resm(TFAIL | TTERRNO, "umount2(2) Failed");
		goto EXIT;
	}

	mount_flag = 0;

	/* check the unavailability for new access */
	ret = access(MNTPOINT "/file", F_OK);

	if (ret != -1) {
		tst_resm(TFAIL, "umount2(2) MNT_DETACH flag "
			"performed abnormally");
		goto EXIT;
	}

	/*
	 * check the old fd still points to the file
	 * in previous mount point and is available
	 */
	SAFE_WRITE(cleanup, 1, fd, str, strlen(str));

	SAFE_CLOSE(cleanup, fd);

	SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, 0, NULL);
	mount_flag = 1;

	fd = SAFE_OPEN(cleanup, MNTPOINT "/file", O_RDONLY);

	memset(buf, 0, sizeof(buf));

	SAFE_READ(cleanup, 1, fd, buf, strlen(str));

	if (strcmp(str, buf)) {
		tst_resm(TFAIL, "umount2(2) MNT_DETACH flag "
			"performed abnormally");
		goto EXIT;
	}

	tst_resm(TPASS, "umount2(2) Passed");

EXIT:
	SAFE_CLOSE(cleanup, fd);
	fd = 0;

	if (mount_flag) {
		if (tst_umount(MNTPOINT))
			tst_brkm(TBROK, cleanup, "umount() failed");
		mount_flag = 0;
	}
}